Skip to main content

Tensor Operations

The OrtValue object represents tensors and other ONNX types (maps, sequences, etc.).

Creating Tensors

CreateTensorAsOrtValue

Create a tensor using an allocator. Memory is allocated by ORT. Parameters:
  • allocator: Allocator to use for tensor memory
  • shape: Array of dimension sizes
  • shape_len: Number of dimensions
  • type: Element data type
  • out: Newly created tensor (must be freed with ReleaseValue)
Returns: NULL on success Example:

CreateTensorWithDataAsOrtValue

Create a tensor backed by user-supplied memory. No data is copied. Parameters:
  • info: Memory location descriptor
  • p_data: Pointer to user-allocated data buffer
  • p_data_len: Size of data buffer in bytes
  • shape: Array of dimension sizes
  • shape_len: Number of dimensions
  • type: Element data type
  • out: Newly created tensor (must be freed with ReleaseValue)
Important:
  • The caller owns p_data and must keep it valid until the OrtValue is released
  • ReleaseValue will NOT free p_data
Example:

ReleaseValue

Free an OrtValue object. Parameters:
  • value: Value to free (can be NULL)

Tensor Data Access

GetTensorMutableData

Get a pointer to the tensor’s internal data for reading or writing. Parameters:
  • value: Tensor value
  • out: Pointer to tensor data (valid until value is released)
Returns: NULL on success Example:

TensorAt

Get a pointer to a specific element in a tensor. Parameters:
  • value: Tensor value
  • location_values: Array of indices specifying element location
  • location_values_count: Number of indices (must match tensor rank)
  • out: Pointer to the element
Example:
Note: Only works for numeric tensors (not strings).

Tensor Type Information

GetTensorTypeAndShape

Get type and shape information from a tensor. Parameters:
  • value: Tensor value to query
  • out: Type and shape info (must be freed with ReleaseTensorTypeAndShapeInfo)
Example:

GetTensorElementType

Get the element data type from type info. Parameters:
  • info: Type and shape info
  • out: Element data type
Data Types:

GetDimensionsCount

Get the number of dimensions in the tensor shape.

GetDimensions

Get the dimensions of the tensor. Parameters:
  • info: Type and shape info
  • dim_values: Array to receive dimension sizes
  • dim_values_length: Size of dim_values array (use GetDimensionsCount)

GetTensorShapeElementCount

Get total number of elements in the tensor (product of all dimensions). Parameters:
  • info: Type and shape info
  • out: Total element count
Note: Returns 1 for scalar (0 dimensions), -1 if any dimension is negative.

IsTensor

Check if an OrtValue is a tensor. Parameters:
  • value: Value to check
  • out: Set to 1 if tensor, 0 otherwise

String Tensors

FillStringTensor

Set all strings in a string tensor at once. Parameters:
  • value: String tensor (type ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING)
  • s: Array of null-terminated strings
  • s_len: Number of strings (must match tensor shape)
Example:

GetStringTensorDataLength

Get total byte length of all strings (without null terminators). Parameters:
  • value: String tensor
  • len: Total byte length

GetStringTensorContent

Get all strings from a string tensor. Parameters:
  • value: String tensor
  • s: Buffer to receive all strings (not null-terminated)
  • s_len: Buffer size (get from GetStringTensorDataLength)
  • offsets: Array to receive start offsets of each string
  • offsets_len: Size of offsets array (matches number of strings)
Example:

GetStringTensorElement

Get a single string from a string tensor. Parameters:
  • value: String tensor
  • s_len: Size of buffer (get from GetStringTensorElementLength)
  • index: Index of string to retrieve
  • s: Buffer to receive string (not null-terminated)

FillStringTensorElement

Set a single string in a string tensor. Parameters:
  • value: String tensor
  • s: Null-terminated UTF-8 string
  • index: Index where to set the string

Memory Information

CreateMemoryInfo

Create a memory info descriptor. Parameters:
  • name: Memory location name (e.g., “Cpu”, “Cuda”)
  • type: Allocator type
  • id: Device ID
  • mem_type: Memory type
  • out: Newly created memory info (must be freed with ReleaseMemoryInfo)
Allocator Types:
Memory Types:

CreateCpuMemoryInfo

Create memory info for CPU memory (shortcut for CreateMemoryInfo with name=“Cpu” and id=0).

ReleaseMemoryInfo

Free memory info.

Non-Tensor Types

CreateValue

Create a map or sequence value. Parameters:
  • in: Array of OrtValue objects
  • num_values: Number of values (2 for maps, N for sequences)
  • value_type: ONNX_TYPE_MAP or ONNX_TYPE_SEQUENCE
  • out: Newly created value
For Maps: num_values must be 2, with in[0] containing keys and in[1] containing values. For Sequences: num_values is the sequence length.

GetValue

Get an element from a map or sequence. Parameters:
  • value: Map or sequence value
  • index: For maps: 0=keys, 1=values; For sequences: element index
  • allocator: Allocator for the new value
  • out: Retrieved element (must be freed with ReleaseValue)

GetValueCount

Get count of elements in a map or sequence. Parameters:
  • value: Map or sequence value
  • out: Element count (always 2 for maps)

C++ Helper Types

Float16_t

Example:

BFloat16_t

See Also