Skip to content

GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold

Python
4.7 KB
Raw Blame
diff --git tensorfold/families/glm5_next/cuda/decode.py tensorfold/families/glm5_next/cuda/decode.py
index 1e6d712..40aa44e 100644
--- tensorfold/families/glm5_next/cuda/decode.py
+++ tensorfold/families/glm5_next/cuda/decode.py
@@ -113,12 +113,10 @@ class Engine:
 
             kda_mod.reserve(max(max_rows, prefill_rows), tp.of(w).lin_heads, w.device)
         self.buf = Buffers(w, max_rows, capacity)
-        # the row split's pad rows past a full chunk (three ranks: 1; ``hcsplit.pad_rows``)
-        pad = 0
-        if split is not None and split.split:
-            from .hcsplit import pad_rows
+        # the row split's pad rows past a full chunk (``hcsplit.pad_rows``: 0 at two and four ranks, 1 at three)
+        from .hcsplit import buffer_rows
 
-            pad = pad_rows(prefill_rows, w.world)
+        pad = buffer_rows(prefill_rows, w.world, split) - prefill_rows
         self.pbuf = Buffers(w, prefill_rows, capacity, prefill=True, pad=pad)
         if taps:
             self.buf.set_taps(tuple(taps), w.cfg.hidden)         # before any graph capture
diff --git tensorfold/families/glm5_next/cuda/engine.py tensorfold/families/glm5_next/cuda/engine.py
index 96ee202..a6332ba 100644
--- tensorfold/families/glm5_next/cuda/engine.py
+++ tensorfold/families/glm5_next/cuda/engine.py
@@ -548,12 +548,18 @@ class GlmEngine:
                                                    if name.startswith("model.visual.") else text_weights(name, info))
 
         from .exl3_mm import prompt_kernels
+        from .hcsplit import buffer_rows
         from .latent import SPARSE_ONEPASS
 
+        # the prompt buffers' rows, the split's pad rows included (``hcsplit.buffer_rows``; the prompt chunk's rows at
+        # two and four ranks)
+        prompt_buffer_rows = buffer_rows(prefill_rows, world, self.split)
+
         def geometry(text):
             from tensorfold.cuda.capacity import Geometry
 
-            g = mla_geometry(text, world, MAX_ROWS, share=share, minimum_slots=DENSE_CAPACITY, latent=LATENT, prefill_rows=prefill_rows,
+            g = mla_geometry(text, world, MAX_ROWS, share=share, minimum_slots=DENSE_CAPACITY, latent=LATENT,
+                             prefill_rows=prompt_buffer_rows,
                              mtp=self.mtp_on, onepass=SPARSE_ONEPASS, exl3_prompt=prompt_kernels(),
                              prompt_split_k=cfg.quant == "exl3" and dense_kind() != "q4", kv=kv)
             # --parallel: every stream slot past the first (KDA states, conv windows, window scratch); the batched
diff --git tensorfold/families/glm5_next/cuda/hcsplit.py tensorfold/families/glm5_next/cuda/hcsplit.py
index 3c14726..79b4f8c 100644
--- tensorfold/families/glm5_next/cuda/hcsplit.py
+++ tensorfold/families/glm5_next/cuda/hcsplit.py
@@ -58,14 +58,25 @@ def _int(env, name: str, default: int, lo: int, hi: int) -> int:
 
 def pad_rows(rows: int, world: int) -> int:
     """Rows a prompt buffer of ``rows`` rows holds past them for the split's pad rows: a chunk of ``rows`` rows rounded
-    up to whole shares a rank (``HcSplit.padded``). 0 at two and four ranks (prompt chunks are a multiple of 64 rows);
-    1 at three, where a 2,048-row chunk splits as 3 x 683 = 2,049 rows: without that row every full chunk failed
-    ``applies`` and ran unsplit (an all-gather of every rank's whole partial at each of the 90 sites, the glue on
-    every row, no overlap)."""
+    up to whole shares a rank (``HcSplit.padded``), so that a full chunk, and every shorter one, runs split at any
+    world size. Prompt chunks are a multiple of 64 rows (TF_GLM_PREFILL_ROWS), so this is 0 wherever the world size
+    divides 64 (two and four ranks: 2,048 = 2 x 1,024 = 4 x 512) and ``-rows % 3`` at three (2,048 = 3 x 683 - 1:
+    one row; 2,112: none). Without that row every full chunk at three ranks failed ``applies`` and ran unsplit (an
+    all-gather of every rank's whole partial at each of the 90 sites, the glue on every row, no overlap)."""
 
     return -int(rows) % int(world)
 
 
+def buffer_rows(rows: int, world: int, settings: "SplitSettings | None") -> int:
+    """Rows the prompt buffers of ``rows``-row chunks hold (``forward.Buffers``' ``rows + pad``): ``rows`` plus the
+    split's pad rows when TF_GLM_HC_SPLIT is on past one rank, else ``rows``. The engine's buffers and its memory
+    estimate both take it, so they agree; at two and four ranks it is ``rows`` (the same buffers and estimate)."""
+
+    if settings is None or not settings.split or int(world) < 2:
+        return int(rows)
+    return int(rows) + pad_rows(rows, world)
+
+
 @dataclass(frozen=True)
 class SplitSettings:
     """What TF_GLM_HC_SPLIT / TF_GLM_PREFILL_OVERLAP ask for; both ranks must agree (the startup comparison)."""