Skip to content

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

Python

Drafter: check candidate ids against the vocabulary, log and re-copy once before failing (#80, patch 0084)

MiaAI-Lab123e97455017
diff --git a/README.md b/README.md
index 5956b8c..5fda116 100644
--- a/README.md
+++ b/README.md
@@ -585,6 +585,7 @@ applied with `patch -p0` in filename order); `start.sh` rebuilds or re-pulls the
| Capacity status | `0081-tfcap-capacity-status` | every capacity refusal renders the same way: 429 with `Retry-After: 5` (was a bare 503 at two sites, a 400 on the generate path) (by johnwhited, #48) | a refused request's client can back off instead of stacking |
| Admission control | `0082-tfcap-admission-cap` | `TF_GLM_MAX_QUEUED` (unset: queue, as before; a number: past the lanes plus that many queued foreground requests, refuse) turns saturation into a refusal instead of an invisible queue (by johnwhited, #48) | with `TF_GLM_MAX_QUEUED=0`, the fifth request on four lanes: 429 at once with `Retry-After: 5`; unset, it queues as before |
| Delivery abort | `0083-tfcap-delivery-abort` | a stream whose delivery callback raises (broken pipe, reset, timeout) ends at once and frees its lane, instead of the exception escaping and the lane leaking until generation ends (by johnwhited, #48) | a client that dies mid-reply no longer holds a lane |
+| Checked draft candidates | `0084-glm-draft-candidates-checked` | the drafter checks each block pass's candidate ids against the vocabulary; on an id outside it (issue #80: a float's bits read as an id) it logs the value, copies the rows again once the device is idle, and fails only if they are still bad | a rare drafter `IndexError` is retried (and its cause logged) instead of killing a rank |
| Concurrent fill | `0062-glm-sliced-fill` | a new prompt's chunks run in layer slices (`FILL_BUDGET_MS`) with decode rounds between them, drafted as usual (`FILL_DRAFTS`), both ranks on the same layer boundaries; the chunk's buffers kept between slices, grouped fills, cache moves and cancellation mid-fill; whole forwards when nothing else decodes | with smooth streaming, the other replies' pauses during a 25k-token fill 630-690 ms -> 81-149 ms (table below) |
Concurrent prompt fill and smooth streaming on two Sparks (`PARALLEL=4`, DFlash2, 1,024-row chunks): three 400-token
diff --git a/patches/0084-glm-draft-candidates-checked.patch b/patches/0084-glm-draft-candidates-checked.patch
new file mode 100644
index 0000000..3aa1c56
--- /dev/null
+++ b/patches/0084-glm-draft-candidates-checked.patch
@@ -0,0 +1,131 @@
+diff --git tensorfold/families/glm5_next/cuda/dflash2.py tensorfold/families/glm5_next/cuda/dflash2.py
+--- tensorfold/families/glm5_next/cuda/dflash2.py
++++ tensorfold/families/glm5_next/cuda/dflash2.py
+@@ -3,6 +3,7 @@
+ from __future__ import annotations
+
+ import json
++import sys
+ from dataclasses import dataclass
+ from pathlib import Path
+
+@@ -184,13 +185,23 @@
+ return qmm.matmul(x, w, xs, f32=f32)
+
+
+-def merge_candidates(g: torch.Tensor, proj: torch.Tensor, k: int) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
++class BadCandidates(RuntimeError):
++ """A block pass's host rows hold a candidate id outside the vocabulary (a float's bits read as an id)."""
++
++
++def merge_candidates(g: torch.Tensor, proj: torch.Tensor, k: int,
++ vocab: int | None = None) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
+ """A block pass's host rows -> (ids [depth, k], logits, projected rows): ``g`` [ranks, depth, 2 * k] (each rank's
+ top-k values, then ids as fp32 bits), merged by value then id over the ranks; ``proj`` [depth, selector rank]."""
+
+ values = torch.cat([g[r, :, :k] for r in range(g.shape[0])], dim=1).numpy().astype(np.float64)
+ tokens = torch.cat([g[r, :, k:].contiguous().view(torch.int32) for r in range(g.shape[0])],
+ dim=1).numpy().astype(np.int64)
++ if vocab is not None and tokens.size and (int(tokens.min()) < 0 or int(tokens.max()) >= vocab):
++ bad = tokens[(tokens < 0) | (tokens >= vocab)]
++ raise BadCandidates(f"{bad.size} of {tokens.size} candidate ids are outside the vocabulary of {vocab} "
++ f"(first {int(bad[0])} = 0x{int(bad[0]) & 0xFFFFFFFF:08x}, as a float "
++ f"{float(np.array([int(bad[0]) & 0xFFFFFFFF], dtype=np.uint32).view(np.float32)[0]):.4g})")
+ if g.shape[0] > 1:
+ order = np.lexsort((tokens, -values), axis=-1)[:, :k]
+ values = np.take_along_axis(values, order, axis=1)
+@@ -491,15 +502,24 @@
+ else:
+ self._block_compute()
+ # one pinned copy of candidates and projected rows (two .cpu() calls were two syncs and two pageable copies)
+- if self.cand_ready is not None:
+- self.cand_host.copy_(self.cand, non_blocking=True)
+- self.cand_ready.record()
+- self.cand_ready.synchronize()
+- else:
+- self.cand_host.copy_(self.cand)
+- g = self.cand_host[:self.cand_n].view(self.gathered, self.block - 1, 2 * self.top_k)[:, :depth]
+- proj = self.cand_host[self.cand_n:].view(self.block - 1, -1)[:depth]
+- return merge_candidates(g, proj, self.top_k)
++ vocab = self.succ.shape[0]
++ for attempt in (0, 1):
++ if self.cand_ready is not None:
++ self.cand_host.copy_(self.cand, non_blocking=True)
++ self.cand_ready.record()
++ self.cand_ready.synchronize()
++ else:
++ self.cand_host.copy_(self.cand)
++ g = self.cand_host[:self.cand_n].view(self.gathered, self.block - 1, 2 * self.top_k)[:, :depth]
++ proj = self.cand_host[self.cand_n:].view(self.block - 1, -1)[:depth]
++ try:
++ return merge_candidates(g, proj, self.top_k, vocab)
++ except BadCandidates as e:
++ if attempt:
++ raise
++ print(f"[tensorfold] drafter: {e}; copying the rows again once the device is idle", file=sys.stderr,
++ flush=True)
++ torch.cuda.synchronize()
+
+ def chain(self, tokens: np.ndarray, values: np.ndarray, proj: np.ndarray, anchor: int, first: int,
+ sampling: Sampling | None, confidence: float = 0.0, *, beta: float = 0.0,
+diff --git tensorfold/families/glm5_next/cuda/dflash2_multi.py tensorfold/families/glm5_next/cuda/dflash2_multi.py
+--- tensorfold/families/glm5_next/cuda/dflash2_multi.py
++++ tensorfold/families/glm5_next/cuda/dflash2_multi.py
+@@ -20,6 +20,7 @@
+
+ from __future__ import annotations
+
++import sys
+ from dataclasses import dataclass
+ from typing import Sequence
+
+@@ -32,7 +33,7 @@
+ from tensorfold.engine.exact_sampling import Sampling
+
+ from . import glue
+-from .dflash2 import NO_LIMIT, Drafter, _dconv, _mm, merge_candidates
++from .dflash2 import NO_LIMIT, BadCandidates, Drafter, _dconv, _mm, merge_candidates
+
+
+ @triton.jit
+@@ -389,20 +390,28 @@
+ g.replay()
+ else:
+ self._block_compute(S)
+- if self.cand_ready is not None:
+- self.cand_host.copy_(self.cand, non_blocking=True)
+- self.cand_ready.record()
+- self.cand_ready.synchronize()
+- else:
+- self.cand_host.copy_(self.cand)
+ d = self.d
+ m, k = self.block - 1, d.top_k
+- g_all = self.cand_host[:d.gathered * S * m * 2 * k].view(d.gathered, S * m, 2 * k)
+- p_all = self.cand_host[self.cand_n:].view(St * m, -1)
+- out = []
+- for s, (_, _, depth) in enumerate(reqs):
+- out.append(merge_candidates(g_all[:, s * m:s * m + depth], p_all[s * m:s * m + depth], k))
+- return out
++ vocab = d.succ.shape[0]
++ for attempt in (0, 1):
++ if self.cand_ready is not None:
++ self.cand_host.copy_(self.cand, non_blocking=True)
++ self.cand_ready.record()
++ self.cand_ready.synchronize()
++ else:
++ self.cand_host.copy_(self.cand)
++ g_all = self.cand_host[:d.gathered * S * m * 2 * k].view(d.gathered, S * m, 2 * k)
++ p_all = self.cand_host[self.cand_n:].view(St * m, -1)
++ try:
++ return [merge_candidates(g_all[:, s * m:s * m + depth], p_all[s * m:s * m + depth], k, vocab)
++ for s, (_, _, depth) in enumerate(reqs)]
++ except BadCandidates as e:
++ if attempt:
++ raise
++ print(f"[tensorfold] drafter: {e} ({S} streams); copying the rows again once the device is idle",
++ file=sys.stderr, flush=True)
++ torch.cuda.synchronize()
++ raise AssertionError("unreachable")
+
+ def propose(self, reqs: Sequence[DraftRequest]) -> list[list[int]]:
+ """Each stream's ``Drafter.propose``: streams with a depth and a context share one block pass; the others