Skip to content

glm cuda: TF_GLM_KEEP_SEQS, a released stream's sequence stays for its next request - #4809

Merged
ashhart merged 12 commits into
ashhart:mainfrom
BadAd84:keep-seqs
Nov 25, 2026
Merged

ashhart merged 12 commits into
ashhart:mainfrom
BadAd84:keep-seqs

Conversation

@BadAd84

@BadAd84 BadAd84 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Refs #4247.

Engine.release frees every unpaged slot's sequence, so a server's slots 1 to
--parallel rebuild per request: the next request allocates a window, zeroes it
and fills it from the prompt. Slot 0 already keeps its sequence across requests.
TF_GLM_KEEP_SEQS=1 makes every slot do what slot 0 does. Unset or 0 is main's
behaviour, byte for byte.

The setting is a row of platform/environment.zig and a field of the ranks'
settings, read once from the environment and passed in the engine's options, so
the server and the GPU checks reach the same engine with the same value. It is
in the ranks' fingerprint, so a run whose ranks disagree stops before the
weights load.

What a kept sequence holds while idle

Its arena, which is the window the sequence writes its KV into, and its State.
Nothing else: no prompt rows, no logits, no drafter state. The next request on
that slot runs begin, which resets the State, and the kept arena is laid over
by that request's own prefill exactly as a fresh one would be.

What the setting saves is the allocation of a whole window, the zero fill of a
fresh arena, and that fill's copy of the prompt.

Startup memory

Unchanged in both arms. Nothing is kept at startup: with the setting on, a slot's
first request still allocates, and a sequence only stays if it was released. The
held memory appears after a release, as one window per idle slot, and goes back
when the engine closes. A paged slot still gives its pages back on release.

Tests

zig build test-glm-keep-seqs-host runs four cases with no GPU. Off, a released
slot's sequence is freed and the next request allocates and zeroes a whole window.
On, it allocates, fills and frees nothing and the same sequence comes back. Slot 0
keeps its sequence either way, and every slot's arena goes back at close. A paged
slot still gives its pages back with the setting on.

On one GB10 the check runs with TF_GLM_LAYERS=2, because the 4bpw snapshot is
163.6 GiB and one rank holds less than that; the comparison is unchanged by the
layer count.

glm-keep-seqs-check CKPT ID_PATH 0 1 compares kept sequences with fresh ones:
the same greedy tokens, the same bf16 logits rows and the same MTP head drafts at
each of 24 steps. It runs a prompt shorter than the one the slot last held (6,000
tokens, then 600 and 3,000), a kept state with every byte poisoned to 0xFF, slot 0
with the setting off, and a planted control that must differ from the fresh run.
The control fails as planted if the comparison cannot see it.

Gates

Base is upstream main at 0b39f4d.

  • python tools/pr_check.py against that base: 0 errors, 1 warning, the
    changelog line, which needs this pull request's number and goes in as a commit
    once it exists.
  • lean, base against head, each side's own checker: 7,702 files and 165 problems
    on the base, 7,704 and 165 on this branch, 0 new.
  • zig build test-glm-keep-seqs-host: 4 of 4 pass, no GPU.
  • zig build compile-cuda-tests: the check compiles into the GPU runner.
  • Every GLM host step passes: test-glm-startup-host, test-startup-watch-host,
    test-glm-pdl-policy-host, test-glm-exl3-prompt-host, test-glm-relay-host,
    test-glm-warm-peers-host, test-kda-host, test-glm-thin-weights-host.
  • zig build test test-golden on an x86_64 host whose GPU cannot run these
    kernels: every failure is one of 27 test binaries under the test step, and the
    pristine base fails the same 27 with the same step set, so they are the host and
    not the change. test-golden passes on this branch.
  • zig fmt --check build.zig zig, git diff --check: clean.

Could this be the default?

No. It is free when unused and it keeps tokens exactly, but it trades memory for
the fill: each idle slot holds its window, so a server with --parallel 4 holds
three more windows than main does while nothing is in flight. That is an
operator-visible trade, and a setting is the honest place for it. Unset is main's
behaviour.

The check's run

On one GB10, branch ed007da20 (its patch is byte-identical to fe63072f3, the
head after the rebase onto 0b39f4d64), TF_GLM_LAYERS=2, TF_GLM_KEEP_SEQS=1, with
the image built from that tree:

RESULT control kv8=false, 600-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=false, 3000-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=true, 600-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=true, 3000-token prompt: differs from the fresh run at step 0, as planted
PASS kept sequences, as left and with every byte poisoned, equal fresh ones: 24 greedy steps' tokens, bf16 logits rows and MTP drafts after prompts of { 600, 3000 } tokens, fp8 and bf16 caches

The same command with the setting unset stops before the comparison:
FAIL glm-keep-seqs-check needs TF_GLM_KEEP_SEQS=1. Both arms print the same
startup lines, the checkpoint digest and the loader's read-ahead, and no code
before release reads the setting, so nothing about startup memory or startup
time moves. This runner does not print the server's own memory receipt; two
server boots would show it, and I will post them if you want that line rather
than the argument.

BadAd84 added a commit to BadAd84/TensorFold that referenced this pull request Nov 23, 2026
tools/pr_check.py looks for the pull request's number in the Unreleased line.
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BadAd84 for this, and for the careful write-up. The trade between memory and the per-request fill is stated plainly, and keeping it a setting is the right call.

The change itself looks right to us:

  • Unset: release keeps exactly main's condition (slot 0 only), so nothing changes.
  • Set: a released slot keeps its sequence, and the next request on that slot binds it instead of allocating. Every sequence has the same window and cache format, begin resets it as it already does for slot 0, and everything is freed at close.
  • Memory: startup already reserves a full window for every stream it admits, and the prompt cache is sized beside that. So kept idle windows stay inside the startup plan.
  • Ranks: putting the setting in the ranks' fingerprint, with its own name in the mismatch message, is a nice touch.
  • The GPU check: the poisoned arena, the shorter prompts and the planted control make it convincing.

One small fix is needed. GLM's environment test counts the glm rows in the registry and expects each to be listed (zig/src/families/glm/cuda_env_test.zig, the test "every GLM reader's name is a glm row…", run by zig build test-glm-rev25-host). TF_GLM_KEEP_SEQS isn't in its switches list yet, so that test will fail on the count. Adding .{ .name = "TF_GLM_KEEP_SEQS" } to switches should fix it.

After that, the remaining question is the full model. Your check ran with two layers. If you can, please run the same comparison, kept against fresh with the poisoned arena, on the complete checkpoint. We'd like to see every layer kind and the MTP head covered, and a prompt-cache resume that lands on a kept slot.

There's a small changelog overlap with main, which we'll sort out on our side.

Thanks again.

  • Synthena

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BadAd84. The switches entry is in on 40d4e19b4e, so GLM's environment test now counts TF_GLM_KEEP_SEQS.

The one item still open is the full checkpoint: the same kept-against-fresh comparison with the poisoned arena, covering every layer kind and the MTP head, plus a prompt-cache resume that lands on a kept slot. The changelog overlap with main is ours to sort out.

  • Synthena

@BadAd84

BadAd84 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks Synthena, and thanks for reading it that closely.

The switches list is fixed in 40d4e19: TF_GLM_KEEP_SEQS is in switches now, and zig build test-glm-rev25-host passes with it.

On the full checkpoint, you are right that two layers leaves most of it untested. I ran the same comparison over the complete checkpoint across the three ranks, each rank its own part, in 266 seconds. Every layer kind and the MTP head are in it, and the planted controls fired before the comparison ran:

RESULT control kv8=false, 600-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=false, 3000-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=true, 600-token prompt: differs from the fresh run at step 0, as planted
RESULT control kv8=true, 3000-token prompt: differs from the fresh run at step 0, as planted
PASS kept sequences, as left and with every byte poisoned, equal fresh ones: 24 greedy steps' tokens, bf16 logits rows and MTP drafts after prompts of { 600, 3000 } tokens, fp8 and bf16 caches

Still to come: the prompt-cache resume that lands on a kept slot.

The changelog overlap is yours to handle, thanks.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BadAd84. The environment registration fix is present, and the complete-checkpoint comparison now covers the earlier two-layer gap, including the poisoned arenas and planted controls.

The remaining case is the prompt-cache resume onto a kept slot. Please include the exact tested head, checkpoint revision and launch settings with that receipt so the results stay reproducible. We will handle the changelog overlap.

  • Synthena

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @BadAd84. The new test now saves a prompt state, restores it onto the same retained slot, compares tokens, logits and drafts with a fresh run, and checks that poisoning the saved state is detected. That covers the missing test case in source. The earlier complete-checkpoint result is also credited.

The remaining item is the new restore test’s result, with the exact tested head, checkpoint revision and launch settings. The thread doesn't have that result yet. We will handle the changelog overlap.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked 2ca51d7de9 against 4c79cfe346, keeping main's changelog in the trial tree where the two branches overlap.

zig build test-glm-keep-seqs-host -Dfixed-cpu --summary all
zig build compile-cuda-tests -Dtarget=x86_64-linux-gnu -Dfixed-cpu -j2 --summary all

The M5 Max host suite passed 14 of 14 tests. The Linux x86_64 CUDA test runner, including the new restore test source, cross-compiled successfully in all 12 steps. That target uses placeholder kernel images on this toolchain. The checkpoint-backed CUDA restore result requested above is still pending.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

I reran the kept-sequence checks on the exact #5013 train tip 613ce5ca23. It matches this head (7ad3b9626b) after #4258 on 4beda42498 outside the changelog. Both source merges were clean in the trial.

  • zig build test-glm-keep-seqs-host -Dfixed-cpu -j2 --summary all passed all 14 kept-sequence host tests as part of the train build.
  • zig build check-native-cuda-host compile-cuda-tests -Dtarget=aarch64-linux-gnu -Dfixed-cpu -j2 --summary all passed all 20 steps, including the ARM64 CUDA test runner with the restore case.

The remaining check is the model-backed TF_GLM_KEEP_SEQS=1 run of glm-keep-seqs-check CKPT ID_PATH 0 1 on a CUDA device. My test host could not reach the CUDA box, so the kept-slot restore, fresh comparison and poisoned-state control still need the exact head, pinned checkpoint and device settings in a runtime receipt.

Igor Lessio - AIFLOW LABS

ashhart and others added 12 commits November 25, 2026 09:22
…e next request

Engine.release frees every unpaged slot's sequence, so a server's slots 1 to 3
rebuild per request: the next request allocates a window, zeroes it and fills it
from the prompt. Slot 0 already keeps its sequence across requests; with
TF_GLM_KEEP_SEQS=1 every slot does, as the default-off switch its name implies.

The setting is read through platform/environment.zig on every rank, since release
runs on each of them. A released sequence keeps its arena and its State reset by
the next request's begin; nothing else is held, so an idle slot is the same bytes
it held after its last release.

glm-keep-seqs-check compares kept sequences against fresh ones byte for byte and
step for step: greedy tokens, bf16 logits rows and the MTP head's drafts at each
step. It covers a prompt shorter than the one the slot last held (6,000 tokens,
then 600 and 3,000), a kept state with every byte poisoned to 0xFF, slot 0 with
the switch off, and it plants a control that must differ from the fresh run,
failing as planted when the comparison cannot see it.
The check joins zig/tests/cuda/main.zig's commands as glm-keep-seqs-check CKPT
ID_PATH RANK WORLD, beside glm-solo-check and glm-pair-check, so
compile-cuda-tests builds it and the GPU runner can dispatch it.
keepsSequence takes the engine now, so the comment says the slot keeps its
sequence rather than naming a call shape the code no longer has.
Engine.init takes usize for both; the runner hands the check c_int, as the
other GLM checks cast theirs. compile-cuda-tests failed on the uncast form.
The switch was read where the server builds its engine, which left any other
caller of Engine.init without it: the GPU check set the variable and still saw
the setting off. It is a field of ranks.Settings now, read once from the
environment and passed in the engine's options, so the server and the checks
agree and the ranks' agreement covers it too.
The fingerprint is compared across ranks to stop a run whose settings differ, and
it lists its fields one by one, so the new setting joins it and its version is
24. The divergence test gains the field, so a rank that keeps or frees a
released sequence unlike its peers stops the run.
…ep_seqs

Fingerprint.Field and mismatch() list the fingerprint's fields by hand, so the
new setting joins both. Without them the divergence test named a member the enum
did not have.
…y field

The mismatch report's switch covers every Fingerprint.Field value, so a new
member needs its line or the runner does not compile.
zig/src/families/glm/cuda_env_test.zig asserts that every glm row is named in one of
its lists and that the count matches, so a new flag row has to be listed or the test
fails on the count.
The check compared a kept sequence against a fresh one; it never resumed. This adds the
resume: a prompt state kept at 2048, a second request that resumes from it on the same kept
slot, compared against a fresh run on tokens, bf16 logits rows and MTP drafts, with a control
that poisons every prompt-cache byte and must make the comparison fail.
@ashhart
ashhart merged commit ef4d642 into ashhart:main Nov 25, 2026
1 check failed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @BadAd84. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants