diff --git a/tmva/sofie/inc/TMVA/ROperator_Conv.hxx b/tmva/sofie/inc/TMVA/ROperator_Conv.hxx index b4a2bb547c3bd..fcd41e427015b 100644 --- a/tmva/sofie/inc/TMVA/ROperator_Conv.hxx +++ b/tmva/sofie/inc/TMVA/ROperator_Conv.hxx @@ -198,8 +198,8 @@ public: } } else { // general case (stride not 1) int64_t v = pad - kernel; - std::string outStr = "((" + inputDim.param + "+" + std::to_string(v) + ")/" - + std::to_string(stride) + "1)"; + std::string outStr = + "((" + inputDim.param + "+" + std::to_string(v) + ")/" + std::to_string(stride) + "+1)"; return Dim{ outStr, static_cast(-1)}; } } diff --git a/tmva/sofie/test/TestCustomModelsFromONNX.cxx b/tmva/sofie/test/TestCustomModelsFromONNX.cxx index 94993f601a3c4..3c79390afc4d7 100644 --- a/tmva/sofie/test/TestCustomModelsFromONNX.cxx +++ b/tmva/sofie/test/TestCustomModelsFromONNX.cxx @@ -706,6 +706,29 @@ TEST(ONNX, ConvWithStridesNoPadding) } +TEST(ONNX, ConvWithDynShapeStride) +{ + // Conv1d with dynamic spatial dimension W and stride=2. + // Verifies fix for output dimension formula: ((W+pad-kernel)/stride+1) + // was incorrectly generated as ((W+pad-kernel)/stride1) before the fix. + // + // Model: kernel=3, stride=2, pad=0, weight=all-ones, input shape (1,1,W) + // With W=7: output shape (1,1,3), output = [0+1+2, 2+3+4, 4+5+6] = [3, 9, 15] + + std::vector input = {0, 1, 2, 3, 4, 5, 6}; // shape (1,1,7) + std::vector correct_output = {3, 9, 15}; + + // model is dynamic in spatial dim W, use W = 7 + ASSERT_INCLUDE_AND_RUN_SESSION_ARGS(std::vector, "ConvWithDynShapeStride", + "\"ConvWithDynShapeStride_FromONNX.dat\", 7", 7, input); + + EXPECT_EQ(output.size(), correct_output.size()); + for (size_t i = 0; i < output.size(); ++i) { + EXPECT_LE(std::abs(output[i] - correct_output[i]), DEFAULT_TOLERANCE); + } +} + + // Disables test (asymmetric padding not supported) TEST(DISABLED_ONNX, ConvWithAsymmetricPadding) { diff --git a/tmva/sofie/test/input_models/ConvWithDynShapeStride.onnx b/tmva/sofie/test/input_models/ConvWithDynShapeStride.onnx new file mode 100644 index 0000000000000..69864d933ef01 Binary files /dev/null and b/tmva/sofie/test/input_models/ConvWithDynShapeStride.onnx differ