Skip to content

Fix #4205 (Fix padding leakage in Conformer convolution module)#4206

Open
proshian wants to merge 1 commit into
pytorch:mainfrom
proshian:fix-conformer
Open

Fix #4205 (Fix padding leakage in Conformer convolution module)#4206
proshian wants to merge 1 commit into
pytorch:mainfrom
proshian:fix-conformer

Conversation

@proshian

@proshian proshian commented Jul 12, 2026

Copy link
Copy Markdown

Problem

_ConvolutionModule within Conformer ignores the padding mask, so a sequence's valid-frame output depends on the padding appended to it: the depthwise conv mixes in padded activations, and BatchNorm/GroupNorm pool padded steps. In eval() with correct lengths and defaults, this shifts the output by up to 0.28 (1.23 with use_group_norm=True) against a mean magnitude of 0.8. That is enough to collapse quality when moving from batched to single-sequence inference.

Full mechanism, minimal repro, and measurements are in #4205.

Changes

Two new opt-in arguments on Conformer / ConformerLayer (threaded down to _ConvolutionModule):

  • conv_mask_padding: bool = False - when True, padded time steps are zeroed right before the depthwise convolution, using the same padding mask (derived from lengths) that attention already uses. This stops padding from bleeding into valid frames.
  • conv_norm_type: Optional[Literal["batch_norm", "group_norm", "layer_norm"]] = None - explicit choice of the normalization after the depthwise conv. "layer_norm" (new) is padding-invariant, since it never pools statistics across time. None keeps the existing use_group_norm selection; passing an explicit conv_norm_type together with use_group_norm=True raises ValueError.

For fully padding-invariant behavior use conv_mask_padding=True, conv_norm_type="layer_norm". With these flags the repro's output difference drops from 0.28 to 2e-6 (float32 precision).

Backward compatibility

  • Defaults reproduce the previous behavior bit-exactly (masking off, norm chosen by use_group_norm); no existing code or checkpoint is affected.
  • state_dict layout is unchanged: the norm stays at sequential.3 and no parameters are added or renamed, so checkpoints trained before this PR load with strict=True, including into models that enable the new flags.

How other implementations handle padding

Masking before the depthwise conv and adding a layer_norm option follows the standard treatment in production Conformer implementations (NeMo and WeNet - links and code in #4205).

Tests

Added to test/torchaudio_unittest/models/conformer/conformer_test_impl.py:

  • test_padding_invariance - with conv_mask_padding=True, conv_norm_type="layer_norm", a batched padded forward matches per-example forwards on all valid frames.
  • test_invalid_norm_configs - use_group_norm=True together with conv_norm_type raises ValueError.

All conformer unit tests pass (python -m pytest test/torchaudio_unittest/models/conformer/conformer_cpu_test.py → 6 passed).

_ConvolutionModule ignored the padding mask, so a sequence's valid-frame
output depended on the padding appended to it: the depthwise conv mixed in
padded activations and BatchNorm/GroupNorm pooled padded steps.

Fix it via two opt-in arguments (off by default for backward compatibility):
- conv_mask_padding: zero padded frames before the depthwise conv, using the
  padding mask derived from lengths.
- conv_norm_type: select batch_norm/group_norm/layer_norm; layer_norm is
  padding-invariant. Mutually exclusive with use_group_norm.
@proshian
proshian requested a review from a team as a code owner July 12, 2026 18:50
@pytorch-bot

pytorch-bot Bot commented Jul 12, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/audio/4206

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Jul 12, 2026

Copy link
Copy Markdown

Hi @proshian!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jul 12, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Jul 12, 2026
@proshian proshian changed the title Fix padding leakage in Conformer convolution module Fix #4205 (Fix padding leakage in Conformer convolution module) Jul 12, 2026
@proshian proshian changed the title Fix #4205 (Fix padding leakage in Conformer convolution module) Fix #4205 Jul 12, 2026
@proshian proshian changed the title Fix #4205 Fix #4205 (Fix padding leakage in Conformer convolution module) Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant