Field operators

A field operator is a Python function decorated with @gtx.field_operator. Its body is written in a subset of Python where values are whole fields: there are no loops over grid points and no indices. This page describes that subset.

import numpy as np

import gt4py.next as gtx

I = gtx.Dimension("I")
J = gtx.Dimension("J")

The examples use these fields:

domain = gtx.domain({I: 4, J: 3})

a = gtx.as_field(domain, np.arange(12.0).reshape(4, 3))
b = gtx.full(domain, 10.0)
result = gtx.zeros(domain)

print(a.asnumpy())
[[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]
 [ 9. 10. 11.]]

Anatomy of a field operator

A field operator takes fields and scalars, and returns fields:

IJField = gtx.Field[gtx.Dims[I, J], gtx.float64]


@gtx.field_operator
def weighted_mean(a: IJField, b: IJField, weight: gtx.float64) -> IJField:
    return weight * a + (1.0 - weight) * b


weighted_mean(a, b, 0.25, out=result)
print(result.asnumpy())
[[ 7.5   7.75  8.  ]
 [ 8.25  8.5   8.75]
 [ 9.    9.25  9.5 ]
 [ 9.75 10.   10.25]]
  • All the parameters must have a type annotation. The annotation of the return value is optional, and when present it is checked against the type of the returned expression.

  • A field operator is a pure function: it does not modify its arguments, and the result only depends on them.

  • The function is analyzed when it is decorated, so mistakes in the definition are reported at that point, before running anything.

Note

The built-in functions and data types used in the body of an operator must be imported by name, as in from gt4py.next import where, astype, float32. Calling them through the module (gtx.where(...)) is not supported inside operators. We are working on removing this restriction, which will go away soon. Types in annotations can already be written either way.

Types

Fields

The type of a field is gtx.Field[gtx.Dims[...], dtype], with its dimensions and the data type of its values.

Tip

Give names to the field types used in a model, as done with IJField above. Signatures become shorter and easier to read.

Scalars

Scalar parameters are annotated with one of the data types:

Kind

Types

Boolean

bool

Signed integers

gtx.int8, gtx.int16, gtx.int32, gtx.int64, int (same as int64)

Unsigned

gtx.uint8, gtx.uint16, gtx.uint32, gtx.uint64

Floating point

gtx.float32, gtx.float64, float (same as float64)

A floating-point number written in the code (2.0, 1e-3) has type float64. An integer has type int32, or int64 if it does not fit.

No implicit conversions

GT4Py never converts between data types on its own. Both sides of an operation must have the same data type, which means that:

  • With float64 fields, write 2.0 * a, not 2 * a.

  • With float32 fields, write float32(2.0) * a.

  • With int64 values, including the parameters annotated with int, write int64(1).

  • To combine values of different types, convert one of them with astype.

from gt4py.next import astype, float32, float64


@gtx.field_operator
def add_single_precision(
    a: IJField, b: gtx.Field[gtx.Dims[I, J], gtx.float32]
) -> IJField:
    return a + astype(float32(2.0) * b, float64)


b_single = gtx.full(domain, 10.0, dtype=np.float32)
add_single_precision(a, b_single, out=result)
print(result.asnumpy())
[[20. 21. 22.]
 [23. 24. 25.]
 [26. 27. 28.]
 [29. 30. 31.]]

The conversions are explicit so that the precision of every operation can be read directly from the code. This matters for models running in single or mixed precision.

Tuples

Tuples of fields or scalars can be used as arguments, as local values and as results. They are annotated with tuple[...]:

@gtx.field_operator
def sum_and_difference(a: IJField, b: IJField) -> tuple[IJField, IJField]:
    return a + b, a - b


@gtx.field_operator
def product(a: IJField, b: IJField) -> IJField:
    total, difference = sum_and_difference(a, b)
    return total * difference


product(a, b, out=result)
print(result.asnumpy())
[[-100.  -99.  -96.]
 [ -91.  -84.  -75.]
 [ -64.  -51.  -36.]
 [ -19.    0.   21.]]

This example also shows that a field operator can call other field operators.

A tuple can also be an argument. Its members are unpacked, or accessed with an index like vector[0], and the operator is called with a regular Python tuple:

@gtx.field_operator
def squared_norm(vector: tuple[IJField, IJField]) -> IJField:
    u, v = vector
    return u * u + v * v


squared_norm((a, b), out=result)
print(result.asnumpy())
[[100. 101. 104.]
 [109. 116. 125.]
 [136. 149. 164.]
 [181. 200. 221.]]

Tip

Build large computations out of small operators. The compiled backends inline the operators called by another one and optimize the result as a whole.

Expressions

Operation

Syntax

Arithmetic

+, -, *, /, //, %, **, unary -

Comparison

==, !=, <, <=, >, >= (the result is a field of bool)

Logical

& (and), | (or), ^ (xor), ~ (not)

Premap

field(I + 1), field(E2C[0]) (see Premap and neighbors)

Operations between fields and scalars apply to every point of the field. When the fields have different dimensions, the result has all of them: adding a field over K to a field over I, J gives a field over I, J, K.

% is only defined for integers; use fmod for floating point values.

Important

Use & and | to combine conditions, and put each comparison between parentheses: (a > 0.0) & (a < 1.0). The Python keywords and and or, and chained comparisons like 0.0 < a < 1.0, are not supported, not even for scalars. Use ~ instead of not for fields.

Built-in functions

These are the built-in functions, all of them imported from gt4py.next:

Functions

Description

abs, neg, sqrt, cbrt, exp, log, gamma, power, fmod

Elementary functions

sin, cos, tan, arcsin, arccos, arctan

Trigonometric functions

sinh, cosh, tanh, arcsinh, arccosh, arctanh

Hyperbolic functions

floor, ceil, trunc

Rounding

minimum, maximum

Minimum or maximum of two values

isfinite, isinf, isnan

Checks on floating point values

where(mask, a, b)

a where mask is true, b elsewhere

astype(value, dtype)

Conversion to another data type

broadcast(value, (I, J))

Extension of a value to more dimensions

neighbor_sum, min_over, max_over

Reductions over neighbors

from gt4py.next import maximum, sqrt, where


@gtx.field_operator
def clipped_root(a: IJField, threshold: gtx.float64) -> IJField:
    return where(a > threshold, sqrt(maximum(a - threshold, 0.0)), 0.0)


clipped_root(a, 2.0, out=result)
print(result.asnumpy())
[[0.         0.         0.        ]
 [1.         1.41421356 1.73205081]
 [2.         2.23606798 2.44948974]
 [2.64575131 2.82842712 3.        ]]

Local variables and conditionals

Intermediate results can be stored in local variables, which helps to write long expressions in readable steps:

from gt4py.next import abs  # the Python built-in `abs` is not accepted inside operators


@gtx.field_operator
def relative_difference(a: IJField, b: IJField) -> IJField:
    difference = a - b
    scale = maximum(abs(a), abs(b))
    return difference / scale


relative_difference(a, b, out=result)
print(result.asnumpy())
[[-1.         -0.9        -0.8       ]
 [-0.7        -0.6        -0.5       ]
 [-0.4        -0.3        -0.2       ]
 [-0.1         0.          0.09090909]]

There are two ways to choose between values, for two different situations:

  • where(mask, a, b) selects point by point with a field of bool. Both a and b are computed at all the points.

  • if statements and the x if condition else y expression take a scalar bool condition, so they select the computation for the whole field. They are meant for configuration options.

@gtx.field_operator
def limit(a: IJField, upper: gtx.float64, enabled: bool) -> IJField:
    if enabled:
        limited = where(a > upper, upper, a)
    else:
        limited = a
    return limited


limit(a, 6.0, True, out=result)
print(result.asnumpy())
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 6. 6.]
 [6. 6. 6.]]

A variable assigned in the two branches of an if must have the same type in both of them.

Values from the enclosing scope

Operators can refer to objects defined outside them: dimensions, other operators and constants. Scalar constants are grouped in an Enum class whose members also subclass a numeric type, so they behave like plain numbers in arithmetic:

import enum


class Constants(np.float64, enum.Enum):
    GRAVITY = 9.80665
    EARTH_RADIUS = 6.371e6


@gtx.field_operator
def geopotential(height: IJField) -> IJField:
    return Constants.GRAVITY * height


geopotential(a, out=result)
print(result.asnumpy())
[[  0.        9.80665  19.6133 ]
 [ 29.41995  39.2266   49.03325]
 [ 58.8399   68.64655  78.4532 ]
 [ 88.25985  98.0665  107.87315]]

Important

The compiled backends inline the values of the constants in the generated code when the operator is compiled. This is why they have to be immutable: a plain global variable (GRAVITY = 9.80665) is only accepted in embedded execution, and the compiled backends reject it.

Tip

Use this only for true constants. A value which may change during the execution of the application must be passed as an argument.

Named collections

Models usually handle groups of related fields, like the components of the wind or the state of the atmosphere. Instead of long lists of arguments or anonymous tuples, operators can take and return named collections: a typing.NamedTuple, or a dataclass, whose members are fields, scalars or other collections.

import typing


class Wind(typing.NamedTuple):
    u: IJField
    v: IJField


@gtx.field_operator
def kinetic_energy(wind: Wind) -> IJField:
    return 0.5 * (wind.u * wind.u + wind.v * wind.v)


@gtx.field_operator
def scaled(wind: Wind, factor: gtx.float64) -> Wind:
    return Wind(u=factor * wind.u, v=factor * wind.v)


wind = Wind(u=a, v=b)

kinetic_energy(wind, out=result)
print(result.asnumpy())
[[ 50.   50.5  52. ]
 [ 54.5  58.   62.5]
 [ 68.   74.5  82. ]
 [ 90.5 100.  110.5]]

When the result is a collection, out is a collection of the same type:

half_wind = Wind(u=gtx.zeros(domain), v=gtx.zeros(domain))

scaled(wind, 0.5, out=half_wind)
print(half_wind.u.asnumpy())
[[0.  0.5 1. ]
 [1.5 2.  2.5]
 [3.  3.5 4. ]
 [4.5 5.  5.5]]

Dataclasses work in the same way:

import dataclasses


@dataclasses.dataclass
class State:
    temperature: IJField
    pressure: IJField


@gtx.field_operator
def heated(state: State, increment: gtx.float64) -> State:
    return State(temperature=state.temperature + increment, pressure=state.pressure)


new_state = State(temperature=gtx.zeros(domain), pressure=gtx.zeros(domain))

heated(State(temperature=a, pressure=b), 0.5, out=new_state)
print(new_state.temperature.asnumpy())
[[ 0.5  1.5  2.5]
 [ 3.5  4.5  5.5]
 [ 6.5  7.5  8.5]
 [ 9.5 10.5 11.5]]

The rules are:

  • The class must be defined at module level, not inside a function.

  • Dataclasses are accepted when all their members are set by the constructor and none of them has a default value.

Note

Support for other kinds of static containers (pytrees) will probably be added in the future.

Unsupported Python, and what to use instead

The body of an operator is not arbitrary Python. These are the most common constructs which are rejected, and what to use instead:

Not supported

Use instead

for and while loops

Field expressions, or a scan operator

and, or, chained comparisons

&, | with parentheses

if with a field as condition

where or concat_where

Indexing a field, a[i, j]

Premap, a(I + 1)

Mixing data types, 2 * a

Literals of the right type (2.0 * a) or astype

Modifying an argument in place

Return a new field

lambda, comprehensions, try, with

A separate field operator, or regular Python code outside of it

Calling NumPy or other Python functions

The built-in functions, or compute the value before the call

In all these cases the error is reported when the operator is defined, before anything runs.