Repository navigation
fix(audio): install the audio decode shim before the training worker loads a dataset - #8436
Conversation
…s a dataset An audio dataset failed training on any host without an FFmpeg full-shared build, reporting `datasets`' own "To support decoding audio data, please install 'torchcodec'" while torchcodec was installed and the soundfile fallback that exists for this case was importable. ensure_audio_decoding() clears TORCHCODEC_AVAILABLE and installs the soundfile decoder, and the dataset format-check endpoint already calls it. That runs in the API process. The training worker is a separate process and starts without the shim, and its only call sits in the audio preprocessing branches, which run after load_dataset() and format_and_template_dataset() have already read rows. An Audio column decodes during that read, so the load raised before anything installed a decoder, and the broad handler reported it as "Error loading dataset". Call it at the same point load_and_format_dataset already quiets third-party progress bars, documented there as the first point before any load. The audio branches keep their own call and their own message, which names FFmpeg and is what a caller should see when neither backend works. Ordering is asserted in a dependency-free module: the existing audio decode suite importorskips soundfile and librosa, so it skips on precisely the hosts this ordering protects.
|
To use Codex here, create a Codex account and connect to github. |
Review pass on head
|
…fail a text run The call this branch added for the training worker sits above load_and_format_dataset's own try, so anything it raises escapes the method instead of landing in the handler that reports a dataset error. ensure_audio_decoding() catches ImportError, OSError and RuntimeError around its probes, but `import librosa` pulls numba, llvmlite and numpy, whose ABI mismatches surface as AttributeError, TypeError or SystemError at least as often. Before the hoist that blast radius was audio runs only; from the new position it would fail every training run, text included, on a host with a half-broken librosa. That is worse than the bug being fixed. Guarded the same way as the progress-bar block directly above it. The audio branches further down still call it and still report the failure that names FFmpeg, so nothing a caller sees changes. Pinned with an ast assertion rather than source text: the first statement in the method that calls the shim must be a try that holds the call directly and whose handler catches Exception. The method's own large try contains a later call but not directly, so it cannot satisfy the assertion by accident.
…load' into fix/audio-decode-before-dataset-load
…fore-dataset-load
|
Pushed since the review comment above:
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
datasets<4 has no config.TORCHCODEC_AVAILABLE, so reading it raised AttributeError, which the function catches nowhere and its callers do not expect. pyproject still allows datasets>=3.4.1, and on such an install the call in the audio branch of load_and_format_dataset is unguarded, so an audio run failed reporting a missing config attribute instead of training. Those versions decode audio through soundfile already, so there is nothing to install and nothing to patch: return True. Verified against a real datasets 3.6.0 in an isolated venv, plus 4.2.0, 4.3.0 and 4.4.0 which are unchanged.
|
Reviewed this together with #8625 and validated it on a host that reproduces the bug natively: this box has torchcodec 0.10.0 installed with FFmpeg's shared libraries absent, so The bug is real, and the fix worksDriving the real
The log counts are the part a screenshot cannot carry: the decoder is installed in the worker, which is the process that lacked it. The slice path is the "case 2" your description flags as untested, and it is the one that fires here. One fix pushed
Those versions decode through soundfile already, so there is nothing to install: return Verified in isolated Compatibility
Two notes, neither blockingA text run gains an audio side effect. On a broken-torchcodec host the speculative call patches A pre-existing hazard, not yours. Both PRs merge cleanly with each other, and the suites pass: 85 in the audio/preflight modules on this branch, plus 282 source-level and 26 end-to-end checks across the merge base, each PR, and both combined. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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 bug
Training an audio dataset fails on a host without an FFmpeg full-shared build:
torchcodec is installed, and the soundfile fallback built for this case is importable. The run dies at step 0 with an upstream message naming a package that is already present.
Root cause
ensure_audio_decoding()clearsTORCHCODEC_AVAILABLEwhen torchcodec cannot dlopen its libraries and installs the soundfile decoder in its place. The dataset format-check endpoint calls it early, in the API process. The training worker is a separate process and starts without the shim.The worker's only call sits behind a model-type guess:
That is enough on the common path: a correctly detected audio model reaches the guard before its preprocessing branch decodes anything, because plain
load_dataset()is lazy. It is not enough in two cases, and both leave the shim uninstalled while something reads a row:1. Audio detection returns
Noneon a dataset that does have audio.detect_audio_typereadstokenizer_config.json, and returnsNonewhen the file is unreadable for any reason: gated repo, offline, or a cache that holds the weights but not the tokenizer files. The guard tests_audio_type, so it never fires, and the run falls through to the text format path, which reads rows and raisesdatasets' own torchcodecImportError. The broad handler reports that as "Error loading dataset".2. A manual dataset slice. The slice branch materialises rows with
Dataset.from_list(list(stream.take(rows_to_stream))), which decodes the Audio column before the guard is reached, independent of what detection returned.Case 1 is what produced the log above. Both halves are visible in one run:
The worker never logs the first line.
The fix
Call
ensure_audio_decoding()inload_and_format_datasetat the same point it already quiets third-party progress bars, documented there as the first point before any load. A decoder should be in place before anything reads a row, rather than depending on a model-type guess made earlier from a file that may not be readable.The audio branches keep their own call and their own error, which names FFmpeg and the soundfile pair, and is what a caller should see when neither backend works. This only has to precede them. The return value is deliberately ignored at the early call: the loader does not yet know whether the dataset has an audio column, and a non-audio run on an FFmpeg-less host must stay unaffected.
Verified in a real run
Same host, same dataset, on this branch:
The worker now logs the line it never logged before, the dataset loads, and no torchcodec error appears anywhere. The run then fails further along for an unrelated reason, which is case 1's other consequence: with
_audio_typeempty the model is treated as text, so the snac branch is skipped and the text path cannot map anaudio+textdataset. That is a separate bug indetect_audio_typesilently conflating "not an audio model" with "could not tell", and is not addressed here.Tests
Three ordering assertions in a new dependency-free module. The existing
test_audio_dataset_decode.pyimportorskips soundfile and librosa, so it skips on hosts with neither, which is precisely where this ordering is load-bearing. I put the test there first and it silently skipped, which is how I noticed.Verified as a real regression test: with the trainer change stashed, two of the three fail; with it applied, all pass.
One pre-existing failure,
test_the_soundfile_decoder_resamples_to_the_cast_rate, reproduces on a cleanmaincheckout atc9d92df12and is untouched by this change.Not yet covered by a test: the slice path in case 2. Worth adding if reviewers want it pinned rather than argued from the source.
Not addressed
Why an FFmpeg-less Windows host has torchcodec at all.
pyproject.tomldeclares it forwin32/AMD64 andscripts/notebook_validator.pyenforces a torch/torchcodec compatibility matrix, but nothing installs FFmpeg or checks that the wheel can load. That is a packaging question and a bigger change; this PR just makes the fallback reach the process that needs it.