Add support for weighted-sum heads on multi-head checkpoints - #157
Add support for weighted-sum heads on multi-head checkpoints#157zyxwwxyz wants to merge 12 commits into
Conversation
|
|
||
| .. autofunction:: create_weighted_sum_checkpoint | ||
|
|
||
| .. autofunction:: extract_wrapped_checkpoint |
There was a problem hiding this comment.
Not sure if this is worth having as a new public function. Could it go through the normal get_upet & co?
There was a problem hiding this comment.
I am not finding a nice way to do it. The two functions are a distinct checkpoint-authoring step that is not directly compatible with standard get_upet & co. Maybe I can tighten the namespace/docs to make that separation clearer.
There was a problem hiding this comment.
ah, I misunderstood what extract_wrapped_checkpoint was doing. I'm not fully convinced it is required (the unwrapped checkpoint should exist elsewhere anyway), but it is fine to keep it!
There was a problem hiding this comment.
I would honestly keep it as well. If I want to publish a model on huggingface, I think it is easier to publish the .ckpt file with weighted heads only, and leave users (and the future me) the opportunity of stripping these heads and do further fine-tuning, rather than uploading both the models with and without weighted sum heads.
|
|
||
| def create_weighted_sum_checkpoint( | ||
| checkpoint_path: str, | ||
| specs: Dict[str, Dict[str, Any]], |
There was a problem hiding this comment.
I don't love this API, because one does not know what needs to go in the specs without reading the docs. I would rather have separate keyword arguments with descriptive names directly
There was a problem hiding this comment.
I tried to solve it by replacing WeightedSumModel's untyped dict specs with a typed WeightedSumHead dataclass.
|
I don't understand the need for such a functionality, especially within a CLI command. I we need a weighted sum - we can get one by running run_model method of the underlying MetatomicCalculator. |
The reason of this PR is to implement the pet-exp protocol (https://arxiv.org/html/2604.24607v1), in order to do things like MD/phonon calculations with lammps/i-pi/ase using a weighted sum of various heads of a model as a force field. About the CLI command, I think this is a minor side issue that we can easily remove. |
Summary
Adds a way to attach fixed-coefficient linear-combination heads to a trained multi-head checkpoint - e.g. an
energy/mixhead equal to0.25 * energy/pbe + 0.75 * energy/pbesolwithout any retraining. Linear-combination coefficients are hard-coded in the head, so the head contains no free parameters, and needs no retraining.upet._weighted_summodule exposingcreate_weighted_sum_checkpoint/extract_wrapped_checkpoint(also re-exported from upet), plus a CLI (python -c "from upet._weighted_sum import _main; _main()") driven by a YAML spec ofsources: {head: coefficient}per new head.normalize_coefficients: true(raises an error if the provided coefficients sum to zero).specs(forcreate_weighted_sum_checkpoint/WeightedSumModel) takesWeightedSumHeadinstances (a small dataclass:sources,normalize_coefficients,description) rather than an untyped dict, so a misspelled or unsupported field fails immediately with aTypeError.description(auto-generated from sources/coefficients, or overridable) stored on the exported model.src/upet/_models.py: _load_model_with_custom_headsteachesupet's loading path to recognize these wrapped checkpoints (viaarchitecture_name) and reconstruct them throughWeightedSumModel.load_checkpoint, since they aren't a registered metatrain architecture and can't go throughmtt export/mtt evalor plainload_modeldirectly.pyyamlas a new dependency (for the YAML spec format).docs/src/weighted-sum-heads.rstpage with CLI and Python API usage, linked fromindex.rst/fine-tuning.rst; the two top-level functions are also documented inapi.rstalongsideget_upet/save_upet.Example
model-wsum.ckptstill works withget_upet/save_upetviacheckpoint_path=...; useextract_wrapped_checkpointto pull the original checkpoint back out for further fine-tuning.Notes for reviewers
get_upet/save_upetviacheckpoint_path=....To fine-tune further, pull the original back out first withextract_wrapped_checkpoint.tests/upet/test_weighted_sum.pycover spec parsing (YAML + inline--head),normalize_coefficientsrescaling (including the zero-sum-coefficients error),WeightedSumHead's own field validation, description defaults/overrides, checkpoint round-tripping, the_mainCLI end to end, and forces/stresses correctness against the wrapped model.Test plan
pytest tests/upet/test_weighted_sum.pya0828f9added more rigorous coverage)docs/src/weighted-sum-heads.rstrenders correctly,sphinxrefs resolve)