Skip to content

[LoRA] Target modules by FQN - #5095

Merged
jinsooihm merged 4 commits into
pytorch:mainfrom
jinsooihm:lora-fqn
Oct 7, 2026
Merged

jinsooihm merged 4 commits into
pytorch:mainfrom
jinsooihm:lora-fqn

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

Addresses #4995 (comment)

Also addresses #4995 (comment)
since handler interface is gone.

Summary

HuggingFace link and Megatron link target LoRA modules by FQN.

This PR lets the user only specify the FQN, and the transform handles applying the correct LoRA for different kinds of modules, including Linear, GroupedLinear, DistMoeRoutedExperts.

Only complication is the DistMoeRoutedExperts because the previous implementation assumed both w13 and w2 are targeted, but this one allows one to target only one of them.

  • traverse the modules from bottom up (child -> parent)
  • apply GroupedLinear or Linear LoRA for targeted modules
  • when we reach DistMoeRoutedExperts and it has child w13 or w2 that is already LoRA transformed, edit _weight_operands to return the LoRA weight summed matrix.

@pytorch-bot pytorch-bot Bot added ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 6, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 6, 2026
Comment thread torchtitan/config/transform/lora.py Resolved
Comment thread torchtitan/config/transform/lora.py Resolved
# each occurrence of an aliased config before an earlier FQN mutates the
# shared object, keeping exact-FQN selection path-local. Walking bottom-up
# also lets a Dist-MoE parent select its wrapper from converted children.
configs = list(model.traverse(Module.Config, recurse=True))

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.

_make_frozen_config uses shallow copy. So if there is a list of config field inside a shared config, but we only want to transform some modules not all of them that share that config, does this still hold/work correctly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That would be problematic. I will try to fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems a problem that is not too easy to fix in a nice way.

The problem in general is that the model config is often conceptually a tree, but it could sometimes be a DAG because some modules may share the config.

One way to handle this case would be to first canonicalize the model config into a tree, and then applying the transform on the modules.

Another way would be to just deepcopy every time, but this would incur unnecessary many copies for each subtrees because we traverse from bottom up.

But is it possible that say different layers genuinely share a common module? I don't know the answer.

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.

okay, let's not fix this now as discussed offline. We need a separate diff stack to fix the transform and config traversal problems. Please add a note.

@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.

lgtm, please merge after @fegin approves

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

Please add a note about the config/transform issue before landing.

# each occurrence of an aliased config before an earlier FQN mutates the
# shared object, keeping exact-FQN selection path-local. Walking bottom-up
# also lets a Dist-MoE parent select its wrapper from converted children.
configs = list(model.traverse(Module.Config, recurse=True))

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.

okay, let's not fix this now as discussed offline. We need a separate diff stack to fix the transform and config traversal problems. Please add a note.

@jinsooihm

Copy link
Copy Markdown
Contributor Author

@fegin I made it error out in the cases

  • when a shared module gets inconsistent LoRA choice
  • LoRA targets weight tied lm head

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI 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.

3 participants