Repository navigation
sc-5374: honor diffusers lora_adapter_metadata lora_alpha (alpha≠rank) in the candle inference adapter merge - #46
Merged
Conversation
…) 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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.alphatensor), but wrong for the diffuserssave_lora_adapter/ raw PEFT format — which most community DiT LoRAs (and these very Lens/Z-Image pipelines) ship: that format carries no.alphatensor.lora_alpha,r, and the optional per-modulealpha_pattern/rank_patternlive in the safetensors header__metadata__["lora_adapter_metadata"]JSON blob. With no tensor the merge silently fell back toalpha = rank(scale 1.0), applying any such file at the wrong strength wheneverlora_alpha ≠ r(alpha = 2r / r/2 / fixed 16/32 — extremely common).Fix
LoraAdapterMetaincandle_gen::train::lora(the in-repo shared home —gen_coreis a git-pinned dep, so the helper lives here rather thangen_core::weightsmeta). Parses the blob and resolves the effective(alpha, rank)per module using PEFT's singletarget_name_keyprecedence (re.match(r".*\.{key}$")fromLoraModel._create_and_replace). Malformed/absent blob →None(never poisons the merge).merge_lora_fileacrosscandle-gen-{sdxl,z-image,lens}(SDXL: both the Linear and conv branches). Precedence: per-target.alphatensor →alpha_pattern[module]→lora_alpha→ factor rank (today's last-resort default). The scaling denominator honorsrank_pattern/rwhen present (a well-formed PEFT file'sAleading dim already equals it).Scope / non-regressions
rank/alphaare top-level__metadata__strings the LoKr merge already reads; diffusers-LoKr carries them at the top level too.save_lora_peftwrites a per-target.alphatensor (and top-levelrank/alpha, not this blob), so it never hits the new path. This is community/external-adapter coverage, same flavor as sc-5225.Tests
LoraAdapterMetaunit tests: globallora_alpha/r, per-modulealpha_pattern/rank_patternoverride + non-matching fallback, absent/malformed →None..alphatensor +{"lora_alpha":16,"r":8}merges at effective(16/8)·scale = 2.0, asserted to matchreconstruct_lora_delta(.., 16, 8, ..)and to differ from the oldalpha=rankdefault.cargo fmt --all --checkclean,cargo clippy --all-targets -- -D warningsclean, 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 viasafetensors) 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