Skip to content

Add single precision option to the dycore (intermediate state) - #970

Open
starkphi wants to merge 142 commits into
mainfrom
add_single_precision_dycore_part1
Open

starkphi wants to merge 142 commits into
mainfrom
add_single_precision_dycore_part1

Conversation

@starkphi

@starkphi starkphi commented Dec 5, 2025

Copy link
Copy Markdown

Adds the option to set wp/vpfloat to single via the env var ICON4PY_FLOAT_PRECISION (previous options were 'double' and 'mixed') to run in single precision.

This PR was created to later reduce merge complexity of #886.

Changes

  • Replace native python float types by either wpfloat or vpfloat
    • Change type annotations where there were native Python floats were present (which are by default passed as gtx.float64 to gtx.field_operators and gtx.programs) and add casts around float literals
    • Swap wpfloat <-> vpfloat where an inconsistency with an operator was caught (to hopefully make fixing mixed precision easier in the future)
    • Replace DBL_EPS by WP_EPS and VP_EPS
  • Setup pytests for single precision
    • Add pytest marker single_precision_ready (running a test with ICON4PY_FLOAT_PRECISION=single deselects all tests without) and mark integration tests for the standalone driver and most components (required much larger tolerances for some cases)
    • Remove pytest option --enable-mixed-precision (wpfloat or vpfloat are already set according to ICON4PY_FLOAT_PRECISION at import time of a pytest file)
    • Added PRECISION_VARIANTS as dimension to the ci pipeline matrix
  • Keep calculations and results in gtx.float64 internally in geometry, metrics or interpolation factories independently of the selected precision. Mainly to keep the precision of the RBF and other geometry factors acceptable when running in ICON4PY_FLOAT_PRECISION
    • factory.store_allfloats_as_double forces float allocations in a FieldSource to double-precision
    • FieldSource.export_field casts to the dtype declared in the field metadata where the fields leave the factories

@starkphi
starkphi force-pushed the add_single_precision_dycore_part1 branch from dd41784 to adcf8fb Compare December 5, 2025 15:49
@starkphi
starkphi force-pushed the add_single_precision_dycore_part1 branch from 577d435 to f0d747e Compare December 8, 2025 11:08
@starkphi
starkphi force-pushed the add_single_precision_dycore_part1 branch from f0d747e to 5daea04 Compare December 8, 2025 14:30
@muellch

muellch commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

cscs-ci run default

Comment thread model/driver/src/icon4py/model/driver/icon4py_driver.py Outdated

@egparedes egparedes left a comment

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.

It looks good in general although I have a few comments.

Comment thread model/common/src/icon4py/model/common/grid/vertical.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
Comment thread model/testing/src/icon4py/model/testing/test_utils.py Outdated
Comment thread model/atmosphere/dycore/tests/dycore/integration_tests/test_velocity_advection.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/velocity_advection.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/velocity_advection.py Outdated
Comment thread model/common/src/icon4py/model/common/type_alias.py Outdated
# Check if the --enable-mixed-precision option is set and set the environment variable accordingly
if config.getoption("--enable-mixed-precision"):
os.environ["FLOAT_PRECISION"] = "mixed"
config.addinivalue_line(

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.

Question: is this marker needed only in a transition phase? Otherwise, why is actually needed?

@starkphi starkphi Dec 19, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, that would be the idea.
I thought that it might be good to be able to have all tests skipped that have not been prepared for single precision yet. So I suggest using this marker until all tests have suitable tolerances defined for single precision operation.

Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
Comment thread model/atmosphere/diffusion/src/icon4py/model/atmosphere/diffusion/diffusion.py Outdated
@starkphi

Copy link
Copy Markdown
Author

cscs-ci run default

starkphi and others added 4 commits September 10, 2026 18:55
This reverts commit 611f658.

Without the int(..) there appears a 0-dim cupy array as argument in
`range` and an error is raised telling that this can not be interpreted
as `int`.
…con4py into add_single_precision_dycore_part1
starkphi added a commit to starkphi/icon4py that referenced this pull request Sep 14, 2026
Inspired by [a comment](C2SM#970 (comment)) by @msimberg
- replaced export_field -> get, get(.., RetrievalType=Metadata) ->
get_metadata
- cherry-picked factory.py changes from main
[#1463](#1463): Moved output_dtype
into FieldSource

---------

Co-authored-by: Hannes Vogt <hannes@havogt.de>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Comment thread model/atmosphere/diffusion/src/icon4py/model/atmosphere/diffusion/diffusion.py Outdated

@msimberg msimberg left a comment

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.

Tentative approval with knowledge that there are a few things to clean up still. The overall changes look good to me now!

Comment thread model/atmosphere/diffusion/tests/diffusion/integration_tests/test_diffusion.py Outdated
Comment thread noxfile.py Outdated
Comment thread model/driver/src/icon4py/model/driver/driver_utils.py Outdated
offset_provider={},
)

#TODO(pstark): Probably dz (or it + others wrapped in a MuphysMetricState) should be an arg in State.__init__

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.

Is this something you wanted to handle still in this PR (it sounds like a follow-up)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's indeed a follow up. Just wanted to mark this. It's the only place where a factory appears inside a state.

Comment thread model/atmosphere/dycore/src/icon4py/model/atmosphere/dycore/solve_nonhydro.py Outdated
@github-actions

Copy link
Copy Markdown

When developing, you can test your changes on CSCS CI before merge with the default pipeline: cscs-ci run default. This will run a default subset of tests.

You can pass options to override pipeline variables, for example:

  • cscs-ci run default;BACKENDS=gtfn_cpu;LEVELS=unit
  • cscs-ci run default;MODEL_SUBPACKAGES=common:driver;SESSIONS=model

Avoid running the pipeline for all tests when you are developing.

Available options are:

  • SESSIONS: model, model_mpi, or tools (correspond to nox sessions)
  • MODEL_SUBSETS: datatest, basic, or stencils (correspond to nox session selections)
  • MODEL_SUBPACKAGES: subpackages for non-MPI tests (last component, e.g. diffusion, driver)
  • MODEL_MPI_SUBPACKAGES: subpackages for MPI tests (as above)
  • BACKENDS: backends
  • GRIDS: grids for stencil tests (simple, icon_regional, or icon_global)
  • LEVELS: testing level for non-stencil tests (unit or integration)

For each option, all can be used as a shorthand for all possible values of that variable, e.g. LEVELS=all.

Multiple values can be given to each option with : used as the separator (; separates options and , separates pipelines).

See scripts/python/generate_ci_pipeline.py and noxfile.py for available values for each option.

The all pipeline can be run with cscs-ci run all. This will run all icon4py tests in CSCS CI which can be expensive. This pipeline runs on a schedule on main, and can be run when extensive validation is needed (e.g. before releases).

Merging

Once your PR is approved and ready for merging, add it to the merge queue. The merge CSCS CI pipeline will run automatically on the merge-queue branch and must pass before the PR is merged. A dummy merge check will be triggered on the PR itself since it's required to add a PR to the merge queue.

Optional Tests

To run benchmarks you can use:

  • cscs-ci run benchmark-bencher

For more detailed information please look at CI in the EXCLAIM universe.

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.

7 participants