Repository navigation
Description
Summary
An encoder built on Conv1dSubsampling* / Conv2dSubsampling* returns a different olens for the same utterance depending on which other utterances share its batch. The padding mask is subsampled by slicing relative to the padded width of the batch, rather than as a function of each utterance's own length.
I ran into this while adding batched beam search (#6599): decoding a minibatch cannot reproduce --batch_size 1 because the encoder itself disagrees about how long the encoder output is. Filing it separately because it is pre-existing and independent of that PR.
Reproduction
No data or trained model needed:
import torch
from espnet2.asr.encoder.transformer_encoder import TransformerEncoder
torch.manual_seed(0)
enc = TransformerEncoder(
input_size=8, output_size=4, attention_heads=2, linear_units=4,
num_blocks=1, input_layer="conv1d2",
).eval()
short, long_ = 288, 363
feats = torch.randn(2, long_, 8)
with torch.no_grad():
_, olens_alone, _ = enc(feats[1:2, :short], torch.tensor([short]))
_, olens_batch, _ = enc(feats, torch.tensor([long_, short]))
_, olens_pair, _ = enc(feats[:, :short], torch.tensor([short, short]))
print(olens_alone.tolist(), olens_batch.tolist(), olens_pair.tolist())feats_lengths = 288 alone -> [142]
feats_lengths = [363, 288] batched together -> [180, 144]
feats_lengths = [288, 288] batched together -> [142, 142]
The 288-frame utterance is 142 frames long on its own and 144 frames long next to a 363-frame one.
Why
Conv1dSubsampling2.forward (subsampling.py#L249):
x = self.conv(x) # Conv1d(k=3,s=1) then Conv1d(k=3,s=2)
...
x_mask = x_mask[:, :, :-2:1][:, :, :-2:2]The two slices mirror the two convolutions, but they trim from the end of the tensor, and the tensor is as wide as the longest utterance in the batch. make_pad_mask builds the mask at width max(ilens), so what gets trimmed depends on the batch:
| mask width | after [:-2:1] |
after [:-2:2] |
True count for the 288-frame row |
|
|---|---|---|---|---|
| alone | 288 | 286 | 142 | 142 — the last two real frames are trimmed away |
| next to a 363-frame utterance | 363 | 361 | 180 | 144 — only padding is trimmed |
So olens is not a function of ilens alone. For the shortest utterance in a batch the trim lands on padding; for an utterance decoded alone it lands on real frames.
Note which side is "wrong": the extra two frames in the batched case are real audio that the single-utterance path discards. Batching is arguably the more correct of the two.
The same pattern appears in every subsampling class in that file: Conv1dSubsampling1/2/3, Conv2dSubsampling, Conv2dSubsampling1/2/6/8.
Consequences
- Inference. With
--batch_size > 1(now possible for the attention/CTC beam search via Decode a batch of utterances in one beam search #6599) an utterance decodes differently than it would on its own, and differently again depending on how the batch is bucketed. Not a large difference in practice, but not reproducible either. - Training. The same utterance contributes a different number of encoder frames depending on its batch, so the CTC loss denominator and the attention context change with batch composition. Bucketing by length (
batch_type: sorted/folded) makes this mostly consistent, which is probably why it has gone unnoticed. - CTC alignment / segmentation tools that batch will inherit the same drift.
What a fix would involve
Making the mask trim per-utterance, e.g. deriving olens from ilens with the same formula the convolutions use — for Conv1dSubsampling2, ((ilens - 2) - 2) // 2 + 1 — instead of slicing the padded mask.
This is not a free change:
- It alters the encoder output length of existing models, so decoding results move for every recipe using a conv subsampling front-end. Published results and released models would need re-checking.
- The per-utterance formula agrees with today's behaviour for the longest utterance in a batch, and disagrees by one or two frames for shorter ones, so the effect on any given recipe depends on its batching.
Given that, I did not touch it in #6599 — I documented the consequence there instead. Happy to prepare the fix if you think the correction is worth the churn, or to leave it documented if not.
Environment
espnet master (ec5632b16), torch 2.13.0, CPU.
Activity
Address the second CodeRabbit review
modelpath-dev commentedon Sep 4, 2026
I'll work on this. Could you assign it to me?
I'll pin a regression test (or stabilize the flake) and keep the change small.
sw005320 commentedon Sep 4, 2026
Oh, cool! Yes, could you make a PR, then!
modelpath-dev commentedon Sep 4, 2026
PR is up: #6633
olensnow comes from each utterance's own length, so the 288-frame case in this issue is 142 whether it sits alone or next to 363. The longest row is unchanged. The snippet from here is pinned in the test.modelpath-dev commentedon Sep 5, 2026
Let me know if there are any changes needed in the PR.
sw005320 commentedon Sep 7, 2026
Thanks!
I'm checking it.
modelpath-dev commentedon Sep 9, 2026
Great, thank you! Let me know if you have any feedback or if there's anything else I can do.
sw005320 commentedon Sep 9, 2026
@modelpath-dev, could you deal with the review comments in #6633?
Fix Conv*Subsampling mask so encoder olens depends only on ilens