Tensor

This is a preview of the Modular Inference Engine. It is not publicly available yet and APIs are subject to change.

If you’re interested, please sign up for early access.

#include "modular/c/tensor.h"

Functions

M_TensorSpec *M_newTensorSpec(const int64_t *shape, size_t rankSize, M_Dtype dtype)

Creates a Tensor Spec.

Parameters:
  • shape – The shape of the tensor.

  • rankSize – The rank size of the tensor.

  • dtype – The datatype for the tensor.

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

int64_t M_getDimAt(const M_TensorSpec *spec, size_t index)

Gets the element at a particular index.

Parameters:
  • spec – The tensor spec.

  • index – The index into the tensor spec.

Returns:

The element at the dimension given by index if the spec and index are valid. Otherwise, 0.

size_t M_getRank(const M_TensorSpec *spec)

Gets the rank in the Tensor Spec.

Parameters:

spec – The tensor spec.

Returns:

The number of dimensions in the tensor spec if the spec is valid. Otherwise, 0.

M_Dtype M_getDtype(const M_TensorSpec *spec)

Gets the datatype of 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_AsyncTensorArray *M_newAsyncTensorArray(const M_RuntimeContext *context, size_t size)

Creates a collection of async tensors.

Parameters:
  • context – The runtime context.

  • size – The size of the collection.

Returns:

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

size_t M_getTensorArrayLength(const M_AsyncTensorArray *tensorArray, M_Status *status)

Gets the size of the Tensor Array.

Parameters:
  • tensorArray – The tensor array.

  • status – The status object for reporting errors.

Returns:

The size of the tensor array if the tensor array is valid. Otherwise, 0 and the status parameter contains an error message.

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

Adds a tensor to the Tensor Array.

You are responsible for the lifetime of the input tensor data. Its data gets “borrowed” into the Tensor Array.

Parameters:
  • tensors – The tensor array.

  • input – The input tensor data.

  • tensorSpec – The tensor spec.

  • status – The status object for reporting errors.

M_AsyncTensor *M_getTensorByIndexFrom(const M_AsyncTensorArray *tensorArray, size_t index, M_Status *status)

Gets a tensor from the Tensor Array by index.

Parameters:
  • tensorArray – The tensor array.

  • index – The index into the tensor array.

  • 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_AsyncTensorArray. If the tensor array or index are invalid, a NULL pointer is returned and the status parameter contains an error message.

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

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

void M_freeTensor(M_AsyncTensor *tensor)

Deallocates the memory for the tensor.

Parameters:

tensor – The tensor to deallocate.

void M_freeTensorSpec(M_TensorSpec *spec)

Deallocates the memory for the Tensor Spec

Parameters:

spec – The tensor spec to deallocate.

void M_freeAsyncTensorArray(M_AsyncTensorArray *tensorArray)

Deallocates the memory for the Tensor Array.

Parameters:

tensorArray – The tensor array to deallocate.