public void SpeedTest() { int inwidth = 32, inheight = 32, inchannels = 33, outchannels = 33, ksize = 3, stride = 2; int outwidth = (inwidth - ksize) / stride + 1, outheight = (inheight - ksize) / stride + 1; OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight)); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels / 3 * 4, outchannels / 3, ksize, ksize)); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight)); TrivectorDeconvolution2D ope = new TrivectorDeconvolution2D(inwidth, inheight, outchannels, inchannels, ksize, ksize, stride); ope.Execute(y_tensor, w_tensor, x_tensor); Stopwatch sw = new Stopwatch(); sw.Start(); ope.Execute(y_tensor, w_tensor, x_tensor); ope.Execute(y_tensor, w_tensor, x_tensor); ope.Execute(y_tensor, w_tensor, x_tensor); ope.Execute(y_tensor, w_tensor, x_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[] { 3, 6, 9, 12 }) { foreach (int outchannels in new int[] { 3, 6, 9, 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[] yval = (new float[outwidth * outheight * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[kwidth * kheight * inchannels * outchannels / 9 * 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray(); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight, batch), yval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels / 3 * 4, outchannels / 3, kwidth, kheight), wval); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight, batch)); TrivectorDeconvolution2D ope = new TrivectorDeconvolution2D(inwidth, inheight, outchannels, inchannels, kwidth, kheight, stride, gradmode, batch); ope.Execute(y_tensor, w_tensor, x_tensor); CollectionAssert.AreEqual(yval, y_tensor.State); CollectionAssert.AreEqual(wval, w_tensor.State); x_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[] { 3, 6, 9, 12 }) { foreach (int outchannels in new int[] { 3, 6, 9, 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[] yval = (new float[outwidth * outheight * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[kwidth * kheight * inchannels * outchannels / 9 * 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray(); Trivector[] ycval = (new Trivector[yval.Length / 3]) .Select((_, idx) => new Trivector(yval[idx * 3], yval[idx * 3 + 1], yval[idx * 3 + 2])).ToArray(); Quaternion.Quaternion[] wcval = (new Quaternion.Quaternion[wval.Length / 4]) .Select((_, idx) => new Quaternion.Quaternion(wval[idx * 4], wval[idx * 4 + 1], wval[idx * 4 + 2], wval[idx * 4 + 3])).ToArray(); TrivectorMap2D y = new TrivectorMap2D(outchannels / 3, outwidth, outheight, batch, ycval); Quaternion.QuaternionFilter2D w = new Quaternion.QuaternionFilter2D(inchannels / 3, outchannels / 3, kwidth, kheight, wcval); TrivectorMap2D x = Reference(y, w, inwidth, inheight, kwidth, kheight, stride); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight, batch), yval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels / 3 * 4, outchannels / 3, kwidth, kheight), wval); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight, batch)); TrivectorDeconvolution2D ope = new TrivectorDeconvolution2D(inwidth, inheight, outchannels, inchannels, kwidth, kheight, stride, gradmode: false, batch); ope.Execute(y_tensor, w_tensor, x_tensor); float[] x_expect = x.ToArray(); float[] x_actual = x_tensor.State; CollectionAssert.AreEqual(yval, y_tensor.State); CollectionAssert.AreEqual(wval, w_tensor.State); AssertError.Tolerance(x_expect, x_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}"); }