Tensor Operations
TheOrtValue object represents tensors and other ONNX types (maps, sequences, etc.).
Creating Tensors
CreateTensorAsOrtValue
allocator: Allocator to use for tensor memoryshape: Array of dimension sizesshape_len: Number of dimensionstype: Element data typeout: Newly created tensor (must be freed withReleaseValue)
NULL on success
Example:
CreateTensorWithDataAsOrtValue
info: Memory location descriptorp_data: Pointer to user-allocated data bufferp_data_len: Size of data buffer in bytesshape: Array of dimension sizesshape_len: Number of dimensionstype: Element data typeout: Newly created tensor (must be freed withReleaseValue)
- The caller owns
p_dataand must keep it valid until theOrtValueis released ReleaseValuewill NOT freep_data
ReleaseValue
OrtValue object.
Parameters:
value: Value to free (can be NULL)
Tensor Data Access
GetTensorMutableData
value: Tensor valueout: Pointer to tensor data (valid until value is released)
NULL on success
Example:
TensorAt
value: Tensor valuelocation_values: Array of indices specifying element locationlocation_values_count: Number of indices (must match tensor rank)out: Pointer to the element
Tensor Type Information
GetTensorTypeAndShape
value: Tensor value to queryout: Type and shape info (must be freed withReleaseTensorTypeAndShapeInfo)
GetTensorElementType
info: Type and shape infoout: Element data type
GetDimensionsCount
GetDimensions
info: Type and shape infodim_values: Array to receive dimension sizesdim_values_length: Size ofdim_valuesarray (useGetDimensionsCount)
GetTensorShapeElementCount
info: Type and shape infoout: Total element count
IsTensor
OrtValue is a tensor.
Parameters:
value: Value to checkout: Set to 1 if tensor, 0 otherwise
String Tensors
FillStringTensor
value: String tensor (typeONNX_TENSOR_ELEMENT_DATA_TYPE_STRING)s: Array of null-terminated stringss_len: Number of strings (must match tensor shape)
GetStringTensorDataLength
value: String tensorlen: Total byte length
GetStringTensorContent
value: String tensors: Buffer to receive all strings (not null-terminated)s_len: Buffer size (get fromGetStringTensorDataLength)offsets: Array to receive start offsets of each stringoffsets_len: Size of offsets array (matches number of strings)
GetStringTensorElement
value: String tensors_len: Size of buffer (get fromGetStringTensorElementLength)index: Index of string to retrieves: Buffer to receive string (not null-terminated)
FillStringTensorElement
value: String tensors: Null-terminated UTF-8 stringindex: Index where to set the string
Memory Information
CreateMemoryInfo
name: Memory location name (e.g., “Cpu”, “Cuda”)type: Allocator typeid: Device IDmem_type: Memory typeout: Newly created memory info (must be freed withReleaseMemoryInfo)
CreateCpuMemoryInfo
CreateMemoryInfo with name=“Cpu” and id=0).
ReleaseMemoryInfo
Non-Tensor Types
CreateValue
in: Array ofOrtValueobjectsnum_values: Number of values (2 for maps, N for sequences)value_type:ONNX_TYPE_MAPorONNX_TYPE_SEQUENCEout: Newly created value
num_values must be 2, with in[0] containing keys and in[1] containing values.
For Sequences: num_values is the sequence length.
GetValue
value: Map or sequence valueindex: For maps: 0=keys, 1=values; For sequences: element indexallocator: Allocator for the new valueout: Retrieved element (must be freed withReleaseValue)
GetValueCount
value: Map or sequence valueout: Element count (always 2 for maps)