Run 35B MoE models in ~2.5 GB of RAM. Weights stream from SSD; a trained router predicts loads a step ahead. Mac, iPhone, Android, Windows supported
Python
fix(streaming): give the hot stack its zero overflow row
Demetrios Agourakis1e6f8c66545f
diff --git a/src/edge0/streaming/layer.py b/src/edge0/streaming/layer.pyindex 9ac9199..a82027e 100644--- a/src/edge0/streaming/layer.py+++ b/src/edge0/streaming/layer.py@@ -327,7 +327,7 @@ class StreamingSwitchGLU:b = {}for (proj, part), buf in self._hot_backing.items():sh = shape[(proj, part)]- per = buf.size // len(self._hot_key)+ per = buf.size // (len(self._hot_key) + 1) # + zero rowsl = buf[idx * per:(idx + 1) * per]if part == "weight":b[(proj, part)] = core.array(@@ -984,7 +984,11 @@ class StreamingSwitchGLU:raw = self._shard_for(name).raw(name)per = raw.size // self.num_expertsrows = [raw[e * per:(e + 1) * per] for e in hot]- # concatenated numpy backing (page cache, not GPU)+ # concatenated numpy backing (page cache, not GPU), plus+ # one all-zero row: the prefill path sends misses to row+ # n_hot ("overflow row") and needs it to contribute zero.+ # Without it that gather reads past the end of the stack.+ rows.append(np.zeros_like(rows[0]))backing[(proj, part)] = np.concatenate(rows)self._hot_backing = backingself._hot_key = key@@ -1000,7 +1004,7 @@ class StreamingSwitchGLU:returnw = {}shape0 = self._bundle_shape- n_e = len(self._hot_key)+ n_e = len(self._hot_key) + 1 # + zero overflow rowfor (proj, part), buf in self._hot_backing.items():shape = shape0[(proj, part)]if part == "weight":diff --git a/tests/test_streaming_math.py b/tests/test_streaming_math.pyindex e77154e..eeb0f19 100644--- a/tests/test_streaming_math.py+++ b/tests/test_streaming_math.py@@ -274,6 +274,51 @@ def test_hot_stack_matches_exact(layer):assert mx.allclose(out, exact).item(), "hot path != exact"+def _hot_layer(fuse_gu, td, hot):+ import os+ path = os.path.join(td, "w.safetensors")+ _write_shard(path, fuse_gu)+ s = StreamingSwitchGLU([SafetensorsMmap(path)], 0, _spec(fuse_gu),+ _options(staged=True, hot_per_layer=0),+ shared_cache=SharedExpertCache(64))+ s._hot_counts = {e: 1.0 for e in hot}+ s.load_hot_layer(n_hot=len(hot))+ s.materialize_hot()+ return s+++def test_hot_stack_has_zero_overflow_row(layer):+ """Prefill misses are routed to row len(hot_key) of the hot stack and+ must contribute zero, so that row has to exist and be all zeros --+ otherwise gather_qmm reads past the end of the stack."""+ lay, _, _ = layer+ import tempfile+ with tempfile.TemporaryDirectory() as td:+ s = _hot_layer(lay._fuse_gu, td, hot=[0, 2, 5, 7])+ n = len(s._hot_key)+ for (proj, part), arr in s._hot_weights.items():+ assert arr.shape[0] == n + 1, (proj, part, arr.shape)+ assert not mx.any(arr[n]).item(), (proj, part)+++def test_hot_stack_with_misses_matches_exact(layer):+ """Half the routed experts are outside the hot stack: the misses go+ through the exact scatter-add correction and the result must still+ equal the exact path."""+ lay, _, _ = layer+ import tempfile+ with tempfile.TemporaryDirectory() as td:+ s = _hot_layer(lay._fuse_gu, td, hot=[0, 2, 5, 7])+ x = mx.random.normal((1, 4, DIM)).astype(mx.float16)+ inds = _inds(tokens=4)+ assert set(inds.reshape(-1).tolist()) - set(s._hot_key), \+ "test needs at least one miss"+ out = s(x, inds)+ exact = lay(x, inds)+ assert out.shape == exact.shape+ assert mx.allclose(out, exact).item(), "hot path with misses != exact"++def test_double_buffered_swap(layer):"""stage_experts fills next; swap promotes; consume uses the new set."""lay, _, _ = layer