Fix #4205 (Fix padding leakage in Conformer convolution module)#4206
Fix #4205 (Fix padding leakage in Conformer convolution module)#4206proshian wants to merge 1 commit into
Conversation
_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.
🔗 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. |
|
Hi @proshian! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Problem
_ConvolutionModulewithinConformerignores 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. Ineval()with correctlengthsand defaults, this shifts the output by up to0.28(1.23withuse_group_norm=True) against a mean magnitude of0.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- whenTrue, padded time steps are zeroed right before the depthwise convolution, using the same padding mask (derived fromlengths) 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.Nonekeeps the existinguse_group_normselection; passing an explicitconv_norm_typetogether withuse_group_norm=TrueraisesValueError.For fully padding-invariant behavior use
conv_mask_padding=True, conv_norm_type="layer_norm". With these flags the repro's output difference drops from0.28to2e-6(float32 precision).Backward compatibility
use_group_norm); no existing code or checkpoint is affected.state_dictlayout is unchanged: the norm stays atsequential.3and no parameters are added or renamed, so checkpoints trained before this PR load withstrict=True, including into models that enable the new flags.How other implementations handle padding
Masking before the depthwise conv and adding a
layer_normoption 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- withconv_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=Truetogether withconv_norm_typeraisesValueError.All conformer unit tests pass (
python -m pytest test/torchaudio_unittest/models/conformer/conformer_cpu_test.py→ 6 passed).