Skip to content

feat(glm cuda): prompt_logprobs on completions - #399

Closed
Bizuayeu wants to merge 3 commits into
ashhart:mainfrom
Bizuayeu:pr/glm-prompt-logprobs
Closed

Bizuayeu wants to merge 3 commits into
ashhart:mainfrom
Bizuayeu:pr/glm-prompt-logprobs

Conversation

@Bizuayeu

@Bizuayeu Bizuayeu commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Addresses part of #298: scoring a given text without generating, on GLM-5.3-Flash on two ranks. It does not add
logprobs for generated tokens, the echo: true form, or prompt_logprobs on chat completions.

What changes

  • prompt_logprobs: k (0 to 20) on nonstreamed /v1/completions, for engines that declare
    supports_prompt_logprobs. The reply follows vLLM's shape: null, then for each later position the prompt token
    (ranked in the whole vocabulary) and the top k (ranked by position). Streaming and chat completions answer 400.
  • GLM prefill, only when asked: after each chunk's forward and before the MTP absorb (which writes fnormed), the head
    runs on the chunk's rows in groups bounded as logprobs.capture bounds its temporaries. The prefill matmuls give a
    row the same bits in any group, so the rows do not depend on the chunking.
  • Ranks combine by two gathers per group: the log-sum-exp, the top k, and the logit at column target % shard (read
    from slot target // shard), then the counts at or above it. This is the cross-rank denominator GLM-5.3 on CUDA (two Sparks): logprobs and prompt_logprobs, so quantization variants can be compared by perplexity #298 asks for.
  • Such a request never resumes a kept prefix; rank 1 learns that from one more header field.
  • A request without prompt_logprobs only gains an is-None check. The tiny checkpoints' tokens and cache hashes (MLX
    4-bit, dense and long) are unchanged.

Tests

  • tests/test_cuda_prompt_logprobs_fields.py (host): token-id prompts reach the engine as sent; the vLLM shape;
    malformed requests are refused before generation; engines without prompt rows refuse them.
  • tests/cuda/test_glm_prompt_logprobs.py (tiny MLX 4-bit checkpoint): rows match a plain fp32 head; they do not
    depend on the chunking; a repeat gives the same rows; the reply and caches are those without the request; a kept
    prefix is never resumed; the reply's entries hold each position's own token, so the teacher-forced NLL reads off
    them; a one-token prompt has no rows.

Receipt

  • Environment. 0.6.4; PyTorch 2.13.0a0+9186a08 (NVIDIA's 26.07 container, CUDA 13.3, NCCL 2.30.7); two DGX Sparks (GB10, 128 GB each,
    GPU clock capped at 2,200 MHz), TP=2 over one RoCE link; --context 0, MTP drafts only;
    TensorFold/GLM-5.3-Flash-MLX-4bit-MTP@d2b17f52.
  • Exactness. tools/bench_concurrent.py … --alone --serial: 48 of 48 and 10 of 10. One request twice, the second
    with "draft": false: token_sha equal for 5 of 5 prompts (14 to 32,762 tokens). A request without
    prompt_logprobs, main / this: the same token_sha for 5 of 5.
  • Decode. main / this, one start each: 51.93 / 52.18, 42.88 / 42.94, 62.64 / 62.29, 47.07 / 47.06 tok/s (code / chat at temperature 1, then 0).
  • Prompt. main / this, one start each, median of 3 a length: 1,107 / 1,110, 1,033 / 1,036, 1,045 / 1,045,
    1,043 / 1,043, 1,033 / 1,034 tok/s at 2k / 8k / 16k / 32k / 64k tokens.
  • Scoring. One 2,036-token text with prompt_logprobs 0 and 5: 2,036 positions, the first entry null; with 5,
    each entry holds 5 keys, or 6 when the prompt token is outside the top 5, and the prompt token's logprob equals
    the one prompt_logprobs 0 returns; mean NLL 0.237. The reply (token_sha 2014c7cc1bc6) is the same with the
    field at 0, 5 or absent. A one-token prompt returns one position. Streaming and chat requests with the field get
    400 (prompt_logprobs support nonstreamed completions); main accepts the field with 200 and returns no
    prompt_logprobs.
  • Tests. On both Sparks, tests/test_cuda_*.py tests/test_glm*.py and the GLM files of tests/cuda: 1,530
    passed, 14 skipped (main: 1,506 passed, 14 skipped); the 24 new tests pass.
  • Not run. Metal: the Mac server is unchanged. RTX PRO 6000. lm-evaluation-harness (it needs the echo form).

Written with Weave (My Symbiotic AI)

Comparing a text's teacher-forced NLL with vLLM's needs vLLM's completions
prompt_logprobs, which the CUDA server did not take.

- prompt_logprobs: k (0..20, nonstreamed completions, engines that declare
  supports_prompt_logprobs) returns vLLM's shape: None, then per position the
  prompt token (vocabulary rank) and the top k (positional ranks).
- GLM prefill, only when asked: after each chunk's forward and before the MTP
  absorb (which writes fnormed), the head runs on the chunk's rows in groups
  bounded as logprobs.capture bounds its temporaries; the prefill matmuls give
  a row the same bits in any group, so the rows do not depend on the chunking.
  Ranks combine by two gathers per group: lse, top k, and the logit at column
  target % shard (read from slot target // shard), then the counts at or
  above it. Such a request never resumes a kept prefix; rank 1 learns it
  from one more header field.
- The no-request path only gains an is-None check: the tiny checkpoints'
  tokens and cache hashes (MLX 4-bit, dense and long) are unchanged.
…6.3's words

v0.6.3 reads token-id prompts with server.token_routes.token_ids, which
names booleans and floats as not integer ids instead of giving the range.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @Bizuayeu. Scoring a prompt the way vLLM does, the prompt token ranked in the whole vocabulary beside the top k, is what perplexity runs need.

The Python engine is frozen (#286), so this PR won't merge. The Zig API reserves the prompt_logprobs request field, in vLLM's shape, but no Zig engine produces prompt logprobs yet. They come with engine work, since the forward has to report each prompt row while it prefills. When that lands, your design is the reference, and the commit carries your credit. GLM, where you built it across two ranks, isn't on the Zig engine yet.

@ashhart ashhart closed this Oct 6, 2026
@Bizuayeu
Bizuayeu deleted the pr/glm-prompt-logprobs branch October 25, 2026 07:27
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.

2 participants