Skip to content

[LoRA] add LoKr adapter support (Z-Image, Flux2/Klein) - #3

Open
christopher5106 wants to merge 3 commits into
mainfrom
lokr_support
Open

christopher5106 wants to merge 3 commits into
mainfrom
lokr_support

Conversation

@christopher5106

Copy link
Copy Markdown

Upstream: huggingface#14163 (open since 2026-07-10). Both review comments were addressed in 96bb95c on 2026-09-26, and a re-review was requested.

Problem: diffusers only loads LoRA adapters, so LoKr (LyCORIS Kronecker-product) checkpoints for Z-Image and FLUX.2 / Klein cannot be loaded.

Change:

  • load_lora_adapter detects lokr_ keys and injects a peft LoKrConfig inferred from the tensor shapes.
  • Converters cover ai-toolkit Z-Image, ai-toolkit BFL FLUX.2 (fused QKV, loaded by fusing the model's QKV so the Kronecker factors map one to one), and the LyCORIS underscore and dotted-diffusers layouts. The LyCORIS alpha convention is applied at conversion.
  • Review follow-ups: one adapter-agnostic error message, and a fused-QKV load is refused when an adapter already sits on the unfused projections.

Test: tests/lora/test_lora_lokr.py.

In scenario: yes, cherry-picked as one commit. The branch has two commits, to squash at the next re-sync.

This PR documents a fix branch of this fork and is not meant to be merged. It is closed once the change is merged upstream, and the branch is then deleted.

christopher5106 and others added 2 commits September 24, 2026 14:41
Adds loading of LoKr (LyCORIS Kronecker product) adapters:

- `load_lora_adapter` detects `lokr_` keys and injects a peft `LoKrConfig`,
  inferred from the tensor shapes via `_create_lokr_config` (decompose
  factor, per-module rank/alpha patterns).
- State dict conversions for the formats in the wild: ai-toolkit Z-Image
  (dotted diffusers paths under `diffusion_model.`), ai-toolkit BFL Flux2
  (fused qkv), LyCORIS underscore format, and bare dotted diffusers paths.
- BFL fused-QKV LoKr cannot be split exactly into separate Q/K/V Kronecker
  factors, so `Flux2LoraLoaderMixin.load_lora_weights` fuses the model's
  QKV projections and maps the adapter 1:1 (exact).
- Alpha follows the LyCORIS convention: scaling applies only to
  rank-decomposed factors and is baked into the weights at conversion.

Fixes huggingface#13221
… fused-QKV load over an unfused adapter

- "Invalid adapter checkpoint. We currently support LoRA and LoKr." replaces the
  message that still said "LoRA checkpoint" and described the substring check.
- Loading a fused-QKV LoKr checkpoint now refuses when an adapter is already
  injected on to_q/to_k/to_v or add_{q,k,v}_proj: fuse_qkv_projections() would
  replace those modules and orphan it. Covered by a test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The file exercises checkpoint conversion and loading for LoKr adapters, not LoRA.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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