Example #1
0
        private unsafe static void ToStringUInt16()
        {
            int size = Unsafe.SizeOf <Vector64 <UInt16> >() / sizeof(UInt16);

            UInt16[] values = new UInt16[size];

            for (int i = 0; i < size; i++)
            {
                values[i] = TestLibrary.Generator.GetUInt16();
            }

            Vector64 <UInt16> vector = Vector64.Create(values[0], values[1], values[2], values[3]);
            string            actual = vector.ToString();

            string expected = '<' + string.Join(", ", values.Select(x => x.ToString("G", System.Globalization.CultureInfo.InvariantCulture))) + '>';

            bool succeeded = string.Equals(expected, actual, StringComparison.Ordinal);

            if (!succeeded)
            {
                TestLibrary.TestFramework.LogInformation($"Vector64UInt16ToString: Vector64<UInt16>.ToString() returned an unexpected result.");
                TestLibrary.TestFramework.LogInformation($"Expected: {expected}");
                TestLibrary.TestFramework.LogInformation($"Actual: {actual}");
                TestLibrary.TestFramework.LogInformation(string.Empty);

                throw new Exception("One or more scenarios did not complete as expected.");
            }
        }
        public void UInt16ArrayTest()
        {
            var array = new UInt16[] { 0, 1 };
            var table = this.fixture.Execute(array);
            var ids   = table.AsEnumerable().Select(x => x.Field <int>("Id")).ToArray();

            Assert.Equal(array.Select(x => (int)x).ToArray(), ids);
        }
        public void UInt16ArrayTest()
        {
            var array            = new UInt16[] { 0, 1 };
            var arrayXmlToCursor = new ArrayXmlToCursor(array);
            var table            = GetDataTable(arrayXmlToCursor);
            var ids = table.AsEnumerable().Select(x => x.Field <int>("Id")).ToArray();

            CollectionAssert.AreEqual(array.Select(x => (int)x).ToArray(), ids);
        }
Example #4
0
        public void FillUInt16Array_ShouldProduceDesiredResults_ForBoundedInvocation()
        {
            // Arrange.
            var arrayLength = 180;
            var floor       = 10000;
            var ceiling     = 30000;
            var array       = new UInt16[arrayLength];

            using (var randomNumberGenerator = RandomNumberGenerator.Create())
            {
                // Act.
                randomNumberGenerator.FillUInt16Array(array, (UInt16)floor, (UInt16)ceiling);

                // Assert.
                array.Length.Should().Be(arrayLength);
                array.Select(value => Convert.ToDouble(value)).Average().Should().BeGreaterOrEqualTo(15000).And.BeLessOrEqualTo(25000);
            }
        }
Example #5
0
        public ONNXTensor(TensorProto onnxTensor)
        {
            // read shape
            var onnxShape = onnxTensor.Dims.ToArray();
            var shape     = ONNXLayout.ConvertShapeToBarracuda(onnxShape, onnxLayout: "?");

            // read data
            float[] data;
            if ((onnxTensor.RawData != null) && (!onnxTensor.RawData.IsEmpty))
            {
                var byteArray = new byte[onnxTensor.RawData.Length];
                onnxTensor.RawData.CopyTo(byteArray, 0);

                // Float32
                if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Float)
                {
                    data = new float[shape.length];
                    Debug.Assert((sizeof(float) * shape.length) == onnxTensor.RawData.Length);
                    Buffer.BlockCopy(byteArray, 0, data, 0, byteArray.Length);
                }
                // Float16
                else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Float16)
                {
                    var typedData = new UInt16[shape.length];
                    Debug.Assert((sizeof(UInt16) * shape.length) == onnxTensor.RawData.Length);
                    Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                    data = typedData.Select(x => HalfHelper.HalfToSingle(x)).ToArray();
                }
                // Int32
                else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int32)
                {
                    var typedData = new int[shape.length];
                    Debug.Assert((sizeof(int) * shape.length) == onnxTensor.RawData.Length);
                    Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                    data = typedData.Select(x => (float)x).ToArray();
                }
                // Int64
                else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int64)
                {
                    var typedData = new long[shape.length];
                    Debug.Assert((sizeof(long) * shape.length) == onnxTensor.RawData.Length);
                    Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                    data = typedData.Select(x => (float)x).ToArray();
                }
                else
                {
                    throw new OnnxLayerImportException($"Tensor data type {(TensorProto.Types.DataType)onnxTensor.DataType} is not supported.");
                }
            }
            // Float32
            else if ((onnxTensor.FloatData != null) && (onnxTensor.FloatData.Count != 0))
            {
                Debug.Assert(shape.length == onnxTensor.FloatData.Count);
                data = new float[shape.length];
                onnxTensor.FloatData.CopyTo(data, 0);
            }
            // Int32
            else if ((onnxTensor.Int32Data != null) && (onnxTensor.Int32Data.Count != 0))
            {
                Debug.Assert(shape.length == onnxTensor.Int32Data.Count);
                data = onnxTensor.Int32Data.Select(x => (float)x).ToArray();
            }
            // Int64
            else if ((onnxTensor.Int64Data != null) && (onnxTensor.Int64Data.Count != 0))
            {
                Debug.Assert(shape.length == onnxTensor.Int64Data.Count);
                data = onnxTensor.Int64Data.Select(x => (float)x).ToArray();
            }
            else
            {
                throw new OnnxLayerImportException("Could not read tensor data for constant tensor.");
            }

            m_Data  = new Tensor(shape, new SharedArrayTensorData(data));
            m_Shape = onnxShape;
        }
Example #6
0
        public ONNXTensor(TensorProto onnxTensor)
        {
            // read shape
            var onnxShape = onnxTensor.Dims.Select(v => v <int.MinValue?int.MinValue : v> int.MaxValue ? int.MaxValue : (int)v).ToArray();

            if (onnxShape.Any(s => s == 0))
            {
                // empty tensor, not data
                m_Shape = onnxShape;
                m_Data  = null;
            }
            else
            {
                // read data
                var     shape = ONNXLayout.ConvertShapeToBarracuda(onnxShape, onnxLayout: "?");
                float[] data;
                if ((onnxTensor.RawData != null) && (!onnxTensor.RawData.IsEmpty))
                {
                    var byteArray = new byte[onnxTensor.RawData.Length];
                    onnxTensor.RawData.CopyTo(byteArray, 0);

                    // Double
                    if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Double)
                    {
                        var typedData = new double[shape.length];
                        Assert.IsTrue((sizeof(double) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => v <int.MinValue?(float)int.MinValue : v> int.MaxValue ? (float)int.MaxValue : (float)v).ToArray();
                    }
                    // Float32
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Float)
                    {
                        data = new float[shape.length];
                        Assert.IsTrue((sizeof(float) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, data, 0, byteArray.Length);
                    }
                    // Float16
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Float16)
                    {
                        var typedData = new UInt16[shape.length];
                        Assert.IsTrue((sizeof(UInt16) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => HalfHelper.HalfToSingle(v)).ToArray();
                    }
                    // Int8
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int8)
                    {
                        var typedData = new sbyte[shape.length];
                        Assert.IsTrue((sizeof(sbyte) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // Int16
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int16)
                    {
                        var typedData = new short[shape.length];
                        Assert.IsTrue((sizeof(short) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // Int32
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int32)
                    {
                        var typedData = new int[shape.length];
                        Assert.IsTrue((sizeof(int) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // Int64
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Int64)
                    {
                        var typedData = new long[shape.length];
                        Assert.IsTrue((sizeof(long) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => v <int.MinValue?(float)int.MinValue : v> int.MaxValue ? (float)int.MaxValue : (float)v).ToArray();
                    }
                    // UInt8
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Uint8)
                    {
                        var typedData = new byte[shape.length];
                        Assert.IsTrue((sizeof(byte) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // UInt16
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Uint16)
                    {
                        var typedData = new ushort[shape.length];
                        Assert.IsTrue((sizeof(ushort) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // UInt32
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Uint32)
                    {
                        var typedData = new uint[shape.length];
                        Assert.IsTrue((sizeof(uint) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => (float)v).ToArray();
                    }
                    // UInt64
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Uint64)
                    {
                        var typedData = new ulong[shape.length];
                        Assert.IsTrue((sizeof(ulong) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => v > uint.MaxValue ? (float)uint.MaxValue : (float)v).ToArray();
                    }
                    // Bool
                    else if (onnxTensor.DataType == (int)TensorProto.Types.DataType.Bool)
                    {
                        var typedData = new bool[shape.length];
                        Assert.IsTrue((sizeof(bool) * shape.length) == onnxTensor.RawData.Length);
                        Buffer.BlockCopy(byteArray, 0, typedData, 0, byteArray.Length);
                        data = typedData.Select(v => v ? 1.0f : 0.0f).ToArray();
                    }
                    else
                    {
                        throw new OnnxLayerImportException($"Tensor data type {(TensorProto.Types.DataType)onnxTensor.DataType} is not supported.");
                    }
                }
                // Float32
                else if ((onnxTensor.FloatData != null) && (onnxTensor.FloatData.Count != 0))
                {
                    Assert.IsTrue(shape.length == onnxTensor.FloatData.Count);
                    data = new float[shape.length];
                    onnxTensor.FloatData.CopyTo(data, 0);
                }
                // Int32
                else if ((onnxTensor.Int32Data != null) && (onnxTensor.Int32Data.Count != 0))
                {
                    Assert.IsTrue(shape.length == onnxTensor.Int32Data.Count);
                    data = onnxTensor.Int32Data.Select(v => (float)v).ToArray();
                }
                // Int64
                else if ((onnxTensor.Int64Data != null) && (onnxTensor.Int64Data.Count != 0))
                {
                    Assert.IsTrue(shape.length == onnxTensor.Int64Data.Count);
                    data = onnxTensor.Int64Data.Select(v => v <int.MinValue?(float)int.MinValue : v> int.MaxValue ? (float)int.MaxValue : (float)v).ToArray();
                }
                else
                {
                    throw new OnnxLayerImportException("Could not read tensor data for constant tensor.");
                }

                m_Data  = new Tensor(shape, new SharedArrayTensorData(data));
                m_Shape = onnxShape;
            }
        }