Skip to content

[tmva][sofie] Account for dilation in Conv2D same padding calculation#21685

Open
harz05 wants to merge 3 commits intoroot-project:masterfrom
harz05:fix/sofie-conv-dilation
Open

[tmva][sofie] Account for dilation in Conv2D same padding calculation#21685
harz05 wants to merge 3 commits intoroot-project:masterfrom
harz05:fix/sofie-conv-dilation

Conversation

@harz05
Copy link
Copy Markdown
Contributor

@harz05 harz05 commented Mar 25, 2026

Changes

The padding='same' calculation in the Keras Conv2D parser was using the raw kernel size instead of the effective kernel size, which for dilated convolutions is dilation * (kernel - 1) + 1. fAttrDilations was extracted from the layer attributes but never factored into the padding formula.

# before
padding_height = max((outputHeight - 1) * strides[0] + kernel_shape[0] - inputHeight, 0)

# after
effective_kH = dilations[0] * (kernel_shape[0] - 1) + 1
padding_height = max((outputHeight - 1) * strides[0] + effective_kH - inputHeight, 0)

For dilation=2, kernel=3, input=8, stride=1 the old formula produced padding=2 while the correct value is 4, causing the generated inference code to operate on tensors with wrong spatial dimensions. With dilation=1 the effective kernel size reduces to the raw kernel size, so existing models are unaffected.

Checklist

  • tested changes locally

This PR fixes #21684

@guitargeek guitargeek changed the title tmva/sofie: account for dilation in Conv2D same padding calculation [tmva][sofie] Account for dilation in Conv2D same padding calculation Mar 25, 2026
Copy link
Copy Markdown
Collaborator

@sanjibansg sanjibansg left a comment

Choose a reason for hiding this comment

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

Hi @harz05,
Thanks for this PR. Can you add a test so that we can verify this patch?

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 26, 2026

Test Results

    21 files      21 suites   2d 21h 12m 42s ⏱️
 3 834 tests  3 833 ✅  1 💤 0 ❌
72 202 runs  72 184 ✅ 18 💤 0 ❌

Results for commit 22e2914.

♻️ This comment has been updated with latest results.

@harz05
Copy link
Copy Markdown
Contributor Author

harz05 commented Mar 27, 2026

Hello @sanjibansg, I tried adding a numerical test with Conv2D(kernel=3, padding='same', dilation_rate=2) on 8×8 input and on testing locally I found that the output shape was correct (8x8x4=256 vs 144 before the fix), confirming the padding calculation works.
However the numerical values don't match Keras. The reason I could figure out is that ROperator_Conv::ShapeInference in ROperator_Conv.hxx overwrites fAttrKernelShape with the dilated kernel size and then Im2col applies dilation again. This is a seperate double dilation bug and a full numerical test might fail due to this and not due to the padding fix.
Should I raise a follow up issue first?

@sanjibansg
Copy link
Copy Markdown
Collaborator

Hello @sanjibansg, I tried adding a numerical test with Conv2D(kernel=3, padding='same', dilation_rate=2) on 8×8 input and on testing locally I found that the output shape was correct (8x8x4=256 vs 144 before the fix), confirming the padding calculation works. However the numerical values don't match Keras. The reason I could figure out is that ROperator_Conv::ShapeInference in ROperator_Conv.hxx overwrites fAttrKernelShape with the dilated kernel size and then Im2col applies dilation again. This is a seperate double dilation bug and a full numerical test might fail due to this and not due to the padding fix. Should I raise a follow up issue first?

Hi @harz05,
Could that be fixed within this PR itself, that should be fine and we can expand its scope.

@harz05 harz05 requested a review from lmoneta as a code owner March 27, 2026 23:41
@harz05
Copy link
Copy Markdown
Contributor Author

harz05 commented Mar 27, 2026

hello @sanjibansg
i've changed Im2col calls in ROperator_Conv.hxx to pass dilation as 1, also added a dilated Conv2d test and corresponding model generator. Pls review it once.
Thank you.

@harz05
Copy link
Copy Markdown
Contributor Author

harz05 commented Mar 29, 2026

There was a clang and ruff formatting issue during the run, I fixed that. CI was also getting Windows x86 failure but i'm not really sure what's the issue in that.
pls review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[tmva][sofie] Keras parser: Conv2D padding='same' ignores dilation_rate, producing wrong padding

4 participants