ISSUE DESCRIPTION OPD losses error for Megatron when i use megatron for compute losses…
- User
ISSUE DESCRIPTION
OPD losses error for Megatron when i use megatron for compute losses the code position is: /verl/trainer/distillation/megatron/losses.py Line 248 teacher_topk_log_probs_cp_split, *_ = preprocess_bshd_engine(teacher_topk_log_probs, pre_process=True)
the teacher_topk_log_probs is [b, length, 64] but preprocess_bshd_engine this function need [n, length] dim, so error happens
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /verl, current file: none) bash-$
Assistant
Let me start by understanding the issue. The problem is in
/verl/trainer/distillation/megatron/losses.pyat line 248 (or around there). Theteacher_topk_log_probshas shape[b, length, 64](3D), butpreprocess_bshd_engineexpects[n, length](2D). So the error happens because the tensor has an extra dimension.Let me explore the repository structure and understand the code.
Assistant
Let me look at the specific file mentioned in the issue.
Assistant
Let me find the actual path to this file.
Assistant
Okay, the path is
/verl/verl/trainer/distillation/megatron/losses.py. Let me read it.Assistant
Now let me look at the
preprocess_bshd_engineandpreprocess_thd_enginefunctions to understand their expected input format.Assistant
Now let me find where
compute_forward_kl_topkis called to understand how the data flows.Assistant
Let me look at the calling code and the FSDP version for comparison.
Assistant
Let me look at the FSDP version for comparison:
Assistant
Now let me look at
preprocess_thd_enginetooAssistant
Let me read that function.
Assistant
Let me look at the test file to understand the expected
Assistant
Let me find the test file.
Assistant
Let me read that test file.
Assistant
Let me check who else calls
preprocess_bshd_engineandpreprocess_thd_engine.Assistant
Now I have a clear picture. Let me create a script to reproduce the error, then fix
Assistant
Let me examine how nested tensors are used in model_forward.py and understand the shapes better.
Assistant
Let me see the full `preprocess_thd_engine
Assistant
Now I understand the issue. Let me look at both functions fully and then make targeted fixes. The core problem is that
preprocess_bshd_engine(andpreprocess_thd_enginefor the CP case) assume per-element data is 1D, butteacher_topk_log_probshas 2D per element[seqlen, topk].Let me read the full
preprocess_bshd_engineto see all places that need changes:Assistant
Let me also check the
preprocess_for_mindspeedfunction since it's called frompreprocess_thd_engine:Assistant
Let me read that function.
Assistant
Now I have a full understanding of the issue. Let me implement the fix. I need to modify both
preprocess_bshd_engineandpreprocess_thd_engineto handle nested tensors with multi-dimensional per-element data (like[seqlen, topk]).Let me start with `preprocess_bshd_
Assistant
Let me use the edit command directly. First, let me check the exact lines I need to modify.
Assistant
Now let me fix
preprocess_bshd_engine. I'll make several edits:Edit 1: Add extra_dims computation and update input_ids_bshd creation
Assistant
Let me use the replace command with the correct syntax.
Assistant
Let me use sed to make the changes directly.
Assistant
Let me use Python to make all the edits at once.
Assistant
Let me verify the changes.
Assistant
Now let me verify the fix works with a reproduction script.
Assistant
Let me also simulate the CP case to make sure it works.
Assistant
Let me also verify backward compatibility with 1D elements (token ids):
Assistant
Let me also verify the
preprocess_thd_engineCP padding fix:Assistant
Now let me run the existing tests to check if anything is broken. Let me first find the relevant tests.
Assistant
Let me check if the module imports correctly and run the relevant CPU tests.
Assistant
The megatron module isn't available in this environment (expected since it's GPU-specific). Let me verify the syntax is correct with a Python syntax check.
Assistant
Let me check for relevant CPU tests I can run.
Assistant
Let me check if those test failures are pre-existing by reverting my changes and running the tests.
Assistant
Same failures on original code — pre-existing. Let me restore my changes.
Assistant
Let me run the distillation test on CPU.
Assistant
All distillation tests pass. Let me also check that the
losses.pyfile in the megatron path is importing correctly by checking syntax.Assistant
Let me review the complete diff of my changes.
Assistant
The diff looks correct. Let me do one final comprehensive test simulating all edge cases.
Assistant
All tests pass. Let me go through the checklist:
Checklist:
-
[x] If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I reproduced the error: when a nested tensor with 2D per-element data (e.g.,
[seqlen, 64]for teacher top-k log probs) is passed topreprocess_bshd_engine, it fails because the function creates output tensors without the extra dimension ([batch, max_seqlen]instead of ).
- Yes, I reproduced the error: when a nested tensor with 2D per-element data (e.g.,
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.