MCPcopy Create free account
hub / github.com/Luce-Org/lucebox-hub / load_routing_data

Function load_routing_data

scripts/train_predictor.py:86–132  ·  view source on GitHub ↗

Load a single binary routing file into numpy arrays.

(path, hidden_dim)

Source from the content-addressed store, hash-verified

84
85
86def load_routing_data(path, hidden_dim):
87 """Load a single binary routing file into numpy arrays."""
88 data = Path(path).read_bytes()
89 offset = 0
90 layers = []
91 hiddens = []
92 experts = []
93
94 while offset < len(data) - 8: # at least header
95 layer_idx = struct.unpack_from('<i', data, offset)[0]
96 offset += 4
97 K = struct.unpack_from('<i', data, offset)[0]
98 offset += 4
99
100 # Check for truncated record (e.g. server killed mid-write)
101 needed = hidden_dim * 4 + K * 4
102 if offset + needed > len(data):
103 break
104
105 h = np.frombuffer(data, dtype=np.float32, count=hidden_dim, offset=offset).copy()
106 offset += hidden_dim * 4
107
108 ei = np.frombuffer(data, dtype=np.int32, count=K, offset=offset).copy()
109 offset += K * 4
110
111 layers.append(layer_idx)
112 hiddens.append(h)
113 experts.append(ei)
114
115 # Empty or fully-truncated file: return well-formed empty arrays rather
116 # than crashing in np.stack([]) / max([]).
117 if not experts:
118 return (np.array([], dtype=np.int32),
119 np.zeros((0, hidden_dim), dtype=np.float32),
120 np.zeros((0, 0), dtype=np.int32), 0)
121
122 layers = np.array(layers, dtype=np.int32)
123 hiddens = np.stack(hiddens)
124 max_K = max(len(e) for e in experts)
125 # Pad with -1 (not 0): 0 is a valid expert index, so a 0 sentinel would be
126 # indistinguishable from a real top-0 routing and corrupt labels/eval when
127 # K varies across records. Consumers filter out negative indices.
128 experts_padded = np.full((len(experts), max_K), -1, dtype=np.int32)
129 for i, e in enumerate(experts):
130 experts_padded[i, :len(e)] = e
131
132 return layers, hiddens, experts_padded, max_K
133
134
135def load_multiple_routing_files(paths, hidden_dim, num_layers, seed=42):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected