|
4 | 4 | #include "core/util/math.h" |
5 | 5 | #include "gtest/gtest.h" |
6 | 6 | #include "test/providers/provider_test_utils.h" |
| 7 | +#include "test/unittest_util/op_tester.h" // Ensure this include is present |
7 | 8 |
|
8 | 9 | namespace onnxruntime { |
9 | 10 | namespace test { |
@@ -178,5 +179,95 @@ TEST(AffineGridTest, test_3d_7) { |
178 | 179 | test.SetOutputTolerance(0.0001f); |
179 | 180 | test.Run(); |
180 | 181 | } |
| 182 | + |
| 183 | +// Validation tests for input shape checks |
| 184 | + |
| 185 | +// Test: theta must be a 3D tensor |
| 186 | +TEST(AffineGridTest, invalid_theta_not_3d) { |
| 187 | + OpTester test("AffineGrid", 20); |
| 188 | + // theta is 2D instead of 3D |
| 189 | + test.AddInput<float>("theta", {2, 6}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}); |
| 190 | + test.AddInput<int64_t>("size", {4}, {2, 1, 2, 3}); |
| 191 | + test.AddOutput<float>("grid", {2, 2, 3, 2}, std::vector<float>(24, 0.0f)); |
| 192 | + test.Run(OpTester::ExpectResult::kExpectFailure, "AffineGrid : Input theta tensor dimension is not 3"); |
| 193 | +} |
| 194 | + |
| 195 | +// Test: size length must be 4 or 5 |
| 196 | +TEST(AffineGridTest, invalid_size_length_3) { |
| 197 | + OpTester test("AffineGrid", 20); |
| 198 | + test.AddInput<float>("theta", {1, 2, 3}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}); |
| 199 | + test.AddInput<int64_t>("size", {3}, {1, 1, 2}); |
| 200 | + test.AddOutput<float>("grid", {1, 2, 2}, std::vector<float>(4, 0.0f)); |
| 201 | + test.Run(OpTester::ExpectResult::kExpectFailure, "Length of input 'size' is 3. It must be 4 for 2D or 5 for 5D"); |
| 202 | +} |
| 203 | + |
| 204 | +// Test: size length must be 4 or 5 (too long) |
| 205 | +TEST(AffineGridTest, invalid_size_length_6) { |
| 206 | + OpTester test("AffineGrid", 20); |
| 207 | + test.AddInput<float>("theta", {1, 2, 3}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}); |
| 208 | + test.AddInput<int64_t>("size", {6}, {1, 1, 2, 3, 4, 5}); |
| 209 | + test.AddOutput<float>("grid", {1, 2, 3, 2}, std::vector<float>(12, 0.0f)); |
| 210 | + test.Run(OpTester::ExpectResult::kExpectFailure, "Length of input 'size' is 6. It must be 4 for 2D or 5 for 5D"); |
| 211 | +} |
| 212 | + |
| 213 | +// Test: 2D - batch dimension mismatch between theta and size |
| 214 | +TEST(AffineGridTest, invalid_2d_batch_mismatch) { |
| 215 | + OpTester test("AffineGrid", 20); |
| 216 | + // theta has N=1, but size has N=2 |
| 217 | + test.AddInput<float>("theta", {1, 2, 3}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}); |
| 218 | + test.AddInput<int64_t>("size", {4}, {2, 1, 2, 3}); |
| 219 | + test.AddOutput<float>("grid", {2, 2, 3, 2}, std::vector<float>(24, 0.0f)); |
| 220 | + test.Run(OpTester::ExpectResult::kExpectFailure, "must equal theta batch dimension"); |
| 221 | +} |
| 222 | + |
| 223 | +// Test: 2D - theta shape must be [N, 2, 3], wrong second dimension |
| 224 | +TEST(AffineGridTest, invalid_2d_theta_wrong_dim1) { |
| 225 | + OpTester test("AffineGrid", 20); |
| 226 | + // theta is [1, 3, 3] but for 2D it must be [N, 2, 3] |
| 227 | + test.AddInput<float>("theta", {1, 3, 3}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); |
| 228 | + test.AddInput<int64_t>("size", {4}, {1, 1, 2, 3}); |
| 229 | + test.AddOutput<float>("grid", {1, 2, 3, 2}, std::vector<float>(12, 0.0f)); |
| 230 | + test.Run(OpTester::ExpectResult::kExpectFailure, "theta shape must be [N, 2, 3] for 2D"); |
| 231 | +} |
| 232 | + |
| 233 | +// Test: 2D - theta shape must be [N, 2, 3], wrong third dimension |
| 234 | +TEST(AffineGridTest, invalid_2d_theta_wrong_dim2) { |
| 235 | + OpTester test("AffineGrid", 20); |
| 236 | + // theta is [1, 2, 4] but for 2D it must be [N, 2, 3] |
| 237 | + test.AddInput<float>("theta", {1, 2, 4}, {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}); |
| 238 | + test.AddInput<int64_t>("size", {4}, {1, 1, 2, 3}); |
| 239 | + test.AddOutput<float>("grid", {1, 2, 3, 2}, std::vector<float>(12, 0.0f)); |
| 240 | + test.Run(OpTester::ExpectResult::kExpectFailure, "theta shape must be [N, 2, 3] for 2D"); |
| 241 | +} |
| 242 | + |
| 243 | +// Test: 3D - batch dimension mismatch between theta and size |
| 244 | +TEST(AffineGridTest, invalid_3d_batch_mismatch) { |
| 245 | + OpTester test("AffineGrid", 20); |
| 246 | + // theta has N=1, but size has N=2 |
| 247 | + test.AddInput<float>("theta", {1, 3, 4}, {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}); |
| 248 | + test.AddInput<int64_t>("size", {5}, {2, 1, 2, 2, 3}); |
| 249 | + test.AddOutput<float>("grid", {2, 2, 2, 3, 3}, std::vector<float>(72, 0.0f)); |
| 250 | + test.Run(OpTester::ExpectResult::kExpectFailure, "must equal theta batch dimension"); |
| 251 | +} |
| 252 | + |
| 253 | +// Test: 3D - theta shape must be [N, 3, 4], wrong second dimension |
| 254 | +TEST(AffineGridTest, invalid_3d_theta_wrong_dim1) { |
| 255 | + OpTester test("AffineGrid", 20); |
| 256 | + // theta is [1, 2, 4] but for 3D it must be [N, 3, 4] |
| 257 | + test.AddInput<float>("theta", {1, 2, 4}, {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}); |
| 258 | + test.AddInput<int64_t>("size", {5}, {1, 1, 2, 2, 3}); |
| 259 | + test.AddOutput<float>("grid", {1, 2, 2, 3, 3}, std::vector<float>(36, 0.0f)); |
| 260 | + test.Run(OpTester::ExpectResult::kExpectFailure, "theta shape must be [N, 3, 4] for 3D"); |
| 261 | +} |
| 262 | + |
| 263 | +// Test: 3D - theta shape must be [N, 3, 4], wrong third dimension |
| 264 | +TEST(AffineGridTest, invalid_3d_theta_wrong_dim2) { |
| 265 | + OpTester test("AffineGrid", 20); |
| 266 | + // theta is [1, 3, 3] but for 3D it must be [N, 3, 4] |
| 267 | + test.AddInput<float>("theta", {1, 3, 3}, {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}); |
| 268 | + test.AddInput<int64_t>("size", {5}, {1, 1, 2, 2, 3}); |
| 269 | + test.AddOutput<float>("grid", {1, 2, 2, 3, 3}, std::vector<float>(36, 0.0f)); |
| 270 | + test.Run(OpTester::ExpectResult::kExpectFailure, "theta shape must be [N, 3, 4] for 3D"); |
| 271 | +} |
181 | 272 | } // namespace test |
182 | 273 | } // namespace onnxruntime |
0 commit comments