Repository navigation
perf(model): drop repeated dumps and validation in chat conversion - #5934
Conversation
The Responses to Chat Completions converter dumped and revalidated the same data several times per request. Every chat message was also validated against all six message types, because the union had no discriminator. - Discriminate the chat message union by role, and by token metadata for the assistant training variant. Messages are still fully validated, including the final check on converter-built messages. - Read request token metadata from the validated input items instead of dumping, revalidating and dumping it again. Exclude token fields from the request dump only when an item carries them. - Attach reply token metadata with a single copy, and pass the lifted output items to the response directly instead of dumping and rebuilding them. Items keep every field marked as set, so replay dumps are unchanged. - Skip the message copy in the litellm cleanup validator when no message needs cleaning. - Make the per-request converter state a plain dataclass. Converted output is byte-identical to the previous implementation on the new benchmark script's corpus, with and without token metadata. Closes #3042 Signed-off-by: Ananth Subramaniam <[email protected]>
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
- Point the training-variant invariant test at _lift_assistant_message, where the output items are now appended, and fail when it finds none. It passed without checking anything after the lift moved. - Validate a raw dict input item whole, null token fields included, as the request dump did before. Null token extras on a model still count as absent. - Name the roles in the error for a chat message with no known role, instead of the discriminator's generic tag error. - Correct comments and docstrings that named the old call path, the mapping types the atomic token check covers, and where token lists are copied. - Test that token metadata is dropped when it was not requested. - Benchmark: keep key order in the differential dumps, add custom tool call items, and report min and max instead of p99 and a block count. Signed-off-by: Ananth Subramaniam <[email protected]>
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP — LGTM — no concerns found.
I reviewed all five changed files and traced the affected request validation, token metadata handling, reply conversion, serialization, and replay paths. I found no qualifying correctness or compatibility issues.
This was a static review. I didn't execute tests or the differential benchmark.
Note: formal approval is not available for this request, so this review is posted as a comment only.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
|
The changes proposed here look good to me. Some performance testing results on a microbenchmark that models a real Gym server under load show good CPU performance improvements, especially for token-in harnesses.
|
|
|
||
| class ChatCompletionsConverterState(BaseModel): | ||
| @dataclass | ||
| class ChatCompletionsConverterState: |
There was a problem hiding this comment.
Nit for the compatibility section:
ChatCompletionsConverterState is now a dataclass: construction and assignment are no longer validated or coerced, and Pydantic methods such as model_dump and model_copy are gone. The final chat request validation still catches invalid messages.
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/42915699403 |
What changed and why
The Responses ↔ Chat Completions converter repeated the same work several times on every model call, as #3042 describes. Most of the repeats were in token metadata, but several also ran on calls that carry none. Every chat message was also validated against all six chat message types, because the message union had no discriminator. This PR removes that repeated work and keeps every validation boundary.
/v1/chat/completions.exclude_unsetdumps carry the same null fields.scripts/benchmark_chat_completions_conversion.pymeasures request conversion, reply conversion and the CPU cost of one model-server hop on synthetic conversations. The conversations include tools, images, reasoning, custom tool calls and outputs, and optional token metadata.--dump-outputswrites the outputs with generated IDs replaced and key order kept, so two checkouts can be diffed.The final validation of the built chat request stays. It is the only check on messages the converter itself builds, for example a developer message that ends up with an image part.
Closes #3042.
Results
Minimum CPU time over three runs, alternating between a
mainprocess and a branch process, each pinned to one core. The machine was shared and busy (load average about 30), so single runs varied by up to 2x. An earlier run on a different load gave the same picture.The hop includes parsing the request, dumping the chat request, validating the backend reply and serializing the response, which this PR does not change. Peak memory for request conversion at 512 turns with token metadata dropped from about 180 MB to about 60 MB.
Compatibility
mainfield for field and in key order for every benchmark conversation, with and without token metadata, includingexclude_unsetdumps.chat_message_rolethat names the valid roles. Before, the error listed one failure for each message type, and error locations named the member class. They now name the role, for examplemessages.0.assistant.content.ChatCompletionsConverterStateis now a dataclass. Code that assigns invalid values to its fields is no longer rejected at assignment; the final chat request validation still catches invalid messages.postprocess_chat_responselifts a reply that carries token metadata without callingpostprocess_assistant_message_dict, so an override of that method sees only replies without token metadata. Nothing in Gym or NeMo RL overrides it.Validation
PYTHONPATH=<checkout> python scripts/benchmark_chat_completions_conversion.py --dump-outputs out.jsonon this branch and onmaingives identical files at 1, 32 and 512 turns.mainon about 40,000 randomly generated requests and replies, plus hand-picked edge cases, across all dump modes and the replay round trip, and found no output differences. Chat message validation accepted and rejected the same messages as onmain.pytest tests/unit_tests/models/test_chat_completions_converter.pypasses.pytest -n 16 tests/unit_tests/on this branch and onmainin the same local venv fails on the same environment-dependent tests (missing optional dependencies). Two process tests failed on one side only under load and pass when run alone.PYTHONPATH=<checkout> python scripts/benchmark_chat_completions_conversion.py --dump-outputs out.jsonon both checkouts gives identical files.pre-commit run --fileson the changed files passes.Rollout evidence
N/A. This is a core-library performance change with byte-identical converted output on the differential corpus, so a model rollout would not show a difference.