Add opt-in hook to exempt fields from the fp-dtype downcast - #127
Add opt-in hook to exempt fields from the fp-dtype downcast#127EricZQu wants to merge 6 commits into
Conversation
AtomicData.check_fp_dtype_consistency casts every floating-point tensor to
positions.dtype (typically float32) so the model computes in one precision.
That is the right default for compute inputs, but it also downcasts label
fields: e.g. total energy is extensive (~1e4-1e5 eV for large systems), so
float32 quantizes it to ~1e-2 eV and reconstructed energies come out
discrete/staircased downstream.
Add a _precision_preserving_keys ClassVar and skip those fields in the cast.
It is empty by default, so behavior is unchanged; a subclass can opt a
high-precision label out of the downcast, e.g.
class MyData(AtomicData):
_precision_preserving_keys = frozenset({"energy"})
The exemption holds across add_system_property / add_node_property, since
validate_assignment=True re-runs the validator on every setattr.
Signed-off-by: Eric Qu <ericq@nvidia.com>
Greptile SummaryThis PR adds an opt-in
Important Files Changed
Reviews (4): Last reviewed commit: "Merge branch 'main' into allow-fp64-ener..." | Re-trigger Greptile |
|
Added tests for batch, and a first time warning for casting |
laserkelvin
left a comment
There was a problem hiding this comment.
I'll conditionally approve - I think I agree with the behavior change, but I'm not 100% set on how it's being done here.
I'm not 100% sure if this is best as part of the private API: the way you are using this in the test is by subclassing AtomicData which I think is not unreasonable, but I wonder if we should just let users be able to change the precision preservation without jumping through this hoop.
I'm considering these two scenarios:
- You modify the set once per Python session, so that the change in casting behavior is global - you can do this simply by making
_precision_preserving_keyspublic - You are working in a notebook environment, and you want to change the casting behavior temporarily for some subset of the data - for this you would need to have it as instance level
Can you consider the latter case? We can mark it out of scope for now, but I just want you to try and think of a good solution
|
|
||
| # Process-global (field, src_dtype, tgt_dtype) casts already warned about, so | ||
| # check_fp_dtype_consistency warns once per cast rather than per construction. | ||
| _fp_cast_warned: ClassVar[set[tuple[str, str, str]]] = set() |
There was a problem hiding this comment.
Ooof I'm not a big fan of this since it clutters up the schema...
I think I would be okay to have _FP_CAST_WARNED as like a module-level variable that tracks instead of it belonging to the schema (even though it's nominally a ClassVar).
|
/ok to test ac8b6d2 |
|
Thanks a lot for the comments @laserkelvin! The API is public now and the warned set is now global. I thought about the per-instance "notebook" use case, and I think maybe we could use a context manager ( |
ALCHEMI Toolkit Pull Request
Description
Add opt-in hook to exempt fields from the fp-dtype downcast.
Type of Change
Related Issues
Changes Made
AtomicData.check_fp_dtype_consistency casts every floating-point tensor to positions.dtype (typically float32) so the model computes in one precision. That is the right default for compute inputs, but it also downcasts label fields: e.g. total energy is extensive (~1e4-1e5 eV for large systems), so float32 quantizes it to ~1e-2 eV and reconstructed energies come out discrete/staircased downstream.
Add a _precision_preserving_keys ClassVar and skip those fields in the cast. It is empty by default, so behavior is unchanged; a subclass can opt a high-precision label out of the downcast, e.g.
The exemption holds across add_system_property / add_node_property, since validate_assignment=True re-runs the validator on every setattr.
Testing
make pytest)make lint)Checklist
Additional Notes
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.