Exemplo n.º 1
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x1 = (new float[length + 1]).Select((_) => (float)rd.NextDouble()).ToArray();
                float[] x2 = (new float[length + 1]).Select((_) => (float)rd.NextDouble()).ToArray();
                float[] y  = new float[length + 1];

                AvxArray <float> vx1 = x1, vx2 = x2, vy = y;

                Elementwise.Maximum((uint)length, vx1, vx2, vy);

                y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(Math.Max(x1[i], x2[i]), y[i], $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
Exemplo n.º 2
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 4 - 2).ToArray();
                float[] y = new float[length + 1];

                AvxArray <float> vx = x, vy = y;

                Elementwise.LeakyRelu((uint)length, 0.5f, vx, vy);

                y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(x[i] > 0 ? x[i] : x[i] * 0.5f, y[i], 1e-4f, $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x = (new float[length + 1]).Select((_) => (float)rd.Next(5) - 2).ToArray();
                float[] y = new float[length + 1];

                AvxArray <float> vx = x, vy = y;

                Elementwise.EqualConstant((uint)length, -1, vx, vy);

                y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(-1 == x[i] ? 1f : 0f, y[i], $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (uint vector_length = 1; vector_length <= 100; vector_length++)
            {
                for (uint map_length = 0; map_length <= vector_length * 128; map_length += vector_length)
                {
                    float[] x1 = (new float[vector_length + 1]).Select((_) => (float)rd.NextDouble()).ToArray();
                    float[] x2 = (new float[map_length + 1]).Select((_) => (float)rd.NextDouble()).ToArray();
                    float[] y  = new float[map_length + 1];
                    float   v  = y[map_length] = (float)rd.NextDouble();

                    AvxArray <float> vx1 = x1, vx2 = x2, vy = y;

                    Channelwise.SubLVector(vector_length, map_length, vx1, vx2, vy);

                    x1 = vx1;  x2 = vx2;  y = vy;

                    for (uint i = 0; i < map_length; i++)
                    {
                        Assert.AreEqual(x1[i % vector_length] - x2[i], y[i], $"vector:{vector_length}, map:{map_length}, index:{i}");
                    }

                    Assert.AreEqual(v, y[map_length], $"vector:{vector_length}, map:{map_length}, last_index");

                    Console.WriteLine($"pass:{vector_length}, {map_length}");
                }
            }
        }
Exemplo n.º 5
0
        public void ZerosetTest()
        {
            const int length = 15;

            float[] v  = (new float[length]).Select((_, idx) => (float)idx).ToArray();
            float[] v2 = new float[length];
            float[] v3 = (new float[length]).Select((_, idx) => idx < (float)(length - 1) ? 0 : (float)idx).ToArray();
            float[] v4 = (new float[length]).Select((_, idx) => idx >= 3 && idx < (float)(length - 1) ? 0 : (float)idx).ToArray();

            float[] v5 = new float[length];

            AvxArray <float> arr = new AvxArray <float>(length);

            arr.Write(v, length);
            arr.Zeroset();
            arr.Read(v5, length);
            CollectionAssert.AreEqual(v2, v5);

            arr.Write(v, length);
            arr.Zeroset(length - 1);
            arr.Read(v5, length);
            CollectionAssert.AreEqual(v3, v5);

            arr.Write(v, length);
            arr.Zeroset(3, length - 4);
            arr.Read(v5, length);
            CollectionAssert.AreEqual(v4, v5);
        }
Exemplo n.º 6
0
        public void CopyTest()
        {
            const int length = 15;

            float[] v  = (new float[length]).Select((_, idx) => (float)idx).ToArray();
            float[] v3 = (new float[length]).Select((_, idx) => idx < (float)(length - 1) ? (float)idx : 0).ToArray();
            float[] v4 = (new float[length]).Select((_, idx) => idx >= 3 && idx < (float)(length - 1) ? (float)idx : 0).ToArray();
            float[] v5 = (new float[length]).Select((_, idx) => idx >= 2 && idx < (float)(length - 2) ? (float)idx + 1 : 0).ToArray();

            float[] v6 = new float[length];

            AvxArray <float> arr  = new AvxArray <float>(v);
            AvxArray <float> arr2 = new AvxArray <float>(length);

            arr2.Zeroset();
            arr.CopyTo(arr2, arr.Length);
            arr2.Read(v6);
            CollectionAssert.AreEqual(v, v6);

            arr2.Zeroset();
            arr.CopyTo(arr2, arr.Length - 1);
            arr2.Read(v6);
            CollectionAssert.AreEqual(v3, v6);

            arr2.Zeroset();
            arr.CopyTo(3, arr2, 3, arr.Length - 4);
            arr2.Read(v6);
            CollectionAssert.AreEqual(v4, v6);

            arr2.Zeroset();
            arr.CopyTo(3, arr2, 2, arr.Length - 4);
            arr2.Read(v6);
            CollectionAssert.AreEqual(v5, v6);
        }
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                float[] y = new float[length + 1];

                AvxArray <float> vx = x, vy = y;

                Elementwise.ClampConstant((uint)length, -0.5f, 0.5f, vx, vy);

                y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(Math.Min(Math.Max(x[i], -0.5f), 0.5f), y[i], $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
Exemplo n.º 8
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 10 - 5).ToArray();
                float[] y = new float[length + 1];

                AvxArray <float> vx = x, vy = y;

                Elementwise.Exp2((uint)length, vx, vy);

                y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(Math.Pow(2, x[i]), y[i], 1e-3f, $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
Exemplo n.º 9
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x1 = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                float[] x2 = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                float[] y  = new float[length + 1];

                AvxArray <float> vx1 = x1, vx2 = x2, vy = y;

                Elementwise.EluGrad((uint)length, 0.25f, vx1, vx2, vy);

                x1 = vx1; x2 = vx2; y = vy;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(x2[i] > 0 ? x1[i] : x1[i] * (0.25f * Math.Exp(x2[i])), y[i], 1e-4f, $"index:{i}");
                }

                Assert.AreEqual(0f, y[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }
Exemplo n.º 10
0
        /// <summary>操作を実行</summary>
        public override void Execute(params Tensor[] tensors)
        {
            CheckArgumentShapes(tensors);

            AvxArray <float>[] inmaps = tensors.Take(tensors.Length - 1).Select((tensor) => tensor.Buffer).ToArray();
            AvxArray <float>   refmap = tensors.Last().Buffer;

            TensorShaderAvxBackend.Elementwise.Sum((uint)Shape.Length, inmaps, refmap);
        }
Exemplo n.º 11
0
        public void DenormalizedTest()
        {
            AvxArray <float> vx1 = new float[] { 0, 1, -1, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
            AvxArray <float> vx2 = new float[] { 1, 1, 1, 1, 1, 1 };
            AvxArray <float> vx3 = new float[] { -1, -1, -1, -1, -1, -1 };

            float[]          y;
            AvxArray <float> vy = new AvxArray <float>(vx1.Length);

            Elementwise.EluGrad((uint)vx1.Length, 0.25f, vx1, vx2, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(1, y[1]);
            Assert.AreEqual(-1, y[2]);
            Assert.AreEqual(float.PositiveInfinity, y[3]);
            Assert.AreEqual(float.NegativeInfinity, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));

            Elementwise.EluGrad((uint)vx1.Length, 0.25f, vx1, vx3, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(0.25f * Math.Exp(-1), y[1], 1e-4f);
            Assert.AreEqual(-0.25f * Math.Exp(-1), y[2], 1e-4f);
            Assert.AreEqual(float.PositiveInfinity, y[3]);
            Assert.AreEqual(float.NegativeInfinity, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));

            Elementwise.EluGrad((uint)vx1.Length, 0.25f, vx2, vx1, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(1, y[1]);
            Assert.AreEqual(0.25f * Math.Exp(-1), y[2], 1e-4f);
            Assert.AreEqual(1, y[3]);
            Assert.AreEqual(0, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));

            Elementwise.EluGrad((uint)vx1.Length, 0.25f, vx3, vx1, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(-1, y[1]);
            Assert.AreEqual(-0.25f * Math.Exp(-1), y[2], 1e-4f);
            Assert.AreEqual(-1, y[3]);
            Assert.AreEqual(0, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));
        }
Exemplo n.º 12
0
        public void CreateTest()
        {
            const int length = 15;

            AvxArray <float> arr = new AvxArray <float>(length);

            Assert.IsTrue(arr.IsValid);
            Assert.AreEqual(Marshal.SizeOf(typeof(float)) * length, (int)arr.ByteSize);

            arr.Dispose();

            Assert.IsFalse(arr.IsValid);
            Assert.AreEqual(0, (int)arr.ByteSize);
        }
Exemplo n.º 13
0
        public void InitializeTest()
        {
            const int length = 15;

            float[] v = new float[length];

            AvxArray <float> arr = new AvxArray <float>(length);

            arr.Read(v);

            foreach (float f in v)
            {
                Assert.AreEqual(0.0f, f);
            }
        }
Exemplo n.º 14
0
        public void DenormalizedTest() {
            float[] x = new float[] { 0, 1, -1, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
            float[] y = new float[x.Length];

            AvxArray<float> vx = x, vy = y;

            Elementwise.Step((uint)x.Length, vx, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(1, y[1]);
            Assert.AreEqual(0, y[2]);
            Assert.AreEqual(1, y[3]);
            Assert.AreEqual(0, y[4]);
            Assert.AreEqual(0, y[5]);
        }
Exemplo n.º 15
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (uint dst_length = 1; dst_length <= 32; dst_length++)
            {
                for (uint src_length = dst_length; src_length <= dst_length * 32; src_length += dst_length)
                {
                    for (uint slides = 0; slides <= 4; slides++)
                    {
                        float[] x = (new float[src_length * slides + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                        float[] y = (new float[dst_length * slides + 1]).Select((_) => (float)rd.NextDouble() + 1).ToArray();

                        float v1 = y[dst_length * slides];

                        AvxArray <float> vx = x, vy = y;

                        Aggregation.Average(src_length, vx, dst_length, vy, slides);

                        y = vy;

                        double inv = (double)dst_length / src_length;

                        for (int j = 0; j < slides; j++)
                        {
                            double[] sum = new double[dst_length];

                            for (int i = 0; i < src_length; i++)
                            {
                                sum[i % dst_length] += x[i + j * src_length];
                            }

                            for (int i = 0; i < dst_length; i++)
                            {
                                Assert.AreEqual(sum[i] * inv, y[i + j * dst_length], 1e-5f);
                            }
                        }

                        Assert.AreEqual(v1, y[dst_length * slides]);

                        Console.WriteLine($"pass: {src_length} {dst_length} {slides}");
                    }
                }
            }
        }
Exemplo n.º 16
0
        public void WriteReadTest()
        {
            const int length = 15;

            float[] v  = (new float[length]).Select((_, idx) => (float)idx).ToArray();
            float[] v2 = new float[length];

            AvxArray <float> arr  = new AvxArray <float>(length);
            AvxArray <float> arr2 = new AvxArray <float>(length);

            arr.Write(v);

            AvxArray <float> .Copy(arr, arr2, length);

            arr2.Read(v2);

            CollectionAssert.AreEqual(v, v2);
        }
Exemplo n.º 17
0
        public void DenormalizedTest()
        {
            float[] x = new float[] { 0, 1, -1, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
            float[] y = new float[x.Length];

            AvxArray <float> vx = x, vy = y;

            Elementwise.Exp2((uint)x.Length, vx, vy);

            y = vy;

            Assert.AreEqual(1, y[0]);
            Assert.AreEqual(2, y[1], 1e-4f);
            Assert.AreEqual(0.5f, y[2], 1e-4f);
            Assert.AreEqual(float.PositiveInfinity, y[3]);
            Assert.AreEqual(0, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));
        }
Exemplo n.º 18
0
        public void DenormalizedTest()
        {
            float[] x = new float[] { 0, 1, -1, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
            float[] y = new float[x.Length];

            AvxArray <float> vx = x, vy = y;

            Elementwise.Rcbrt((uint)x.Length, vx, vy);

            y = vy;

            //Assert.IsTrue(float.IsNaN(y[0])); //_mm256_invcbrt_ps(zero) = +inf
            Assert.AreEqual(1, y[1], 1e-6f);
            Assert.AreEqual(-1, y[2], 1e-6f);
            Assert.AreEqual(0, y[3]);
            Assert.AreEqual(0, y[4]);
            Assert.IsTrue(float.IsNaN(y[5]));
        }
Exemplo n.º 19
0
        public void DenormalizedTest()
        {
            float[] x = new float[] { 0, 1, -1, float.PositiveInfinity, float.NegativeInfinity, float.NaN };
            float[] y = new float[x.Length];

            AvxArray <float> vx = x, vy = y;

            Elementwise.Sign((uint)x.Length, vx, vy);

            y = vy;

            Assert.AreEqual(0, y[0]);
            Assert.AreEqual(1, y[1]);
            Assert.AreEqual(-1, y[2]);
            Assert.AreEqual(1, y[3]);
            Assert.AreEqual(-1, y[4]);
            Assert.AreEqual(0, y[5]); //Math.Sign(float.NaN) : throw System.ArithmeticException
        }
Exemplo n.º 20
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (uint dst_length = 1; dst_length <= 32; dst_length++)
            {
                for (uint src_length = dst_length; src_length <= dst_length * 32; src_length += dst_length)
                {
                    for (uint slides = 0; slides <= 4; slides++)
                    {
                        float[] x = (new float[src_length * slides + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                        float[] y = (new float[dst_length * slides + 1]).Select((_) => (float)rd.NextDouble() + 1).ToArray();

                        float v1 = y[dst_length * slides];

                        AvxArray <float> vx = x, vy = y;

                        Aggregation.Max(src_length, vx, dst_length, vy, slides);

                        y = vy;

                        for (int j = 0; j < slides; j++)
                        {
                            float[] max = (new float[dst_length]).Select((_) => float.NegativeInfinity).ToArray();

                            for (int i = 0; i < src_length; i++)
                            {
                                max[i % dst_length] = Math.Max(max[i % dst_length], x[i + j * src_length]);
                            }

                            for (int i = 0; i < dst_length; i++)
                            {
                                Assert.AreEqual(max[i], y[i + j * dst_length], 1e-5f);
                            }
                        }

                        Assert.AreEqual(v1, y[dst_length * slides]);

                        Console.WriteLine($"pass: {src_length} {dst_length} {slides}");
                    }
                }
            }
        }
Exemplo n.º 21
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (uint copy_channels = 0; copy_channels <= 32; copy_channels++)
            {
                for (uint slides = 1; slides <= 100; slides++)
                {
                    float[] x = (new float[(copy_channels + 2) * slides]).Select((_) => (float)rd.NextDouble()).ToArray();
                    float[] y = (new float[(copy_channels + 4) * slides]).Select((_) => (float)rd.NextDouble()).ToArray();

                    float[] x_copy = (float[])x.Clone(), y_copy = (float[])y.Clone();

                    AvxArray <float> vx = x;
                    AvxArray <float> vy = y;

                    ArrayManipulation.PatternCopy(copy_channels + 2, 1, copy_channels + 4, 2, copy_channels, slides, vx, vy);

                    x = vx;
                    y = vy;

                    CollectionAssert.AreEqual(x_copy, x);

                    for (uint i = 0; i < slides; i++)
                    {
                        Assert.AreEqual(y_copy[i * (copy_channels + 4) + 0], y[i * (copy_channels + 4) + 0], $"slides:{slides}, copys:{copy_channels}, index:{i}");
                        Assert.AreEqual(y_copy[i * (copy_channels + 4) + 1], y[i * (copy_channels + 4) + 1], $"slides:{slides}, copys:{copy_channels}, index:{i}");

                        for (uint j = 0; j < copy_channels; j++)
                        {
                            Assert.AreEqual(x[i * (copy_channels + 2) + (j + 1)], y[i * (copy_channels + 4) + (j + 2)], $"slides:{slides}, copys:{copy_channels}, index:{i}, {j}");
                        }

                        Assert.AreEqual(y_copy[i * (copy_channels + 4) + (copy_channels + 2)], y[i * (copy_channels + 4) + (copy_channels + 2)], $"slides:{slides}, copys:{copy_channels}, index:{i}");
                        Assert.AreEqual(y_copy[i * (copy_channels + 4) + (copy_channels + 3)], y[i * (copy_channels + 4) + (copy_channels + 3)], $"slides:{slides}, copys:{copy_channels}, index:{i}");
                    }

                    Console.WriteLine($"pass:{slides}, {copy_channels}");
                }
            }
        }
Exemplo n.º 22
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                for (int n = 0; n < 12; n++)
                {
                    float[][] xs = new float[n][];

                    for (int j = 0; j < n; j++)
                    {
                        xs[j] = (new float[length + 1]).Select((_) => (float)rd.NextDouble()).ToArray();
                    }

                    AvxArray <float>[] vxs = xs.Select((x) => new AvxArray <float>(x)).ToArray();

                    float[]          y  = new float[length + 1];
                    AvxArray <float> vy = y;

                    Elementwise.Sum((uint)length, vxs, vy);

                    y = vy;

                    for (int i = 0; i < length; i++)
                    {
                        float sum = 0;
                        for (int j = 0; j < n; j++)
                        {
                            sum += xs[j][i];
                        }

                        Assert.AreEqual(sum, y[i], 1e-5f, $"index:{i}");
                    }

                    Assert.AreEqual(0f, y[length], $"index:{length}");

                    Console.WriteLine($"pass:{length}, {n}");
                }
            }
        }
Exemplo n.º 23
0
        public void SpeedTest()
        {
            int length = 10000000;

            for (int n = 0; n < 12; n++)
            {
                float[][] xs = (new float[n][]).Select((_) => new float[length]).ToArray();
                float[]   y  = new float[length];

                AvxArray <float>[] vxs = xs.Select((x) => new AvxArray <float>(x)).ToArray();
                AvxArray <float>   vy  = y;

                Stopwatch sw = new Stopwatch();

                sw.Start();

                Elementwise.Sum((uint)length, vxs, vy);

                sw.Stop();

                Console.WriteLine($"{n} : {sw.ElapsedMilliseconds} msec");
            }
        }
Exemplo n.º 24
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (uint src_length = 1; src_length <= 32; src_length++)
            {
                for (uint dst_length = src_length; dst_length <= src_length * 32; dst_length += src_length)
                {
                    for (uint slides = 0; slides <= 4; slides++)
                    {
                        float[] x = (new float[src_length * slides + 1]).Select((_) => (float)rd.NextDouble() * 2 - 1).ToArray();
                        float[] y = (new float[dst_length * slides + 1]).Select((_) => (float)rd.NextDouble() + 1).ToArray();

                        float v1 = y[dst_length * slides];

                        AvxArray <float> vx = x, vy = y;

                        ArrayManipulation.Broadcast(src_length, vx, dst_length, vy, slides);

                        y = vy;

                        for (int j = 0; j < slides; j++)
                        {
                            for (int i = 0; i < dst_length; i++)
                            {
                                Assert.AreEqual(x[i % src_length + j * src_length], y[i + j * dst_length]);
                            }
                        }

                        Assert.AreEqual(v1, y[dst_length * slides]);

                        Console.WriteLine($"pass: {src_length} {dst_length} {slides}");
                    }
                }
            }
        }
Exemplo n.º 25
0
        public void ExecuteTest()
        {
            Random rd = new Random(1234);

            for (int length = 0; length < 1000; length++)
            {
                float[] x = (new float[length + 1]).Select((_) => (float)rd.NextDouble() * 10 - 5).ToArray();

                AvxArray <float> vx = x;

                ArrayManipulation.Clear((uint)length, 0.5f, vx);

                x = vx;

                for (int i = 0; i < length; i++)
                {
                    Assert.AreEqual(0.5f, x[i], $"index:{i}");
                }

                Assert.AreNotEqual(0.5f, x[length], $"index:{length}");

                Console.WriteLine($"pass:{length}");
            }
        }