Skip to content

Studio: merge LoRA and LoKR adapters into ComfyUI-quantized weights - #16874

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
oobabooga:comfy-quant-lora-merge
Nov 6, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
oobabooga:comfy-quant-lora-merge

Conversation

@oobabooga

Copy link
Copy Markdown
Member

A ComfyUI-quantized single file (fp8 or int8 ConvRot) loaded with adapters bakes them before the compile, and they then run as layers on every step. A LoRA adds two small GEMMs per adapted Linear. A full-rank LoKR adds about a quarter of the Linear's own GEMM in bf16, which costs more than the fp8 GEMM it sits next to. ComfyUI patches the adapters into the weights instead (dequantize, add the delta, requantize), so they cost nothing per step there.

Seconds per step, RTX 6000 Ada, 1024², community adapters from #16711's set, lower is better:

Before (main) After (this PR) ComfyUI v0.44.0
Krea 2 Turbo fp8 + LoKR 1.27 0.74 1.07
Krea 2 fine-tune fp8 + LoRA 0.83 0.76 0.81
MOSS-Image SFT int8 ConvRot + LoKR (fused qkv) 1.19 0.54 0.44

The remaining MOSS-Image gap is the base model's own int8 step (0.54 against 0.44 without adapters too).

Change

New diffusion_lora_quant_merge.py, called where a resident quantized load bakes its adapters:

  • Every adapted Linear is dequantized in float32 (ConvRot rotation undone), the deltas are added (scale * B @ A for a LoRA, kron(w1, w2) with its row slice for a LoKR), and the result is quantized back into the layer's own Float8Tensor / Int8Tensor (or plain float) tensors: a per-row scale recomputed for the merged weight, and seeded stochastic rounding, as ComfyUI rounds a patched weight. Round-to-nearest dropped every delta below half a step: the MOSS-Image LoKR's effect fell from 19.6 to 16.5 per pixel.
  • The PEFT and LoKR layers are then removed, so the compile sees the base model alone (MOSS-Image's fused int8 path included).
  • The original quantized data of every touched layer is kept in host memory. A weight change copies it back into the same tensors and merges again; weight 0 is the original weights. Compiled code and captured graphs read those tensors, so they stay valid.

Kept on the current adapter-layer path: offloaded loads (the host copy, not the card's, is the source there), adapters that also touch a text encoder, DoRA / LoRA-bias layers, anything but a plain or ConvRot Linear with a registered weight, other weight formats, and a host without room for the backups in its usable RAM (cgroup limits included). UNSLOTH_DIFFUSION_LORA_QUANT_MERGE=0 turns it off.

At max speed, diffusers' QKV fusion is skipped on a merged load: its fused to_qkv is a copy that a weight change would not reach. Studio's own fused paths (Z-Image, MOSS-Image, int8) are views of the projections and follow it.

A weight change that fails (an out-of-memory during the re-merge) leaves the base weights and records the adapters at weight 0, so the next request merges again. A load that ended up offloaded refuses weight changes with a reload message, as a streamed graph already does.

Verification

  • The table above, against ComfyUI on the same files.
  • Weight 1, weight 0, weight 1 again through /images/generate with no reload, for the Krea 2 LoKR, the Krea 2 LoRA, the MOSS-Image LoKR and the MOSS-Image 4-step LoRA: weight 0 equals the model without the adapter exactly (mean |diff| 0.0; with the adapter layers at weight 0 it was 1.6 to 11 per pixel), the repeated weight reproduces the image exactly (0.0), and the adapter effect matches the layer path (Krea 2 LoKR 37.2, Krea 2 LoRA 38.5, MOSS-Image LoKR 19.9 per pixel).
  • 3 prompts x 2 seeds per case, side by side with ComfyUI: no artifacts.
  • Full diffusion, MOSS-Image and LoRA test files (pytest -n 8, 19,100+ tests): the only failures are the same 27 that fail on main with the same command (environment-dependent PyAV and attention-kernel tests, and tests that only fail when xdist puts them in one worker).
  • Tests:
    • test_diffusion_lora_quant_merge.py: merged weight against base + delta for int8 ConvRot, fp8 and dense; weight 0 restores the original codes and a weight merges to the same codes twice; the qdata / scale tensors are written in place; a fused qkv row slice; a PEFT LoRA at its alpha / r; adapters outside the denoiser, an unsupported format and the kill switch keep the adapter layers.
    • Also: a wrapped Linear and a host without room keep the adapter layers; a failed re-merge records the adapters as off and the next request merges again; an offloaded load refuses a weight change. These four fail before the follow-up commit. max speed never fuses QKV over merged weights (fails without its guard).
    • test_merged_quant_file_adapters_change_weight_in_place_with_no_recompile: a compiled model follows a weight change, switching the adapter off and back on with one compile and no graph reset. It fails without the routing in _adjust_baked_loras.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 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-06T13:45:43.776009Z 3dcdb8a 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 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member

(Python 3.13, a-f) fails on this PR (run 42167383515) with 12 failures, all in the tests it adds or extends, and all for the same reason:

test_a_lokr_merges_into_the_weight_and_its_layer_is_removed[int8]
test_merged_adapters_on_an_offloaded_load_need_a_reload
test_a_failed_remerge_records_the_adapters_as_off
...
E   ModuleNotFoundError: No module named 'diffusers'

The backend CPU shards do not install diffusers. Other diffusion tests in the shard skip without it (pytest.importorskip("diffusers")) or stub the import, so these need the same.

The Chat UI Tests (chat) red on this PR is the floating monitor resize flake that #16885 is looking at, not this change.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…cated; stub host RAM in merge tests

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 98d1c789db

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

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3dcdb8af15

ℹ️ 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 61f2c06 into unslothai:main Nov 6, 2026
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.

2 participants