Repository navigation
Commit 33ed3c5
Read the query length at build time where it is known (pytorch#22494)
Follow-up polish on the causal slicing added in pytorch#22443. No behaviour
change for any shape that worked before.
### Read the query length at build time where it is known
The slice that shortens the keys for a causal call always emitted a node
to read the query length at run time. For a decode step that length is a
constant known when the program is built, so the node was pure overhead
on exactly the path this slicing exists to speed up.
There is already a helper for this, used by the cached attention handler
a few hundred lines down in the same file: it returns a literal for a
concrete dimension and emits a size node only for a symbolic one.
| Call | Before | After |
| --- | --- | --- |
| query 1 against 32 keys, static | size node, 2336 bytes | no size
node, **2288 bytes** |
| query 6 against 32 keys, static | size node, 2336 bytes | no size
node, **2288 bytes** |
| dynamic query length | size node | size node, unchanged |
### Two comments promised less than the code delivered
The guard that declines causal attention also declines a query length
that **cannot be compared** at build time, two unrelated dynamic
dimensions for instance, and the comment above it described only the
case where the query is genuinely longer. It now says both, because the
second is the case a reader is more likely to meet.
### A half-wired test knob
The key length knob added to the attention test reached the query, key
and value but not either mask branch, so a case combining a mask with an
unequal key length built a mask of the wrong width. No shipped case does
that today, so nothing was failing, but the next person to use it would
have hit it.
### Test plan
Causal attention at every shape the operator suite covers, run against
eager on an Apple Silicon Mac: query 1, 2 and 6 against longer keys,
equal lengths, non-causal, float32 and bfloat16. All match to 4.8e-07 or
exactly, 8 of 8 cases.
A dynamic query length bounded by the keys keeps its size node, exports,
and matches eager to 1.2e-07.
Building the test mask both ways in eager torch: with the key length it
runs, and with the query length it raises the size mismatch this
removes.
Exported the speech model: unchanged, 8 fused attention nodes and no
size nodes.
Ran the neighbouring backend test files, all passing.
Co-authored-by: PyTorch Bot <pytorchbot@users.noreply.github.com>1 parent 902fcf5 commit 33ed3c5
2 files changed
Lines changed: 14 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
583 | 584 | | |
584 | 585 | | |
585 | 586 | | |
586 | | - | |
587 | | - | |
588 | | - | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
589 | 592 | | |
590 | 593 | | |
591 | 594 | | |
| |||
720 | 723 | | |
721 | 724 | | |
722 | 725 | | |
723 | | - | |
724 | | - | |
725 | | - | |
726 | | - | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
727 | 729 | | |
728 | 730 | | |
729 | 731 | | |
| |||
732 | 734 | | |
733 | 735 | | |
734 | 736 | | |
735 | | - | |
| 737 | + | |
736 | 738 | | |
737 | 739 | | |
738 | 740 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6270 | 6270 | | |
6271 | 6271 | | |
6272 | 6272 | | |
6273 | | - | |
6274 | | - | |
| 6273 | + | |
| 6274 | + | |
6275 | 6275 | | |
6276 | 6276 | | |
6277 | 6277 | | |
6278 | 6278 | | |
6279 | 6279 | | |
6280 | | - | |
| 6280 | + | |
6281 | 6281 | | |
6282 | | - | |
| 6282 | + | |
6283 | 6283 | | |
6284 | 6284 | | |
6285 | 6285 | | |
| |||
0 commit comments