feat(training): add LoRA fine-tuning strategy - #149
Conversation
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Greptile SummaryThis PR adds LoRA-based parameter-efficient fine-tuning (PEFT) to
Important Files Changed
Reviews (5): Last reviewed commit: "reduce epochs in example" | Re-trigger Greptile |
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
| # Register LoRA wrappers and apply LoRA adapters according to the saved strategy. | ||
| register_builtin_lora_wrappers() | ||
| for layer_cls, wrapper_cls in cls._wrapper_registrations_from_spec(strategy): | ||
| _peft.register_lora_wrapper(layer_cls, wrapper_cls) |
There was a problem hiding this comment.
A trust policy shoud be enforced for the custom classes in strategy.json. Coincidently, there is a related PR in another project: isayevlab/aimnetcentral#108
There was a problem hiding this comment.
There was a problem hiding this comment.
Good suggestion! In the refactored version, I have now introduced allowed_import_paths and trust_remote_code controls in nvalchemi/training/peft/loading.py, specifically for load_peft_checkpoint_into_model.
| if not isinstance(normalized, Mapping): | ||
| return | ||
| current = { | ||
| name: _peft.compute_base_fingerprint(model) |
There was a problem hiding this comment.
Note, this computes hash of the architecture, not the weights. LoRA weights should be tied to the weights of the model.
There was a problem hiding this comment.
Yes, I am aware of that. I have not added weight hashes yet as I am still thinking through the best way to do it without hurting scalability for large models.
| for name in model_names: | ||
| # Collect this model's patch targets, metadata, and trainable states. | ||
| patch_prefix = f"{name}." | ||
| named_parameters = dict(checkpoint_models[name].named_parameters()) |
There was a problem hiding this comment.
Not commonly used in MLIPs, but buffers like running_mean and running_var for BatchNorm should also be exported.
There was a problem hiding this comment.
Thanks for pointing it out. I have now added buffers in nvalchemi.training._checkpoint._filter_snapshot_to_trainable_state.
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
Feature/lora finetuning refactor
Signed-off-by: Ying Shi Teh <yteh@nvidia.com>
ALCHEMI Toolkit Pull Request
Description
Adds LoRA-based parameter-efficient fine-tuning support within
FineTuningStrategyfor pretrained MLIP models. The implementation supportspeft_config=LoRAConfig(...), equivariant LoRA wrappers for e3nn/cuEquivariance layers, trainable-state PEFT checkpoint loading, base-model fingerprint validation, and a MACE LoRA fine-tuning example.FSDP support is out of scope for this PR. CLI support will be added in a separate PR.
Type of Change
Related Issues
N/A
Changes Made
peft_configsupport toFineTuningStrategy, with LoRA as the currently supported PEFT method.FineTuningStrategy.trainable_patterns.save_trainable_state_onlysupport toTrainingStrategy.save_checkpointandCheckpointHookfor parameter-efficient checkpoint saving.ModulePatchHookandTrainableParameterHookto use registered trainable/managed parameter names.FineTuningSummaryHookto provide a concise report on trainable parameters.DemoModelWrapper.from_checkpoint/export_modelfull-model round-tripping.LoRAConfig, fine-tuning serialization, checkpoint validation, PEFT checkpoint loading, and merge-into-base support.LoRAConfigsupport for configuring LoRA adapter injection throughFineTuningStrategy.e3nn.o3.Linear, e3nn fully connected layers, and cuEquivariance linear layers.examples/intermediate/08_lora_finetuning.pyfor MACEmedium-mpa-0LoRA fine-tuning on LPSC data.Testing
Targeted tests run:
uv run --extra cu12 pytest test/training/test_peft_lora.py test/training/test_finetune.py test/training/test_reference_energies.py test/training/test_strategy.py test/training/test_checkpoint.py(passed: 252 tests)Unit tests pass locally (
make pytest)Linting passes (
make lint)New tests added for new functionality meets coverage expectations?
Checklist
Additional Notes
Simple benchmarking sanity check with
TRAINING_EPOCHS=100using the provided LoRA example, compared against a similar setup trained from scratch:This currently depends on a PhysicsNeMo version with the PEFT LoRA support used here. The version pin can be revisited and updated as the required PEFT APIs become available in a newer stable PhysicsNeMo release.
Tip
This repository uses Greptile, an AI code review service, to help conduct
pull request reviews. We encourage contributors to read and consider suggestions
made by Greptile, but note that human maintainers will provide the necessary
reviews for merging: Greptile's comments are not a qualitative judgement
of your code, nor is it an indication that the PR will be accepted/rejected.
We encourage the use of emoji reactions to Greptile comments, depending on
their usefulness and accuracy.