Skip to content

[rl] Verifiers generation server: faster request path, required sampling params (4/10) - #5454

Merged
felipemello1 merged 23 commits into
mainfrom
felipemello1/genserver-request-path
Nov 5, 2026
Merged

felipemello1 merged 23 commits into
mainfrom
felipemello1/genserver-request-path

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

TLDR: Token-id validation is 5.7x faster (~390 -> 68 us per 7k-token prompt) and the response build 1.4x faster with no GC passes. Together they were ~42% of the controller thread's GIL-holding samples at 10 nodes, so its GIL-holding time drops by ~25% (estimated from microbenchmarks, not re-measured). temperature, top_p and max_tokens are now required (the rollouter always sends them).

Stack (10 PRs, merge bottom-up; this PR is in bold)

The four HTTP generation PRs left this stack. They wait on the generator/router design discussion.

# PR Independent? Needs (stack positions)
1 Verifiers 0.4.0 (merged) yes —
2 Verifiers: release each finished rollout's routing session (merged) yes — (resolves its test overlap with 1)
3 Verifiers generation server: no per-request access log (merged) yes —
4 Verifiers generation server: faster request path, required sampling params yes — (resolves its test overlap with 1-3)
5 Rollout workers write structured logs yes —
6 Drain in-flight rollouts during shutdown yes —
7 Rollouts that fail outside the policy are recorded, not trained no 2 (resolves its overlap with 4-6)
8 Cancel a rollout group on its worker no 5, 6, 7
9 Optional native rollout timeout no 7, 8
10 Run Verifiers groups in rollout workers no 7, 8, 9

Problem

  1. Each generate request type-checks every prompt token in a Python generator expression (~7k tokens in the profiled run).
  2. Each response builds one dict and one f-string per completion token.
  3. At 10 nodes the controller thread holds the GIL in 94% of samples: (1) is 23.6% of them, (2) 18.0%.
  4. (2)'s per-token dicts start a GC pass every ~700 tokens: 61% of the controller's GC time.

Solution

  1. _validate_token_ids checks set(map(type, token_ids)) <= {int} and returns the list without a copy.
  2. The logprobs content is written as JSON text and spliced into the response dict.
  3. temperature, top_p and max_tokens are now required. A missing one used to sample at the defaults (temperature 0.8, 100 tokens); the rollouter always sends all three.
  4. Unchanged: the response JSON (the test now asserts the exact payload).

Results

Microbenchmark, AMD EPYC 9654, Python 3.12, GC on; us per call, 3 repeats (min-max)
                                   main                          this PR
validate a 7,000-token prompt      385-409                       67-68
response body, 1,000 tokens        721-748, 1 GC pass            507-531, 0 GC passes
response body, 8,000 tokens        5,858-5,884, 11 GC passes     4,263-4,312, 0 GC passes

py-spy --gil, 120 s, controller of a 10-node Qwen3.5-27B SWE RL run (same validation and response code)
share of GIL-holding samples       _validate_token_ids 23.6%, response build 18.0%
expected saving                    23.6% x 83% + 18.0% x 27-30% = ~25% (not re-measured)

You may ask: why not orjson? It's a new dependency, and the request decode it would also speed up (13.4%) is smaller than either fix here.

NOTE: a non-finite logprob now fails the client's JSON parse instead of its finiteness check; the request fails either way.

Test plan

pytest tests/rl/unit_tests/cpu/test_verifiers.py -k "generation_server or validate_token_ids or sampling_config"

test_parse_sampling_config_requires_temperature_top_p_max_tokens fails on main; test_validate_token_ids and the exact-payload assertion pin unchanged behavior.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 16, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/genserver-request-path branch from 02a2df0 to 1a3ab66 Compare October 18, 2026 23:30
@felipemello1
felipemello1 changed the base branch from main to felipemello1/genserver-no-access-log October 18, 2026 23:30
@felipemello1
felipemello1 added this pull request to stack #5513 October 18, 2026 23:32
@felipemello1
felipemello1 force-pushed the felipemello1/genserver-request-path branch from 1a3ab66 to 7378cf6 Compare October 19, 2026 01:15
Comment on lines +266 to +273
# Text, not one dict per token: 1.4x faster, and no GC pass every ~700 tokens.
# A finite Python float's repr is valid JSON; vLLM's logprobs are Python floats.
logprobs_content = ",".join(
f'{{"token":"token_id:{token_id}","logprob":{logprob!r}}}'
for token_id, logprob in zip(
completion.token_ids, completion.token_logprobs
)
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is creative. Was this tested for 131k context? does any other library do it? Would we extend that to router replay?

@felipemello1 felipemello1 changed the title [rl] Verifiers generation server: faster request path, required sampling params [rl] Verifiers generation server: faster request path, required sampling params (4/14) Oct 19, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/genserver-request-path branch 2 times, most recently from 15db4c0 to b5e3419 Compare October 23, 2026 07:34
@felipemello1

Copy link
Copy Markdown
Contributor Author

reply from claude:

  • 131k: not tested at 131k, but context length doesn't matter here. The spliced list holds the completion's tokens only, so max_tokens per turn bounds it. Our 65k-context agent runs (16,384 tokens per turn) send every turn through this path.
  • Other libraries: not that I know of. vLLM, whose response format this is, builds one pydantic object per token: ~9 us per completion token vs ~1 us here (measured in PR 9).
  • Router replay: no. The expert rows don't go through this JSON; a follow-up keeps them on the generator and sends 4-byte handles.
  • Caveat: it assumes finite logprobs, which a sampled token's always are. PR 9's "response_format": "torchtitan" sends raw float64, which also round-trips -inf and NaN.

@felipemello1
felipemello1 force-pushed the felipemello1/genserver-request-path branch from b5e3419 to c4649cf Compare November 5, 2026 00:35
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 55s —— View job


Approve (please add the missing "Summary (human-written)" section before merging) -- review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- please address the item below before merging -- the code keeps the same wire format, the speedups are backed by microbenchmarks, and the new required-field check turns a silent fallback to default sampling into a 400.

What this PR does. The Verifiers generation server checks prompt token ids with one C-level set(map(type, ...)) instead of a per-token Python generator, and no longer copies the list. It builds the per-token logprobs.content list as JSON text and splices it into the json.dumps output, so it no longer creates one dict per token. temperature, top_p and max_tokens are now required in sampling_params.

Example.

POST /inference/v1/generate  {"token_ids": [10, 11], "sampling_params": {"stop_token_ids": [99], "torchtitan_group_id": 1}}
before: 200, sampled at temperature=0.8, max_tokens=100 (SamplingConfig defaults)
after:  400 "sampling_params must include ['max_tokens', 'temperature', 'top_p']"

Completion(token_ids=[31, 32], token_logprobs=[-0.1, -0.2])
before/after: ..."logprobs": {"content": [{"token": "token_id:31", "logprob": -0.1}, ...]}...  (same bytes after parsing; pinned by the exact-payload test)

Must fix

  1. PR description -- there is no "Summary (human-written)" section from .github/pull_request_template.md. Please add the 2-5 sentence author summary. The rest of the description matches the code.
Details
  • Splice safety. rpartition(json.dumps(placeholder)) only matches the real placeholder. In any other JSON string value, " is escaped as \", so the quoted placeholder can't appear inside request_id (which comes from the X-Session-ID header). finish_reason is restricted to stop/length before this point, and the match takes the last occurrence anyway.
  • Dropping zip(strict=True) is fine: line 233 already returns 500 when the lengths differ.
  • {logprob!r}. The logprobs reach this code as Python floats: engine.py:500 calls list(completion_output.logprobs.logprobs) with flat_logprobs=True. repr of a finite float (-0.1, 1e-05, -1.5e+20) is valid JSON. Numpy scalars would print as np.float64(...) and break the response, but this path never produces them.
  • No copy in _validate_token_ids. Both callers pass lists from the freshly parsed request body, which nothing else holds, so aliasing them is safe.
  • Required sampling fields. The only in-repo client, rollouter.py:316-319, always sets all three on VerifiersSamplingConfig.
  • Non-finite logprobs. These now fail in the client's JSON parse instead of its finiteness check. That is still a loud failure, so no extra guard is needed under the "no just-in-case checks" rule.
  • Tests. test_validate_token_ids pins the bool rejection across the rewrite (type() vs isinstance). The sampling-config test guards against the silent fallback to defaults coming back. Both are cheap CPU tests.

Not verified: that Verifiers 0.4.0 forwards top_p/max_tokens verbatim (I didn't inspect the package), and the end-to-end GIL saving (the PR says it was estimated, not re-measured). A maintainer still needs to review.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 5, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 5, 2026 05:33
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review again

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 38s —— View job


Approve (one item still open: add the "Summary (human-written)" section to the PR description) -- review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- please address the item below before merging -- the only code change since the last review is a comment deletion. Both maintainer comments are resolved. The one remaining item is in the PR description.

What changed since the last review. dd6fff4ec deletes the comment above _validate_token_ids. The other three commits (1e310855e, 4d329617c, 982fcf127) merge the base branch and don't touch this PR's hunks. The diff against felipemello1/genserver-no-access-log is otherwise the same code I approved before.

Must fix

  1. PR description -- still open from the last review: the body has no ## Summary (human-written) section from .github/pull_request_template.md. Please add the 2-5 sentence author summary.
Details
  • Maintainer comment, generation_server.py:267 (scale, router replay, score centering, other libraries). The author answered it with a 1k/100k-token CPU microbenchmark. The parsed output is identical, and the speedup is about 1.5x at both sizes. Router replay and score centering don't change this response payload: it carries only token_ids and per-token logprobs. The code agrees. The splice covers only completion.token_ids and completion.token_logprobs (lines 268-272), so context length and expert rows never reach it. No maintainer pushed back on the answer.
  • Maintainer comment, "we can remove this comment" (the type() vs isinstance note). Fixed in dd6fff4ec. test_validate_token_ids keeps the [1, True] case, so the bool rejection is still pinned.
  • Minor, not blocking. The comment at line 266 says "1.4x faster". The new benchmark reply measures 1.5x. Either number is fine.

Not verified: the end-to-end GIL saving (the description says it is estimated, not re-measured). A maintainer still needs to review.

Base automatically changed from felipemello1/genserver-no-access-log to main November 5, 2026 07:20
@felipemello1
felipemello1 removed this pull request from stack #5639 November 5, 2026 07:53
@felipemello1
felipemello1 added this pull request to stack #5857 November 5, 2026 07:54
@felipemello1 felipemello1 changed the title [rl] Verifiers generation server: faster request path, required sampling params (4/14) [rl] Verifiers generation server: faster request path, required sampling params (4/10) Nov 5, 2026
@felipemello1
felipemello1 merged commit 06f1546 into main Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants