Commit 329504f
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 b5a5398 commit 329504f
2 files changed
Lines changed: 33 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
126 | 132 | | |
127 | 133 | | |
128 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| 284 | + | |
| 285 | + | |
284 | 286 | | |
285 | 287 | | |
286 | 288 | | |
| |||
295 | 297 | | |
296 | 298 | | |
297 | 299 | | |
298 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
299 | 304 | | |
300 | 305 | | |
301 | 306 | | |
302 | 307 | | |
303 | 308 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
315 | 314 | | |
316 | 315 | | |
317 | 316 | | |
| |||
413 | 412 | | |
414 | 413 | | |
415 | 414 | | |
| 415 | + | |
| 416 | + | |
416 | 417 | | |
417 | 418 | | |
418 | 419 | | |
| |||
427 | 428 | | |
428 | 429 | | |
429 | 430 | | |
430 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
431 | 437 | | |
432 | 438 | | |
433 | 439 | | |
434 | 440 | | |
435 | | - | |
436 | | - | |
437 | 441 | | |
438 | 442 | | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
450 | 449 | | |
451 | 450 | | |
452 | 451 | | |
| |||
465 | 464 | | |
466 | 465 | | |
467 | 466 | | |
| 467 | + | |
| 468 | + | |
468 | 469 | | |
469 | 470 | | |
470 | 471 | | |
| |||
0 commit comments