Skip to content

Add validation and unit tests AffineGrid#27572

Open
yuslepukhin wants to merge 4 commits intomainfrom
yuslepukhin/affine_grid_validate
Open

Add validation and unit tests AffineGrid#27572
yuslepukhin wants to merge 4 commits intomainfrom
yuslepukhin/affine_grid_validate

Conversation

@yuslepukhin
Copy link
Member

Description

This pull request improves the robustness and correctness of the AffineGrid operator in ONNX Runtime by adding stricter input validation and updating the implementation for better type safety. It also introduces comprehensive unit tests to verify error handling for invalid inputs.

Input validation enhancements

  • Added explicit checks in affine_grid.cc to ensure that the batch dimension in size matches the batch dimension in theta, and that theta has the correct shape for both 2D ([N, 2, 3]) and 3D ([N, 3, 4]) cases. This prevents silent errors due to shape mismatches. [1] [2]

Type safety and code clarity improvements

  • Updated grid generation functions to use the template type T for Eigen vector creation, replacing hardcoded float types. This ensures correct behavior for different data types and improves code clarity. [1] [2]
  • Added clarifying comments to matrix and array initializations in affine grid generator functions to make the code easier to understand and maintain.

Test coverage improvements

  • Added a series of unit tests in affine_grid_test.cc to validate input shape checks, including cases for incorrect tensor dimensions, mismatched batch sizes, and invalid size lengths. These tests ensure that the operator fails gracefully with informative error messages when given invalid inputs.
  • Ensured the required test utility header is included to support new tests.

Code cleanup

  • Removed unused includes from affine_grid.cc to tidy up the codebase.

@yuslepukhin yuslepukhin requested a review from Copilot March 5, 2026 23:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves the robustness of the CPU AffineGrid kernel by adding stricter shape validation and expanding unit tests to verify failures on invalid inputs.

Changes:

  • Added explicit validation for theta/size batch consistency and expected theta shapes for 2D/3D cases.
  • Improved type safety in base-grid generation by using templated Eigen vector types rather than hardcoded float.
  • Added negative unit tests covering invalid dimensions, invalid size lengths, batch mismatches, and invalid theta shapes.

Reviewed changes

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

File Description
onnxruntime/core/providers/cpu/tensor/affine_grid.cc Adds stricter input validation and improves type correctness in grid generation.
onnxruntime/test/providers/cpu/tensor/affine_grid_test.cc Adds unit tests to ensure invalid inputs fail with expected error messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

yuslepukhin and others added 3 commits March 5, 2026 15:26
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

2 participants