Skip to content

Added sample_kind to ModelOutput, without breaking changes#168

Open
pfebrer wants to merge 7 commits intomainfrom
sample_kind
Open

Added sample_kind to ModelOutput, without breaking changes#168
pfebrer wants to merge 7 commits intomainfrom
sample_kind

Conversation

@pfebrer
Copy link

@pfebrer pfebrer commented Feb 26, 2026

This PR substitutes #165.

It also implements sample_kind, with the short-term goal of enabling per-pair targets, but in this case in a way that doesn't break backward compatibility (new code using old ModelOutput might fail, but old code using the new ModelOutput won't). This is achieved by keeping the possibility to pass per_atom as an argument, and setting/getting it as a property, even though from now on the only thing ModelOutput stores is sample_kind.

Thanks to @Luthaf for letting me know that one can set optional arguments in torch exportable classes :)

If you agree with merging this one I will finalize it with some more tests ✌️

📚 Download documentation for this pull-request

⚙️ Download Python wheels for this pull-request (you can install these with pip)

📚 Download documentation for this pull-request

⚙️ Download Python wheels for this pull-request (you can install these with pip)

📚 Download documentation for this pull-request

⚙️ Download Python wheels for this pull-request (you can install these with pip)

📚 Download documentation for this pull-request

⚙️ Download Python wheels for this pull-request (you can install these with pip)

📚 Download documentation for this pull-request

@pfebrer
Copy link
Author

pfebrer commented Mar 13, 2026

This is ready for a final look. I checked that one can take the conda lammps-metatomic and use this modified metatomic version without problems to run an MD with a pet-mad.pt exported using the last metatomic release. The results of the MD are exactly the same as when running without the modified version.

Comment on lines 48 to +54
ModelOutputHolder(
std::string quantity,
std::string unit,
bool per_atom_,
torch::optional<bool> per_atom_,
std::vector<std::string> explicit_gradients_,
std::string description_
std::string description_,
torch::optional<std::string> sample_kind = torch::nullopt
Copy link
Member

Choose a reason for hiding this comment

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

So we should keep the previous constructor as-is for compatibility, and add another one that takes only sample_kind.

This one is useful for the Python API bindings, but I would do something like

ModelOutputHolder(string quantity, string unit, torch::IValue _3, string[] explicit_gradients, string description, string? sample_kind, bool? per_atom)

And then allow to pass sample_kind as a positional argument in the same place as the previous per_atom, while still supporting keyword arguments, and disallowing setting both as the same time

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand what is the use of torch::IValue _3, I have done the changes without it and it seems fine

Copy link
Member

Choose a reason for hiding this comment

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

Basically allowing someone to do ModelOutput("quantity", "unit", true) as well as ModelOutput("quantity", "unit", "atom_pair"), while also allowing for ModelOutput("quantity", "unit", per_atom=true) and ModelOutput("quantity", "unit", sample_kind="atom")

Copy link
Author

Choose a reason for hiding this comment

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

But for this to work I imagine I have to process the _3 argument no? Or this is magic? If you have something specific in mind please go ahead with the changes

Comment on lines +92 to +94
"Sample_kind '", sample_kind, "' is not officially supported. ",
"This means that metatomic doesn't natively understand how to deal ",
"with such outputs. If this is a mistake, pass one of the supported ",
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, not sure if a warning is the solution here =/ I'd rather make this an error.

Copy link
Author

Choose a reason for hiding this comment

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

The error will be raised when validating data. My idea is that this will help people play with new sample kinds before they are officially supported, which is nice

}
} else {
/// We don't validate values for other cases for now
return;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should do some validation here =)

Copy link
Author

Choose a reason for hiding this comment

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

How can we know the expected samples? This depends on the system and the neighbor list options 😅

Copy link
Author

Choose a reason for hiding this comment

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

I imagine there is some way, do you want to do it? :) This feels daunting to me since it is my first C++ PR haha

Copy link
Member

Choose a reason for hiding this comment

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

This depends on the system and the neighbor list options 😅

Hmm, true =/ I guess we can only validate that the names are corrects and the indices in-bounds. I can do this if you want!

Copy link
Author

Choose a reason for hiding this comment

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

Yes go ahead 😅 The system that you receive for validation doesn't contain the neighbor list?

pfebrer and others added 2 commits March 16, 2026 15:21
Co-authored-by: Guillaume Fraux <guillaume.fraux@epfl.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants