public void ReferenceTest()
        {
            int inchannels = 8, outchannels = 12, batch = 3;

            float[] xval = (new float[batch * inchannels]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels / 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Quaternion[] xcval = (new Quaternion[xval.Length / 4])
                                 .Select((_, idx) => new Quaternion(xval[idx * 4], xval[idx * 4 + 1], xval[idx * 4 + 2], xval[idx * 4 + 3])).ToArray();

            Quaternion[] wcval = (new Quaternion[wval.Length / 4])
                                 .Select((_, idx) => new Quaternion(wval[idx * 4], wval[idx * 4 + 1], wval[idx * 4 + 2], wval[idx * 4 + 3])).ToArray();

            QuaternionMap0D    x = new QuaternionMap0D(inchannels / 4, batch, xcval);
            QuaternionFilter0D w = new QuaternionFilter0D(inchannels / 4, outchannels / 4, wcval);

            QuaternionMap0D y = Reference(x, w);

            float[] y_expect =
            {
                -3.520000e-04f, 1.440000e-04f,  3.200000e-04f, 2.200000e-04f, -1.920000e-04f, 8.000000e-05f,
                1.920000e-04f,  1.240000e-04f, -3.200000e-05f, 1.600000e-05f,  6.400000e-05f, 2.800000e-05f,
                -9.280000e-04f, 7.840000e-04f,  9.920000e-04f, 8.280000e-04f, -5.120000e-04f, 4.640000e-04f,
                6.080000e-04f,  4.760000e-04f, -9.600000e-05f, 1.440000e-04f,  2.240000e-04f, 1.240000e-04f,
                -1.504000e-03f, 1.424000e-03f,  1.664000e-03f, 1.436000e-03f, -8.320000e-04f, 8.480000e-04f,
                1.024000e-03f,  8.280000e-04f, -1.600000e-04f, 2.720000e-04f,  3.840000e-04f, 2.200000e-04f,
            };

            float[] y_actual = y.ToArray();

            AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
        public void ReferenceTest()
        {
            int inchannels = 8, outchannels = 12, batch = 3;

            float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Quaternion[] xcval = (new Quaternion[xval.Length / 4])
                                 .Select((_, idx) => new Quaternion(xval[idx * 4], xval[idx * 4 + 1], xval[idx * 4 + 2], xval[idx * 4 + 3])).ToArray();

            Quaternion[] ycval = (new Quaternion[yval.Length / 4])
                                 .Select((_, idx) => new Quaternion(yval[idx * 4], yval[idx * 4 + 1], yval[idx * 4 + 2], yval[idx * 4 + 3])).ToArray();

            QuaternionMap0D x = new QuaternionMap0D(inchannels / 4, batch, xcval);
            QuaternionMap0D y = new QuaternionMap0D(outchannels / 4, batch, ycval);

            QuaternionFilter0D gw = Reference(x, y);

            float[] gw_expect =
            {
                1.668000e-03f,  -1.084202e-19f, -3.720000e-04f, -1.860000e-04f,  2.700000e-03f,  1.084202e-19f,
                -4.200000e-04f, -2.100000e-04f,  1.212000e-03f, -0.000000e+00f, -3.240000e-04f, -1.620000e-04f,
                2.052000e-03f,   1.084202e-19f, -3.720000e-04f, -1.860000e-04f,  7.560000e-04f, -0.000000e+00f,
                -2.760000e-04f, -1.380000e-04f,  1.404000e-03f,  5.421011e-20f, -3.240000e-04f, -1.620000e-04f,
            };

            float[] gw_actual = gw.ToArray();

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
예제 #3
0
        public void ReferenceTest()
        {
            int inchannels = 8, outchannels = 12, batch = 3;

            float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels / 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Quaternion[] ycval = (new Quaternion[yval.Length / 4])
                                 .Select((_, idx) => new Quaternion(yval[idx * 4], yval[idx * 4 + 1], yval[idx * 4 + 2], yval[idx * 4 + 3])).ToArray();

            Quaternion[] wcval = (new Quaternion[wval.Length / 4])
                                 .Select((_, idx) => new Quaternion(wval[idx * 4], wval[idx * 4 + 1], wval[idx * 4 + 2], wval[idx * 4 + 3])).ToArray();

            QuaternionMap0D    y = new QuaternionMap0D(outchannels / 4, batch, ycval);
            QuaternionFilter0D w = new QuaternionFilter0D(inchannels / 4, outchannels / 4, wcval);

            QuaternionMap0D x = Reference(y, w);

            float[] x_expect =
            {
                -3.880000e-04f, 2.080000e-04f,  4.120000e-04f, 2.740000e-04f, -2.200000e-04f, 1.120000e-04f,
                2.680000e-04f,  1.540000e-04f, -1.252000e-03f, 1.216000e-03f,  1.492000e-03f, 1.210000e-03f,
                -7.960000e-04f, 8.320000e-04f,  1.060000e-03f, 8.020000e-04f, -2.116000e-03f, 2.224000e-03f,
                2.572000e-03f,  2.146000e-03f, -1.372000e-03f, 1.552000e-03f,  1.852000e-03f, 1.450000e-03f,
            };

            float[] x_actual = x.ToArray();

            AssertError.Tolerance(x_expect, x_actual, 1e-7f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
        public void ExecuteTest()
        {
            float max_err = 0;

            foreach (int batch in new int[] { 1, 2, 3 })
            {
                foreach (int inchannels in new int[] { 4, 8, 12 })
                {
                    foreach (int outchannels in new int[] { 4, 8, 12 })
                    {
                        float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                        float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

                        Quaternion[] xcval = (new Quaternion[xval.Length / 4])
                                             .Select((_, idx) => new Quaternion(xval[idx * 4], xval[idx * 4 + 1], xval[idx * 4 + 2], xval[idx * 4 + 3])).ToArray();

                        Quaternion[] ycval = (new Quaternion[yval.Length / 4])
                                             .Select((_, idx) => new Quaternion(yval[idx * 4], yval[idx * 4 + 1], yval[idx * 4 + 2], yval[idx * 4 + 3])).ToArray();

                        QuaternionMap0D x = new QuaternionMap0D(inchannels / 4, batch, xcval);
                        QuaternionMap0D y = new QuaternionMap0D(outchannels / 4, batch, ycval);

                        QuaternionFilter0D gw = Reference(x, y);

                        OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map0D(inchannels, batch), xval);
                        OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map0D(outchannels, batch), yval);

                        OverflowCheckedTensor gw_tensor = new OverflowCheckedTensor(Shape.Kernel0D(inchannels, outchannels / 4));

                        QuaternionKernelProductDense ope = new QuaternionKernelProductDense(inchannels, outchannels, transpose: false, batch);

                        ope.Execute(x_tensor, y_tensor, gw_tensor);

                        float[] gw_expect = gw.ToArray();
                        float[] gw_actual = gw_tensor.State;

                        CollectionAssert.AreEqual(xval, x_tensor.State);
                        CollectionAssert.AreEqual(yval, y_tensor.State);

                        AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {inchannels},{outchannels},{batch}");

                        Console.WriteLine($"pass: {inchannels},{outchannels},{batch}");
                    }
                }
            }

            Console.WriteLine($"maxerr:{max_err}");
        }
예제 #5
0
        public static QuaternionMap0D Reference(QuaternionMap0D y, QuaternionFilter0D w)
        {
            int inchannels = w.InChannels, outchannels = w.OutChannels, batch = y.Batch;

            QuaternionMap0D x = new QuaternionMap0D(inchannels, batch);

            for (int th = 0; th < batch; th++)
            {
                for (int outch = 0; outch < outchannels; outch++)
                {
                    Quaternion v = y[outch, th];

                    for (int inch = 0; inch < inchannels; inch++)
                    {
                        x[inch, th] += v * w[inch, outch];
                    }
                }
            }

            return(x);
        }
        public static QuaternionFilter0D Reference(QuaternionMap0D x, QuaternionMap0D gy)
        {
            int inchannels = x.Channels, outchannels = gy.Channels, batch = x.Batch;

            QuaternionFilter0D w = new QuaternionFilter0D(inchannels, outchannels);

            for (int inch, outch = 0; outch < outchannels; outch++)
            {
                for (inch = 0; inch < inchannels; inch++)
                {
                    Quaternion sum = 0;

                    for (int th = 0; th < batch; th++)
                    {
                        sum += Quaternion.MulGrad(gy[outch, th], x[inch, th]);
                    }

                    w[inch, outch] = sum;
                }
            }

            return(w);
        }
        public static QuaternionMap0D Reference(QuaternionMap0D x, QuaternionFilter0D w)
        {
            int inchannels = x.Channels, outchannels = w.OutChannels, batch = x.Batch;

            QuaternionMap0D y = new QuaternionMap0D(outchannels, batch);

            for (int th = 0; th < batch; th++)
            {
                for (int outch = 0; outch < outchannels; outch++)
                {
                    Quaternion sum = y[outch, th];

                    for (int inch = 0; inch < inchannels; inch++)
                    {
                        sum += x[inch, th] * w[inch, outch];
                    }

                    y[outch, th] = sum;
                }
            }

            return(y);
        }