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 }) { float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[inchannels * outchannels / 9 * 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray(); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map0D(inchannels, batch), xval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel0D(inchannels / 3 * 4, outchannels / 3), wval); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map0D(outchannels, batch)); TrivectorDense ope = new TrivectorDense(inchannels, outchannels, 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},{batch},{gradmode}"); } } } } }
public void SpeedTest() { int inchannels = 33, outchannels = 33; OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map0D(inchannels)); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel0D(inchannels / 3 * 4, outchannels / 3)); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map0D(outchannels)); TrivectorDense ope = new TrivectorDense(inchannels, outchannels); 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 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 }) { float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray(); float[] wval = (new float[inchannels * outchannels / 9 * 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray(); Trivector[] xcval = (new Trivector[xval.Length / 3]) .Select((_, idx) => new Trivector(xval[idx * 3], xval[idx * 3 + 1], xval[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(); TrivectorMap0D x = new TrivectorMap0D(inchannels / 3, batch, xcval); Quaternion.QuaternionFilter0D w = new Quaternion.QuaternionFilter0D(inchannels / 3, outchannels / 3, wcval); TrivectorMap0D y = Reference(x, w); OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map0D(inchannels, batch), xval); OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel0D(inchannels / 3 * 4, outchannels / 3), wval); OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map0D(outchannels, batch)); TrivectorDense ope = new TrivectorDense(inchannels, outchannels, 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},{batch}"); Console.WriteLine($"pass: {inchannels},{outchannels},{batch}"); } } } Console.WriteLine($"maxerr:{max_err}"); }