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).

Tensor

#include "max/c/tensor.h"

Functions:

M_newTensorSpec()​

M_TensorSpec *M_newTensorSpec(const int64_t *shape, int64_t rankSize, M_Dtype dtype, const char *tensorName, const M_Device *device)

Creates a tensor specification.

You need this in order to set the input tensors with M_borrowTensorInto().

When storing tensor data in memory, we always use a diminishing stride size. That is, earlier dimensions in the shape have larger strides than later dimensions. For example, a C array declared as int arr[1][2][3] would have a shape specified as {1, 2, 3}.

  • Parameters:

    • shape – The shape of the tensor.
    • rankSize – The rank size of the tensor.
    • dtype – The datatype for the tensor.
    • tensorName – The name for the tensor. This string gets copied as part of the operation of M_newTensorSpec, so your original string need not remain valid after the completion of this call.
    • device – The device on which the tensor resides.
  • Returns:

    A pointer to the tensor spec. You are responsible for the memory associated with the pointer returned. The memory can be deallocated by calling M_freeTensorSpec().

M_isDynamicRanked()​

int M_isDynamicRanked(const M_TensorSpec *spec)

Returns if the given spec has a dynamic rank.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    1 if the rank is dynamic. 0 otherwise.

M_getDimAt()​

int64_t M_getDimAt(const M_TensorSpec *spec, size_t axis)

Gets the element at a particular axis.

  • Parameters:

    • spec – The tensor spec.
    • axis – The requested axis
  • Returns:

    The dimension at requested axis if the spec and axis are valid and has static rank. Otherwise, 0. A dimension equaling kDynamicDimensionValue indicates dynamic dimension e.g. batch-size of a model expecting a batched tensor.

M_getRank()​

int64_t M_getRank(const M_TensorSpec *spec)

Gets the rank from the tensor spec.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    The number of dimensions in the tensor spec if the spec is static and valid, kDynamicRankValue if dynamic. Otherwise, 0.

M_getDtype()​

M_Dtype M_getDtype(const M_TensorSpec *spec)

Gets the datatype from the tensor spec.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    The element type from the tensor spec if the tensor spec is valid. Otherwise, M_UNKNOWN.

M_getName()​

const char *M_getName(M_TensorSpec *spec)

Gets the name of the tensor from the tensor spec.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    A null-terminated string containing the name of the tensor if the spec is valid. Otherwise, NULL. The memory associated with the returned string is owned by spec.

M_newAsyncTensorMap()​

M_AsyncTensorMap *M_newAsyncTensorMap(const M_RuntimeContext *context)

Creates a map of tensor names to async tensors.

  • Parameters:

    context – The runtime context.

  • Returns:

    A pointer to the tensor map. You are responsible for the memory associated with the pointer returned. The memory can be deallocated by calling M_freeAsyncTensorMap().

M_borrowTensorInto()​

void M_borrowTensorInto(M_AsyncTensorMap *tensors, void *input, const M_TensorSpec *tensorSpec, M_Status *status)

Adds a tensor to the tensor map.

You are responsible for the lifetime of the input tensor data. Its data gets β€œborrowed” into the Tensor Map.

  • Parameters:

    • tensors – The tensor map, from M_newAsyncTensorMap().
    • input – The input tensor data.
    • tensorSpec – The tensor spec, from M_newTensorSpec(). This gets copied as part of the operation of M_borrowTensorInto, so your original tensorSpec need not exist through the lifetime of the tensor map.
    • status – The status object for reporting errors.

M_getTensorByNameFrom()​

M_AsyncTensor *M_getTensorByNameFrom(M_AsyncTensorMap *tensorMap, const char *name, M_Status *status)

Gets a tensor from the tensor map by name.

  • Parameters:

    • tensorMap – The tensor map.
    • name – The name of the tensor.
    • status – The status object for reporting errors.
  • Returns:

    A pointer to the tensor. You are responsible for the memory associated with the pointer returned. The memory can be deallocated by calling M_freeTensor(). The held tensor inside the return value is simply borrowed from the corresponding input M_AsyncTensorMap. If the tensor map or name are invalid, a NULL pointer is returned and the status parameter contains an error message.

M_getTensorNumElements()​

size_t M_getTensorNumElements(const M_AsyncTensor *tensor)

Gets the number of elements for the tensor.

  • Parameters:

    tensor – The tensor which must not be NULL.

  • Returns:

    The number of elements for the given tensor.

M_getTensorType()​

M_Dtype M_getTensorType(const M_AsyncTensor *tensor)

Gets the corresponding M_Dtype for the tensor.

  • Parameters:

    tensor – The tensor which must not be NULL.

  • Returns:

    The corresponding M_Dtype for the tensor.

M_getTensorData()​

const void *M_getTensorData(const M_AsyncTensor *tensor)

Gets a pointer to underlying data of the tensor.

  • Parameters:

    tensor – The tensor which must not be NULL.

  • Returns:

    A pointer to the underlying data of the tensor. This pointer is valid for the lifetime of the underlying tensor.

M_getTensorSpec()​

M_TensorSpec *M_getTensorSpec(const M_AsyncTensor *tensor)

Gets a Tensor Spec for the tensor.

  • Parameters:

    tensor – The tensor.

  • Returns:

    The tensor spec for the tensor if the tensor is valid. Otherwise, NULL.

M_getDeviceTypeFromSpec()​

M_DeviceType M_getDeviceTypeFromSpec(const M_TensorSpec *spec)

Gets the device type from a tensor specification.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    The device type (CPU or GPU).

M_getDeviceIdFromSpec()​

int M_getDeviceIdFromSpec(const M_TensorSpec *spec)

Gets the device ID from a tensor specification.

  • Parameters:

    spec – The tensor spec.

  • Returns:

    The device ID. Returns 0 if the spec is invalid.

M_getTensorDevice()​

M_Device *M_getTensorDevice(const M_AsyncTensor *tensor)

Gets the device on which a tensor resides.

  • Parameters:

    tensor – The tensor.

  • Returns:

    The device on which the tensor resides, or NULL if the tensor is invalid. The caller owns the returned device and must free it with M_freeDevice().

M_copyTensorToDevice()​

M_AsyncTensor *M_copyTensorToDevice(M_AsyncTensor *tensor, M_Device *device, M_Status *status)

Copies a tensor to a different device.

Creates a copy of the tensor on the specified device.

  • Parameters:

    • tensor – The tensor to copy.
    • device – The target device.
    • status – The status object for reporting errors.
  • Returns:

    A pointer to the tensor on the target device. The caller owns the returned memory and must deallocate it by calling M_freeTensor(). Returns NULL if the operation fails, with an error message in the status.

M_freeTensor()​

void M_freeTensor(M_AsyncTensor *tensor)

Deallocates the memory for the tensor. No-op if tensor is NULL.

  • Parameters:

    tensor – The tensor to deallocate.

M_freeTensorNameArray()​

void M_freeTensorNameArray(M_TensorNameArray *names)

Deallocates the memory for the array of tensor names. No-op if names is NULL.

  • Parameters:

    names – The tensor names to deallocate.

M_freeTensorSpec()​

void M_freeTensorSpec(M_TensorSpec *spec)

Deallocates the memory for the tensor spec. No-op if spec is NULL.

  • Parameters:

    spec – The tensor spec to deallocate.

M_freeAsyncTensorMap()​

void M_freeAsyncTensorMap(M_AsyncTensorMap *tensorMap)

Deallocates the memory for the tensor map. No-op if tensorMap is NULL.

  • Parameters:

    tensorMap – The tensor map to deallocate.