Skip to content

Studio: fix image and video loads on torchao 0.14 (FqnToConfig import error with diffusers 0.41) - #13308

Merged
danielhanchen merged 4 commits into
mainfrom
studio-torchao-fqn-alias
Oct 11, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-torchao-fqn-alias

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Fixes image and video loads failing with cannot import name 'FqnToConfig' from 'torchao.quantization' on Studio installs that pair diffusers 0.41 with torchao 0.14.

Problem

Loading MiniMax-H3 (or any model or LoRA that goes through diffusers.loaders.peft) on these installs fails before any weights are read:

Failed to import diffusers.models.transformers.transformer_minimax_h3 because of the following error (look up to see its traceback):
Failed to import diffusers.loaders.peft because of the following error (look up to see its traceback):
cannot import name 'FqnToConfig' from 'torchao.quantization'

Fix

alias_torchao_fqn_to_config() in core/_torchao_stub.py gives torchao older than 0.15 the torchao.quantization.FqnToConfig name as an alias of ModuleFqnToConfig: the same alias torchao 0.15 keeps, the other way round. It is called with the existing pre-diffusers hooks, right after the Windows ROCm torchao stub, in every place that imports diffusers first: run.py, core/inference/diffusion.py, core/training/diffusion_train_common.py (the spawned diffusion trainer) and the diffusers prewarm in utils/torch_warmup.py.

  • The alias is deferred to the first import diffusers through a meta path finder that never resolves a module itself. Importing torchao in run.py would load torch into the server process and every spawn child before main.py sets up the GPU environment; on torch 2.8 + torchao 0.14.0 that took import run from 0.1 s / 25 MB to 6.6 to 16 s / about 665 MB. With the deferral import run is unchanged (0.10 s, 25 MB, no torch) and the alias lands when diffusers loads. If diffusers or torchao.quantization is already loaded, it aliases at once.
  • Reads the version from package metadata and imports torchao.quantization only when an alias is due, so a torchao >= 0.15 install never imports torchao earlier than before.
  • No-op on a stubbed (Windows ROCm), absent, or already-new torchao, never overwrites an existing FqnToConfig, and a failure inside the finder never breaks the diffusers import.
  • Existing broken installs recover with the Studio update alone, no reinstall or torchao change.

Testing

  • Real venv (uv, Python 3.12, torch 2.8.0+cu128, torchao 0.14.0, diffusers 0.41.0, transformers 5.5.0, peft 0.18.1): on the merge base, importing Studio's core.inference.diffusion, core.inference.video, core.training.diffusion_train_common and the run.py hook sequence followed by diffusers.models.transformers.transformer_minimax_h3 all fail with the exact error above. With this PR all four succeed, plus diffusers.loaders.peft and TorchAoConfig.
  • Same venv: with the alias, torchao 0.14 int8 weight-only quantizes a tiny Flux transformer and the pipeline denoises to a finite image; a quantized Linear stack gives bitwise-identical output with and without the alias.
  • Windows ROCm (spoofed): the torchao stub is left untouched, diffusers imports as on main.
  • After the alias, what still fails on torchao 0.14 is unchanged from main and is not an import error: constructing TorchAoConfig (diffusers', which Studio's image and video paths do not use, and transformers') raises the library's own "requires torchao >= 0.15.0", exactly as on main; transformers 5.x treats torchao < 0.15 as unavailable.
  • New tests/test_torchao_fqn_alias.py: the alias for 0.13 / 0.14 / 0.14.1 / local-tagged and dev versions, idempotence, 0.15+ left alone and not imported, absent and stubbed torchao, an existing FqnToConfig kept, the alias deferred until a real import diffusers and applied then, a failing alias not breaking the import, and the hook order in the three module-level hook blocks. tests/test_diffusers_prewarm.py asserts the alias runs in the prewarm's stub sequence before diffusers is imported. 45 passed on ubuntu-latest, macos-15 and windows-latest.

diffusers 0.41 imports torchao.quantization.FqnToConfig unguarded, and that
name first exists in torchao 0.15.0. Studio pairs torch <= 2.8 (and torch 2.9
on installs predating the 0.15 pin) with torchao 0.14.0, whose class is still
called ModuleFqnToConfig, so every model that imports diffusers.loaders.peft
(MiniMax-H3, any LoRA load) failed with "cannot import name 'FqnToConfig'".

alias_torchao_fqn_to_config() adds the new name as an alias of the old class,
the same alias torchao 0.15 keeps the other way round. It runs with the other
pre-diffusers hooks (run.py, the diffusion and diffusion-training modules,
the diffusers prewarm), reads the version from package metadata, imports
torchao only when the alias is due, and leaves a stubbed, absent or >= 0.15
torchao alone.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 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-10-11T11:21:21.658357Z e05fc7a 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@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: 8ab79c1bd5

ℹ️ 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 studio/backend/run.py
install_xformers_windows_rocm_stub()
hide_xformers_built_for_another_torch()
install_torchao_windows_rocm_stub()
alias_torchao_fqn_to_config()

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 Defer the alias until after GPU environment initialization

On every standard run.py launch with torchao < 0.15, this call eagerly imports torchao.quantization, whose package initialization imports torch, before main.py can set CUDA_DEVICE_ORDER, normalize allocator settings, restore the desktop ROCm environment, or register Windows ROCm DLL directories. This violates the explicit pre-torch initialization ordering in main.py and can make affected older-torch installations select the wrong GPU or initialize ROCm with missing settings; install the alias without importing torch here, or perform it only after that initialization but before diffusers is imported.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in e05fc7a: the alias is now deferred to the first diffusers import through a meta path finder, so run.py and spawn children no longer import torch or torchao (import run on torch 2.8 + torchao 0.14.0: 0.10 s / 25 MB again, alias still applied when diffusers loads).

…port

Importing torchao.quantization from run.py loaded torch into the server
process and every spawn child before main.py's pre-torch environment
setup, costing seconds and about 600 MB on the torchao 0.14 hosts the
alias targets. A meta path finder now aliases on the first import of
diffusers, the only package that needs the name.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Oct 11, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: e05fc7a847

ℹ️ 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 Author

The reds here come from this PR; the same files pass on main (513 passed):

  • (Python 3.13, rest): test_video_vae_bf16_decode_rocm.py errors with AttributeError: module 'diffusers' has no attribute 'AutoencoderKLWan', and test_video_backend.py::test_validate_rejects_local_file_suffix_kind_mismatch plus test_train_precision_scheme_contract.py::test_the_trainer_accepts_exactly_what_the_schema_advertises fail. The new torchao stub / lazy-import changes appear to stop diffusers from exposing the video VAE class and make DiffusionLoraConfig.normalized() reject a precision the request schema still advertises.
  • Repo tests (CPU, rest): test_formatter_fixed_point flags studio/backend/core/_torchao_stub.py and studio/backend/tests/test_torchao_fqn_alias.py; running python scripts/run_ruff_format.py on those two files fixes it.

danielhanchen added a commit that referenced this pull request Oct 11, 2026
…3336)

test_torchao_fqn_alias.py (from #13308) imports a fake diffusers package from tmp_path. monkeypatch.delitem records only entries that existed, so in an xdist worker where the real diffusers had not been imported yet, the fake one stayed in sys.modules and every later test in that worker saw a diffusers without AutoencoderKLWan or its pipelines: test_video_vae_bf16_decode_rocm, test_video_backend and test_train_precision_scheme_contract fail on main and on unrelated PRs (#13315). An autouse fixture now restores the diffusers/torchao entries of sys.modules exactly after every test.

Also apply the ruff-format-with-kwargs hook to the two files #13308 added, which test_formatter_fixed_point flags on main.
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