Skip to content

Raise ImportError on stable torch/torchvision mismatch#4065

Merged
danielhanchen merged 3 commits into
mainfrom
daniel/raise-importerror-on-stable-torchvision-mismatch
Feb 16, 2026
Merged

Raise ImportError on stable torch/torchvision mismatch#4065
danielhanchen merged 3 commits into
mainfrom
daniel/raise-importerror-on-stable-torchvision-mismatch

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

@danielhanchen danielhanchen commented Feb 16, 2026

Summary

  • make torchvision_compatibility_check() fail fast for stable version mismatches, including inferred mappings for newer stable torch releases
  • keep warning-only behavior for custom/source builds and prerelease builds
  • remove warning downgrade for stable unknown-table versions so mismatches raise ImportError

Why

A warning on stable mismatches can defer failure until runtime with errors like operator torchvision::nms does not exist. This change surfaces the incompatibility at import-time with a clear actionable message.

Validation

  • local runtime sanity check with current environment (torch==2.10.0+cu130, torchvision==0.24.1) confirms torchvision_compatibility_check() now raises ImportError

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 torchvision compatibility check by transitioning from a warning to an ImportError for stable torch and torchvision version mismatches. This change ensures that incompatible stable setups are identified and reported immediately at import time, preventing obscure runtime errors and providing clearer guidance to users.

Highlights

  • Fail-Fast for Stable Mismatches: Implemented fail-fast behavior for stable torch/torchvision version mismatches by raising an ImportError immediately upon detection.
  • Preserved Warning Behavior: Maintained the existing warning-only behavior for custom, source, or prerelease builds, allowing these specific scenarios to proceed without an ImportError.
  • New Regression Tests: Added comprehensive regression tests to validate the updated compatibility check logic, covering stable mismatches, environment variable bypass, and prerelease warnings.

🧠 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
  • tests/test_import_fixes_torchvision_compat.py
    • Added new test cases to verify the torchvision compatibility check behavior.
    • Included tests for stable version mismatches raising ImportError.
    • Added tests to confirm the UNSLOTH_SKIP_TORCHVISION_CHECK environment variable bypasses the check.
    • Included tests to ensure prerelease mismatches still emit a warning.
  • unsloth/import_fixes.py
    • Removed the 'is_in_known_table' variable as its logic is no longer required for conditional warning.
    • Modified the torchvision_compatibility_check function to raise an ImportError for stable torch/torchvision mismatches.
    • Adjusted the conditional logic to only downgrade to a warning for custom/source or prerelease builds, ensuring stable mismatches fail fast.
Activity
  • No human activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
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

The pull request introduces a new test file tests/test_import_fixes_torchvision_compat.py to cover the torchvision_compatibility_check function. The main change in unsloth/import_fixes.py is to make stable torch/torchvision mismatches raise an ImportError instead of just a warning, while still allowing custom/prerelease builds to only warn. This is a good change to prevent runtime errors from incompatible versions. The new tests adequately cover the intended behavior of the compatibility check, including stable mismatches, environment variable skipping, and prerelease warnings.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 333810210c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


import pytest

os.environ.setdefault("UNSLOTH_SKIP_TORCHVISION_CHECK", "1")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove module-level env mutation from test module

Setting UNSLOTH_SKIP_TORCHVISION_CHECK at module import time makes this test file mutate process-wide state during pytest collection, and that state is never restored; when this file is imported before other tests that import unsloth, those tests will silently skip the torchvision compatibility guard and can miss real regressions. This creates order-dependent behavior in full-suite runs, so the env override should be scoped inside individual tests/fixtures (e.g., via monkeypatch).

Useful? React with 👍 / 👎.

@danielhanchen danielhanchen merged commit ce1d386 into main Feb 16, 2026
4 checks passed
@danielhanchen danielhanchen deleted the daniel/raise-importerror-on-stable-torchvision-mismatch branch February 16, 2026 03:14
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
* Raise ImportError for stable torchvision mismatches

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove torchvision compatibility tests from PR scope

---------

Co-authored-by: Daniel Hanchen <danielhanchen@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

1 participant