88using System . Diagnostics ;
99using System . Linq ;
1010
11- #if NET8_0_OR_GREATER
12- using DotnetTensors = System . Numerics . Tensors ;
13- using TensorPrimitives = System . Numerics . Tensors . TensorPrimitives ;
14- #endif
15-
1611namespace Microsoft . ML . OnnxRuntime
1712{
1813 /// <summary>
@@ -35,37 +30,37 @@ internal MapHelper(TensorBase keys, TensorBase values)
3530 /// <summary>
3631 /// This is a legacy class that is kept for backward compatibility.
3732 /// Use OrtValue based API.
38- ///
39- /// The class associates a name with an Object.
33+ ///
34+ /// The class associates a name with an Object.
4035 /// The name of the class is a misnomer, it does not hold any Onnx values,
4136 /// just managed representation of them.
42- ///
37+ ///
4338 /// The class is currently used as both inputs and outputs. Because it is non-
4439 /// disposable, it can not hold on to any native objects.
45- ///
40+ ///
4641 /// When used as input, we temporarily create OrtValues that map managed inputs
4742 /// directly. Thus we are able to avoid copying of contiguous data.
48- ///
43+ ///
4944 /// For outputs, tensor buffers works the same as input, providing it matches
5045 /// the expected output shape. For other types (maps and sequences) we create a copy of the data.
5146 /// This is because, the class is not Disposable and it is a public interface, thus it can not own
5247 /// the underlying OrtValues that must be destroyed before Run() returns.
53- ///
48+ ///
5449 /// To avoid data copying on output, use DisposableNamedOnnxValue class that is returned from Run() methods.
5550 /// This provides access to the native memory tensors and avoids copying.
56- ///
51+ ///
5752 /// It is a recursive structure that may contain Tensors (base case)
5853 /// Other sequences and maps. Although the OnnxValueType is exposed,
5954 /// the caller is supposed to know the actual data type contained.
60- ///
55+ ///
6156 /// The convention is that for tensors, it would contain a DenseTensor{T} instance or
6257 /// anything derived from Tensor{T}.
63- ///
58+ ///
6459 /// For sequences, it would contain a IList{T} where T is an instance of NamedOnnxValue that
6560 /// would contain a tensor or another type.
66- ///
61+ ///
6762 /// For Maps, it would contain a IDictionary{K, V} where K,V are primitive types or strings.
68- ///
63+ ///
6964 /// </summary>
7065 public class NamedOnnxValue
7166 {
@@ -145,23 +140,6 @@ public static NamedOnnxValue CreateFromTensor<T>(string name, Tensor<T> value)
145140 return new NamedOnnxValue ( name , value , OnnxValueType . ONNX_TYPE_TENSOR ) ;
146141 }
147142
148- #if NET8_0_OR_GREATER
149- #pragma warning disable SYSLIB5001 // System.Numerics.Tensors is only in preview so we can continue receiving API feedback
150- /// <summary>
151- /// This is a factory method that instantiates NamedOnnxValue
152- /// and associated name with an instance of a Tensor<typeparamref name="T"/>
153- /// </summary>
154- /// <typeparam name="T"></typeparam>
155- /// <param name="name">name</param>
156- /// <param name="value">Tensor<typeparamref name="T"/></param>
157- /// <returns></returns>
158- public static NamedOnnxValue CreateFromDotnetTensor < T > ( string name , DotnetTensors . Tensor < T > value )
159- {
160- return new NamedOnnxValue ( name , value , OnnxValueType . ONNX_TYPE_TENSOR ) ;
161- }
162- #pragma warning restore SYSLIB5001 // System.Numerics.Tensors is only in preview so it can continue receiving API feedback
163- #endif
164-
165143 /// <summary>
166144 /// This is a factory method that instantiates NamedOnnxValue.
167145 /// It would contain a sequence of elements
@@ -218,21 +196,6 @@ public Tensor<T> AsTensor<T>()
218196 return _value as Tensor < T > ; // will return null if not castable
219197 }
220198
221-
222- #if NET8_0_OR_GREATER
223- #pragma warning disable SYSLIB5001 // System.Numerics.Tensors is only in preview so we can continue receiving API feedback
224- /// <summary>
225- /// Try-get value as a Tensor<T>.
226- /// </summary>
227- /// <typeparam name="T">Type</typeparam>
228- /// <returns>Tensor object if contained value is a Tensor. Null otherwise</returns>
229- public DotnetTensors . Tensor < T > AsDotnetTensor < T > ( )
230- {
231- return _value as DotnetTensors . Tensor < T > ; // will return null if not castable
232- }
233- #pragma warning restore SYSLIB5001 // System.Numerics.Tensors is only in preview so it can continue receiving API feedback
234- #endif
235-
236199 /// <summary>
237200 /// Try-get value as an Enumerable<T>.
238201 /// T is usually a NamedOnnxValue instance that may contain
@@ -303,7 +266,7 @@ internal virtual IntPtr OutputToOrtValueHandle(NodeMetadata metadata, out IDispo
303266 }
304267 }
305268
306- throw new OnnxRuntimeException ( ErrorCode . NotImplemented ,
269+ throw new OnnxRuntimeException ( ErrorCode . NotImplemented ,
307270 $ "Can not create output OrtValue for NamedOnnxValue '{ metadata . OnnxValueType } ' type." +
308271 $ " Only tensors can be pre-allocated for outputs " +
309272 $ " Use Run() overloads that return DisposableNamedOnnxValue to get access to all Onnx value types that may be returned as output.") ;
0 commit comments