Skip to content

[Feat] sampling: Count penalty history right after sampling in a per-request pool - #52035

Merged
hnyls2002 merged 8 commits into
mainfrom
lsyin/penalty-device-ledger
Nov 21, 2026
Merged

hnyls2002 merged 8 commits into
mainfrom
lsyin/penalty-device-ledger

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Sampling penalties need each request's output history. On main that history lives in per-batch penalizer tables: the scheduler fills them from the host output_ids in prepare_for_decode, which lags one step under overlap, and filters and merges them on the schedule stream while a forward may still read them. Exact speculative penalties (the next PR) need the history current at verify and owned by the device.

This PR counts each committed token right after sampling, on the sampling stream, into a per-request pool; the scheduler only hands out and frees penalty indices and builds each step's params.

Modifications

  • sampling/penalties.py: PenaltyStatusPool, one per scheduler. It holds an int32 per-token count table plus output lengths, with one row per request that uses a penalty; the request holds its row's penalty index (Req.penalty_index), and a step carries the penalty indices it was built with. StepPenalties holds a step's params and builds the [bs, vocab] additive and scaling tables from the pool. The penalizer orchestrator and its four penalizers are removed.

  • Rows:

    • A request gets a penalty index at its first extend admission (alloc), and the row is reset and replayed from output_ids at every extend admission (reset_history) (prefill, every chunk, retraction readmission, PD decode prebuilt). This also wipes a discarded chunk sample.
    • A row is freed at request exit in RequestRetirer; a retraction keeps its row.
    • The tables are dropped when no row is live.
  • Streams: host calls only queue device writes. Growth, reset, replay, and counting run on the sampling stream, so the scheduler never writes penalty state and no cross-stream fence is needed.

  • Counting: tokens are counted right after sampling, in ModelRunner.sample (which also covers delayed sampling and watermark sampling), the EAGLE-family and NGRAM verify, and the DFlash and DSpark block verify. Ids outside the vocab, from NaN logits or masked draft slots, count nothing.

  • Tables: SamplingBatchInfo.step_penalties replaces penalizer_orchestrator. StepPenalties builds and caches the step's tables (built at copy_for_forward on the forward stream); acc_additive_penalties and acc_scaling_penalties are read-only views of them.

  • New request-entry rejections for sampling penalties:

    • diffusion LLMs (they never sample through the sampler);
    • Unlimited-OCR document reuse windows (their first outputs are spliced on the host);
    • DFlash with a bound runtime (it keeps no penalty history).

    UNO still rejects them.

  • Removed: the host bookkeeping Req.penalty_cumulated_len and ScheduleBatch.cumulate_penalty_output_tokens.

Behavior changes

  • Under overlap, the token sampled in the previous step now counts. This affects non-spec decoding, min_new_tokens (which no longer blocks EOS one extra step), and the block-start penalties of speculative verify. Verify rows still share the block-start penalties; per-row exactness is the next PR.
  • The frequency penalty is f * count, not a running sum of f, so results can differ in the last float bits.
  • Additive penalties always apply before repetition scaling.
  • The re-prefill sample after a retraction sees the replayed history.
  • Requests that use no penalty hold no row, and a step without penalized requests builds no tables.
  • sglang.srt.sampling.penalties no longer exports the orchestrator classes.

Accuracy Tests

  • CPU unit tests: test_penalty_status_pool.py, spec/test_verify_penalty_commit.py, test_request_retirer.py, and test_sampling_batch_info.py pass. Every other failure in the sampling, managers, spec, speculative, disaggregation, and session unit directories also fails on main.
  • GPU e2e penalty and speculative tests run through CI on this PR.

Checklist

  • Unit tests added or updated.

CI States

Latest PR Test (Base): 🚫 Run #44882128219
Latest PR Test (Extra): ⊘ Skipped -- add the run-ci-nv-extra label to opt in.
Latest PR Test (AMD ROCm 10): 🚫 Run #44882128222

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

… sampling

Penalty history (per-token output counts and the output length) moves from
per-batch penalizer tables, which the scheduler filled from host output_ids
in prepare_for_decode and filtered and merged on the schedule stream, into
one PenaltyStore per scheduler: an int32 count table plus lengths, one row
per request that uses a penalty, found through a request-slot -> row table.

- The scheduler only admits and frees rows and builds each step's params
  (StepPenalties); every device write (reset and replay at admission,
  growth, counting) runs on the sampling stream, so no cross-stream fence
  is needed.
- A row is reset and replayed from output_ids at every extend admission
  (chunks, retraction, PD decode) and freed at request exit in
  RequestRetirer; the tables are dropped when no row is live.
- Tokens are counted right after sampling: ModelRunner.sample, the
  EAGLE-family and NGRAM verify, and the DFlash and DSpark block verify.
- DLLM, Unlimited-OCR document reuse, and DFlash with a bound runtime
  reject sampling penalties at request entry; UNO still rejects them.
- The host bookkeeping (Req.penalty_cumulated_len,
  cumulate_penalty_output_tokens) and the penalizer orchestrator are removed.

Behavior: under overlap, the token sampled in the previous step now counts
(non-spec, and spec block-start penalties); frequency penalty is f * count;
additive penalties apply before repetition scaling.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@github-actions github-actions Bot added documentation Improvements or additions to documentation Multi-modal multi-modal language model labels Nov 21, 2026
@hnyls2002

hnyls2002 commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

/rerun-test registered/sampling/test_penalty.py registered/openai_server/basic/test_openai_server.py registered/spec/eagle/test_spec_eagle.py registered/spec/eagle/test_spec_eagle_topk.py registered/spec/dflash/test_dflash.py registered/spec/test_spec_ngram.py registered/scheduler/test_retract_decode.py registered/dllm/test_dllm_batching_fdfo.py
(3 tries)

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (8 tests): ✅ View workflow run

cd test/ && python3 registered/unit/sampling/test_penalty_store.py  # base-a
cd test/ && python3 registered/unit/spec/test_verify_penalty_commit.py  # base-a
cd test/ && python3 registered/unit/managers/scheduler_components/test_request_retirer.py  # base-a
cd test/ && python3 registered/unit/sampling/test_sampling_batch_info.py  # base-a
cd test/ && python3 registered/unit/managers/test_auxiliary_output.py  # base-a
cd test/ && python3 registered/unit/managers/test_overlap_uocr_decode.py  # base-a
cd test/ && python3 registered/unit/dllm/test_gemma4_uniform_lifecycle.py  # base-a
cd test/ && python3 registered/unit/disaggregation/test_decode_retraction_transfer_progress.py  # base-a

🚀 1-gpu-5090 (8 tests): ✅ View workflow run

cd test/ && python3 registered/unit/sampling/test_sampling_metadata_staging.py  # base-b-kernel-unit
cd test/ && python3 registered/unit/sampling/test_custom_logit_processor.py  # base-b
cd test/ && python3 registered/unit/spec/test_eagle_post_verify_graphs.py  # base-b
cd test/ && python3 registered/unit/speculative/test_dspark_sampling_accept_graph.py  # base-b
cd test/ && python3 registered/sampling/test_penalty.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle_topk.py  # extra-a
cd test/ && python3 registered/openai_server/basic/test_openai_server.py  # base-b

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@hnyls2002 hnyls2002 changed the title [Feature] sampling: Keep penalty history on the device, counted after sampling [Feat] sampling: Count penalty history right after sampling in a per-request pool Nov 21, 2026
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (8 tests): ✅ View workflow run

cd test/ && python3 registered/unit/sampling/test_penalty_status_pool.py  # base-a
cd test/ && python3 registered/unit/spec/test_verify_penalty_commit.py  # base-a
cd test/ && python3 registered/unit/managers/scheduler_components/test_request_retirer.py  # base-a
cd test/ && python3 registered/unit/sampling/test_sampling_batch_info.py  # base-a
cd test/ && python3 registered/unit/managers/test_auxiliary_output.py  # base-a
cd test/ && python3 registered/unit/managers/test_overlap_uocr_decode.py  # base-a
cd test/ && python3 registered/unit/dllm/test_gemma4_uniform_lifecycle.py  # base-a
cd test/ && python3 registered/unit/disaggregation/test_decode_retraction_transfer_progress.py  # base-a

🚀 1-gpu-5090 (8 tests): ✅ View workflow run

cd test/ && python3 registered/unit/sampling/test_sampling_metadata_staging.py  # base-b-kernel-unit
cd test/ && python3 registered/unit/sampling/test_custom_logit_processor.py  # base-b
cd test/ && python3 registered/unit/spec/test_eagle_post_verify_graphs.py  # base-b
cd test/ && python3 registered/unit/speculative/test_dspark_sampling_accept_graph.py  # base-b
cd test/ && python3 registered/sampling/test_penalty.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle_topk.py  # extra-a
cd test/ && python3 registered/openai_server/basic/test_openai_server.py  # base-b

@hnyls2002 hnyls2002 added the run-ci CI: run the baseline test suite on this PR label Nov 21, 2026
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-5090 (6 tests): ✅ View workflow run

cd test/ && python3 registered/sampling/test_penalty.py  # base-b
cd test/ && python3 registered/openai_server/basic/test_openai_server.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle.py  # base-b
cd test/ && python3 registered/spec/eagle/test_spec_eagle_topk.py  # extra-a
cd test/ && python3 registered/spec/dflash/test_dflash.py  # extra-a
cd test/ && python3 registered/scheduler/test_retract_decode.py  # extra-a

🚀 1-gpu-h100 (2 tests): ❌ View workflow run

cd test/ && python3 registered/spec/test_spec_ngram.py  # base-b
cd test/ && python3 registered/dllm/test_dllm_batching_fdfo.py  # extra-a

@hnyls2002
hnyls2002 merged commit d643847 into main Nov 21, 2026
132 of 162 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/penalty-device-ledger branch November 21, 2026 08:16
shyeh25 added a commit to shyeh25/sglang that referenced this pull request Nov 21, 2026
Resolve conflicts with the per-request penalty pool (sgl-project#52035), DFLASH request
penalties (sgl-project#47020) and the megakernel runtime binding (sgl-project#51676): the shared
logit-adjustment check reads step_penalties, so penalized batches keep the
dense verify and get upstream's penalties; the sparse accept syncs its
decision across TP ranks like the dense selector accept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Multi-modal multi-modal language model run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant