Skip to content

settings.dtype and MACE-style calculator default_dtype - #45

Merged
Roy-Kid merged 6 commits into
Teoroo-CMC:masterfrom
Roy-Kid:feat/train-precision
Sep 16, 2026
Merged

Roy-Kid merged 6 commits into
Teoroo-CMC:masterfrom
Roy-Kid:feat/train-precision

Conversation

@Roy-Kid

@Roy-Kid Roy-Kid commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • YAML settings.dtype is float32 or float64 (default float32). Float weights and Keras ops use that dtype.
  • ASE calculator default_dtype is a constructor argument only (not YAML), same idea as MACE. None follows settings.dtype.
  • If inference dtype differs from training, the predict graph is built at default_dtype and the checkpoint is cast once at load (tf.cast into the new variables — TF equivalent of model.float() / model.double()). After that the whole network runs in that dtype.
  • ASE MD stays float64; the calculator boundary copies ASE arrays into TF tensors of default_dtype.
  • Ships as v2.1.1.
settings:
  dtype: float32
get_calc(model_dir)                            # same dtype as training
get_calc(model_dir, default_dtype='float32')   # like MACE model.float()

Test plan

  • tests/test_precision.py: name parsing, YAML dtype, calculator override, float32 default, float64 short train
  • existing potential tests still pass

Add a top-level `precision` key (fp32 / fp16 / bf16). fp16 and bf16
use Keras mixed precision so variables stay float32. fp16 uses a
static loss scale of 128.
Keep precision as a child of numeric so later numerical knobs
(loss scale, TF32) have a place without sitting on network or
optimizer.
YAML only configures training: settings.train_dtype is float32 or
float64 (default float32) for all float weights and ops. The ASE
calculator takes default_dtype as a constructor argument, not a YAML
key. If it differs from training, checkpoint weights are cast once at
load (like model.float()/double()) and the network runs in that dtype.
ASE MD remains float64.
@Roy-Kid Roy-Kid changed the title Allow specifying training precision in the YAML params settings.train_dtype and MACE-style calculator default_dtype Aug 24, 2026
@Roy-Kid
Roy-Kid requested review from AishSu and zzy2014 August 24, 2026 10:24
Comment thread docs/usage/cli/train.md Outdated
Train a PiNN model given a parameter file.

The parameter YAML may include `settings.train_dtype` (`float32` or
`float64`; default `float32`). That key is training-only: it sets Keras

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why the key is training-only? In my view, it is controlling all the precision of PiNN on both training/validation and prediction stages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Training here means training stage, opposite to inference/MD stage. You cant control how to use the model in the input yaml file

Comment thread docs/usage/cli/train.md Outdated
The parameter YAML may include `settings.train_dtype` (`float32` or
`float64`; default `float32`). That key is training-only: it sets Keras
`floatx` / dtype policy, so new network weights, Keras ops, and numpy/ANI
float inputs use that width. Integer indices stay `int32`. TFRecords keep

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no! int32 means we set dtype of index(ind_1, ind_2) as int32, unchanged.

Comment thread docs/usage/models.md Outdated
`settings` is training-time configuration. The only dtype key in the YAML is
`train_dtype` (`float32` or `float64`; default `float32`). It is **not** the
ASE calculator's `default_dtype` (that argument exists only on
`PiNN_calc` / `get_calc`, see [Potential](potential.md#ase-calculator)).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

YAML already describes training, so use settings.dtype with TF names
only (float32/float64). Drop fp aliases and canonical helpers; default
to float32 unless settings overrides. Slim docs; calculator default_dtype
None follows settings.dtype.
@Roy-Kid Roy-Kid changed the title settings.train_dtype and MACE-style calculator default_dtype settings.dtype and MACE-style calculator default_dtype Aug 25, 2026
tf.as_dtype raises TypeError for names it does not know (e.g. fp64);
wrap that so settings.dtype typos stay a consistent ValueError.
@Roy-Kid
Roy-Kid force-pushed the feat/train-precision branch from ac6948b to dae3423 Compare August 26, 2026 08:24
_CastSaver.restore ran var.assign(ndarray), which adds a const + assign op
pair to the graph. The Estimator finalizes the graph before the scaffold's
saver restores, so serving a float32 checkpoint at float64 (or the reverse)
through PiNN_calc always raised "Graph is finalized and cannot be modified".

var.load() feeds the value into the variable's existing initializer instead,
creating no ops. Add a regression test that trains a tiny float32 model and
predicts through PiNN_calc at both dtypes -- the previous test only covered
the dtype plumbing, never a cross-dtype restore.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Roy-Kid
Roy-Kid merged commit 6c930d1 into Teoroo-CMC:master Sep 16, 2026
8 checks passed
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