// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include #include "gtest/gtest.h" #include "test/providers/provider_test_utils.h" namespace onnxruntime { namespace test { struct LinearRegressorParam { const char* post_transform; std::vector expected_value; int64_t targets; LinearRegressorParam(const char* post_transform1, const std::vector& expected_value1, int targets1) : post_transform(post_transform1), expected_value(expected_value1), targets(targets1) {} }; class LinearRegressorTest : public testing::TestWithParam {}; /* TEST MODEL TRAINING from sklearn import linear_model X = [[0., 0.5], [1., 1.5], [2., 2.9], [3., 13.3]] Z = [[41.], [32.], [23.], [14.]] model = linear_model.LinearRegression() r3 =model.fit(X, Z) r4 =model.predict ([[1, 0.],[3.,44.],[23.,11.3]]) r3.coef_ array([-9.00000000e+00, -1.99600736e-16]) r3.intercept_ array([4.10000000e+01]) r4 array([[32.],[14.],[-166.]]) */ /* TEST MODEL TRAINING from sklearn import linear_model X = [[0., 0.5], [1., 1.5], [2., 2.9], [3., 13.3]] Z = [[0., 41.], [1., 32.], [2., 23.], [3., 14.]] model = linear_model.LinearRegression() r3 =model.fit(X, Z) r4 =model.predict ([[1, 0.],[3.,44.],[23.,11.3]]) r3.coef_ array([[ 1.00000000e+00, -2.49500920e-17], [ -9.00000000e+00, -1.99600736e-16]]) r3.intercept_ array([ 2.22044605e-16, 4.10000000e+01]) r4 array([[ 1., 32.],[ 3., 14.],[ 23., -166.]]) */ TEST_P(LinearRegressorTest, LinearRegressorUniTarget) { const LinearRegressorParam& param = GetParam(); OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); std::vector coefficients, intercepts; if (param.targets == 1) { coefficients = {-9.00000000f, -1.99600736e-16f}; intercepts = {41.0000000f}; } else { coefficients = {1.00000000f, -2.49500920e-17f, -9.00000000f, -1.99600736e-16f}; intercepts = {2.22044605e-16f, 41.0000000f}; } test.AddAttribute("intercepts", intercepts); test.AddAttribute("coefficients", coefficients); if (strcmp(param.post_transform, "NONE") != 0) { test.AddAttribute("post_transform", param.post_transform); } test.AddAttribute("targets", param.targets); test.AddInput("X", {3, 2}, {1.f, 0.f, 3.f, 44.f, 23.f, 11.3f}); test.AddOutput("Y", {static_cast(param.expected_value.size() / param.targets), param.targets}, param.expected_value); test.Run(); } TEST(MLOpTest, LinearRegressorInvalidCoefficientsSize) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("coefficients", std::vector{1.f, 2.f}); test.AddAttribute("intercepts", std::vector{0.f, 0.f}); test.AddAttribute("targets", static_cast(2)); test.AddInput("X", {1, 2}, {1.f, 2.f}); test.AddOutput("Y", {1, 2}, {0.f, 0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "coefficients length"); } // For PROBIT, all the output values are NaN. INSTANTIATE_TEST_SUITE_P( LinearRegressorTest, LinearRegressorTest, testing::Values(LinearRegressorParam("NONE", {32.0f, 14.0f, -166.0f}, 1), LinearRegressorParam("SOFTMAX", {32.0f, 14.0f, -166.0f}, 1), LinearRegressorParam("LOGISTIC", {32.0f, 14.0f, -166.0f}, 1), LinearRegressorParam("SOFTMAX_ZERO", {32.0f, 14.0f, -166.0f}, 1), LinearRegressorParam("NONE", {1.0f, 32.0f, 3.0f, 14.0f, 23.0f, -166.0f}, 2), LinearRegressorParam("SOFTMAX", {3.442477e-14f, 1.f, 1.670142e-05f, 1.f, 1.0f, 0.f}, 2), LinearRegressorParam("LOGISTIC", {0.731058f, 1.0f, 0.9525741f, 1.f, 1.0f, 0.f}, 2), LinearRegressorParam("SOFTMAX_ZERO", {3.442477e-14f, 1.f, 1.670142e-05f, 1.f, 1.0f, 0.f}, 2) )); TEST(MLOpTest, LinearRegressorUndersizedCoefficients) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f}); // needs 2 test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {1, 2}, {1.f, 2.f}); test.AddOutput("Y", {1, 1}, {0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "LinearRegressor: coefficients length (1) must be at least targets (1) * features (2)"); } TEST(MLOpTest, LinearRegressorExtraCoefficientsAreIgnored) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f, 2.f, 99.f, 100.f}); test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {1, 2}, {3.f, 4.f}); test.AddOutput("Y", {1, 1}, {11.f}); test.Run(); } TEST(MLOpTest, LinearRegressorInvalidTargets) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(0)); test.AddAttribute("coefficients", std::vector{1.f}); test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {1, 1}, {1.f}); test.AddOutput("Y", {1, 1}, {0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "targets must be in range [1,"); } // 1D input: treated as single batch with num_features = shape.Size() TEST(MLOpTest, LinearRegressor1DInput) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); // coefficients for 1 target, 3 features: [1, 2, 3] // input: [1, 2, 3] -> dot product = 1*1 + 2*2 + 3*3 = 14, + intercept 1 = 15 test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f, 2.f, 3.f}); test.AddAttribute("intercepts", std::vector{1.f}); test.AddInput("X", {3}, {1.f, 2.f, 3.f}); test.AddOutput("Y", {1, 1}, {15.f}); test.Run(); } // 1D input with undersized coefficients should fail validation TEST(MLOpTest, LinearRegressor1DInputUndersizedCoefficients) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f}); // needs 3 test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {3}, {1.f, 2.f, 3.f}); test.AddOutput("Y", {1, 1}, {0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "LinearRegressor: coefficients length (1) must be at least targets (1) * features (3)"); } // Zero batches: input shape {0, 2} should produce output shape {0, targets} TEST(MLOpTest, LinearRegressorZeroBatches) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f, 2.f}); test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {0, 2}, {}); test.AddOutput("Y", {0, 1}, {}); test.Run(); } // No intercepts provided: the GEMM path without bias TEST(MLOpTest, LinearRegressorNoIntercepts) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); // coefficients for 1 target, 2 features: [2, 3] // input: [1, 4] -> 2*1 + 3*4 = 14 test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{2.f, 3.f}); // No intercepts attribute added test.AddInput("X", {1, 2}, {1.f, 4.f}); test.AddOutput("Y", {1, 1}, {14.f}); test.Run(); } // Mismatched intercepts size: intercepts are silently ignored (not equal to num_targets) TEST(MLOpTest, LinearRegressorMismatchedInterceptsIgnored) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); // 2 targets, but intercepts has 1 element -> intercepts ignored // coefficients: target0=[1,0], target1=[0,1] // input: [3, 7] -> target0=3, target1=7 (no intercept added) test.AddAttribute("targets", static_cast(2)); test.AddAttribute("coefficients", std::vector{1.f, 0.f, 0.f, 1.f}); test.AddAttribute("intercepts", std::vector{100.f}); // wrong size, ignored test.AddInput("X", {1, 2}, {3.f, 7.f}); test.AddOutput("Y", {1, 2}, {3.f, 7.f}); test.Run(); } // Overflow: num_targets * num_features overflows size_t TEST(MLOpTest, LinearRegressorCoefficientsOverflow) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); // Use a targets value that is valid per constructor checks (<= ptrdiff_t::max) but // will overflow when multiplied by num_features (3) after casting to size_t. constexpr int64_t large_targets = static_cast(std::numeric_limits::max()); test.AddAttribute("targets", large_targets); test.AddAttribute("coefficients", std::vector{1.f, 2.f, 3.f}); test.AddInput("X", {1, 3}, {1.f, 2.f, 3.f}); test.AddOutput("Y", {1, 1}, {0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "overflows"); } // 3D input is rejected TEST(MLOpTest, LinearRegressorInvalidInputDims) { OpTester test("LinearRegressor", 1, onnxruntime::kMLDomain); test.AddAttribute("targets", static_cast(1)); test.AddAttribute("coefficients", std::vector{1.f, 2.f}); test.AddAttribute("intercepts", std::vector{0.f}); test.AddInput("X", {1, 1, 2}, {1.f, 2.f}); test.AddOutput("Y", {1, 1}, {0.f}); test.Run(OpTester::ExpectResult::kExpectFailure, "more than 2 dimension"); } } // namespace test } // namespace onnxruntime