Commit d0dcd31
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 d5b7860 commit d0dcd31
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 | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
273 | 275 | | |
274 | 276 | | |
275 | 277 | | |
| |||
284 | 286 | | |
285 | 287 | | |
286 | 288 | | |
287 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
288 | 293 | | |
289 | 294 | | |
290 | 295 | | |
291 | 296 | | |
292 | 297 | | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
304 | 303 | | |
305 | 304 | | |
306 | 305 | | |
| |||
402 | 401 | | |
403 | 402 | | |
404 | 403 | | |
| 404 | + | |
| 405 | + | |
405 | 406 | | |
406 | 407 | | |
407 | 408 | | |
| |||
416 | 417 | | |
417 | 418 | | |
418 | 419 | | |
419 | | - | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
420 | 426 | | |
421 | 427 | | |
422 | 428 | | |
423 | 429 | | |
424 | | - | |
425 | | - | |
426 | 430 | | |
427 | 431 | | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
439 | 438 | | |
440 | 439 | | |
441 | 440 | | |
| |||
454 | 453 | | |
455 | 454 | | |
456 | 455 | | |
| 456 | + | |
| 457 | + | |
457 | 458 | | |
458 | 459 | | |
459 | 460 | | |
| |||
0 commit comments