Handle missing CSM depth decoder loss during loss aggregation#496
Handle missing CSM depth decoder loss during loss aggregation#496danielhanchen wants to merge 5 commits intomainfrom
Conversation
Summary of ChangesHello @danielhanchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the CSM model's loss computation by introducing conditional logic to handle cases where the depth decoder loss might be absent. This prevents runtime errors during training by ensuring that loss components are only summed when both are valid, thereby improving the stability of the model's forward pass without altering behavior when both losses are present. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a TypeError that could occur during loss aggregation when the depth decoder loss is not present. The fix is sound and handles all cases where backbone_loss or depth_decoder_loss might be None. I've added one suggestion to refactor the loss aggregation logic to be more concise and idiomatic, which could also improve scalability if more loss components are added in the future.
| if backbone_loss is None: | ||
| loss = depth_decoder_loss | ||
| elif depth_decoder_loss is None: | ||
| loss = backbone_loss | ||
| else: | ||
| loss = backbone_loss + depth_decoder_loss |
There was a problem hiding this comment.
While the current logic is correct, it can be simplified to be more concise and scalable for potentially adding more loss components in the future. You can use a list comprehension to filter out None values and then use sum().
losses = [l for l in (backbone_loss, depth_decoder_loss) if l is not None]
loss = sum(losses) if losses else None|
Pushed follow-up fixes to this PR branch for transformers 5.x CSM compatibility. New commits:
What changed:
Validation notes (local, no CSM short-circuit):
|
|
Additional updates pushed after further validation:
Current local status on transformers 5.1.0 (no CSM short-circuit):
Smoke harness run with standard CSM shortcut remains passing after these updates. |
|
btw did you ask it to verify correctness on both transformers v4 and v5? The two seem to have different args/params |
|
Ref: #495 |
Summary
Tensor + NoneTypeduring training loss computationRoot Cause
In the patched
CsmForConditionalGeneration.forward,losswas always computed as:backbone_loss + depth_decoder_lossWhen
depth_decoder_lossisNone, this raises:TypeError: unsupported operand type(s) for +: 'Tensor' and 'NoneType'Changes
unsloth_zoo/temporary_patches/misc.pyValidation
python -m compileall unsloth_zoo/temporary_patches/misc.pyNotes