/// <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); }
/// <summary> /// This fetches bound outputs after running the model with RunWithBinding() /// </summary> /// <returns>IDisposableReadOnlyCollection<OrtValue></returns> public IDisposableReadOnlyCollection <OrtValue> GetOutputValues() { IntPtr ortValues = IntPtr.Zero; UIntPtr count = UIntPtr.Zero; var allocator = OrtAllocator.DefaultInstance; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetBoundOutputValues(handle, allocator.Pointer, out ortValues, out count)); if (count.Equals(UIntPtr.Zero)) { return(new DisposableList <OrtValue>()); } using (var ortValuesAllocation = new OrtMemoryAllocation(allocator, ortValues, 0)) { int outputCount = (int)count; var ortList = new DisposableList <OrtValue>(outputCount); try { for (int i = 0; i < outputCount; ++i) { IntPtr ortValue = Marshal.ReadIntPtr(ortValues, IntPtr.Size * i); ortList.Add(new OrtValue(ortValue)); } } catch (Exception e) { ortList.Dispose(); throw e; } return(ortList); } }
/// <summary> /// This method return a collection of DisposableNamedOnnxValue as in other interfaces /// Query names from OrtIoBinding object and pair then with the array of OrtValues returned /// from OrtIoBinding.GetOutputValues() /// /// </summary> /// <param name="runOptions">RunOptions</param> /// <param name="ioBinding">OrtIoBinding instance with bindings</param> /// <param name="names">optional parameter. If you already know the names of the outputs you can save a native /// call to retrieve output names. They will be paired with the returned OrtValues and combined into DisposbleNamedOnnxValues. /// Otherwise, the method will retrieve output names from the OrtIoBinding instance. /// It is an error if you supply a different number of names than the returned outputs</param> public IDisposableReadOnlyCollection <DisposableNamedOnnxValue> RunWithBindingAndNames(RunOptions runOptions, OrtIoBinding ioBinding, string[] names = null) { NativeApiStatus.VerifySuccess(NativeMethods.OrtRunWithBinding(Handle, runOptions.Handle, ioBinding.Handle)); using (var ortValues = ioBinding.GetOutputValues()) { string[] outputNames = names; if (outputNames == null) { outputNames = ioBinding.GetOutputNames(); } if (outputNames.Length != ortValues.Count) { throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "Number of specified names: " + names.Length + " does not match the output number: " + ortValues.Count); } var result = new DisposableList <DisposableNamedOnnxValue>(outputNames.Length); try { for (int i = 0; i < outputNames.Length; ++i) { var ortValue = ortValues.ElementAt(i); result.Add(DisposableNamedOnnxValue.CreateTensorFromOnnxValue(outputNames[i], ortValue.Handle)); ortValue.Disown(); } } catch (Exception e) { result.Dispose(); throw e; } return(result); } }
IDisposableReadOnlyCollection <DisposableNamedOnnxValue> CreateDisposableResult(List <OrtValue> ortValues, IReadOnlyCollection <string> outputNames) { var result = new DisposableList <DisposableNamedOnnxValue>(outputNames.Count); try { for (int i = 0; i < ortValues.Count; i++) { var ortValue = ortValues[i]; result.Add(DisposableNamedOnnxValue.CreateFromOrtValue(outputNames.ElementAt(i), ortValue)); } } catch (OnnxRuntimeException e) { result.Dispose(); throw e; } return(result); }
protected virtual void Dispose(bool disposing) { if (_disposed) { return; } // dispose managed state (managed objects). if (disposing) { if (_disposables != null) { _disposables.Dispose(); _disposables = null; } // _ortValueHolder can be null when no native memory is involved if (_ortValue != null) { _ortValue.Dispose(); _ortValue = null; } _disposed = true; } }