Sliding window layers rebuild a dense (T, T) attention mask with a Python loop on every training forward pass
In CausalSelfAttention.forward (litgpt/model.py), when there is no KV cache (input_pos is None, which covers training, evaluation and any full forward pass) and the layer uses sliding window attention, the mask is built like this:
if input_pos is None:
if mask is None:
mask = torch.ones(T, T, dtype=q.dtype, device=q.device).triu(diagonal=1)
mask.masked_fill_(mask.bool(), float("-inf"))
mask = mask.view(1, 1, *mask.shape)
sliding_window_mask = torch.full((T, T), float("-inf"), dtype=q.dtype, device=q.device)
for i in range(T):
window_start = max(0, i - self.config.sliding_window_size + 1)
sliding_window_mask[i, window_start : i + 1] = 0.0
sliding_window_mask = sliding_window_mask.view(1, 1, T, T)
mask = sliding_window_maskThere are three problems:
- The causal mask built in the
if mask is Noneblock is overwritten right away. - The window mask is filled one row at a time:
Tslice assignments (each one a small kernel launch) per sliding window layer per forward pass. Gemma-3-4b-it has 29 sliding window layers, so at T=2048 that is about 59,000 slice assignments per forward pass. Undertorch.compile, the loop is also unrolled into the graph and specializes it onT. - When
T <= sliding_window_size, the window covers every causal position, so the result is just a causal mask. Passing it explicitly still keeps SDPA off its flash kernel.
The loop came in with #2153 (commit 3023ca8), which fixed the sliding window KV cache. Before that commit, this mask was built with triu/tril and no loop. The PR description of #2153 does not mention the no-cache path.
Point 3 affects more configs than it seems. Phi-3-mini-128k-instruct, Phi-4-mini-instruct and Phi-4-mini-reasoning have sliding_window_size=262145, which is larger than their block_size of 131072, so the window never masks anything. They still pay for the loop and the masked kernel in all 32 layers. Mistral-7B-v0.1 has sliding_window_size == block_size == 4096, so it is in the same situation.
Configs with sliding_window_size set (from litgpt/config.py at 18c931c):
| configs | window | sliding layers | attention path |
|---|---|---|---|
| Gemma-2-2b, -9b, -27b (and -it) | 4096 | 13/26, 21/42, 23/46 | softcapping (eager) |
| Gemma-3-1b-it | 512 | 22/26 | SDPA |
| Gemma-3-4b-it, -12b-it, -27b-it | 1024 | 29/34, 40/48, 52/62 | SDPA |
| Phi-3-mini-4k-instruct | 2048 | 32/32 | SDPA |
| Phi-3-mini-128k-instruct, Phi-4-mini-instruct, Phi-4-mini-reasoning | 262145 (> block_size) | 32/32 | SDPA |
| Mistral-7B-v0.1, Mistral-7B-Instruct-v0.1 | 4096 (= block_size) | 32/32 | SDPA |
| Mathstral-7B-v0.1 | 4096 | 32/32 | SDPA |
On an RTX 5090 (bf16, torch 2.14.1), one loop-built mask at T=2048 with window 1024 took 19-28 ms across two runs. The loop is bound by CPU-side kernel launches, so the exact figure depends on CPU load. A triu/tril construction of the same mask takes under 0.1 ms. End to end, a LoRA forward+backward step for Gemma-3-4b-it at T=2048 takes 0.954 s on main and 0.454 s with the mask built by triu/tril. With every layer forced to global attention it takes 0.308 s.
Proposed fix: build the same mask with triu/tril again, and when T <= sliding_window_size skip it and use the default causal path. The KV-cache path would stay as it is. I have a small PR with tests and benchmarks ready if this sounds good.
Source: Lightning-AI/litgpt