Add input validation to LinearClassifier
Merged
YU
5mo ago+222+222+012345678901234567890123456789-8-8-0123456789
This pull request adds comprehensive validation checks to the LinearClassifier operator to ensure its configuration and input data are consistent with the ONNX specification. It also significantly expands the unit tests to cover new validation logic and edge cases. The main changes are grouped below:
Validation and Error Handling Improvements
- Enforced that exactly one of
classlabels_stringsorclasslabels_intsis specified, and validated that the number of class labels matches or exceeds the number of classes defined by intercepts. Also, ensured the coefficients array size is a multiple of the number of classes. These checks are performed during construction and provide clear error messages. - Updated input shape validation to require that the input tensor is either 1-D or 2-D, providing a more precise error message if not.
- Used
SafeIntfor pointer arithmetic in score computation to guard against potential overflow.
Unit Test Enhancements
- Added new tests to verify that invalid configurations (such as both class label types specified, too few class labels, or coefficients size mismatches) correctly fail with the expected error messages. These tests are guarded to only run when exceptions are enabled.
- Improved existing tests to check for updated error messages and wrapped some tests with exception guards for platforms without exception support. [1] [2] [3] [4]
Additional Test Coverage
- Added a test to verify that 3-D input tensors are rejected at runtime.
- Added a test to confirm that 1-D input tensors are treated as a single batch with the correct feature count.