Repository navigation
fix(amd:audio): decode audio through PyAV when torchcodec installs but cannot load - #8642
Conversation
pyproject declares torchcodec on linux and win32, and notebook_validator enforces a torch/torchcodec compatibility matrix for it, but its wheel is Python-side only: at import it dlopens FFmpeg's avcodec/avutil. Windows ships neither, and pip has no FFmpeg wheel to depend on, so the package installs, reports a version, passes the matrix, and then fails at import. datasets 4.x decodes audio only through torchcodec and reports that failure as "To support decoding audio data, please install 'torchcodec'" -- naming a package that is already installed, on a host where the problem is FFmpeg. Nothing between the install and that message says otherwise. Both installers now probe the import once deps are in and say what they found. The message names FFmpeg, says decoding falls back to soundfile, and gives that fallback's real coverage (wav/flac/mp3/ogg via libsndfile, not m4a/aac/webm), so it reads as a limitation rather than as audio being broken. An absent torchcodec stays silent: that says nothing about FFmpeg, and the soundfile path still stands. Reporting only. Vendoring an FFmpeg full-shared build would mean redistributing an LGPL/GPL binary per arch, with its own update path, to reach formats most speech datasets do not use. That is a bigger decision than this gap needs.
- setup.sh read _SKIP_PYTHON_DEPS outside the base install that assigns it, so UNSLOTH_STUDIO_LLAMA_ONLY=1 aborted the run under `set -u`. Gate on _LLAMA_ONLY as well: that path installs no Python deps, so there is nothing new to report. The read is defaulted too, so a later edit cannot reintroduce the unbound case. - Importing torchcodec imports torch, so bound both probes the way this repo already bounds torch imports: `timeout 60` in setup.sh, next to the XPU probe that does the same, and Invoke-BoundedPythonProbe in setup.ps1. - Split the unloadable state into ffmpeg and broken. A damaged wheel or a torch/torchcodec ABI mismatch also raises at import, and answering that with "install an FFmpeg build" sends the reader at the wrong thing. The split is between torchcodec's own loader and an unrelated import error; it does not claim to pick between the causes that loader lists, which it prints together. - The missing-package test accepted all three states, so it could never fail. It now forces ModuleNotFoundError and requires absent. The behavioural tests read the probe out of setup.sh rather than carrying a copy of it, so they exercise what actually ships, and a new test holds the two installers to the same probe body.
install.ps1 runs setup.ps1 with SKIP_STUDIO_BASE=1, which skips the base install the other bare-`python` probes sit in, and nothing puts the venv on PATH. So the torchcodec probe read the system interpreter, found no torchcodec, and printed nothing on the path every Windows install actually takes.
Invoke-BoundedPythonProbe builds -c <body> by wrapping the body in double quotes, so the four in the comment closed that argument early. Windows split the rest into stray argv entries and python ran the truncated head, which still parses: the except-Exception branch lost its print, so the probe exited 0 with no output and the report never fired on Windows. The comment says the same thing without quoting it, and the pwsh test runs the shipped helper against the shipped body with an unloadable torchcodec on PYTHONPATH -- the one case a truncated body answers nothing at all.
Three findings from the review on 17ed9e7: The shell fallback for hosts with no coreutils timeout, stock macOS above all, ran the import with no deadline. The body now carries signal.alarm(60) like the XPU probe in this same script; the default action ends the process, which reads as the silent case. alarm is POSIX-only, and Windows is bounded by the helper. ModuleNotFoundError from a transitive module reported an installed torchcodec as absent, so a damaged install got no warning. Only a missing torchcodec itself is absent now, read off the exception name. The state was compared against all of stdout, so any banner ahead of the answer matched no arm and dropped the report. Both installers now read a TORCHCODEC= line, sed in setup.sh and a line-anchored match in setup.ps1.
A damaged wheel that fails on one of torchcodec's own submodules raises ModuleNotFoundError naming torchcodec.<sub>, and folding that back to the top-level name reported an installed-but-broken package as absent, which is silent. An absent package always names itself exactly from import torchcodec, so the exact comparison loses nothing and treats torchcodec.* as broken. The test asserting the submodule form was absent was wrong; it now expects broken.
|
End-to-end check on the AMD box this came from: RX 9060 XT (gfx1200), ROCm 7.1, Windows 11. torchcodec is installed and cannot load. Importing it raises the The second half of that message holds up too. With torchcodec unable to load, an audio LoRA finetune of So the warning is accurate in both directions: it names the real dependency, and it does not overstate the damage, since the formats libsndfile covers keep working. |
The loader message names a missing FFmpeg, a torch mismatch and another native dependency in one breath, so matching libtorchcodec in the traceback sent every one of them to install FFmpeg. Documenting that limit in a comment was not enough: the probe now asks the system instead, through find_library plus a PATH walk for the versioned avutil-*.dll names Windows uses and find_library cannot glob. No FFmpeg on the loader path keeps the ffmpeg state and its advice. FFmpeg present with the loader still failing is a new native state, which names the two remaining causes without picking between them, since nothing available here can. The tests pin the FFmpeg answer in both directions, so a runner that happens to have it installed cannot flip the states this rests on.
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Final state at 62d987d, after four Codex rounds (stream_index, PyAV declared in the audio extras and the Studio floor raised to 12.3, any torchcodec import failure treated as broken, librosa import guarded):
Two failures in tests/studio/install/test_dependency_pass_skips.py show up here on this branch and identically on a clean main checkout, so they are not from this change. |
…h installers for PR unslothai#8642
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Update for 472295d, which moves the torchcodec probe out of the two installer heredocs into
|
…ixes instead for PR unslothai#8642
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |


The gap
On a Windows host, audio training fails with:
torchcodec is installed.
importlib.metadata.version("torchcodec")returns0.10.0. The message names a package that is already there.Why
pyproject.tomldeclares torchcodec forlinuxandwin32on x86_64/AMD64, andscripts/notebook_validator.pyenforces a torch/torchcodec compatibility matrix for it, citingunslothai/notebooks#261. Both are about versions. The wheel is Python-side only: at import it dlopens FFmpeg'savcodec/avutil.Windows ships neither, and pip has no FFmpeg wheel to depend on, so the package installs cleanly, reports a version, satisfies the matrix, and fails at
import:datasets4.x decodes audio only through torchcodec, and its availability check reports an unloadable install the same way it reports a missing one. Nothing between setup finishing and that error says otherwise.The change
Both installers probe the import once python deps are in, and report what they found, in the same shape as the existing
whisper.cppsteps (what is wrong, what to do, what still works):The coverage claim is measured, not assumed.
soundfile0.14 bundles libsndfile 1.2.2, whose formats include WAV, FLAC, MP3, OGG and AIFF, and exclude m4a/aac/webm. So this is a real limitation on a narrow set of containers, not audio being broken, and the message says so.An absent torchcodec stays silent: that says nothing about FFmpeg, and
utils/datasets/audio_decode.pystill provides the soundfile path. A probe that cannot run is silent for the same reason.Reporting only, deliberately
Vendoring an FFmpeg full-shared build would mean redistributing an LGPL/GPL binary per arch, with its own update and CVE path, to reach containers most speech datasets do not use. That is a bigger decision than this gap needs, and it can be made later without undoing this. The tradeoff: m4a/webm datasets still fail, but with an error that finally names FFmpeg.
Tests
Seven, four of which fail without the change, across both installers via
parametrize. They cover the three-way probe outcome (ok/absent/unloadable, exercised by forcing each import result), that the reported line names FFmpeg, soundfile and the real format coverage, that the probe sits behind the skip-python-deps guard, and that setup.sh still parses underbash -n.The heredoc test feeds the script to bash as bytes. A text pipe on Windows re-encodes the box characters setup.sh draws its rules with, and translates every
\nback to\r\n, which bash rejects as a syntax error the real file does not have. I hit both in that order.test_setup_llama_cpp_backend.pyhas 20 failures here and the identical 20 on a cleanmaincheckout; they shell out to setup.sh under Windows bash and are untouched by this change.Related
The "Not addressed" note in #8436 pointed at exactly this. That PR made the soundfile fallback reach the training worker; this one makes the installer say when the fallback is what you are running on. No file overlap with #8436 or #8625.
Update: decode through PyAV instead of only reporting
Commits 881794c through 82c1171 turn the report into a fix, and drop the installer changes entirely. When torchcodec is installed but cannot load, audio rows decode through soundfile first and PyAV second, so m4a, aac and webm work without an FFmpeg full-shared build. The fallback lives in two places: Studio's
utils/datasets/audio_decode.py, andunsloth/import_fixes.py, wheredisable_torchcodec_if_brokeninstalls the same decoder ondatasets.features.audio.Audioso anyimport unslothscript gets it. It only runs on datasets 4.x with a torchcodec that fails to load, honoursAudio(stream_index=...)(an absolute index, or the default track throughstreams.bestwhen unset, as torchcodec does), and leaves a working torchcodec alone. The dataset preview now summarises decoded audio cells whether they arrive as lists or numpy arrays. Everyaudio-torch2xxextra lists soundfile and PyAV beside torchcodec, the Studio PyAV floor is 12.3 (older wheels need numpy<2), resampling falls through to PyAV when librosa is absent or cannot import, and the disabler treats any exception from the torchcodec import as a broken install, matching the installer probe.studio/setup.shandstudio/setup.ps1are untouched: the fix needs no installer step. The one report a user gets is a warning atimport unsloththat names why torchcodec did not load (FFmpeg missing from the loader path, FFmpeg present so the cause is elsewhere, or a failure that never reached libtorchcodec) and that audio datasets now decode through soundfile and PyAV. Tests:studio/backend/tests/test_audio_dataset_decode.pygained PyAV cases andtests/test_datasets_audio_fallback.pycovers the library patch, including an AST check that keeps both decoders identical.Studio evidence: same host, same broken torchcodec, before and after
Two isolated Studio installs (merge base 1e07231 and head bcfe23e) under separate
UNSLOTH_STUDIO_HOMEs, each withtorchcodec==0.9.0against torch 2.11 sofrom torchcodec.decoders import AudioDecoderraises the "Could not load libtorchcodec" error. A three-row parquet with an m4a Audio column is uploaded through the dataset picker, then opened with View dataset.Before:
/api/hub/datasets/check-formatanswers 500 withFormat not recognisedfrom soundfile and the dialog shows its error state. After: three rows,is_audiotrue, every cell<audio, 46080 samples @ 22050 Hz, 2.1s>.Recording of the before flow:
Recording of the after flow: