Skip to main content

Python function

defaults_like

defaults_like()

max.experimental.tensor.defaults_like(like)

source

Context manager setting the default dtype and device for tensor creation.

Sets the default data type and device used for tensor creation within the context. All tensors created inside the context block without explicit dtypes or devices will use these parameters.

from max.experimental import tensor
from max.driver import CPU
from max.dtype import DType

x = Tensor.zeros([1], dtype=DType.int32, device=CPU())
with tensor.defaults_like(x):
    y = tensor.Tensor.zeros((2, 3))  # int32, cpu
    z = tensor.Tensor.zeros((2, 3), dtype=DType.float32)  # float32, cpu

Parameters:

like (Tensor | TensorType) – Tensor or tensor type whose dtype and device to use as defaults.

Returns:

A context manager that sets the default dtype and device.

Return type:

Generator[None]