Repository navigation
Fix LoRA freezing for non-linear modules - #3456
Conversation
|
Hi @Kevin-Li-2025! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
|
699760a to
455ded9
Compare
455ded9 to
69eb84f
Compare
|
|
||
| def __setattr__(self, name: str, value) -> None: | ||
| if name in type(self).__slots__: | ||
| if name in {f.name for f in fields(type(self))}: |
There was a problem hiding this comment.
curious what's the motivation of this change
|
|
||
| @dataclass(kw_only=True, slots=True) | ||
| class FrozenConfig(Configurable.Config): | ||
| class FrozenConfig(Module.Config): |
There was a problem hiding this comment.
Do we need this change? I understand that FrozenConfig always wraps a Module, but Module has many metadata which is probably not needed by this class.
There was a problem hiding this comment.
probably should make this Module.Config?
| else: | ||
| setattr(parent, attr, new_cfg) | ||
|
|
||
| # Second pass: freeze non-Linear leaf modules such as embeddings and |
There was a problem hiding this comment.
Why do we need two passes? I had thought one pass should suffice.
69eb84f to
a9489fd
Compare
| def _is_parameterized_non_linear_config(cfg: Module.Config) -> bool: | ||
| return isinstance( | ||
| cfg, | ||
| ( | ||
| Conv2d.Config, | ||
| Embedding.Config, | ||
| GroupNorm.Config, | ||
| LayerNorm.Config, | ||
| RMSNorm.Config, | ||
| ), | ||
| ) |
There was a problem hiding this comment.
This only cover the existing cases today, which won't extend well when a model has new types of submodules.
In fact, I don't think we have to test if something is parametrized at all. Can we just apply FrozenConfig wrapping regardless?
| elif ( | ||
| self._is_parameterized_non_linear_config(cfg) | ||
| and not self._has_nested_module_config(cfg) | ||
| ): |
There was a problem hiding this comment.
I don't think this is necessary / sufficient either.
What if a Module has submodule and trainable parameters -- would you skip setting requires_grad=False?
I think we just need to have two cases:
- if Linear.Config and matching target, do lora wrapping
- else do FrozenConfig wrapping
WDYT?
Overall there might be the tricky case where a Module has Linear submodule, and a trainable parameter, which we need to make sure traverse order and wrapping order is compatible.
There was a problem hiding this comment.
Thanks, this makes sense. I updated the PR to use the two-case logic:
- matching
Linear.Config-> LoRA wrapping - every other
Module.Config->FrozenConfigwrapping
To handle the composite-module case, FrozenConfig now freezes only direct parameters via parameters(recurse=False). Traversal still recurses into the original config tree, so child configs are converted/wrapped separately; this avoids recursively freezing nested LoRA adapters while still freezing direct parameters on a parent module that owns both parameters and submodules.
I also added a regression test with a composite module that has a direct Parameter plus a child Linear target, asserting the direct param and base weight are frozen and only LoRA adapter weights remain trainable.
Updated commit: 9d25530
a9489fd to
9d25530
Compare
| if self.param_init is not None: | ||
| self.inner.param_init = self.param_init | ||
| if self.sharding_config is not None: | ||
| self.inner.sharding_config = self.sharding_config |
There was a problem hiding this comment.
I'm assuming you are adding this because the order follows:
- create a non-lora model config, which doesn't have sharding config. E.g. https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/llama3/__init__.py#L213
- call lora config converter to freeze non-lora parameters. E.g. https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/llama3/config_registry.py#L118
- set sharding_config before actual build, e.g. https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/decoder_sharding.py#L239-L241
However, 3 should already be covered by the __setattr__ override above. Thus
- it is not necessary to do it here one more time
- actually I feel this could be wrong, because
self.sharding_configis not really set due to the setattr override (unless the override itself has bugs)
| @dataclass(kw_only=True, slots=True) | ||
| class FrozenConfig(Configurable.Config): | ||
| """Config wrapper that freezes all parameters at build time. | ||
| class FrozenConfig(Module.Config): |
There was a problem hiding this comment.
For the reason mentioned below, this should still not be a Module.Config subclass?
|
What should we do if we have nn.Parameter? |
what's the context of this question? |
Like this 'self.scale_shift_table = nn.Parameter(torch.empty(video_mod_param_num, config.dim))', bare nn.Parameter, not Module. |
What is "self", can we not wrap its config by |
like this, it is not wraped by Config. After convert, parameter still trainable. |
|
It's in and latest change would freeze it? |
|
emm,i haven't test the latest change, is this can freeze its parameters? |
|
Yeah, it can solve the ltx block, but it can't solve parameter in top model,like the WanTransformer3DModel: FrozenConfig is not a BaseModel.Config |
oh, I think that was caused by not searching the |
a2f1dbc to
a67de64
Compare
|
Thanks for pointing out the top-level bare What changed:
I also added regression coverage for:
Validation run locally in a Python 3.10 + PyTorch nightly CPU environment matching the unit-test workflow setup:
The full |
tianyu-l
left a comment
There was a problem hiding this comment.
plus some of my earlier comments not addressed
| config_cls: type, | ||
| *, | ||
| recurse: bool = False, | ||
| include_self: bool = False, |
There was a problem hiding this comment.
why providing False option? Should always include self.
| "in-place. Pass a model config that owns the target " | ||
| "Linear.Config as a field." | ||
| ) | ||
| cfg._freeze_direct_params = True |
There was a problem hiding this comment.
I don't see why we have to introduce this mechanism. Couldn't we make it self contained here?
| class Config(Configurable.Config): | ||
| param_init: dict | None = None | ||
| sharding_config: ShardingConfig | None = None | ||
| _freeze_direct_params: bool = field(default=False, init=False, repr=False) |
There was a problem hiding this comment.
shouldn't introduce this, I need to be convinced o/w
|
Thanks, this is fair. I tried to cover the top-level bare I'll rework the patch along these lines:
For the root bare-parameter case, I think the current in-place I also see the earlier |
I think this is fair, we can change ModelConfigConverter to return a modified model_config, not necessarily inplace. |
a67de64 to
e620d31
Compare
e620d31 to
338a6f0
Compare
|
Pushed an updated version at 338a6f0. This should address the earlier review comments and the top-level bare parameter case without keeping the LoRA-specific flag in Module.Config. What changed:
I added regression coverage for recursive traversal parent/attr semantics, composite modules with direct parameters, and root modules with direct parameters. Local validation:
I could not run test_lora.py end-to-end on my local macOS/Python 3.14 environment because the import path requires triton and there is no triton wheel for this environment, but the LoRA changes are covered by the added tests and should run in the Linux CI environment. |
| @@ -106,13 +107,13 @@ def forward(self, input: torch.Tensor) -> torch.Tensor: | |||
|
|
|||
| @dataclass(kw_only=True, slots=True) | |||
| class FrozenConfig(Configurable.Config): | |||
There was a problem hiding this comment.
CPU unit test failed. It seems caused by instance of this FrozenConfig would fail conditions like
if isinstance(x, FlexAttention.Config):
I'm not sure what's the best solution -- I guess we have to introduce dynamic subclassing?
338a6f0 to
5cbb2dc
Compare
|
Pushed an update at 1255e9a to address the CPU unit test failure from the frozen config wrapper. The previous One important detail: because the new frozen config is a copy rather than a forwarding wrapper, the converter now applies replacements from leaves to root. That keeps child LoRA replacements visible when the root/composite config is copied into its frozen subclass. I also added a regression test covering the Local validation:
I still cannot run |
| non-target Linear modules. | ||
| """ | ||
| class FrozenConfig(config_cls): # type: ignore[valid-type, misc] | ||
| _lora_frozen_config = True |
There was a problem hiding this comment.
This feels unnecessary, can we remove?
|
Thanks for the review. I removed the redundant |
| if any( | ||
| issubclass(config_cls, frozen_cls) | ||
| for frozen_cls in _frozen_config_class_cache.values() | ||
| ): | ||
| return config_cls |
There was a problem hiding this comment.
why do we need this? Is the two lines below not enough?
|
I updated the branch to address the latest CI state: removed the unrelated fake-backend timeout change from this PR and merged current upstream/main (the branch was 17 commits behind; the latest scheduled RL workflow on main passed). The prior RL failure occurred before LoRA conversion, while Tyro parsed a stale ModelSpec default. The PR diff is back to the LoRA/config-converter scope, and py_compile plus git diff --check pass locally. Could a maintainer approve/rerun the fork workflows on commit 280c352? |
|
The RL test is healthy on main https://github.com/pytorch/torchtitan/actions/workflows/integration_test_8gpu_rl.yaml?query=branch%3Amain |
|
I found the RL H100 failure root cause and pushed The PR changed The fix makes Local checks run:
I tried to run |
|
CI triage update after I was able to fetch the failing job logs through the job-level logs API ( Current failing checks on this PR:
I do not want to add unrelated fixes to this already-approved LoRA PR. Could a maintainer rerun the H100 job (network failure), and rerun/compare the 8GPU model + RL jobs once the corresponding |
|
I believe several issues have been fixed. Would you please rebase to latest main so that we can test this PR again? Thanks! |
Signed-off-by: Kevin-Li-2025 <[email protected]>
This reverts commit dc83a0d.
639bb4f to
e494cff
Compare
|
Rebased onto latest Local checks I ran after the rebase:
I also tried |
Clean merge, no conflicts, no code replays. Fused-QKV-by-default (pytorch#3714) flows via common/ but ezpz/agpt keeps fuse_qkv=False (zero checkpoint risk to 2B/80B chains); LoRA/protocol convert() change (pytorch#3456) benign for ezpz (moe quant builds OK); varlen+PP assert (pytorch#3777) and SPMD Partial size-1 fix (pytorch#3762) are no-op/free-win. Config-build smoke passed for agpt_2b/2b_real/80b + moe_10b_2b/sdpa.
Fixes #3438.
Summary
recursemode toConfigurable.Config.traversewhile preserving the default stop-at-match behaviorFrozenConfigaModule.Configwrapper so frozen configs still satisfy module-config type checksTest plan
python3 -m compileall -q torchtitan/config/configurable.py torchtitan/components/lora.py tests/unit_tests/test_lora.py tests/unit_tests/test_configurable.pygit diff --check.venv/bin/python -m ruff check torchtitan/config/configurable.py torchtitan/components/lora.py tests/unit_tests/test_lora.py tests/unit_tests/test_configurable.pyPYTHONPATH=. /tmp/torchtitan-py311-nightly/bin/python -m pytest -q tests/unit_tests/test_lora.py tests/unit_tests/test_configurable.py