-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
161 lines (124 loc) · 4.75 KB
/
Copy pathcontroller.py
File metadata and controls
161 lines (124 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import base64
import pickle
from typing import List
import numpy as np
import requests
from prototype.model_tools import ToyModel, WeightSlice
class Controller:
"""
Model partitioning controller with safe serialization.
Replaces pickle-based transport with binary format for security.
"""
def __init__(self, workers):
"""
Initialize controller with worker URLs.
Args:
workers: List of worker URLs (e.g., ["http://127.0.0.1:8001", "http://127.0.0.1:8002"])
"""
self.workers = workers
# Connection pooling for performance
self.session = requests.Session()
def partition_model(
self, model: ToyModel, num_slices: int = 2
) -> List[WeightSlice]:
"""
Partition model into balanced slices.
Algorithm: Balanced partitioning ensures even distribution of layers.
Args:
model: ToyModel instance to partition
num_slices: Number of slices (e.g., 2 for bipartition)
Returns:
List of WeightSlice objects in execution order
"""
L = len(model.weights)
# Balanced partitioning using ceiling division
slice_size = (L + num_slices - 1) // num_slices
slices = []
for i in range(num_slices):
start = i * slice_size
end = min(L, start + slice_size)
if start >= L:
break
sub = model.slice(start, end)
slices.append(sub)
return slices
def preload_slices(
self, slices: List[WeightSlice], encrypt: bool = False
) -> List[tuple]:
"""
Preload model slices to workers.
Args:
slices: List of WeightSlice objects in execution order
encrypt: Whether to encrypt weights during transport
Returns:
List of (slice_id, worker_url) tuples for distributed execution
"""
assigned = []
for i, slice_obj in enumerate(slices):
# Round-robin assignment to workers
w = self.workers[i % len(self.workers)]
# Serialize weights safely (no pickle)
blob = slice_obj.to_bytes()
manifest = {
"start": slice_obj.start_layer,
"end": slice_obj.end_layer,
"version": slice_obj.version,
}
payload = {
"slice_id": f"slice_{slice_obj.start_layer}_{slice_obj.end_layer}",
"manifest": manifest,
"weights_b64": base64.b64encode(blob).decode("ascii"),
"version": slice_obj.version,
}
# Retry with exponential backoff for transient failures
max_attempts = 3
backoff_base = 0.1
for attempt in range(1, max_attempts + 1):
try:
r = self.session.post(f"{w}/preload", json=payload, timeout=10)
r.raise_for_status()
break
except Exception as e:
if attempt == max_attempts:
raise
sleep_t = backoff_base * (2 ** (attempt - 1))
import time
time.sleep(sleep_t)
assigned.append((payload["slice_id"], w))
return assigned
def run_distributed(
self, assigned: List[tuple], x: np.ndarray, encrypt: bool = False
) -> np.ndarray:
"""
Run distributed inference across workers.
Args:
assigned: List of (slice_id, worker_url) tuples in execution order
x: Input tensor as numpy array
encrypt: Whether to encrypt activations during transport
Returns:
Output tensor after passing through all slices
"""
from prototype.model_tools import ToyModel
current = x
for slice_id, w in assigned:
try:
# Prepare input for this slice
if encrypt:
raise NotImplementedError("Encryption not yet implemented")
else:
b64_input = base64.b64encode(current.tobytes()).decode("ascii")
payload = {
"slice_id": slice_id,
"input_b64": b64_input,
"version": "v1.0",
}
# Execute on worker
r = self.session.post(f"{w}/execute", json=payload, timeout=30)
r.raise_for_status()
# Get output
out_b64 = r.json()["output_b64"]
current = np.frombuffer(base64.b64decode(out_b64), dtype=np.float32)
except Exception as e:
print(f"Error executing slice {slice_id}: {e}")
raise
return current