Skip to content

[LoRA] Ignore Megatron fields in LoRA adapter metadata - #15412

Open
LinZiyuu wants to merge 2 commits into
huggingface:mainfrom
LinZiyuu:fix/lora-metadata-drop-megatron-keys
Open

LinZiyuu wants to merge 2 commits into
huggingface:mainfrom
LinZiyuu:fix/lora-metadata-drop-megatron-keys

Conversation

@LinZiyuu

@LinZiyuu LinZiyuu commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

_create_lora_config passes a LoRA checkpoint's lora_adapter_metadata straight to LoraConfig(**metadata). Among the fields this lets a checkpoint set are megatron_config and megatron_core. When megatron_config is set, peft builds Megatron tensor-parallel LoRA layers and imports the module named in megatron_core (peft.tuners.lora.tp_layer.dispatch_megatron). Diffusers models never use Megatron layers, so nothing a diffusers checkpoint legitimately needs depends on these fields.

This PR drops megatron_config / megatron_core from the metadata before building the LoraConfig, and logs a warning when a checkpoint actually requests Megatron (megatron_config is not None). Everything else in the metadata is used as before.

Note on the warning condition: adapters saved by diffusers always carry both fields with their defaults (megatron_core="megatron.core", megatron_config=None), because save_lora_adapter stores LoraConfig.to_dict(). So warning on the mere presence of the keys would fire on every regular LoRA load. Warning only when megatron_config is set avoids that.

This follows up on a private discussion with @sayakpaul and @BenjaminBossan, where dropping the Megatron entries was agreed as the fix.

Tests

Added test_lora_adapter_metadata_megatron_fields_are_ignored to LoraTesterMixin (tests/models/testing_utils/lora.py), so it runs for every model that uses the mixin. It saves an adapter with save_lora_adapter, rewrites the lora_adapter_metadata in the safetensors header, loads it back with load_lora_adapter, and checks:

  • with megatron_core / megatron_config set: model.peft_config["default_0"] has the default Megatron values, a warning is logged, and the injected layers are regular peft.tuners.lora.Linear;
  • with the default values (what diffusers itself saves): the same, but no warning.

On current main, the Megatron case fails with ModuleNotFoundError: No module named 'megatron' (peft imports the module named in megatron_core); with this PR both cases pass. I ran the LoRA metadata tests under tests/models locally (macOS, MPS, peft 0.21.2, torch 2.14.1): all pass except the CogVideoX ones, which fail the same way on unmodified main here (MPS has no float64).

Self-review notes

Reviewed the diff against .ai/references/review-rules.md, code_style.md and testing.md:

  • First pass flagged two issues, both fixed: the warning fired for default-valued Megatron keys that every saved adapter contains, and a test monkeypatched importlib.import_module to record calls (against the testing guide's advice on call-level doubles); it now asserts on the resulting config and injected layers instead.
  • Left for review: _create_lokr_config also builds LoKrConfig(**metadata) from checkpoint metadata. LoKrConfig has no import-by-name field today, so it is not changed here; happy to apply the same filtering there if you prefer.
  • No docs impact (internal helper, no public API change). Verdict: ready.

Before submitting

  • Did you use an AI agent to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case. (Discussed privately with the maintainers, see above.)
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Who can review?

@sayakpaul @BenjaminBossan

`_create_lora_config` passed a checkpoint's `lora_adapter_metadata` to
`LoraConfig` as-is. That metadata can set `megatron_config` and
`megatron_core`, which make peft build Megatron tensor-parallel layers by
importing the module named in `megatron_core`. Diffusers models never use
these layers, so drop both fields from the metadata and warn when a
checkpoint requests Megatron.

Regularly saved adapters always carry the two fields with their default
values (`LoraConfig.to_dict()` includes them), so the warning only fires
when `megatron_config` is set.
@github-actions github-actions Bot added tests utils size/M PR with diff < 200 LOC labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Hi @LinZiyuu, thanks for the PR! It does not appear to link an issue it fixes.

  • If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked.
  • If you are the author of the model/pipeline this PR relates to, or part of the team we work with on it, tick the Are you the author (or part of the team we work with) of the model/pipeline this PR relates to? box in the PR description (if you removed the template, paste that line back, checked). This reminder then no longer applies.
  • Otherwise there is nothing you need to do: a maintainer will look at the PR and add the no-issue-needed label to silence this reminder if it should stay open without an issue.

See the contribution guide for more details.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an existing issue, the author box is ticked, or the no-issue-needed label is added, you can ignore this message — it stays here as a comment, but it no longer applies.

@yiyixuxu yiyixuxu added the no-issue-needed for PRs that do not require link to an issue label Dec 2, 2026
@yiyixuxu
yiyixuxu requested a review from sayakpaul December 2, 2026 09:09
Comment thread tests/lora/test_peft_utils.py Outdated
Comment on lines +153 to +154
model = inject_adapter_in_model(config, _TinyAttention(), adapter_name="default")
assert isinstance(model.to_q, LoraLinear)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should also check if Megatron-related attributes are not set in the peft_config set in model.

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.

Done, the test now also checks model.peft_config["default_0"] after loading.

Comment thread tests/lora/test_peft_utils.py Outdated


@require_peft_backend
def test_lora_metadata_megatron_fields_are_ignored():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another point is these tests exercise private APIs. Could we check with more public-facing APIs?

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.

Good point. I moved the test into LoraTesterMixin, so it goes through save_lora_adapter / load_lora_adapter and runs for every model that uses the mixin, and removed the _create_lora_config tests.

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

Move the tests off the private `_create_lora_config` and into
`LoraTesterMixin`, so they go through `save_lora_adapter` /
`load_lora_adapter` for every model using the mixin. Also check the
`peft_config` stored on the model after loading.

This branch has not been deployed

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

Labels

no-issue-needed for PRs that do not require link to an issue size/M PR with diff < 200 LOC tests utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants