Repository navigation
feat[next]: embedded accepts 0-d arrays as scalars (traced scalars under jax.jit / torch.compile) - #3030
Conversation
…arguments work under jax.jit and torch.compile
|
🤖 Written by an agent on behalf of @havogt |
| if core_defs.is_scalar_type(lhs) and core_defs.is_scalar_type(rhs): | ||
| return getattr(np, name)(lhs, rhs) | ||
| array = cast(core_defs.NDArrayObject, rhs if core_defs.is_scalar_type(lhs) else lhs) | ||
| ops = array_ops.of(array) | ||
| return ops.elementwise_function(name)(ops.operand(lhs, array), ops.operand(rhs, array)) |
There was a problem hiding this comment.
this looks a bit patchy, why does dispatching via the normal field not work?
There was a problem hiding this comment.
Couldn't we follow the same approach as suggested above for the unary functions: convert scalar arguments to numpy scalars and dispatch based on that?
There was a problem hiding this comment.
These are the scalar defaults. Under jit, a traced scalar is a 0-d array, not a field, so field dispatch does not apply to it. The defaults treat it as the scalar it stands for.
On the suggestion above: done in 62aa8d5. Every scalar goes through ArrayOps. A 0-d array goes through its library's; a Python or NumPy scalar goes through NumPy's. The binary case converts the other operand with operand(…, like=array).
🤖 Written by an agent on behalf of @havogt
| ) | ||
| from .constructors import FieldConstructor, as_connectivity, as_field, empty, full, ones, zeros | ||
| from .embedded import ( # Just for registering field implementations | ||
| array_libraries as _array_libraries, |
There was a problem hiding this comment.
nd_array_field and array_libraries import ffront.fbuiltins, which in this PR needs array_ops at module level. So embedded/__init__ no longer imports those two, and gt4py.next imports them for their registrations (the comment there says so now). #3033 removes the reason: fbuiltins then imports nothing from embedded, and this goes back to what main has.
🤖 Written by an agent on behalf of @havogt
…_or_0d_array, scan accumulator typed without an ignore
…rray library, torch setitem and scan under a trace
…entation, as 0-d fields
…ere and astype of 0-d arrays, weak typing through builtins
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
⚙️ Agent run details
|
egparedes
left a comment
There was a problem hiding this comment.
I have some questions and comments.
| pos: Sequence[common.NamedIndex], | ||
| target: xtyping.MaybeNestedInTuple[common.MutableField], | ||
| source: xtyping.MaybeNestedInTuple[core_defs.Scalar], | ||
| source: xtyping.MaybeNestedInTuple[core_defs.Scalar | core_defs.NDArrayObject], |
There was a problem hiding this comment.
Would it make sense to create an alias for embedded related modules for core_defs.Scalar | core_defs.NDArrayObject? It is probably used in several places
| return ops | ||
|
|
||
|
|
||
| def is_scalar_or_0d_array(value: Any) -> bool: |
There was a problem hiding this comment.
Maybe call it is_scalar_like()?
There was a problem hiding this comment.
Renamed to is_scalar_like, a TypeIs[ScalarLike]. The check for a 0-d array alone is is_0d_array.
🤖 Written by an agent on behalf of @havogt
| # default implementation for scalars, Fields are handled via dispatch | ||
| assert array_ops.is_scalar_or_0d_array(value) | ||
| if core_defs.is_scalar_type(value): | ||
| return cast(common.Field | core_defs.ScalarT, _math_builtin(value)) |
There was a problem hiding this comment.
Can't this case be implemented as a preliminary conversion to numpy scalar (or 0D array) and then just dispatch to the array_ops? We could then drop the implementation maps for functions which exist in this file.
There was a problem hiding this comment.
Done in 62aa8d5. Python and NumPy scalars go to NumPy's ArrayOps, and 0-d arrays to their library's. The np/math maps are gone from fbuiltins. It is also faster: sqrt(np.float64) takes 0.59 µs instead of 1.92 µs.
One result type changes: abs/neg of a Python int now return a NumPy int (pinned in ca92357). NumPy scalar types are kept. Without SciPy, NumPy's gamma fallback now returns a scalar for a scalar.
🤖 Written by an agent on behalf of @havogt
| if core_defs.is_scalar_type(lhs) and core_defs.is_scalar_type(rhs): | ||
| return getattr(np, name)(lhs, rhs) | ||
| array = cast(core_defs.NDArrayObject, rhs if core_defs.is_scalar_type(lhs) else lhs) | ||
| ops = array_ops.of(array) | ||
| return ops.elementwise_function(name)(ops.operand(lhs, array), ops.operand(rhs, array)) |
There was a problem hiding this comment.
Couldn't we follow the same approach as suggested above for the unary functions: convert scalar arguments to numpy scalars and dispatch based on that?
| # not `nd_array_field` and `array_libraries`: they import `ffront.fbuiltins`, which imports | ||
| # this package | ||
| from . import array_ops, common, context, exceptions |
There was a problem hiding this comment.
Question: is it really needed to import these modules here? They are only used for reexporting them, right? Is that a requirement of coding conventions? If not, why is this done here?
There was a problem hiding this comment.
Not a convention. They make gt4py.next.embedded.context etc. reliable as attributes (next_embedded.context in the decorator, gtx_embedded.context in context.py). What this PR changes is that nd_array_field/array_libraries are no longer imported here; see the reply on next/__init__.py.
🤖 Written by an agent on behalf of @havogt
There was a problem hiding this comment.
Can we avoid all these important?
…ed accepts as a scalar (review)
…uiltin defaults again, not the field implementation
…re with 0-d array branches
…, NumPy's for Python and NumPy scalars (review)
…es keep the NumPy scalar type
| #: What embedded accepts where it accepts a scalar: a scalar, or a 0-d array (`is_scalar_like`) | ||
| ScalarLike: TypeAlias = core_defs.Scalar | core_defs.NDArrayObject | ||
|
|
||
|
|
||
| def is_scalar_like(value: Any) -> TypeIs[ScalarLike]: | ||
| """A scalar, or a 0-d array.""" | ||
| return core_defs.is_scalar_type(value) or is_0d_array(value) | ||
|
|
There was a problem hiding this comment.
scalar_like makes sense for the function name and for the case where the NDArrayObject represents a 0-D array, but not for the general case where scalars or ndarray objects are allowed. For this case, I think we should use a more general name like TensorLike, or NDScalar.
…r and context import what they use (review of GridTools#3030)
Under
jax.jitortorch.compile, a scalar argument is a traced 0-d array. Embedded now accepts 0-d arrays wherever it accepts scalars.array_ops.is_scalar_like: a scalar or a 0-d array. It is used for field operands,__setitem__and the scan carry.ArrayOps: a 0-d array's own, or NumPy's for Python/NumPy scalars, which replaces thenp/mathmaps infbuiltins.abs/negof a Python int now return a NumPy int.ArrayOps.scalar(used byas_scalar) keeps a traced element as a 0-d array under jax and torch.embedded/__init__.pyno longer importsnd_array_fieldandarray_libraries(gt4py.nextdoes), which avoids an import cycle withfbuiltins.