diff --git a/onnxruntime/test/providers/cpu/nn/conv_fp16_test.cc b/onnxruntime/test/providers/cpu/nn/conv_fp16_test.cc index 3569b4a6cf299..3b0d8bd9777f4 100644 --- a/onnxruntime/test/providers/cpu/nn/conv_fp16_test.cc +++ b/onnxruntime/test/providers/cpu/nn/conv_fp16_test.cc @@ -1,8 +1,14 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -#include "core/graph/constants.h" + +#include "core/mlas/inc/mlas.h" + +#if defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) || defined(COREML_ENABLE_MLPROGRAM) || defined(USE_XNNPACK) + #include "gtest/gtest.h" #include "test/providers/provider_test_utils.h" +#include "test/providers/run_options_config_keys.h" +#include "default_providers.h" using namespace std; namespace onnxruntime { @@ -18,71 +24,130 @@ struct ConvOpAndTestAttributes { vector pads; vector strides; std::unordered_set excluded_providers; + string activation = ""; + vector activation_parameters = {}; }; -void TestConvOp(const ConvOpAndTestAttributes& attributes, - const vector>& inputs, - const vector>& input_shapes, - const std::initializer_list& expected_output, - const vector& expected_output_shape, - bool weight_is_initializer = false, - optional epsilon = optional(), - OpTester::ExpectResult expect_result = OpTester::ExpectResult::kExpectSuccess, - const std::string& err_str = "", - int opset = 7, - bool exclude_cuda_nhwc = false) { - OpTester test("Conv", opset); - test.AddAttribute("group", attributes.group); - test.AddAttribute("kernel_shape", attributes.kernel_shape); +/* +Please notice that, we have predefined macros in the head of the file +#if defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) || defined(COREML_ENABLE_MLPROGRAM) +When we have these two macro defines, this UT will turn into green light and work. + +If attributes.activation is set the NhwcFusedConv contrib op is used. +If you are adding support for a new EP to the test and the EP does not support NhwcFusedConv +please add the EP to the excluded_providers list. +*/ +void TestConvFp16Op(const ConvOpAndTestAttributes& attributes, + const vector>& inputs, + const vector>& input_shapes, + const std::initializer_list& expected_output, + const vector& expected_output_shape, + bool weight_is_initializer = false, + OpTester::ExpectResult expect_result = OpTester::ExpectResult::kExpectSuccess, + const std::string& err_str = "", + int opset = 11) { + std::unique_ptr tester; + if (!attributes.activation.empty()) { + tester = std::make_unique("NhwcFusedConv", 1, onnxruntime::kMSDomain); + tester->AddAttribute("activation", attributes.activation); + + if (!attributes.activation_parameters.empty()) { + tester->AddAttribute("activation_params", attributes.activation_parameters); + } + } else { + tester = std::make_unique("Conv", opset); + } + + tester->AddAttribute("group", attributes.group); + tester->AddAttribute("kernel_shape", attributes.kernel_shape); if (!attributes.dilations.empty()) { - test.AddAttribute("dilations", attributes.dilations); + tester->AddAttribute("dilations", attributes.dilations); } // Only one of pads / auto_pad can be present if (!attributes.pads.empty()) { - test.AddAttribute("pads", attributes.pads); + tester->AddAttribute("pads", attributes.pads); } else { - test.AddAttribute("auto_pad", attributes.auto_pad); + tester->AddAttribute("auto_pad", attributes.auto_pad); } if (!attributes.strides.empty()) { - test.AddAttribute("strides", attributes.strides); + tester->AddAttribute("strides", attributes.strides); } - ORT_ENFORCE(inputs.size() <= 3, "Our name array is only setup to handle 3 inputs"); - const char* szNames[] = {"X", "W", "B"}; - test.AddInput(szNames[0], input_shapes[0], inputs[0]); - test.AddInput(szNames[1], input_shapes[1], inputs[1], weight_is_initializer); - if (inputs.size() == 3) - test.AddInput(szNames[2], input_shapes[2], inputs[2]); + ORT_ENFORCE(inputs.size() <= 4, "Our name array is only setup to handle 4 inputs"); + const char* szNames[] = {"X", "W", "B", "Z"}; + tester->AddInput(szNames[0], input_shapes[0], inputs[0]); + tester->AddInput(szNames[1], input_shapes[1], inputs[1], weight_is_initializer); + if (inputs.size() >= 3) + tester->AddInput(szNames[2], input_shapes[2], inputs[2]); + if (inputs.size() >= 4) + tester->AddInput(szNames[3], input_shapes[3], inputs[3]); - test.AddOutput("Y", expected_output_shape, expected_output); - - if (epsilon.has_value()) { - test.SetOutputTolerance(*epsilon); - } + tester->AddOutput("Y", expected_output_shape, expected_output, /*no sort*/ false, 0.002f, 0.0f); std::unordered_set excluded_providers(attributes.excluded_providers); // Disable TensorRT because weight as input is not supported excluded_providers.insert(kTensorrtExecutionProvider); - - if (exclude_cuda_nhwc) { -#ifdef ENABLE_CUDA_NHWC_OPS - excluded_providers.insert(kCudaNHWCExecutionProvider); -#endif + // QNN has issue with dynamic weight, auto pad with SAME_UPPER, SAME_LOWER + if (!weight_is_initializer || attributes.auto_pad == "SAME_UPPER" || attributes.auto_pad == "SAME_LOWER") { + excluded_providers.insert(kQnnExecutionProvider); + } + if (!weight_is_initializer || !attributes.activation.empty()) { + excluded_providers.insert(kCoreMLExecutionProvider); } + tester->Run(expect_result, err_str, excluded_providers); +} - // QNN SDK 2.10.0 has a bug that breaks support for dynamic bias inputs. - excluded_providers.insert(kQnnExecutionProvider); +} // namespace + +TEST(ConvFp16Test, Conv1D_Invalid_Input_Shape) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1}, // dilations + 1, // group + vector{2}, // kernel_shape + vector{0, 0}, // pads + vector{1}, // strides + {} // excluded EPs + }; - test.Run(expect_result, err_str, excluded_providers); + vector X = vector(1, MLFloat16(1.0f)); + vector X_shape = {1, 1, 1}; + vector dummy_shape = {1, 1, 2}; + auto dummy_vals = {MLFloat16(0.0f), MLFloat16(0.0f)}; + TestConvFp16Op(attrs, {X, dummy_vals}, {X_shape, dummy_shape}, dummy_vals, dummy_shape, false, + OpTester::ExpectResult::kExpectFailure, + "Node:node1 Output:Y [ShapeInferenceError] Can't merge shape info. " + "Both inferred and declared dimension have values but they differ. Inferred=0 Declared=2 Dimension=2", + -1); // use latest opset for shape inferencing errors } -} // namespace +TEST(ConvFp16Test, Conv2D_Invalid_Input_Shape) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1}, // dilations + 1, // group + vector{3, 3}, // kernel_shape + vector{0, 0, 0, 0}, // pads + vector{1, 1}, // strides + {} // excluded EPs + }; -// Conv -TEST(ConvTest, Conv1D_1) { + vector X = vector(1 * 3 * 1 * 111, MLFloat16(1.0f)); + vector X_shape = {1, 3, 1, 111}; + vector dummy_shape = {2, 2, 1, 2}; + auto dummy_vals = {MLFloat16(-0.0f), MLFloat16(0.0f), MLFloat16(-0.0f), MLFloat16(-0.0f), + MLFloat16(-0.0f), MLFloat16(0.0f), MLFloat16(-0.0f), MLFloat16(-0.0f)}; + TestConvFp16Op(attrs, {X, dummy_vals}, {X_shape, dummy_shape}, dummy_vals, dummy_shape, false, + OpTester::ExpectResult::kExpectFailure, + "Node:node1 Output:Y [ShapeInferenceError] Can't merge shape info. " + "Both inferred and declared dimension have values but they differ. Inferred=1 Declared=2 Dimension=0", + -1); // use latest opset for shape inferencing errors +} + +TEST(ConvFp16Test, Conv1D_1) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1}, // dilations @@ -93,22 +158,21 @@ TEST(ConvTest, Conv1D_1) { {} // excluded EPs }; - vector X = {-0.21559301018714905f, 0.4691687822341919f, 0.4426700472831726f, -0.4517466723918915f, - -0.05216419696807861f, 0.29067182540893555f, 0.251010000705719f}; + vector X = {MLFloat16(-0.215576172f), MLFloat16(0.469238281f), MLFloat16(0.442626953f), + MLFloat16(-0.451660156f), MLFloat16(-0.0521545410f), MLFloat16(0.290771484f), MLFloat16(0.250976562f)}; vector X_shape = {1, 1, 7}; - vector W = {0.24472862482070923f}; + vector W = {MLFloat16(0.244750977f)}; vector W_shape = {1, 1, 1}; vector Y_shape = {1, 1, 7}; - auto expected_vals = {-0.052761781960725784f, 0.11481902748346329f, 0.10833403468132019f, -0.11055534332990646f, - -0.012766072526574135f, 0.07113571465015411f, 0.061429332941770554f}; + auto expected_vals = {MLFloat16(-0.0527624786f), MLFloat16(0.114846528f), MLFloat16(0.108333379f), + MLFloat16(-0.110544264f), MLFloat16(-0.0127648748f), MLFloat16(0.0711666048f), MLFloat16(0.0614267588f)}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); - // CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv1D_1_DefaultStridesAndDilations) { +TEST(ConvFp16Test, Conv1D_1_DefaultStridesAndDilations) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{}, // dilations @@ -119,23 +183,24 @@ TEST(ConvTest, Conv1D_1_DefaultStridesAndDilations) { {} // excluded EPs }; - vector X = {-0.21559301018714905f, 0.4691687822341919f, 0.4426700472831726f, -0.4517466723918915f, - -0.05216419696807861f, 0.29067182540893555f, 0.251010000705719f}; + vector X = {MLFloat16(-0.215576172f), MLFloat16(0.469238281f), MLFloat16(0.442626953f), + MLFloat16(-0.451660156f), MLFloat16(-0.0521545410f), MLFloat16(0.290771484f), + MLFloat16(0.250976562f)}; vector X_shape = {1, 1, 7}; - vector W = {0.24472862482070923f}; + vector W = {MLFloat16(0.244750977f)}; vector W_shape = {1, 1, 1}; vector Y_shape = {1, 1, 7}; - auto expected_vals = {-0.052761781960725784f, 0.11481902748346329f, 0.10833403468132019f, -0.11055534332990646f, - -0.012766072526574135f, 0.07113571465015411f, 0.061429332941770554f}; + auto expected_vals = {MLFloat16(-0.0527624786f), MLFloat16(0.114846528f), MLFloat16(0.108333379f), + MLFloat16(-0.110544264f), MLFloat16(-0.0127648748f), MLFloat16(0.0711666048f), + MLFloat16(0.0614267588f)}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); // CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -// Conv3 -TEST(ConvTest, Conv1D_2) { +TEST(ConvFp16Test, Conv1D_2) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{2}, // dilations @@ -146,33 +211,32 @@ TEST(ConvTest, Conv1D_2) { {} // excluded EPs }; - vector X = {0.11094123125076294f, -0.0038032233715057373f, 0.3896123170852661f, 0.33259105682373047f, - 0.02794349193572998f, -0.08360505104064941f, -0.4100455045700073f, -0.09502679109573364f, - -0.11361867189407349f, -0.025495320558547974f, 0.3696536421775818f, 0.3529144525527954f, - -0.34991076588630676f, -0.22024285793304443f, 0.23085933923721313f, -0.4575521945953369f, - -0.17685726284980774f, -0.06030535697937012f, -0.3996139168739319f, -0.19385704398155212f, - -0.10454908013343811f, -0.14503943920135498f, -0.31941986083984375f, -0.15372398495674133f}; + vector X = {MLFloat16(0.112f), MLFloat16(-0.0038f), MLFloat16(0.382f), MLFloat16(0.332f), + MLFloat16(0.0279f), MLFloat16(-0.0836f), MLFloat16(-0.41f), MLFloat16(-0.095f), + MLFloat16(-0.113f), MLFloat16(-0.0254f), MLFloat16(0.369f), MLFloat16(0.352f), + MLFloat16(-0.349f), MLFloat16(-0.22f), MLFloat16(0.231f), MLFloat16(-0.457f), + MLFloat16(-0.176f), MLFloat16(-0.0603f), MLFloat16(-0.399f), MLFloat16(-0.193f), + MLFloat16(-0.104f), MLFloat16(-0.145f), MLFloat16(-0.319f), MLFloat16(-0.153f)}; vector X_shape = {3, 1, 8}; - vector W = {0.13225573301315308f, 0.09750443696975708f, 0.3469849228858948f, 0.4743430018424988f}; + vector W = {MLFloat16(0.132f), MLFloat16(0.0975f), MLFloat16(0.346f), MLFloat16(0.474f)}; vector W_shape = {2, 1, 2}; vector Y_shape = {3, 2, 5}; - auto expected_vals = {0.010817262344062328f, 0.05266154557466507f, 0.054253075271844864f, -0.03628557175397873f, - -0.05423086881637573f, 0.05262419581413269f, 0.22330480813980103f, 0.14844439923763275f, - -0.1848062425851822f, -0.14227961003780365f, -0.011078324168920517f, 0.02101614698767662f, - 0.014770962297916412f, -0.023767895996570587f, 0.03053247183561325f, -0.053894221782684326f, - 0.13591864705085754f, -0.03771348297595978f, -0.011907249689102173f, 0.08010470867156982f, - -0.01724436692893505f, -0.06235451623797417f, -0.06304522603750229f, -0.044972069561481476f, - -0.042245108634233475f, -0.08389100432395935f, -0.2509208619594574f, -0.18825212121009827f, - -0.18779152631759644f, -0.11083387583494186f}; - - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); - - // CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + auto expected_vals = { + MLFloat16(0.0109176636f), MLFloat16(0.0520324707f), MLFloat16(0.0531311035f), MLFloat16(-0.0362854004f), + MLFloat16(-0.0540771484f), MLFloat16(0.0531005859f), MLFloat16(0.219848633f), MLFloat16(0.145385742f), + MLFloat16(-0.184692383f), MLFloat16(-0.141845703f), MLFloat16(-0.0110092163f), MLFloat16(0.0210418701f), + MLFloat16(0.0146484375f), MLFloat16(-0.0235595703f), MLFloat16(0.0304718018f), MLFloat16(-0.0535583496f), + MLFloat16(0.135864258f), MLFloat16(-0.0379028320f), MLFloat16(-0.0112762451f), MLFloat16(0.0798950195f), + MLFloat16(-0.0171508789f), MLFloat16(-0.0621032715f), MLFloat16(-0.0628051758f), MLFloat16(-0.0448303223f), + MLFloat16(-0.0421142578f), MLFloat16(-0.0834350586f), MLFloat16(-0.250000000f), MLFloat16(-0.187377930f), + MLFloat16(-0.187255859f), MLFloat16(-0.110412598f)}; + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } // Conv1 -TEST(ConvTest, Conv1D_Bias) { +TEST(ConvFp16Test, Conv1D_Bias) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{2}, // dilations @@ -183,41 +247,33 @@ TEST(ConvTest, Conv1D_Bias) { {} // excluded EPs }; - vector X = {0.4582272171974182f, 0.3877705931663513f, -0.05413919687271118f, -0.3013981878757477f, - 0.19299334287643433f, -0.4758569598197937f, 0.4670986533164978f, 0.4078403115272522f, - 0.24010121822357178f, 0.41645896434783936f, -0.038333237171173096f, 0.22969317436218262f, - 0.3565492033958435f, 0.12812334299087524f, 0.10096627473831177f, 0.25682520866394043f, - 0.41700226068496704f, 0.34114283323287964f, -0.429997980594635f, 0.3545404076576233f, - 0.40339237451553345f, 0.10174298286437988f, 0.45713120698928833f, 0.08574831485748291f, - 0.38086581230163574f, 0.16378509998321533f, 0.12321442365646362f, -0.19936135411262512f, - 0.26019394397735596f, -0.18406429886817932f, 0.3110783100128174f, 0.15553230047225952f, - -0.14629846811294556f, -0.1779327094554901f, -0.01390346884727478f, -0.09264758229255676f}; + vector X = {MLFloat16(0.458251953f), MLFloat16(0.387695312f), MLFloat16(-0.0541381836f), + MLFloat16(-0.301513672f), MLFloat16(0.192993164f), MLFloat16(-0.475830078f), + MLFloat16(0.467041016f), MLFloat16(0.407958984f), MLFloat16(0.240112305f), + MLFloat16(0.416503906f), MLFloat16(-0.0383300781f), MLFloat16(0.229736328f), + MLFloat16(0.356445312f), MLFloat16(0.128173828f), MLFloat16(0.100952148f), + MLFloat16(0.256835938f), MLFloat16(0.416992188f), MLFloat16(0.341064453f), + MLFloat16(-0.429931641f), MLFloat16(0.354492188f), MLFloat16(0.403320312f), + MLFloat16(0.101745605f), MLFloat16(0.457031250f), MLFloat16(0.0857543945f), + MLFloat16(0.380859375f), MLFloat16(0.163818359f), MLFloat16(0.123229980f), + MLFloat16(-0.199340820f), MLFloat16(0.260253906f), MLFloat16(-0.184082031f), + MLFloat16(0.311035156f), MLFloat16(0.155517578f), MLFloat16(-0.146240234f), + MLFloat16(-0.177978516f), MLFloat16(-0.0139007568f), MLFloat16(-0.0926513672f)}; vector X_shape = {2, 2, 9}; - vector W = {-0.17206084728240967f, 0.3236315846443176f}; + vector W = {MLFloat16(-0.172119141f), MLFloat16(0.323730469f)}; vector W_shape = {1, 2, 1}; - vector B = {0.37892162799835205f}; + vector B = {MLFloat16(0.378906250f)}; vector B_shape = {1}; vector Y_shape = {2, 1, 4}; - auto expected_vals = {0.37892162799835205f, 0.4625728130340576f, 0.4934738576412201f, 0.44801419973373413f, - 0.37892162799835205f, 0.2499445676803589f, 0.31682088971138f, 0.32773756980895996f}; - - // For the CUDA EP: Due to CUDNN Frontend using TF32 for FP32 operations we get a higher error than using FP32 only, - // as TF32 has a 10 bit mantissa. - float epsilon = 1.1e-5f; - - // This case is not supported by cuDNN frontend, and the fallback (legacy code) requires weight to 4D tensor for NHWC. - constexpr bool exclude_cuda_nhwc = true; + auto expected_vals = {MLFloat16(0.378906250f), MLFloat16(0.462597132f), MLFloat16(0.493487000f), + MLFloat16(0.447991282f), MLFloat16(0.378906250f), MLFloat16(0.249894142f), + MLFloat16(0.316803873f), MLFloat16(0.327701926f)}; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, false, epsilon, - OpTester::ExpectResult::kExpectSuccess, "", 10, exclude_cuda_nhwc); - - // CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true, epsilon, - OpTester::ExpectResult::kExpectSuccess, "", 10, exclude_cuda_nhwc); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -// Conv47 -TEST(ConvTest, Conv2D_1) { +TEST(ConvFp16Test, Conv2D_1) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -228,71 +284,28 @@ TEST(ConvTest, Conv2D_1) { {} // excluded EPs }; - vector X = {-0.09103918075561523f, -0.32513630390167236f}; + vector X = {MLFloat16(-0.0910644531f), MLFloat16(-0.325195312f)}; vector X_shape = {2, 1, 1, 1}; - vector W = {0.4312484860420227f, -0.12559029459953308f, 0.44889551401138306f, -0.3100617825984955f, - 0.13522827625274658f, -0.06791308522224426f, 0.22671669721603394f, -0.17391827702522278f, - -0.31299442052841187f, -0.31545522809028625f, 0.06560015678405762f, 0.2656586766242981f, - 0.41363757848739624f, 0.31231558322906494f, -0.376018226146698f, -0.005708813667297363f, - 0.34922850131988525f, 0.45095211267471313f}; + vector W = {MLFloat16(0.431152344f), MLFloat16(-0.125610352f), MLFloat16(0.448974609f), + MLFloat16(-0.310058594f), MLFloat16(0.135253906f), MLFloat16(-0.0679321289f), + MLFloat16(0.226684570f), MLFloat16(-0.173950195f), MLFloat16(-0.312988281f), + MLFloat16(-0.315429688f), MLFloat16(0.065612793f), MLFloat16(0.265625f), + MLFloat16(0.413574219f), MLFloat16(0.312255859f), MLFloat16(-0.375976562f), + MLFloat16(-0.00571060181f), MLFloat16(0.349121094f), MLFloat16(0.450927734f)}; vector W_shape = {2, 1, 3, 3}; vector Y_shape = {2, 2, 1, 2}; - auto expected_vals = {-0.012311071157455444f, 0.02822777070105076f, -0.028432954102754593f, -0.037657227367162704f, - -0.04396762326359749f, 0.10081233829259872f, -0.10154513269662857f, -0.13448859751224518f}; + auto expected_vals = {MLFloat16(-0.012316823f), MLFloat16(0.0282353163f), + MLFloat16(-0.0284354091f), MLFloat16(-0.0376619101f), + MLFloat16(-0.0439839363f), MLFloat16(0.100829601f), + MLFloat16(-0.101544142f), MLFloat16(-0.134492397f)}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); -} - -TEST(ConvTest, Conv1D_Invalid_Input_Shape) { - ConvOpAndTestAttributes attrs = { - "", // auto_pad - vector{1}, // dilations - 1, // group - vector{2}, // kernel_shape - vector{0, 0}, // pads - vector{1}, // strides - {} // excluded EPs - }; - - vector X = vector(1, 1.0f); - vector X_shape = {1, 1, 1}; - vector dummy_shape = {1, 1, 2}; - auto dummy_vals = {0.0f, 0.0f}; - TestConvOp(attrs, {X, dummy_vals}, {X_shape, dummy_shape}, dummy_vals, dummy_shape, false, optional(), - OpTester::ExpectResult::kExpectFailure, - "Node:node1 Output:Y [ShapeInferenceError] Can't merge shape info. " - "Both inferred and declared dimension have values but they differ. Inferred=0 Declared=2 Dimension=2", - -1); // use latest opset for shape inferencing errors -} - -TEST(ConvTest, Conv2D_Invalid_Input_Shape) { - ConvOpAndTestAttributes attrs = { - "", // auto_pad - vector{1, 1}, // dilations - 1, // group - vector{3, 3}, // kernel_shape - vector{0, 0, 0, 0}, // pads - vector{1, 1}, // strides - {} // excluded EPs - }; - - vector X = vector(1 * 3 * 1 * 111, 1.0f); - vector X_shape = {1, 3, 1, 111}; - vector dummy_shape = {2, 2, 1, 2}; - auto dummy_vals = {-0.0f, 0.0f, -0.0f, -0.0f, - -0.0f, 0.0f, -0.0f, -0.0f}; - TestConvOp(attrs, {X, dummy_vals}, {X_shape, dummy_shape}, dummy_vals, dummy_shape, false, optional(), - OpTester::ExpectResult::kExpectFailure, - "Node:node1 Output:Y [ShapeInferenceError] Can't merge shape info. " - "Both inferred and declared dimension have values but they differ. Inferred=1 Declared=2 Dimension=0", - -1); // use latest opset for shape inferencing errors + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -// Conv30 -TEST(ConvTest, Conv2D_2) { +TEST(ConvFp16Test, Conv2D_2) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -303,43 +316,54 @@ TEST(ConvTest, Conv2D_2) { {} // excluded EPs }; - vector X = {0.45246148109436035f, 0.15498268604278564f, 0.11199361085891724f, -0.39421093463897705f, - 0.2626858949661255f, 0.13414543867111206f, -0.27184486389160156f, -0.43028733134269714f, - -0.26825493574142456f, 0.3893144130706787f, -0.13631996512413025f, -0.009590476751327515f, - -0.48771554231643677f, -0.25256502628326416f, -0.2812897562980652f, 0.4043201804161072f, - 0.07795023918151855f, 0.326981782913208f, 0.13114392757415771f, -0.4416425824165344f, - 0.12446999549865723f, 0.36739975214004517f, 0.1698915958404541f, 0.2008744478225708f, - 0.23339951038360596f, 0.38613730669021606f, 0.11117297410964966f, 0.3877097964286804f, - 0.20812749862670898f, -0.34297940135002136f, -0.029246658086776733f, -0.20483523607254028f, - -0.19244328141212463f, -0.11104947328567505f, -0.32830488681793213f, -0.01800677180290222f, - 0.3618946671485901f, -0.40949052572250366f, -0.18248388171195984f, -0.3349453806877136f, - -0.34091079235076904f, 0.006497859954833984f, 0.4537564516067505f, 0.08006560802459717f, - -0.14788749814033508f, 0.034442365169525146f, -0.33322954177856445f, 0.06049239635467529f, - 0.42619407176971436f}; + vector X = { + MLFloat16(0.452392578f), MLFloat16(0.155029297f), MLFloat16(0.111999512f), + MLFloat16(-0.394287109f), MLFloat16(0.262695312f), MLFloat16(0.134155273f), + MLFloat16(-0.271728516f), MLFloat16(-0.430175781f), MLFloat16(-0.268310547f), + MLFloat16(0.389404297f), MLFloat16(-0.136352539f), MLFloat16(-0.00959014893f), + MLFloat16(-0.487792969f), MLFloat16(-0.252685547f), MLFloat16(-0.281250000f), + MLFloat16(0.404296875f), MLFloat16(0.0779418945f), MLFloat16(0.326904297f), + MLFloat16(0.131103516f), MLFloat16(-0.441650391f), MLFloat16(0.124450684f), + MLFloat16(0.367431641f), MLFloat16(0.169921875f), MLFloat16(0.200927734f), + MLFloat16(0.233398438f), MLFloat16(0.386230469f), MLFloat16(0.111145020f), + MLFloat16(0.387695312f), MLFloat16(0.208129883f), MLFloat16(-0.343017578f), + MLFloat16(-0.0292510986f), MLFloat16(-0.204833984f), MLFloat16(-0.192382812f), + MLFloat16(-0.111022949f), MLFloat16(-0.328369141f), MLFloat16(-0.0180053711f), + MLFloat16(0.361816406f), MLFloat16(-0.409423828f), MLFloat16(-0.182495117f), + MLFloat16(-0.334960938f), MLFloat16(-0.340820312f), MLFloat16(0.00649642944f), + MLFloat16(0.453857422f), MLFloat16(0.0800781250f), MLFloat16(-0.147827148f), + MLFloat16(0.0344543457f), MLFloat16(-0.333251953f), MLFloat16(0.0604858398f), + MLFloat16(0.426269531f)}; vector X_shape = {1, 1, 7, 7}; - vector W = {-0.4406261742115021f}; + vector W = {MLFloat16(-0.440673828f)}; vector W_shape = {1, 1, 1, 1}; vector Y_shape = {1, 1, 7, 7}; - auto expected_vals = {-0.19936637580394745f, -0.06828942894935608f, -0.04934731498360634f, 0.17369966208934784f, - -0.11574628204107285f, -0.05910799279808998f, 0.1197819635272026f, 0.18959586322307587f, - 0.1182001456618309f, -0.17154212296009064f, 0.06006614491343498f, 0.0042258151806890965f, - 0.21490024030208588f, 0.11128675937652588f, 0.12394362688064575f, -0.17815405130386353f, - -0.034346915781497955f, -0.14407673478126526f, -0.05778544768691063f, 0.19459928572177887f, - -0.05484473705291748f, -0.16188594698905945f, -0.07485868036746979f, -0.08851054310798645f, - -0.10284193605184555f, -0.17014220356941223f, -0.04898572340607643f, -0.17083507776260376f, - -0.09170642495155334f, 0.1511256992816925f, 0.012886842712759972f, 0.09025576710700989f, - 0.08479554951190948f, 0.0489313043653965f, 0.14465972781181335f, 0.007934254594147205f, - -0.15946026146411896f, 0.1804322451353073f, 0.08040717244148254f, 0.1475857049226761f, - 0.15021422505378723f, -0.0028631272725760937f, -0.19993697106838226f, -0.03527900204062462f, - 0.06516310572624207f, -0.015176207758486271f, 0.14682966470718384f, -0.02665453404188156f, - -0.18779225647449493f}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + auto expected_vals = { + MLFloat16(-0.199340820f), MLFloat16(-0.0682983398f), MLFloat16(-0.0493469238f), + MLFloat16(0.173706055f), MLFloat16(-0.115783691f), MLFloat16(-0.0591125488f), + MLFloat16(0.119750977f), MLFloat16(0.189575195f), MLFloat16(0.118225098f), + MLFloat16(-0.171630859f), MLFloat16(0.0600891113f), MLFloat16(0.00422668457f), + MLFloat16(0.214965820f), MLFloat16(0.111328125f), MLFloat16(0.123962402f), + MLFloat16(-0.178222656f), MLFloat16(-0.0343322754f), MLFloat16(-0.144042969f), + MLFloat16(-0.0577697754f), MLFloat16(0.194580078f), MLFloat16(-0.0548400879f), + MLFloat16(-0.161865234f), MLFloat16(-0.0748901367f), MLFloat16(-0.0885620117f), + MLFloat16(-0.102844238f), MLFloat16(-0.170166016f), MLFloat16(-0.0489807129f), + MLFloat16(-0.170898438f), MLFloat16(-0.0917358398f), MLFloat16(0.151123047f), + MLFloat16(0.0128936768f), MLFloat16(0.0902709961f), MLFloat16(0.0847778320f), + MLFloat16(0.0489196777f), MLFloat16(0.144653320f), MLFloat16(0.00793457031f), + MLFloat16(-0.159423828f), MLFloat16(0.180419922f), MLFloat16(0.0804443359f), + MLFloat16(0.147583008f), MLFloat16(0.150146484f), MLFloat16(-0.00286293030f), + MLFloat16(-0.199951172f), MLFloat16(-0.0352783203f), MLFloat16(0.0651245117f), + MLFloat16(-0.0151824951f), MLFloat16(0.146850586f), MLFloat16(-0.0266571045f), + MLFloat16(-0.187866211f)}; + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv2D_Bias_1) { +TEST(ConvFp16Test, Conv2D_Bias_1) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -350,23 +374,21 @@ TEST(ConvTest, Conv2D_Bias_1) { {} // excluded EPs }; - vector X = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f}; + vector X = {MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f)}; vector X_shape = {1, 1, 3, 3}; - vector W = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}; + vector W = {MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f)}; vector W_shape = {2, 1, 2, 2}; vector Y_shape = {1, 2, 2, 2}; - vector B = {1.0f, -1.0f}; + vector B = {MLFloat16(1.0f), MLFloat16(-1.0f)}; vector B_shape = {2}; - auto expected_vals = {13.0f, 17.0f, 25.0f, 29.0f, 11.0f, 15.0f, 23.0f, 27.0f}; - - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + auto expected_vals = {MLFloat16(13.0f), MLFloat16(17.0f), MLFloat16(25.0f), MLFloat16(29.0f), MLFloat16(11.0f), MLFloat16(15.0f), MLFloat16(23.0f), MLFloat16(27.0f)}; - // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } // Conv48 -TEST(ConvTest, Conv2D_Bias_2) { +TEST(ConvFp16Test, Conv2D_Bias_2) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -377,46 +399,55 @@ TEST(ConvTest, Conv2D_Bias_2) { {} // excluded EPs }; - vector X = {-0.22904816269874573f, -0.20278319716453552f, -0.4723144471645355f, 0.027880489826202393f, - 0.2685856819152832f, -0.19361668825149536f, -0.39857280254364014f, 0.40285515785217285f, - 0.20966708660125732f, -0.39234158396720886f, -0.07502302527427673f, 0.4662899374961853f, - -0.2567148208618164f, -0.1186269223690033f, -0.1897754967212677f, -0.3967694342136383f, - -0.4268943667411804f, -0.344584584236145f, -0.4483465552330017f, -0.41608482599258423f, - -0.23649904131889343f, -0.4195239543914795f, 0.3277903199195862f, -0.11628741025924683f, - 0.2873995900154114f, 0.21717703342437744f, -0.26514798402786255f, 0.08272713422775269f, - 0.0050997138023376465f, -0.41409194469451904f, 0.2826550006866455f, 0.4891064763069153f, - -0.1522480845451355f, -0.2554396986961365f, 0.04099029302597046f, -0.35793858766555786f, - 0.2557554841041565f, 0.41162675619125366f, -0.06953108310699463f, 0.029517710208892822f, - 0.32956594228744507f, 0.4615175127983093f, -0.3216847777366638f, 0.15545696020126343f, - -0.3779126703739166f, -0.01712372899055481f, 0.07461833953857422f, 0.38875824213027954f, - 0.1980893611907959f, -0.19913813471794128f, -0.011296629905700684f, 0.30053526163101196f, - 0.4461088180541992f, 0.025034189224243164f, -0.3370230793952942f, -0.21012544631958008f, - -0.41627752780914307f, -0.43801137804985046f, 0.13566172122955322f, -0.47898364067077637f, - -0.45526939630508423f, -0.3007912039756775f, 0.06994932889938354f, -0.0749855637550354f, - -0.22754916548728943f, -0.469131737947464f, 0.08644282817840576f, 0.06157493591308594f, - -0.3920745849609375f, 0.458797812461853f, 0.18890488147735596f, 0.40145808458328247f}; + vector X = { + MLFloat16(-0.625f), MLFloat16(0.4375f), MLFloat16(0.0625f), + MLFloat16(-0.3125f), MLFloat16(-0.6875f), MLFloat16(0.375f), + MLFloat16(0.0625f), MLFloat16(-0.375f), MLFloat16(0.6875f), + MLFloat16(0.3125f), MLFloat16(-0.0625f), MLFloat16(-0.4375f), + MLFloat16(0.625f), MLFloat16(0.25f), MLFloat16(-0.125f), + MLFloat16(-0.5f), MLFloat16(0.5625f), MLFloat16(0.1875f), + MLFloat16(-0.1875f), MLFloat16(-0.5625f), MLFloat16(0.5f), + MLFloat16(0.125f), MLFloat16(-0.25f), MLFloat16(-0.625f), + MLFloat16(0.4375f), MLFloat16(0.0625f), MLFloat16(-0.3125f), + MLFloat16(-0.6875f), MLFloat16(0.375f), MLFloat16(0.25f), + MLFloat16(-0.375f), MLFloat16(0.6875f), MLFloat16(0.3125f), + MLFloat16(-0.0625f), MLFloat16(-0.4375f), MLFloat16(0.625f), + MLFloat16(0.25f), MLFloat16(-0.125f), MLFloat16(-0.5f), + MLFloat16(0.5625f), MLFloat16(0.1875f), MLFloat16(-0.1875f), + MLFloat16(-0.5625f), MLFloat16(0.5f), MLFloat16(0.125f), + MLFloat16(-0.25f), MLFloat16(-0.625f), MLFloat16(0.4375f), + MLFloat16(0.0625f), MLFloat16(-0.3125f), MLFloat16(-0.6875f), + MLFloat16(0.375f), MLFloat16(0.125f), MLFloat16(-0.375f), + MLFloat16(0.6875f), MLFloat16(0.3125f), MLFloat16(-0.0625f), + MLFloat16(-0.4375f), MLFloat16(0.625f), MLFloat16(0.25f), + MLFloat16(-0.125f), MLFloat16(-0.5f), MLFloat16(0.5625f), + MLFloat16(0.1875f), MLFloat16(-0.1875f), MLFloat16(-0.5625f), + MLFloat16(0.5f), MLFloat16(0.125f), MLFloat16(-0.25f), + MLFloat16(-0.625f), MLFloat16(0.4375f), MLFloat16(0.0625f)}; vector X_shape = {1, 2, 6, 6}; - vector W = {-0.48007914423942566f, -0.21048793196678162f, 0.2505034804344177f, 0.1610567569732666f, - -0.24951639771461487f, 0.1918455958366394f, 0.44247758388519287f, 0.06943017244338989f, - -0.10510382056236267f, -0.41663575172424316f, -0.3053555488586426f, -0.19126328825950623f, - -0.42332321405410767f, 0.498790979385376f, 0.081226646900177f, -0.21777048707008362f, - 0.46603143215179443f, -0.43488776683807373f, -0.3080252408981323f, -0.3844330906867981f, - -0.17214277386665344f, -0.3650006353855133f, 0.21724021434783936f, 0.1636529564857483f, - -0.22924479842185974f, 0.044009625911712646f, 0.274614155292511f, -0.06811442971229553f, - 0.450619637966156f, 0.4611729383468628f, 0.20782196521759033f, -0.3136714696884155f}; + vector W = { + MLFloat16(-0.3125f), MLFloat16(-0.6875f), MLFloat16(0.375f), MLFloat16(0.025f), + MLFloat16(-0.375f), MLFloat16(0.6875f), MLFloat16(0.3125f), MLFloat16(-0.0625f), + MLFloat16(-0.4375f), MLFloat16(0.625f), MLFloat16(0.25f), MLFloat16(-0.125f), + MLFloat16(-0.5f), MLFloat16(0.5625f), MLFloat16(0.1875f), MLFloat16(-0.1875f), + MLFloat16(-0.5625f), MLFloat16(0.5f), MLFloat16(0.125f), MLFloat16(-0.25f), + MLFloat16(-0.625f), MLFloat16(0.4375f), MLFloat16(0.0625f), MLFloat16(-0.3125f), + MLFloat16(-0.6875f), MLFloat16(0.375f), MLFloat16(-0.125f), MLFloat16(-0.375f), + MLFloat16(0.6875f), MLFloat16(0.3125f), MLFloat16(-0.0625f), MLFloat16(-0.4375f)}; vector W_shape = {1, 2, 4, 4}; - vector B = {-0.40378910303115845f}; + vector B = {MLFloat16(-0.8125f)}; vector B_shape = {1}; vector Y_shape = {1, 1, 4, 2}; - auto expected_vals = {-0.3419531583786011f, -0.6116723418235779f, -0.39677709341049194f, -0.7316848039627075f, - -0.5647197365760803f, 0.02788025140762329f, -0.30450713634490967f, -0.6786775588989258f}; + auto expected_vals = { + MLFloat16(-0.83203125f), MLFloat16(-1.40625f), MLFloat16(-0.595312476f), MLFloat16(-1.93906248f), + MLFloat16(-0.896875024f), MLFloat16(-1.53750002f), MLFloat16(-0.904687524f), MLFloat16(-1.65937495f)}; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv2D_AutoPad1) { +TEST(ConvFp16Test, Conv2D_AutoPad1) { ConvOpAndTestAttributes attrs = { "SAME_UPPER", // auto_pad vector{1, 1}, // dilations @@ -427,26 +458,26 @@ TEST(ConvTest, Conv2D_AutoPad1) { {} // excluded EPs }; - vector X = vector(25, 1.0f); + vector X = vector(25, MLFloat16(1.0f)); vector X_shape = {1, 1, 5, 5}; - vector W = {0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f}; + vector W = {MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), + MLFloat16(3.0f), MLFloat16(4.0f), MLFloat16(5.0f), + MLFloat16(6.0f), MLFloat16(7.0f), MLFloat16(8.0f)}; vector W_shape = {1, 1, 3, 3}; vector Y_shape = {1, 1, 5, 5}; - auto expected_vals = {24.0f, 33.0f, 33.0f, 33.0f, 20.0f, - 27.0f, 36.0f, 36.0f, 36.0f, 21.0f, - 27.0f, 36.0f, 36.0f, 36.0f, 21.0f, - 27.0f, 36.0f, 36.0f, 36.0f, 21.0f, - 12.0f, 15.0f, 15.0f, 15.0f, 8.0f}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + auto expected_vals = {MLFloat16(24.0f), MLFloat16(33.0f), MLFloat16(33.0f), MLFloat16(33.0f), MLFloat16(20.0f), + MLFloat16(27.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(21.0f), + MLFloat16(27.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(21.0f), + MLFloat16(27.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(36.0f), MLFloat16(21.0f), + MLFloat16(12.0f), MLFloat16(15.0f), MLFloat16(15.0f), MLFloat16(15.0f), MLFloat16(8.0f)}; + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv2D_AutoPad2) { +TEST(ConvFp16Test, Conv2D_AutoPad2) { ConvOpAndTestAttributes attrs = { "SAME_LOWER", // auto_pad vector{1, 1}, // dilations @@ -457,31 +488,29 @@ TEST(ConvTest, Conv2D_AutoPad2) { {} // excluded EPs }; - vector X = {1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 1.0f, 0.0f, 1.0f}; + vector X = {MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(0.0f), MLFloat16(1.0f)}; vector X_shape = {1, 1, 5, 5}; - vector W = {0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f}; + vector W = {MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), + MLFloat16(3.0f), MLFloat16(4.0f), MLFloat16(5.0f), + MLFloat16(6.0f), MLFloat16(7.0f), MLFloat16(8.0f)}; vector W_shape = {1, 1, 3, 3}; vector Y_shape = {1, 1, 5, 5}; - auto expected_vals = {11.0f, 22.0f, 11.0f, 22.0f, 11.0f, - 12.0f, 24.0f, 12.0f, 24.0f, 12.0f, - 12.0f, 24.0f, 12.0f, 24.0f, 12.0f, - 12.0f, 24.0f, 12.0f, 24.0f, 12.0f, - 5.0f, 10.0f, 5.0f, 10.0f, 5.0f}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); - - // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + auto expected_vals = {MLFloat16(11.0f), MLFloat16(22.0f), MLFloat16(11.0f), MLFloat16(22.0f), MLFloat16(11.0f), + MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), + MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), + MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), MLFloat16(24.0f), MLFloat16(12.0f), + MLFloat16(5.0f), MLFloat16(10.0f), MLFloat16(5.0f), MLFloat16(10.0f), MLFloat16(5.0f)}; + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -// Conv10 -TEST(ConvTest, Conv3D_1) { +TEST(ConvFp16Test, Conv3D_1) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1, 1}, // dilations @@ -492,33 +521,35 @@ TEST(ConvTest, Conv3D_1) { {} // excluded EPs }; - vector X = {-0.43337246775627136f, -0.48385289311408997f, -0.30954962968826294f, - 0.16074687242507935f, -0.46670910716056824f, 0.46576786041259766f, - -0.37056273221969604f, 0.40604978799819946f, -0.035478413105010986f, - -0.3125576674938202f, 0.42677170038223267f, 0.39851123094558716f, - -0.3906140625476837f, 0.2590462565422058f, -0.20646807551383972f, - 0.1382436752319336f, -0.20149192214012146f, 0.10030072927474976f, - -0.2413364052772522f, 0.1231224536895752f, 0.032734215259552f, - 0.29610633850097656f, -0.23117440938949585f, 0.3345826268196106f, - 0.02567422389984131f, 0.24579226970672607f, 0.11724984645843506f}; + vector X = { + MLFloat16(-0.433349609f), MLFloat16(-0.483886719f), MLFloat16(-0.309570312f), + MLFloat16(0.160766602f), MLFloat16(-0.466796875f), MLFloat16(0.465820312f), + MLFloat16(-0.370605469f), MLFloat16(0.406005859f), MLFloat16(-0.0354919434f), + MLFloat16(-0.312500000f), MLFloat16(0.426757812f), MLFloat16(0.398437500f), + MLFloat16(-0.390625000f), MLFloat16(0.259033203f), MLFloat16(-0.206420898f), + MLFloat16(0.138183594f), MLFloat16(-0.201538086f), MLFloat16(0.100280762f), + MLFloat16(-0.241333008f), MLFloat16(0.123107910f), MLFloat16(0.0327453613f), + MLFloat16(0.296142578f), MLFloat16(-0.231201172f), MLFloat16(0.334472656f), + MLFloat16(0.0256805420f), MLFloat16(0.245849609f), MLFloat16(0.117248535f)}; vector X_shape = {1, 1, 3, 3, 3}; - vector W = {-0.44214117527008057f}; + vector W = {MLFloat16(-0.442138672f)}; vector W_shape = {1, 1, 1, 1, 1}; vector Y_shape = {1, 1, 3, 3, 3}; - auto expected_vals = {0.19161181151866913f, 0.21393129229545593f, 0.13686463236808777f, - -0.07107280939817429f, 0.20635131001472473f, -0.20593515038490295f, - 0.16384103894233704f, -0.17953133583068848f, 0.01568646728992462f, - 0.13819462060928345f, -0.1886933445930481f, -0.17619822919368744f, - 0.17270655930042267f, -0.11453501880168915f, 0.09128803759813309f, - -0.06112322211265564f, 0.08908787369728088f, -0.04434708133339882f, - 0.10670476406812668f, -0.054437506943941116f, -0.014473143965005875f, - -0.13092079758644104f, 0.10221172869205475f, -0.1479327529668808f, - -0.011351631954312325f, -0.10867488384246826f, -0.05184098333120346f}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + auto expected_vals = { + MLFloat16(0.191600621f), MLFloat16(0.213945031f), MLFloat16(0.136873007f), + MLFloat16(-0.0710811317f), MLFloat16(0.206388950f), MLFloat16(-0.205957174f), + MLFloat16(0.163859010f), MLFloat16(-0.179510891f), MLFloat16(0.0156923607f), + MLFloat16(0.138168335f), MLFloat16(-0.188686132f), MLFloat16(-0.176164627f), + MLFloat16(0.172710419f), MLFloat16(-0.114528596f), MLFloat16(0.0912666619f), + MLFloat16(-0.0610963106f), MLFloat16(0.0891077816f), MLFloat16(-0.0443380028f), + MLFloat16(0.106702656f), MLFloat16(-0.0544307679f), MLFloat16(-0.0144779906f), + MLFloat16(-0.130936086f), MLFloat16(0.102222979f), MLFloat16(-0.147883296f), + MLFloat16(-0.0113543607f), MLFloat16(-0.108699620f), MLFloat16(-0.0518401116f)}; + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -// Conv22 -TEST(ConvTest, Conv3D_2) { +TEST(ConvFp16Test, Conv3D_2) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1, 1}, // dilations @@ -529,39 +560,40 @@ TEST(ConvTest, Conv3D_2) { {} // excluded EPs }; - vector X = {0.010772407054901123f, -0.43806642293930054f, 0.455391526222229f, -0.28657248616218567f, - 0.45676887035369873f, -0.0320507287979126f, 0.4229400157928467f, -0.18730869889259338f, - -0.45851585268974304f, 0.042054951190948486f, -0.13332295417785645f, -0.25374430418014526f, - -0.23845627903938293f, 0.12214112281799316f, -0.1778157651424408f, 0.1891845464706421f, - 0.37962496280670166f, -0.033982306718826294f, 0.12737131118774414f, -0.040284961462020874f, - 0.46427029371261597f, -0.22687292098999023f, 0.17398333549499512f, -0.3014046251773834f, - -0.4043419063091278f, -0.33206477761268616f, 0.04655301570892334f, -0.4947906732559204f, - 0.0755157470703125f, 0.1173025369644165f, 0.47043120861053467f, 0.4824737310409546f, - -0.37734976410865784f, -0.056491583585739136f, -0.10790631175041199f, 0.043476223945617676f, - 0.24469023942947388f, -0.4100031852722168f, 0.0616222620010376f, 0.2296960949897766f, - 0.27883386611938477f, 0.08150351047515869f, 0.2453773021697998f, 0.08250969648361206f, - -0.1471814215183258f, -0.43011274933815f, 0.027180075645446777f, 0.3605625033378601f, - 0.24954384565353394f, -0.22505927085876465f, -0.36272895336151123f, -0.47674262523651123f, - 0.11275297403335571f, 0.49773406982421875f, 0.2686365246772766f, 0.025525271892547607f, - -0.3037869930267334f, 0.41126757860183716f, 0.36149072647094727f, 0.00883406400680542f, - -0.07959523797035217f, 0.3601323366165161f, 0.17322391271591187f, -0.012007325887680054f}; + vector X = { + MLFloat16(0.0107727051f), MLFloat16(-0.437988281f), MLFloat16(0.455322266f), MLFloat16(-0.286621094f), + MLFloat16(0.456787109f), MLFloat16(-0.0320434570f), MLFloat16(0.422851562f), MLFloat16(-0.187255859f), + MLFloat16(-0.458496094f), MLFloat16(0.0420532227f), MLFloat16(-0.133300781f), MLFloat16(-0.253662109f), + MLFloat16(-0.238403320f), MLFloat16(0.122131348f), MLFloat16(-0.177856445f), MLFloat16(0.189208984f), + MLFloat16(0.379638672f), MLFloat16(-0.0339965820f), MLFloat16(0.127319336f), MLFloat16(-0.0402832031f), + MLFloat16(0.464355469f), MLFloat16(-0.226928711f), MLFloat16(0.173950195f), MLFloat16(-0.301513672f), + MLFloat16(-0.404296875f), MLFloat16(-0.332031250f), MLFloat16(0.0465393066f), MLFloat16(-0.494873047f), + MLFloat16(0.0755004883f), MLFloat16(0.117309570f), MLFloat16(0.470458984f), MLFloat16(0.482421875f), + MLFloat16(-0.377441406f), MLFloat16(-0.0564880371f), MLFloat16(-0.107910156f), MLFloat16(0.0434875488f), + MLFloat16(0.244750977f), MLFloat16(-0.409912109f), MLFloat16(0.0616149902f), MLFloat16(0.229736328f), + MLFloat16(0.278808594f), MLFloat16(0.0814819336f), MLFloat16(0.245361328f), MLFloat16(0.0825195312f), + MLFloat16(-0.147216797f), MLFloat16(-0.430175781f), MLFloat16(0.0271759033f), MLFloat16(0.360595703f), + MLFloat16(0.249511719f), MLFloat16(-0.225097656f), MLFloat16(-0.362792969f), MLFloat16(-0.476806641f), + MLFloat16(0.112731934f), MLFloat16(0.497802734f), MLFloat16(0.268554688f), MLFloat16(0.0255279541f), + MLFloat16(-0.303710938f), MLFloat16(0.411376953f), MLFloat16(0.361572266f), MLFloat16(0.00883483887f), + MLFloat16(-0.0795898438f), MLFloat16(0.360107422f), MLFloat16(0.173217773f), MLFloat16(-0.0120086670f)}; vector X_shape = {1, 1, 4, 4, 4}; - vector W = {0.32824617624282837f}; + vector W = {MLFloat16(0.328125f)}; vector W_shape = {1, 1, 1, 1, 1}; vector Y_shape = {1, 1, 4, 4, 4}; - auto expected_vals = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0035360013134777546f, 0.14948052167892456f, 0.0f, - 0.0f, -0.15050607919692993f, -0.043762750923633575f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -0.12386361509561539f, -0.03541983291506767f, 0.0f, - 0.0f, 0.09152615070343018f, 0.08054415881633759f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + auto expected_vals = {MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), + MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), + MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(0.00353479385f), MLFloat16(0.149402618f), MLFloat16(), + MLFloat16(), MLFloat16(-0.150444031f), MLFloat16(-0.0437393188f), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), + MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(-0.123847961f), MLFloat16(-0.03540802f), MLFloat16(), + MLFloat16(), MLFloat16(0.0914840698f), MLFloat16(0.0805091858f), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), + MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), + MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16(), MLFloat16()}; + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -// Conv23 -TEST(ConvTest, Conv3D_Bias) { +TEST(ConvFp16Test, Conv3D_Bias) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{2, 2, 2}, // dilations @@ -572,84 +604,96 @@ TEST(ConvTest, Conv3D_Bias) { {} // excluded EPs }; - vector X = {0.46796226501464844f, -0.4613912105560303f, 0.33512794971466064f, -0.4010460674762726f, - 0.41722816228866577f, -0.048133403062820435f, 0.20415884256362915f, 0.03189706802368164f, - -0.04779183864593506f, -0.0795503556728363f, 0.4987630844116211f, 0.3506373167037964f, - 0.48065757751464844f, 0.269855260848999f, -0.2463444471359253f, 0.19044137001037598f, - -0.11830493807792664f, -0.2576887905597687f, -0.33940935134887695f, -0.257951021194458f, - -0.08279827237129211f, 0.3513314127922058f, -0.29122066497802734f, -0.43358397483825684f, - -0.13429927825927734f, 0.44032156467437744f, 0.05308258533477783f, -0.3499870300292969f, - -0.28474611043930054f, -0.44209951162338257f, -0.07418054342269897f, -0.10919415950775146f, - 0.2845439314842224f, 0.3498746156692505f, -0.19313520193099976f, 0.32609254121780396f, - 0.4880145788192749f, 0.05574071407318115f, -0.46457427740097046f, -0.02524462342262268f, - -0.18780940771102905f, -0.14720159769058228f, 0.207585871219635f, 0.47157740592956543f, - -0.05567386746406555f, -0.49871665239334106f, 0.2274145483970642f, 0.4589425325393677f, - -0.4725189805030823f, -0.4358765780925751f, 0.2841453552246094f, -0.27037882804870605f, - 0.34227508306503296f, 0.33575427532196045f, -0.19485199451446533f, -0.27679920196533203f, - -0.4238079786300659f, -0.4385119676589966f, 0.43724071979522705f, 0.3065117597579956f, - 0.45696544647216797f, 0.05291992425918579f, -0.023618370294570923f, -0.1860884726047516f, - 0.08669537305831909f, 0.32541000843048096f, 0.1846179962158203f, -0.1984834372997284f, - -0.2754465937614441f, 0.32004624605178833f, -0.34846532344818115f, 0.0999596118927002f, - -0.11374691128730774f, 0.21225297451019287f, -0.02315312623977661f, 0.1671370267868042f, - 0.22319108247756958f, 0.03609824180603027f, -0.1587022840976715f, 0.059984564781188965f, - -0.03951650857925415f, -0.4841443598270416f, 0.32919085025787354f, -0.23115816712379456f, - 0.39441078901290894f, -0.3554944396018982f, -0.17022761702537537f, -0.055081307888031006f, - 0.15856128931045532f, -0.4183449149131775f, -0.2474445104598999f, 0.03603637218475342f, - -0.2836887538433075f, 0.4602506160736084f, 0.29092925786972046f, -0.199321448802948f, - 0.380856454372406f, -0.13847029209136963f, -0.238397479057312f, -0.1907123327255249f, - -0.11061936616897583f, -0.08717870712280273f, 0.24449139833450317f, -0.14727482199668884f, - 0.1437196135520935f, 0.3955056071281433f, -0.12538021802902222f, 0.11590522527694702f, - 0.4598066806793213f, -0.30005723237991333f, -0.46578651666641235f, -0.33955082297325134f, - -0.2671887278556824f, 0.3611910939216614f, -0.11423084139823914f, -0.08382436633110046f, - -0.31819307804107666f, 0.14515334367752075f, 0.3157258629798889f, 0.33179205656051636f, - -0.2558857202529907f, 0.11888682842254639f, 0.12824326753616333f, -0.33106181025505066f, - 0.2549159526824951f, -0.46760573983192444f, -0.11983257532119751f, 0.1834418773651123f}; + vector X = { + MLFloat16(0.468017578f), MLFloat16(-0.461425781f), MLFloat16(0.335205078f), MLFloat16(-0.401123047f), + MLFloat16(0.417236328f), MLFloat16(-0.0481262207f), MLFloat16(0.204101562f), MLFloat16(0.0318908691f), + MLFloat16(-0.0477905273f), MLFloat16(-0.0795288086f), MLFloat16(0.498779297f), MLFloat16(0.350585938f), + MLFloat16(0.480712891f), MLFloat16(0.269775391f), MLFloat16(-0.246337891f), MLFloat16(0.190429688f), + MLFloat16(-0.118286133f), MLFloat16(-0.257568359f), MLFloat16(-0.339355469f), MLFloat16(-0.258056641f), + MLFloat16(-0.0828247070f), MLFloat16(0.351318359f), MLFloat16(-0.291259766f), MLFloat16(-0.433593750f), + MLFloat16(-0.134277344f), MLFloat16(0.440429688f), MLFloat16(0.0530700684f), MLFloat16(-0.350097656f), + MLFloat16(-0.284667969f), MLFloat16(-0.442138672f), MLFloat16(-0.0741577148f), MLFloat16(-0.109191895f), + MLFloat16(0.284423828f), MLFloat16(0.349853516f), MLFloat16(-0.193115234f), MLFloat16(0.326171875f), + MLFloat16(0.488037109f), MLFloat16(0.0557556152f), MLFloat16(-0.464599609f), MLFloat16(-0.0252380371f), + MLFloat16(-0.187866211f), MLFloat16(-0.147216797f), MLFloat16(0.207641602f), MLFloat16(0.471679688f), + MLFloat16(-0.0556640625f), MLFloat16(-0.498779297f), MLFloat16(0.227416992f), MLFloat16(0.458984375f), + MLFloat16(-0.472412109f), MLFloat16(-0.435791016f), MLFloat16(0.284179688f), MLFloat16(-0.270263672f), + MLFloat16(0.342285156f), MLFloat16(0.335693359f), MLFloat16(-0.194824219f), MLFloat16(-0.276855469f), + MLFloat16(-0.423828125f), MLFloat16(-0.438476562f), MLFloat16(0.437255859f), MLFloat16(0.306396484f), + MLFloat16(0.457031250f), MLFloat16(0.0529174805f), MLFloat16(-0.0236206055f), MLFloat16(-0.186035156f), + MLFloat16(0.0866699219f), MLFloat16(0.325439453f), MLFloat16(0.184570312f), MLFloat16(-0.198486328f), + MLFloat16(-0.275390625f), MLFloat16(0.320068359f), MLFloat16(-0.348388672f), MLFloat16(0.0999755859f), + MLFloat16(-0.113769531f), MLFloat16(0.212280273f), MLFloat16(-0.0231475830f), MLFloat16(0.167114258f), + MLFloat16(0.223144531f), MLFloat16(0.0361022949f), MLFloat16(-0.158691406f), MLFloat16(0.0599975586f), + MLFloat16(-0.0395202637f), MLFloat16(-0.484130859f), MLFloat16(0.329101562f), MLFloat16(-0.231201172f), + MLFloat16(0.394531250f), MLFloat16(-0.355468750f), MLFloat16(-0.170288086f), MLFloat16(-0.0550842285f), + MLFloat16(0.158569336f), MLFloat16(-0.418457031f), MLFloat16(-0.247436523f), MLFloat16(0.0360412598f), + MLFloat16(-0.283691406f), MLFloat16(0.460205078f), MLFloat16(0.291015625f), MLFloat16(-0.199340820f), + MLFloat16(0.380859375f), MLFloat16(-0.138427734f), MLFloat16(-0.238403320f), MLFloat16(-0.190673828f), + MLFloat16(-0.110595703f), MLFloat16(-0.0871582031f), MLFloat16(0.244506836f), MLFloat16(-0.147216797f), + MLFloat16(0.143676758f), MLFloat16(0.395507812f), MLFloat16(-0.125366211f), MLFloat16(0.115905762f), + MLFloat16(0.459716797f), MLFloat16(-0.300048828f), MLFloat16(-0.465820312f), MLFloat16(-0.339599609f), + MLFloat16(-0.267089844f), MLFloat16(0.361083984f), MLFloat16(-0.114257812f), MLFloat16(-0.0838012695f), + MLFloat16(-0.318115234f), MLFloat16(0.145141602f), MLFloat16(0.315673828f), MLFloat16(0.331787109f), + MLFloat16(-0.255859375f), MLFloat16(0.118896484f), MLFloat16(0.128295898f), MLFloat16(-0.331054688f), + MLFloat16(0.254882812f), MLFloat16(-0.467529297f), MLFloat16(-0.119812012f), MLFloat16(0.183471680f)}; vector X_shape = {2, 1, 4, 4, 4}; - vector W = {0.388077974319458f, -0.16366064548492432f, -0.42871910333633423f, 0.4276432394981384f, - 0.21517693996429443f, 0.007908165454864502f, 0.33897721767425537f, 0.21843165159225464f, - 0.34095364809036255f, -0.17043980956077576f, -0.013571739196777344f, -0.26793742179870605f, - -0.34863436222076416f, -0.2672275900840759f, -0.36691007018089294f, 0.37296557426452637f}; + vector W = { + MLFloat16(0.388183594f), MLFloat16(-0.163696289f), + MLFloat16(-0.428710938f), MLFloat16(0.427734375f), + MLFloat16(0.215209961f), MLFloat16(0.00791168213f), + MLFloat16(0.338867188f), MLFloat16(0.218383789f), + MLFloat16(0.341064453f), MLFloat16(-0.170410156f), + MLFloat16(-0.0135726929f), MLFloat16(-0.267822266f), + MLFloat16(-0.348632812f), MLFloat16(-0.267333984f), + MLFloat16(-0.366943359f), MLFloat16(0.373046875f)}; vector W_shape = {2, 1, 2, 2, 2}; - vector B = {0.4310183525085449f, -0.4564093053340912f}; + vector B = {MLFloat16(0.430908203f), MLFloat16(-0.456298828f)}; vector B_shape = {2}; vector Y_shape = {2, 2, 3, 3, 3}; - auto expected_vals = {0.5332361459732056f, 0.6628494262695312f, 0.544619083404541f, 0.4242798388004303f, - 0.6271085739135742f, 0.6721994876861572f, 0.43064039945602417f, 0.4246789515018463f, - 0.53834068775177f, 0.6932926177978516f, 0.42797625064849854f, 0.2218741625547409f, - 0.29522019624710083f, 0.8329390287399292f, 0.37605351209640503f, 0.43735477328300476f, - 0.2920728623867035f, 0.6692450046539307f, 0.5527016520500183f, 0.22643595933914185f, - 0.5138190984725952f, 0.3041342794895172f, 0.7423423528671265f, 0.26707080006599426f, - 0.4617553651332855f, 0.32416003942489624f, 0.511577844619751f, -0.28187549114227295f, - -0.5031181573867798f, -0.5793710947036743f, -0.5992864370346069f, -0.5055556893348694f, - -0.7562476396560669f, -0.44363799691200256f, -0.5730307102203369f, -0.6302952766418457f, - -0.4756688177585602f, -0.728988528251648f, -0.3900943398475647f, -0.6694478988647461f, - -0.38822290301322937f, -0.35774707794189453f, -0.39807581901550293f, -0.547709047794342f, - -0.35872578620910645f, -0.5326492786407471f, -0.40852290391921997f, -0.4537881314754486f, - -0.4545857608318329f, -0.379546195268631f, -0.5250767469406128f, -0.42439910769462585f, - -0.5558245182037354f, -0.38563215732574463f, 0.44995537400245667f, 0.5007325410842896f, - 0.49359965324401855f, 0.40685802698135376f, 0.407518208026886f, 0.4628955125808716f, - 0.4301188290119171f, 0.40635955333709717f, 0.4260363280773163f, 0.55128413438797f, - 0.5498291254043579f, 0.27105778455734253f, 0.40259143710136414f, 0.5747092962265015f, - 0.4187920391559601f, 0.4507707953453064f, 0.420598566532135f, 0.3950541913509369f, - 0.593889057636261f, 0.16578882932662964f, 0.5332239270210266f, 0.43014785647392273f, - 0.50260329246521f, 0.39225444197654724f, 0.4074971079826355f, 0.5073125958442688f, - 0.3823610544204712f, -0.4240749180316925f, -0.41936254501342773f, -0.5241475105285645f, - -0.5220003724098206f, -0.502869725227356f, -0.5122783780097961f, -0.4260129928588867f, - -0.4105660617351532f, -0.4483373165130615f, -0.33759188652038574f, -0.735706090927124f, - -0.3714444637298584f, -0.4888814687728882f, -0.6191370487213135f, -0.2640320658683777f, - -0.47542816400527954f, -0.5078460574150085f, -0.4205915927886963f, -0.5584549903869629f, - -0.39770257472991943f, -0.45317384600639343f, -0.5598302483558655f, -0.2542789578437805f, - -0.5359901785850525f, -0.48090484738349915f, -0.38603779673576355f, -0.4991581439971924f}; - - // For the CUDA EP: Due to CUDNN Frontend using TF32 for FP32 operations we get a higher error than using FP32 only, - // as TF32 has a 10 bit mantissa. - float epsilon = 2.1e-4f; - - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, false, epsilon); + auto expected_vals = { + MLFloat16(0.533115625f), MLFloat16(0.662707329f), MLFloat16(0.544498205f), + MLFloat16(0.424174339f), MLFloat16(0.627012968f), MLFloat16(0.672067642f), + MLFloat16(0.430530101f), MLFloat16(0.424569398f), MLFloat16(0.538250446f), + MLFloat16(0.693208933f), MLFloat16(0.427851349f), MLFloat16(0.221761703f), + MLFloat16(0.295077145f), MLFloat16(0.832913339f), MLFloat16(0.375999779f), + MLFloat16(0.437245011f), MLFloat16(0.291920483f), MLFloat16(0.669212699f), + MLFloat16(0.552566051f), MLFloat16(0.226370573f), MLFloat16(0.513698816f), + MLFloat16(0.303992242f), MLFloat16(0.742284894f), MLFloat16(0.266925812f), + MLFloat16(0.461661220f), MLFloat16(0.323991477f), MLFloat16(0.511511266f), + MLFloat16(-0.281706333f), MLFloat16(-0.502987564f), MLFloat16(-0.579300106f), + MLFloat16(-0.599243939f), MLFloat16(-0.505472362f), MLFloat16(-0.756186068f), + MLFloat16(-0.443522811f), MLFloat16(-0.572978139f), MLFloat16(-0.630189657f), + MLFloat16(-0.475540936f), MLFloat16(-0.728834927f), MLFloat16(-0.389986098f), + MLFloat16(-0.669373453f), MLFloat16(-0.387869477f), MLFloat16(-0.357608467f), + MLFloat16(-0.397931814f), MLFloat16(-0.547608852f), MLFloat16(-0.358573616f), + MLFloat16(-0.532473862f), MLFloat16(-0.408438683f), MLFloat16(-0.453677744f), + MLFloat16(-0.454452783f), MLFloat16(-0.379444361f), MLFloat16(-0.524981856f), + MLFloat16(-0.424284518f), MLFloat16(-0.555757523f), MLFloat16(-0.385479659f), + MLFloat16(0.449835509f), MLFloat16(0.500584960f), MLFloat16(0.493453026f), + MLFloat16(0.406748474f), MLFloat16(0.407412887f), MLFloat16(0.462785602f), + MLFloat16(0.430008084f), MLFloat16(0.406240731f), MLFloat16(0.425926626f), + MLFloat16(0.551153421f), MLFloat16(0.549696267f), MLFloat16(0.270993829f), + MLFloat16(0.402447432f), MLFloat16(0.574599743f), MLFloat16(0.418689728f), + MLFloat16(0.450668573f), MLFloat16(0.420462728f), MLFloat16(0.394942641f), + MLFloat16(0.593814850f), MLFloat16(0.165656328f), MLFloat16(0.533114314f), + MLFloat16(0.430018425f), MLFloat16(0.502558053f), MLFloat16(0.392109811f), + MLFloat16(0.407388866f), MLFloat16(0.507203162f), MLFloat16(0.382243097f), + MLFloat16(-0.423966885f), MLFloat16(-0.419248402f), MLFloat16(-0.524025679f), + MLFloat16(-0.521910012f), MLFloat16(-0.502744913f), MLFloat16(-0.512152255f), + MLFloat16(-0.425884366f), MLFloat16(-0.410446912f), MLFloat16(-0.448228836f), + MLFloat16(-0.337432563f), MLFloat16(-0.735596657f), MLFloat16(-0.371323436f), + MLFloat16(-0.488816738f), MLFloat16(-0.618983328f), MLFloat16(-0.263916761f), + MLFloat16(-0.475321025f), MLFloat16(-0.507732749f), MLFloat16(-0.420486867f), + MLFloat16(-0.558301449f), MLFloat16(-0.397618413f), MLFloat16(-0.453063041f), + MLFloat16(-0.559680939f), MLFloat16(-0.254149109f), MLFloat16(-0.535908163f), + MLFloat16(-0.480782807f), MLFloat16(-0.385932118f), MLFloat16(-0.499056786f)}; + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv2D_group) { +TEST(ConvFp16Test, Conv2D_group) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -660,20 +704,28 @@ TEST(ConvTest, Conv2D_group) { {} // excluded EPs }; - vector X = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f}; + vector X = { + MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), MLFloat16(7.0f), + MLFloat16(8.0f), MLFloat16(9.0f), MLFloat16(10.0f), MLFloat16(11.0f), + MLFloat16(12.0f), MLFloat16(13.0f), MLFloat16(14.0f), MLFloat16(15.0f), + MLFloat16(16.0f), MLFloat16(17.0f)}; vector X_shape = {1, 2, 3, 3}; - vector W = {1.0f, 2.0f}; + vector W = {MLFloat16(1.0f), MLFloat16(2.0f)}; vector W_shape = {2, 1, 1, 1}; vector Y_shape = {1, 2, 3, 3}; - auto expected_vals = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 18.0f, 20.0f, 22.0f, 24.0f, 26.0f, 28.0f, 30.0f, 32.0f, 34.0f}; - - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); - - // NNAPI/CoreML EP requires weight to be an initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + auto expected_vals = { + MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), MLFloat16(7.0f), + MLFloat16(8.0f), MLFloat16(18.0f), MLFloat16(20.0f), MLFloat16(22.0f), + MLFloat16(24.0f), MLFloat16(26.0f), MLFloat16(28.0f), MLFloat16(30.0f), + MLFloat16(32.0f), MLFloat16(34.0f)}; + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Depthwise2D_Bias_Group1_Issue18992) { +TEST(ConvFp16Test, Depthwise2D_Bias_Group1_Issue18992) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -684,20 +736,20 @@ TEST(ConvTest, Depthwise2D_Bias_Group1_Issue18992) { {} // excluded EPs }; - vector X = {1.0f}; + vector X = {MLFloat16(1.0f)}; vector X_shape = {1, 1, 1, 1}; - vector W = {0.5f}; + vector W = {MLFloat16(0.5f)}; vector W_shape = {1, 1, 1, 1}; - vector B = {0.5f}; + vector B = {MLFloat16(0.5f)}; vector B_shape = {1}; vector Y_shape = {1, 1, 1, 1}; - auto expected_vals = {1.0f}; + auto expected_vals = {MLFloat16(1.0f)}; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Depthwise2D_Bias_Group2) { +TEST(ConvFp16Test, Depthwise2D_Bias_Group2) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -708,34 +760,34 @@ TEST(ConvTest, Depthwise2D_Bias_Group2) { {} // excluded EPs }; - vector X = { - 0.0f, 1.0f, 2.0f, - 3.0f, 4.0f, 5.0f, - 6.0f, 7.0f, 8.0f, + vector X = { + MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), + MLFloat16(3.0f), MLFloat16(4.0f), MLFloat16(5.0f), + MLFloat16(6.0f), MLFloat16(7.0f), MLFloat16(8.0f), - 9.0f, 10.0f, 11.0f, - 12.0f, 13.0f, 14.0f, - 15.0f, 16.0f, 17.0f}; + MLFloat16(9.0f), MLFloat16(10.0f), MLFloat16(11.0f), + MLFloat16(12.0f), MLFloat16(13.0f), MLFloat16(14.0f), + MLFloat16(15.0f), MLFloat16(16.0f), MLFloat16(17.0f)}; vector X_shape = {1, 2, 3, 3}; - vector W = {1.0f, 2.0f}; + vector W = {MLFloat16(1.0f), MLFloat16(2.0f)}; vector W_shape = {2, 1, 1, 1}; - vector B = {1.0f, -1.0f}; + vector B = {MLFloat16(1.0f), MLFloat16(-1.0f)}; vector B_shape = {2}; vector Y_shape = {1, 2, 3, 3}; auto expected_vals = { - 1.0f, 2.0f, 3.0f, - 4.0f, 5.0f, 6.0f, - 7.0f, 8.0f, 9.0f, + MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), + MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f), - 17.0f, 19.0f, 21.0f, - 23.0f, 25.0f, 27.0f, - 29.0f, 31.0f, 33.0f}; + MLFloat16(17.0f), MLFloat16(19.0f), MLFloat16(21.0f), + MLFloat16(23.0f), MLFloat16(25.0f), MLFloat16(27.0f), + MLFloat16(29.0f), MLFloat16(31.0f), MLFloat16(33.0f)}; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Depthwise2D_Bias_Group15) { +TEST(ConvFp16Test, Depthwise2D_Bias_Group15) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -746,169 +798,169 @@ TEST(ConvTest, Depthwise2D_Bias_Group15) { {} // excluded EPs }; - vector X = { + vector X = { // C = 0 - 0.0f, 1.0f, - 2.0f, 3.0f, + MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(2.0f), MLFloat16(3.0f), // C = 1 - 4.0f, 5.0f, - 6.0f, 7.0f, + MLFloat16(4.0f), MLFloat16(5.0f), + MLFloat16(6.0f), MLFloat16(7.0f), // C = 2 - 8.0f, 9.0f, - 10.0f, 11.0f, + MLFloat16(8.0f), MLFloat16(9.0f), + MLFloat16(10.0f), MLFloat16(11.0f), // C = 3 - 12.0f, 13.0f, - 14.0f, 15.0f, + MLFloat16(12.0f), MLFloat16(13.0f), + MLFloat16(14.0f), MLFloat16(15.0f), // C = 4 - 16.0f, 17.0f, - 18.0f, 19.0f, + MLFloat16(16.0f), MLFloat16(17.0f), + MLFloat16(18.0f), MLFloat16(19.0f), // C = 5 - 20.0f, 21.0f, - 22.0f, 23.0f, + MLFloat16(20.0f), MLFloat16(21.0f), + MLFloat16(22.0f), MLFloat16(23.0f), // C = 6 - 24.0f, 25.0f, - 26.0f, 27.0f, + MLFloat16(24.0f), MLFloat16(25.0f), + MLFloat16(26.0f), MLFloat16(27.0f), // C = 7 - 28.0f, 29.0f, - 30.0f, 31.0f, + MLFloat16(28.0f), MLFloat16(29.0f), + MLFloat16(30.0f), MLFloat16(31.0f), // C = 8 - 32.0f, 33.0f, - 34.0f, 35.0f, + MLFloat16(32.0f), MLFloat16(33.0f), + MLFloat16(34.0f), MLFloat16(35.0f), // C = 9 - 36.0f, 37.0f, - 38.0f, 39.0f, + MLFloat16(36.0f), MLFloat16(37.0f), + MLFloat16(38.0f), MLFloat16(39.0f), // C = 10 - 40.0f, 41.0f, - 42.0f, 43.0f, + MLFloat16(40.0f), MLFloat16(41.0f), + MLFloat16(42.0f), MLFloat16(43.0f), // C = 11 - 44.0f, 45.0f, - 46.0f, 47.0f, + MLFloat16(44.0f), MLFloat16(45.0f), + MLFloat16(46.0f), MLFloat16(47.0f), // C = 12 - 48.0f, 49.0f, - 50.0f, 51.0f, + MLFloat16(48.0f), MLFloat16(49.0f), + MLFloat16(50.0f), MLFloat16(51.0f), // C = 13 - 52.0f, 53.0f, - 54.0f, 55.0f, + MLFloat16(52.0f), MLFloat16(53.0f), + MLFloat16(54.0f), MLFloat16(55.0f), // C = 14 - 56.0f, 57.0f, - 58.0f, 59.0f}; + MLFloat16(56.0f), MLFloat16(57.0f), + MLFloat16(58.0f), MLFloat16(59.0f)}; vector X_shape = {1, 15, 2, 2}; - vector W = { + vector W = { // M = 0 - 0.0f, 1.0f, - 2.0f, 3.0f, + MLFloat16(0.0f), MLFloat16(1.0f), + MLFloat16(2.0f), MLFloat16(3.0f), // M = 1 - 4.0f, 5.0f, - 6.0f, 7.0f, + MLFloat16(4.0f), MLFloat16(5.0f), + MLFloat16(6.0f), MLFloat16(7.0f), // M = 2 - 8.0f, 9.0f, - 10.0f, 11.0f, + MLFloat16(8.0f), MLFloat16(9.0f), + MLFloat16(10.0f), MLFloat16(11.0f), // M = 3 - 12.0f, 13.0f, - 14.0f, 15.0f, + MLFloat16(12.0f), MLFloat16(13.0f), + MLFloat16(14.0f), MLFloat16(15.0f), // M = 4 - 16.0f, 17.0f, - 18.0f, 19.0f, + MLFloat16(16.0f), MLFloat16(17.0f), + MLFloat16(18.0f), MLFloat16(19.0f), // M = 5 - 20.0f, 21.0f, - 22.0f, 23.0f, + MLFloat16(20.0f), MLFloat16(21.0f), + MLFloat16(22.0f), MLFloat16(23.0f), // M = 6 - 24.0f, 25.0f, - 26.0f, 27.0f, + MLFloat16(24.0f), MLFloat16(25.0f), + MLFloat16(26.0f), MLFloat16(27.0f), // M = 7 - 28.0f, 29.0f, - 30.0f, 31.0f, + MLFloat16(28.0f), MLFloat16(29.0f), + MLFloat16(30.0f), MLFloat16(31.0f), // M = 8 - 32.0f, 33.0f, - 34.0f, 35.0f, + MLFloat16(32.0f), MLFloat16(33.0f), + MLFloat16(34.0f), MLFloat16(35.0f), // M = 9 - 36.0f, 37.0f, - 38.0f, 39.0f, + MLFloat16(36.0f), MLFloat16(37.0f), + MLFloat16(38.0f), MLFloat16(39.0f), // M = 10 - 40.0f, 41.0f, - 42.0f, 43.0f, + MLFloat16(40.0f), MLFloat16(41.0f), + MLFloat16(42.0f), MLFloat16(43.0f), // M = 11 - 44.0f, 45.0f, - 46.0f, 47.0f, + MLFloat16(44.0f), MLFloat16(45.0f), + MLFloat16(46.0f), MLFloat16(47.0f), // M = 12 - 48.0f, 49.0f, - 50.0f, 51.0f, + MLFloat16(48.0f), MLFloat16(49.0f), + MLFloat16(50.0f), MLFloat16(51.0f), // M = 13 - 52.0f, 53.0f, - 54.0f, 55.0f, + MLFloat16(52.0f), MLFloat16(53.0f), + MLFloat16(54.0f), MLFloat16(55.0f), // M = 14 - 56.0f, 57.0f, - 58.0f, 59.0f}; + MLFloat16(56.0f), MLFloat16(57.0f), + MLFloat16(58.0f), MLFloat16(59.0f)}; vector W_shape = {15, 1, 2, 2}; - vector B = { - 101.0f, - 102.0f, - 103.0f, - 104.0f, - 105.0f, - 106.0f, - 107.0f, - 108.0f, - 109.0f, - 110.0f, - 111.0f, - 112.0f, - 113.0f, - 114.0f, - 115.0f}; + vector B = { + MLFloat16(101.0f), + MLFloat16(102.0f), + MLFloat16(103.0f), + MLFloat16(104.0f), + MLFloat16(105.0f), + MLFloat16(106.0f), + MLFloat16(107.0f), + MLFloat16(108.0f), + MLFloat16(109.0f), + MLFloat16(110.0f), + MLFloat16(111.0f), + MLFloat16(112.0f), + MLFloat16(113.0f), + MLFloat16(114.0f), + MLFloat16(115.0f)}; vector B_shape = {15}; vector Y_shape = {1, 15, 1, 1}; auto expected_vals = { - 115.0f, // 0.0*0.0 + 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 101.0 - 228.0f, - 469.0f, - 838.0f, - 1335.0f, - 1960.0f, - 2713.0f, // 24.0*24.0 + 25.0*25.0 + 26.0*26.0 + 27.0*27.0 + 107.0 - 3594.0f, - 4603.0f, - 5740.0f, - 7005.0f, - 8398.0f, - 9919.0f, // 48.0*48.0 + 49.0*49.0 + 50.0*50.0 + 51.0*51.0 + 113.0 - 11568.0f, // 52.0*52.0 + 53.0*53.0 + 54.0*54.0 + 55.0*55.0 + 114.0 - 13345.0f // 56.0*56.0 + 57.0*57.0 + 58.0*58.0 + 59.0*59.0 + 115.0 + MLFloat16(115.0f), // 0.0*0.0 + 1.0*1.0 + 2.0*2.0 + 3.0*3.0 + 101.0 + MLFloat16(228.0f), + MLFloat16(469.0f), + MLFloat16(838.0f), + MLFloat16(1335.0f), + MLFloat16(1960.0f), + MLFloat16(2713.0f), // 24.0*24.0 + 25.0*25.0 + 26.0*26.0 + 27.0*27.0 + 107.0 + MLFloat16(3594.0f), + MLFloat16(4603.0f), + MLFloat16(5740.0f), + MLFloat16(7005.0f), + MLFloat16(8398.0f), + MLFloat16(9919.0f), // 48.0*48.0 + 49.0*49.0 + 50.0*50.0 + 51.0*51.0 + 113.0 + MLFloat16(11568.0f), // 52.0*52.0 + 53.0*53.0 + 54.0*54.0 + 55.0*55.0 + 114.0 + MLFloat16(13345.0f) // 56.0*56.0 + 57.0*57.0 + 58.0*58.0 + 59.0*59.0 + 115.0 }; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, ConvDimWithZero) { +TEST(ConvFp16Test, ConvDimWithZero) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1, 1}, // dilations @@ -919,20 +971,16 @@ TEST(ConvTest, ConvDimWithZero) { {} // excluded EPs }; - vector X = vector(); + vector X; vector X_shape = {0, 2, 4, 4}; // N of 0 should be handled - vector W = {1.0f, 2.0f, 1.0f, 2.0f}; + vector W = {MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(1.0f), MLFloat16(2.0f)}; vector W_shape = {2, 2, 1, 1}; vector out_shape = {0, 2, 4, 4}; - // not handled by ACL - attrs.excluded_providers.insert(kAclExecutionProvider); - - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, {}, out_shape, false, optional(), - OpTester::ExpectResult::kExpectSuccess, "", 10); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, {}, out_shape); } -TEST(ConvTest, Conv1D_asymmetric_padding) { +TEST(ConvFp16Test, Conv1D_asymmetric_padding) { ConvOpAndTestAttributes attrs = { "", // auto_pad vector{1}, // dilations @@ -943,21 +991,20 @@ TEST(ConvTest, Conv1D_asymmetric_padding) { {} // excluded EPs }; - vector X = {1.f, 2.f, 3.f}; + vector X = {MLFloat16(1.f), MLFloat16(2.f), MLFloat16(3.f)}; vector X_shape = {1, 1, 3}; - vector W = {1.f, 1.f, 1.f}; + vector W = {MLFloat16(1.f), MLFloat16(1.f), MLFloat16(1.f)}; vector W_shape = {1, 1, 3}; - vector B = {0.f}; + vector B = {MLFloat16()}; vector B_shape = {1}; vector Y_shape = {1, 1, 2}; - auto expected_vals = {3.f, 6.f}; - - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + auto expected_vals = {MLFloat16(3.f), MLFloat16(6.f)}; - TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true); } -TEST(ConvTest, Conv_AutoPad_with_non_default_strides) { +TEST(ConvFp16Test, Conv_AutoPad_with_non_default_strides) { ConvOpAndTestAttributes attrs = { "SAME_LOWER", // auto_pad vector{1, 1}, // dilations @@ -968,29 +1015,312 @@ TEST(ConvTest, Conv_AutoPad_with_non_default_strides) { {} // excluded EPs }; - vector X = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, - 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, - 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, - 15.0f, 16.0f, 17.0f, 18.0f, - 19.0f, 20.0f, 21.0, 22.0f, 23.0f, 24.0f}; + vector X = { + MLFloat16(0.0f), MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), MLFloat16(4.0f), + MLFloat16(5.0f), MLFloat16(6.0f), MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f), + MLFloat16(10.0f), MLFloat16(11.0f), MLFloat16(12.0f), MLFloat16(13.0f), MLFloat16(14.0f), + MLFloat16(15.0f), MLFloat16(16.0f), MLFloat16(17.0f), MLFloat16(18.0f), MLFloat16(19.0f), + MLFloat16(20.0f), MLFloat16(21.0f), MLFloat16(22.0f), MLFloat16(23.0f), MLFloat16(24.0f)}; vector X_shape = {1, 1, 5, 5}; - vector W = {1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f}; + vector W = {MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f)}; vector W_shape = {1, 1, 3, 3}; - auto expected_vals = {12.0f, 27.0f, 24.0f, - 63.0f, 108.0f, 81.0f, - 72.0f, 117.0f, 84.0f}; + auto expected_vals = {MLFloat16(12.0f), MLFloat16(27.0f), MLFloat16(24.0f), + MLFloat16(63.0f), MLFloat16(108.0f), MLFloat16(81.0f), + MLFloat16(72.0f), MLFloat16(117.0f), MLFloat16(84.0f)}; vector Y_shape = {1, 1, 3, 3}; - // Test with weight as initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); +} + +TEST(ConvFp16Test, Pointwise_2D) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1}, // dilations + 1, // group + vector{1, 1}, // kernel_shape + vector{0, 0, 0, 0}, // pads + vector{1, 1}, // strides + {} // excluded EPs + }; + vector X = { + MLFloat16(-9.f), MLFloat16(1.f), MLFloat16(2.f), + MLFloat16(-5.f), MLFloat16(3.f), MLFloat16(-2.f), + MLFloat16(5.f), MLFloat16(-3.f), MLFloat16(1.f), + MLFloat16(1.f), MLFloat16(8.f), MLFloat16(-4.f), + MLFloat16(-1.f), MLFloat16(6.f), MLFloat16(7.f), + MLFloat16(-1.f), MLFloat16(4.f), MLFloat16(-5.f), + MLFloat16(-9.f), MLFloat16(1.f), MLFloat16(2.f), + MLFloat16(-5.f), MLFloat16(3.f), MLFloat16(-2.f), + MLFloat16(5.f), MLFloat16(-3.f), MLFloat16(1.f)}; + vector X_shape = {1, 3, 3, 3}; + vector W = {MLFloat16(2.f), MLFloat16(-3.f), MLFloat16(0.5f), + MLFloat16(0.25f), MLFloat16(-2.f), MLFloat16(-0.75f)}; + vector W_shape = {2, 3, 1, 1}; + vector Y_shape = {1, 2, 3, 3}; + auto expected_vals = { + MLFloat16(-25.5f), MLFloat16(-21.5f), MLFloat16(17.f), + MLFloat16(-9.5f), MLFloat16(-10.5f), MLFloat16(-26.f), + MLFloat16(15.5f), MLFloat16(-19.5f), MLFloat16(17.5f), + MLFloat16(2.5f), MLFloat16(-16.5f), MLFloat16(7.f), + MLFloat16(4.5f), MLFloat16(-13.5f), MLFloat16(-13.f), + MLFloat16(-0.5f), MLFloat16(-6.5f), MLFloat16(9.5f)}; + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); +} + +TEST(ConvFp16Test, Pointwise_3D) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1, 1}, // dilations + 1, // group + vector{1, 1, 1}, // kernel_shape + vector{0, 0, 0, 0, 0, 0}, // pads + vector{1, 1, 1}, // strides + {} // excluded EPs + }; + + vector X = { + MLFloat16(2 / 16.f), MLFloat16(3 / 16.f), MLFloat16(4 / 16.f), + MLFloat16(5 / 16.f), MLFloat16(6 / 16.f), MLFloat16(7 / 16.f), + MLFloat16(8 / 16.f), MLFloat16(9 / 16.f), MLFloat16(10 / 16.f), + MLFloat16(11 / 16.f), MLFloat16(12 / 16.f), MLFloat16(13 / 16.f), + MLFloat16(14 / 16.f), MLFloat16(15 / 16.f), MLFloat16(16 / 16.f), + MLFloat16(17 / 16.f), MLFloat16(18 / 16.f), MLFloat16(19 / 16.f), + MLFloat16(20 / 16.f), MLFloat16(21 / 16.f), MLFloat16(22 / 16.f), + MLFloat16(23 / 16.f), MLFloat16(24 / 16.f), MLFloat16(25 / 16.f), + MLFloat16(26 / 16.f), MLFloat16(27 / 16.f), MLFloat16(28 / 16.f)}; + vector X_shape = {1, 1, 3, 3, 3}; + + vector W = {MLFloat16(0.5f)}; + vector W_shape = {1, 1, 1, 1, 1}; + + auto expected_vals = { + MLFloat16(0.0625f), MLFloat16(0.09375f), MLFloat16(0.125f), + MLFloat16(0.15625f), MLFloat16(0.1875f), MLFloat16(0.21875f), + MLFloat16(0.25f), MLFloat16(0.28125f), MLFloat16(0.3125f), + MLFloat16(0.34375f), MLFloat16(0.375f), MLFloat16(0.40625f), + MLFloat16(0.4375f), MLFloat16(0.46875f), MLFloat16(0.5f), + MLFloat16(0.53125f), MLFloat16(0.5625f), MLFloat16(0.59375f), + MLFloat16(0.625f), MLFloat16(0.65625f), MLFloat16(0.6875f), + MLFloat16(0.71875f), MLFloat16(0.75f), MLFloat16(0.78125f), + MLFloat16(0.8125f), MLFloat16(0.84375f), MLFloat16(0.875f)}; + vector Y_shape = {1, 1, 3, 3, 3}; // Test with weight as initializer - TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); } +#ifndef DISABLE_CONTRIB_OPS + +TEST(ConvFp16Test, Pointwise_Relu) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1}, // dilations + 1, // group + vector{1, 1}, // kernel_shape + vector{0, 0, 0, 0}, // pads + vector{1, 1}, // strides + {}, // excluded EPs + "Relu" // activation + }; + + vector X = { + MLFloat16(-9.f), MLFloat16(1.f), MLFloat16(-9.f), + MLFloat16(1.f), MLFloat16(8.f), MLFloat16(1.f), + MLFloat16(2.f), MLFloat16(-4.f), MLFloat16(2.f), + MLFloat16(-5.f), MLFloat16(-1.f), MLFloat16(-5.f), + MLFloat16(3.f), MLFloat16(6.f), MLFloat16(3.f), + MLFloat16(-2.f), MLFloat16(7.f), MLFloat16(-2.f), + MLFloat16(5.f), MLFloat16(-1.f), MLFloat16(5.f), + MLFloat16(-3.f), MLFloat16(4.f), MLFloat16(-3.f), + MLFloat16(1.f), MLFloat16(-5.f), MLFloat16(1.f)}; + vector X_shape = {1, 3, 3, 3}; + vector W = {MLFloat16(2.f), MLFloat16(-3.f), MLFloat16(0.5f), + MLFloat16(0.25f), MLFloat16(-2.f), MLFloat16(-0.75f)}; + vector W_shape = {2, 3, 1, 1}; + vector Y_shape = {1, 3, 3, 2}; + auto expected_vals = { + MLFloat16(0.f), MLFloat16(2.5f), + MLFloat16(0.f), MLFloat16(0.f), + MLFloat16(17.f), MLFloat16(7.f), + MLFloat16(0.f), MLFloat16(4.5f), + MLFloat16(0.f), MLFloat16(0.f), + MLFloat16(0.f), MLFloat16(0.f), + MLFloat16(15.5f), MLFloat16(0.f), + MLFloat16(0.f), MLFloat16(0.f), + MLFloat16(17.5f), MLFloat16(9.5f)}; + + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); +} + +TEST(ConvFp16Test, Conv2D_HardSigmoid) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1}, // dilations + 1, // group + vector{2, 2}, // kernel_shape + vector{0, 0, 0, 0}, // pads + vector{1, 1}, // strides + {}, // excluded EPs + "HardSigmoid", // activation + vector{0.2f, 0.5f} // activation_parameters + }; + + vector X = {MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), + MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f)}; + vector X_shape = {1, 3, 3, 1}; + vector W = {MLFloat16(0.125f), MLFloat16(0.125f), MLFloat16(0.125f), MLFloat16(0.125f), + MLFloat16(-0.125f), MLFloat16(-0.125f), MLFloat16(-0.125f), MLFloat16(-0.125f)}; + vector W_shape = {2, 1, 2, 2}; + vector Y_shape = {1, 2, 2, 2}; + auto expected_vals = { + MLFloat16(0.8f), MLFloat16(0.2f), + MLFloat16(0.9f), MLFloat16(0.1f), + MLFloat16(1.0f), MLFloat16(0.0f), + MLFloat16(1.0f), MLFloat16(0.0f)}; + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true); +} + +TEST(ConvFp16Test, Conv2D_Bias_Z_Relu) { + ConvOpAndTestAttributes attrs = { + "", // auto_pad + vector{1, 1}, // dilations + 1, // group + vector{2, 2}, // kernel_shape + vector{0, 0, 0, 0}, // pads + vector{1, 1}, // strides + {}, // excluded EPs + "Relu" // activation + }; + + vector X = {MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), + MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f)}; + vector X_shape = {1, 3, 3, 1}; + vector W = {MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f)}; + vector W_shape = {2, 1, 2, 2}; + vector Y_shape = {1, 2, 2, 2}; + vector B = {MLFloat16(1.0f), MLFloat16(-1.0f)}; + vector B_shape = {2}; + vector Z = {MLFloat16(-1.0f), MLFloat16(0.0f), MLFloat16(0.0f), MLFloat16(0.0f), + MLFloat16(0.0f), MLFloat16(0.0f), MLFloat16(0.0f), MLFloat16(1.0f)}; + vector Z_shape = {1, 2, 2, 2}; + auto expected_vals = {MLFloat16(12.0f), MLFloat16(11.0f), MLFloat16(17.0f), MLFloat16(15.0f), MLFloat16(25.0f), MLFloat16(23.0f), MLFloat16(29.0f), MLFloat16(28.0f)}; + TestConvFp16Op(attrs, {X, W, B, Z}, {X_shape, W_shape, B_shape, Z_shape}, expected_vals, Y_shape); + TestConvFp16Op(attrs, {X, W, B, Z}, {X_shape, W_shape, B_shape, Z_shape}, expected_vals, Y_shape, true); +} + +#endif // CONTRIB_OPS + +#ifndef ENABLE_TRAINING +// Prepacking is disabled in full training build so no need to test the feature in a training build. + +const onnxruntime::RunOptions run_options = []() { + onnxruntime::RunOptions options{}; + ORT_THROW_IF_ERROR(options.config_options.AddConfigEntry(kOpTesterRunOptionsConfigTestTunableOp, "true")); + return options; +}(); + +const constexpr auto run_with_tunable_op = &run_options; + +TEST(ConvFp16Test, SharedPrepackedWeights) { + OpTester test("Conv", 11); + + vector X = {MLFloat16(1.0f), MLFloat16(2.0f), MLFloat16(3.0f), + MLFloat16(4.0f), MLFloat16(5.0f), MLFloat16(6.0f), + MLFloat16(7.0f), MLFloat16(8.0f), MLFloat16(9.0f)}; + vector X_shape = {1, 1, 3, 3}; + vector W = {MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), + MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f), MLFloat16(1.0f)}; + vector W_shape = {2, 1, 2, 2}; + vector Y_shape = {1, 2, 2, 2}; + vector B = {MLFloat16(1.0f), MLFloat16(-1.0f)}; + vector B_shape = {2}; + auto expected_vals = { + MLFloat16(13.0f), MLFloat16(17.0f), MLFloat16(25.0f), MLFloat16(29.0f), + MLFloat16(11.0f), MLFloat16(15.0f), MLFloat16(23.0f), MLFloat16(27.0f)}; + + test.AddInput("X", X_shape, X); + test.AddInput("W", W_shape, W, true); + test.AddInput("B", B_shape, B, true); + test.AddOutput("Y", Y_shape, expected_vals, /*no sort*/ false, 0.002f, 0.0f); + + OrtValue w; + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape(W_shape), + W.data(), OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator), w); + + SessionOptions so; + // Set up B as a shared initializer to be shared between sessions + ASSERT_EQ(so.AddInitializer("W", &w), Status::OK()); + + // We want all sessions running using this OpTester to be able to share pre-packed weights if applicable + test.EnableSharingOfPrePackedWeightsAcrossSessions(); + + // Pre-packing is limited just to the CPU EP for now and we will only test the CPU EP + // and we want to ensure that it is available in this build + auto cpu_ep = []() -> std::vector> { + std::vector> execution_providers; + execution_providers.push_back(DefaultCpuExecutionProvider()); + return execution_providers; + }; + + size_t number_of_pre_packed_weights_counter_session_1 = 0; + size_t number_of_shared_pre_packed_weights_counter = 0; + + // Session 1 + { + test.Config(so) + .Config(run_with_tunable_op) + .ConfigEps(cpu_ep()) + .RunWithConfig(&number_of_pre_packed_weights_counter_session_1, &number_of_shared_pre_packed_weights_counter); + // Assert that no pre-packed weights have been shared thus far + ASSERT_EQ(number_of_shared_pre_packed_weights_counter, static_cast(0)); + } + + auto number_of_elements_in_shared_prepacked_buffers_container = + test.GetNumPrePackedWeightsShared(); + // Assert that the number of elements in the shared container + // is the same as the number of weights that have been pre-packed + ASSERT_EQ(number_of_pre_packed_weights_counter_session_1, number_of_elements_in_shared_prepacked_buffers_container); + + // On some platforms/architectures MLAS may choose to not do any pre-packing and the number of elements + // that have been pre-packed will be zero in which case we do not continue with the testing + // of "sharing" of pre-packed weights as there are no pre-packed weights to be shared at all. + if (number_of_pre_packed_weights_counter_session_1 == 0) + return; + + // Session 2 + { + size_t number_of_pre_packed_weights_counter_session_2 = 0; + test.Config(so) + .Config(run_with_tunable_op) + .ConfigEps(cpu_ep()) + .RunWithConfig(&number_of_pre_packed_weights_counter_session_2, &number_of_shared_pre_packed_weights_counter); + + // Assert that the same number of weights were pre-packed in both sessions + ASSERT_EQ(number_of_pre_packed_weights_counter_session_1, number_of_pre_packed_weights_counter_session_2); + + // Assert that the number of pre-packed weights that were shared equals + // the number of pre-packed weights in the second session + ASSERT_EQ(number_of_pre_packed_weights_counter_session_2, + static_cast(number_of_shared_pre_packed_weights_counter)); + } +} + +#endif + } // namespace test -} // namespace onnxruntime \ No newline at end of file +} // namespace onnxruntime + +#endif // MLAS_F16VEC_INTRINSICS_SUPPORTED \ No newline at end of file