MoonshineStreaming: sliding_window_mask_function off-by-one — right_window_size=4 attends to 3 future frames instead of 4
Author: Kush0610
Pull request: no
Labels: name: bug

### System Info

- transformers version: 5.9.0
- Platform: Linux-6.8.0-100-generic-x86_64-with-glibc2.39
- Python version: 3.12.3
- PyTorch version: 2.12.0


### Who can help?

@eustlb 

### Information

`sliding_window_mask_function` in `modeling_moonshine_streaming.py` (line 349) uses strict less-than (`< right_window_size`) for the right context, which means `right_window_size=4` only attends to 3 future frames instead of 4.

The Moonshine v2 paper (arXiv:2602.12241) explicitly states:

`"a right window of w_right=4 implies an algorithmic lookahead of 4×20 ms = 80 ms"`

At 50 Hz, 4 future frames = 80 ms. But the current code gives 3 future frames = 60 ms.

- [ ] The official example scripts
- [x] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [x] My own task or dataset (give details below)

### Reproduction

from transformers.models.moonshine_streaming.modeling_moonshine_streaming import sliding_window_mask_function

fn = sliding_window_mask_function((16, 4))

q = 10
future = [kv for kv in range(q + 1, 20) if fn(0, 0, q, kv)]
print(f"Future frames attended: {future}")
Actual:   [11, 12, 13]     — 3 frames, 60ms
Expected: [11, 12, 13, 14] — 4 frames, 80ms (per paper)

### Expected behavior

With sliding_window_mask_function((16, 4)), a query at position 10 should attend to future positions {11, 12, 13, 14} — 4 future frames, matching the paper's definition of w_right=4

Comments:
Hi, I'd like to take a stab at fixing this!

I've looked at the code and confirmed the bug. In modular_moonshine_streaming.py (line 272), the right context mask uses a strict less-than:


right_mask = (dist < 0) & (-dist < right_window_size)
With right_window_size=4, this only attends to frames where -dist ∈ {1, 2, 3} — 3 frames instead of 4 — because the boundary value 4 is excluded. The fix is simply changing < to <=:


right_mask = (dist < 0) & (-dist <= right_window_size)
The left mask uses strict < intentionally (the query position itself is included via dist >= 0), so that side is correct. Only the right mask needs the change.

Since modeling_moonshine_streaming.py is auto-generated from the modular file, I'll edit modular_moonshine_streaming.py and run make fix-repo to propagate it, as per the contribution guidelines.

Let me know if you're happy for me to open a PR for this!

Hi @eustlb @keveman , thank you for integrating moonshine streaming into transformers! Could you have a look at this issue and comment whether its a bug if we go by the paper or maybe the models released on HF were trained with a different sliding window? But looking at config for example [here](https://huggingface.co/UsefulSensors/moonshine-streaming-small/blob/main/config.json) it is (16,4) indeed.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.