public void SpeedTest() { int inwidth = 512, inheight = 512, inchannels = 32, outchannels = 32, ksize = 3, stride = 2; int outwidth = (inwidth - ksize) / stride + 1, outheight = (inheight - ksize) / stride + 1; OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight)); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels, outchannels / 4, ksize, ksize)); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight)); QuaternionConvolution2D ope = new QuaternionConvolution2D(inwidth, inheight, inchannels, outchannels, ksize, ksize, stride); Stopwatch sw = new Stopwatch(); sw.Start(); ope.Execute(x_tensor, w_tensor, y_tensor); ope.Execute(x_tensor, w_tensor, y_tensor); ope.Execute(x_tensor, w_tensor, y_tensor); ope.Execute(x_tensor, w_tensor, y_tensor); sw.Stop(); Console.WriteLine($"{sw.ElapsedMilliseconds / 4} msec"); }
public void OverflowTest() { foreach (bool gradmode in new bool[] { false, true }) { 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 }) { foreach (int kheight in new int[] { 1, 3, 5 }) { foreach (int kwidth in new int[] { 1, 3, 5 }) { foreach (int stride in new int[] { 1, 2, 3 }) { foreach (int inwidth in new int[] { 8, 9, 13, 17 }) { foreach (int inheight in new int[] { 8, 9, 19, 23 }) { int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1; float[] xval = (new float[inwidth * inheight * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[kwidth * kheight * inchannels * outchannels / 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray(); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight, batch), xval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels, outchannels / 4, kwidth, kheight), wval); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight, batch)); QuaternionConvolution2D ope = new QuaternionConvolution2D(inwidth, inheight, inchannels, outchannels, kwidth, kheight, stride, gradmode, batch); ope.Execute(x_tensor, w_tensor, y_tensor); CollectionAssert.AreEqual(xval, x_tensor.State); CollectionAssert.AreEqual(wval, w_tensor.State); y_tensor.CheckOverflow(); Console.WriteLine($"pass: {inchannels},{outchannels},{kwidth},{kheight},{stride},{inwidth},{inheight},{batch},{gradmode}"); } } } } } } } } } }
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 }) { foreach (int kheight in new int[] { 1, 3, 5 }) { foreach (int kwidth in new int[] { 1, 3, 5 }) { foreach (int stride in new int[] { 1, 2, 3 }) { foreach (int inwidth in new int[] { 8, 9, 13, 17 }) { foreach (int inheight in new int[] { 8, 9, 19, 23 }) { int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1; float[] xval = (new float[inwidth * inheight * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[kwidth * kheight * inchannels * outchannels / 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(); QuaternionMap2D x = new QuaternionMap2D(inchannels / 4, inwidth, inheight, batch, xcval); QuaternionFilter2D w = new QuaternionFilter2D(inchannels / 4, outchannels / 4, kwidth, kheight, wcval); QuaternionMap2D y = Reference(x, w, kwidth, kheight, stride); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight, batch), xval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels, outchannels / 4, kwidth, kheight), wval); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight, batch)); QuaternionConvolution2D ope = new QuaternionConvolution2D(inwidth, inheight, inchannels, outchannels, kwidth, kheight, stride, gradmode: false, batch); ope.Execute(x_tensor, w_tensor, y_tensor); float[] y_expect = y.ToArray(); float[] y_actual = y_tensor.State; CollectionAssert.AreEqual(xval, x_tensor.State); CollectionAssert.AreEqual(wval, w_tensor.State); AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {inchannels},{outchannels},{kwidth},{kheight},{stride},{inwidth},{inheight},{batch}"); Console.WriteLine($"pass: {inchannels},{outchannels},{kwidth},{kheight},{stride},{inwidth},{inheight},{batch}"); } } } } } } } } Console.WriteLine($"maxerr:{max_err}"); }