/// <summary> /// This constructs an instance representing an native memory allocation. /// Typically returned by OrtAllocator.Allocate(). However, some APIs return /// natively allocated IntPtr using a specific allocator. It is a good practice /// to wrap such a memory into OrtAllocation for proper disposal. You can set /// size to zero if not known, which is not important for disposing. /// </summary> /// <param name="allocator"></param> /// <param name="pointer"></param> /// <param name="size"></param> internal OrtMemoryAllocation(OrtAllocator allocator, IntPtr pointer, uint size) : base(pointer, true) { _allocator = allocator; Size = size; }
/// <summary> /// Will extract keys and values from the map and create a DisposableNamedOnnxValue from it /// </summary> /// <param name="name">name of the output</param> /// <param name="ortValueMap">ortValue that represents a map. /// This function does not take ownership of the map as it we copy all keys an values into a dictionary. We let the caller dispose of it</param> /// <param name="allocator"></param> /// <returns>DisposableNamedOnnxValue</returns> private static DisposableNamedOnnxValue DisposableNamedOnnxValueFromNativeMap(string name, OrtValue ortValueMap, OrtAllocator allocator) { DisposableNamedOnnxValue result = null; // Map processing is currently not recursing. It is assumed to contain // only primitive types and strings tensors. No sequences or maps. // The data is being copied to a dictionary and all ortValues are being disposed. // not mapped for client consumption. using (var cleanUpList = new DisposableList <IDisposable>()) { // Take possession of the map ortValueElement IntPtr nativeOnnxValueMapKeys = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(ortValueMap.Handle, 0, allocator.Pointer, out nativeOnnxValueMapKeys)); var ortValueKeys = new OrtValue(nativeOnnxValueMapKeys); cleanUpList.Add(ortValueKeys); IntPtr nativeOnnxValueMapValues = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(ortValueMap.Handle, 1, allocator.Pointer, out nativeOnnxValueMapValues)); var ortValueValues = new OrtValue(nativeOnnxValueMapValues); cleanUpList.Add(ortValueValues); IntPtr typeAndShape = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorTypeAndShape(nativeOnnxValueMapKeys, out typeAndShape)); TensorElementType elemType = TensorElementType.DataTypeMax; try { IntPtr el_type; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorElementType(typeAndShape, out el_type)); elemType = (TensorElementType)el_type; } finally { NativeMethods.OrtReleaseTensorTypeAndShapeInfo(typeAndShape); } /// XXX: This code always assumes that the value type is float and makes no checks /// similar to that of the key. Also Map type in general can also be another sequence or map, /// not just a tensor switch (elemType) { case TensorElementType.Int64: result = DisposableNamedOnnxValueFromNativeMapElements <Int64, float>(string.Empty, ortValueKeys, ortValueValues); break; case TensorElementType.String: result = DisposableNamedOnnxValueFromNativeMapElements <string, float>(string.Empty, ortValueKeys, ortValueValues); break; default: throw new NotSupportedException("Map of element type: " + elemType + " is not supported"); } } return(result); }
/// <summary> /// This method will create an instance of DisposableNamedOnnxValue that will own ortSequenceValue /// an all disposable native objects that are elements of the sequence /// </summary> /// <param name="name"></param> /// <param name="ortValueSequence">ortValueElement that has native sequence</param> /// <param name="allocator"> used allocator</param> /// <returns>DisposableNamedOnnxValue</returns> private static DisposableNamedOnnxValue DisposableNamedOnnxValueFromSequence(string name, OrtValue ortValueSequence, OrtAllocator allocator) { DisposableNamedOnnxValue result = null; IntPtr count; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValueCount(ortValueSequence.Handle, out count)); var sequence = new DisposableList <DisposableNamedOnnxValue>(count.ToInt32()); try { for (int i = 0; i < count.ToInt32(); i++) { IntPtr nativeOnnxValueSeq; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(ortValueSequence.Handle, i, allocator.Pointer, out nativeOnnxValueSeq)); using (var ortValueElement = new OrtValue(nativeOnnxValueSeq)) { // Will take ownership or throw sequence.Add(CreateFromOrtValue(string.Empty, ortValueElement, allocator)); } } // NativeOrtValueCollectionOwner will take ownership of ortValueSequence and will make sure sequence // is also disposed. var nativeCollectionManager = new NativeOrtValueCollectionOwner(ortValueSequence, sequence); result = new DisposableNamedOnnxValue(name, sequence, OnnxValueType.ONNX_TYPE_SEQUENCE, TensorElementType.DataTypeMax, nativeCollectionManager); } catch (Exception) { sequence.Dispose(); throw; } return(result); }
internal static DisposableNamedOnnxValue CreateFromOrtValue(string name, OrtValue ortValue, OrtAllocator allocator) { DisposableNamedOnnxValue result = null; IntPtr valueType; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValueType(ortValue.Handle, out valueType)); OnnxValueType onnxValueType = (OnnxValueType)valueType; switch (onnxValueType) { case OnnxValueType.ONNX_TYPE_TENSOR: result = CreateTensorFromOnnxValue(name, ortValue); break; case OnnxValueType.ONNX_TYPE_SEQUENCE: result = DisposableNamedOnnxValueFromSequence(name, ortValue, allocator); break; case OnnxValueType.ONNX_TYPE_MAP: result = DisposableNamedOnnxValueFromNativeMap(name, ortValue, allocator); break; default: throw new NotSupportedException("OnnxValueType : " + onnxValueType + " is not supported"); } return(result); }
internal static DisposableNamedOnnxValue CreateFromOnnxValue(string name, IntPtr nativeOnnxValue, OrtAllocator allocator) { IntPtr valueType; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValueType(nativeOnnxValue, out valueType)); OnnxValueType onnxValueType = (OnnxValueType)valueType; switch (onnxValueType) { case OnnxValueType.ONNX_TYPE_TENSOR: return(CreateTensorFromOnnxValue(name, nativeOnnxValue)); case OnnxValueType.ONNX_TYPE_SEQUENCE: IntPtr count = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValueCount(nativeOnnxValue, out count)); var sequence = new DisposableList <DisposableNamedOnnxValue>(count.ToInt32()); for (int i = 0; i < count.ToInt32(); i++) { IntPtr nativeOnnxValueSeq; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(nativeOnnxValue, i, allocator.Pointer, out nativeOnnxValueSeq)); sequence.Add(CreateFromOnnxValue(string.Empty, nativeOnnxValueSeq, allocator)); } return(new DisposableNamedOnnxValue(name, sequence, OnnxValueType.ONNX_TYPE_SEQUENCE, TensorElementType.DataTypeMax, null)); case OnnxValueType.ONNX_TYPE_MAP: IntPtr nativeOnnxValueMapKeys = IntPtr.Zero; IntPtr nativeOnnxValueMapValues = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(nativeOnnxValue, 0, allocator.Pointer, out nativeOnnxValueMapKeys)); NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(nativeOnnxValue, 1, allocator.Pointer, out nativeOnnxValueMapValues)); IntPtr typeAndShape = IntPtr.Zero; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorTypeAndShape(nativeOnnxValueMapKeys, out typeAndShape)); TensorElementType elemType = TensorElementType.DataTypeMax; try { IntPtr el_type; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetTensorElementType(typeAndShape, out el_type)); elemType = (TensorElementType)el_type; } finally { NativeMethods.OrtReleaseTensorTypeAndShapeInfo(typeAndShape); } switch (elemType) { case TensorElementType.Int64: return(DisposableNamedOnnxValueFromNativeMap <Int64, float>(string.Empty, nativeOnnxValueMapKeys, nativeOnnxValueMapValues)); case TensorElementType.String: return(DisposableNamedOnnxValueFromNativeMap <string, float>(string.Empty, nativeOnnxValueMapKeys, nativeOnnxValueMapValues)); default: throw new NotSupportedException("Map of element type: " + elemType + " is not supported"); } default: throw new NotSupportedException("OnnxValueType : " + onnxValueType + " is not supported"); } }
/// <summary> /// This constructs an instance representing an native memory allocation. /// Typically returned by OrtAllocator.Allocate(). However, some APIs return /// natively allocated IntPtr using a specific allocator. It is a good practice /// to wrap such a memory into OrtAllocation for proper disposal. You can set /// size to zero if not known, which is not important for disposing. /// </summary> /// <param name="allocator"></param> /// <param name="pointer"></param> /// <param name="size"></param> internal OrtMemoryAllocation(OrtAllocator allocator, IntPtr pointer, uint size) { _allocator = allocator; Pointer = pointer; Size = size; }