Skip to content

Add input validation to LinearClassifier#29060

Merged
yuslepukhin merged 5 commits into
mainfrom
yuslepukhin/linearclassifier_109934
Jun 17, 2026
Merged

Add input validation to LinearClassifier#29060
yuslepukhin merged 5 commits into
mainfrom
yuslepukhin/linearclassifier_109934

Conversation

@yuslepukhin

Copy link
Copy Markdown
Contributor

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_strings or classlabels_ints is 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 SafeInt for 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.

…ts, and input shape

- Validate classlabels size >= class_count to prevent OOB read in multi-class path
- Reject models specifying both classlabels_strings and classlabels_ints
- Validate coefficients size is a multiple of class_count at construction
- Restrict input to 1-D or 2-D per ONNX spec (reject 3-D+)
- Use SafeInt for end_scores pointer arithmetic
- Add unit tests for all new validation paths

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens LinearClassifier validation logic (constructor-time attribute checks and runtime input shape checks) to better align operator configuration with expected constraints, and expands unit tests to cover new failure modes and edge cases.

Changes:

  • Added construction-time validation for mutually exclusive class label attributes and for coefficient array shape consistency.
  • Tightened runtime input shape validation to require 1-D or 2-D inputs, and adjusted tests accordingly (including new 1-D/3-D input cases).
  • Introduced SafeInt usage in score pointer arithmetic to reduce overflow risk.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/core/providers/cpu/ml/linearclassifier.cc Adds constructor-time attribute validation, improves input-rank error handling, and hardens score-pointer arithmetic.
onnxruntime/test/providers/cpu/ml/linearclassifer_test.cc Expands/adjusts unit tests to cover new validation paths and updated error messages (with exception guards).

Comment thread onnxruntime/core/providers/cpu/ml/linearclassifier.cc
Comment thread onnxruntime/test/providers/cpu/ml/linearclassifer_test.cc
@yuslepukhin
yuslepukhin force-pushed the yuslepukhin/linearclassifier_109934 branch 2 times, most recently from fddb520 to 69edca5 Compare June 15, 2026 23:44
@yuslepukhin
yuslepukhin force-pushed the yuslepukhin/linearclassifier_109934 branch from b1eb7ce to 494c42e Compare June 16, 2026 00:49
xadupre
xadupre previously approved these changes Jun 16, 2026
Comment thread onnxruntime/core/providers/cpu/ml/linearclassifier.cc
Both-empty classlabels is only safe for binary classification (class_count==1)
which has a default 0/1 fallback. The multi-class path unconditionally indexes
the classlabels vector, so empty labels with class_count > 1 causes an OOB read.

Add constructor-time validation and test coverage.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread onnxruntime/core/providers/cpu/ml/linearclassifier.cc
For binary (class_count==1), the compute path only uses labels when the
array has exactly 2 elements. Any other non-empty size was silently ignored.
Reject misconfigured models at construction time.

@neilmsft neilmsft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@yuslepukhin
yuslepukhin merged commit 9d64146 into main Jun 17, 2026
87 of 90 checks passed
@yuslepukhin
yuslepukhin deleted the yuslepukhin/linearclassifier_109934 branch June 17, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants