[Bugfix][HunyuanImage3] Fix DiT LoRA binding: namespace alias + GQA QKV de-interleave (#6802)

Signed-off-by: Asthenia <asthenia0412@gmail.com>
Signed-off-by: Gao Han <hgaoaf@connect.ust.hk>
Co-authored-by: Asthenia <asthenia0412@gmail.com>
Co-authored-by: Gao Han <hgaoaf@connect.ust.hk>
This commit is contained in:
authored and GitHub committed 2026-09-23 16:06:54 +00:00
1 parent 21f167eacd
commit a473ae78c3
9 files changed
+509 -19

No files matched your search

+53 -11
View File
@@ -548,7 +548,21 @@ class DiffusionLoRAManager:
return lora_weights
module_suffix = full_module_name.split(".")[-1]
return lora_model.get_lora(module_suffix)
lora_weights = lora_model.get_lora(module_suffix)
if lora_weights is not None:
return lora_weights
# Model-scoped namespace aliases. HunyuanImage-3 registers the DiT
# under ``transformer.layers.*`` (``self.transformer`` aliases
# ``self.model``) while PEFT adapters use ``model.layers.*``.
name_aliases = getattr(self.pipeline, "_get_diffusion_lora_name_aliases", None)
if callable(name_aliases):
for alias in name_aliases(full_module_name) or []:
lora_weights = lora_model.get_lora(alias)
if lora_weights is not None:
return lora_weights
return None
def _is_active_at_scale(self, adapter_id: int, scale: float) -> bool:
"""True if the adapter_id is active and the current scale matches."""
@@ -646,18 +660,46 @@ class DiffusionLoRAManager:
lora_layer.reset_lora(0)
continue
total = sum(output_slices)
if lora_weights.lora_b.shape[0] != total:
logger.warning(
"Skipping LoRA for %s due to shape mismatch: lora_b[0]=%d != sum(output_slices)=%d",
full_module_name,
lora_weights.lora_b.shape[0],
total,
# HunyuanImage-3 fused ``qkv_proj`` LoRA-B rows are
# GQA-interleaved in the checkpoint (per-KV-head
# [Q-group, K, V]); de-interleave them to the block layout
# [all Q, all K, all V] the QKV output slices expect. The
# checkpoint sizes exclude replicated KV heads; set_lora()
# selects the appropriate Q shard and shared KV shard.
deinterleave = getattr(self.pipeline, "_deinterleave_fused_qkv_lora_b", None)
if isinstance(getattr(lora_layer, "base_layer", None), QKVParallelLinear) and callable(deinterleave):
deinterleaved = deinterleave(lora_weights.lora_b)
base = lora_layer.base_layer
output_sizes = (
base.total_num_heads * base.head_size,
base.total_num_kv_heads * base.head_size,
base.total_num_kv_heads * base.v_head_size,
)
lora_layer.reset_lora(0)
continue
if (
deinterleaved is None
or len(output_sizes) != n_slices
or deinterleaved.shape[0] != sum(output_sizes)
):
logger.warning(
"Skipping LoRA for %s: cannot establish HunyuanImage-3 fused-QKV layout",
full_module_name,
)
lora_layer.reset_lora(0)
continue
b_splits = list(torch.split(deinterleaved, list(output_sizes), dim=0))
else:
total = sum(output_slices)
if lora_weights.lora_b.shape[0] != total:
logger.warning(
"Skipping LoRA for %s due to shape mismatch: lora_b[0]=%d != sum(output_slices)=%d",
full_module_name,
lora_weights.lora_b.shape[0],
total,
)
lora_layer.reset_lora(0)
continue
b_splits = list(torch.split(lora_weights.lora_b, list(output_slices), dim=0))
b_splits = list(torch.split(lora_weights.lora_b, list(output_slices), dim=0))
lora_a_list = [lora_weights.lora_a] * n_slices
lora_b_list = [b * scale for b in b_splits]
lora_layer.set_lora(index=0, lora_a=lora_a_list, lora_b=lora_b_list)