diff --git a/onnxruntime/core/providers/cpu/tensor/pad.cc b/onnxruntime/core/providers/cpu/tensor/pad.cc index f996bac55e..72a1f5e4ea 100644 --- a/onnxruntime/core/providers/cpu/tensor/pad.cc +++ b/onnxruntime/core/providers/cpu/tensor/pad.cc @@ -223,10 +223,10 @@ Status PadCpuImpl(OpKernelContext* ctx, std::vector input_extents; // Calculate output dimensions, and handle any negative padding - input_starts.reserve(2 * new_dims_count); - input_extents.reserve(2 * new_dims_count); + input_starts.reserve(new_dims_count); + input_extents.reserve(new_dims_count); for (size_t i = 0; i < new_dims_count; i++) { - input_starts.push_back(reshaped_slice[i]); + input_starts.push_back(-1 * reshaped_slice[i]); input_extents.push_back(reshaped_input_dims[i] + reshaped_slice[i] + reshaped_slice[i + new_dims_count]); reshaped_output_dims[i] += reshaped_pad[i] + reshaped_pad[i + new_dims_count] + reshaped_slice[i] + reshaped_slice[i + new_dims_count]; } diff --git a/onnxruntime/test/providers/cpu/tensor/pad_test.cc b/onnxruntime/test/providers/cpu/tensor/pad_test.cc index 8ca80b0488..656aafa69d 100644 --- a/onnxruntime/test/providers/cpu/tensor/pad_test.cc +++ b/onnxruntime/test/providers/cpu/tensor/pad_test.cc @@ -154,7 +154,7 @@ TEST(TensorOpTest, Pad_Constant_2D) { 1234.0f, 1234.0f, 1234.0f, 1234.0f, 1234.0f, 1234.0f}); } -TEST(TensorOpTest, Pad_Constant_2D_negative) { +TEST(TensorOpTest, Pad_Constant_2D_negative_pads_1) { RunAllOpsetAllDomainPadTests({2, 3}, {11.0f, 21.0f, 31.0f, 12.0f, 22.0f, 32.0f}, @@ -167,6 +167,54 @@ TEST(TensorOpTest, Pad_Constant_2D_negative) { 1234.0f, 1234.0f, 1234.0f, 1234.0f}); } +TEST(TensorOpTest, Pad_Constant_2D_negative_pads_2) { + RunAllOpsetAllDomainPadTests({2, 3}, + {11.0f, 21.0f, 31.0f, + 12.0f, 22.0f, 32.0f}, + {-1, 0, 0, 0}, + 1234.f, + {1, 3}, + {12.0f, 22.0f, 32.0f}); +} + +TEST(TensorOpTest, Pad_Constant_3D_negative_pads) { + RunAllOpsetAllDomainPadTests({1, 1, 3}, + {0.f, 1.0f, 2.f}, + {0, 0, -1, 0, 0, -1}, + 0.f, + {1, 1, 1}, + {1.f}); +} + +TEST(TensorOpTest, Pad_Constant_4D_negative_pads) { + // input_vals contains values from 0 to 99 (inclusive) + std::vector input_vals; + input_vals.reserve(100); + for (int i = 0; i < 100; ++i) { + input_vals.push_back(static_cast(i)); + } + + // holder for output_vals (expected) + std::vector output_vals; + output_vals.reserve(21); + + float seed = 13; + for (int i = 0; i < 7; ++i) { + for (int j = 0; j < 3; ++j) { + output_vals.push_back(static_cast(seed + j)); + } + seed += 10; + } + + // run tests + RunAllOpsetAllDomainPadTests({1, 1, 10, 10}, + input_vals, + {0, 0, -1, -3, 0, 0, -2, -4}, + 0.f, + {1, 1, 7, 3}, + output_vals); +} + TEST(TensorOpTest, Pad_3D_complex) { RunAllOpsetAllDomainPadTests({2, 2, 2}, {111.0f, 112.0f,