fix[next]: scalar dtype in math builtins + implement embedded gamma (#2277)#2658
Open
havogt wants to merge 2 commits into
Open
fix[next]: scalar dtype in math builtins + implement embedded gamma (#2277)#2658havogt wants to merge 2 commits into
havogt wants to merge 2 commits into
Conversation
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related changes to gt4py.next math builtins, both centered on gamma and scalar dtype.
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.
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
Fixes #2277.