Skip to content

Studio: let peft 0.19 attach a LoRA to torchao int8 / fp8 weights of quantised single files - #17087

Merged
danielhanchen merged 2 commits into
mainfrom
studio-lora-nov7-peft
Nov 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-lora-nov7-peft

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #16711. With peft 0.19.x, loading a plain LoRA onto a quantised single file's int8 / fp8 transformer fails during adapter injection:

TypeError: TorchaoLoraLinear.__init__() missing 1 required keyword-only argument: 'get_apply_tensor_subclass'

Why it fails:

  • peft 0.19 dispatches every TorchAOBaseTensor weight to TorchaoLoraLinear, and its constructor requires a requantisation callback.
  • peft reads that callback only from a transformers hf_quantizer. Studio builds these Int8Tensor / Float8Tensor weights directly from the file, so the callback is never there.
  • peft 0.18 routes those weights to a plain lora.Linear, so it works.
  • peft 0.20 made the callback optional: forward and training work, and only merge / unmerge refuse (huggingface/peft src/peft/tuners/lora/torchao.py).
  • Studio pins peft 0.18.1, but the supported range is 0.18.0 to 0.20.x, so 0.19.x users hit this.

Changes

  • When peft's LoRA module loads, diffusion_torchao_patches gives TorchaoLoraLinear.__init__ the same get_apply_tensor_subclass=None default that peft 0.20 has.
  • The patch only applies when the installed signature has the keyword-only, no-default parameter, which means peft 0.19.x. On peft 0.18 and 0.20+ it changes nothing.
  • A callback that transformers supplies still wins.
  • The patch is idempotent and keeps __wrapped__.
  • It hooks into the existing peft import hook, so importing peft stays lazy.
  • Studio does not call peft's merge on these layers. The quantised merge path (diffusion_lora_quant_merge) does its own dequantise, add and requantise.
  • Kill switch: UNSLOTH_STUDIO_PEFT_TORCHAO_OPTIONAL_CALLBACK=0.

Evidence (CPU, Studio's real _int8_tensor / _fp8_tensor constructors, diffusers load_lora_adapter)

Results for test_diffusion_lora_quant_merge.py + test_diffusion_peft_torchao_guard.py, torch 2.10 / torchao 0.18 / diffusers 0.42.1:

peft main this PR
0.18.0 31 passed (new unit tests absent) 35 passed
0.19.1 test_a_peft_lora_merges_at_its_alpha_scale and the new int8 / fp8 injection tests fail with the TypeError above 35 passed
0.20.0 31 passed 35 passed
0.21.2 31 passed 35 passed

The new head tests also pass on:

  • torch 2.12.1 / torchao 0.17 / peft 0.19.1 / diffusers 0.40
  • torch 2.10 CPU / torchao 0.17 with peft 0.18.0 and 0.19.1

The int8 injection test also checks the LoRA output against base + B @ A x (relative error < 2%, the bf16 path). The fp8 forward is not run on CPU, because torch's CPU _scaled_mm only takes per-tensor scales.

GPU (Kaggle T4 sm75 and Colab L4 sm89; torch 2.10 / peft 0.19.1 / torchao 0.16 / diffusers 0.42.1)

Base and head ran one per T4 in the same kernel, 3 reps each. Setup: an int8 ConvRot layer of 256 x 512 on CUDA, a LoRA loaded through diffusers load_lora_adapter, then a forward pass checked against base + B @ A x.

main this PR
int8 TypeError: ... missing 1 required keyword-only argument: 'get_apply_tensor_subclass' loads as TorchaoLoraLinear, rel. error 1.9e-3
fp8 (Colab L4, sm89, 3 interleaved reps) same TypeError loads as TorchaoLoraLinear, rel. error 1.9e-3

…transformers

peft 0.19 sends every torchao weight to TorchaoLoraLinear but requires its
requantisation callback, which it only finds on a transformers hf_quantizer. A
LoRA on a quantised single file (int8 / fp8 torchao weights) raised a TypeError
at injection. peft 0.20 made the callback optional; give 0.19 the same default.
UNSLOTH_STUDIO_PEFT_TORCHAO_OPTIONAL_CALLBACK=0 keeps the required keyword.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T04:49:32.356384Z 877691e 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.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 877691e27c

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

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