internal static DisposableNamedOnnxValue CreateFromOrtValue(string name, OrtValue ortValue) { var result = CreateFromOnnxValue(name, ortValue.Handle, OrtAllocator.DefaultInstance); ortValue.Disown(); return(result); }
private string[] _dataBufferAsString; // string tensor values copied into managed memory /// <summary> /// Constructs an instance and takes ownership of ortValue on success /// </summary> /// <param name="ortValue">ortValue that is a Tensor</param> public NativeOnnxTensorMemory(OrtValue ortValue) { Type type = null; int width = 0; IntPtr typeAndShape = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorTypeAndShape(ortValue.Handle, out typeAndShape)); try { TensorElementType elemType; { IntPtr el_type; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorElementType(typeAndShape, out el_type)); elemType = (TensorElementType)el_type; } TensorElementTypeConverter.GetTypeAndWidth(elemType, out type, out width); if (typeof(T) != type) { throw new NotSupportedException(nameof(NativeOnnxTensorMemory <T>) + " does not support T = " + nameof(T)); } ElementType = elemType; ElementWidth = width; UIntPtr dimension; long count; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensionsCount(typeAndShape, out dimension)); { IntPtr el_count; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorShapeElementCount(typeAndShape, out el_count)); // count can be negative. count = (long)el_count; } if (count < 0) { throw new NotSupportedException("Symbolic dimensions in the tensor is not supported"); } long[] shape = new long[dimension.ToUInt64()]; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensions(typeAndShape, shape, dimension)); //Note: shape must be alive during the call Count = (int)count; Dimensions = new int[dimension.ToUInt64()]; for (ulong i = 0; i < dimension.ToUInt64(); i++) { Dimensions[i] = (int)shape[i]; } if (typeof(T) != typeof(string)) { NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorMutableData(ortValue.Handle, out _dataBufferPointer)); } else { UIntPtr strLen; var offsets = new UIntPtr[Count]; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetStringTensorDataLength(ortValue.Handle, out strLen)); var dataBuffer = new byte[strLen.ToUInt64()]; using (var dataBufferHandle = new Memory <byte>(dataBuffer).Pin()) using (var offsetMemoryHandle = new Memory <UIntPtr>(offsets).Pin()) { unsafe { _dataBufferPointer = (IntPtr)dataBufferHandle.Pointer; NativeApiStatus.VerifySuccess( NativeMethods.OrtGetStringTensorContent( ortValue.Handle, _dataBufferPointer, strLen, (IntPtr)offsetMemoryHandle.Pointer, (UIntPtr)Count)); } _dataBufferAsString = new string[Count]; for (var i = 0; i < offsets.Length; i++) { var length = (i == offsets.Length - 1) ? strLen.ToUInt64() - offsets[i].ToUInt64() : offsets[i + 1].ToUInt64() - offsets[i].ToUInt64(); // Onnx specifies strings always in UTF-8, no trailing null, no leading BOM _dataBufferAsString[i] = Encoding.UTF8.GetString(dataBuffer, (int)offsets[i], (int)length); } } } // Transfer ownership, but only do so if the original OrtValue is already owned. if (ortValue.IsOwned) { _ortValue = new OrtValue(ortValue.Disown()); } else { _ortValue = new OrtValue(ortValue.Handle, false); } } finally { NativeMethods.OrtReleaseTensorTypeAndShapeInfo(typeAndShape); } }
internal NativeOrtValueCollectionOwner(OrtValue ortValue, DisposableList <DisposableNamedOnnxValue> disposables) { Debug.Assert(ortValue.IsOwned); _ortValue = new OrtValue(ortValue.Disown()); _disposables = disposables; }
private string[] _dataBufferAsString; // string tensor values copied into managed memory /// <summary> /// Constructs an instance and takes ownership of ortValue on success /// </summary> /// <param name="ortValue">ortValue that is a Tensor</param> public NativeOnnxTensorMemory(OrtValue ortValue) { Type type = null; int width = 0; IntPtr typeAndShape = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorTypeAndShape(ortValue.Handle, out typeAndShape)); try { TensorElementType elemType; { IntPtr el_type; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorElementType(typeAndShape, out el_type)); elemType = (TensorElementType)el_type; } if (!TensorElementTypeConverter.GetTypeAndWidth(elemType, out type, out width)) { throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "Unable to query type information for data type: " + elemType.ToString()); } if (typeof(T) != type) { var message = String.Format("The NativeOnnxTensorMemory<T> type being instantiated for T = : {0} while supplied OrtValue contains T = {1}", typeof(T), type); throw new OnnxRuntimeException(ErrorCode.InvalidArgument, message); } ElementType = elemType; ElementWidth = width; UIntPtr dimension; long count; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensionsCount(typeAndShape, out dimension)); { IntPtr el_count; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorShapeElementCount(typeAndShape, out el_count)); // count can be negative. count = (long)el_count; } if (count < 0) { throw new NotSupportedException("Symbolic dimensions in the tensor is not supported"); } long[] shape = new long[dimension.ToUInt64()]; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDimensions(typeAndShape, shape, dimension)); //Note: shape must be alive during the call Count = (int)count; Dimensions = new int[dimension.ToUInt64()]; for (ulong i = 0; i < dimension.ToUInt64(); i++) { Dimensions[i] = (int)shape[i]; } if (elemType != TensorElementType.String) { NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorMutableData(ortValue.Handle, out _dataBufferPointer)); } else { UIntPtr strLen; var offsets = new UIntPtr[Count]; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetStringTensorDataLength(ortValue.Handle, out strLen)); var dataBuffer = new byte[strLen.ToUInt64()]; using (var dataBufferHandle = new Memory <byte>(dataBuffer).Pin()) using (var offsetMemoryHandle = new Memory <UIntPtr>(offsets).Pin()) { unsafe { _dataBufferPointer = (IntPtr)dataBufferHandle.Pointer; NativeApiStatus.VerifySuccess( NativeMethods.OrtGetStringTensorContent( ortValue.Handle, _dataBufferPointer, strLen, (IntPtr)offsetMemoryHandle.Pointer, (UIntPtr)Count)); } _dataBufferAsString = new string[Count]; for (var i = 0; i < offsets.Length; i++) { var length = (i == offsets.Length - 1) ? strLen.ToUInt64() - offsets[i].ToUInt64() : offsets[i + 1].ToUInt64() - offsets[i].ToUInt64(); // Onnx specifies strings always in UTF-8, no trailing null, no leading BOM _dataBufferAsString[i] = Encoding.UTF8.GetString(dataBuffer, (int)offsets[i], (int)length); } } } // Transfer ownership _ortValue = new OrtValue(ortValue.Disown()); } finally { NativeMethods.OrtReleaseTensorTypeAndShapeInfo(typeAndShape); } }