/// <summary> /// Creates a <see cref="FixedBufferOnnxValue"/> object from the tensor and pins its underlying buffer. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static FixedBufferOnnxValue CreateFromTensor <T>(Tensor <T> value) { MemoryHandle?memHandle; var ortValue = OrtValue.CreateFromTensorObject(value, out memHandle, out TensorElementType elementType); if (memHandle.HasValue) { return(new FixedBufferOnnxValue((MemoryHandle)memHandle, ortValue, OnnxValueType.ONNX_TYPE_TENSOR, elementType)); } else { return(new FixedBufferOnnxValue(default(MemoryHandle), ortValue, OnnxValueType.ONNX_TYPE_TENSOR, elementType)); } }
/// <summary> /// Creates a <see cref="FixedBufferOnnxValue"/> object from the tensor and pins its underlying buffer. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns>a disposable instance of FixedBufferOnnxValue</returns> public static FixedBufferOnnxValue CreateFromTensor <T>(Tensor <T> value) { MemoryHandle?memHandle; var ortValue = OrtValue.CreateFromTensorObject(value, out memHandle, out TensorElementType elementType); // memHandle will have a value when CreateFromTensorObject() pins managed memory and that will have to be /// disposed (unpinned) when all is said is done. This is the case for blittable types but does not /// happen for string type where each element has its own allocation. if (memHandle.HasValue) { return(new FixedBufferOnnxValue((MemoryHandle)memHandle, ortValue, OnnxValueType.ONNX_TYPE_TENSOR, elementType)); } else { return(new FixedBufferOnnxValue(default(MemoryHandle), ortValue, OnnxValueType.ONNX_TYPE_TENSOR, elementType)); } }
/// <summary> /// Pin the underlying memory and create an instance of OrtValue /// based on the pinned managed memory. The caller is responsible for Disposing /// both OrtValue and pinnedMemoryHandle /// </summary> /// <param name="pinnedMemoryHandle">dispose after returned OrtValus is disposed</param> /// <returns>an instance of OrtValue. The lifespan of OrtValue must overlap pinnedMemoryHandle</returns> internal virtual OrtValue ToOrtValue(out MemoryHandle?pinnedMemoryHandle) { return(OrtValue.CreateFromTensorObject(_value, out pinnedMemoryHandle, out TensorElementType elementType)); }