fix: use prompt token length for advantage group extraction and fix token mask#2176
fix: use prompt token length for advantage group extraction and fix token mask#2176yfw wants to merge 2 commits into
Conversation
|
/ok to test 628a248 |
628a248 to
7d96ad3
Compare
7d96ad3 to
d5961e5
Compare
|
/ok to test 7d96ad3 |
@macandro96, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
|
/ok to test d5961e5 |
The previous role-based extraction (`_extract_prompt_only_messages`) broke on multi-turn prompts containing assistant messages in the conversation history — it would strip them, corrupting the prompt IDs used for advantage estimation. Replace with `extract_initial_prompt_messages()` which uses the `length` field to identify the original prompt boundary. Applied to both sync and async GRPO paths. Closes #1960 Co-Authored-By: Jiaqi Zeng <jiaqiz@nvidia.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Yi-Fu Wu <yifu.wu@gmail.com>
Signed-off-by: Anish Mahishi <amahishi@cw-dfw-cs-001-vscode-02.cm.cluster>
d5961e5 to
20adf67
Compare
| role = cast(str, message["role"]) | ||
| token_ids = cast(torch.Tensor, message["token_ids"]) | ||
|
|
||
| if role == "assistant" and "generation_logprobs" in message: |
There was a problem hiding this comment.
we didn't check generation_logprobs before, is there a reason we need to check it now?
| if role == "assistant" and "generation_logprobs" in message: | |
| if role == "assistant": |
There was a problem hiding this comment.
Yes - I think we want to set token_mask = 1 for assistant part of messages where generation logprobs are available. If its not available, it means - that assistant text was part of input prompt for a multi-turn conversation and should be excluded while computing gradients.
There was a problem hiding this comment.
This was a separate commit for super. I combined it into this PR as it was related.
| prompt_only_message_logs, | ||
| pad_value_dict={"token_ids": tokenizer.pad_token_id}, | ||
|
|
||
| prompt_batched_flat, prompt_input_lengths = ( |
There was a problem hiding this comment.
nit: looks prompt_input_lengths is never used.
| prompt_batched_flat, prompt_input_lengths = ( | |
| prompt_batched_flat, _ = ( |
| prompt_batched_flat, _ = batched_message_log_to_flat_message( | ||
| prompt_only_message_logs, | ||
| pad_value_dict={"token_ids": tokenizer.pad_token_id}, | ||
| prompt_batched_flat, prompt_input_lengths = ( |
There was a problem hiding this comment.
nit: same as above
| prompt_batched_flat, prompt_input_lengths = ( | |
| prompt_batched_flat, _ = ( |
|
@yfw @HeyyyyyyG could you help to take a review as well? |
This PR fixes two multi-turn GRPO training issues:
The previous role-based extraction (
_extract_prompt_only_messages) broke on multi-turn prompts containing assistant messages in the conversation history — it would strip them, corrupting the prompt IDs used for advantage estimation.Replace with
extract_initial_prompt_messages()which uses thelengthfield to identify the original prompt boundary. Applied to both sync and async GRPO paths.GRPO token loss masks previously unmasked every message with
role == "assistant". In multi-turn data, assistant messages can be part of the prompt history, not generated rollout output, so those tokens should not contribute to the policy loss. This PR updates masking so only assistant messages produced by generation, identified by existinggeneration_logprobs, are trainable. Missinggeneration_logprobsare still filled with zeros for downstream tensorization.Closes #1960 and #1956
What does this PR do ?
Add a one line overview of what this PR aims to accomplish.
Issues
List issues that this PR closes (syntax):
Usage
# Add a code snippet demonstrating how to use thisBefore your PR is "Ready for review"
Pre checks:
Additional Information