/// <summary> /// This method maps keys and values of the map and copies them into a Dictionary /// and returns as an instance of DisposableNamedOnnxValue that does not own or dispose /// any onnx/ortValueElement. The method takes possession of ortValueTensorKeys and ortValueTensorValues /// and disposes of them. The original ortValueElement looses ownership of the Tensor. The caller is still responsible /// for disposing these arguments. Disposing ortValueElement that does not have ownership is a no-op, however, either /// of the arguments may still need to be disposed on exception. /// </summary> /// <typeparam name="K">key type</typeparam> /// <typeparam name="V">value type</typeparam> /// <param name="name">name of the output parameter</param> /// <param name="ortValueTensorKeys">tensor with map keys.</param> /// <param name="nativeOnnxValueValues">tensor with map values</param> /// <returns>instance of DisposableNamedOnnxValue with Dictionary</returns> private static DisposableNamedOnnxValue DisposableNamedOnnxValueFromNativeMapElements <K, V>(string name, OrtValue ortValueTensorKeys, OrtValue ortValueTensorValues) { using (var nativeTensorWrapperValues = new NativeOnnxTensorMemory <V>(ortValueTensorValues)) { var denseTensorValues = new DenseTensor <V>(nativeTensorWrapperValues.Memory, nativeTensorWrapperValues.Dimensions); if (typeof(K) == typeof(string)) { var map = new Dictionary <string, V>(); using (var nativeTensorWrapper = new NativeOnnxTensorMemory <string>(ortValueTensorKeys)) { var denseTensorKeys = new DenseTensor <string>(nativeTensorWrapper.GetBytesAsStringMemory(), nativeTensorWrapper.Dimensions); for (var i = 0; i < denseTensorKeys.Length; i++) { map.Add(denseTensorKeys.GetValue(i), denseTensorValues.GetValue(i)); } return(new DisposableNamedOnnxValue(name, map, OnnxValueType.ONNX_TYPE_MAP, TensorElementType.DataTypeMax, null)); } } else { var map = new Dictionary <K, V>(); using (var nativeTensorWrapper = new NativeOnnxTensorMemory <K>(ortValueTensorKeys)) { var denseTensorKeys = new DenseTensor <K>(nativeTensorWrapper.Memory, nativeTensorWrapper.Dimensions); for (var i = 0; i < denseTensorKeys.Length; i++) { map.Add(denseTensorKeys.GetValue(i), denseTensorValues.GetValue(i)); } return(new DisposableNamedOnnxValue(name, map, OnnxValueType.ONNX_TYPE_MAP, TensorElementType.DataTypeMax, null)); } } } }
private static DisposableNamedOnnxValue DisposableNamedOnnxValueFromNativeMap <K, V>(string name, IntPtr nativeOnnxValueKeys, IntPtr nativeOnnxValueValues) { var nativeTensorWrapperValues = new NativeOnnxTensorMemory <V>(nativeOnnxValueValues); var denseTensorValues = new DenseTensor <V>(nativeTensorWrapperValues.Memory, nativeTensorWrapperValues.Dimensions); if (typeof(K) == typeof(string)) { var map = new Dictionary <string, V>(); var nativeTensorWrapper = new NativeOnnxTensorMemory <byte>(nativeOnnxValueKeys, true); var denseTensorKeys = new DenseTensor <string>(nativeTensorWrapper.GetBytesAsStringMemory(), nativeTensorWrapper.Dimensions); for (var i = 0; i < denseTensorKeys.Length; i++) { map.Add(denseTensorKeys.GetValue(i), denseTensorValues.GetValue(i)); } // release native memory nativeTensorWrapperValues.Dispose(); nativeTensorWrapper.Dispose(); return(new DisposableNamedOnnxValue(string.Empty, map, null)); } else { var map = new Dictionary <K, V>(); var nativeTensorWrapper = new NativeOnnxTensorMemory <K>(nativeOnnxValueKeys); var denseTensorKeys = new DenseTensor <K>(nativeTensorWrapper.Memory, nativeTensorWrapper.Dimensions); for (var i = 0; i < denseTensorKeys.Length; i++) { map.Add(denseTensorKeys.GetValue(i), denseTensorValues.GetValue(i)); } // release native memory nativeTensorWrapperValues.Dispose(); nativeTensorWrapper.Dispose(); return(new DisposableNamedOnnxValue(string.Empty, map, null)); } }