Repository navigation
Conversation
`_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.
|
Hi @LinZiyuu, thanks for the PR! It does not appear to link an issue it fixes.
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 |
| model = inject_adapter_in_model(config, _TinyAttention(), adapter_name="default") | ||
| assert isinstance(model.to_q, LoraLinear) |
There was a problem hiding this comment.
We should also check if Megatron-related attributes are not set in the peft_config set in model.
There was a problem hiding this comment.
Done, the test now also checks model.peft_config["default_0"] after loading.
|
|
||
|
|
||
| @require_peft_backend | ||
| def test_lora_metadata_megatron_fields_are_ignored(): |
There was a problem hiding this comment.
Another point is these tests exercise private APIs. Could we check with more public-facing APIs?
There was a problem hiding this comment.
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.
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.
What does this PR do?
_create_lora_configpasses a LoRA checkpoint'slora_adapter_metadatastraight toLoraConfig(**metadata). Among the fields this lets a checkpoint set aremegatron_configandmegatron_core. Whenmegatron_configis set, peft builds Megatron tensor-parallel LoRA layers and imports the module named inmegatron_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_corefrom the metadata before building theLoraConfig, and logs a warning when a checkpoint actually requests Megatron (megatron_configis notNone). 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), becausesave_lora_adapterstoresLoraConfig.to_dict(). So warning on the mere presence of the keys would fire on every regular LoRA load. Warning only whenmegatron_configis 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_ignoredtoLoraTesterMixin(tests/models/testing_utils/lora.py), so it runs for every model that uses the mixin. It saves an adapter withsave_lora_adapter, rewrites thelora_adapter_metadatain the safetensors header, loads it back withload_lora_adapter, and checks:megatron_core/megatron_configset:model.peft_config["default_0"]has the default Megatron values, a warning is logged, and the injected layers are regularpeft.tuners.lora.Linear;On current
main, the Megatron case fails withModuleNotFoundError: No module named 'megatron'(peft imports the module named inmegatron_core); with this PR both cases pass. I ran the LoRA metadata tests undertests/modelslocally (macOS, MPS, peft 0.21.2, torch 2.14.1): all pass except the CogVideoX ones, which fail the same way on unmodifiedmainhere (MPS has no float64).Self-review notes
Reviewed the diff against
.ai/references/review-rules.md,code_style.mdandtesting.md:importlib.import_moduleto record calls (against the testing guide's advice on call-level doubles); it now asserts on the resulting config and injected layers instead._create_lokr_configalso buildsLoKrConfig(**metadata)from checkpoint metadata.LoKrConfighas no import-by-name field today, so it is not changed here; happy to apply the same filtering there if you prefer.Before submitting
self-reviewskill on the diff?Who can review?
@sayakpaul @BenjaminBossan