Fix Boolean value of Tensor ambiguity error in mistral.py#3790
Fix Boolean value of Tensor ambiguity error in mistral.py#3790danielhanchen merged 4 commits intounslothai:mainfrom
Conversation
- Fix bug in rope_embedding.py where is_contiguous was used without parentheses, causing the method object (always truthy) to be evaluated instead of calling the method. This fixes issue unslothai#3781 where fast rope backpropagation was broken for zero strided/non-contiguous tensors. - Remove duplicate `import torch` in rl.py (lines 20 and 25) - Remove duplicate `import functools` and `import types` in vision.py 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace `or` operator with explicit `is None` check when getting n_items from kwargs. The `or` operator fails when the value is a Tensor because Python cannot determine the boolean value of a multi-element tensor. Fixes unslothai#3766 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary of ChangesHello @yurekami, 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 primarily addresses a critical 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. 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 primarily fixes a RuntimeError: Boolean value of Tensor with more than one value is ambiguous in unsloth/models/mistral.py by replacing the or operator with an explicit is None check. This is a correct and necessary fix. The PR also includes several other changes not mentioned in the description:
- A bug fix in
unsloth/kernels/rope_embedding.pywhereis_contiguouswas used as a property instead of a method. - Removal of a redundant
import torchinunsloth/models/rl.py. - Removal of
import functoolsandimport typesinunsloth/models/vision.py. While removingfunctoolsis fine as it's imported elsewhere, removingtypeswill cause aNameErroras it is used in the file without being imported from another module. This is a critical issue that needs to be addressed.
I've added a comment to fix the critical issue in vision.py. I'd also recommend updating the PR title and description to reflect all the changes made.
| from typing import Optional, Tuple, List, Union | ||
| import re, inspect, sys | ||
| import contextlib | ||
| import types |
|
Thank you @yurekami ! |
Summary
RuntimeError: Boolean value of Tensor with more than one value is ambiguouswhen training Qwen3 MoE modelsoroperator with explicitis Nonecheck when gettingn_itemsfrom kwargsBug Fix (Fixes #3766)
The code was using Python's
oroperator to get fallback values:When
num_items_in_batchis a Tensor, Python'soroperator tries to evaluate it as a boolean, which fails for multi-element tensors with:The fix uses explicit
is Nonecheck (matching the pattern inllama.py):Files Changed
unsloth/models/mistral.py- Fixed 2 instances (lines 310-312 and 369-371)Test plan
llama.py