GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold
Python
Fix NV_ERR_NO_MEMORY on a fresh conversation after a long one (patch 0078)
m-naoki-mb9fa2ff98a78
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f64aec0..1d75ef5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -3,6 +3,21 @@Every change to this recipe, newest first. Each release names the image it serves: `scripts/prepare.sh` pulls`ghcr.io/miaai-lab/glm-5.3-flash-exl3-2x-dgx-sparks-tensorfold` by the digest pinned in `scripts/config.sh`.+## Unreleased++Image: `v0.6.0-dcc41707fc76` is the tag the patch set hashes to (`1692d2df78d2` at v1.7.1); maintainers build it, publish it and pin its digest in `scripts/config.sh` at release. 77 patches.++### Fixed+- **A fresh conversation after a long one no longer runs both ranks out of memory** (`_take_over`, patch+ `0078-glm-take-over-decide-then-copy`, the same change as upstream ashhart/TensorFold#421). When a conversation+ sharing no prefix with the last one arrived after a long conversation, both ranks died with `NV_ERR_NO_MEMORY`; it+ also happened with the 1M window and `KV_POOL_GIB=3`. `_take_over` cloned every kept state and only then dropped the+ ones over the budget, so reserved memory grew to the sum of all their sizes. It now works out which states stay and+ clones only those; the states that stay and the replies are the same by design (200 random cases against the old+ walk). After a 256k, 21-turn conversation, a fresh one on 2 Sparks: `save_rows` 22 -> 1, reserved +14.8 -> +1.4 GiB,+ `take_over` 3.3 -> 0.10 s; before, the driver logged `NV_ERR_NO_MEMORY` 22 times. After: 3 fresh boots, 5 cycles+ of a long task then a fresh one, 48 benchmark attempts, no allocation failure; pass counts match the unpatched build where it had the same trials (15 of 21 graded at low effort on both; 15 of 21 at high effort against 10 of 11 before). These runs used the v1.4 patch set; patches 0071, 0074 and 0077 change the shared pool and the kept cap (`multi.py`), not `_take_over`, so the patch applies to v1.7.1 unchanged.+## v1.7.1 (2026-10-05): a new agent run resumes its system prompt again once the kept cap is fullImage: `v0.6.0-1692d2df78d2` (`sha256:a8067cd7e14c14fa83d1dbed60261428f6d1737cec4554445573354af040dd7c`), 76 patches, for two and three Sparks (v1.7's image plus `0077`).diff --git a/CREDITS.md b/CREDITS.mdindex 1d47ccc..bb0af4e 100644--- a/CREDITS.md+++ b/CREDITS.md@@ -109,6 +109,9 @@ checkpoint's ShapleyMcg attribution and the drafter's license).- `0077-glm-kept-cap-shared-by-recency` (a new agent run keeps its system prompt's state once the kept-state cap isfull): the diagnosis, and the fix of evicting shared-prefix states by recency only, by[meleesciony](https://github.com/meleesciony) in [issue #75](https://github.com/MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold/issues/75); the code is our own.+- `0078-glm-take-over-decide-then-copy` (a fresh conversation after a long one clones only the kept states that stay):+ by [m-naoki-m](https://github.com/m-naoki-m), also proposed upstream as+ [TensorFold PR #421](https://github.com/ashhart/TensorFold/pull/421).- Every patch, except the parts credited above: by MiaAI-Lab, developed with[Claude Code](https://claude.com/claude-code), under the Apache License 2.0; the TensorFold code the patches modify orquote as context stays under TensorFold's licenses (Apache 2.0, and MIT for code written before v0.6.0; seediff --git a/README.md b/README.mdindex 06932fb..14829e4 100644--- a/README.md+++ b/README.md@@ -14,7 +14,7 @@Serve **GLM-5.3-Flash** from two NVIDIA DGX Sparks (GB10, 128 GB each, linked by their ConnectX-7 ports) through anOpenAI-compatible API, with **4 concurrent requests**, the model's full **1,048,576-token context** and **image andvideo input**. It runs [TensorFold](https://github.com/ashhart/TensorFold) v0.6.0 on both Sparks (one rank on each)-in NVIDIA's PyTorch container, plus 76 patches (65 of v1.4, 3 for 3 Sparks, experimental, 1 for up to 8 requests at once, 1 for stopping serial requests, 5 of v1.6: a shared system prompt keeps each conversation's history, the display reservation in the pool and the pool compacting before it evicts, by [ezoushen](https://github.com/ezoushen); queued requests whose client left dropped at once, by [desy0305](https://github.com/desy0305); `<|assistant|>` ending a reply; 1 of v1.7.1: a new agent run keeps its system prompt's state once the kept-state cap is full, diagnosed and the fix proposed by [meleesciony](https://github.com/meleesciony) in [issue #75](https://github.com/MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold/issues/75)): DFlash2 and copy drafts, 4-bit dense weights, an FP8 KV cache,+in NVIDIA's PyTorch container, plus 77 patches (65 of v1.4, 3 for 3 Sparks, experimental, 1 for up to 8 requests at once, 1 for stopping serial requests, 5 of v1.6: a shared system prompt keeps each conversation's history, the display reservation in the pool and the pool compacting before it evicts, by [ezoushen](https://github.com/ezoushen); queued requests whose client left dropped at once, by [desy0305](https://github.com/desy0305); `<|assistant|>` ending a reply; 1 of v1.7.1: a new agent run keeps its system prompt's state once the kept-state cap is full, diagnosed and the fix proposed by [meleesciony](https://github.com/meleesciony) in [issue #75](https://github.com/MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold/issues/75); and 1 fix since v1.7.1: a fresh conversation after a long one no longer runs out of memory): DFlash2 and copy drafts, 4-bit dense weights, an FP8 KV cache,faster prompt kernels, a one-shot RoCE all-gather between the Sparks, several requests over one shared cache pool,vision, tool calling, `/tokenize` and `/metrics`.@@ -562,7 +562,7 @@ applied with `patch -p0` in filename order); `start.sh` rebuilds or re-pulls the| Prompt | `0001-glm-exl3-prompt-experts`, `0004-glm-prompt-kernels`, `0009-glm-prefill-kernels`, `0020-glm-prompt-experts-order`, `0024-glm-prompt-select-rows`, `0028-glm-lean-prompt-scratch` | EXL3 expert kernels that keep a prompt chunk's rows in L2, launched in a better order; each row's input rotated once; dense attention only where the sparse pass needs it; token selection in blocks of 512 rows; smaller prompt scratch | faster prefill, less memory at 1M || Prompt, two Sparks | `0010-glm-hc-split`, `0033-glm-prefill-overlap2`, `0017-glm-overlap-priority`, `0022-glm-overlap-normal-priority`, `0064-glm-split-connect-early` | hyper-connection glue split by rows between the Sparks, exchanges overlapped with the next rows' work (`SPLIT`); the split's send/receive connection opened before the weights and the cache pool (issue #36) | 50k prefill ~1,270 -> ~1,730 tok/s with 0009 and 0020; decode rounds pay ~1.5% || Prompt, KDA | `0012-glm-kda-chunked`, `0014-glm-kda-chunked-gb10`, `0039-glm-kda-chunked-kernel` | the linear-attention layers' prompt chunks in chunked (WY) form, one CUDA kernel (`KDA_CHUNKED`) | 50k prefill 29.3 -> 26.4 s, 149k 91.1 -> 84.5 s (one start each) |-| Prompt reuse | `0008-glm-prompt-grid`, `0015-glm-shared-prefix`, `0042-glm-prompt-replay`, `0054-glm-image-prompt-reuse`, `0060-glm-keep-thinking`, `0063-glm-kept-cap-superseded-first`, `0071-glm-shared-prefix-copy`, `0077-glm-kept-cap-shared-by-recency` | prompt chunks and kept states on a token grid; a shared system prompt's state reused (copied out of the conversation that wrote it, which keeps its own state, by ezoushen, #43); an identical prompt resumes from its kept end state; prompts with pictures resume too, their kept states told apart by each picture's content (by abhicnv007, issue #11); earlier turns' reasoning kept in the prompt (`TF_GLM_CLEAR_THINKING`, by kky42, #23); past the kept-state cap a conversation's superseded states go first (by Alexbob0, #32), and shared-prefix states by recency only, so a new agent run still finds its system prompt (diagnosed and the fix proposed by meleesciony, #75) | [Performance](#performance) |+| Prompt reuse | `0008-glm-prompt-grid`, `0015-glm-shared-prefix`, `0042-glm-prompt-replay`, `0054-glm-image-prompt-reuse`, `0060-glm-keep-thinking`, `0063-glm-kept-cap-superseded-first`, `0071-glm-shared-prefix-copy`, `0077-glm-kept-cap-shared-by-recency`, `0078-glm-take-over-decide-then-copy` | prompt chunks and kept states on a token grid; a shared system prompt's state reused (copied out of the conversation that wrote it, which keeps its own state, by ezoushen, #43); an identical prompt resumes from its kept end state; prompts with pictures resume too, their kept states told apart by each picture's content (by abhicnv007, issue #11); earlier turns' reasoning kept in the prompt (`TF_GLM_CLEAR_THINKING`, by kky42, #23); past the kept-state cap a conversation's superseded states go first (by Alexbob0, #32), and shared-prefix states by recency only, so a new agent run still finds its system prompt (diagnosed and the fix proposed by meleesciony, #75); a fresh conversation after a long one clones only the kept states that stay, not every one first (reserved +14.8 -> +1.4 GiB after 256k, 21 turns; upstream #421) | [Performance](#performance) || Decode | `0013-glm-decode-rounds`, `0016-glm-decode-kernels`, `0019-glm-decode-kernels2`, `0031-glm-decode-index-regs` | verify windows of up to 16 rows; faster decode matmuls, hyper-connection mixing and indexer scoring | faster decode rounds; long copy drafts (`COPY_MAX` 15: edit replies 84 -> 119 tok/s) || Decode, memory | `0046-glm-l2-prefetch`, `0047-glm-exl3-decode-loads` | the next kernels' weights prefetched into L2 during each layer's all-gathers (`TF_GLM_L2PF`, adapted from jayleaton/glm53-tensorfold-spark's patch 0460); the routed experts' trellis as 16-byte non-coherent loads a step ahead (`TF_GLM_EXL3_LOADS`, adapted from its patch 0580) | with `TF_ROCE_MAX_KB=512`: one request's prose 48.36 -> 49.68 tok/s, code 59.54 -> 61.49 (+2.7% / +3.3%); 4 at once prose 74.8 -> 76.6, code 100.0 -> 102.7 (two starts each); the same bits || Decode, indexer | `0043-glm-visible-pools` | a decode row's token scoring and split selection bounded to the pools it can see (TensorFold v0.6.0 bounds its one-program selection, PR #140) | the same tokens |diff --git a/patches/0078-glm-take-over-decide-then-copy.patch b/patches/0078-glm-take-over-decide-then-copy.patchnew file mode 100644index 0000000..e3919fe--- /dev/null+++ b/patches/0078-glm-take-over-decide-then-copy.patch@@ -0,0 +1,86 @@+# Kept prompt states: a new conversation that shares no prefix with the last one no longer clones every kept state and+# then drops some; it works out which states stay and clones only those. The states that stay and the replies are the+# same by design (the old walk is simulated by size; 200 random cases match). On 2 DGX Sparks, after a 256k, 21-turn+# conversation, a fresh one: save_rows 22 -> 1, reserved +14.8 -> +1.4 GiB, take_over 3.3 -> 0.10 s; before, the driver+# logged NV_ERR_NO_MEMORY. Applies after 0015 and 0063. Upstream: ashhart/TensorFold#421 (drop this patch once merged).++diff --git tensorfold/families/glm5_next/cuda/engine.py tensorfold/families/glm5_next/cuda/engine.py+index a6332ba..0543fd9 100644+--- tensorfold/families/glm5_next/cuda/engine.py++++ tensorfold/families/glm5_next/cuda/engine.py+@@ -960,36 +960,60 @@+ torch.cuda.empty_cache()++ def _take_over(self, keep: list[int]) -> None:+- """Save the rows of every kept snapshot the next prefill overwrites, dropping the oldest entries past the memory budget; both ranks decide alike."""+- from .decode import row_bytes, save_rows++ """Save the rows of every kept snapshot the next prefill overwrites, dropping the oldest entries past the memory budget; both ranks decide alike.++++ Decided first, copied after: which snapshots stay within the budget is worked out from their sizes alone, the++ rest are dropped without a copy, and only the survivors are cloned. Cloning each in turn and dropping it for the++ next grew the allocator's pool with every larger copy (reserved memory far past the budget)."""++ from .decode import row_bytes, save_rows, snapshot_bytes++ drafter = self._kept_drafter()+ live = self.live+- dropped = False++ order = list(self.cache)++ def resumes(c) -> bool:+ return len(c.ids) <= len(keep) and keep[:len(c.ids)] == c.ids++- for snap in list(self.cache):++ alive = {id(c) for c in order}++ saved: dict[int, int] = {} # a survivor's held bytes once its rows are saved++ drops: list = []++ over_budget = False # a drop made for the budget (not a row already gone)++++ def held() -> int:++ return sum(saved[id(c)] if id(c) in saved else snapshot_bytes(c) for c in order if id(c) in alive)++++ def forget(c) -> None:++ alive.discard(id(c))++ saved.pop(id(c), None)++ drops.append(c)++++ for snap in order: # the walk of saving one by one, with the sizes standing in for the copies+ n = len(snap.ids)+- if snap not in self.cache or snap.rows is not None or resumes(snap):++ if id(snap) not in alive or snap.rows is not None or resumes(snap):+ continue+ if live[:n] != snap.ids: # its rows are already gone: nothing to resume from+- self._drop(snap)++ forget(snap)+ continue+ need = row_bytes(self.e, snap, drafter=drafter)+- while self._held_bytes() + need > self.cache_bytes:+- old = next((c for c in self.cache if c is not snap and not resumes(c)), None)++ while held() + need > self.cache_bytes:++ old = next((c for c in order if id(c) in alive and c is not snap and not resumes(c)), None)+ if old is None:+ break+- self._drop(old)+- dropped = True+- if self._held_bytes() + need > self.cache_bytes:+- self._drop(snap)+- dropped = True++ forget(old)++ over_budget = True++ if held() + need > self.cache_bytes:++ forget(snap)++ over_budget = True+ continue+- save_rows(self.e, snap, drafter=drafter)+- if dropped:++ shed = 0 if drafter is not None and snap.drafter_end == n else sum(++ t.numel() * t.element_size() for t in snap.drafter_rows or [])++ saved[id(snap)] = snapshot_bytes(snap) - shed + need++ for snap in drops:++ self._drop(snap)++ for snap in order:++ if id(snap) in saved:++ save_rows(self.e, snap, drafter=drafter)++ if over_budget:+ import torch++ torch.cuda.empty_cache() # give the freed rows back rather than keep them in torch's pool