// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Microsoft.ML.OnnxRuntime
{
///
/// The class holds keys and values for the dictionary
/// in a for of two DenseTensors. The class is used to avoid
/// data copy and make these available to the native code.
/// Strings require special handling.
///
internal class MapHelper
{
internal MapHelper(TensorBase keys, TensorBase values)
{
Keys = keys;
Values = values;
}
internal TensorBase Keys { get; } // DenseTensor
internal TensorBase Values { get; } // DenseTensor
}
///
/// This is a legacy class that is kept for backward compatibility.
/// Use OrtValue based API.
///
/// The class associates a name with an Object.
/// The name of the class is a misnomer, it does not hold any Onnx values,
/// just managed representation of them.
///
/// The class is currently used as both inputs and outputs. Because it is non-
/// disposable, it can not hold on to any native objects.
///
/// When used as input, we temporarily create OrtValues that map managed inputs
/// directly. Thus we are able to avoid copying of contiguous data.
///
/// For outputs, tensor buffers works the same as input, providing it matches
/// the expected output shape. For other types (maps and sequences) we create a copy of the data.
/// This is because, the class is not Disposable and it is a public interface, thus it can not own
/// the underlying OrtValues that must be destroyed before Run() returns.
///
/// To avoid data copying on output, use DisposableNamedOnnxValue class that is returned from Run() methods.
/// This provides access to the native memory tensors and avoids copying.
///
/// It is a recursive structure that may contain Tensors (base case)
/// Other sequences and maps. Although the OnnxValueType is exposed,
/// the caller is supposed to know the actual data type contained.
///
/// The convention is that for tensors, it would contain a DenseTensor{T} instance or
/// anything derived from Tensor{T}.
///
/// For sequences, it would contain a IList{T} where T is an instance of NamedOnnxValue that
/// would contain a tensor or another type.
///
/// For Maps, it would contain a IDictionary{K, V} where K,V are primitive types or strings.
///
///
public class NamedOnnxValue
{
///
/// Managed Tensor, Dictionary or IList
///
private Object _value;
///
/// Name of the instance, model input/output
///
private string _name;
private MapHelper _mapHelper; // used for maps, otherwise null
///
/// Constructs an instance of NamedOnnxValue and represents
/// a model input to an inference session.
///
/// input/output name
/// Object that may be a tensor, Dictionary, IList
[Obsolete("Use constructors with valueType or static factory methods")]
protected NamedOnnxValue(string name, Object value)
{
_name = name;
_value = value;
ValueType = OnnxValueType.ONNX_TYPE_UNKNOWN;
}
///
/// Constructs an instance that contains a tensor, sequence or optional type.
///
///
///
///
internal NamedOnnxValue(string name, Object value, OnnxValueType valueType)
{
_name = name;
_value = value;
ValueType = valueType;
if (valueType == OnnxValueType.ONNX_TYPE_MAP)
{
throw new OnnxRuntimeException(ErrorCode.InvalidArgument, "Use another __ctor for maps");
}
}
///
/// Use this to construct maps
///
///
///
///
internal NamedOnnxValue(string name, Object value, MapHelper helper)
{
_name = name;
_value = value;
ValueType = OnnxValueType.ONNX_TYPE_MAP;
_mapHelper = helper;
}
///
/// Onnx Value Type if known. In general, NamedOnnxValue is able to contain
/// arbitrary objects. Please, follow the convention described in the class doc.
///
public OnnxValueType ValueType { get; internal set; }
///
/// This is a factory method that instantiates NamedOnnxValue
/// and associated name with an instance of a Tensor
///
///
/// name
/// Tensor
///
public static NamedOnnxValue CreateFromTensor(string name, Tensor value)
{
return new NamedOnnxValue(name, value, OnnxValueType.ONNX_TYPE_TENSOR);
}
///
/// This is a factory method that instantiates NamedOnnxValue.
/// It would contain a sequence of elements
///
///
///
///
public static NamedOnnxValue CreateFromSequence(string name, IEnumerable value)
{
return new NamedOnnxValue(name, value, OnnxValueType.ONNX_TYPE_SEQUENCE);
}
///
/// Instantiates NamedOnnxValue that contains IDictionary{K, V}
///
/// Keys type
/// Values type
///
///
/// new instance of NamedOnnxValue
public static NamedOnnxValue CreateFromMap(string name, IDictionary value)
{
// The order in which Keys and Values are unspecified,
// but it is guaranteed to be the same order
// These tensors are 1-D
return CreateFromMap(name, value.Keys, value.Values);
}
internal static NamedOnnxValue CreateFromMap(string name, ICollection keys, ICollection values)
{
var keysTensor = new DenseTensor(keys.ToArray(), new int[1] { keys.Count });
var valuesTensor = new DenseTensor(values.ToArray(), new int[1] { values.Count });
return new NamedOnnxValue(name, values, new MapHelper(keysTensor, valuesTensor));
}
///
/// Exposes the name of the of the model input/output
///
/// name string
public string Name { get { return _name; } set { _name = value; } }
///
/// Exposes the underlying managed object
///
/// object
public Object Value { get { return _value; } set { _value = value; } }
///
/// Try-get value as a Tensor<T>.
///
/// Type
/// Tensor object if contained value is a Tensor. Null otherwise
public Tensor AsTensor()
{
return _value as Tensor; // will return null if not castable
}
///
/// Try-get value as an Enumerable<T>.
/// T is usually a NamedOnnxValue instance that may contain
/// Tensors, Sequences, Maps or optional types
///
/// Type
/// Enumerable object if contained value is a Enumerable. Null otherwise
public IEnumerable AsEnumerable()
{
var x = _value as IEnumerable;
return x;
}
///
/// Try-get value as an Dictionary<K,V>.
///
/// Key type currently primitive type only
/// Value type, currently primitive type only
/// Dictionary object if contained value is a Dictionary. Null otherwise
public IDictionary AsDictionary()
{
return _value as IDictionary;
}
///
/// Pin the underlying memory and create an instance of OrtValue containing a tensor
/// based on the pinned managed memory. The caller is responsible for Disposing
/// both OrtValue and pinnedMemoryHandle
///
/// dispose after returned OrtValue is disposed
/// The native OrtValue handle
internal virtual IntPtr InputToOrtValueHandle(NodeMetadata metadata, out IDisposable memoryOwner)
{
var projection = ManagedTypeProjection.CreateProjection(this, metadata);
memoryOwner = projection;
return projection.Handle;
}
///
/// Produces an output value for outputs. This produces an output value
/// only for tensors or optional types that can contain a tensor.
/// For all other Onnx value types, this method throws. Use Run() overloads
/// that return DisposableNamedOnnxValue to get access to all Onnx value types
/// that may be returned as output.
///
///
///
///
internal virtual IntPtr OutputToOrtValueHandle(NodeMetadata metadata, out IDisposable memoryOwner)
{
// For NamedOnnxValue for output we only allow to produce OrtValue for tensors
// or optional type that may contain a tensor
if (metadata.OnnxValueType == OnnxValueType.ONNX_TYPE_TENSOR)
{
var projection = ManagedTypeProjection.CreateProjection(this, metadata);
memoryOwner = projection;
return projection.Handle;
}
if (metadata.OnnxValueType == OnnxValueType.ONNX_TYPE_OPTIONAL)
{
var meta = metadata.AsOptionalMetadata().ElementMeta;
if (meta.OnnxValueType == OnnxValueType.ONNX_TYPE_TENSOR)
{
var projection = ManagedTypeProjection.CreateProjection(this, metadata);
memoryOwner = projection;
return projection.Handle;
}
}
throw new OnnxRuntimeException(ErrorCode.NotImplemented,
$"Can not create output OrtValue for NamedOnnxValue '{metadata.OnnxValueType}' type." +
$" Only tensors can be pre-allocated for outputs " +
$" Use Run() overloads that return DisposableNamedOnnxValue to get access to all Onnx value types that may be returned as output.");
}
internal TensorBase GetDictionaryKeys()
{
if (ValueType != OnnxValueType.ONNX_TYPE_MAP)
{
throw new OnnxRuntimeException(ErrorCode.Fail, "This NamedOnnxValue instance does not contain a dictionary");
}
Debug.Assert(_mapHelper != null);
return _mapHelper.Keys;
}
internal TensorBase GetDictionaryValues()
{
if (ValueType != OnnxValueType.ONNX_TYPE_MAP)
{
throw new OnnxRuntimeException(ErrorCode.Fail, "This NamedOnnxValue instance does not contain a dictionary");
}
Debug.Assert(_mapHelper != null);
return _mapHelper.Values;
}
}
}