@@ -166,5 +166,47 @@ TEST(MLOpTest, LinearClassifierMulticlassInt32Input) {
166166TEST (MLOpTest, LinearClassifierMulticlassDoubleInput) {
167167 LinearClassifierMulticlass<double >();
168168}
169+
170+ // Regression test: coefficients size doesn't match class_count * num_features.
171+ TEST (MLOpTest, LinearClassifierInvalidCoefficientsSizeFails) {
172+ OpTester test (" LinearClassifier" , 1 , onnxruntime::kMLDomain );
173+
174+ // 3 intercepts => class_count = 3, input has 2 features => expects 6 coefficients.
175+ std::vector<float > coefficients = {-0 .22562418f , 0 .34188559f , 0 .68346153f };
176+ std::vector<int64_t > classes = {1 , 2 , 3 };
177+ std::vector<float > intercepts = {-3 .91601811f , 0 .42575697f , 0 .13731251f };
178+
179+ test.AddAttribute (" coefficients" , coefficients);
180+ test.AddAttribute (" intercepts" , intercepts);
181+ test.AddAttribute (" classlabels_ints" , classes);
182+
183+ test.AddInput <float >(" X" , {1 , 2 }, {1 .f , 0 .f });
184+ test.AddOutput <int64_t >(" Y" , {1 }, {0LL });
185+ test.AddOutput <float >(" Z" , {1 , 3 }, {0 .f , 0 .f , 0 .f });
186+
187+ test.Run (OpTester::ExpectResult::kExpectFailure ,
188+ " LinearClassifier: coefficients length (3) is less than classes (3) * features (2)" );
189+ }
190+
191+ // Regression test: coefficients not divisible by class_count.
192+ TEST (MLOpTest, LinearClassifierCoefficientsSizeNotDivisibleByClassCountFails) {
193+ OpTester test (" LinearClassifier" , 1 , onnxruntime::kMLDomain );
194+
195+ // 3 intercepts => class_count = 3, but 5 coefficients is not divisible by 3.
196+ std::vector<float > coefficients = {1 .f , 2 .f , 3 .f , 4 .f , 5 .f };
197+ std::vector<int64_t > classes = {1 , 2 , 3 };
198+ std::vector<float > intercepts = {0 .1f , 0 .2f , 0 .3f };
199+
200+ test.AddAttribute (" coefficients" , coefficients);
201+ test.AddAttribute (" intercepts" , intercepts);
202+ test.AddAttribute (" classlabels_ints" , classes);
203+
204+ test.AddInput <float >(" X" , {1 , 2 }, {1 .f , 0 .f });
205+ test.AddOutput <int64_t >(" Y" , {1 }, {0LL });
206+ test.AddOutput <float >(" Z" , {1 , 3 }, {0 .f , 0 .f , 0 .f });
207+
208+ test.Run (OpTester::ExpectResult::kExpectFailure ,
209+ " coefficients size (5) must be a multiple of the number of classes (3)" );
210+ }
169211} // namespace test
170212} // namespace onnxruntime
0 commit comments