For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Python module
max.experimental.functional
Distributed functional ops with explicit per-op SPMD dispatch.
Usage:
from max.experimental import functional as F
y = F.matmul(a, b)
z = F.add(x, y)
w = F.transfer_to(z, new_mapping)Layout:
spmd_ops–per_shard_dispatchengine and per-op functions.collective_ops– collectives andtransfer_to.creation_ops–full/ones/zerosand friends.custom()/inplace_custom()live here because they combine graph ops with extension loading.
abs()
max.experimental.functional.abs(x)
Computes the absolute value of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
result = F.abs(x)
# result is [2.0, 1.0, 0.0, 1.0, 2.0]acos()
max.experimental.functional.acos(x)
Computes the arccosine of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-1.0, 0.0, 1.0])
result = F.acos(x)
# result is approximately [3.1416, 1.5708, 0.0] or [pi, pi/2, 0]-
Parameters:
-
x (Tensor) – The input tensor with values in
[-1, 1]. Must have a floating-point dtype. Forfloat16,bfloat16, andfloat32, values outside this domain are clamped to the valid range. Forfloat64, out-of-domain values produceNaN. -
Returns:
-
A
Tensorof the same shape and dtype asxcontaining the arccosine of each element ofx. Values range from[0, π](radians). -
Return type:
add()
max.experimental.functional.add(lhs, rhs)
Adds two tensors element-wise.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 2.0, 3.0])
b = Tensor([10.0, 20.0, 30.0])
result = F.add(a, b)
# result is [11.0, 22.0, 33.0]
# Scalar is auto-promoted to a tensor.
result = F.add(a, 0.5)
# result is [1.5, 2.5, 3.5]allgather()
max.experimental.functional.allgather(t, tensor_axis=0, mesh_axis=0)
All-gathers a tensor’s shards along a mesh axis.
Transitions the tensor’s placement on mesh_axis from
Sharded to
Replicated. Each device gathers
the shards from its peers and concatenates them along tensor_axis.
-
Parameters:
-
Returns:
-
A tensor with the full data replicated across
mesh_axis. -
Return type:
allreduce_sum()
max.experimental.functional.allreduce_sum(t, mesh_axis=0)
All-reduces a tensor by summing its shards across a mesh axis.
Transitions the tensor’s placement on mesh_axis from
Partial to
Replicated. Every device on
mesh_axis ends up holding the sum of all inputs along that axis.
any_distributed()
max.experimental.functional.any_distributed(args)
True if any Tensor in args is distributed (multi-device).
arange()
max.experimental.functional.arange(start, stop, step=1, out_dim=None, *, dtype=None, device=None)
Creates a sequence of evenly spaced values from start to stop.
The sequence begins at start and increments by step, stopping
before stop (the upper bound is exclusive).
stop - start must be zero or have the same sign as step.
Currently, graph compilation fails when stop - start isn’t evenly
divisible by step. For example, range(0, 5, 2) should produce
three values, [0, 2, 4], but shape inference declares an output length
of 2. The generated values therefore don’t fit the declared output shape.
from max.dtype import DType
from max.experimental import functional as F
result = F.range(0, 5, 1, dtype=DType.float32)
# result holds [0.0, 1.0, 2.0, 3.0, 4.0].-
Parameters:
-
- start (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The first value in the sequence. Must be a scalar value.
- stop (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The exclusive upper bound. The sequence stops before this value. Must be a scalar value.
- step (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The spacing between consecutive values. Must be non-zero.
Defaults to
1. - out_dim (int | str | Dim | integer | TypedAttr | None) – The expected length of the output. Required when dynamic scalar tensor inputs prevent static length inference. When omitted, it’s computed from scalar literals.
- dtype (DType | None) – The element type of the result tensor. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMapping. Sharded placement is not supported.
-
Returns:
-
A
Tensorcontaining the generated sequence. -
Raises:
-
- ValueError – If
out_dimis omitted for dynamic scalar inputs, if any input isn’t scalar, if any input isn’t on the CPU, or ifdevicerequests a sharded placement. - RuntimeError – If a statically known interval isn’t evenly divisible
by
step, causing the inferred output length to disagree with the number of generated values.
- ValueError – If
-
Return type:
argmax()
max.experimental.functional.argmax(x, axis=-1)
Returns the indices of the maximum values along an axis.
It’s useful for finding the position of the largest element along a given dimension, such as determining predicted classes in classification.
When the input contains ties (identical maximum values), behavior depends on the device: CPU returns the first matching index, while GPU may return any of them.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.2, 3.5, 2.1, 0.8], [2.3, 1.9, 4.2, 3.1]])
indices = F.argmax(x, axis=-1)
# indices has shape (2, 1): [[1], [2]]
# Or flatten before reducing:
flat_index = F.argmax(x, axis=None)
# flat_index has shape (1,): [6] (flattened index of max value 4.2)-
Parameters:
-
Returns:
-
A
Tensorwithint64dtype containing the indices of the maximum values alongaxis. For an integeraxis, the result has the same rank asxwith theaxisdimension reduced to size1. WhenaxisisNone, the result has shape(1,). -
Raises:
-
ValueError – If
axisis out of range forx. -
Return type:
argmin()
max.experimental.functional.argmin(x, axis=-1)
Returns the indices of the minimum values along an axis.
When the input contains ties (identical minimum values), behavior depends on the device: CPU returns the first matching index, while GPU may return any of them.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.2, 3.5, 2.1, 0.8], [2.3, 1.9, 4.2, 3.1]])
indices = F.argmin(x, axis=-1)
# indices has shape (2, 1): [[3], [1]]-
Parameters:
-
Returns:
-
A
Tensorwithint64dtype containing the indices of the minimum values alongaxis. For an integeraxis, the result has the same rank asxwith theaxisdimension reduced to size1. WhenaxisisNone, the result has shape(1,). -
Raises:
-
ValueError – If
axisis out of range forx. -
Return type:
argsort()
max.experimental.functional.argsort(x, ascending=True)
Returns the indices that would sort a rank-1 tensor.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([3.0, 1.0, 2.0])
# Ascending order visits 1, 2, 3, so the indices are [1, 2, 0].
result = F.argsort(x, ascending=True)
# result is [1, 2, 0]-
Parameters:
-
Returns:
-
A
Tensorcontaining the sorting indices, with the same shape asxandint64dtype. -
Raises:
-
ValueError – If
xis not rank 1. -
Return type:
as_interleaved_complex()
max.experimental.functional.as_interleaved_complex(x)
Reshapes a real tensor of alternating (real, imag) values into complex form.
Pulls each adjacent (real, imag) pair in the last dimension out into
a trailing pair of size 2.
-
Parameters:
-
x (Tensor) – A real tensor representing complex numbers as alternating pairs of
(real, imag)values. The last dimension must have an even size. -
Returns:
-
A tensor of shape
(*x.shape[:-1], x.shape[-1] // 2, 2). All dimensions except the last are unchanged; the last dimension is halved, and a final dimension of size 2 is appended to hold the(real, imag)components. -
Return type:
atanh()
max.experimental.functional.atanh(x)
Computes the inverse hyperbolic tangent of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-0.5, 0.0, 0.5])
result = F.atanh(x)
# result is approximately [-0.549, 0.0, 0.549]avg_pool2d()
max.experimental.functional.avg_pool2d(input, kernel_size, stride=1, dilation=1, padding=0, ceil_mode=False, count_boundary=True)
Applies 2D average pooling.
Slides a window of size kernel_size over the spatial dimensions and
replaces each window with its average value.
-
Parameters:
-
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
(batch_size, height, width, channels). - kernel_size (tuple[DimLike, DimLike]) – A tuple
(kernel_h, kernel_w)giving the height and width of the sliding window. - stride (int | tuple[int, int]) – The stride of the sliding window. Either a single
intapplied to both spatial dimensions, or a tuple(stride_h, stride_w). Defaults to1. - dilation (int | tuple[int, int]) – The spacing between kernel elements. Either a single
intapplied to both spatial dimensions, or a tuple(dilation_h, dilation_w). Defaults to1. - padding (int | tuple[int, int]) – Zero-padding added to both sides of each spatial dimension.
Either a single
intapplied to both spatial dimensions, or a tuple(pad_h, pad_w). Defaults to0. - ceil_mode (bool) – When
True, uses ceil instead of floor when computing the output spatial shape. Defaults toFalse. - count_boundary (bool) – When
True, includes padding elements in the divisor when computing the average. Defaults toTrue.
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
-
Returns:
-
A
Tensorcontaining the averaged values, with shape(batch_size, height_out, width_out, channels). -
Return type:
band_part()
max.experimental.functional.band_part(x, num_lower=None, num_upper=None, exclude=False)
Set all values to zero except a diagonal band of an input matrix.
All but the last two axes are treated as batches, and the last two axes define the matrices.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
# Keep the main diagonal and one sub-diagonal, producing
# [[1, 0, 0], [1, 1, 0], [0, 1, 1]].
result = F.band_part(x, num_lower=1, num_upper=0)
# result is [[1, 0, 0], [1, 1, 0], [0, 1, 1]]-
Parameters:
-
- x (Tensor) – The input tensor to mask.
- num_lower (int | None) – The number of diagonal bands to include below the central
diagonal. If
Noneor-1, includes the entire lower triangle. Defaults toNone. - num_upper (int | None) – The number of diagonal bands to include above the central
diagonal. If
Noneor-1, includes the entire upper triangle. Defaults toNone. - exclude (bool) – Whether to invert the selection, zeroing out the elements in
the band instead. Defaults to
False.
-
Returns:
-
A
Tensorcontainingxwith the masked-out elements set to zero and the remaining elements copied fromx. It has the same shape and dtype asx. -
Raises:
-
ValueError – If the input tensor rank is less than 2, or if
num_lowerornum_upperare out of bounds for statically known dimensions. -
Return type:
bottom_k()
max.experimental.functional.bottom_k(input, k, axis=-1)
Returns the k smallest values along an axis with their indices.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 3.0, 2.0, 5.0, 4.0])
# The two smallest values are 1 and 2 at indices 0 and 2.
values, indices = F.bottom_k(x, k=2, axis=-1)
# values is [1, 2]
# indices is [0, 2]-
Parameters:
-
Returns:
-
A tuple of two
Tensorobjects. The first holds the bottomkvalues alongaxisin ascending order, and the second holds theirint64indices ininput. Both tensors have the shape ofinputwith theaxisdimension reduced to sizek. -
Return type:
broadcast_to()
max.experimental.functional.broadcast_to(x, shape)
Broadcasts a tensor to a target shape.
Each input dimension must either equal the corresponding target
dimension or be 1 (which is then stretched to match). This
follows NumPy broadcasting semantics and is equivalent to PyTorch’s
torch.broadcast_to().
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor.ones([3, 1])
result = F.broadcast_to(x, [3, 4])
# result has shape (3, 4)
# Add a new leading dimension
result = F.broadcast_to(x, [2, 3, 4])
# result has shape (2, 3, 4)buffer_store()
max.experimental.functional.buffer_store(destination, source)
Stores values from a tensor into a tensor buffer.
buffer_store_slice()
max.experimental.functional.buffer_store_slice(destination, source, indices)
Stores values into a slice of a tensor buffer.
cast()
max.experimental.functional.cast(x, dtype)
Casts a tensor to a different data type.
Values may change when the source and target types can’t represent each other exactly. Float-to-integer casts truncate toward zero; float-to-float casts with lower precision round to the nearest representable value.
from max.dtype import DType
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.7, -1.7, 2.5]) # float32 on CPU by default
result = F.cast(x, DType.int32)
# result has dtype int32 and values [1, -1, 2]ceil()
max.experimental.functional.ceil(x)
Computes the ceiling of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.5, 2.0, -1.5, -2.7])
result = F.ceil(x)
# result is [2.0, 2.0, -1.0, -2.0]chunk()
max.experimental.functional.chunk(x, chunks, axis=0)
Splits a tensor into equal-sized chunks along an axis.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1, 2, 3, 4, 5, 6])
# Split into three equal chunks along axis 0
parts = F.chunk(x, 3, axis=0)
# parts[0] is [1, 2]
# parts[1] is [3, 4]
# parts[2] is [5, 6]-
Parameters:
-
Returns:
-
A list of
Tensorobjects (chunks), each the same size alongaxis. -
Raises:
-
- ValueError – If
chunksdoes not evenly divide the size ofxalongaxis, or ifxis a scalar andchunksis greater than1. - IndexError – If
axisis out of range forx.
- ValueError – If
-
Return type:
clamp()
max.experimental.functional.clamp(x, lower_bound, upper_bound)
Clamps tensor values to [lower_bound, upper_bound].
-
Parameters:
-
Return type:
clip()
max.experimental.functional.clip(x, lower_bound, upper_bound)
Clamps tensor values to [lower_bound, upper_bound].
-
Parameters:
-
Return type:
complex_mul()
max.experimental.functional.complex_mul(lhs, rhs)
Multiplies two complex-valued tensors element-wise.
Both inputs must use the interleaved complex representation (trailing dimension of size 2).
concat()
max.experimental.functional.concat(original_vals, axis=0)
Concatenates tensors along an axis.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([[1, 2], [3, 4]])
b = Tensor([[5, 6], [7, 8]])
vertical = F.concat([a, b], axis=0)
# vertical has shape (4, 2):
# [[1, 2], [3, 4], [5, 6], [7, 8]]
horizontal = F.concat([a, b], axis=1)
# horizontal has shape (2, 4):
# [[1, 2, 5, 6], [3, 4, 7, 8]]-
Parameters:
-
Returns:
-
A
Tensorcontaining the concatenated inputs. Its size alongaxisis the sum of the inputs’ sizes and every other axis is unchanged. -
Raises:
-
- ValueError – If no tensors are provided, if the inputs don’t all have
the same rank, if they differ in size on any dimension other than
axis, or if they aren’t all on the same device. - IndexError – If
axisis out of range.
- ValueError – If no tensors are provided, if the inputs don’t all have
the same rank, if they differ in size on any dimension other than
-
Return type:
cond()
max.experimental.functional.cond(pred, out_types, then_fn, else_fn)
Conditionally executes one of two branches based on a boolean predicate.
Both branches must return the same number and types of values as
specified by out_types. The predicate is evaluated at runtime to
determine which branch executes. If pred lives on a non-CPU device,
it is transferred to CPU automatically.
from max.driver import CPU
from max.dtype import DType
from max.experimental import Tensor
from max.experimental import functional as F
from max.graph import DeviceRef, TensorType
def then_fn():
return Tensor([1.0, 2.0], dtype=DType.float32, device=CPU())
def else_fn():
return Tensor([10.0, 20.0], dtype=DType.float32, device=CPU())
pred = Tensor(True, dtype=DType.bool, device=CPU())
out_types = [TensorType(DType.float32, [2], DeviceRef.CPU())]
(result,) = F.cond(pred, out_types, then_fn, else_fn)
# pred is True, so result is [1.0, 2.0]-
Parameters:
-
- pred (Tensor) – A boolean scalar tensor of type
booldetermining which branch to execute. - out_types (Iterable[Type[Any]] | None) – The expected output types for both branches. Use
Nonefor branches that don’t return values (such as buffer mutations). - then_fn (Callable[[], Iterable[Tensor] | Tensor | None]) – A callable executed when
predisTrue. - else_fn (Callable[[], Iterable[Tensor] | Tensor | None]) – A callable executed when
predisFalse.
- pred (Tensor) – A boolean scalar tensor of type
-
Returns:
-
The output values from the executed branch, or an empty list when
out_typesisNone. -
Return type:
constant()
max.experimental.functional.constant(value, dtype=None, device=None)
Creates a constant tensor from a Python literal or array-like value.
For an array-like value, the array’s own dtype is preserved when
dtype is None. For a Python scalar or sequence, dtype
defaults to float32 on CPU or bfloat16 on accelerators.
from max.dtype import DType
from max.experimental import functional as F
result = F.constant([[1.0, 2.0], [3.0, 4.0]], DType.float32)
# result holds [[1.0, 2.0], [3.0, 4.0]].-
Parameters:
-
- value (DLPackArray | Sequence[float | number[Any] | Sequence[Number | NestedArray]] | float | number[Any]) – The value to embed. A Python scalar, a (nested) sequence of numbers, or an array-like object that supports DLPack, such as a NumPy array.
- dtype (DType | None) – The constant tensor’s element type. For an array-like
value, defaults to the array’s dtype. For a Python scalar or sequence, defaults tofloat32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement. Defaults to an accelerator if available, otherwise the CPU.
-
Returns:
-
A
Tensorcontaining the constant, with the same shape asvalue. A scalarvalueproduces a rank-0 tensor. -
Raises:
-
- TypeError – If
dtypeis a sub-byte type. - ValueError – If
valueis a nested sequence that isn’t rectangular, if an integer invalueis out of range fordtype, or ifdtypedoesn’t match the dtype of an array-likevalue.
- TypeError – If
-
Return type:
constant_external()
max.experimental.functional.constant_external(name, type, device=None, align=None)
Creates a constant tensor from external (weight) data.
External constants are loaded at compile time from the named weight rather than being inlined into the graph.
-
Parameters:
-
- name (str) – The external symbol name to load (typically a weight identifier).
- type (TensorType) – The
TensorTypedescribing the constant’s shape and dtype. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement. - align (int | None) – The alignment of the constant. If not provided, the default alignment for the type’s dtype will be used.
-
Returns:
-
A tensor on the requested placement initialized from the external data.
-
Return type:
conv2d()
max.experimental.functional.conv2d(x, filter, stride=(1, 1), dilation=(1, 1), padding=(0, 0, 0, 0), groups=1, bias=None, input_layout=ConvInputLayout.NHWC, filter_layout=FilterLayout.RSCF)
Computes the 2-D convolution product of the input with the given filter, bias, strides, dilations, paddings, and groups.
This uses the following layout assumptions:
- The input has channels-last (NHWC) layout, meaning
(batch_size, height, width, in_channels). - The filter has RSCF layout, meaning
(height, width, in_channels / num_groups, out_channels). - The bias has shape
(out_channels,).
The padding values are expected to take the form (pad_dim1_before, pad_dim1_after, pad_dim2_before, pad_dim2_after…) and represent padding 0’s before and after the indicated spatial dimensions in the input. In 2-D convolution, dim1 here represents H and dim2 represents W. In Python-like syntax, padding a 2x3 spatial input with [0, 1, 2, 1] would yield:
input = [
[1, 2, 3],
[4, 5, 6]
]
# Shape is 2x3
padded_input = [
[0, 0, 1, 2, 3, 0],
[0, 0, 4, 5, 6, 0],
[0, 0, 0, 0, 0, 0]
]
# Shape is 3x6This op currently only supports strides and padding on the input.
Convolving a 2x2 input with an all-ones 2x2 filter sums the window:
from max.experimental import Tensor
from max.experimental import functional as F
# NHWC input: batch 1, 2x2 spatial, 1 channel.
x = Tensor([[[[1.0], [2.0]], [[3.0], [4.0]]]])
# RSCF filter: 2x2, 1 in-channel, 1 out-channel, all ones.
filter = Tensor([[[[1.0]], [[1.0]]], [[[1.0]], [[1.0]]]])
result = F.conv2d(x, filter)
# result is [[[[10]]]] with shape (1, 1, 1, 1)-
Parameters:
-
- x (Tensor) – An NHWC input tensor to perform the convolution upon.
- filter (Tensor) – The convolution filter in RSCF layout,
(height, width, in_channels / num_groups, out_channels). - stride (tuple[int, int]) – The stride of the convolution operation.
- dilation (tuple[int, int]) – The spacing between the kernel points.
- padding (tuple[int, int, int, int]) – The amount of padding applied to the input.
- groups (int) – When greater than 1, divides the convolution into multiple parallel convolutions. The number of input and output channels must both be divisible by the number of groups.
- bias (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray | None) – An optional 1-D bias of shape
(out_channels,). - input_layout (ConvInputLayout) – The layout of the input tensor. Defaults to NHWC.
- filter_layout (FilterLayout) – The layout of the filter tensor. Defaults to RSCF.
-
Returns:
-
A
Tensorcontaining the result of the convolution, with shape(batch_size, height_out, width_out, out_channels). -
Raises:
-
ValueError – If
xisn’t rank 4,filterisn’t rank 4,biasis given and isn’t rank 1, orxandfilteraren’t on the same device. -
Return type:
conv2d_transpose()
max.experimental.functional.conv2d_transpose(x, filter, stride=(1, 1), dilation=(1, 1), padding=(0, 0, 0, 0), output_paddings=(0, 0), bias=None, input_layout=ConvInputLayout.NHWC, filter_layout=FilterLayout.RSCF)
Computes the 2-D deconvolution of the input with the given filter, strides, dilations, and paddings.
This computes the transpose (gradient) of convolution, with the following
layout assumptions (where out_channels is with respect to the original
convolution):
- The input
xhas channels-last (NHWC) layout, meaning(batch_size, height, width, in_channels). - The filter has RSCF layout, meaning
(kernel_height, kernel_width, out_channels, in_channels). - The bias has shape
(out_channels,).
This op effectively computes the gradient of a convolution with respect to its input, as if the original convolution had the same filter and hyperparameters as this op. For a visualization of the computation, see Transposed Convolution.
The padding values take the form (pad_dim1_before, pad_dim1_after, pad_dim2_before, pad_dim2_after, ...) and are cropped (removed) from the
indicated spatial dimensions of the output. In 2-D transposed
convolution, dim1 represents H_out and dim2 represents
W_out. In Python-like syntax, cropping a 2x4 spatial output with
[0, 1, 2, 1] would yield:
output = [
[1, 2, 3, 4],
[5, 6, 7, 8]
]
# Shape is 2x4
cropped_output = [
[3],
]
# Shape is 1x1Deconvolving a 1x1 input with an all-ones 2x2 filter (filter is RSCF, with
out_channels and in_channels with respect to the original
convolution):
from max.experimental import Tensor
from max.experimental import functional as F
# NHWC input: batch 1, 1x1 spatial, 1 channel.
x = Tensor([[[[3.0]]]])
# RSCF filter: 2x2 kernel, 1 out-channel, 1 in-channel, all ones.
filter = Tensor([[[[1.0]], [[1.0]]], [[[1.0]], [[1.0]]]])
result = F.conv2d_transpose(x, filter)-
Parameters:
-
- x (Tensor) – An NHWC input tensor to perform the deconvolution upon.
- filter (Tensor) – The convolution filter in RSCF layout,
(height, width, out_channels, in_channels). - stride (tuple[int, int]) – The stride of the sliding window as a tuple
(stride_h, stride_w). Defaults to(1, 1). - dilation (tuple[int, int]) – The spacing between the kernel points.
- padding (tuple[int, int, int, int]) – The amount cropped from each spatial dimension of the output.
- output_paddings (tuple[int, int]) – The number of zeros added at the end of each output
spatial axis. This resolves the ambiguity between multiple output
shapes when a stride is greater than 1. Only
0is supported. - bias (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray | None) – An optional tensor of shape
(out_channels,). - input_layout (ConvInputLayout) – The layout of the input tensor. Defaults to NHWC.
- filter_layout (FilterLayout) – The layout of the filter tensor. Defaults to RSCF.
-
Returns:
-
A
Tensorcontaining the result of the deconvolution, in channels-first (NCHW) layout(batch_size, out_channels, height_out, width_out). This differs from the channels-last (NHWC) input layout. -
Raises:
-
ValueError – If
xisn’t rank 4,filterisn’t rank 4,biasis given and isn’t rank 1, an output padding isn’t smaller than its stride, orxandfilteraren’t on the same device. -
Return type:
conv3d()
max.experimental.functional.conv3d(x, filter, stride=(1, 1, 1), dilation=(1, 1, 1), padding=(0, 0, 0, 0, 0, 0), groups=1, bias=None, input_layout=ConvInputLayout.NHWC, filter_layout=FilterLayout.QRSCF)
Computes the 3-D convolution product of the input with the given filter, bias, strides, dilations, paddings, and groups.
This uses the following layout assumptions:
- The input has channels-last (NDHWC) layout, meaning
(batch_size, depth, height, width, in_channels). - The filter has QRSCF layout, meaning
(depth, height, width, in_channels / num_groups, out_channels).
The padding values are expected to take the form (pad_dim1_before, pad_dim1_after, pad_dim2_before, pad_dim2_after…) and represent padding 0’s before and after the indicated spatial dimensions in the input. In 3-D convolution, dim1 here represents D, dim2 represents H and dim3 represents W. In Python-like syntax, padding a 2x3 spatial input with [0, 1, 2, 1] would yield:
input = [
[1, 2, 3],
[4, 5, 6]
]
# Shape is 2x3
padded_input = [
[0, 0, 1, 2, 3, 0],
[0, 0, 4, 5, 6, 0],
[0, 0, 0, 0, 0, 0]
]
# Shape is 3x6This op currently only supports strides and padding on the input.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
# NDHWC input: batch 1, 4x4x4 spatial, 1 channel.
x = Tensor.ones((1, 4, 4, 4, 1), dtype=DType.float32)
# QRSCF filter: 2x2x2, 1 in-channel, 1 out-channel.
filter = Tensor.ones((2, 2, 2, 1, 1), dtype=DType.float32)
result = F.conv3d(x, filter)
# result has shape (1, 3, 3, 3, 1)-
Parameters:
-
- x (Tensor) – An NDHWC input tensor to perform the convolution upon.
- filter (Tensor) – The convolution filter in QRSCF layout,
(depth, height, width, in_channels / num_groups, out_channels). - stride (tuple[int, int, int]) – The stride of the convolution operation.
- dilation (tuple[int, int, int]) – The spacing between the kernel points.
- padding (tuple[int, int, int, int, int, int]) – The amount of padding applied to the input.
- groups (int) – When greater than 1, divides the convolution into multiple parallel convolutions. The number of input and output channels must both be divisible by the number of groups.
- bias (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray | None) – An optional 1-D bias of shape
(out_channels,). - input_layout (ConvInputLayout) – The layout of the input tensor. Defaults to NDHWC.
- filter_layout (FilterLayout) – The layout of the filter tensor. Defaults to QRSCF.
-
Returns:
-
A
Tensorcontaining the result of the convolution, with shape(batch_size, depth_out, height_out, width_out, out_channels). -
Raises:
-
ValueError – If
xisn’t rank 5,filterisn’t rank 5, orbiasis given and isn’t rank 1. -
Return type:
cos()
max.experimental.functional.cos(x)
Computes the cosine of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([0.0, 0.5, 1.0])
result = F.cos(x)
# result is approximately [1.0, 0.878, 0.540]cumsum()
max.experimental.functional.cumsum(x, axis=-1, exclusive=False, reverse=False)
Computes the cumulative sum of a tensor along an axis.
-
Parameters:
-
- x (Tensor) – The input tensor.
- axis (int) – The axis along which to compute the cumulative sum. Defaults to
-1. - exclusive (bool) – When
True, the first output value is0and the final input element is excluded from the sum. Defaults toFalse. - reverse (bool) – When
True, computes the sum starting from the end of the axis. Defaults toFalse.
-
Returns:
-
A tensor of the same shape and dtype where each element is the sum of the corresponding input elements up to that position along
axis. -
Return type:
custom()
max.experimental.functional.custom(name, device, values, out_types, parameters=None, custom_extensions=None)
Calls a custom op, optionally loading custom Mojo extensions first.
-
Parameters:
-
- name (str) – The registered name of the custom op.
- device (Device | DeviceRef) – The device on which to execute the op.
- values (Sequence[Any]) – The input values passed to the op.
- out_types (Sequence[Type[Any]]) – The expected output types.
- parameters (Mapping[str, bool | int | str | DType] | None) – Optional compile-time parameters for the op.
- custom_extensions (str | Path | Sequence[str | Path] | None) – Optional path or sequence of paths to custom
Mojo extensions (
.mojocor.mojosources) to load before invoking the op.
-
Returns:
-
A list of tensors produced by the custom op.
-
Return type:
dequantize()
max.experimental.functional.dequantize(encoding, quantized)
Dequantizes a quantized tensor to floating point.
-
Parameters:
-
- encoding (QuantizationEncoding) – The quantization encoding to use.
- quantized (Tensor) – The quantized tensor to dequantize.
-
Returns:
-
A
Tensorcontaining the dequantized, floating point result. -
Raises:
-
- ValueError – If
encodingis not a supported quantization encoding, or if the last dimension isn’t divisible by the encoding’s block size. - TypeError – If the last dimension of
quantizedisn’t static.
- ValueError – If
-
Return type:
distributed_broadcast()
max.experimental.functional.distributed_broadcast(t, mesh)
Replicates a non-distributed tensor onto every device with one collective.
-
Parameters:
-
- t (Tensor) – A non-distributed source tensor.
- mesh (DeviceMesh) – The device mesh to replicate onto.
-
Returns:
-
A distributed tensor with
Replicatedplacement on every axis. -
Return type:
div()
max.experimental.functional.div(lhs, rhs)
Divides two tensors element-wise using true division (Python /).
For integer operands, this performs true division by promoting to float,
matching Python’s / operator behavior. For floating-point operands,
this performs standard floating-point division.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([10.0, 6.0, 3.0])
b = Tensor([2.0, 3.0, 4.0])
result = F.div(a, b)
# result is [5.0, 2.0, 0.75]-
Parameters:
-
Returns:
-
A
Tensorwith the broadcast shape containinglhs / rhselement-wise. The result has a floating-point dtype for integer operands and the promoted dtype for mixed types. -
Return type:
elementwise_max()
max.experimental.functional.elementwise_max(lhs, rhs)
Computes the element-wise maximum of two tensors.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([3.0, 1.0, 4.0])
b = Tensor([1.0, 5.0, 9.0])
result = F.elementwise_max(a, b)
# result is [3.0, 5.0, 9.0]elementwise_min()
max.experimental.functional.elementwise_min(lhs, rhs)
Computes the element-wise minimum of two tensors.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([3.0, 1.0, 4.0])
b = Tensor([1.0, 5.0, 9.0])
result = F.elementwise_min(a, b)
# result is [1.0, 1.0, 4.0]ensure_context()
max.experimental.functional.ensure_context()
Ensures a realization context exists for Tensor / TensorValue conversion.
-
Return type:
-
Generator[None]
equal()
max.experimental.functional.equal(lhs, rhs)
Tests element-wise equality between two tensors.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 2.0, 3.0])
b = Tensor([1.0, 5.0, 3.0])
result = F.equal(a, b)
# result is [True, False, True]erf()
max.experimental.functional.erf(x)
Computes the error function of a tensor element-wise.
The error function erf is the probability that a randomly sampled
normal distribution falls within a given range.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-1.0, 0.0, 1.0])
result = F.erf(x)
# result is approximately [-0.843, 0.0, 0.843]exp()
max.experimental.functional.exp(x)
Computes the exponential of a tensor element-wise.
This applies exp(x) = e^x, where e is Euler’s number.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([0.0, 1.0, 2.0])
result = F.exp(x)
# result is approximately [1.0, 2.718, 7.389]flatten()
max.experimental.functional.flatten(x, start_dim=0, end_dim=-1)
Flattens the specified dimensions of a tensor.
This does not change the order or total number of elements in the tensor.
All dimensions from start_dim to end_dim (inclusive) are merged into
a single output dimension.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (2, 2, 2).
x = Tensor.ones([2, 2, 2])
# Merge dimensions 1 and 2 into one, producing shape (2, 4).
result = F.flatten(x, start_dim=1)-
Parameters:
-
Returns:
-
A
Tensorcontaining thestart_dimthroughend_dimofxmerged into one dimension. -
Raises:
-
- IndexError – If
start_dimorend_dimis out of range. - ValueError – If
start_dimcomes afterend_dim.
- IndexError – If
-
Return type:
floor()
max.experimental.functional.floor(x)
Computes the floor of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.5, 2.0, -1.5, -2.7])
result = F.floor(x)
# result is [1.0, 2.0, -2.0, -3.0]floor_div()
max.experimental.functional.floor_div(lhs, rhs)
Divides two tensors element-wise using floor division (Python //).
The result is rounded toward negative infinity, matching Python’s //.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor. Integer operands stay in the integer
domain (no float64 promotion), unlike div().
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
a = Tensor([7, 10, 18], dtype=DType.int32)
b = Tensor([2, 5, 6], dtype=DType.int32)
result = F.floor_div(a, b)
# result is [3, 2, 3]
# Floating-point operands are supported and still round toward -inf.
result = F.floor_div(Tensor([7.5, -7.5], dtype=DType.float32), 2.0)
# result is [3.0, -4.0]fold()
max.experimental.functional.fold(input, output_size, kernel_size, stride=1, dilation=1, padding=0)
Combines an array of sliding local blocks into a larger tensor.
L, the number of blocks, must equal prod((output_size[d] + 2 * padding[d] - dilation[d] * (kernel_size[d] - 1) - 1) // stride[d] + 1),
where d ranges over all spatial dimensions.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
# Shape (N, C * kernel_h * kernel_w, L) = (1, 1 * 2 * 2, 9).
x = Tensor.ones((1, 4, 9), dtype=DType.float32)
# Fold nine 2x2 blocks into a 4x4 image.
result = F.fold(x, output_size=(4, 4), kernel_size=(2, 2))
# result has shape (1, 1, 4, 4)-
Parameters:
-
- input (Tensor) – The 3-D tensor to fold, with shape
(N, C * kernel_sizes, L), whereNis the batch dimension,Cis the number of channels,kernel_sizesis the product of the kernel sizes, andLis the number of local blocks. - output_size (tuple[DimLike, DimLike]) – The spatial dimensions of the output tensor, as a tuple of two ints.
- kernel_size (tuple[DimLike, DimLike]) – The size of the sliding blocks, as a tuple of two ints.
- stride (int | tuple[int, int]) – The stride of the sliding blocks. Either an int or a tuple of
two ints. Defaults to
1. - dilation (int | tuple[int, int]) – The spacing between kernel elements. Either an int or a
tuple of two ints. Defaults to
1. - padding (int | tuple[int, int]) – The zero-padding added on both sides of the input. Either an
int or a tuple of two ints. Defaults to
0.
- input (Tensor) – The 3-D tensor to fold, with shape
-
Returns:
-
A
Tensorcontaining the folded 4-D tensor, with shape(N, C, output_size[0], output_size[1]). -
Raises:
-
ValueError – If the input’s channel dimension isn’t a multiple of the total kernel size, or if the number of blocks
Ldoesn’t match the value computed from the other arguments. -
Return type:
full()
max.experimental.functional.full(shape, value, *, dtype=None, device=None)
Creates a tensor filled with a single value.
When device is a
DeviceMapping, the result is
distributed across that mesh according to its placements.
-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- value (float | number[Any]) – The fill value.
- dtype (DType | None) – The data type of the tensor. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement. Defaults to the current realization context’s device.
-
Returns:
-
A tensor of the requested shape, dtype, and placement with every element set to
value. -
Return type:
full_like()
max.experimental.functional.full_like(like, value)
Creates a tensor filled with a single value, matching another tensor’s shape and dtype.
-
Parameters:
-
- like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
- value (float | number[Any]) – The fill value.
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with every element set tovalue. -
Return type:
functional()
max.experimental.functional.functional(graph_op, rule=None)
Wraps a graph op as a distributed dispatch entry.
Returns a callable that local-auto-shards when any argument is a
distributed Tensor (and a rule is bound), and otherwise
forwards to the bare graph_op. The returned wrapper carries
graph_op and rule as attributes; reassign wrapper.rule
to swap the sharding rule at runtime without re-wrapping.
gather()
max.experimental.functional.gather(input, indices, axis)
Selects elements out of an input tensor by index.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
x = Tensor([[1, 2], [3, 4], [5, 6]], dtype=DType.int32)
indices = Tensor([0, 2], dtype=DType.int64)
# Select rows 0 and 2, producing [[1, 2], [5, 6]].
result = F.gather(x, indices, axis=0)
# result is [[1, 2], [5, 6]]-
Parameters:
-
- input (Tensor) – The input tensor to select elements from.
- indices (Tensor) – A tensor of
int32orint64index values on the same device asinput. - axis (int) – The dimension that
indicesindexes intoinput. If negative, indexes relative to the end of the input tensor. For example,gather(input, indices, axis=-1)indexes against the last dimension ofinput.
-
Returns:
-
A
Tensorcontaining the selected elements. Its shape isinput.shapewith the dimension ataxisreplaced byindices.shape. -
Raises:
-
- IndexError – If
axisis out of range forinput. - ValueError – If
indicesisn’t integral or isn’t on the same device asinput.
- IndexError – If
-
Return type:
gather_nd()
max.experimental.functional.gather_nd(input, indices, batch_dims=0)
Selects elements from a tensor by N-dimensional index.
Unlike gather(), which indexes along a single axis,
gather_nd() indexes along multiple dimensions at once. The last
dimension of indices is the index vector: its values select
elements from input immediately after any batch_dims leading
dimensions. Any remaining trailing dimensions of input are sliced
into the output as features.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
input = Tensor(
[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]
)
indices = Tensor([[0, 1], [1, 0]], dtype=DType.int64)
gathered = F.gather_nd(input, indices)
# gathered is [[3.0, 4.0], [5.0, 6.0]]Each row of indices selects one row from the first two dimensions of
input. The trailing dimension is copied into the output.
-
Parameters:
-
- input (Tensor) – The input tensor to gather from.
- indices (Tensor) – An integer tensor of multi-dimensional indices. Its last dimension must be static and gives the size of the index vector.
- batch_dims (int) – The number of leading batch dimensions shared by
inputandindices. The shapes must match exactly along these leading dimensions. This function does not broadcast. Defaults to0.
-
Returns:
-
A
Tensorcontaining the gathered elements, with the same dtype asinput. Its shape is the concatenation of:input.shape[:batch_dims]— the leading batch dimensions.indices.shape[batch_dims:-1]— the gather dimensions.input.shape[batch_dims + indices.shape[-1]:]— the trailing sliced dimensions.
-
Raises:
-
ValueError – If any input is invalid. This includes when
indices’s last dimension is not static,indicesis not an integer tensor,batch_dimsis negative or greater thanindices.rank - 1,batch_dims + indices.shape[-1]exceedsinput.rank, or the leadingbatch_dimsofinputandindicesdon’t match. -
Return type:
gaussian()
max.experimental.functional.gaussian(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Samples values from a Gaussian (normal) distribution with the given mean and standard deviation.
When device is a
DeviceMapping, each Sharded
axis draws an independent stream while shards on Replicated axes
draw identical values.
from max.experimental import functional as F
result = F.gaussian((2, 3), mean=0.0, std=1.0)
# result is a (2, 3) tensor sampled from a standard normal distribution.-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- mean (float) – The mean of the distribution. Defaults to
0.0. - std (float) – The standard deviation of the distribution. Defaults to
1.0. - dtype (DType | None) – The data type of the tensor.
- device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement.
-
Returns:
-
A
Tensorof the requested shape, dtype, and placement with values sampled fromNormal(mean, std**2). -
Return type:
gaussian_like()
max.experimental.functional.gaussian_like(like, mean=0.0, std=1.0)
Samples Gaussian values matching another tensor’s shape and dtype.
-
Parameters:
-
- like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
- mean (float) – The mean of the distribution. Defaults to
0.0. - std (float) – The standard deviation of the distribution. Defaults to
1.0.
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with values sampled fromNormal(mean, std**2). -
Return type:
gelu()
max.experimental.functional.gelu(x, approximate='none')
Applies the GELU (Gaussian Error Linear Unit) activation element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-1.0, 0.0, 1.0])
result = F.gelu(x)
# result is approximately [-0.159, 0.0, 0.841]-
Parameters:
-
Returns:
-
A
Tensorof the same shape and dtype asxcontaining the GELU activation applied to each element ofx. -
Return type:
greater()
max.experimental.functional.greater(lhs, rhs)
Tests element-wise whether one tensor is greater than another.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 5.0, 3.0])
b = Tensor([2.0, 3.0, 3.0])
result = F.greater(a, b)
# result is [False, True, False]greater_equal()
max.experimental.functional.greater_equal(lhs, rhs)
Tests element-wise whether one tensor is greater than or equal to another.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 5.0, 3.0])
b = Tensor([2.0, 3.0, 3.0])
result = F.greater_equal(a, b)
# result is [False, True, True]group_norm()
max.experimental.functional.group_norm(input, gamma, beta, num_groups, epsilon)
Computes group normalization over the channel axis of input.
Splits the channel axis (axis 1) of input into num_groups
groups, computes the mean and variance within each group, and
normalizes. gamma and beta then apply a per-channel affine
transform. Useful when the batch axis is small enough that batch
normalization is unstable.
-
Parameters:
-
- input (Tensor) – The tensor to normalize, of shape
(batch, channels, ...). - gamma (Tensor) – The per-channel scale applied after normalization. A 1-D
tensor whose length matches the channel axis of
input. - beta (Tensor) – The per-channel bias added after scaling. A 1-D tensor with
the same shape as
gamma. - num_groups (int) – The number of groups to split the channel axis into. Must divide the channel size evenly.
- epsilon (float) – A small positive constant added to the variance for numerical stability.
- input (Tensor) – The tensor to normalize, of shape
-
Returns:
-
A
Tensorwith the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputhas fewer than 2 dimensions. -
Return type:
hann_window()
max.experimental.functional.hann_window(window_length, *, periodic=True, dtype=None, device=None)
Computes a Hann window of a given length.
For a symmetric window of N points with N > 1, the value at index
n is:
w[n] = 0.5 * (1 - cos(2 * pi * n / (N - 1)))A window of length 0 is empty, and a window of length 1 is
[1].
A periodic window instead computes N + 1 points and drops the last
one, which makes it suitable for spectral analysis.
from max.experimental import functional as F
result = F.hann_window(4, periodic=True)
# result holds [0.0, 0.5, 1.0, 0.5].-
Parameters:
-
- window_length (int) – The number of points in the window.
- periodic (bool) – Whether to return a periodic window. When
True, computes a symmetric window ofwindow_length + 1points and drops the last point, so thathann_window(L, periodic=True)equalshann_window(L + 1, periodic=False)[:-1]. Defaults toTrue. - dtype (DType | None) – The output tensor’s data type. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMapping. Sharded placement is not supported.
-
Returns:
-
A 1-D
Tensorof shape(window_length,)containing the window. -
Raises:
-
- ValueError – If
window_lengthis negative, or ifdevicerequests a sharded placement. - TypeError – If
window_lengthisn’t an integer.
- ValueError – If
-
Return type:
in_graph_context()
max.experimental.functional.in_graph_context()
Returns True when executing inside a Graph context.
-
Return type:
inplace_custom()
max.experimental.functional.inplace_custom(name, device, values, out_types=None, parameters=None, custom_extensions=None)
Calls an in-place custom op that mutates one or more of its inputs.
Like custom(), but for ops that mutate buffer values rather
than returning new tensors.
-
Parameters:
-
- name (str) – The registered name of the custom op.
- device (Device | DeviceRef) – The device on which to execute the op.
- values (Sequence[Any]) – The input values; one or more are mutated in place.
- out_types (Sequence[Type[Any]] | None) – Optional expected output types. Most in-place ops
return no outputs and can leave this as
None. - parameters (dict[str, bool | int | str | DType] | None) – Optional compile-time parameters for the op.
- custom_extensions (str | Path | Sequence[str | Path] | None) – Optional path or sequence of paths to custom Mojo extensions to load before invoking the op.
-
Returns:
-
A list of tensors produced by the custom op, or an empty list when the op produces no outputs.
-
Return type:
irfft()
max.experimental.functional.irfft(input_tensor, n=None, axis=-1, normalization=Normalization.BACKWARD, input_is_complex=False, buffer_size_mb=512)
Computes the inverse of the real-input FFT.
-
Parameters:
-
- input_tensor (Tensor) – The input tensor to compute the inverse real FFT of.
- n (int | None) – The size of the output tensor. The input tensor is padded or
truncated to
n // 2 + 1alongaxis. - axis (int) – The axis along which to compute the inverse FFT. Defaults to
-1. - normalization (Normalization | str) – The normalization to apply to the output tensor. One of
"backward","ortho", or"forward". When"backward", the output is divided byn. When"ortho", the output is divided bysqrt(n). When"forward", no normalization is applied. - input_is_complex (bool) – Whether the input tensor is already interleaved
complex. When
True, the last dimension of the input tensor must be 2, and is excluded from the dimension referred to byaxis. - buffer_size_mb (int) – The estimated size of a persistent buffer to use for
storage of intermediate results. Needs to be the same across
multiple calls to
irfftwithin the same graph.
-
Returns:
-
A real tensor that is the inverse FFT of the complex input. The shape matches the input shape, except along
axis, which is replaced byn.
is_inf()
max.experimental.functional.is_inf(x)
Tests element-wise whether a tensor contains infinite values.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, float("inf"), float("-inf"), float("nan")])
result = F.is_inf(x)
# result is [False, True, True, False]is_nan()
max.experimental.functional.is_nan(x)
Tests element-wise whether a tensor contains NaN values.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, float("inf"), float("nan"), 0.0])
result = F.is_nan(x)
# result is [False, False, True, False]layer_norm()
max.experimental.functional.layer_norm(input, gamma, beta, epsilon)
Computes layer normalization over the last dimension of input.
The output is gamma * (input - mean) / sqrt(var + epsilon) + beta,
where mean and var are reduced over the last axis of input
and broadcast back across the leading axes.
Reduction is performed in the dtype of input. For numerically stable
normalization on float16 or bfloat16 inputs, cast to float32 before
calling this op and cast the result back.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.0, 3.0]])
gamma = Tensor([1.0, 1.0])
beta = Tensor([0.0, 0.0])
result = F.layer_norm(x, gamma, beta, epsilon=1e-5)
# Each row is normalized to zero mean and approximately unit variance.-
Parameters:
-
- input (TensorValue) – The tensor to normalize. Reduction runs over the last axis.
- gamma (Tensor) – The scale applied after normalization. A 1-D tensor whose
length matches the last dimension of
input. - beta (Tensor) – The bias added after scaling. A 1-D tensor with the same
shape as
gamma. - epsilon (float) – A small positive constant added to the variance for numerical stability.
-
Returns:
-
A
Tensorwith the same shape and dtype asinput. -
Raises:
-
ValueError – If
gammaorbetadoes not match the last dimension ofinput, or ifepsilonis not positive. -
Return type:
lazy()
max.experimental.functional.lazy()
Defers tensor realization until explicitly awaited.
-
Return type:
-
Generator[None]
log()
max.experimental.functional.log(x)
Computes the natural logarithm of a tensor element-wise.
This applies log(x). It is the inverse of the exponential
function x = e^y, where e is Euler’s number.
Note that log(x) is undefined for x <= 0 and complex numbers
are not currently supported.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 2.718, 7.389, 20.0])
result = F.log(x)
# result is approximately [0.0, 1.0, 2.0, 2.996]log1p()
max.experimental.functional.log1p(x)
Computes log(1 + x) element-wise.
Note that log(1 + x) is undefined for x <= -1 and complex
numbers are not currently supported.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([0.0, 1e-7, 1.0])
result = F.log1p(x)
# result is approximately [0.0, 1e-7, 0.693]logical_and()
max.experimental.functional.logical_and(lhs, rhs)
Computes the element-wise logical AND of two boolean tensors.
Only supports boolean inputs.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
a = Tensor([True, True, False], dtype=DType.bool)
b = Tensor([True, False, False], dtype=DType.bool)
result = F.logical_and(a, b)
# result is [True, False, False]logical_not()
max.experimental.functional.logical_not(x)
Computes the element-wise logical NOT of a boolean tensor.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
x = Tensor([True, False, True], dtype=DType.bool)
result = F.logical_not(x)
# result is [False, True, False]logical_or()
max.experimental.functional.logical_or(lhs, rhs)
Computes the element-wise logical OR of two boolean tensors.
Only supports boolean inputs.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
a = Tensor([True, True, False], dtype=DType.bool)
b = Tensor([True, False, False], dtype=DType.bool)
result = F.logical_or(a, b)
# result is [True, True, False]logical_xor()
max.experimental.functional.logical_xor(lhs, rhs)
Computes the element-wise logical XOR of two boolean tensors.
Only supports boolean inputs.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
a = Tensor([True, True, False], dtype=DType.bool)
b = Tensor([True, False, False], dtype=DType.bool)
result = F.logical_xor(a, b)
# result is [False, True, False]logsoftmax()
max.experimental.functional.logsoftmax(value, axis=-1)
Computes the log-softmax of a tensor along an axis.
-
Parameters:
-
Returns:
-
A
Tensorof the same shape and dtype asvaluecontaining the log-softmax ofvaluecomputed alongaxis. -
Return type:
map_tensors()
max.experimental.functional.map_tensors(fn, args)
Applies fn to every Tensor leaf in args.
Recurses into list and tuple containers; non-tensor leaves
pass through unchanged.
masked_scatter()
max.experimental.functional.masked_scatter(input, mask, updates, out_dim)
Updates tensor values at positions where mask is true.
Positions are filled in row-major order, so the first True position in
mask takes the first element of updates, and so on.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
x = Tensor([[1, 2], [3, 4]], dtype=DType.int32)
mask = Tensor([[True, False], [False, True]], dtype=DType.bool)
updates = Tensor([10, 20], dtype=DType.int32)
# Write into the True positions, producing [[10, 2], [3, 20]].
result = F.masked_scatter(x, mask, updates, out_dim="num_updates")
# result is [[10, 2], [3, 20]]-
Parameters:
-
- input (Tensor) – The input tensor to write elements to.
- mask (Tensor) – A tensor selecting the positions to write, broadcast to the shape
of
input. Pass a boolean tensor. A weak Python value is converted to boolean, but an existing tensor is used unchanged. - updates (Tensor) – A tensor of elements to write to
input. - out_dim (int | str | Dim | integer | TypedAttr) – The new data-dependent dimension for the number of
Truepositions inmask.
-
Returns:
-
A
Tensorcontaininginputwithupdateswritten wheremaskis true. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputandupdateshave mismatched dtypes, or if the inputs aren’t all on the same device. -
Return type:
matmul()
max.experimental.functional.matmul(lhs, rhs)
Performs matrix multiplication between two tensors.
Treats the innermost two dimensions of each input as a matrix: lhs
of shape (..., M, K) and rhs of shape (..., K, N) produce
an output of shape (..., M, N). The K dimensions must match.
Any outer batch dimensions are broadcast.
When inputs are distributed across devices, the operation is sharded according to the matmul sharding rule.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([[1.0, 2.0], [3.0, 4.0]])
b = Tensor([[5.0, 6.0], [7.0, 8.0]])
result = F.matmul(a, b)
# result has shape (2, 2):
# [[19.0, 22.0], [43.0, 50.0]]
# The ``@`` operator on Tensor also calls matmul.
result = a @ bmax()
max.experimental.functional.max(x, y=None, /, axis=-1)
Computes the maximum of a tensor, or the element-wise maximum of two tensors.
Called with one argument, reduces x along axis. Called with two
tensor arguments, returns their element-wise maximum.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.2, 3.5, 2.1, 0.8], [2.3, 1.9, 4.2, 3.1]])
row_max = F.max(x, axis=-1)
# row_max has shape (2, 1): [[3.5], [4.2]]
col_max = F.max(x, axis=0)
# col_max has shape (1, 4): [[2.3, 3.5, 4.2, 3.1]]
y = Tensor([[2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0]])
element_wise = F.max(x, y)
# element_wise: [[2.0, 3.5, 2.1, 2.0], [2.3, 2.0, 4.2, 3.1]]-
Parameters:
-
Returns:
-
A
Tensorcontaining either the reduced maximum alongaxisor the element-wise maximum with the broadcast shape of the inputs. -
Raises:
-
ValueError – If
axisis out of range forxwhen reducing. -
Return type:
max_pool2d()
max.experimental.functional.max_pool2d(input, kernel_size, stride=1, dilation=1, padding=0, ceil_mode=False)
Applies 2D max pooling to a tensor.
Slides a window of size kernel_size over the spatial dimensions and
replaces each window with its maximum value.
-
Parameters:
-
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
(batch_size, height, width, channels). - kernel_size (tuple[DimLike, DimLike]) – A tuple
(kernel_h, kernel_w)giving the height and width of the sliding window. - stride (int | tuple[int, int]) – The stride of the sliding window. Either a single
intapplied to both spatial dimensions, or a tuple(stride_h, stride_w). Defaults to1. - dilation (int | tuple[int, int]) – The spacing between kernel elements. Either a single
intapplied to both spatial dimensions, or a tuple(dilation_h, dilation_w). Defaults to1. - padding (int | tuple[int, int]) – Padding added to both sides of each spatial dimension.
Out-of-bounds positions are excluded from the maximum (equivalently,
they use the dtype’s minimum value or negative infinity), so padding
cannot win over negative input values. Either a single
intapplied to both spatial dimensions, or a tuple(pad_h, pad_w). Defaults to0. - ceil_mode (bool) – When
True, uses ceil instead of floor when computing the output spatial shape. Defaults toFalse.
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
-
Returns:
-
A
Tensorcontaining the max-pooled values, with shape(batch_size, height_out, width_out, channels). -
Return type:
mean()
max.experimental.functional.mean(x, axis=-1)
Computes the mean of elements along a specified axis.
-
Parameters:
-
Returns:
-
A
Tensorcontaining the mean alongaxis. For an integeraxis, it has the same rank asxwith theaxisdimension reduced to size1. WhenaxisisNone, the result has shape(1,). -
Raises:
-
ValueError – If
axisis out of range forx. -
Return type:
min()
max.experimental.functional.min(x, y=None, /, axis=-1)
Computes the minimum of a tensor, or the element-wise minimum of two tensors.
Called with one argument, reduces x along axis. Called with two
tensor arguments, returns their element-wise minimum.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1.2, 3.5, 2.1, 0.8], [2.3, 1.9, 4.2, 3.1]])
row_min = F.min(x, axis=-1)
# row_min has shape (2, 1): [[0.8], [1.9]]
col_min = F.min(x, axis=0)
# col_min has shape (1, 4): [[1.2, 1.9, 2.1, 0.8]]
y = Tensor([[2.0, 2.0, 2.0, 2.0], [2.0, 2.0, 2.0, 2.0]])
element_wise = F.min(x, y)
# element_wise: [[1.2, 2.0, 2.0, 0.8], [2.0, 1.9, 2.0, 2.0]]-
Parameters:
-
Returns:
-
A
Tensorcontaining either the reduced minimum alongaxisor the element-wise minimum with the broadcast shape of the inputs. -
Raises:
-
ValueError – If
axisis out of range forxwhen reducing. -
Return type:
mod()
max.experimental.functional.mod(lhs, rhs)
Computes the element-wise modulus of two tensors.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([7.0, 10.0, 15.0])
b = Tensor([3.0, 4.0, 6.0])
result = F.mod(a, b)
# result is [1.0, 2.0, 3.0]mul()
max.experimental.functional.mul(lhs, rhs)
Multiplies two tensors element-wise.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 2.0, 3.0])
b = Tensor([4.0, 5.0, 6.0])
result = F.mul(a, b)
# result is [4.0, 10.0, 18.0]negate()
max.experimental.functional.negate(x)
Negates a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-1.0, 0.0, 2.0])
result = F.negate(x)
# result is [1.0, 0.0, -2.0]non_maximum_suppression()
max.experimental.functional.non_maximum_suppression(boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold, out_dim='num_selected')
Filters boxes with high intersection-over-union (IoU).
Applies greedy non-maximum suppression independently per (batch, class) pair. For each pair, the algorithm:
- Discards boxes whose score is at or below
score_threshold. - Sorts the remaining boxes by score in descending order.
- Greedily selects boxes, suppressing any later candidate whose IoU with
an already-selected box exceeds
iou_threshold. - Stops after
max_output_boxes_per_classselections per pair.
Boxes use (y1, x1, y2, x2) corner format. Coordinates may be normalized
or absolute, since the op handles both. All inputs must be on CPU.
from max.driver import CPU
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
device = CPU()
# boxes: (batch, num_boxes, 4); scores: (batch, num_classes, num_boxes).
boxes = Tensor.ones((1, 3, 4), dtype=DType.float32, device=device)
scores = Tensor.ones((1, 1, 3), dtype=DType.float32, device=device)
# Each output row is (batch_index, class_index, box_index), with a
# data-dependent number of rows.
result = F.non_maximum_suppression(
boxes,
scores,
max_output_boxes_per_class=Tensor(2, dtype=DType.int64, device=device),
iou_threshold=Tensor(0.5, dtype=DType.float32, device=device),
score_threshold=Tensor(0.0, dtype=DType.float32, device=device),
)
# result has shape (num_selected, 3)-
Parameters:
-
- boxes (Tensor) – The input boxes tensor of shape
(batch_size, num_boxes, 4), with a float dtype. - scores (Tensor) – The per-class scores of shape
(batch_size, num_classes, num_boxes), with the same dtype asboxes. - max_output_boxes_per_class (Tensor) – A scalar
int64tensor giving the maximum number of boxes to select per (batch, class) pair. - iou_threshold (Tensor) – A scalar float tensor giving the IoU suppression threshold.
- score_threshold (Tensor) – A scalar float tensor giving the minimum score to consider.
- out_dim (str) – The name for the dynamic output dimension, which is the number
of selected boxes. Defaults to
"num_selected".
- boxes (Tensor) – The input boxes tensor of shape
-
Returns:
-
A
Tensorcontaining the selected boxes, with shape(out_dim, 3)andint64dtype. Each row is(batch_index, class_index, box_index). -
Return type:
nonzero()
max.experimental.functional.nonzero(x, out_dim)
Returns the indices of all nonzero elements of a tensor.
Each row is the multi-index of one nonzero element, and the rows are generated in row-major order.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[0, 1], [2, 0]])
# Nonzero elements at (0, 1) and (1, 0) produce [[0, 1], [1, 0]].
result = F.nonzero(x, out_dim="nonzero")
# result is [[0, 1], [1, 0]]-
Parameters:
-
Returns:
-
A
Tensorcontaining the indices of the nonzero elements ofx, with shape[out_dim, x.rank]andint64dtype. -
Raises:
-
ValueError – If
xis scalar, or ifxis on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. -
Return type:
normal()
max.experimental.functional.normal(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Samples values from a Gaussian (normal) distribution with the given mean and standard deviation.
When device is a
DeviceMapping, each Sharded
axis draws an independent stream while shards on Replicated axes
draw identical values.
from max.experimental import functional as F
result = F.gaussian((2, 3), mean=0.0, std=1.0)
# result is a (2, 3) tensor sampled from a standard normal distribution.-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- mean (float) – The mean of the distribution. Defaults to
0.0. - std (float) – The standard deviation of the distribution. Defaults to
1.0. - dtype (DType | None) – The data type of the tensor.
- device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement.
-
Returns:
-
A
Tensorof the requested shape, dtype, and placement with values sampled fromNormal(mean, std**2). -
Return type:
normal_like()
max.experimental.functional.normal_like(like, mean=0.0, std=1.0)
Samples Gaussian values matching another tensor’s shape and dtype.
-
Parameters:
-
- like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
- mean (float) – The mean of the distribution. Defaults to
0.0. - std (float) – The standard deviation of the distribution. Defaults to
1.0.
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with values sampled fromNormal(mean, std**2). -
Return type:
not_equal()
max.experimental.functional.not_equal(lhs, rhs)
Tests element-wise inequality between two tensors.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([1.0, 2.0, 3.0])
b = Tensor([1.0, 5.0, 3.0])
result = F.not_equal(a, b)
# result is [False, True, False]ones()
max.experimental.functional.ones(shape, *, dtype=None, device=None)
Creates a tensor filled with ones.
-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- dtype (DType | None) – The data type. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement.
-
Returns:
-
A tensor of the requested shape, dtype, and placement with every element set to
1. -
Return type:
ones_like()
max.experimental.functional.ones_like(like)
Creates a tensor filled with ones, matching another tensor’s shape and dtype.
-
Parameters:
-
like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with every element set to1. -
Return type:
outer()
max.experimental.functional.outer(lhs, rhs)
Computes the outer product of two vectors.
from max.experimental import Tensor
from max.experimental import functional as F
lhs = Tensor([1.0, 2.0, 3.0])
rhs = Tensor([4.0, 5.0])
# Outer product, producing [[4, 5], [8, 10], [12, 15]].
result = F.outer(lhs, rhs)
# result has shape (3, 2)-
Parameters:
-
Returns:
-
A
Tensorcontaining the outer product of the two input vectors. It has rank 2, with dimension sizes equal to the number of elements oflhsandrhsrespectively. -
Raises:
-
ValueError – If
lhsorrhsis not rank 1. -
Return type:
pad()
max.experimental.functional.pad(input, paddings, mode='constant', value=0)
Pads a tensor along every dimension.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1, 2], [3, 4]])
# Pad one element before and after each dimension.
result = F.pad(x, [1, 1, 1, 1])
# [[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]-
Parameters:
-
-
input (Tensor) – The tensor to pad.
-
paddings (Iterable[int]) – The amount to pad. For a tensor of rank
N, pass2*Nnon-negative integers in the order[before_dim0, after_dim0, before_dim1, after_dim1, ...]. -
mode (Literal['constant', 'reflect', 'edge']) –
How to fill the padded cells. Supported values:
"constant": fill usingvalue."reflect": reflect the content across each edge, excluding the boundary element (likenumpy.padwithmode='reflect')."edge": repeat the nearest boundary element (likenumpy.padwithmode='edge').
-
value (Tensor) – The fill value for
mode="constant". Defaults to0.
-
-
Returns:
-
A
Tensorcontaining the padded input, with the same dtype asinput. -
Raises:
-
- ValueError – If
modeis unsupported, or any padding value is negative. - AssertionError – If the number of padding values isn’t twice the input rank.
- ValueError – If
-
Return type:
per_shard_dispatch()
max.experimental.functional.per_shard_dispatch(graph_op, args, output_mappings)
Runs graph_op once per shard and reassembles distributed outputs.
permute()
max.experimental.functional.permute(x, dims)
Permutes all dimensions of a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (1, 2, 3).
x = Tensor.ones([1, 2, 3])
# Reorder the dimensions to (2, 0, 1), producing shape (3, 1, 2).
result = F.permute(x, [2, 0, 1])-
Parameters:
-
Returns:
-
A
Tensorcontainingxwith its dimensions reordered to matchdims. It has the same elements and dtype asx, with the order of the elements changed according to the permutation. -
Raises:
-
- ValueError – If the length of
dimsdoes not match the rank of the input, or ifdimscontains duplicate dimensions. - IndexError – If any dimension in
dimsis out of range.
- ValueError – If the length of
-
Return type:
pow()
max.experimental.functional.pow(lhs, rhs)
Raises elements of one tensor to the power of another element-wise.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([2.0, 3.0, 4.0])
b = Tensor([3.0, 2.0, 0.5])
result = F.pow(a, b)
# result is [8.0, 9.0, 2.0]print()
max.experimental.functional.print(value, name)
Prints a tensor to the console.
prod()
max.experimental.functional.prod(x, axis=-1)
Computes the product of elements along a specified axis.
-
Parameters:
-
Returns:
-
A
Tensorcontaining the product alongaxis. For an integeraxis, it has the same rank asxwith theaxisdimension reduced to size1. WhenaxisisNone, the result has shape(1,). -
Raises:
-
ValueError – If
axisis out of range forx. -
Return type:
qmatmul()
max.experimental.functional.qmatmul(encoding, config, lhs, *rhs)
Performs matrix multiplication between floating point and quantized tensors.
Quantizes the lhs floating point value to match the encoding of the
rhs quantized value, performs the matmul, and then dequantizes the
result. Compared to a regular matmul op, this one expects the rhs value
to be transposed. For example, if the lhs shape is [32, 64] and the
quantized rhs shape is also [32, 64], then the output shape is
[32, 32]. That is, this function returns the result from:
dequantize(quantize(lhs) @ transpose(rhs))The last two dimensions in lhs are treated as matrices and multiplied
by rhs (which must be a 2-D tensor). Any remaining dimensions in
lhs are broadcast dimensions.
-
Parameters:
-
- encoding (QuantizationEncoding) – The quantization encoding to use.
- config (QuantizationConfig | None) – The quantization config. Pass
Nonefor Vroom encodings; a supported configuration is required for GPTQ. - lhs (TensorValue) – The non-quantized, left-hand side of the matmul.
- rhs (TensorValue) – The transposed and quantized right-hand side tensor(s).
-
Returns:
-
A
Tensorcontaining the dequantized, floating point result. -
Raises:
-
- ValueError – If
encodingis not a supported quantization encoding. - TypeError – If
lhsorrhshas an unsupported dtype or rank. - AssertionError – If GPTQ is selected without a configuration.
- ValueError – If
-
Return type:
range()
max.experimental.functional.range(start, stop, step=1, out_dim=None, *, dtype=None, device=None)
Creates a sequence of evenly spaced values from start to stop.
The sequence begins at start and increments by step, stopping
before stop (the upper bound is exclusive).
stop - start must be zero or have the same sign as step.
Currently, graph compilation fails when stop - start isn’t evenly
divisible by step. For example, range(0, 5, 2) should produce
three values, [0, 2, 4], but shape inference declares an output length
of 2. The generated values therefore don’t fit the declared output shape.
from max.dtype import DType
from max.experimental import functional as F
result = F.range(0, 5, 1, dtype=DType.float32)
# result holds [0.0, 1.0, 2.0, 3.0, 4.0].-
Parameters:
-
- start (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The first value in the sequence. Must be a scalar value.
- stop (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The exclusive upper bound. The sequence stops before this value. Must be a scalar value.
- step (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | DLPackArray) – The spacing between consecutive values. Must be non-zero.
Defaults to
1. - out_dim (int | str | Dim | integer | TypedAttr | None) – The expected length of the output. Required when dynamic scalar tensor inputs prevent static length inference. When omitted, it’s computed from scalar literals.
- dtype (DType | None) – The element type of the result tensor. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMapping. Sharded placement is not supported.
-
Returns:
-
A
Tensorcontaining the generated sequence. -
Raises:
-
- ValueError – If
out_dimis omitted for dynamic scalar inputs, if any input isn’t scalar, if any input isn’t on the CPU, or ifdevicerequests a sharded placement. - RuntimeError – If a statically known interval isn’t evenly divisible
by
step, causing the inferred output length to disagree with the number of generated values.
- ValueError – If
-
Return type:
rebind()
max.experimental.functional.rebind(x, shape, message='', layout=None)
Rebinds the symbolic shape of a tensor.
Asserts at runtime that the tensor’s dimensions match the new shape. Useful for narrowing dynamic dimensions to specific sizes when you have external knowledge of their values.
-
Parameters:
-
Returns:
-
A tensor with the same data and the new symbolic shape.
-
Return type:
reduce_scatter()
max.experimental.functional.reduce_scatter(t, scatter_axis=0, mesh_axis=0, *, even=True)
Reduces a tensor across a mesh axis and scatters the result.
Transitions the tensor’s placement on mesh_axis from
Partial to
Sharded. Each device contributes
to the sum and ends up with one shard of the reduced tensor along
scatter_axis.
-
Parameters:
-
Returns:
-
A tensor with the reduced and re-sharded result.
-
Return type:
relu()
max.experimental.functional.relu(x)
Applies the ReLU (Rectified Linear Unit) activation element-wise.
ReLU is defined as relu(x) = max(0, x), meaning negative values are set
to zero while positive values are unchanged.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[-2.0, -1.0, 0.0], [1.0, 2.0, 3.0]])
result = F.relu(x)
# result is [[0.0, 0.0, 0.0], [1.0, 2.0, 3.0]]repeat_interleave()
max.experimental.functional.repeat_interleave(x, repeats, axis=None, out_dim=None)
Repeats each element of a tensor along an axis.
Unlike tile(), which repeats whole blocks, this repeats each
element repeats times consecutively.
This op runs on CPU only; a GPU input raises an error.
The examples below use an input containing [[1.0, 2.0], [3.0, 4.0]]:
from max.experimental import Tensor
from max.experimental import functional as F
from max.driver import CPU
input = Tensor([[1.0, 2.0], [3.0, 4.0]], device=CPU())
# Repeat each row twice.
output = F.repeat_interleave(input, repeats=2, axis=0)
# [[1, 2], [1, 2], [3, 4], [3, 4]], shape (4, 2)
# Repeat each column twice.
output = F.repeat_interleave(input, repeats=2, axis=1)
# [[1, 1, 2, 2], [3, 3, 4, 4]], shape (2, 4)
# With no axis, flatten the input first, then repeat each element.
output = F.repeat_interleave(input, repeats=2)
# [1, 1, 2, 2, 3, 3, 4, 4], shape (8,)-
Parameters:
-
- x (Tensor) – The input tensor.
- repeats (int | TensorValue) – The integer number of times to repeat each element.
- axis (int | None) – The axis to repeat along. If
None(the default), the input is flattened first. - out_dim (int | str | Dim | integer | TypedAttr | None) – The output size along
axis. This is inferred whenrepeatsis an integer.
-
Returns:
-
A
Tensorcontaining the input with its elements interleaved. -
Raises:
-
ValueError – If
repeatsis non-positive, ifaxisis out of range, or if the input is on a GPU device. -
Return type:
reshape()
max.experimental.functional.reshape(x, shape)
Reshapes a tensor.
If a value of -1 is present in shape, that dimension becomes an
automatically calculated dimension collecting all unspecified dimensions.
Its length becomes the number of elements in the original tensor divided by
the product of the other dimensions of shape.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (2, 3).
x = Tensor.ones([2, 3])
# Reshape the same 6 elements into shape (3, 2).
result = F.reshape(x, [3, 2])-
Parameters:
-
Returns:
-
A
Tensorcontainingxwith a newshape. The order and total number of elements stays the same as the input. -
Raises:
-
ValueError – If
shapecontains more than one-1dimension, if a-1dimension is requested while another dimension is0, or if the input and target shapes have a different number of elements. -
Return type:
resize()
max.experimental.functional.resize(input, shape, interpolation=InterpolationMode.BILINEAR)
Resizes a tensor to a given shape using a specified interpolation method.
from max.experimental import Tensor
from max.experimental import functional as F
from max.graph.ops import InterpolationMode
# NCHW input: batch 1, 1 channel, 2x2 spatial.
x = Tensor([[[[1.0, 2.0], [3.0, 4.0]]]])
# Upscale the spatial dimensions to 4x4.
result = F.resize(x, [1, 1, 4, 4], InterpolationMode.BILINEAR)
# result has shape (1, 1, 4, 4)-
Parameters:
-
- input (Tensor) – The input tensor to resize. Must be rank 4 in channels-first
(NCHW) layout,
(batch_size, channels, height, width). - shape (Iterable[int | str | Dim | integer | TypedAttr]) – The desired output shape, of length 4, layout
(batch_size, channels, height, width). - interpolation (InterpolationMode) – The interpolation method, given as an
InterpolationMode. Defaults toBILINEAR.
- input (Tensor) – The input tensor to resize. Must be rank 4 in channels-first
(NCHW) layout,
-
Returns:
-
A
Tensorcontaining the resized tensor with the givenshape. -
Raises:
-
ValueError – If
inputdoesn’t have rank 4, or ifshapehas the wrong number of elements. -
Return type:
resize_bicubic()
max.experimental.functional.resize_bicubic(input, size)
Resizes a tensor using bicubic interpolation.
Produces an output tensor whose dimensions are given by size using a
4x4-pixel Keys/PyTorch (a = -0.75) cubic convolution filter with
half-pixel coordinate mapping.
from max.experimental import Tensor
from max.experimental import functional as F
# NCHW input: batch 1, 1 channel, 2x2 spatial.
x = Tensor([[[[1.0, 2.0], [3.0, 4.0]]]])
# Upscale the spatial dimensions to 4x4.
result = F.resize_bicubic(x, [1, 1, 4, 4])
# result has shape (1, 1, 4, 4)-
Parameters:
-
Returns:
-
A
Tensorcontaining the resized tensor, with shapesizeand the same dtype asinput. -
Raises:
-
ValueError – If
inputdoesn’t have rank 4, or ifsizehas a different length. -
Return type:
resize_linear()
max.experimental.functional.resize_linear(input, size, coordinate_transform_mode=0, antialias=False)
Resizes a tensor using linear (bilinear) interpolation.
Produces an output tensor whose shape is given by size using separable
1-D linear filters. It resizes any dimension whose size changes, including
the batch and channel dimensions. The operation maps output coordinates
back to input coordinates according to coordinate_transform_mode.
from max.experimental import Tensor
from max.experimental import functional as F
# NCHW input: batch 1, 1 channel, 2x2 spatial.
x = Tensor([[[[1.0, 2.0], [3.0, 4.0]]]])
# Upscale the spatial dimensions to 4x4.
result = F.resize_linear(x, [1, 1, 4, 4])
# result has shape (1, 1, 4, 4)-
Parameters:
-
-
input (Tensor) – The input tensor to resize.
-
size (Iterable[int | str | Dim | integer | TypedAttr]) – The desired output shape. Must have the same rank as
input. -
coordinate_transform_mode (int) –
How to map an output coordinate to an input coordinate. Allowed values:
0(half_pixel): Default. Shifts by 0.5 before scaling, consistent with most deep learning frameworks.1(align_corners): Aligns the corner pixels of the input and output so that the first and last coordinates are preserved exactly.2(asymmetric): Applies no shift, mapping each output coordinate tocoordinate / scale.3(half_pixel_1D): Likehalf_pixel, except any axis whose output size is1maps to coordinate0.
-
antialias (bool) – When
True, applies an antialiasing filter when downscaling, which reduces aliasing artifacts by widening the tent filter support by1 / scale. Has no effect when upscaling. Defaults toFalse.
-
-
Returns:
-
A
Tensorcontaining the resized tensor, with shapesizeand the same dtype asinput. -
Raises:
-
ValueError – If
coordinate_transform_modeisn’t 0-3, or ifsizehas a different rank thaninput. -
Return type:
resize_nearest()
max.experimental.functional.resize_nearest(input, size, coordinate_transform_mode=0, round_mode=0)
Resizes a tensor using nearest-neighbor interpolation.
Produces an output tensor whose dimensions are given by size by
selecting the nearest input sample for each output coordinate.
from max.experimental import Tensor
from max.experimental import functional as F
# NCHW input: batch 1, 1 channel, 2x2 spatial.
x = Tensor([[[[1.0, 2.0], [3.0, 4.0]]]])
# Upscale the spatial dimensions to 4x4.
result = F.resize_nearest(x, [1, 1, 4, 4])
# result has shape (1, 1, 4, 4)-
Parameters:
-
-
input (Tensor) – The input tensor to resize.
-
size (Iterable[int | str | Dim | integer | TypedAttr]) – The desired output shape. Must have the same rank as
input. -
coordinate_transform_mode (int) –
How to map an output coordinate to an input coordinate. Allowed values:
0(half_pixel). Default.1(align_corners).2(asymmetric).3(half_pixel_1D).
See
resize_linear()for a description of each mode. -
round_mode (int) –
How to round the mapped coordinate to select the nearest input sample. Allowed values:
0(HalfDown, the default):ceil(x - 0.5).1(HalfUp):floor(x + 0.5).2(Floor):floor(x).3(Ceil):ceil(x).
-
-
Returns:
-
A
Tensorcontaining the resized tensor, with shapesizeand the same dtype asinput. -
Raises:
-
ValueError – If
coordinate_transform_modeisn’t 0-3,round_modeisn’t 0-3, orsizehas a different rank thaninput. -
Return type:
rms_norm()
max.experimental.functional.rms_norm(input, weight, epsilon, weight_offset=0.0, multiply_before_cast=False)
Computes root mean square normalization over the last dimension of input.
The output is input / rms(input) * (weight + weight_offset) where
rms(x) = sqrt(mean(x ** 2) + epsilon). Reduction runs over the last
axis of input and is broadcast back across the leading axes. See
Root Mean Square Layer Normalization for the original formulation.
Two variants are supported through weight_offset and
multiply_before_cast:
- Llama-style (default):
weight_offset=0andmultiply_before_cast=False. The normalized input is cast to the output dtype before multiplication by the weight. - Gemma-style:
weight_offset=1andmultiply_before_cast=True. The weight is treated as1 + weightand multiplication runs in the reduction dtype before casting back.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[3.0, 4.0]])
weight = Tensor([1.0, 1.0])
# Llama-style (default).
y_llama = F.rms_norm(x, weight, epsilon=1e-6)
# Gemma-style treats the weight as 1 + weight.
y_gemma = F.rms_norm(
x, weight, epsilon=1e-6, weight_offset=1.0, multiply_before_cast=True
)-
Parameters:
-
- input (Tensor) – The tensor to normalize. Reduction runs over the last axis.
- weight (Tensor) – The scale applied after normalization. A 1-D tensor whose
shape matches the last dimension of
input. - epsilon (float) – A small positive constant added to the mean of squares for numerical stability.
- weight_offset (float) – A value added to
weightbefore scaling. Use1.0for Gemma-style normalization and0.0otherwise. Defaults to0.0. - multiply_before_cast (bool) – Whether to multiply by the (offset) weight
before casting the normalized input back to the output dtype.
Llama-style sets this to
False. Defaults toFalse.
-
Returns:
-
A
Tensorwith the same shape and dtype asinput. -
Raises:
-
ValueError – If
weightdoes not match the last dimension ofinput. -
Return type:
roi_align()
max.experimental.functional.roi_align(input, rois, output_height, output_width, spatial_scale=1.0, sampling_ratio=0.0, aligned=False, mode='AVG')
Applies ROI-align pooling.
Extracts fixed-size feature maps from regions of interest (ROIs) using bilinear interpolation.
-
Parameters:
-
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
(batch_size, height, width, channels). - rois (Tensor) – The regions of interest with shape
(num_rois, 5), where each row is(batch_index, x1, y1, x2, y2). - output_height (int) – The height of each output feature map.
- output_width (int) – The width of each output feature map.
- spatial_scale (float) – The multiplicative factor mapping ROI coordinates to
input spatial coordinates. Defaults to
1.0. - sampling_ratio (float) – The number of sampling points per bin in each
direction.
0means adaptive (ceil(bin_size)). Defaults to0.0. - aligned (bool) – When
True, applies a half-pixel offset to ROI coordinates for more precise alignment. Defaults toFalse. - mode (str) – The pooling mode, either
"AVG"or"MAX". Defaults to"AVG".
- input (Tensor) – The input tensor in channels-last (NHWC) layout,
-
Returns:
-
A
Tensorcontaining the pooled values, with shape(num_rois, output_height, output_width, channels). -
Raises:
-
ValueError – If
inputisn’t rank 4,roisisn’t rank 2 with 5 columns, ormodeis invalid. -
Return type:
round()
max.experimental.functional.round(x)
Rounds a tensor to the nearest integer element-wise.
Values exactly halfway between two integers round to the nearest even integer
(for example, 2.5 rounds to 2.0 and 3.5 rounds to 4.0). All
other values follow normal rounding to the nearest integer.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([0.5, 1.5, 2.5, -0.5])
result = F.round(x)
# Ties round to the nearest even integer:
# result is [0.0, 2.0, 2.0, 0.0]rsqrt()
max.experimental.functional.rsqrt(x)
Computes the reciprocal square root of a tensor element-wise.
Computes 1 / sqrt(x) for each element.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 4.0, 16.0])
result = F.rsqrt(x)
# result is [1.0, 0.5, 0.25]scatter()
max.experimental.functional.scatter(input, updates, indices, axis=-1)
Writes updates into a copy of input at positions given by indices.
from max.experimental import Tensor
from max.experimental import functional as F
from max.driver import CPU
from max.dtype import DType
x = Tensor([1, 2, 3, 4, 5], dtype=DType.int32, device=CPU())
updates = Tensor([10, 20], dtype=DType.int32, device=CPU())
indices = Tensor([0, 3], dtype=DType.int64, device=CPU())
# Overwrite positions 0 and 3, producing [10, 2, 3, 20, 5].
result = F.scatter(x, updates, indices, axis=0)
# result is [10, 2, 3, 20, 5]-
Parameters:
-
Returns:
-
A
Tensorcontaininginputwithupdateswritten atindices. It has the same shape and dtype asinput. -
Raises:
-
- ValueError – If
axisis out of range, if the input and updates dtypes mismatch, ifindicesdtype is not int32/int64, if the inputs aren’t all on the same device, or if any input is on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. - Error – If
input,updates, andindicesdon’t share the same rank, ifupdatesandindicesdon’t have the same shape, or if anyindicesdimension exceeds the matchinginputdimension.
- ValueError – If
-
Return type:
scatter_add()
max.experimental.functional.scatter_add(input, updates, indices, axis=-1)
Creates a new tensor by accumulating updates into input at indices.
Produces an output tensor by scattering elements from updates into
input according to indices, summing values at duplicate indices. For
a 2-D input with axis=0 the update rule is:
output[indices[i][j]][j] += updates[i][j]and with axis=1:
output[i][indices[i][j]] += updates[i][j]-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
- ValueError – If
axisis out of range, if the input and updates dtypes mismatch, ifindicesdtype is not int32/int64, if the inputs aren’t all on the same device, or if any input is on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. - Error – If
input,updates, andindicesdon’t share the same rank, ifupdatesandindicesdon’t have the same shape, or if anyindicesdimension exceeds the matchinginputdimension.
- ValueError – If
-
Return type:
scatter_max()
max.experimental.functional.scatter_max(input, updates, indices, axis=-1)
Creates a new tensor by scattering the maximum of updates into input.
Produces an output tensor by scattering elements from updates into
input according to indices, keeping the maximum at duplicate indices.
For a 2-D input with axis=0 the update rule is:
output[indices[i][j]][j] = max(output[indices[i][j]][j], updates[i][j])and with axis=1:
output[i][indices[i][j]] = max(output[i][indices[i][j]], updates[i][j])-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
- ValueError – If
axisis out of range, if the input and updates dtypes mismatch, ifindicesdtype is not int32/int64, if the inputs aren’t all on the same device, or if any input is on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. - Error – If
input,updates, andindicesdon’t share the same rank, ifupdatesandindicesdon’t have the same shape, or if anyindicesdimension exceeds the matchinginputdimension.
- ValueError – If
-
Return type:
scatter_min()
max.experimental.functional.scatter_min(input, updates, indices, axis=-1)
Creates a new tensor by scattering the minimum of updates into input.
Produces an output tensor by scattering elements from updates into
input according to indices, keeping the minimum at duplicate indices.
For a 2-D input with axis=0 the update rule is:
output[indices[i][j]][j] = min(output[indices[i][j]][j], updates[i][j])and with axis=1:
output[i][indices[i][j]] = min(output[i][indices[i][j]], updates[i][j])-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
- ValueError – If
axisis out of range, if the input and updates dtypes mismatch, ifindicesdtype is not int32/int64, if the inputs aren’t all on the same device, or if any input is on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. - Error – If
input,updates, andindicesdon’t share the same rank, ifupdatesandindicesdon’t have the same shape, or if anyindicesdimension exceeds the matchinginputdimension.
- ValueError – If
-
Return type:
scatter_mul()
max.experimental.functional.scatter_mul(input, updates, indices, axis=-1)
Creates a new tensor by scattering the product of updates into input.
Produces an output tensor by scattering elements from updates into
input according to indices, multiplying values at duplicate indices.
For a 2-D input with axis=0 the update rule is:
output[indices[i][j]][j] *= updates[i][j]and with axis=1:
output[i][indices[i][j]] *= updates[i][j]-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
- ValueError – If
axisis out of range, if the input and updates dtypes mismatch, ifindicesdtype is not int32/int64, if the inputs aren’t all on the same device, or if any input is on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. - Error – If
input,updates, andindicesdon’t share the same rank, ifupdatesandindicesdon’t have the same shape, or if anyindicesdimension exceeds the matchinginputdimension.
- ValueError – If
-
Return type:
scatter_nd()
max.experimental.functional.scatter_nd(input, updates, indices)
Scatters slices from updates into a copy of input at N-dimensional indices.
The last dimension of indices is the index vector. Its values select a
slice (or scalar) in input. When the index vector length k is less
than input.rank, each update writes a whole slice of the trailing
input.rank - k dimensions.
from max.experimental import Tensor
from max.experimental import functional as F
from max.driver import CPU
from max.dtype import DType
x = Tensor(
[[1, 2], [3, 4], [5, 6]], dtype=DType.int32, device=CPU()
)
updates = Tensor(
[[10, 20], [50, 60]], dtype=DType.int32, device=CPU()
)
indices = Tensor([[0], [2]], dtype=DType.int64, device=CPU())
# Overwrite rows 0 and 2, producing [[10, 20], [3, 4], [50, 60]].
result = F.scatter_nd(x, updates, indices)
# result is [[10, 20], [3, 4], [50, 60]]-
Parameters:
-
- input (Tensor) – The input tensor to write elements to.
- updates (Tensor) – A tensor of elements to write to
input, with shapeindices.shape[:-1] + input.shape[k:]. - indices (Tensor) – An
int32orint64tensor specifying where to writeupdates. Its last dimensionkis the index vector length (k <= input.rank) and its leading dimensions may take any shape. Full indexing usesk = input.rankand partial indexing usesk < input.rank.
-
Returns:
-
A
Tensorcontaininginputwithupdatesscattered in. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If dtypes, devices, ranks, or shapes are incompatible, or if
indicesisn’t an integral tensor. -
Return type:
scatter_nd_add()
max.experimental.functional.scatter_nd_add(input, updates, indices)
Creates a new tensor by accumulating updates into input at N-D indices.
Produces an output tensor by scattering slices from updates into a copy
of input according to N-dimensional index vectors, summing values at
duplicate index positions. Each index vector is the last dimension of
indices and selects a slice (or scalar) in input.
Example for input.shape = [4, 2], indices.shape = [3, 1]
(1-D partial indexing, writes whole rows):
output[indices[i, 0], :] += updates[i, :]-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputandupdatesdtypes mismatch, ifindicesdtype isn’t int32 or int64, or if the inputs aren’t all on the same device. -
Return type:
scatter_nd_max()
max.experimental.functional.scatter_nd_max(input, updates, indices)
Creates a new tensor by scattering the maximum of updates into input at N-D indices.
Produces an output tensor by scattering slices from updates into a copy
of input according to N-dimensional index vectors, keeping the maximum at
duplicate index positions. Each index vector is the last dimension of
indices and selects a slice (or scalar) in input.
Example for input.shape = [4, 2], indices.shape = [3, 1]
(1-D partial indexing, writes whole rows):
output[indices[i, 0], :] = max(output[indices[i, 0], :], updates[i, :])-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputandupdatesdtypes mismatch, ifindicesdtype isn’t int32 or int64, or if the inputs aren’t all on the same device. -
Return type:
scatter_nd_min()
max.experimental.functional.scatter_nd_min(input, updates, indices)
Creates a new tensor by scattering the minimum of updates into input at N-D indices.
Produces an output tensor by scattering slices from updates into a copy
of input according to N-dimensional index vectors, keeping the minimum at
duplicate index positions. Each index vector is the last dimension of
indices and selects a slice (or scalar) in input.
Example for input.shape = [4, 2], indices.shape = [3, 1]
(1-D partial indexing, writes whole rows):
output[indices[i, 0], :] = min(output[indices[i, 0], :], updates[i, :])-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputandupdatesdtypes mismatch, ifindicesdtype isn’t int32 or int64, or if the inputs aren’t all on the same device. -
Return type:
scatter_nd_mul()
max.experimental.functional.scatter_nd_mul(input, updates, indices)
Creates a new tensor by scattering the product of updates into input at N-D indices.
Produces an output tensor by scattering slices from updates into a copy
of input according to N-dimensional index vectors, multiplying values at
duplicate index positions. Each index vector is the last dimension of
indices and selects a slice (or scalar) in input.
Example for input.shape = [4, 2], indices.shape = [3, 1]
(1-D partial indexing, writes whole rows):
output[indices[i, 0], :] *= updates[i, :]-
Parameters:
-
Returns:
-
A
Tensorcontaining the updated tensor. It has the same shape and dtype asinput. -
Raises:
-
ValueError – If
inputandupdatesdtypes mismatch, ifindicesdtype isn’t int32 or int64, or if the inputs aren’t all on the same device. -
Return type:
sigmoid()
max.experimental.functional.sigmoid(x)
Applies the sigmoid activation function element-wise.
Computes sigmoid(x) = 1 / (1 + exp(-x)), mapping all values to the
range (0, 1).
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[-2.0, -1.0, 0.0], [1.0, 2.0, 3.0]])
result = F.sigmoid(x)
# result is approximately:
# [[0.119, 0.269, 0.5], [0.731, 0.881, 0.953]]silu()
max.experimental.functional.silu(x)
Applies the SiLU (Swish) activation function element-wise.
Computes silu(x) = x * sigmoid(x).
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([-1.0, 0.0, 1.0, 2.0])
result = F.silu(x)
# result is approximately [-0.269, 0.0, 0.731, 1.762]sin()
max.experimental.functional.sin(x)
Computes the sine of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([0.0, 0.5, 1.0])
result = F.sin(x)
# result is approximately [0.0, 0.479, 0.841]slice_tensor()
max.experimental.functional.slice_tensor(x, indices)
Slices out a subtensor of the input tensor based on indices.
The semantics of slice_tensor() follow basic NumPy slicing
semantics, with one index per dimension. Each index is one of:
- An integer.
- A scalar tensor (a dynamic integer index).
- A
slice. - A
(slice, out_dim)tuple, which names the output dimension when slicing a dynamic dimension. None(to insert a size-1 dimension).Ellipsis(to fill in full slices for the remaining dimensions).
Slice indices must stay within [-dim, dim], and slice steps must be
positive.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Take rows 0 and 1 and columns 1 and 2, producing [[2, 3], [5, 6]].
result = F.slice_tensor(x, [slice(0, 2), slice(1, 3)])
# result is [[2, 3], [5, 6]]-
Parameters:
-
- x (TensorValue) – The input tensor to slice.
- indices (SliceIndices) – The per-dimension index expressions. Each entry is an integer,
a scalar tensor, a
slice, a(slice, out_dim)tuple,None, orEllipsis.
-
Returns:
-
A
Tensorcontaining the sliced subtensor ofx. -
Raises:
-
- IndexError – If a slice bound or integer index is out of range for its dimension.
- ValueError – If
xis a scalar, if more indices than dimensions are given, if more than oneEllipsisappears, or if a slice step is0. - NotImplementedError – If a plain
slicetargets a dynamic dimension. Pass a(slice, out_dim)tuple instead.
-
Return type:
softmax()
max.experimental.functional.softmax(value, axis=-1)
Computes the softmax of a tensor along an axis.
Normalizes the values along axis so that they sum to 1, with each
output element representing the exponentiated input divided by the sum of
exponentiated values along that axis.
-
Parameters:
-
Returns:
-
A
Tensorof the same shape and dtype asvaluecontaining the softmax ofvaluecomputed alongaxis. -
Return type:
split()
max.experimental.functional.split(x, split_size_or_sections, axis=0)
Splits a tensor into chunks along an axis.
An int split_size_or_sections produces equal chunks (the
last may be smaller); a sequence specifies per-chunk sizes.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
first, second = F.split(x, [2, 4], axis=0)
# first is [1.0, 2.0]
# second is [3.0, 4.0, 5.0, 6.0]-
Parameters:
-
Returns:
-
A list of tensors in their original order along
axis. -
Raises:
-
- TypeError – If an integer chunk size is used for a non-static axis.
- ValueError – If a section size is negative or explicit section sizes don’t sum to the input size.
- IndexError – If
axisis out of range.
-
Return type:
sqrt()
max.experimental.functional.sqrt(x)
Computes the square root of a tensor element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 4.0, 9.0, 16.0])
result = F.sqrt(x)
# result is [1.0, 2.0, 3.0, 4.0]squeeze()
max.experimental.functional.squeeze(x, axis)
Removes a dimension of size 1 from a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (2, 1, 3).
x = Tensor.ones([2, 1, 3])
# Remove the size-1 dimension at axis 1, producing shape (2, 3).
result = F.squeeze(x, 1)-
Parameters:
-
Returns:
-
A
Tensorcontainingxwith the dimension ataxisremoved. That dimension size must equal1, so the result holds the same elements asxwith one fewer dimension. -
Raises:
-
- ValueError – If the dimension at
axisdoes not have size1. - IndexError – If
axisis out of range, including for a rank-zero input.
- ValueError – If the dimension at
-
Return type:
stack()
max.experimental.functional.stack(values, axis=0)
Stacks tensors along a new axis.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([[1, 2], [3, 4]])
b = Tensor([[5, 6], [7, 8]])
# Stack the two (2, 2) tensors into one (2, 2, 2) tensor
result = F.stack([a, b], axis=0)
# result has shape (2, 2, 2)-
Parameters:
-
Returns:
-
A
Tensorcontaining the stacked inputs. It has one more dimension than the inputs, and the new dimension has sizelen(values). -
Raises:
-
- ValueError – If
valuesis empty, or if the tensors don’t all have the same dtype, rank, shape, and device. - IndexError – If
axisis out of range.
- ValueError – If
-
Return type:
sub()
max.experimental.functional.sub(lhs, rhs)
Subtracts two tensors element-wise.
Either operand may be a Python int or float scalar, which is
automatically promoted to a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
a = Tensor([10.0, 20.0, 30.0])
b = Tensor([1.0, 2.0, 3.0])
result = F.sub(a, b)
# result is [9.0, 18.0, 27.0]sum()
max.experimental.functional.sum(x, axis=-1)
Computes the sum of elements along a specified axis.
-
Parameters:
-
Returns:
-
A
Tensorcontaining the sum alongaxis. For an integeraxis, it has the same rank asxwith theaxisdimension reduced to size1. WhenaxisisNone, the result has shape(1,). -
Raises:
-
ValueError – If
axisis out of range forx. -
Return type:
tanh()
max.experimental.functional.tanh(x)
Computes the hyperbolic tangent of a tensor element-wise.
This applies tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)), which
maps all values to the range (-1, 1).
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[-2.0, -1.0, 0.0], [1.0, 2.0, 3.0]])
result = F.tanh(x)
# result is approximately:
# [[-0.964, -0.762, 0.0], [0.762, 0.964, 0.995]]tensor_to_layout()
max.experimental.functional.tensor_to_layout(t)
Converts a Tensor to a TensorLayout for sharding-rule evaluation.
t.shape already carries per-device cells on Sharded axes
(via PerShardDim), so the rules that fold per-rank cells
(notably reshape_rule) can do the correct shape arithmetic
directly. Non-distributed tensors fall back to a plain Shape.
-
Parameters:
-
t (Tensor)
-
Return type:
tile()
max.experimental.functional.tile(x, repeats)
Repeats a tensor along each of its dimensions.
Each dimension i is copied repeats[i] times, so its output size is
x.shape[i] * repeats[i].
This op runs on CPU. An input on another device is copied to CPU for the operation and the result is copied back.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([[1, 2], [3, 4]])
# Repeat the columns twice, leaving the rows unchanged.
result = F.tile(x, [1, 2])
# [[1, 2, 1, 2], [3, 4, 3, 4]]-
Parameters:
-
Returns:
-
A
Tensorcontaining the tiled input. -
Raises:
-
ValueError – If
repeatsdoesn’t have one value per dimension, if any statically known value isn’t positive, or ifxis on a non-CPU device andstrict_device_placement=DevicePlacementPolicy.Error. -
Return type:
to_tensors()
max.experimental.functional.to_tensors(values)
Converts graph op results to Tensor, preserving container type.
Recurses one level into list and tuple containers; unknown
types pass through unchanged. Returns Tensor for Buffer and
TensorValue leaves, and a same-shape container for list/tuple
inputs (each leaf converted independently). Any reflects that
leaves change type while the container type is preserved.
top_k()
max.experimental.functional.top_k(input, k, axis=-1)
Returns the k largest values along an axis with their indices.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.0, 3.0, 2.0, 5.0, 4.0])
values, indices = F.top_k(x, k=2, axis=-1)
# values is [5, 4] and indices is [3, 4]-
Parameters:
-
Returns:
-
A tuple of two
Tensorobjects. The first holds the topkvalues alongaxis, and the second holds theirint64indices ininput. Both tensors have the shape ofinputwith theaxisdimension reduced to sizek. -
Return type:
transfer_to()
max.experimental.functional.transfer_to(t, target)
Moves a tensor to a target device or device mapping.
Handles every kind of placement transition: single-device transfers, scattering an unsharded tensor onto a mesh, redistributing across placements on the same mesh, and gathering then re-distributing across different meshes.
-
Parameters:
-
Returns:
-
A tensor with the requested placement on the target device or mesh.
-
Return type:
transpose()
max.experimental.functional.transpose(x, axis_1, axis_2)
Transposes two axes of a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (2, 3).
x = Tensor.ones([2, 3])
# Swap axes 0 and 1, producing shape (3, 2).
result = F.transpose(x, 0, 1)-
Parameters:
-
Returns:
-
A
Tensorcontaining the input withaxis_1andaxis_2transposed. It has the same elements and dtype asx, with the order of the elements changed according to the transposition. For a rank-zero tensor, axes-1and0are accepted and the scalar is returned unchanged. -
Raises:
-
IndexError – If
axis_1oraxis_2is out of range. -
Return type:
trunc()
max.experimental.functional.trunc(x)
Truncates a tensor toward zero element-wise.
from max.experimental import Tensor
from max.experimental import functional as F
x = Tensor([1.5, 2.7, -1.5, -2.7])
result = F.trunc(x)
# result is [1.0, 2.0, -1.0, -2.0]uniform()
max.experimental.functional.uniform(shape=(), range=(0, 1), *, dtype=None, device=None)
Samples values uniformly from the half-open interval [range[0], range[1]).
Values satisfy range[0] <= x < range[1]. When device is a
DeviceMapping, each Sharded
axis draws an independent stream while shards on Replicated axes
draw identical values.
from max.experimental import functional as F
result = F.uniform((2, 3), range=(0.0, 1.0))
# result is a (2, 3) tensor sampled uniformly from [0.0, 1.0).-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- range (tuple[float, float]) – A
(low, high)pair giving the half-open interval to sample from. Defaults to(0, 1). - dtype (DType | None) – The data type of the tensor.
- device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement.
-
Returns:
-
A
Tensorof the requested shape, dtype, and placement with values sampled uniformly from[range[0], range[1]). -
Return type:
uniform_like()
max.experimental.functional.uniform_like(like, range=(0, 1))
Samples uniform values matching another tensor’s shape and dtype.
-
Parameters:
-
- like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
- range (tuple[float, float]) – A
(low, high)pair giving the half-open interval to sample from. Defaults to(0, 1).
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with values sampled uniformly from[range[0], range[1]). -
Return type:
unsqueeze()
max.experimental.functional.unsqueeze(x, axis)
Inserts a dimension of size 1 into a tensor.
from max.experimental import Tensor
from max.experimental import functional as F
# x has shape (3,).
x = Tensor.ones([3])
# Insert a size-1 dimension at axis 0, producing shape (1, 3).
result = F.unsqueeze(x, 0)-
Parameters:
-
- x (Tensor) – The input tensor to unsqueeze.
- axis (int) – The index at which to insert a new dimension into the input’s
shape. Elements at that index or higher are shifted back. If
negative, it indexes relative to
1plus the rank of the tensor. For example, a value of-1adds a new dimension at the end, and-2inserts the dimension immediately before the last dimension.
-
Returns:
-
A
Tensorcontainingxwith a new dimension inserted ataxis. That dimension has a size of1, so the result holds the same elements asxwith one more dimension. -
Raises:
-
ValueError – If
axisis out of bounds. -
Return type:
where()
max.experimental.functional.where(cond, x, y)
Selects elements from two tensors element-wise based on a condition.
At each position, takes the element from x where cond is true and
the element from y where it’s false. Scalar x or y operands are
promoted to tensors, and the inputs are broadcast to a common shape.
from max.experimental import Tensor
from max.experimental import functional as F
from max.dtype import DType
cond = Tensor([True, False, True], dtype=DType.bool)
x = Tensor([1, 2, 3], dtype=DType.int32)
y = Tensor([10, 20, 30], dtype=DType.int32)
# Take x where True and y where False, producing [1, 20, 3].
result = F.where(cond, x, y)
# result is [1, 20, 3]-
Parameters:
-
Returns:
-
A
Tensorcontaining the element-wise selection fromxandyaccording tocond. It has the promoted dtype ofxandy, lives on their shared device, and has the broadcast shape of the inputs. -
Raises:
-
- ValueError – If
conddoesn’t have a boolean dtype, if the inputs aren’t all on the same device, or if the dtypes ofxandycan’t be safely promoted. - Error – If the input shapes aren’t broadcast-compatible.
- ValueError – If
-
Return type:
while_loop()
max.experimental.functional.while_loop(initial_values, predicate, body)
Repeatedly executes a body function while a predicate holds.
Both predicate and body receive and return Tensor
values. They take the same number and types of arguments as the initial
values. The predicate must return a single boolean scalar tensor that
controls loop continuation, and that tensor must reside on CPU; the body
must return updated values matching the types of initial_values.
from max.driver import CPU
from max.dtype import DType
from max.experimental import Tensor
from max.experimental import functional as F
def predicate(x):
return x < 10
def body(x):
return x + 1
x = Tensor(0, dtype=DType.int32, device=CPU())
(result,) = F.while_loop(x, predicate, body)
# Loop continues until ``x >= 10``; result is ``10``.-
Parameters:
-
- initial_values (Iterable[Tensor] | Tensor) – The initial values for the loop arguments. Must be non-empty.
- predicate (Callable[[...], Tensor]) – A callable that takes the loop arguments and returns a
boolean scalar tensor of type
bool. - body (Callable[[...], Tensor | Iterable[Tensor]]) – A callable that takes the loop arguments and returns updated
values matching the types of
initial_values.
-
Returns:
-
The output values from the final loop iteration.
-
Return type:
zeros()
max.experimental.functional.zeros(shape, *, dtype=None, device=None)
Creates a tensor filled with zeros.
-
Parameters:
-
- shape (Iterable[int | str | Dim | integer | TypedAttr]) – The shape of the resulting tensor.
- dtype (DType | None) – The data type. Defaults to
float32on CPU orbfloat16on accelerators. - device (Device | DeviceMapping | DeviceRef | None) – A single device or a
DeviceMappingfor distributed placement.
-
Returns:
-
A tensor of the requested shape, dtype, and placement with every element set to
0. -
Return type:
zeros_like()
max.experimental.functional.zeros_like(like)
Creates a tensor filled with zeros, matching another tensor’s shape and dtype.
-
Parameters:
-
like (Tensor | TensorType | DistributedTensorType) – The template tensor whose shape, dtype, and placement are copied.
-
Returns:
-
A tensor matching the shape, dtype, and placement of
like, with every element set to0. -
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!