Skip to content

[lora] Keep selected modules fully trainable in LoRATransform - #6003

Merged
fegin merged 1 commit into
mainfrom
fegin/stack/30
Nov 12, 2026
Merged

fegin merged 1 commit into
mainfrom
fegin/stack/30

Conversation

@fegin

@fegin fegin commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

## 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
@fegin

fegin commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

@claude review this PR

@claude

claude Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 2m 24s —— View job


Request changes: with weight tying on, trainable_modules=["tok_embeddings"] leaves the embedding frozen and raises no error. See the review.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. torchtitan/config/transform/lora.py:118 -- weight tying swaps tok_embeddings.weight for the frozen lm_head.weight, so the module the user listed as trainable ends up frozen. Require lm_head and tok_embeddings to get the same trainable decision when tying is on (inline).
Details
  • Gate. The problem is real: before this PR, LoRATransform freezes 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_target changes nothing. Non-projection configs with is_target=True still reach the frozen else branch, and the DistMoeRoutedExperts branch never read is_target.
  • Dist-MoE. If the trainable path is a routed_experts subtree, the parent and its children are all skipped, which is consistent. If the trainable path is only ...routed_experts.w13 and w2 is LoRA-targeted, the parent gets the LoRA Dist-MoE wrapper and w13 stays a stock trainable GroupedLinear. _make_dist_moe_lora_config accepts that owner.
  • Weight tying. Decoder is the only place that ties parameters. Muse Spark rejects enable_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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fegin
fegin marked this pull request as draft November 10, 2026 08:37
@fegin
fegin changed the base branch from fegin/stack/25 to main November 10, 2026 08:37
@pytorch-bot pytorch-bot Bot added ciflow/rl and removed ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Nov 10, 2026
@fegin
fegin changed the base branch from main to fegin/stack/25 November 10, 2026 08:38
@fegin
fegin marked this pull request as ready for review November 10, 2026 08:38
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 10, 2026
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026
@fegin
fegin changed the base branch from main to fegin/stack/25 November 11, 2026 04:29
@fegin
fegin marked this pull request as ready for review November 11, 2026 04:29
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026
fegin added a commit that referenced this pull request Nov 11, 2026
## 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
fegin added a commit that referenced this pull request Nov 11, 2026
## 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
fegin added a commit that referenced this pull request Nov 11, 2026
## 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
@fegin
fegin marked this pull request as draft November 11, 2026 08:53
@fegin
fegin changed the base branch from fegin/stack/25 to main November 11, 2026 08:53
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026
@fegin
fegin changed the base branch from main to fegin/stack/25 November 11, 2026 08:54
@fegin
fegin marked this pull request as ready for review November 11, 2026 08:54
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 11, 2026

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm, didn't check every detail

fegin added a commit that referenced this pull request Nov 11, 2026
## 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
@fegin
fegin force-pushed the fegin/stack/30 branch 2 times, most recently from 68c0df8 to 455866b Compare November 12, 2026 01:14
@fegin
fegin changed the base branch from fegin/stack/25 to main November 12, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants