[Feature] Enable LoRA adapter injection for BAGEL (#2490)
Signed-off-by: Zhengyuan Su <su.zhengyuan@u.nus.edu> Signed-off-by: Claude <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
328de586aa
commit
486d77d797
6 files changed
+392
-31
No files matched your search
@@ -366,13 +366,17 @@ class DiffusionLoRAManager:
|
||||
fully_sharded_loras=False,
|
||||
)
|
||||
|
||||
for component_name in ("transformer", "transformer_2", "dit"):
|
||||
for component_name in ("transformer", "transformer_2", "dit", "bagel"):
|
||||
if not hasattr(self.pipeline, component_name):
|
||||
continue
|
||||
component = getattr(self.pipeline, component_name)
|
||||
if not isinstance(component, nn.Module):
|
||||
continue
|
||||
|
||||
# Collect replacements first to avoid mutating the module tree
|
||||
# while iterating over named_modules().
|
||||
pending_replacements: list[tuple[str, str, nn.Module, list[str]]] = []
|
||||
|
||||
for module_name, module in component.named_modules(remove_duplicate=False):
|
||||
# Don't recurse into already-replaced LoRA wrappers. Their
|
||||
# original LinearBase lives under "base_layer", and replacing
|
||||
@@ -401,6 +405,9 @@ class DiffusionLoRAManager:
|
||||
if not should_replace:
|
||||
continue
|
||||
|
||||
pending_replacements.append((module_name, full_module_name, module, packed_modules_list))
|
||||
|
||||
for module_name, full_module_name, module, packed_modules_list in pending_replacements:
|
||||
lora_layer = from_layer_diffusion(
|
||||
layer=module,
|
||||
max_loras=1,
|
||||
|
||||
@@ -929,17 +929,31 @@ class Qwen2MoTForCausalLM(Qwen2PreTrainedModel):
|
||||
(".qkv_proj", ".q_proj", "q"),
|
||||
(".qkv_proj", ".k_proj", "k"),
|
||||
(".qkv_proj", ".v_proj", "v"),
|
||||
# MLP gate/up projections — the DiT uses separate
|
||||
# ColumnParallelLinear layers (no fused gate_up_proj), but
|
||||
# these entries are needed so that DiffusionLoRAManager can
|
||||
# derive the packed→sublayer mapping for LoRA checkpoints
|
||||
# that store weights under fused gate_up_proj keys.
|
||||
# The weight loader gracefully falls through to the
|
||||
# non-stacked path when the fused parameter doesn't exist.
|
||||
(".gate_up_proj", ".gate_proj", 0),
|
||||
(".gate_up_proj", ".up_proj", 1),
|
||||
]
|
||||
self.stacked_params_mapping = stacked_params_mapping
|
||||
params_dict = dict(self.named_parameters())
|
||||
loaded_params: set[str] = set()
|
||||
|
||||
for name, loaded_weight in weights:
|
||||
original_name = name
|
||||
for param_name, weight_name, shard_id in stacked_params_mapping:
|
||||
if weight_name not in name:
|
||||
continue
|
||||
name = name.replace(weight_name, param_name)
|
||||
param = params_dict.get(name)
|
||||
if param is None:
|
||||
# Fused param doesn't exist (e.g. gate_up_proj on DiT);
|
||||
# restore original name and fall through to non-stacked path.
|
||||
name = original_name
|
||||
break
|
||||
weight_loader = getattr(param, "weight_loader", default_weight_loader)
|
||||
weight_loader(param, loaded_weight, shard_id)
|
||||
|
||||
Reference in new issue
Block a user