Skip to content

sc-5374: honor diffusers lora_adapter_metadata lora_alpha (alpha≠rank) in the candle inference adapter merge - #46

Merged
michaeltrefry merged 1 commit into
mainfrom
claude/sc-5374-lora-alpha
Jun 14, 2026
Merged

michaeltrefry merged 1 commit into
mainfrom
claude/sc-5374-lora-alpha

Conversation

@michaeltrefry

Copy link
Copy Markdown
Collaborator

Closes the alpha≠rank gap surfaced during sc-5116. The candle inference LoRA merge derived per-target scaling as alpha = per_target_.alpha_tensor.unwrap_or(rank). That is correct for kohya and the candle trainer's own output (both ship a per-target .alpha tensor), but wrong for the diffusers save_lora_adapter / raw PEFT format — which most community DiT LoRAs (and these very Lens/Z-Image pipelines) ship: that format carries no .alpha tensor. lora_alpha, r, and the optional per-module alpha_pattern/rank_pattern live in the safetensors header __metadata__["lora_adapter_metadata"] JSON blob. With no tensor the merge silently fell back to alpha = rank (scale 1.0), applying any such file at the wrong strength whenever lora_alpha ≠ r (alpha = 2r / r/2 / fixed 16/32 — extremely common).

Fix

  • Shared parser LoraAdapterMeta in candle_gen::train::lora (the in-repo shared home — gen_core is a git-pinned dep, so the helper lives here rather than gen_core::weightsmeta). Parses the blob and resolves the effective (alpha, rank) per module using PEFT's single target_name_key precedence (re.match(r".*\.{key}$") from LoraModel._create_and_replace). Malformed/absent blob → None (never poisons the merge).
  • Wired into merge_lora_file across candle-gen-{sdxl,z-image,lens} (SDXL: both the Linear and conv branches). Precedence: per-target .alpha tensor → alpha_pattern[module] → lora_alpha → factor rank (today's last-resort default). The scaling denominator honors rank_pattern/r when present (a well-formed PEFT file's A leading dim already equals it).

Scope / non-regressions

  • LoKr unaffected — its rank/alpha are top-level __metadata__ strings the LoKr merge already reads; diffusers-LoKr carries them at the top level too.
  • Candle-trainer round-trip unchanged — save_lora_peft writes a per-target .alpha tensor (and top-level rank/alpha, not this blob), so it never hits the new path. This is community/external-adapter coverage, same flavor as sc-5225.

Tests

  • LoraAdapterMeta unit tests: global lora_alpha/r, per-module alpha_pattern/rank_pattern override + non-matching fallback, absent/malformed → None.
  • A merge test in each of the three crates: a diffusers-format LoRA with no .alpha tensor + {"lora_alpha":16,"r":8} merges at effective (16/8)·scale = 2.0, asserted to match reconstruct_lora_delta(.., 16, 8, ..) and to differ from the old alpha=rank default.
  • cargo fmt --all --check clean, cargo clippy --all-targets -- -D warnings clean, CPU lib tests green across all four crates (candle-gen 34, sdxl 30, z-image 25, lens 48).

Adds serde_json (already in the lock via safetensors) as a direct workspace dep for the blob parse.

Related: sc-5116 (Lens), sc-5165 (SDXL), sc-5166 (Z-Image), sc-5225 (SDXL community coverage).

🤖 Generated with Claude Code

…) in the candle inference adapter merge

The candle inference LoRA merge derived per-target scaling as
`alpha = per_target_.alpha_tensor.unwrap_or(rank)` — correct for kohya and
the candle trainer's own output (both ship a per-target `.alpha` tensor) but
wrong for diffusers `save_lora_adapter` / raw PEFT files: those carry NO
`.alpha` tensor. `lora_alpha`/`r` (+ per-module alpha_pattern/rank_pattern)
live in the safetensors `__metadata__["lora_adapter_metadata"]` JSON blob.
With no tensor the merge silently fell back to `alpha = rank` (scale 1.0),
applying any such adapter at the wrong strength whenever `lora_alpha != r`
(alpha = 2r / r/2 / fixed-16, the common cases).

Add a shared `LoraAdapterMeta` parser in `candle_gen::train::lora` (the
in-repo shared home; gen_core is a git-pinned dep, so the helper lives here)
that parses the blob and resolves the effective (alpha, rank) per module with
PEFT's single `target_name_key` precedence. Wire it into merge_lora_file
across candle-gen-{sdxl,z-image,lens}: precedence per-target `.alpha` tensor
-> alpha_pattern[module] -> lora_alpha -> factor rank (today's default). The
denominator honors rank_pattern/`r` when present (a well-formed PEFT file's
`A` leading dim already equals it).

LoKr is unaffected (its rank/alpha are top-level __metadata__ strings the
LoKr merge already reads). The candle trainer's own PEFT output ships a
per-target `.alpha` tensor, so its train->infer round-trip is unchanged --
this is community/external-adapter coverage (same flavor as sc-5225).

Tests: LoraAdapterMeta unit tests (global alpha/rank, per-module
alpha_pattern/rank_pattern, absent/malformed -> None) + a merge test in each
of the three crates exercising lora_alpha=16, r=8 -> effective scale 2.0
(and asserting it differs from the old alpha=rank default). fmt + clippy
clean; CPU lib tests green across all four crates.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@michaeltrefry
michaeltrefry merged commit 64f0248 into main Jun 14, 2026
3 checks passed
@michaeltrefry
michaeltrefry deleted the claude/sc-5374-lora-alpha branch June 14, 2026 15:21
michaeltrefry added a commit that referenced this pull request Jul 2, 2026
…(upstream Cow::Owned UAF fix) (#222)

Bump the workspace candle pin to pick up upstream huggingface/candle#3493
(commit c1e6756a89), the use-after-free fix in as_t_slice /
QStorage::from_data(Cow::Owned...) that the sc-9085 quant spike hit: the
helper took the Cow by value and returned a slice into its dropped buffer,
silently corrupting quantized weights. Our Cow::Borrowed workaround stays
(zero-cost), but the footgun is gone at the source.

The 4-commit range (65ecb58c..c1e6756a89) is the fix plus three Metal-only
SDPA fixes: no API changes, and candle-kernels/ is byte-identical across it,
so the sc-7544 vendored fatbin fork needs NO re-vendor (VENDORED.md updated
to record the new rev and why).

candle-llm re-pinned d0ba3e6 -> 3d9fdf0 in lockstep (candle-llm #46 carries
the matching candle bump) so cargo unifies ONE candle-core across the graph;
the intermediate d0ba3e6..3d9fdf0 candle-llm churn is the sc-8528 KV-quant
backout + docs, and candle-gen-joycaption links no candle-llm symbols.

Validation: full CUDA gate (scripts/check-cuda.ps1) green at the new revs on
sm_120 - 115 suites, 0 failures, including cuda_quant_smoke (the Blackwell
quant-fatbin guard). candle-llm's own CPU suite green in #46. The gate ran
against the pre-squash branch commit 1b3bc2f; the squash-merge 3d9fdf0 has
the IDENTICAL git tree (543b33bf), so the result carries over exactly.

Co-authored-by: Claude Fable 5 <[email protected]>
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