Repository navigation
[lora] Keep selected modules fully trainable in LoRATransform - #6003
Conversation
## Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
## Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
**Example.**
```
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
# vision_encoder.*: unchanged, requires_grad=True
# decoder wqkv / wo: LoRA adapters trainable, base frozen
# everything else: frozen
```
## What changed
- Add LoRATransform.trainable_modules: config paths from the model root. Configs at or under each path are left unchanged, so their parameters stay trainable. A config path can differ from the module FQN: on Muse Spark the vision layers are vision_encoder.block, not vision_encoder.layers.0. Matching is on path boundaries ("layers.1" does not match "layers.10"); suffixes are not matched, unlike target_modules.
- Raise ValueError when a LoRA target is inside a trainable module. With target_modules=None, the message says that it targets every supported projection.
- Raise ValueError when no config is at or under a listed path. This catches a typo that would otherwise freeze the module silently. A list container such as "layers" has no config of its own and matches through its children.
- Raise ValueError when one config object appears both inside and outside a trainable module, like the shared-projection check.
- Raise ValueError when weight tying is on and only one of lm_head and tok_embeddings is trainable. Decoder ties the built parameters, so tok_embeddings would silently get the frozen lm_head weight, or the reverse would make tok_embeddings trainable.
- An empty list (the default) keeps the current behavior.
- Mechanical: the "is this config a LoRA target" check moves into one helper, _is_target, used by the validators and the transform loop.
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
## Numerics
No change when trainable_modules is empty, which is every existing caller.
## Testing
- test_lora_model_builds runs with and without trainable_modules and checks that exactly the parameters under the listed paths are trainable besides the adapters. It catches a trainable module that gets frozen, or a frozen module that leaks into training.
- test_lora_validates_trainable_modules covers the four ValueErrors, the target_modules=None message, trainable_modules=["layers"] (a list container), and a tied model with both tok_embeddings and lm_head listed; the last two must not raise. The weight-tying case fails without the new check.
stack-info: PR: #6003, branch: fegin/stack/30
16eed97 to
73ca106
Compare
|
@claude review this PR |
There was a problem hiding this comment.
Verdict: Request changes -- with weight tying on, a trainable tok_embeddings stays frozen and nothing raises. That is the kind of silent freeze this PR's validators are supposed to prevent.
What this PR does. It adds LoRATransform.trainable_modules, a list of config paths from the model root. Configs at or under each path are left alone, so their parameters keep requires_grad=True. Everything else keeps the current behavior: LoRA on the targets, everything else frozen. New ValueErrors cover a LoRA target inside a trainable path, a path that matches no config, and one config object that is both trainable and frozen. The direct consumer is the full-vision-tower SLAKE recipe in #6004.
Example.
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
before: vision_encoder.* frozen (no way to keep it trainable)
after: vision_encoder.* trainable; decoder wqkv/wo get LoRA; rest frozen
LoRATransform(target_modules=["wo"], trainable_modules=["tok_embeddings"]) # qwen3 0.6B, tied
after: tok_embeddings.weight is lm_head's frozen param -> requires_grad=False, no error
Must fix
torchtitan/config/transform/lora.py:118-- weight tying swapstok_embeddings.weightfor the frozenlm_head.weight, so the module the user listed as trainable ends up frozen. Requirelm_headandtok_embeddingsto get the same trainable decision when tying is on (inline).
Details
- Gate. The problem is real: before this PR,
LoRATransformfreezes every non-target config, and #6004 needs a fully trainable vision tower next to LoRA on the decoder. The transform that does the freezing is the right place for this. It adds no dependency and touches no core trainer code. Net LoC is about +60 of logic, mostly validation. Each check guards a silent failure:- Typo path: the module would be frozen without warning.
- Config shared inside and outside a trainable path: the frozen occurrence's loop does
setattr(shared_cfg, child, Frozen(...)), which also changes the trainable occurrence's children.
- Refactor. Moving the target check into
_is_targetchanges nothing. Non-projection configs withis_target=Truestill reach the frozenelsebranch, and theDistMoeRoutedExpertsbranch never readis_target. - Dist-MoE. If the trainable path is a
routed_expertssubtree, the parent and its children are all skipped, which is consistent. If the trainable path is only...routed_experts.w13andw2is LoRA-targeted, the parent gets the LoRA Dist-MoE wrapper andw13stays a stock trainableGroupedLinear._make_dist_moe_lora_configaccepts that owner. - Weight tying.
Decoderis the only place that ties parameters. Muse Spark rejectsenable_weight_tying, so #6004 is not affected.
Not verified: I couldn't run Python here, so the weight-tying freeze comes from reading decoder.py:250-276 and _get_frozen_config_cls, not from a run. A maintainer still needs to review.
| _matches_fqn(fqn, target) for target in target_module_names | ||
| ) | ||
| if is_target: | ||
| if _is_target(fqn, target_module_names): |
There was a problem hiding this comment.
With weight tying on, trainable_modules=["tok_embeddings"] leaves the embedding frozen and raises nothing. Decoder.__init__ builds tok_embeddings (left unchanged, so trainable), then builds lm_head as a FrozenLinear that sets requires_grad=False, and then ties them with self.tok_embeddings.weight = self.lm_head.weight (decoder.py:261, again in init_states at :276). So tok_embeddings ends up holding the frozen parameter. This affects every weight-tied flavor (small qwen3, qwen3_5, two llama3 flavors). It is the same kind of silent freeze that the "match no module config" check is meant to catch. The opposite case, ["lm_head"], quietly makes tok_embeddings trainable as well.
The new check for configs shared inside and outside a trainable module can't catch this, because the two configs are separate objects that share a parameter only after build. The smallest fix is in this validator: pass trainable_modules in, and when parent.enable_weight_tying is set, require the same trainable decision for fqn and its tok_embeddings sibling:
emb_fqn = f"{fqn[: -len('lm_head')]}tok_embeddings"
if any(_in_subtree(fqn, r) for r in trainable_modules) != any(
_in_subtree(emb_fqn, r) for r in trainable_modules
):
raise ValueError(
f"{fqn!r} and {emb_fqn!r} share a weight under weight tying; "
"list both or neither in trainable_modules."
)You could cover it with one more row in test_lora_validates_trainable_modules, using a tied config.
73ca106 to
2b64266
Compare
14aecaf to
2dd5099
Compare
2867348 to
b04ed4e
Compare
## Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
## Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
**Example.**
```
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
# vision_encoder.*: unchanged, requires_grad=True
# decoder wqkv / wo: LoRA adapters trainable, base frozen
# everything else: frozen
```
## What changed
- Add LoRATransform.trainable_modules: config paths from the model root. Configs at or under each path are left unchanged, so their parameters stay trainable. A config path can differ from the module FQN: on Muse Spark the vision layers are vision_encoder.block, not vision_encoder.layers.0. Matching is on path boundaries ("layers.1" does not match "layers.10"); suffixes are not matched, unlike target_modules.
- Raise ValueError when a LoRA target is inside a trainable module. With target_modules=None, the message says that it targets every supported projection.
- Raise ValueError when no config is at or under a listed path. This catches a typo that would otherwise freeze the module silently. A list container such as "layers" has no config of its own and matches through its children.
- Raise ValueError when one config object appears both inside and outside a trainable module, like the shared-projection check.
- Raise ValueError when weight tying is on and only one of lm_head and tok_embeddings is trainable. Decoder ties the built parameters, so tok_embeddings would silently get the frozen lm_head weight, or the reverse would make tok_embeddings trainable.
- An empty list (the default) keeps the current behavior.
- Mechanical: the "is this config a LoRA target" check moves into one helper, _is_target, used by the validators and the transform loop.
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
## Numerics
No change when trainable_modules is empty, which is every existing caller.
## Testing
- test_lora_model_builds runs with and without trainable_modules and checks that exactly the parameters under the listed paths are trainable besides the adapters. It catches a trainable module that gets frozen, or a frozen module that leaks into training.
- test_lora_validates_trainable_modules covers the four ValueErrors, the target_modules=None message, trainable_modules=["layers"] (a list container), and a tied model with both tok_embeddings and lm_head listed; the last two must not raise. The weight-tying case fails without the new check.
stack-info: PR: #6003, branch: fegin/stack/30
2dd5099 to
468422e
Compare
b04ed4e to
ec7c6be
Compare
## Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
## Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
**Example.**
```
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
# vision_encoder.*: unchanged, requires_grad=True
# decoder wqkv / wo: LoRA adapters trainable, base frozen
# everything else: frozen
```
## What changed
- Add LoRATransform.trainable_modules: config paths from the model root. Configs at or under each path are left unchanged, so their parameters stay trainable. A config path can differ from the module FQN: on Muse Spark the vision layers are vision_encoder.block, not vision_encoder.layers.0. Matching is on path boundaries ("layers.1" does not match "layers.10"); suffixes are not matched, unlike target_modules.
- Raise ValueError when a LoRA target is inside a trainable module. With target_modules=None, the message says that it targets every supported projection.
- Raise ValueError when no config is at or under a listed path. This catches a typo that would otherwise freeze the module silently. A list container such as "layers" has no config of its own and matches through its children.
- Raise ValueError when one config object appears both inside and outside a trainable module, like the shared-projection check.
- Raise ValueError when weight tying is on and only one of lm_head and tok_embeddings is trainable. Decoder ties the built parameters, so tok_embeddings would silently get the frozen lm_head weight, or the reverse would make tok_embeddings trainable.
- An empty list (the default) keeps the current behavior.
- Mechanical: the "is this config a LoRA target" check moves into one helper, _is_target, used by the validators and the transform loop.
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
## Numerics
No change when trainable_modules is empty, which is every existing caller.
## Testing
- test_lora_model_builds runs with and without trainable_modules and checks that exactly the parameters under the listed paths are trainable besides the adapters. It catches a trainable module that gets frozen, or a frozen module that leaks into training.
- test_lora_validates_trainable_modules covers the four ValueErrors, the target_modules=None message, trainable_modules=["layers"] (a list container), and a tied model with both tok_embeddings and lm_head listed; the last two must not raise. The weight-tying case fails without the new check.
stack-info: PR: #6003, branch: fegin/stack/30
468422e to
23a8430
Compare
ec7c6be to
4209101
Compare
## Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
## Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
**Example.**
```
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
# vision_encoder.*: unchanged, requires_grad=True
# decoder wqkv / wo: LoRA adapters trainable, base frozen
# everything else: frozen
```
## What changed
- Add LoRATransform.trainable_modules: config paths from the model root. Configs at or under each path are left unchanged, so their parameters stay trainable. A config path can differ from the module FQN: on Muse Spark the vision layers are vision_encoder.block, not vision_encoder.layers.0. Matching is on path boundaries ("layers.1" does not match "layers.10"); suffixes are not matched, unlike target_modules.
- Raise ValueError when a LoRA target is inside a trainable module. With target_modules=None, the message says that it targets every supported projection.
- Raise ValueError when no config is at or under a listed path. This catches a typo that would otherwise freeze the module silently. A list container such as "layers" has no config of its own and matches through its children.
- Raise ValueError when one config object appears both inside and outside a trainable module, like the shared-projection check.
- Raise ValueError when weight tying is on and only one of lm_head and tok_embeddings is trainable. Decoder ties the built parameters, so tok_embeddings would silently get the frozen lm_head weight, or the reverse would make tok_embeddings trainable.
- An empty list (the default) keeps the current behavior.
- Mechanical: the "is this config a LoRA target" check moves into one helper, _is_target, used by the validators and the transform loop.
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
## Numerics
No change when trainable_modules is empty, which is every existing caller.
## Testing
- test_lora_model_builds runs with and without trainable_modules and checks that exactly the parameters under the listed paths are trainable besides the adapters. It catches a trainable module that gets frozen, or a frozen module that leaks into training.
- test_lora_validates_trainable_modules covers the four ValueErrors, the target_modules=None message, trainable_modules=["layers"] (a list container), and a tied model with both tok_embeddings and lm_head listed; the last two must not raise. The weight-tying case fails without the new check.
stack-info: PR: #6003, branch: fegin/stack/30
23a8430 to
345e666
Compare
345e666 to
e71d46a
Compare
tianyu-l
left a comment
There was a problem hiding this comment.
sgtm, didn't check every detail
8fc8922 to
1864df6
Compare
## Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
## Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
**Example.**
```
LoRATransform(target_modules=["wqkv", "wo"], trainable_modules=["vision_encoder"])
# vision_encoder.*: unchanged, requires_grad=True
# decoder wqkv / wo: LoRA adapters trainable, base frozen
# everything else: frozen
```
## What changed
- Add LoRATransform.trainable_modules: config paths from the model root. Configs at or under each path are left unchanged, so their parameters stay trainable. A config path can differ from the module FQN: on Muse Spark the vision layers are vision_encoder.block, not vision_encoder.layers.0. Matching is on path boundaries ("layers.1" does not match "layers.10"); suffixes are not matched, unlike target_modules.
- Raise ValueError when a LoRA target is inside a trainable module. With target_modules=None, the message says that it targets every supported projection.
- Raise ValueError when no config is at or under a listed path. This catches a typo that would otherwise freeze the module silently. A list container such as "layers" has no config of its own and matches through its children.
- Raise ValueError when one config object appears both inside and outside a trainable module, like the shared-projection check.
- Raise ValueError when weight tying is on and only one of lm_head and tok_embeddings is trainable. Decoder ties the built parameters, so tok_embeddings would silently get the frozen lm_head weight, or the reverse would make tok_embeddings trainable.
- An empty list (the default) keeps the current behavior.
- Mechanical: the "is this config a LoRA target" check moves into one helper, _is_target, used by the validators and the transform loop.
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
## Numerics
No change when trainable_modules is empty, which is every existing caller.
## Testing
- test_lora_model_builds runs with and without trainable_modules and checks that exactly the parameters under the listed paths are trainable besides the adapters. It catches a trainable module that gets frozen, or a frozen module that leaks into training.
- test_lora_validates_trainable_modules covers the four ValueErrors, the target_modules=None message, trainable_modules=["layers"] (a list container), and a tied model with both tok_embeddings and lm_head listed; the last two must not raise. The weight-tying case fails without the new check.
stack-info: PR: #6003, branch: fegin/stack/30
68c0df8 to
455866b
Compare
Stacked PRs:
[lora] Keep selected modules fully trainable in LoRATransform
Summary (human-written)
LoRA freezes all parameters except for the lora_a and lora_b. However, people may want to fine-tune vision encoder while applying lora to decoder. This PR allows it.
Why
LoRATransform freezes every non-target config. There is no way to keep a subtree trainable, so a recipe cannot fully fine-tune the vision tower while it trains LoRA adapters on the decoder.
Example.
What changed
No other consumer needs a change: the optimizer already groups every requires_grad parameter, checkpoints save and load the whole model state dict, frozen-param bf16 storage skips trainable params, and a trainable module keeps its HF state dict names.
Numerics
No change when trainable_modules is empty, which is every existing caller.
Testing