Skip to content

[feat] support opd rl#9641

Open
hjh0119 wants to merge 7 commits into
modelscope:mainfrom
hjh0119:opd-rl
Open

[feat] support opd rl#9641
hjh0119 wants to merge 7 commits into
modelscope:mainfrom
hjh0119:opd-rl

Conversation

@hjh0119

@hjh0119 hjh0119 commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request implements Megatron On-Policy Distillation as RL (OPD-RL) by integrating teacher KL as a GRPO advantage across local and Ray-based GKD and GRPO trainers. It also introduces OpenEnvScheduler and OpenEnvWrapper to support multi-turn rollouts in OpenEnv environments. The review feedback highlights several critical issues, including missing imports and potential AttributeErrors in gkd_helpers.py, a rank-guarding mismatch in teacher_mixin.py that could cause runtime failures, and synchronous blocking calls in OpenEnvScheduler that should be run in separate threads to avoid blocking the asyncio event loop. Additionally, defensive checks are recommended to prevent potential IndexError, StopIteration, and type promotion issues.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread swift/rlhf_trainers/gkd_helpers.py
Comment thread swift/megatron/trainers/teacher_mixin.py Outdated
Comment thread swift/rollout/multi_turn.py
Comment thread swift/rollout/multi_turn.py
# Teacher returned logprobs for the full sequence (prompt + response + end tokens).
# Locate the response portion by matching token IDs from the end.
rti = response_token_ids[i]
if isinstance(rti[0], list):

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.

high

If rti is empty (e.g., if the model generated an empty response), accessing rti[0] will raise an IndexError. We should check if rti is non-empty before accessing its first element.

Suggested change
if isinstance(rti[0], list):
if rti and isinstance(rti[0], list):

Comment on lines +858 to +860
tid, info = next(iter(pos_lp.items()))
lps.append([info['logprob']])
ixs.append([int(tid)])

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.

medium

If pos_lp is empty, next(iter(pos_lp.items())) will raise a StopIteration exception. We should handle the empty case defensively to avoid crashes.

Suggested change
tid, info = next(iter(pos_lp.items()))
lps.append([info['logprob']])
ixs.append([int(tid)])
if pos_lp:
tid, info = next(iter(pos_lp.items()))
lps.append([info['logprob']])
ixs.append([int(tid)])
else:
lps.append([0.0])
ixs.append([0])

"""
d = teacher_per_token_logps - policy_per_token_logps
per_token = torch.exp(d) - d - 1
return per_token * completion_mask

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.

medium

To prevent potential type promotion or precision mismatch errors (especially when using bfloat16 or float16 for logprobs and bool/long for the completion mask), we should explicitly cast completion_mask to the dtype of d before multiplication.

Suggested change
return per_token * completion_mask
return per_token * completion_mask.to(d.dtype)

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.

1 participant