Skip to main content
Log in

Mojo struct

TensorMap

Maps inputs and outputs to their respective names and can be used to supply and receive data to MAX Engine model.

This is the data type returned by Model.execute(), and you can also use this type for the inputs you pass in (although execute() also supports other formats for the input).

Implemented traits

AnyType, CollectionElement, Copyable, Movable, SizedRaising, Stringable

Methods

__init__

__init__(inout self: Self, ctx: CRuntimeContext, lib: DLHandle, owned session: InferenceSession)

Internal only. Use InferenceSession.new_tensor_map for external use.

Creates a new instance of tensor map.

Args:

  • ctx (CRuntimeContext): Context of API.
  • lib (DLHandle): Handle to library.
  • session (InferenceSession): Copy of InferenceSession from which this instance was created.

__init__(inout self: Self, ptr: CTensorMap, lib: DLHandle, owned session: InferenceSession)

Internal only. Use InferenceSession.new_tensor_map for external use.

Creates a new instance of tensor map.

Args:

  • ptr (CTensorMap): C API pointer of TensorMap.
  • lib (DLHandle): Handle to library.
  • session (InferenceSession): Copy of InferenceSession from which this instance was created.

__copyinit__

__copyinit__(inout self: Self, existing: Self)

Copy contructor for TensorMap.

Args:

  • existing (Self): Instance of TensorMap to copy from.

__moveinit__

__moveinit__(inout self: Self, owned existing: Self)

Move contructor for TensorMap.

Args:

  • existing (Self): Instance of TensorMap to move from.

__del__

__del__(owned self: Self)

Destructor for the tensor map.

borrow

borrow[type: DType](self: Self, key: String, value: Tensor[type])

Borrow the given tensor into the map at the key location. User needs to make sure tensor is alive for the duration of map.

Parameters:

  • type (DType): DType of tensor being borrowed.

Args:

  • key (String): Name of tensor in map.
  • value (Tensor[type]): Tensor to be held in map.

borrow[type: DType](self: Self, key: String, spec: TensorSpec, ptr: UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1])

Borrow the given pointer into the map at the key location. User needs to make sure the backing array is alive for the duration of map.

Parameters:

  • type (DType): DType of tensor being borrowed.

Args:

  • key (String): Name of tensor in map.
  • spec (TensorSpec): The tensor spec. This is the standard library TensorSpec.
  • ptr (UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]): The tensor pointer.

borrow(self: Self, key: String, value: EngineTensorView)

Borrow the given tensor view into the map at the key location. User needs to make sure tensor backing the view is alive for the duration of map.

Args:

  • key (String): Name of tensor in map.
  • value (EngineTensorView): View of a tensor.

borrow(self: Self, key: String, value: EngineNumpyView)

Borrow the given numpy view into the map at the key location. User needs to make sure numpy array backing the view is alive for the duration of map.

Args:

  • key (String): Name of numpy array in map.
  • value (EngineNumpyView): View of a numpy array.

borrow(self: Self, key: String, value: Value)

Borrow the given value into the map at the key location.

User needs to make sure value is alive for the duration of the map.

Args:

  • key (String): Name of value in map.
  • value (Value): Value to insert into map.

get

get[type: DType](self: Self, key: String) -> Tensor[$0]

Gets the tensor / numpy array indicated by the key. The value is copied and returned to the user.

Parameters:

  • type (DType): DType of tensor to be returned.

Args:

  • key (String): Name of tensor / numpy array in the map.

Returns:

A copy of the tensor held by the map.

buffer

buffer[type: DType](self: Self, key: String) -> Buffer[$0, -31337, 0]

Gets a buffer to the tensor pointed by the key.

Parameters:

  • type (DType): DType of buffer to be returned.

Args:

  • key (String): Name in TensorMap.

Returns:

Buffer of the tensor pointed by the key.

get_spec

get_spec(self: Self, key: String) -> TensorSpec

Gets the spec of the tensor pointed by the key.

Args:

  • key (String): Name in TensorMap.

Returns:

Buffer of the tensor pointed by the key, as a TensorSpec.

get_value

get_value(self: Self, key: String) -> Value

Gets the value pointed by the key.

Args:

  • key (String): Name in TensorMap.

Returns:

Value pointed by the key.

keys

keys(self: Self) -> List[String, 0]

Returns all held keys.

Returns:

A list with all contained keys.

__len__

__len__(self: Self) -> Int

Gets number of elements in the map.

Returns:

Number of elements the map contains.

__str__

__str__(self: Self) -> String

Returns a String representation of this TensorMap.

Returns:

A textual representation of this TensorMap.

Was this page helpful?