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
Interface
No new interfaces are introduced.
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 exploring the repository structure to understand what we're working with.
Assistant
Now let me look at the
preprocess_bshd_enginefunction to understand what shapes it expects.Assistant
Let me look at the
preprocess_thd_enginefunction to see how it handles this.Assistant
Let me search for where
teacher_topk_log_probsis created to understand its structure.Assistant
Let me look at the tests for distillation to understand how this function is used.
Assistant
Let me check the existing CPU tests for the Megatron losses function.
Assistant
Let me look at the existing tests to understand how to reproduce the error.
Assistant
Let me see how
teacher_topk_log_probsis created in the model output.Assistant
Let me look at how teacher_logprobs is created and what shape it has when it reaches the losses function.
Assistant
Let me look at the existing usage of preprocess functions to understand what they look like in different contexts.
Assistant
Let me check the actual line numbers in the current file.
Assistant
Let me first reproduce the error to confirm it.
Assistant
Let me check the structure of the nested tensor to understand the issue.
Assistant
Good, so I've confirmed:
is_nestedis Trueshapeis(2, j1, 64)(with j1 being the jagged dimension)offsets().diff()returns the sequence lengths correctlyvalues()returns(total_nnz, topk)dense tensor- Indexing with
nested[i]returns a 2D tensor of shape(seqlen_i, topk)for 3D nested
So the issue is:
- In
preprocess_bshd_engine,input_ids_bshd[i, :seqlen_i] = input_ids[i]would try to assign a 2D tensor to a 1D slot. - The output shape needs to be
(b, seqlen/cp_size, topk)instead of(b, seqlen/cp_size).
Let me alsocheck the 2D nested tensor case to understand:
Assistant
Let me look at the existing function
preprocess_bshd_enginemore carefully to understand the changes needed.Assistant
Let me look at the THD version of the function for comparison.
Assistant
Let me also check if there are any usages of these functions with 3D nested tensors besides the distillation case.
Assistant
Now let me look at the call in model_forward.py:386 to understand the context.
Assistant
Let me also look at how the user is using this in practice.
Assistant
OK so this commit renamed
preprocess_bshd_no_paddingandpreprocess_thd_no_paddingtopreprocess_bshd_engineandpreprocess_thd_engine. Let me check theold functions to see how they handled 3D nested tensors.Assistant
Let me check the THD function for the 3D case to verify it works:
Assistant
Now I have a clear understanding of the issue. Let me make my plan:
Problem:
preprocess_bshd_engineinverl/models/mcore/util.pyexpects a 2D nested tensor where each element is a 1D sequenceteacher_topk_log_probsis a 3D nested tensor where each element is a 2D(seqlen_i, topk_dim)matrix- The function tries to assign a 2D tensor to a 1D slot, causing shape mismatch
Plan: Modify
preprocess_bshd_engineto auto-detect 3D nested tensors (whereinput_ids.values().ndim == 2) and handle them by:- Creating
input_ids_bshdwith shape(batch_size, local_max_seqlen, topk_dim)instead of(batch_size, local_max_seqlen) - Adjusting the padding logic to preserve the
topk_dim
Let me implement the fix:
Assistant
Now let me implement the fix. I'll modify
preprocess_bshd_engineto auto-detect 3D nested tensors.Assistant
Now let me verify the fix works by writing a test that simulates the function logic without megatron.
Assistant
All tests pass. Let me also verify the CP split logic is correct by checking that data is correctly distributed across ranks.
Assistant
The CP split is correct for 3D nested tensors. Let me also test the partial sequence case (where seqlen < max_seqlen).
Assistant
Let me fix my test - my expected masks were incorrect. Let me trace through more carefully.
Assistant
Now let me also verify the 2D case is still correct.
Assistant
Both 2D and 3D nested tensor cases work correctly. Let me clean up the temporary test files and verify the fix in context.
Assistant
Let me view the changes I made and verify the fix is complete.
Assistant
Let me run the existing tests to make sure I didn't break anything.
Assistant
All tests pass. Let me also verify the syntax of the modified file.
Assistant
Let me also test the
need_roll=Truecase to make sure it works for 3D nested tensors.Assistant
There's an issue with
expand_asfor 3D nested tensors. Let me fix this in the function as well.Assistant
I need to also fix the
expand_ascalls to handle 3D nested tensors. Let me update them.Assistant
Let me check what's in thereexactly and use a different approach to replace.
Assistant
Let me use
editcommand to fix the expand_as calls:Assistant
Now let me verify the fix is complete and works with all the test cases.
Assistant
All tests pass. Let me verify the modified file.
Assistant
Let me run the existing tests to verify the fix doesn't break anything.
Assistant
Let me also run other potentially affected tests.
Assistant
Let me run a more specific set of tests that don't have missing dependencies.
Assistant
Let me check if these failing tests were already failing before my changes.
Assistant
Good - these failures are pre-existing and unrelated to my changes. Let me restore my changes.
Assistant
Let me also clean up temporary files and run a final sanity check.
Assistant
Let me also verify the syntax is correct.
Assistant
Let me do a final review of the changes.
Assistant
Let me verify the changes pass all relevant tests one more time.
Assistant
All tests pass. Let me also check the git status to confirm the changes are ready.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.