Skip to content

Fix Boolean value of Tensor ambiguity error in mistral.py#3790

Merged
danielhanchen merged 4 commits intounslothai:mainfrom
yurekami:fix/tensor-boolean-ambiguity
Dec 29, 2025
Merged

Fix Boolean value of Tensor ambiguity error in mistral.py#3790
danielhanchen merged 4 commits intounslothai:mainfrom
yurekami:fix/tensor-boolean-ambiguity

Conversation

@yurekami
Copy link
Copy Markdown
Contributor

@yurekami yurekami commented Dec 28, 2025

Summary

  • Fix RuntimeError: Boolean value of Tensor with more than one value is ambiguous when training Qwen3 MoE models
  • Replace or operator with explicit is None check when getting n_items from kwargs

Bug Fix (Fixes #3766)

The code was using Python's or operator to get fallback values:

# Before (BUG):
n_items = kwargs.get("num_items_in_batch", None) or kwargs.get("n_items", None)

When num_items_in_batch is a Tensor, Python's or operator tries to evaluate it as a boolean, which fails for multi-element tensors with:

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

The fix uses explicit is None check (matching the pattern in llama.py):

# After (FIXED):
n_items = kwargs.get("num_items_in_batch", None)
if n_items is None:
    n_items = kwargs.get("n_items", None)

Files Changed

  • unsloth/models/mistral.py - Fixed 2 instances (lines 310-312 and 369-371)

Test plan

  • Verified Python syntax is valid
  • Pattern matches the working implementation in llama.py

yurekami and others added 2 commits December 29, 2025 07:14
- 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>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 RuntimeError that arises during the training of Qwen3 MoE models due to an ambiguous boolean evaluation of Tensors. The fix involves refactoring the kwargs retrieval logic in mistral.py to use explicit is None checks instead of the or operator, preventing implicit Tensor-to-boolean conversions. Additionally, it includes minor corrections to method calls in rope_embedding.py and general code cleanup by removing unused imports across several files, enhancing code stability and maintainability.

Highlights

  • Bug Fix: Tensor Ambiguity Error: Resolved a RuntimeError: Boolean value of Tensor with more than one value is ambiguous that occurred when training Qwen3 MoE models. This error stemmed from Python's or operator attempting to evaluate a multi-element Tensor as a boolean.
  • Improved kwargs Retrieval Logic: Replaced the problematic or operator with an explicit is None check when retrieving n_items from kwargs in mistral.py. This ensures robust handling of None values without triggering Tensor-related boolean evaluation errors.
  • Method Call Correction: Corrected calls to is_contiguous in rope_embedding.py from property access (.is_contiguous) to method invocation (.is_contiguous()) for Q, K, dQ, and dK tensors.
  • Code Cleanup: Removed unused torch import from unsloth/models/rl.py and unused functools and types imports from unsloth/models/vision.py.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 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.py where is_contiguous was used as a property instead of a method.
  • Removal of a redundant import torch in unsloth/models/rl.py.
  • Removal of import functools and import types in unsloth/models/vision.py. While removing functools is fine as it's imported elsewhere, removing types will cause a NameError as 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.

Comment on lines 71 to -75
from typing import Optional, Tuple, List, Union
import re, inspect, sys
import contextlib
import types
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.

critical

The import types statement is being removed, but types.MethodType is used on line 895. The types module is not imported from any other file that vision.py depends on (like _utils.py). This will lead to a NameError at runtime. Please re-add import types.

@danielhanchen
Copy link
Copy Markdown
Contributor

Thank you @yurekami !

@danielhanchen danielhanchen merged commit 3423f66 into unslothai:main Dec 29, 2025
1 check passed
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.

[Bug] RuntimeError: Boolean value of Tensor with more than one value is ambiguous

2 participants