Skip to content

Native quant tests: make peft see torchao as absent when the test blocks it - #16704

Merged
danielhanchen merged 1 commit into
mainfrom
dh/native-quant-torchao-probe
Nov 4, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/native-quant-torchao-probe

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

studio/backend/tests/test_diffusion_native_quant.py::test_peft_wrapped_base_layer_runs_native depends on test order. It failed twice in a row in (Python 3.13, a-f) on the staging runs for #16687, which only touches diffusion_multigpu.py and its own test:

tests/test_diffusion_native_quant.py:707: in test_peft_wrapped_base_layer_runs_native
    wrapped = peft.inject_adapter_in_model(cfg, model)
core/inference/diffusion_torchao_patches.py:328: in dispatch_torchao
    return original(*args, **kwargs)
peft/tuners/lora/torchao.py:151: in dispatch_torchao
    from torchao.quantization import LinearActivationQuantizedTensor
E   ModuleNotFoundError: import of torchao.quantization halted; None in sys.modules

The test calls _block_torchao, which sets sys.modules["torchao"] and sys.modules["torchao.quantization"] to None to prove the native path never touches torchao. Then it injects a LoRA with peft. peft's LoRA dispatcher only imports torchao.quantization if peft.import_utils.is_torchao_available() says yes, and that function is lru_cached for the whole process. When an earlier test in the same xdist worker has already asked it with torchao installed, the cached True sends the dispatcher straight into the import the test blocked. Run alone, or in a worker where nothing asked first, the test passes. That is why it shows up only for some shard layouts, and #16687's added test file happens to produce one.

The product code is fine. With torchao really missing, is_torchao_available() is False from the start. Only the test's simulation was incomplete: it hid torchao from imports but not from peft's cached probe.

Change

studio/backend/tests/test_diffusion_native_quant.py, _block_torchao only. While torchao is blocked, it also points is_torchao_available in peft.import_utils and peft.tuners.lora.torchao at lambda: False, through monkeypatch, so peft treats torchao as absent and the patch is undone after the test. A module peft has not loaded yet is left alone. test_diffusion_native_quant_mps.py has its own _block_torchao but never calls peft, so it is unchanged.

Testing

A one-line test that calls peft.import_utils.is_torchao_available() first stands in for the earlier test in the worker:

peft 0.18.1 (backend CI) peft 0.21.2
main, probe then this test 1 failed (the error above) 2 passed
this branch, probe then this test 2 passed 2 passed
this branch, probe, this test, probe again 3 passed (the later probe still sees torchao) 3 passed

tests/test_diffusion_native_quant.py plus tests/test_diffusion_native_quant_mps.py: 188 passed, 14 skipped.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 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-11-04T21:26:49.484212Z aac15d9 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 Review: Didn't find any major issues. Breezy!

Reviewed commit: aac15d9fde

ℹ️ 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 a1e340b into main Nov 4, 2026
@danielhanchen
danielhanchen deleted the dh/native-quant-torchao-probe branch November 4, 2026 21:32
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