Пример #1
0
        /// <summary>
        /// Derive the corresponding <see cref="Type"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
        /// The corresponding <see cref="Type"/> should match the type system in ONNXRuntime's C# APIs.
        /// This function is used when determining the corresponding <see cref="Type"/> of <see cref="OnnxCSharpToProtoWrapper.TypeProto"/>.
        /// </summary>
        /// <param name="dataType">ONNX's tensor element type.</param>
        public static Type GetNativeScalarType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
        {
            Type scalarType = null;

            switch (dataType)
            {
            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Bool:
                scalarType = typeof(System.Boolean);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int8:
                scalarType = typeof(System.SByte);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint8:
                scalarType = typeof(System.Byte);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int16:
                scalarType = typeof(System.Int16);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint16:
                scalarType = typeof(System.UInt16);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int32:
                scalarType = typeof(System.Int32);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint32:
                scalarType = typeof(System.UInt32);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int64:
                scalarType = typeof(System.Int64);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint64:
                scalarType = typeof(System.UInt64);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Double:
                scalarType = typeof(System.Double);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Float:
                scalarType = typeof(System.Single);
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.String:
                scalarType = typeof(string);
                break;

            default:
                throw Contracts.Except("Unsupported ONNX scalar type: " + dataType.ToString());
            }
            return(scalarType);
        }
Пример #2
0
        /// <summary>
        /// Derive the corresponding <see cref="DataViewType"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
        /// </summary>
        /// <param name="dataType">ONNX's tensor element type.</param>
        public static DataViewType GetScalarDataViewType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
        {
            DataViewType scalarType = null;

            switch (dataType)
            {
            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Bool:
                scalarType = BooleanDataViewType.Instance;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int8:
                scalarType = NumberDataViewType.SByte;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint8:
                scalarType = NumberDataViewType.Byte;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int16:
                scalarType = NumberDataViewType.Int16;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint16:
                scalarType = NumberDataViewType.UInt16;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int32:
                scalarType = NumberDataViewType.Int32;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint32:
                scalarType = NumberDataViewType.UInt32;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Int64:
                scalarType = NumberDataViewType.Int64;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Uint64:
                scalarType = NumberDataViewType.UInt64;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Float:
                scalarType = NumberDataViewType.Single;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.Double:
                scalarType = NumberDataViewType.Double;
                break;

            case OnnxCSharpToProtoWrapper.TensorProto.Types.DataType.String:
                scalarType = TextDataViewType.Instance;
                break;

            default:
                throw Contracts.Except("Unsupported ONNX scalar type: " + dataType.ToString());
            }
            return(scalarType);
        }