Skip to content

Studio: keep diffusion LoRA loading working on torchao 0.18 with peft 0.18 - #12494

Merged
danielhanchen merged 1 commit into
mainfrom
studio/peft-torchao-018-lora-guard
Oct 2, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio/peft-torchao-018-lora-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Studio's diffusion and video processes load LoRAs through pipe.load_lora_weights, which goes through peft's LoRA dispatch table. peft 0.18.1 (Studio's pin in extras-no-deps.txt) runs dispatch_torchao for every target before dispatch_default, and that dispatcher does from torchao.quantization import LinearActivationQuantizedTensor. torchao 0.18.0 deleted that class, and _select_torchao_spec installs 0.18.0 on torch 2.12 and later. So on those installs every diffusion LoRA load raises:

ImportError: cannot import name 'LinearActivationQuantizedTensor' from 'torchao.quantization'

#11168 fixed this for import unsloth (fix_peft_torchao_missing_tensor_subclass in unsloth/import_fixes.py), but the diffusion process deliberately never runs unsloth/__init__, so that fix never reaches it. It surfaced on #12476's Backend CI run (test_guard_holds_through_a_lora_injected_after_install), the first Studio test to inject a real LoRA with peft 0.18.1 and torchao 0.18.0 installed.

Reproduced outside Studio with torch 2.12.1+cpu, peft 0.18.1, torchao 0.18.0: peft.inject_adapter_in_model(LoraConfig(target_modules=["0"]), nn.Sequential(nn.Linear(8, 8))) raises the same ImportError.

Change

studio/backend/core/inference/diffusion_torchao_patches.py, which already holds Studio's copy of unsloth's torchao int_mm fix for the same reason:

  • A copy of _peft_torchao_tensor_subclasses and _guard_peft_torchao_dispatcher from unsloth/import_fixes.py. On the removed-class ImportError only, the wrapper redoes the isinstance check against the subclasses torchao still ships, so a plain Linear falls through to peft's ordinary LoRA layer and an AffineQuantizedTensor weight still gets TorchaoLoraLinear. Any other ImportError still raises. Both copies mark their wrapper __unsloth_patched__, so whichever runs second leaves the first alone.
  • install_peft_torchao_dispatch_guard(): patches both bindings (peft.tuners.lora.torchao and peft.tuners.lora.model, which imports the function by value) if peft's LoRA module is loaded, otherwise installs a meta path finder that patches it when it is. peft is not imported eagerly.
  • install_torchao_int_mm_patch() calls it first, independent of UNSLOTH_TORCHAO_INT_MM_FIX. That installer is already called at import by diffusion.py, video.py, diffusion_transformer_quant.py, diffusion_train_common.py and the torch warmup.

Tests

studio/backend/tests/test_diffusion_peft_torchao_guard.py:

  • the two ported functions are AST-identical to unsloth/import_fixes.py apart from the warning's speaker, so the copies cannot drift;
  • with a fake torchao missing the class: a plain weight gets None instead of an ImportError, both peft bindings point at one wrapper, and a second pass is a no-op;
  • an AffineQuantizedTensor weight still gets TorchaoLoraLinear with its kwargs;
  • an unrelated ImportError (a missing shared object) still surfaces;
  • the guard installs a front-of-path finder instead of importing peft, and only once;
  • install_torchao_int_mm_patch() installs the guard even with UNSLOTH_TORCHAO_INT_MM_FIX=0;
  • real peft and torchao: a LoRA injects onto a plain Linear. Backend CI has peft 0.18.1 and torchao 0.18.0, so this runs against the broken pair there.

Sabotage, in a torch 2.12.1 / peft 0.18.1 / torchao 0.18.0 venv:

Variant Result
this PR 8 passed
guard not called from install_torchao_int_mm_patch wiring test fails
_patch_peft_torchao_dispatchers made a no-op both fake-peft behaviour tests and the real injection test fail

A local slice of studio/backend/tests -k "torchao or peft or lora or diffusion_patch or prewarm" produces the same failure and error set on main and on this branch, all of them local environment issues unrelated to this change.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 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-02T00:04:40.936967Z a12ccc3 Manual request
🔒 Security Review ⚠️ Failed 2026-10-02T00:03:03.035023Z a12ccc3 PR opened
ℹ️ 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 Review: Didn't find any major issues. Nice work!

Reviewed commit: a12ccc33bb

ℹ️ 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 38d48d3 into main Oct 2, 2026
@danielhanchen
danielhanchen deleted the studio/peft-torchao-018-lora-guard branch October 2, 2026 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant