Repository navigation
Conversation
Afloat16
marked this pull request as ready for review
November 4, 2026 16:22
Contributor
Author
|
The workflows for the current head ( |
Member
|
Hello! This fixes a reasonable padding issue when using a Transformer followed by a CNN, though that module is not normally used. I've simplified the mask expression and moved the tests into the existing CNN test file. All 14 tests pass, so this looks good to merge.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CNN applies its convolution to padded token embeddings even when an attention mask is supplied. Padding activations produced by an encoder need not be zero, so kernels crossing a sequence boundary mix padding into valid token outputs. The same sentence can receive different pooled embeddings depending on the amount of batch padding.
Change
Zero masked token embeddings before convolution. Use an out-of-place mask so the caller's input tensor remains unchanged. Inputs without an attention mask retain their existing behavior.
Validation
OMP_NUM_THREADS=1 python -m pytest tests/sentence_transformer/modules/test_cnn_padding.py tests/sentence_transformer/modules/test_cnn.py -q— 14 passed on CPU with PyTorch 2.15.0.The new cases compare a cropped sentence with its left/right padded form through CNN and mean Pooling, using single and multiple kernels and both large finite and NaN padding values. They also check zero padding gradients, unchanged input tensors and the unmasked path. Against the original code, 10 new cases fail and three controls pass. Ruff and
git diff --checkpass.