Skip to content

Commit 5e6153d

Browse files
committed
Add regression test for SAME_UPPER stride>1 padding (#26734)
Add test case Conv2D_AutoPad_SAME_UPPER_Stride2 that verifies correct asymmetric padding calculation when auto_pad=SAME_UPPER and stride>1. This catches the off-by-one padding bug fixed in this PR.
1 parent c70b342 commit 5e6153d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

onnxruntime/test/providers/cpu/nn/conv_op_test.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,49 @@ TEST(ConvTest, Conv2D_AutoPad1) {
502502
TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true);
503503
}
504504

505+
// Regression test for issue #26734: SAME_UPPER with stride > 1
506+
// Tests asymmetric padding calculation that was incorrect in WebGPU EP
507+
TEST(ConvTest, Conv2D_AutoPad_SAME_UPPER_Stride2) {
508+
ConvOpAndTestAttributes attrs = {
509+
"SAME_UPPER", // auto_pad
510+
vector<int64_t>{1, 1}, // dilations
511+
1, // group
512+
vector<int64_t>{3, 3}, // kernel_shape
513+
{}, // pads
514+
vector<int64_t>{2, 2}, // strides > 1 triggers asymmetric padding
515+
{} // excluded EPs
516+
};
517+
518+
// 1x1x4x4 input
519+
vector<float> X = {1.0f, 2.0f, 3.0f, 4.0f,
520+
5.0f, 6.0f, 7.0f, 8.0f,
521+
9.0f, 10.0f, 11.0f, 12.0f,
522+
13.0f, 14.0f, 15.0f, 16.0f};
523+
vector<int64_t> X_shape = {1, 1, 4, 4};
524+
525+
// 3x3 kernel of all 1s for easy verification
526+
vector<float> W = {1.0f, 1.0f, 1.0f,
527+
1.0f, 1.0f, 1.0f,
528+
1.0f, 1.0f, 1.0f};
529+
vector<int64_t> W_shape = {1, 1, 3, 3};
530+
531+
// Output: 2x2 (ceil(4/2) = 2)
532+
// SAME_UPPER with total_pad=1: pad_head=0, pad_tail=1
533+
vector<int64_t> Y_shape = {1, 1, 2, 2};
534+
535+
// Expected values:
536+
// (0,0): 1+2+3+5+6+7+9+10+11 = 54
537+
// (0,1): 3+4+0+7+8+0+11+12+0 = 45
538+
// (1,0): 9+10+11+13+14+15+0+0+0 = 72
539+
// (1,1): 11+12+0+15+16+0+0+0+0 = 54
540+
auto expected_vals = {54.0f, 45.0f, 72.0f, 54.0f};
541+
542+
TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape);
543+
544+
// NNAPI/CoreML EP requires weight to be an initializer
545+
TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true);
546+
}
547+
505548
TEST(ConvTest, Conv2D_AutoPad2) {
506549
ConvOpAndTestAttributes attrs = {
507550
"SAME_LOWER", // auto_pad

0 commit comments

Comments
 (0)