Skip to content

Give the torchcodec placeholder a real __spec__ - #11325

Merged
danielhanchen merged 7 commits into
mainfrom
fix/torchcodec-stub-carries-a-real-spec
Sep 19, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
fix/torchcodec-stub-carries-a-real-spec

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Every leg of the Notebooks CI smoke matrix has been failing in Verify imports under spoof, on 2026-09-16, -17, -18 and today's cron:

ValueError: torchcodec.__spec__ is None

Why

The job stubs torchcodec because no CPU wheel is published, and it did so with a bare types.ModuleType, whose __spec__ is None. That is not an inert difference. importlib.util.find_spec raises for a module sitting in sys.modules with no spec, rather than returning None, so every probe of the form

importlib.util.find_spec("torchcodec") is not None

turns into a ValueError. transformers reaches exactly that probe while importing audio_utils:

peft/__init__.py -> peft/auto.py -> peft/config.py -> peft/utils/constants.py
  -> from transformers import BloomPreTrainedModel
  -> transformers/models/bloom/modeling_bloom.py -> modeling_layers -> processing_utils
  -> audio_utils.py:60  is_torchcodec_available()
  -> import_utils.py:1145  _is_package_available("torchcodec")
  -> importlib.util.find_spec  ->  ValueError

so the job died on import peft. The stub has always been under-specified; a transformers that probes on this import path is what exposed it.

The fix

Give the placeholder a real ModuleSpec, in one shared helper (tests/_torchcodec_stub.py) that both stubbing steps call instead of carrying a copy each.

It still answers "not available" on a machine without the wheel: the spec exists, then importlib.metadata.version raises PackageNotFoundError, which is what the stub wants and what a machine lacking the wheel should report.

Verification

Against the real probe, not by inspection:

stub transformers.is_torchcodec_available()
bare types.ModuleType (current) ValueError: torchcodec.__spec__ is None
with a real ModuleSpec (this PR) returns an answer, no raise

And with importlib.metadata.version made to raise PackageNotFoundError, the way a runner with no torchcodec distribution behaves, _is_package_available("torchcodec") returns False.

Three tests: the placeholder answers find_spec where a bare ModuleType raises; it never displaces a real torchcodec; both workflow steps go through the helper with no hand-rolled ModuleType left behind. Removing the spec line fails the first. Putting a hand-rolled stub back in the workflow fails the third. tests/notebooks: 315 passed, 2 skipped.

Not verified here

This box cannot run the smoke job itself. The probe above is exercised directly against the installed transformers, which is 4.57.6 here against Colab's 5.16.1, so what is demonstrated is the mechanism (find_spec raising on a spec-less module, and not raising once the spec is there) rather than a replay of the runner. import peft still fails on this box after the fix, but on bitsandbytes needing real CUDA, two lines further into peft/__init__.py than the ValueError it used to stop at.

Pairs with #11324, which acknowledges the Colab oracle drift in the same workflow. The two failures are independent.

Every leg of the Notebooks CI smoke matrix has been failing in 'Verify imports
under spoof' with

    ValueError: torchcodec.__spec__ is None

The job stubs torchcodec because no CPU wheel is published, and it did so with a
bare types.ModuleType, whose __spec__ is None. That is not inert:
importlib.util.find_spec RAISES on a module sitting in sys.modules with no spec
rather than returning None, so every probe shaped like

    importlib.util.find_spec("torchcodec") is not None

becomes a ValueError. transformers reaches exactly that probe while importing
audio_utils (is_torchcodec_available -> _is_package_available), which peft pulls
in through BloomPreTrainedModel, so the smoke job died on 'import peft'. The
stub has always been under-specified; a transformers that probes on this path is
what exposed it.

Give the placeholder a real ModuleSpec, in one shared helper the two stubbing
steps both call instead of carrying a copy each. It still answers 'not
available' on a machine without the wheel: the spec exists, then
importlib.metadata.version raises PackageNotFoundError, which is the outcome the
stub wants.

Verified against the real probe rather than by inspection. With the old stub,
transformers' is_torchcodec_available() raises ValueError: torchcodec.__spec__
is None -- the CI failure exactly. With the new one it returns an answer, and
with importlib.metadata.version made to raise PackageNotFoundError, the way a
runner with no torchcodec distribution behaves, _is_package_available reports
False.

Three tests: the placeholder answers find_spec where a bare ModuleType raises,
it never displaces a real torchcodec, and both workflow steps go through the
helper with no hand-rolled ModuleType left behind. Removing the spec line fails
the first; putting a hand-rolled stub back in the workflow fails the third.
tests/notebooks: 315 passed, 2 skipped.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-19T08:06:38.970445Z 137d317 Manual request
🔒 Security Review ✅ Completed 2026-09-19T06:55:48.226047Z 7b16301 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 7b16301e82

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

The new shared stub is imported by both smoke steps, but the workflow's
pull_request.paths listed only its sibling _zoo_aggressive_cuda_spoof.py. A PR
touching nothing but the helper would therefore merge without the smoke matrix
or test_smoke_install_contract.py ever running -- a runtime dependency of the
job added without registering it as one.

Listed it, and added a test that derives the set from the steps themselves
rather than restating it, so the next shared helper is caught here instead of by
a silent green run. Removing either entry fails that test by name, including the
sibling that was already correct.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…es entry

The spec-carrying module fixed the ValueError and walked straight into the next
failure. transformers 5.16.1 calls _is_package_available("torchcodec") with no
return_version, so it decides presence from the spec alone and never consults
metadata; audio_utils line 61 then runs

    TORCHCODEC_VERSION = version.parse(importlib.metadata.version("torchcodec"))

at import time. With no distribution installed that is PackageNotFoundError, so
the smoke matrix would have traded one import failure for another, and the test
missed it by asserting only that find_spec answered.

Write a real (tiny) placeholder distribution onto sys.path instead: a package
directory and a .dist-info carrying Name and Version, marked in INSTALLER as
ours. Both probes then answer honestly. The version is deliberately 0.0.0,
below the 0.3.0 floor load_audio requires of torchcodec, so backend="auto"
resolves to librosa and nothing ever asks the placeholder to decode.

Verified in a venv with no torchcodec, running the two probes transformers runs:

    bare ModuleType         is_torchcodec_available() RAISES ValueError
    ModuleType with a spec  available -> True, then PackageNotFoundError
    placeholder dist        available -> True, version 0.0.0, auto -> librosa

All three are now a parametrized test, run under python -S so a real torchcodec
on the host cannot answer for the wrong reason -- they run everywhere instead of
skipping wherever the wheel happens to be present. Reverting the helper to the
sys.modules shape fails the third row; raising the version to 0.9.0 fails the
floor test. tests/notebooks: 319 passed, 2 skipped.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 19, 2026
pre-commit-ci Bot and others added 3 commits September 19, 2026 07:12
Source lint blocked on HOISTED-IMPORT-UNUSED: I added `import importlib.util`
for a skipif that guarded these probes against a host with a real torchcodec,
then replaced that guard with `python -S` in the subprocess so they run
everywhere, and left the import behind. Every remaining importlib reference in
the file is inside the subprocess script string, which runs in another
interpreter, or in a comment. tests/notebooks: 319 passed, 2 skipped.
Repo tests (CPU, rest) went red on this PR while green on main at the same
three commits:

    FAILED tests/test_studio_root_resilience.py::test_infer_studio_home_swallows_permission_error
    ModuleNotFoundError: No module named 'utils.native_path_leases'

My own doing, and the same shape as the two leaks fixed earlier today. To reach
the helper I wrote `sys.path.insert(0, str(REPO / "tests"))`, which is
process-wide and permanent. tests/ holds a utils/ package, so from that moment
every later test in the worker resolved `import utils.*` to tests/utils
instead of studio/backend/utils. This file stayed green and a different file
paid for it.

Load tests/_torchcodec_stub.py through spec_from_file_location instead, under a
private module name, so nothing is added to sys.path at all. The insert inside
the probe subprocess stays: that is a separate interpreter that exits
immediately.

Reproduced and pinned rather than assumed. Running this file and
test_studio_root_resilience.py in one process fails 3 tests with the insert in
place and passes 40 without it, and a new test asserts both that the directory
is absent from sys.path after the helper loads and that the insert is not back
in the source. tests/notebooks: 320 passed, 2 skipped.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: dfdca5b4b4

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

The guard I added with the fix was wrong, and only CI could show it: it asserted
that REPO/tests is absent from sys.path, and pytest's own prepend import mode
puts the basedir of every collected test module there. So it failed in the
auto-discovered lane for a reason that has nothing to do with this file, while
the real casualty -- tests/test_studio_root_resilience.py -- was already fixed
and gone from that run's failures.

Assert what the fix actually claims: loading the helper does not change
sys.path. That holds whoever else has added to it, and it is the property whose
absence caused the breakage. Making _load_stub_helper insert the directory again
fails it by name and prints what it added.

tests/notebooks plus test_studio_root_resilience.py in one process: 326 passed,
2 skipped.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 137d317c09

ℹ️ 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
danielhanchen merged commit 08e2278 into main Sep 19, 2026
29 checks passed
@danielhanchen
danielhanchen deleted the fix/torchcodec-stub-carries-a-real-spec branch September 19, 2026 08:23
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
* Give the torchcodec placeholder a real __spec__

Every leg of the Notebooks CI smoke matrix has been failing in 'Verify imports
under spoof' with

    ValueError: torchcodec.__spec__ is None

The job stubs torchcodec because no CPU wheel is published, and it did so with a
bare types.ModuleType, whose __spec__ is None. That is not inert:
importlib.util.find_spec RAISES on a module sitting in sys.modules with no spec
rather than returning None, so every probe shaped like

    importlib.util.find_spec("torchcodec") is not None

becomes a ValueError. transformers reaches exactly that probe while importing
audio_utils (is_torchcodec_available -> _is_package_available), which peft pulls
in through BloomPreTrainedModel, so the smoke job died on 'import peft'. The
stub has always been under-specified; a transformers that probes on this path is
what exposed it.

Give the placeholder a real ModuleSpec, in one shared helper the two stubbing
steps both call instead of carrying a copy each. It still answers 'not
available' on a machine without the wheel: the spec exists, then
importlib.metadata.version raises PackageNotFoundError, which is the outcome the
stub wants.

Verified against the real probe rather than by inspection. With the old stub,
transformers' is_torchcodec_available() raises ValueError: torchcodec.__spec__
is None -- the CI failure exactly. With the new one it returns an answer, and
with importlib.metadata.version made to raise PackageNotFoundError, the way a
runner with no torchcodec distribution behaves, _is_package_available reports
False.

Three tests: the placeholder answers find_spec where a bare ModuleType raises,
it never displaces a real torchcodec, and both workflow steps go through the
helper with no hand-rolled ModuleType left behind. Removing the spec line fails
the first; putting a hand-rolled stub back in the workflow fails the third.
tests/notebooks: 315 passed, 2 skipped.

* Make the smoke helpers path triggers, and derive the list from the steps

The new shared stub is imported by both smoke steps, but the workflow's
pull_request.paths listed only its sibling _zoo_aggressive_cuda_spoof.py. A PR
touching nothing but the helper would therefore merge without the smoke matrix
or test_smoke_install_contract.py ever running -- a runtime dependency of the
job added without registering it as one.

Listed it, and added a test that derives the set from the steps themselves
rather than restating it, so the next shared helper is caught here instead of by
a silent green run. Removing either entry fails that test by name, including the
sibling that was already correct.

* Install the torchcodec placeholder as a distribution, not a sys.modules entry

The spec-carrying module fixed the ValueError and walked straight into the next
failure. transformers 5.16.1 calls _is_package_available("torchcodec") with no
return_version, so it decides presence from the spec alone and never consults
metadata; audio_utils line 61 then runs

    TORCHCODEC_VERSION = version.parse(importlib.metadata.version("torchcodec"))

at import time. With no distribution installed that is PackageNotFoundError, so
the smoke matrix would have traded one import failure for another, and the test
missed it by asserting only that find_spec answered.

Write a real (tiny) placeholder distribution onto sys.path instead: a package
directory and a .dist-info carrying Name and Version, marked in INSTALLER as
ours. Both probes then answer honestly. The version is deliberately 0.0.0,
below the 0.3.0 floor load_audio requires of torchcodec, so backend="auto"
resolves to librosa and nothing ever asks the placeholder to decode.

Verified in a venv with no torchcodec, running the two probes transformers runs:

    bare ModuleType         is_torchcodec_available() RAISES ValueError
    ModuleType with a spec  available -> True, then PackageNotFoundError
    placeholder dist        available -> True, version 0.0.0, auto -> librosa

All three are now a parametrized test, run under python -S so a real torchcodec
on the host cannot answer for the wrong reason -- they run everywhere instead of
skipping wherever the wheel happens to be present. Reverting the helper to the
sys.modules shape fails the third row; raising the version to 0.9.0 fails the
floor test. tests/notebooks: 319 passed, 2 skipped.

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

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

* Drop the import left behind by the removed skipif

Source lint blocked on HOISTED-IMPORT-UNUSED: I added `import importlib.util`
for a skipif that guarded these probes against a host with a real torchcodec,
then replaced that guard with `python -S` in the subprocess so they run
everywhere, and left the import behind. Every remaining importlib reference in
the file is inside the subprocess script string, which runs in another
interpreter, or in a comment. tests/notebooks: 319 passed, 2 skipped.

* Load the stub helper by path instead of putting tests/ on sys.path

Repo tests (CPU, rest) went red on this PR while green on main at the same
three commits:

    FAILED tests/test_studio_root_resilience.py::test_infer_studio_home_swallows_permission_error
    ModuleNotFoundError: No module named 'utils.native_path_leases'

My own doing, and the same shape as the two leaks fixed earlier today. To reach
the helper I wrote `sys.path.insert(0, str(REPO / "tests"))`, which is
process-wide and permanent. tests/ holds a utils/ package, so from that moment
every later test in the worker resolved `import utils.*` to tests/utils
instead of studio/backend/utils. This file stayed green and a different file
paid for it.

Load tests/_torchcodec_stub.py through spec_from_file_location instead, under a
private module name, so nothing is added to sys.path at all. The insert inside
the probe subprocess stays: that is a separate interpreter that exits
immediately.

Reproduced and pinned rather than assumed. Running this file and
test_studio_root_resilience.py in one process fails 3 tests with the insert in
place and passes 40 without it, and a new test asserts both that the directory
is absent from sys.path after the helper loads and that the insert is not back
in the source. tests/notebooks: 320 passed, 2 skipped.

* Assert the helper leaves sys.path alone, not that tests/ is off it

The guard I added with the fix was wrong, and only CI could show it: it asserted
that REPO/tests is absent from sys.path, and pytest's own prepend import mode
puts the basedir of every collected test module there. So it failed in the
auto-discovered lane for a reason that has nothing to do with this file, while
the real casualty -- tests/test_studio_root_resilience.py -- was already fixed
and gone from that run's failures.

Assert what the fix actually claims: loading the helper does not change
sys.path. That holds whoever else has added to it, and it is the property whose
absence caused the breakage. Making _load_stub_helper insert the directory again
fails it by name and prints what it added.

tests/notebooks plus test_studio_root_resilience.py in one process: 326 passed,
2 skipped.

---------

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