gt4py.cartesian.testing package

Submodules

gt4py.cartesian.testing.input_strategies module

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

Bases: enum.Enum

An enumeration.

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: Optional[Sequence[Tuple[int, int]]], axes: Optional[str], data_dims: Optional[Tuple[int, ...]], value_st_factory: Callable[..., hypothesis.strategies.SearchStrategy])[source]

Bases: object

axes: Optional[str]
boundary: Optional[Sequence[Tuple[int, int]]]
data_dims: Optional[Tuple[int, ...]]
kind: gt4py.cartesian.testing.input_strategies.SymbolKind
value_st_factory: Callable[[...], hypothesis.strategies.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[Optional[int]])[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) hypothesis.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 _test_generation(test, externals_dict)[source]

Test source code generation for all backends and stencil suites.

The generated implementations are cached in a utils.ImplementationsDB instance, to avoid duplication of (potentially expensive) compilations.

classmethod _test_implementation(test, parameters_dict)[source]

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

The generated implementations are reused from previous tests by means of a utils.ImplementationsDB instance shared at module scope.

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.

collect_symbols(cls_dict)[source]
parametrize_generation_tests(cls_dict)[source]
parametrize_implementation_tests(cls_dict)[source]
required_members = {'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 _test_generation(test, externals_dict)[source]

Test source code generation for all backends and stencil suites.

The generated implementations are cached in a utils.ImplementationsDB instance, to avoid duplication of (potentially expensive) compilations.

classmethod _test_implementation(test, parameters_dict)[source]

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

The generated implementations are reused from previous tests by means of a utils.ImplementationsDB instance shared at module scope.

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.