Commit c58b736
committed
[perf]: address hao-ai-lab#1388 bot review — mark lse non-diff + reuse indices in backward
Two improvements, both from the gemini-code-assist + Copilot reviews on hao-ai-lab#1388:
(a) Mark `softmax_lse` non-differentiable in all 3 custom-op setup_contexts
(default + masked + varlen_qk). We return lse alongside out so it can be
saved for backward; nobody should differentiate through it. `del grad_lse`
in backward silently drops grads if a caller wires lse into a loss —
`ctx.mark_non_differentiable(lse)` makes autograd error loudly instead.
The public `*_compilable` dispatchers already drop lse (`out, _ = op(...)`),
so this only matters for callers using `torch.ops.fastvideo._*_forward`
directly, but it's free defensive hygiene.
(b) Stop re-running `unpad_input` for tensors that share a mask. The first
`unpad_input(mask)` call returns `indices` + `cu_seqlens` + `max_s`;
subsequent unpads for out/dout/lse on the same mask just re-derived
the same indices (and the `.max().item()` call inside each unpad does
a GPU→CPU sync). Replace those with `tensor.flatten(0,1)[indices].view(...)`,
which is what `unpad_input` does internally anyway:
- flash_attn_no_pad backward: 4 unpad_input calls -> 1
- flash_attn_varlen_qk_no_pad backward: 6 unpad_input calls -> 2
(one per distinct mask; v reuses k_indices, out/dout/lse reuse
q_indices, the final repad of dk/dv reuses k_indices instead of
recomputing).
Semantics are preserved — `unpad_input(x, mask)[0]` is equivalent to
`x.flatten(0,1)[indices]` for the same mask. The existing 28 unit tests
(inference parity atol=0 rtol=0, training-backward parity, opcheck with
and without grad inputs, all on FA2 + A100) cover the regressions.1 parent 76b6ff6 commit c58b736
2 files changed
Lines changed: 33 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
96 | 102 | | |
97 | 103 | | |
98 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| 266 | + | |
| 267 | + | |
266 | 268 | | |
267 | 269 | | |
268 | 270 | | |
| |||
277 | 279 | | |
278 | 280 | | |
279 | 281 | | |
280 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
281 | 286 | | |
282 | 287 | | |
283 | 288 | | |
284 | 289 | | |
285 | 290 | | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
297 | 296 | | |
298 | 297 | | |
299 | 298 | | |
| |||
395 | 394 | | |
396 | 395 | | |
397 | 396 | | |
| 397 | + | |
| 398 | + | |
398 | 399 | | |
399 | 400 | | |
400 | 401 | | |
| |||
409 | 410 | | |
410 | 411 | | |
411 | 412 | | |
412 | | - | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
413 | 419 | | |
414 | 420 | | |
415 | 421 | | |
416 | 422 | | |
417 | | - | |
418 | | - | |
419 | 423 | | |
420 | 424 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
432 | 431 | | |
433 | 432 | | |
434 | 433 | | |
| |||
447 | 446 | | |
448 | 447 | | |
449 | 448 | | |
| 449 | + | |
| 450 | + | |
450 | 451 | | |
451 | 452 | | |
452 | 453 | | |
| |||
0 commit comments