unsloth/unsloth
Daniel Han aba57d0872
disable_torchcodec_if_broken: also patch datasets and clean sys.modules (#5483)
* disable_torchcodec_if_broken: also patch datasets and clean sys.modules (#5446)

transformers's _torchcodec_available was being flipped to False already,
but datasets keeps its own datasets.config.TORCHCODEC_AVAILABLE flag
(datasets >= 4.0) that gates every torchcodec call site inside
datasets/features/{audio,video}.py, datasets/features/features.py and the
three datasets formatters. Without flipping that flag, transformers
falls back to librosa but datasets still routes through torchcodec and
re-raises the same RuntimeError, which is what users hit on Colab when
libavutil is missing.

Also pops half-loaded torchcodec submodules + datasets.features._torchcodec
from sys.modules so a later re-import does not re-trigger the failed
native-library dlopen.

Verified live state after the patch on a Colab-like broken-torchcodec env:
  transformers._torchcodec_available     = False
  transformers.is_torchcodec_available() = False
  datasets.config.TORCHCODEC_AVAILABLE   = False
  stale torchcodec entries in sys.modules = []

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

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

* disable_torchcodec_if_broken: seat sys.modules[torchcodec]=None sentinel

The first commit on this branch flipped the transformers and datasets
availability flags, but a few unconditional torchcodec call sites in
datasets / torchaudio (e.g. Audio.encode_example does
"from torchcodec.encoders import AudioEncoder" outside the
TORCHCODEC_AVAILABLE gate) still hit a cryptic RuntimeError from the
broken native library load.

Seating sys.modules["torchcodec"] = None makes any subsequent
"import torchcodec" / "from torchcodec.X import Y" raise
ModuleNotFoundError (subclass of ImportError) which the existing
try/except ImportError blocks in datasets / torchaudio catch and
re-raise as the clean "please install torchcodec" message users get
when they uninstall torchcodec manually. This was the workaround in
issue #5446.

Also makes find_spec("torchcodec") return None on re-entry, so the
function is a strict no-op on the second call.

Verified across 12 scenarios in temp/torchcodec_test/: healthy
torchcodec untouched, broken torchcodec sentinel-blocked, datasets 3.x
without the flag handled, no-datasets-installed handled, py3.11 Colab-
exact env (transformers==4.56.2 + trl==0.22.2) handled. Side-by-side
on the user's exact stack: Audio.encode_example switches from
"RuntimeError: Could not load libtorchcodec" to
"ImportError: To support encoding audio data, please install 'torchcodec'.".

* disable_torchcodec_if_broken: trim comments

Same behaviour, shorter docstring and inline comments.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-05-16 16:24:48 -07:00
..
dataprep Fix raw text paragraph break normalization (#4884) 2026-04-09 04:45:43 -07:00
kernels Fix: Add missing utf-8 encoding to text-mode file operations (#5356) 2026-05-14 18:15:27 +04:00
models revert: stop touching DEVICE_TYPE == "cuda" branches for CPU CI (#5473) 2026-05-15 19:41:09 -07:00
optimizers feat: Implement Q-GaLore optimizer and custom embedding learning rate… (#4511) 2026-03-25 01:03:10 -07:00
registry Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" 2025-12-01 07:24:58 -08:00
utils Add check to disable xformers on newer GPUs (#4342) 2026-03-16 22:42:38 -07:00
__init__.py MLX training support for Studio on Apple Silicon (#5340) 2026-05-14 05:24:20 -07:00
_auto_install.py Add PyTorch 2.10 and xformers 0.0.34 support (#3985) 2026-02-05 05:56:26 -08:00
_gpu_init.py revert: stop touching DEVICE_TYPE == "cuda" branches for CPU CI (#5473) 2026-05-15 19:41:09 -07:00
chat_templates.py Fix/issue 3667 vicuna template (#5357) 2026-05-14 18:49:47 +04:00
device_type.py revert: stop touching DEVICE_TYPE == "cuda" branches for CPU CI (#5473) 2026-05-15 19:41:09 -07:00
import_fixes.py disable_torchcodec_if_broken: also patch datasets and clean sys.modules (#5483) 2026-05-16 16:24:48 -07:00
ollama_template_mappers.py Re-apply #4939: updated models template mappers (#4950) 2026-04-15 07:52:12 -07:00
save.py Fix: Add missing utf-8 encoding to text-mode file operations (#5356) 2026-05-14 18:15:27 +04:00
tokenizer_utils.py fix: 3 patch_* helpers — fast_lora import, sft_trainer Union, openenv OSError (#5319) 2026-05-07 00:12:09 -07:00
trainer.py Trim trainer.py import-fix comment to one line 2026-05-06 07:22:56 +00:00