gt4py.cartesian.testing package

Submodules

gt4py.cartesian.testing.input_strategies module

class gt4py.cartesian.testing.input_strategies.SymbolKind(*values)[source]

Bases: Enum

FIELD = 5
GLOBAL_SET = 2
GLOBAL_STRATEGY = 1
NONE = 0
PARAMETER = 4
SINGLETON = 3
class gt4py.cartesian.testing.input_strategies._SymbolStrategy(kind: gt4py.cartesian.testing.input_strategies.SymbolKind, boundary: Sequence[Tuple[int, int]] | None, axes: str | None, data_dims: Tuple[int, ...] | None, value_st_factory: Callable[..., hypothesis.strategies.SearchStrategy])[source]

Bases: object

axes: str | None
boundary: Sequence[Tuple[int, int]] | None
data_dims: Tuple[int, ...] | None
kind: SymbolKind
value_st_factory: Callable[[...], SearchStrategy]
class gt4py.cartesian.testing.input_strategies._SymbolValueTuple(kind: str, boundary: Sequence[Tuple[int, int]], values: Tuple[Any])[source]

Bases: object

boundary: Sequence[Tuple[int, int]]
kind: str
values: Tuple[Any]
gt4py.cartesian.testing.input_strategies.composite_implementation_strategy_factory(dtypes, validation_strategy_factories, global_boundaries)[source]

Generate strategy for run-time StencilTestSuite parameters.

gt4py.cartesian.testing.input_strategies.composite_strategy_factory(dtypes, strategy_factories)[source]
gt4py.cartesian.testing.input_strategies.derived_shape_st(shape_st, extra: Sequence[int | None])[source]

Hypothesis strategy for extending shapes generated from a provided strategy with some extra padding.

If an element of extra contains None, the item will be dropped from the final shape, otherwise, both shape and extra elements are summed together.

gt4py.cartesian.testing.input_strategies.draw_from_strategies(strategies) → SearchStrategy[source]

Generate a Hypothesis strategy example by composing named strategies from a dictionary.

Parameters:
  • draw (function) – Hypothesis draw() implementation.

  • strategies (dict) – Named strategies. - name: Hypothesis strategy (function).

Returns:

args – A dictionary of Hypothesis examples. - name: Hypothesis strategy example (value).

Return type:

dict

gt4py.cartesian.testing.input_strategies.field(*, in_range, boundary=None, axes=None, data_dims=None, extent=None)[source]

Define a field symbol.

gt4py.cartesian.testing.input_strategies.global_name(*, singleton=None, symbol=None, one_of=None, in_range=None)[source]

Define a global symbol.

gt4py.cartesian.testing.input_strategies.ndarray_in_range_st(dtype, shape_st, value_range)[source]

Hypothesis strategy for ndarrays generated using the provided dtype and shape strategies and the value_range.

gt4py.cartesian.testing.input_strategies.ndarray_shape_st(sizes)[source]

Hypothesis strategy for shapes of ndims dimensions and size within size_range.

gt4py.cartesian.testing.input_strategies.ndarray_st(dtype, shape_strategy, value_st_factory)[source]

Hypothesis strategy for ndarrays generated using the provided dtype and shape and value_st_factory strategies/factories.

gt4py.cartesian.testing.input_strategies.none()[source]

Define the symbol None.

gt4py.cartesian.testing.input_strategies.one_of_values_st(args)[source]

Hypothesis strategy returning one of the values passed in the arguments.

gt4py.cartesian.testing.input_strategies.parameter(*, one_of=None, in_range=None)[source]

Define a parameter symbol.

gt4py.cartesian.testing.input_strategies.scalar_value_st(dtype, min_value, max_value, allow_nan=False)[source]

Hypothesis strategy for dtype scalar values in range [min_value, max_value].

gt4py.cartesian.testing.suites module

class gt4py.cartesian.testing.suites.StencilTestSuite[source]

Bases: object

Base class for every stencil test suite.

Every new test suite must inherit from this class and define proper attributes and methods to generate a valid set of testing strategies. For compatibility with pytest, suites must have names starting with ‘Test’

Supported and required class attributes are:

dtypes

Required class attribute. GlobalDecl suite dtypes dictionary. - label: list of dtype. If this value is a list, it will be converted to a dict with the default None key assigned to its value. It is meant to be populated with labels representing groups of symbols that should have the same type.

Example:

{
    "float_symbols": (np.float32, np.float64),
    "int_symbols": (int, np.int_, np.int64),
}
Type:

dict or list

domain_range

Required class attribute. CartesianSpace sizes for testing. Each item encodes the (min, max) range of sizes for every axis.

Type:

Sequence of pairs like ((int, int), (int, int) … )

symbols

Required class attribute. Definition of symbols (globals, parameters and inputs) used in this stencil. - name: utils._SymbolDescriptor. It is recommended to use the convenience functions global_name(), parameter() and field(). These functions have similar and self-explanatory arguments. Note that dtypes could be a list of actual dtypes but it is usually assumed to be a label from the global suite dtypes dictionary.

Type:

dict

definition

Required class attribute. Stencil definition function.

Type:

function

validation

Required class attribute. Stencil validation function. It should have exactly the same signature than arguments to access the actual values used in the current testing invocation. the definition function plus the extra _globals_, _domain_, and _origin_ It should always return a list of numpy.ndarray s, one per output, even if the function only defines one output value.

Type:

function

definition_strategies

Automatically generated. Hypothesis strategies for the stencil parameters used at definition (externals) - constant_name: Hypothesis strategy (strategy).

Type:

dict

validation_strategies

Automatically generated. Hypothesis strategies for the stencil parameters used at run-time (fields and parameters) - field_name: Hypothesis strategy (strategy). - parameter_name: Hypothesis strategy (strategy).

Type:

dict

ndims

Automatically generated. Constant of dimensions (1-3). If the name of class ends in [“1D”, “2D”, “3D”], this attribute needs to match the name or an assertion error will be raised.

Type:

int

global_boundaries

Automatically generated. Expected global boundaries for the input fields. - field_name: ‘list’ of ndim ‘tuple`s ((lower_boundary, upper_boundary)). Example (3D): [(1, 3), (2, 2), (0, 0)]

Type:

dict

classmethod _run_test_implementation(parameters_dict, implementation)[source]
_skip_ = True
classmethod _test_generation(test, externals_dict)[source]

Test source code generation for all backends and stencil suites.

The generated implementation is cached in the test context, to avoid duplication of (potentially expensive) compilation. Note: This caching introduces a dependency between tests, which is captured by an xdist_group marker in combination with –dist loadgroup to ensure safe parallel test execution.

classmethod _test_implementation(test, parameters_dict)[source]

Test computed values for implementations generated for all backends and stencil suites.

The generated implementation was cached in the test context, to avoid duplication of (potentially expensive) compilation. Note: This caching introduces a dependency between tests, which is captured by an xdist_group marker in combination with –dist loadgroup to ensure safe parallel test execution.

class gt4py.cartesian.testing.suites.SuiteMeta(cls_name, bases, cls_dict)[source]

Bases: type

Custom metaclass for all StencilTestSuite classes.

This metaclass generates a namespace-like class where all methods are static and adds new members with the required testing strategies and expected results to test a stencil definition and implementations using Hypothesis and pytest.

classmethod _validate_new_args(cls_name, cls_dict)[source]
collect_symbols(cls_dict)[source]
parametrize_generation_tests(cls_dict)[source]
parametrize_implementation_tests(cls_dict)[source]
required_members: Final[set[str]] = {'backends', 'definition', 'domain_range', 'dtypes', 'symbols', 'validation'}
gt4py.cartesian.testing.suites.unique_str()[source]

gt4py.cartesian.testing.utils module

gt4py.cartesian.testing.utils.annotate_function(function, dtypes)[source]
gt4py.cartesian.testing.utils.copy_func(f, name=None)[source]
gt4py.cartesian.testing.utils.standardize_dtype_dict(dtypes)[source]

Standardizes the dtype dict as it can be specified for the stencil test suites.

In the input dictionary, a selection of possible dtypes or just a single dtype can be specified for a set of fields or a single field. This function makes sure that all keys are tuples (by wrapping single field names and single dtypes as 1-tuples)

Module contents

class gt4py.cartesian.testing.StencilTestSuite[source]

Bases: object

Base class for every stencil test suite.

Every new test suite must inherit from this class and define proper attributes and methods to generate a valid set of testing strategies. For compatibility with pytest, suites must have names starting with ‘Test’

Supported and required class attributes are:

dtypes

Required class attribute. GlobalDecl suite dtypes dictionary. - label: list of dtype. If this value is a list, it will be converted to a dict with the default None key assigned to its value. It is meant to be populated with labels representing groups of symbols that should have the same type.

Example:

{
    "float_symbols": (np.float32, np.float64),
    "int_symbols": (int, np.int_, np.int64),
}
Type:

dict or list

domain_range

Required class attribute. CartesianSpace sizes for testing. Each item encodes the (min, max) range of sizes for every axis.

Type:

Sequence of pairs like ((int, int), (int, int) … )

symbols

Required class attribute. Definition of symbols (globals, parameters and inputs) used in this stencil. - name: utils._SymbolDescriptor. It is recommended to use the convenience functions global_name(), parameter() and field(). These functions have similar and self-explanatory arguments. Note that dtypes could be a list of actual dtypes but it is usually assumed to be a label from the global suite dtypes dictionary.

Type:

dict

definition

Required class attribute. Stencil definition function.

Type:

function

validation

Required class attribute. Stencil validation function. It should have exactly the same signature than arguments to access the actual values used in the current testing invocation. the definition function plus the extra _globals_, _domain_, and _origin_ It should always return a list of numpy.ndarray s, one per output, even if the function only defines one output value.

Type:

function

definition_strategies

Automatically generated. Hypothesis strategies for the stencil parameters used at definition (externals) - constant_name: Hypothesis strategy (strategy).

Type:

dict

validation_strategies

Automatically generated. Hypothesis strategies for the stencil parameters used at run-time (fields and parameters) - field_name: Hypothesis strategy (strategy). - parameter_name: Hypothesis strategy (strategy).

Type:

dict

ndims

Automatically generated. Constant of dimensions (1-3). If the name of class ends in [“1D”, “2D”, “3D”], this attribute needs to match the name or an assertion error will be raised.

Type:

int

global_boundaries

Automatically generated. Expected global boundaries for the input fields. - field_name: ‘list’ of ndim ‘tuple`s ((lower_boundary, upper_boundary)). Example (3D): [(1, 3), (2, 2), (0, 0)]

Type:

dict

classmethod _run_test_implementation(parameters_dict, implementation)[source]
_skip_ = True
classmethod _test_generation(test, externals_dict)[source]

Test source code generation for all backends and stencil suites.

The generated implementation is cached in the test context, to avoid duplication of (potentially expensive) compilation. Note: This caching introduces a dependency between tests, which is captured by an xdist_group marker in combination with –dist loadgroup to ensure safe parallel test execution.

classmethod _test_implementation(test, parameters_dict)[source]

Test computed values for implementations generated for all backends and stencil suites.

The generated implementation was cached in the test context, to avoid duplication of (potentially expensive) compilation. Note: This caching introduces a dependency between tests, which is captured by an xdist_group marker in combination with –dist loadgroup to ensure safe parallel test execution.

gt4py.cartesian.testing.field(*, in_range, boundary=None, axes=None, data_dims=None, extent=None)[source]

Define a field symbol.

gt4py.cartesian.testing.global_name(*, singleton=None, symbol=None, one_of=None, in_range=None)[source]

Define a global symbol.

gt4py.cartesian.testing.none()[source]

Define the symbol None.

gt4py.cartesian.testing.parameter(*, one_of=None, in_range=None)[source]

Define a parameter symbol.