IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
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 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]