// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "gtest/gtest.h" #include "core/graph/contrib_ops/contrib_defs.h" #include "test/unittest_util/function_test_util.h" using namespace ::onnxruntime::common; namespace onnxruntime { namespace test { static void RegisterSchemas() { static bool registered = false; if (!registered) { onnxruntime::contrib::RegisterContribSchemas(); registered = true; } } class ContribFunExpansionTest : public ::testing::Test { protected: void SetUp() override { RegisterSchemas(); } }; template void CheckLayerNorm(bool compute_mean = true, bool compute_isd = true, std::vector shape1 = {8, 16}, std::vector shape2 = {16}, int64_t axis = -1) { FunctionTestCase testCase("LayerNormalization", kOnnxDomain); testCase.AddInput("x", shape1); testCase.AddInput("scale", shape2); testCase.AddInput("bias", shape2); testCase.AddOutput("y"); testCase.AddOutput(compute_mean ? "mean" : ""); testCase.AddOutput(compute_isd ? "invstddev" : ""); testCase.AddAttribute("stash_type", utils::ToTensorProtoElementType()); if (axis != -1) testCase.AddAttribute("axis", axis); if (RunTest) testCase.RunTest(); else testCase.CreateModel(true); } TEST_F(ContribFunExpansionTest, LayerNorm) { // Test expand-and-run CheckLayerNorm(); // Test expand-and-check-only CheckLayerNorm(); } TEST_F(ContribFunExpansionTest, LayerNorm_OptionalOutputs) { // Test expand-and-run CheckLayerNorm(false, false); CheckLayerNorm(false, true); CheckLayerNorm(true, false); } TEST_F(ContribFunExpansionTest, LayerNorm_OtherShapes) { // Test expand-and-run CheckLayerNorm(true, true, {4, 2, 8}, {2, 8}, 1); } template void CheckGelu() { FunctionTestCase testCase("Gelu", kMSDomain); std::vector shape{8, 16}; testCase.AddInput("x", shape); testCase.AddOutput("y"); // Only check expanded graph. Can't run it yet because no implementation of Erf is available yet. testCase.CreateModel(true); } TEST_F(ContribFunExpansionTest, Gelu) { CheckGelu(); CheckGelu(); CheckGelu(); CheckGelu(); } template void CheckFastGelu(bool withBias = true) { FunctionTestCase testCase("FastGelu", kMSDomain); std::vector shape{8, 16}; std::vector bias_shape{16}; testCase.AddInput("x", shape); if (withBias) { testCase.AddInput("bias", bias_shape); } testCase.AddOutput("y"); if (RunTest) testCase.RunTest(); else testCase.CreateModel(true); } TEST_F(ContribFunExpansionTest, FastGeluWithBias) { CheckFastGelu(true); CheckFastGelu(true); CheckFastGelu(true); } TEST_F(ContribFunExpansionTest, FastGeluWithoutBias) { CheckFastGelu(false); CheckFastGelu(false); CheckFastGelu(false); } } // namespace test } // namespace onnxruntime