Core ops¶
anytensor.core
¶
Ordinary tensor ops via array-api-compat (stable, input-adaptive).
promote_options
¶
Temporarily set NumPy-upcast defaults (copy / non-ref fallback).
Example::
with at.promote_options(copy=True):
y = at.maximum(torch_x, numpy_y) # safe if numpy_y will be mutated
Source code in anytensor/core.py
align_arrays
¶
Align operands on one namespace; upcast scalars/NumPy to non-NumPy peers.
None entries are preserved (useful when optional operands share a call).
Source code in anytensor/core.py
as_array_result
¶
Decorator: promote scalar returns to 0-d arrays on the call's namespace.
Reductions (and any op) may hand back np.float64 / Python scalars;
callers need a real array (.shape, .dtype, methods).
Source code in anytensor/core.py
promote
¶
Decorator: namespace upcast + per-operand dtype policy.
Pass keyword roles for each parameter::
@promote(x="data", y="data")
def maximum(x: ArrayT, y: ArrayT) -> ArrayT: ...
@promote(x="data", indices="index")
def take(x, indices, axis=0): ...
@promote(condition="mask", x="data", y="data")
def where(condition: ArrayT, x: ArrayT, y: ArrayT) -> ArrayT: ...
@promote(x="data", segment_ids="index", num_segments="shape")
def segment_sum(x, segment_ids, num_segments): ...
data operands share Array API result_type (so a NumPy int beside a
float tensor becomes float). index stays integral (width is
backend-local: Torch may cast to int64; JAX/TF often keep int32). mask
becomes bool. shape is a size dim (Python int / symbolic / 0-d integral
tensor) and is not promoted to a 0-d array.
copy / fallback control NumPy→framework buffer sharing (see
:func:promote_options).
Source code in anytensor/core.py
promote_scalars
¶
Upcast named operands as data (namespace + result_type).
Prefer :func:promote when some args are indices/masks. Kept as a short
form of @promote(x="data", y="data").
Source code in anytensor/core.py
exp
¶
log
¶
sum
¶
Sum over axes; full reduce returns a 0-d array (not a scalar).
min
¶
Minimum over axes; full reduce returns a 0-d array (not a scalar).
Notes
Length-0 reductions are framework-defined (often error) — prefer
nonempty. Under TF XLA (jit_compile=True), NaN inputs may yield
±inf instead of NaN; eager TF / NumPy / JAX usually keep NaN.
Source code in anytensor/core.py
max
¶
Maximum over axes; full reduce returns a 0-d array (not a scalar).
Notes
Length-0 reductions are framework-defined (often error) — prefer
nonempty. Under TF XLA, NaN inputs may yield ±inf instead of NaN.
Source code in anytensor/core.py
mean
¶
Mean over axes; full reduce returns a 0-d array (not a scalar).
Notes
Empty full-reduce (x.size == 0) returns a 0-d NaN on the backend
dtype to avoid NumPy's Mean of empty slice warning path.
Source code in anytensor/core.py
prod
¶
Product over axes; full reduce returns a 0-d array (not a scalar).
Notes
inf * a subnormal or float32-min value may be inf (NumPy /
eager TF) or nan (JAX / TF XLA) when the tiny flushes to 0. Not
standardized — keep finite samples away from the underflow edge if
you need portable results.
Source code in anytensor/core.py
cumsum
¶
Cumulative sum along axis (default 0, never flatten).
shape
¶
Return the shape of x as a tuple.
Under tracing (tf.function, jax.jit, torch.compile), unknown
dims are backend size tensors / symbols rather than Python None, so
callers can build matching outputs under jit/compile.
Notes
Prefer this over raw x.shape when feeding zeros / full /
*_like under polymorphic TF graphs — tnp.zeros errors on
TensorShape(None,) after retracing.
Source code in anytensor/core.py
take
¶
Take elements from x along axis (default 0).
Source code in anytensor/core.py
reshape
¶
transpose
¶
Permute axes of x.
Source code in anytensor/core.py
concatenate
¶
Concatenate a sequence of arrays along axis.
split
¶
Split x along axis (NumPy split semantics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ShapedArray
|
Array to split. |
required |
indices_or_sections
|
Any
|
Either an |
required |
axis
|
int
|
Axis along which to split. |
0
|
Returns:
| Type | Description |
|---|---|
list
|
A |
Notes
Dispatches via :func:anytensor.backends.get_backend so Torch uses
tensor_split (index cuts) rather than size-based torch.split.
Source code in anytensor/core.py
stack
¶
maximum
¶
Element-wise maximum. Scalars/NumPy upcast; dtypes via result_type.
Notes
Under TF XLA (jit_compile=True), NaN inputs may yield ±inf
instead of NaN; eager TF / NumPy / JAX usually keep NaN. Not
standardized across backends.
Source code in anytensor/core.py
minimum
¶
Element-wise minimum. Scalars/NumPy upcast; dtypes via result_type.
Notes
Under TF XLA, NaN inputs may yield ±inf instead of NaN. Not
standardized across backends.
Source code in anytensor/core.py
sqrt
¶
rsqrt
¶
Element-wise reciprocal square root (1 / sqrt(x)).
where
¶
Choose from x or y by condition. Scalars/NumPy upcast.
Source code in anytensor/core.py
clip
¶
Clip values to [min, max].
Implemented with :func:maximum / :func:minimum so TF tracing does not
emit BroadcastArgs (tf2onnx cannot lower that op).
Source code in anytensor/core.py
astype
¶
cast
¶
zeros_like
¶
Return zeros with the same shape (and backend) as x.
Built from :func:shape so polymorphic tf.function sees symbolic
sizes instead of None dims (raw tnp.zeros_like / tnp.zeros can
fail after retracing).
Source code in anytensor/core.py
ones_like
¶
Return ones with the same shape (and backend) as x.
Uses :func:shape for graph-safe sizes under tf.function (see
:func:zeros_like).
Source code in anytensor/core.py
full_like
¶
Return an array filled with fill_value matching x.
Uses :func:shape for graph-safe sizes under tf.function (see
:func:zeros_like).
Source code in anytensor/core.py
zeros
¶
Return zeros; pass like= to select the backend.
Source code in anytensor/core.py
ones
¶
Return ones; pass like= to select the backend.
Source code in anytensor/core.py
full
¶
Return a filled array; pass like= to select the backend.
Source code in anytensor/core.py
arange
¶
Evenly spaced values; pass like= to select the backend.
device is forwarded when supported (e.g. Torch).
Source code in anytensor/core.py
repeat
¶
Repeat elements of x.
Similar to NumPy / JAX repeat. When total_repeat_length is set with
per-element repeats, matches JAX jnp.repeat(..., total_repeat_length=)
(pad or slice the leading axis to that length).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ShapedArray
|
Input array. |
required |
repeats
|
Any
|
Python |
required |
total_repeat_length
|
Optional[ShapeSize]
|
Optional shape-size (Python |
None
|
axis
|
Optional[int]
|
Axis to repeat along; |
None
|
Returns:
| Type | Description |
|---|---|
ShapedArray
|
Array with repeated elements on the same backend as |
Notes
Under jax.jit, jnp.repeat needs static repeat counts or a
static total_repeat_length. :func:~anytensor.partition_softmax
always passes total_length as that length. Omitting
total_repeat_length on :func:repeat itself is fine eagerly and
on TensorFlow.
Source code in anytensor/core.py
matmul
¶
Matrix product of two arrays. NumPy operands upcast onto peers.
inf
¶
ninf
¶
nan
¶
pi
¶
e
¶
finfo
¶
iinfo
¶
dtype
¶
is_nan
¶
is_finite
¶
is_inf
¶
fill_nan
¶
Replace NaNs in x with value (broadcasts). Leaves ±inf unchanged.
Source code in anytensor/core.py
fill_nan_mask
¶
Return (filled, mask): NaNs replaced, plus a boolean NaN mask.
mask is True where x was NaN (same polarity as :func:is_nan /
NumPy masked-array invalid). Boolean, not 0/1 — cast if you need weights.
Equivalent to (fill_nan(x, value), is_nan(x)); not ~is_finite
(±inf is non-NaN).
Source code in anytensor/core.py
nan_to_num
¶
Replace NaN and ±inf (Array API nan_to_num).
Defaults: NaN → nan (0.0); posinf / neginf None → large
finite values from the dtype's finfo (framework-dependent).
Source code in anytensor/core.py
equal_nan
¶
Element-wise equality treating NaN as equal to NaN.
Returns a boolean array: (x == y) | (isnan(x) & isnan(y)).
Non-NaN values compare with ordinary == (so +inf == +inf).