diff --git a/test/ccunit/mace/ops/lpnorm_test.cc b/test/ccunit/mace/ops/lpnorm_test.cc index c397ab94e6eb7e43b3912b25e2ded667d421b79b..077624ccbda6f8ebf02d07005029af262e1da6d6 100644 --- a/test/ccunit/mace/ops/lpnorm_test.cc +++ b/test/ccunit/mace/ops/lpnorm_test.cc @@ -121,6 +121,63 @@ TEST_F(LpNormOpTest, SimpleTestSquareOpenCL2) { 0.680451, 0.732793, 0.683941, 0.729537}); } + +namespace { +template +void TestLpNormRandom(const std::vector &input_shape, + const int p, + const int axis) { + // Construct graph + OpsTestNet net; + + // Add input data + net.AddRandomInput("Input", input_shape); + + net.TransformDataFormat( + "Input", DataFormat::NHWC, "InputNCHW", DataFormat::NCHW); + + OpDefBuilder("LpNorm", "LpNormTest") + .Input("InputNCHW") + .Output("OutputNCHW") + .AddIntArg("p", p) + .AddIntArg("axis", axis) + .Finalize(net.NewOperatorDef()); + + // run on cpu + net.RunOp(); + + net.TransformDataFormat( + "OutputNCHW", DataFormat::NCHW, "Output", DataFormat::NHWC); + + auto expected = net.CreateTensor(); + expected->Copy(*net.GetOutput("Output")); + + OpDefBuilder("LpNorm", "LpNormTest") + .Input("Input") + .Output("Output") + .AddIntArg("p", p) + .AddIntArg("axis", axis) + .AddIntArg("T", static_cast(DataTypeToEnum::value)) + .Finalize(net.NewOperatorDef()); + net.RunOp(D); + + if (DataTypeToEnum::value == DT_HALF) { + ExpectTensorNear(*expected, *net.GetOutput("Output"), 1e-2, 1e-3); + } else { + ExpectTensorNear(*expected, *net.GetOutput("Output"), 1e-5); + } +} + +} // namespace + +TEST_F(LpNormOpTest, SimpleTestSquareHalfOpenCL) { + TestLpNormRandom({1, 8, 1, 2}, 2, 1); +} + +TEST_F(LpNormOpTest, SimpleTestSquareHalfOpenCL2) { + TestLpNormRandom({1, 8, 1, 2}, 2, 2); +} + } // namespace test } // namespace ops } // namespace mace diff --git a/test/ccunit/mace/ops/mvnorm_test.cc b/test/ccunit/mace/ops/mvnorm_test.cc index 14b73e2e29fef63aa12428f3cdea6c53736d0995..3bafaa1b714492e9ce717aba33ee54fe44681830 100644 --- a/test/ccunit/mace/ops/mvnorm_test.cc +++ b/test/ccunit/mace/ops/mvnorm_test.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "mace/core/types.h" #include "mace/ops/ops_test_util.h" namespace mace { @@ -161,6 +162,61 @@ TEST_F(MVNormOpTest, SimpleTestVarianceOpenCL) { 1.23241, 1.45648, 1.68056, 1.90463, 2.1287}); } +namespace { +template +void TestMVNormRandom(const std::vector &input_shape, + bool normalize_variance, + bool across_channels) { + // Construct graph + OpsTestNet net; + + // Add input data + net.AddRandomInput("Input", input_shape); + + net.TransformDataFormat( + "Input", DataFormat::NHWC, "InputNCHW", DataFormat::NCHW); + + OpDefBuilder("MVNorm", "MVNormTest") + .Input("InputNCHW") + .Output("OutputNCHW") + .AddIntArg("normalize_variance", normalize_variance) + .AddIntArg("across_channels", across_channels) + .Finalize(net.NewOperatorDef()); + + // run on cpu + net.RunOp(); + + net.TransformDataFormat( + "OutputNCHW", DataFormat::NCHW, "Output", DataFormat::NHWC); + + auto expected = net.CreateTensor(); + expected->Copy(*net.GetOutput("Output")); + + OpDefBuilder("MVNorm", "MVNormTest") + .Input("Input") + .Output("Output") + .AddIntArg("normalize_variance", normalize_variance) + .AddIntArg("across_channels", across_channels) + .AddIntArg("T", static_cast(DataTypeToEnum::value)) + .Finalize(net.NewOperatorDef()); + net.RunOp(D); + + if (DataTypeToEnum::value == DT_HALF) { + ExpectTensorNear(*expected, *net.GetOutput("Output"), 1e-2, 1e-3); + } else { + ExpectTensorNear(*expected, *net.GetOutput("Output"), 1e-5); + } +} +} // namespace + +TEST_F(MVNormOpTest, SimpleTestMeanHalfOpenCL) { + TestMVNormRandom({1, 1, 5, 12}, false, true); +} + +TEST_F(MVNormOpTest, SimpleTestVarianceHalfOpenCL) { + TestMVNormRandom({1, 1, 5, 12}, true, true); +} + } // namespace test } // namespace ops } // namespace mace