// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the MIT License. #include "core/mlas/inc/mlas.h" #include "core/session/onnxruntime_session_options_config_keys.h" #include "gtest/gtest.h" #include "test/providers/provider_test_utils.h" #include "core/session/inference_session.h" #include "test/common/dnnl_op_test_utils.h" #include "test/common/cuda_op_test_utils.h" #include "test/common/tensor_op_test_utils.h" #include "test/util/include/test_environment.h" #include "default_providers.h" #include #include #include #include #if defined(MLAS_SBGEMM_AVAILABLE) namespace onnxruntime { namespace test { namespace { 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; } // namespace template struct MatMulTestData { std::string name; std::vector input0_dims; std::vector input1_dims; std::vector expected_dims; std::vector expected_vals; }; template std::vector> GenerateTestCases() { std::vector> test_cases; test_cases.push_back( {"test padding and broadcast A > B", {3, 1, 1, 6}, {2, 6, 7}, {3, 2, 1, 7}, {385, 400, 415, 430, 445, 460, 475, 1015, 1030, 1045, 1060, 1075, 1090, 1105, 1015, 1066, 1117, 1168, 1219, 1270, 1321, 3157, 3208, 3259, 3310, 3361, 3412, 3463, 1645, 1732, 1819, 1906, 1993, 2080, 2167, 5299, 5386, 5473, 5560, 5647, 5734, 5821}}); test_cases.push_back( {"test padding and broadcast B > A", {2, 3, 12}, {3, 2, 12, 3}, {3, 2, 3, 3}, {1518, 1584, 1650, 3894, 4104, 4314, 6270, 6624, 6978, 26574, 27072, 27570, 34134, 34776, 35418, 41694, 42480, 43266, 6270, 6336, 6402, 19014, 19224, 19434, 31758, 32112, 32466, 62430, 62928, 63426, 80358, 81000, 81642, 98286, 99072, 99858, 11022, 11088, 11154, 34134, 34344, 34554, 57246, 57600, 57954, 98286, 98784, 99282, 126582, 127224, 127866, 154878, 155664, 156450}}); test_cases.push_back( {"test 2D", {8, 6}, {6, 6}, {8, 6}, {330, 345, 360, 375, 390, 405, 870, 921, 972, 1023, 1074, 1125, 1410, 1497, 1584, 1671, 1758, 1845, 1950, 2073, 2196, 2319, 2442, 2565, 2490, 2649, 2808, 2967, 3126, 3285, 3030, 3225, 3420, 3615, 3810, 4005, 3570, 3801, 4032, 4263, 4494, 4725, 4110, 4377, 4644, 4911, 5178, 5445}}); test_cases.push_back( {"test 2D special", {2, 2, 16}, {16, 4}, {2, 2, 4}, {4960, 5080, 5200, 5320, 12640, 13016, 13392, 13768, 20320, 20952, 21584, 22216, 28000, 28888, 29776, 30664}}); test_cases.push_back( {"test 2D special 2", {2, 2, 9}, {1, 9, 4}, {2, 2, 4}, {816, 852, 888, 924, 2112, 2229, 2346, 2463, 3408, 3606, 3804, 4002, 4704, 4983, 5262, 5541}}); test_cases.push_back( {"test 2D special 3", {2, 12}, {1, 1, 12, 3}, {1, 1, 2, 3}, {1518, 1584, 1650, 3894, 4104, 4314}}); test_cases.push_back( {"test 3D batch", {3, 1, 18}, {3, 18, 2}, {3, 1, 2}, { // clang-format off 3570, 3723, 26250, 26727, 72258, 73059, // clang-format on }}); test_cases.push_back( {"test 4D batch", {2, 2, 1, 20}, {2, 2, 20, 2}, {2, 2, 1, 2}, { // clang-format off 4940, 5130, 36140, 36730, 99340, 100330, 194540, 195930, // clang-format on }}); return test_cases; } template void RunMatMulTest(int32_t opset_version, bool is_a_constant, bool is_b_constant, bool disable_fastmath) { for (auto t : GenerateTestCases()) { SCOPED_TRACE("test case: " + t.name); OpTester test("MatMul", opset_version); int64_t size0 = TensorShape::FromExistingBuffer(t.input0_dims).SizeHelper(0, t.input0_dims.size()); std::vector input0_vals = ValueRange(size0); test.AddInput("A", t.input0_dims, input0_vals, is_a_constant); int64_t size1 = TensorShape::FromExistingBuffer(t.input1_dims).SizeHelper(0, t.input1_dims.size()); std::vector input1_vals = ValueRange(size1); test.AddInput("B", t.input1_dims, input1_vals, is_b_constant); test.AddOutput("Y", t.expected_dims, t.expected_vals); // OpenVINO EP: Disabled temporarily matmul broadcasting not fully supported // Disable TensorRT because of unsupported data type std::unordered_set excluded_providers{kTensorrtExecutionProvider, kOpenVINOExecutionProvider}; if (t.name == "test 2D empty input") { // NNAPI: currently fails for the "test 2D empty input" case excluded_providers.insert(kNnapiExecutionProvider); } if ("test padding and broadcast A > B" == t.name || "test 2D empty input" == t.name) { // QNN can't handle 0 shap excluded_providers.insert(kQnnExecutionProvider); } SessionOptions so; ASSERT_STATUS_OK(so.config_options.AddConfigEntry( kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16, "1")); test.ConfigExcludeEps(excluded_providers) .Config(run_with_tunable_op) .Config(so) .RunWithConfig(); if (disable_fastmath) { ASSERT_STATUS_OK(so.config_options.AddConfigEntry( kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16, "0")); test.ConfigExcludeEps(excluded_providers) .Config(run_with_tunable_op) .Config(so) .RunWithConfig(); } } } template void RunMatMulTest(int32_t opset_version) { RunMatMulTest(opset_version, false, false, false); } TEST(MathOpTest, MatMulFloatTypeFastMathKTailDoesNotReadPaddedA) { constexpr int64_t M = 1; constexpr int64_t N = 8; for (const int64_t K : std::array{13, 14, 15, 16, 17}) { SCOPED_TRACE("K=" + std::to_string(K)); std::vector input0_vals(K); std::iota(input0_vals.begin(), input0_vals.end(), 1.0f); std::vector input1_vals(K * N, 1.0f); std::vector expected_vals(N, std::accumulate(input0_vals.begin(), input0_vals.end(), 0.0f)); ONNX_NAMESPACE::ModelProto model; model.set_ir_version(ONNX_NAMESPACE::Version::IR_VERSION); model.add_opset_import()->set_version(7); auto* graph = model.mutable_graph(); graph->set_name("MatMulKTailGraph"); auto* input = graph->add_input(); input->set_name("A"); auto* input_tensor_type = input->mutable_type()->mutable_tensor_type(); input_tensor_type->set_elem_type(ONNX_NAMESPACE::TensorProto_DataType_FLOAT); input_tensor_type->mutable_shape()->add_dim()->set_dim_value(M); input_tensor_type->mutable_shape()->add_dim()->set_dim_value(K); auto* output = graph->add_output(); output->set_name("Y"); auto* output_tensor_type = output->mutable_type()->mutable_tensor_type(); output_tensor_type->set_elem_type(ONNX_NAMESPACE::TensorProto_DataType_FLOAT); output_tensor_type->mutable_shape()->add_dim()->set_dim_value(M); output_tensor_type->mutable_shape()->add_dim()->set_dim_value(N); auto* b_initializer = graph->add_initializer(); b_initializer->set_name("B"); b_initializer->set_data_type(ONNX_NAMESPACE::TensorProto_DataType_FLOAT); b_initializer->add_dims(K); b_initializer->add_dims(N); for (float value : input1_vals) { b_initializer->add_float_data(value); } auto* matmul_node = graph->add_node(); matmul_node->set_name("MatMulKTail"); matmul_node->set_op_type("MatMul"); matmul_node->add_input("A"); matmul_node->add_input("B"); matmul_node->add_output("Y"); std::string serialized_model; ASSERT_TRUE(model.SerializeToString(&serialized_model)); SessionOptions so; ASSERT_STATUS_OK(so.config_options.AddConfigEntry( kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16, "1")); InferenceSession session_object{so, GetEnvironment()}; ASSERT_STATUS_OK(session_object.RegisterExecutionProvider(DefaultCpuExecutionProvider())); std::stringstream model_stream(serialized_model); ASSERT_STATUS_OK(session_object.Load(model_stream)); ASSERT_STATUS_OK(session_object.Initialize()); std::vector input0_backing(input0_vals.begin(), input0_vals.end()); // Poison a full 4-float NEON load past the logical A row to verify that // SBGemm K-tail handling does not consume overread values. input0_backing.resize(K + 4, std::numeric_limits::quiet_NaN()); OrtValue input0; Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({M, K}), input0_backing.data(), OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator), input0); NameMLValMap feeds; feeds.insert(std::make_pair(std::string("A"), input0)); std::vector fetches; ASSERT_STATUS_OK(session_object.Run(RunOptions{}, feeds, AsSpan({std::string("Y")}), &fetches)); ASSERT_EQ(fetches.size(), 1u); ASSERT_TRUE(fetches[0].IsTensor()); const auto& output_tensor = fetches[0].Get(); ASSERT_EQ(output_tensor.Shape(), TensorShape({M, N})); const auto* output_data = output_tensor.Data(); for (int64_t i = 0; i < N; ++i) { ASSERT_TRUE(std::isfinite(output_data[i])) << "Output " << i << " should not include padded A tail values."; ASSERT_EQ(output_data[i], expected_vals[i]) << "Output " << i; } } } TEST(MathOpTest, MatMulFloatType_FastMath) { // TODO: Unskip when fixed #41968513 if (DefaultDmlExecutionProvider().get() != nullptr) { GTEST_SKIP() << "Skipping because of the following error: Assertion failed: m_bufferTensorDesc.TotalTensorSizeInBytes >= ComputeByteSizeFromDimensions(nonBroadcastDimensions, dataType)"; } RunMatMulTest(7, false, false, false); } TEST(MathOpTest, MatMulFloatTypeInitializer_FastMath) { // TODO: Unskip when fixed #41968513 if (DefaultDmlExecutionProvider().get() != nullptr) { GTEST_SKIP() << "Skipping because of the following error: Assertion failed: m_bufferTensorDesc.TotalTensorSizeInBytes >= ComputeByteSizeFromDimensions(nonBroadcastDimensions, dataType)"; } RunMatMulTest(7, false, true, false); } TEST(MathOpTest, FusedMatMulNonUnitAlpha_FastMathEnabled) { constexpr int64_t batch_size = 2; constexpr int64_t m = 32; constexpr int64_t n = 32; constexpr int64_t k = 64; constexpr float alpha = 0.125f; OpTester test("FusedMatMul", 1, kMSDomain); test.AddInput("A", {1, batch_size, m, k}, std::vector(batch_size * m * k, 1.0f)); test.AddInput("B", {1, batch_size, k, n}, std::vector(batch_size * k * n, 1.0f)); test.AddAttribute("transA", static_cast(0)); test.AddAttribute("transB", static_cast(0)); test.AddAttribute("transBatchA", static_cast(0)); test.AddAttribute("transBatchB", static_cast(0)); test.AddAttribute("alpha", alpha); test.AddOutput("Y", {1, batch_size, m, n}, std::vector(batch_size * m * n, alpha * k)); // SBGEMM does not implement general alpha scaling. Enabling fastmath must // preserve non-unit alpha by selecting the accurate FP32 path. SessionOptions so; ASSERT_STATUS_OK(so.config_options.AddConfigEntry( kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16, "1")); std::vector> execution_providers; execution_providers.push_back(DefaultCpuExecutionProvider()); test.Config(so).ConfigEps(std::move(execution_providers)).RunWithConfig(); } TEST(MathOpTest, MatMulInt32Type_FastMath) { RunMatMulTest(9); } TEST(MathOpTest, MatMulUint32Type_FastMath) { RunMatMulTest(9); } TEST(MathOpTest, MatMulInt64Type_FastMath) { RunMatMulTest(9); } TEST(MathOpTest, MatMulUint64Type_FastMath) { RunMatMulTest(9); } #ifndef ENABLE_TRAINING // Prepacking is disabled in full training build so no need to test the feature in a training build. TEST(MathOpTest, MatMulSharedPrepackedWeights_FastMath) { OpTester test("MatMul"); std::vector b_init_values(32, 1.0f); test.AddInput("A", {8, 4}, {1.0f, 2.0f, 3.0f, 4.0f, -1.0f, -2.0f, -3.0f, -4.0f, 1.0f, 2.0f, 3.0f, 4.0f, -1.0f, -2.0f, -3.0f, -4.0f, 1.0f, 2.0f, 3.0f, 4.0f, -1.0f, -2.0f, -3.0f, -4.0f, 1.0f, 2.0f, 3.0f, 4.0f, -1.0f, -2.0f, -3.0f, -4.0f}); // B is to be an initializer for triggering pre-packing test.AddInput("B", {4, 8}, b_init_values, true); test.AddOutput("Y", {8, 8}, {10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f}); OrtValue b; Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({4, 8}), b_init_values.data(), OrtMemoryInfo(CPU, OrtAllocatorType::OrtDeviceAllocator), b); SessionOptions so; // Set up B as a shared initializer to be shared between sessions ASSERT_EQ(so.AddInitializer("B", &b), Status::OK()); ASSERT_STATUS_OK(so.config_options.AddConfigEntry( kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16, "1")); // 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 // Dummy run to disable the FastMath mode for the current session TEST(MathOpTest, MatMulUint64Type_DisableFastMath) { RunMatMulTest(9, false, false, true); } } // namespace test } // namespace onnxruntime #endif // MLAS_SBGEMM_AVAILABLE