Skip to content

fix[next]: scalar dtype in math builtins + implement embedded gamma (#2277)#2658

Open
havogt wants to merge 2 commits into
GridTools:mainfrom
havogt:fix-next-math-builtin-scalar-type
Open

fix[next]: scalar dtype in math builtins + implement embedded gamma (#2277)#2658
havogt wants to merge 2 commits into
GridTools:mainfrom
havogt:fix-next-math-builtin-scalar-type

Conversation

@havogt

@havogt havogt commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Two related changes to gt4py.next math builtins, both centered on gamma and scalar dtype.

  1. Preserve scalar dtype in unary math builtins (fixes gtx.sqrt result type on scalars is Python float by default #2277)

Unary math builtins on scalars (gtx.sqrt, gtx.sin, ...) returned a Python float/int/bool instead of preserving the input scalar type, e.g. type(gtx.sqrt(gtx.float32(4))) was float instead of numpy.float32. The scalar fallback in ffront/fbuiltins.py used Python's math module, which widens to a Python scalar. It now uses the numpy equivalents, which keep the input dtype, matching the field path (nd_array_field.py) and the iterator-embedded path (iterator/embedded.py). gamma has no numpy equivalent, so it uses scipy.special.gamma when available (as in gt4py.cartesian), falling back to math.gamma with a cast back to the input scalar type.

  1. Implement gamma for embedded fields (numpy/cupy/jax)

gtx.gamma on an embedded field previously raised AssertionError on every backend: the field path had a '# TODO gamma' and skipped registration because numpy has no gamma ufunc. It is now registered via a per-namespace resolver: scipy.special.gamma (numpy), cupyx.scipy.special.gamma (cupy), jax.scipy.special.gamma (jax), with a np.vectorize(math.gamma) fallback when scipy is absent. _make_builtin was extended to accept a namespace-to-op resolver.

No new dependencies: scipy is already pulled in by gt4py[next] via the standard extra; cupyx.scipy and jax.scipy ship with cupy and jax.

Tests

  • New backend-free unit test for scalar dtype preservation across all unary math builtins (float32/float64) and bool for the predicates.
  • New gamma field-operator test exercising the full backend matrix; verified locally green on embedded numpy, embedded cupy (GPU), embedded jax, gtfn (cpu+gpu), dace (cpu+gpu), and roundtrip.

Fixes #2277.

Unary math builtins (sqrt, sin, ...) on a scalar used Python's math module, which widens the result to a Python float/int/bool. Switch the scalar fallback to the numpy equivalents, which keep the input scalar dtype, matching the field and iterator-embedded paths. gamma uses scipy.special.gamma when available (as in cartesian), falling back to math.gamma.
@havogt havogt requested a review from egparedes June 16, 2026 07:12
gtx.gamma on an embedded field raised AssertionError on every backend; the field path had a '# TODO gamma' and skipped registration because numpy has no gamma ufunc. Register it via a per-namespace resolver: scipy.special.gamma (numpy), cupyx.scipy.special.gamma (cupy), jax.scipy.special.gamma (jax), with a np.vectorize(math.gamma) fallback when scipy is absent. Covered across the backend matrix in test_math_unary_builtins.
@havogt havogt changed the title fix[next]: preserve scalar dtype in math builtins (#2277) fix[next]: scalar dtype in math builtins + implement embedded gamma (#2277) Jun 16, 2026
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.

gtx.sqrt result type on scalars is Python float by default

1 participant