From 395614d037694e0807c2b07c4294d84c8cccb95b Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Fri, 31 Jan 2025 13:20:37 -0500 Subject: [PATCH 1/9] Added feature to enable automatic detection of integer precision. Should remove the need for i32/i64 declaration (although their functionality is still retained) and replace both with the regular Int type --- ndsl/dsl/typing.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index b3fa72d8..fa3e692d 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -48,9 +48,35 @@ def global_set_floating_point_precision(): ) +def integer_precision() -> int: + return int(os.getenv("PACE_INT_PRECISION", "64")) + + +# We redefine the type as a way to distinguish +# the model definition of a float to other usage of the +# common numpy type in the rest of the code. +NDSL_32BIT_INT_TYPE = np.int32 +NDSL_64BIT_INT_TYPE = np.int64 + + +def global_set_integer_precision(): + """Set the global floating point precision for all reference + to Float in the codebase. Defaults to 64 bit.""" + global Int + precision_in_bit = integer_precision() + if precision_in_bit == 64: + return NDSL_64BIT_INT_TYPE + elif precision_in_bit == 32: + return NDSL_32BIT_INT_TYPE + else: + NotImplementedError( + f"{precision_in_bit} bit precision not implemented or tested" + ) + + # Default float and int types Float = global_set_floating_point_precision() -Int = np.int_ +Int = global_set_integer_precision() Bool = np.bool_ FloatField = Field[gtscript.IJK, Float] From f149a35c992b3699b974d246b194d2b050daa875 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Fri, 31 Jan 2025 13:21:23 -0500 Subject: [PATCH 2/9] change default rebuild state to false for get_factories --- ndsl/boilerplate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndsl/boilerplate.py b/ndsl/boilerplate.py index a777cd82..41b34820 100644 --- a/ndsl/boilerplate.py +++ b/ndsl/boilerplate.py @@ -44,7 +44,7 @@ def _get_factories( compilation_config = CompilationConfig( backend=backend, - rebuild=True, + rebuild=False, validate_args=True, format_source=False, device_sync=False, From e57b544d1a5995d0b61d182b04a8489277039218 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Fri, 31 Jan 2025 14:16:29 -0500 Subject: [PATCH 3/9] Merged Float and Int precision detection functions into one common path --- ndsl/dsl/dace/dace_config.py | 4 ++-- ndsl/dsl/typing.py | 39 +++++++----------------------------- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/ndsl/dsl/dace/dace_config.py b/ndsl/dsl/dace/dace_config.py index 5129dac8..27f17375 100644 --- a/ndsl/dsl/dace/dace_config.py +++ b/ndsl/dsl/dace/dace_config.py @@ -11,7 +11,7 @@ from ndsl.dsl.caches.cache_location import identify_code_path from ndsl.dsl.caches.codepath import FV3CodePath from ndsl.dsl.gt4py_utils import is_gpu_backend -from ndsl.dsl.typing import floating_point_precision +from ndsl.dsl.typing import get_precision from ndsl.optional_imports import cupy as cp @@ -264,7 +264,7 @@ def __init__( "compiler", "cuda", "syncdebug", value=dace_debug_env_var ) - if floating_point_precision() == 32: + if get_precision() == 32: # When using 32-bit float, we flip the default dtypes to be all # C, e.g. 32 bit. dace.Config.set( diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index fa3e692d..5c1f88b6 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -22,7 +22,7 @@ DTypes = Union[bool, np.bool_, int, np.int32, np.int64, float, np.float32, np.float64] -def floating_point_precision() -> int: +def get_precision() -> int: return int(os.getenv("PACE_FLOAT_PRECISION", "64")) @@ -31,43 +31,19 @@ def floating_point_precision() -> int: # common numpy type in the rest of the code. NDSL_32BIT_FLOAT_TYPE = np.float32 NDSL_64BIT_FLOAT_TYPE = np.float64 - - -def global_set_floating_point_precision(): - """Set the global floating point precision for all reference - to Float in the codebase. Defaults to 64 bit.""" - global Float - precision_in_bit = floating_point_precision() - if precision_in_bit == 64: - return NDSL_64BIT_FLOAT_TYPE - elif precision_in_bit == 32: - return NDSL_32BIT_FLOAT_TYPE - else: - NotImplementedError( - f"{precision_in_bit} bit precision not implemented or tested" - ) - - -def integer_precision() -> int: - return int(os.getenv("PACE_INT_PRECISION", "64")) - - -# We redefine the type as a way to distinguish -# the model definition of a float to other usage of the -# common numpy type in the rest of the code. NDSL_32BIT_INT_TYPE = np.int32 NDSL_64BIT_INT_TYPE = np.int64 -def global_set_integer_precision(): +def global_set_precision(): """Set the global floating point precision for all reference to Float in the codebase. Defaults to 64 bit.""" - global Int - precision_in_bit = integer_precision() + global Float, Int + precision_in_bit = get_precision() if precision_in_bit == 64: - return NDSL_64BIT_INT_TYPE + return NDSL_64BIT_FLOAT_TYPE, NDSL_64BIT_INT_TYPE elif precision_in_bit == 32: - return NDSL_32BIT_INT_TYPE + return NDSL_32BIT_FLOAT_TYPE, NDSL_32BIT_INT_TYPE else: NotImplementedError( f"{precision_in_bit} bit precision not implemented or tested" @@ -75,8 +51,7 @@ def global_set_integer_precision(): # Default float and int types -Float = global_set_floating_point_precision() -Int = global_set_integer_precision() +Float, Int = global_set_precision() Bool = np.bool_ FloatField = Field[gtscript.IJK, Float] From 9d57eae6b5d9d806dd2c8e873cd01ac3e9f9d449 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Tue, 4 Feb 2025 16:04:30 -0500 Subject: [PATCH 4/9] Re-added old function to fulfil a PACE dependency --- ndsl/dsl/typing.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index 5c1f88b6..46027c48 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -22,6 +22,11 @@ DTypes = Union[bool, np.bool_, int, np.int32, np.int64, float, np.float32, np.float64] +# Depreciated version of get_precision, but retained for a PACE dependency +def floating_point_precision() -> int: + return int(os.getenv("PACE_FLOAT_PRECISION", "64")) + + def get_precision() -> int: return int(os.getenv("PACE_FLOAT_PRECISION", "64")) From a221554aed08f88f2c7a133d5d6812455cf50141 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Tue, 4 Feb 2025 16:07:37 -0500 Subject: [PATCH 5/9] updated docstring --- ndsl/dsl/typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index 46027c48..b0a1d8cb 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -41,8 +41,8 @@ def get_precision() -> int: def global_set_precision(): - """Set the global floating point precision for all reference - to Float in the codebase. Defaults to 64 bit.""" + """Set the global precision for all references of + Float and Int in the codebase. Defaults to 64 bit.""" global Float, Int precision_in_bit = get_precision() if precision_in_bit == 64: From 150856f7a364f059d33a04ec1e62dc9591c519a0 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Tue, 11 Feb 2025 12:09:34 -0500 Subject: [PATCH 6/9] Added ability to declare 32 or 64 bit IntFields, overrulling the system precision --- ndsl/dsl/typing.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index b0a1d8cb..6994e915 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -74,9 +74,23 @@ def global_set_precision(): FloatFieldK = Field[gtscript.K, Float] FloatFieldK64 = Field[gtscript.K, np.float64] FloatFieldK32 = Field[gtscript.K, np.float32] + IntField = Field[gtscript.IJK, Int] +IntField64 = Field[gtscript.IJK, np.int64] +IntField32 = Field[gtscript.IJK, np.int32] +IntFieldI = Field[gtscript.I, Int] +IntFieldI64 = Field[gtscript.I, np.int64] +IntFieldI32 = Field[gtscript.I, np.int32] +IntFieldJ = Field[gtscript.J, Int] +IntFieldJ64 = Field[gtscript.J, np.int64] +IntFieldJ32 = Field[gtscript.J, np.int32] IntFieldIJ = Field[gtscript.IJ, Int] +IntFieldIJ64 = Field[gtscript.IJ, np.int64] +IntFieldIJ32 = Field[gtscript.IJ, np.int32] IntFieldK = Field[gtscript.K, Int] +IntFieldK64 = Field[gtscript.K, np.int64] +IntFieldK32 = Field[gtscript.K, np.int32] + BoolField = Field[gtscript.IJK, Bool] BoolFieldIJ = Field[gtscript.IJ, Bool] From 75eddb865a97d12cb376cde0b79e12d6f96e2054 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Tue, 11 Feb 2025 12:11:25 -0500 Subject: [PATCH 7/9] Added one dimensional bool fields --- ndsl/dsl/typing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index 6994e915..fa52db5f 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -92,6 +92,9 @@ def global_set_precision(): IntFieldK32 = Field[gtscript.K, np.int32] BoolField = Field[gtscript.IJK, Bool] +BoolFieldI = Field[gtscript.I, Bool] +BoolFieldJ = Field[gtscript.J, Bool] +BoolFieldK = Field[gtscript.K, Bool] BoolFieldIJ = Field[gtscript.IJ, Bool] Index3D = Tuple[int, int, int] From a7c03acead62cf0b93b8da27fa7ef37691e64933 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki <79879064+CharlesKrop@users.noreply.github.com> Date: Tue, 11 Feb 2025 12:13:30 -0500 Subject: [PATCH 8/9] Fix error message in typing.py Co-authored-by: Florian Deconinck --- ndsl/dsl/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index fa52db5f..5141a4c7 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -50,7 +50,7 @@ def global_set_precision(): elif precision_in_bit == 32: return NDSL_32BIT_FLOAT_TYPE, NDSL_32BIT_INT_TYPE else: - NotImplementedError( + raise NotImplementedError( f"{precision_in_bit} bit precision not implemented or tested" ) From d66bb6da201bff3ab0d83e6fcf52f1246b366ef3 Mon Sep 17 00:00:00 2001 From: Charles Kropiewnicki Date: Tue, 11 Feb 2025 12:16:24 -0500 Subject: [PATCH 9/9] output type for global_set_precision --- ndsl/dsl/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndsl/dsl/typing.py b/ndsl/dsl/typing.py index 5141a4c7..53c910dc 100644 --- a/ndsl/dsl/typing.py +++ b/ndsl/dsl/typing.py @@ -40,7 +40,7 @@ def get_precision() -> int: NDSL_64BIT_INT_TYPE = np.int64 -def global_set_precision(): +def global_set_precision() -> type: """Set the global precision for all references of Float and Int in the codebase. Defaults to 64 bit.""" global Float, Int