Show / Hide Table of Contents

Class OrtValue

Represents a disposable OrtValue. This class exposes a native instance of OrtValue. The class implements IDisposable via SafeHandle and must be disposed.

Inheritance
System.Object
OrtValue
Namespace: Microsoft.ML.OnnxRuntime
Assembly: cs.temp.dll.dll
Syntax
public class OrtValue : SafeHandle

Properties

IsInvalid

Overrides SafeHandle.IsInvalid

Declaration
public override bool IsInvalid { get; }
Property Value
Type Description
System.Boolean

returns true if handle is equal to Zero

Methods

CreateFromTensorObject(Object, out Nullable<MemoryHandle>, out TensorElementType)

This is a factory method creates a native Onnxruntime OrtValue containing a tensor. The method will attempt to pin managed memory so no copying occurs when data is passed down to native code.

Declaration
public static OrtValue CreateFromTensorObject(Object value, out MemoryHandle? memoryHandle, out TensorElementType elementType)
Parameters
Type Name Description
Object value

Tensor object

System.Nullable<MemoryHandle> memoryHandle

For all tensor types but string tensors we endeavor to use managed memory to avoid additional allocation and copy. This out parameter represents a chunk of pinned memory which will need to be disposed when no longer needed. The lifespan of memoryHandle should eclipse the lifespan of the corresponding OrtValue.

TensorElementType elementType

discovered tensor element type

Returns
Type Description
OrtValue

And instance of OrtValue constructed on top of the object

CreateTensorValueWithData(OrtMemoryInfo, TensorElementType, Int64[], IntPtr, Int64)

Factory method to construct an OrtValue of Tensor type on top of pre-allocated memory. This can be a piece of native memory allocated by OrtAllocator (possibly on a device) or a piece of pinned managed memory.

The resulting OrtValue does not own the underlying memory buffer and will not attempt to deallocate it.

Declaration
public static OrtValue CreateTensorValueWithData(OrtMemoryInfo memInfo, TensorElementType elementType, long[] shape, IntPtr dataBuffer, long bufferLength)
Parameters
Type Name Description
OrtMemoryInfo memInfo

Memory Info. For managed memory it is a default cpu. For Native memory must be obtained from the allocator or OrtMemoryAllocation instance

TensorElementType elementType

DataType for the Tensor

System.Int64[] shape

Tensor shape

IntPtr dataBuffer

Pointer to a raw memory buffer

System.Int64 bufferLength

Buffer length in bytes

Returns
Type Description
OrtValue

A disposable instance of OrtValue

ReleaseHandle()

Overrides SafeHandle.ReleaseHandle() to properly dispose of the native instance of OrtValue

Declaration
protected override bool ReleaseHandle()
Returns
Type Description
System.Boolean

always returns true

In This Article
Back to top