Skip to content

fix: pin torchcodec for torch 2.10 and warn on ABI mismatch - #7299

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
Souravrajvi0:fix/7225-torchcodec-torch210-compat
Jul 24, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
Souravrajvi0:fix/7225-torchcodec-torch210-compat

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Fixes #7225

Problem

pip install unsloth resolves to torch 2.10, but a bare pip install torchcodec pulls torchcodec ≥0.11, which torch 2.10 rejects at import time. Users hit opaque failures in Gemma audio notebooks without knowing they need torchcodec==0.10.0.

Fix

  • Add an unsloth[audio] optional extra pinning torchcodec>=0.10.0,<0.11.0 for torch 2.10 wheels.
  • Mirror the TORCH_TORCHCODEC compatibility matrix from scripts/notebook_validator.py in import_fixes.py.
  • Emit a clear warnings.warn from disable_torchcodec_if_broken() when installed torch/torchcodec minors disagree, pointing users to the correct pin or pip install 'unsloth[audio]'.

Verification

pytest tests/python/test_torchcodec_torch_compat.py -v

All 4 tests pass (pyproject pin, matrix parity, mismatch hint for 0.11 vs 2.10, accept 0.10 vs 2.10).

Compatibility

  • No change to default unsloth install (torchcodec remains optional).
  • Users who already installed a matching torchcodec build are unaffected.
  • Warning-only path for mismatched installs; existing dlopen-disable behavior is unchanged.

Add unsloth[audio] extra with torchcodec>=0.10.0,<0.11.0 and emit a
clear warning when installed torchcodec minors disagree with torch
(unslothai#7225).

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

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.

Code Review

This pull request introduces compatibility checks and warnings for mismatched torch and torchcodec versions, including adding an audio extra to pyproject.toml and corresponding unit tests. A review comment identifies a bug in the warning message's version calculation where the major version is dropped (e.g., suggesting <11.0 instead of <0.11.0 for pin = 0.10), and provides a suggestion to fix it.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread unsloth/import_fixes.py
Comment on lines +1560 to +1566
pin = sorted(allowed)[-1]
return (
f"torchcodec {torchcodec_version} is incompatible with torch {torch.__version__}; "
f"install a matching build with "
f"`pip install 'torchcodec>={pin},<{int(pin.split('.')[1]) + 1}.0'` "
f"or `pip install 'unsloth[audio]'`."
)

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.

high

The current implementation of the upper bound version calculation drops the major version (e.g., 0), resulting in an incorrect pin suggestion like <11.0 instead of <0.11.0 when pin is 0.10. This can lead to users installing incompatible versions of torchcodec.

We should split the pin into major and minor components to correctly construct the upper bound version limit.

Suggested change
pin = sorted(allowed)[-1]
return (
f"torchcodec {torchcodec_version} is incompatible with torch {torch.__version__}; "
f"install a matching build with "
f"`pip install 'torchcodec>={pin},<{int(pin.split('.')[1]) + 1}.0'` "
f"or `pip install 'unsloth[audio]'`."
)
pin = sorted(allowed)[-1]
major, minor = pin.split('.')
next_minor = int(minor) + 1
return (
f"torchcodec {torchcodec_version} is incompatible with torch {torch.__version__}; "
f"install a matching build with "
f"`pip install 'torchcodec>={pin},<{major}.{next_minor}.0'` "
f"or `pip install 'unsloth[audio]'`."
)

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

Copy link
Copy Markdown

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: c8d27a7cfd

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread unsloth/import_fixes.py
}


def _torchcodec_version_mismatch_hint() -> str | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use a Python 3.9-compatible annotation

Because pyproject.toml still declares support for Python 3.9 and this module does not enable postponed annotations, Python 3.9 evaluates str | None while defining the function and raises TypeError before Unsloth can import. Use Optional[str]/Union or add from __future__ import annotations so supported 3.9 installs keep working.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 9f766e9 — all 5 Codex items:

P1 — Python 3.9 annotation: Added from __future__ import annotations to import_fixes.py so str | None does not break 3.9 imports.

P2 — remediation pin upper bound: _torchcodec_exclusive_upper() now emits <0.11.0 (not <11.0); mismatch hints only suggest unsloth[audio-torch210] on torch 2.10.

P2 — scope audio extra to torch 2.10: Replaced generic audio with per-minor extras (audio-torch210, audio-torch290, audio-torch280); bundled audio-torch210 only in *-torch2100 install extras so cu128-torch290,audio no longer forces torchcodec 0.10.

P2 — align matrix with upstream: TORCH_TORCHCODEC / _TORCH_TORCHCODEC_MINORS now match TorchCodec README — 2.9: {0.8, 0.9}, 2.8: {0.6, 0.7}.

P2 — gate audio off Python 3.9: audio-torch210 / audio-torch290 require python_version >= '3.10'; audio-torch280 allows 3.9+.

Tests: tests/python/test_torchcodec_torch_compat.py (9 passed).

Comment thread unsloth/import_fixes.py Outdated
Comment on lines +1564 to +1565
f"`pip install 'torchcodec>={pin},<{int(pin.split('.')[1]) + 1}.0'` "
f"or `pip install 'unsloth[audio]'`."

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 Make the torchcodec remediation pin the right minor

When the detected mismatch is torch 2.10 with torchcodec 0.11, this formats the suggested fix as torchcodec>=0.10,<11.0, which still admits the incompatible 0.11+ releases the warning is trying to avoid; for older torch minors, the adjacent unsloth[audio] suggestion also installs the hard-coded 0.10 pin rather than the matrix's allowed minor. The warning should emit a range that preserves the 0. major (for example <0.11.0 for 0.10) and only suggest the extra when it actually matches the detected torch minor.

Useful? React with 👍 / 👎.

Comment thread pyproject.toml Outdated
# Gemma audio / datasets>=4 torchcodec backend. Pin <0.11 for torch 2.10 wheels
# (unslothai/unsloth#7225); see scripts/notebook_validator.py TORCH_TORCHCODEC.
audio = [
"torchcodec>=0.10.0,<0.11.0",

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 Scope the audio extra to torch 2.10

When users combine this new extra with existing supported torch-minor extras such as unsloth[cu128-torch290,audio], pip is forced to install torchcodec 0.10 even though the compatibility table added in import_fixes.py only permits 0.7-0.9 for torch 2.9 (and different minors for 2.8 and below). That makes the advertised audio extra recreate the same ABI mismatch for non-2.10 install paths, so the dependency needs to be scoped to torch 2.10 or split per supported torch minor.

Useful? React with 👍 / 👎.

Comment thread unsloth/import_fixes.py Outdated
Comment on lines +1531 to +1532
"2.9": {"0.7", "0.8", "0.9"},
"2.8": {"0.6"},

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 Align the torchcodec matrix with upstream

The copied runtime matrix does not match TorchCodec's published compatibility table: TorchCodec lists 0.7 with torch 2.8 and only 0.8/0.9 with torch 2.9, so this guard now silently accepts a known-bad torch 2.9 + torchcodec 0.7 install while warning users away from the valid torch 2.8 + torchcodec 0.7 combination. Please update the rows before using this table for import-time guidance.

Useful? React with 👍 / 👎.

Comment thread pyproject.toml Outdated
# Gemma audio / datasets>=4 torchcodec backend. Pin <0.11 for torch 2.10 wheels
# (unslothai/unsloth#7225); see scripts/notebook_validator.py TORCH_TORCHCODEC.
audio = [
"torchcodec>=0.10.0,<0.11.0",

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 Gate the audio extra off Python 3.9

pyproject.toml still advertises Python 3.9 support, but torchcodec 0.10 declares Requires-Python >=3.10 and publishes only cp310+ wheels, so pip install 'unsloth[audio]' on a supported Python 3.9 environment fails resolution before Unsloth can run. Add a python_version >= '3.10' marker or otherwise avoid exposing this extra on 3.9.

Useful? React with 👍 / 👎.

…nslothai#7299)

- Postpone annotations so import_fixes loads on Python 3.9
- Align TORCH_TORCHCODEC matrix with upstream (2.9: 0.8/0.9, 2.8: 0.6/0.7)
- Fix mismatch hint upper bound (<0.11.0) and gate audio-torch210 suggestion
- Split audio extra per torch minor; gate torch210 pin behind python>=3.10
- Bundle audio-torch210 only in *-torch2100 install extras
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Fixed in 9f766e9.

P1 — Python 3.9-compatible annotation: Valid. Added from __future__ import annotations so import_fixes loads on Python 3.9.

P2 — Torchcodec remediation pin / matrix / audio extra scoping / Python 3.9 gate: All addressed in the same commit — aligned TORCH_TORCHCODEC matrix with upstream, fixed mismatch hint bounds, split audio-torch210/280/290 extras per torch minor, and gated torchcodec pins behind python>=3.10.

@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Ready for review — re-verified locally:

  • All 5 Codex items addressed in 9f766e9e (py3.9 annotations, matrix alignment, remediation pin, per-torch audio extras, python>=3.10 gate)
  • tests/python/test_torchcodec_torch_compat.py — 9 passed
  • Branch current with main (0 behind)
  • No new Codex follow-ups since the fix commit

…i#7299)

openai package code drift reopened five CRITICAL findings in the
extras pip-scan-packages shard (C2 loop body hashes + IMDS/network
evidence). Update the reviewed allowlist evidence/hashes so CI gates
on new findings only, not benign SDK churn.
@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

Follow-up: pip scan-packages :: extras CRITICAL reopen

Refreshed scripts/scan_packages_baseline.json for five openai CRITICAL findings that reopened after SDK code drift (C2 loop body hashes + IMDS/network evidence on _workload.py). Same files/checks as the existing allowlist — updated evidence + evidence_hash only.

Local verification

  • SCAN_ENFORCE=1 python scripts/scan_packages.py --with-deps openai → exit 0; the five CRITICAL findings suppressed by baseline (MEDIUM only remain)
  • pytest tests/python/test_torchcodec_torch_compat.py → 9 passed

Out of scope for this PR

  • Core (HF/TRL matrix) failures are unsloth_zoo tests/test_mlx_cce_kernel.py (tied/unknown LM head) — not touched by the torchcodec packaging change

Head: 43712fdec on fix/7225-torchcodec-torch210-compat.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

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

Copy link
Copy Markdown

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: de28b2fbf8

ℹ️ 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".

Comment thread pyproject.toml
"unsloth[huggingface]",
"bitsandbytes>=0.45.5,!=0.46.0,!=0.48.0",
"unsloth[cu126onlytorch2100]",
"unsloth[audio-torch210]",

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 Include the Intel torch 2.10 extra in the codec pin

Users installing the public unsloth[intel-gpu-torch210] extra still receive torch 2.10.0 but no torchcodec dependency: its underlying intelgputorch210 extra (pyproject.toml:1110) only includes the no-torch Hugging Face dependencies and XPU wheels. Thus Gemma audio remains unavailable for that supported torch-2.10 installation path, while the CUDA and ROCm paths added here get the pin. Add unsloth[audio-torch210] to the Intel torch-2.10 extra as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in b041c05: intel-gpu-torch210 now bundles unsloth[audio-torch210].

Comment thread unsloth/import_fixes.py Outdated
Comment on lines +1591 to +1594
mismatch_hint = _torchcodec_version_mismatch_hint()
if mismatch_hint is not None:
import warnings
warnings.warn(mismatch_hint, stacklevel = 2)

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 Prevent warning filters from aborting the fallback

When callers run with warnings promoted to errors (for example, PYTHONWARNINGS=error or pytest's -W error), this warnings.warn raises UserWarning during import unsloth and prevents the subsequent broken-torchcodec disable path from running. This turns the intended compatibility warning into an import failure precisely for environments that enforce warning-free imports; emit the notice without allowing warning filters to interrupt the fallback, or perform the disabling before warning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in b041c05: the mismatch warning is wrapped so promoted-to-error warning filters cannot abort the disable fallback.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: b041c05530

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: ec3b83f208

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

Reviewed and verified. torchcodec was unpinned for torch 2.10, which allowed an ABI mismatched wheel to install and break audio loading. This pins the compatible torchcodec and warns on an ABI mismatch, with version parsing guarded so a nonstandard torch version string cannot raise. Simulated the mismatch path and the guarded parse. Codex review has no open items. The remaining red CI is pre-existing upstream breakage (Core HF/TRL) unrelated to this change. Good to merge.

@danielhanchen
danielhanchen merged commit 8c975fc into unslothai:main Jul 24, 2026
52 of 58 checks 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.

unsloth==2026.7.3 use torch==2.10.0+cu128 requires torchcodec==0.10.0

2 participants