Commit be36d59
authored
[tt-train] Add causal mask support for SDPA backward (#36661)
### Ticket
#34531
### Problem description
The SDPA backward pass (sdpa_bw_q and sdpa_bw_kv) required materializing
a full S × S triangular attention mask for causal attention, which
involved:
- Precomputing the mask tensor on the host
- Transferring the mask to DRAM
- Reading unique mask tiles from DRAM for each Q/K position during
backward computation
- This introduces an O(S²) memory dependency and unnecessary DRAM
traffic, becoming a bottleneck for long-sequence training. The forward
pass already supports on-the-fly causal mask generation.
### What's changed
This PR extends on-the-fly causal mask generation to the SDPA backward
pass:
- The writer kernel generates a triangular mask tile once during
initialization using generate_causal_mask_tile()
- The compute kernel reuses the same mask tile for all diagonal
positions
- Masked-out regions are skipped entirely:
- sdpa_bw_q: K/V rows beyond the causal boundary are not processed
- sdpa_bw_kv: Q rows before the causal boundary are not processed
- Updated apply_mask_on_reg() with a template parameter to control CB
pop behavior, enabling mask tile reuse for causal mode
### Impact
- Removes the O(S²) memory dependency for causal masking in the SDPA
backward pass
- Reduces DRAM bandwidth usage by eliminating mask tensor reads
- Avoids unnecessary computation for masked regions
- Enables end-to-end causal attention training without mask tensors
Avoiding unnecessary computation in masked regions provides a
significant performance improvement.
Baseline (composite SDPA, memory_efficient):
- Mean step time: 2640.16 ms
(Without fused SDPA we can’t fit TinyLlama (1B) on a single Wormhole
card.)
Fused SDPA kernel:
- Arbitrary mask, memory_efficient: 3082.58 ms
- Causal mask, memory_efficient: 2506.47 ms
- Causal mask, default: 1983.56 ms
### Checklist
- [x] [](https://github.com/tenstorrent/tt-metal/actions/runs/21550483959)
- [x] New/Existing tests provide coverage for changes1 parent 6d954eb commit be36d59
File tree
18 files changed
+354
-66
lines changed- tt-train
- sources/ttml/metal/ops/sdpa_bw
- device
- kernels
- compute
- dataflow
- tests/ops
18 files changed
+354
-66
lines changedLines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| |||
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | | - | |
66 | | - | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| |||
Lines changed: 55 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
| 83 | + | |
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
| |||
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
101 | 106 | | |
102 | 107 | | |
103 | 108 | | |
104 | 109 | | |
105 | 110 | | |
106 | 111 | | |
107 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
108 | 118 | | |
109 | 119 | | |
110 | 120 | | |
111 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
112 | 136 | | |
113 | 137 | | |
114 | 138 | | |
115 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
116 | 142 | | |
117 | 143 | | |
118 | 144 | | |
| |||
134 | 160 | | |
135 | 161 | | |
136 | 162 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
142 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
143 | 184 | | |
144 | 185 | | |
145 | 186 | | |
| |||
151 | 192 | | |
152 | 193 | | |
153 | 194 | | |
| 195 | + | |
154 | 196 | | |
155 | 197 | | |
156 | 198 | | |
157 | 199 | | |
158 | 200 | | |
159 | 201 | | |
160 | 202 | | |
161 | | - | |
| 203 | + | |
162 | 204 | | |
163 | 205 | | |
164 | 206 | | |
| |||
185 | 227 | | |
186 | 228 | | |
187 | 229 | | |
188 | | - | |
| 230 | + | |
189 | 231 | | |
190 | 232 | | |
191 | 233 | | |
| |||
208 | 250 | | |
209 | 251 | | |
210 | 252 | | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
211 | 258 | | |
212 | 259 | | |
213 | 260 | | |
Lines changed: 51 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
73 | 74 | | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
88 | 93 | | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
94 | 104 | | |
95 | 105 | | |
96 | 106 | | |
| |||
101 | 111 | | |
102 | 112 | | |
103 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
104 | 126 | | |
105 | | - | |
| 127 | + | |
106 | 128 | | |
107 | 129 | | |
108 | 130 | | |
| |||
120 | 142 | | |
121 | 143 | | |
122 | 144 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
128 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
129 | 166 | | |
130 | 167 | | |
131 | 168 | | |
| |||
161 | 198 | | |
162 | 199 | | |
163 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
164 | 204 | | |
165 | 205 | | |
166 | 206 | | |
| |||
173 | 213 | | |
174 | 214 | | |
175 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
176 | 221 | | |
177 | 222 | | |
178 | 223 | | |
Lines changed: 27 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| 63 | + | |
61 | 64 | | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
84 | 102 | | |
85 | | - | |
86 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
87 | 106 | | |
88 | 107 | | |
89 | 108 | | |
90 | 109 | | |
91 | 110 | | |
92 | 111 | | |
93 | 112 | | |
94 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
95 | 116 | | |
96 | 117 | | |
97 | 118 | | |
| 119 | + | |
98 | 120 | | |
99 | 121 | | |
100 | 122 | | |
| 123 | + | |
| 124 | + | |
101 | 125 | | |
102 | 126 | | |
103 | 127 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
27 | 33 | | |
28 | 34 | | |
29 | 35 | | |
| |||
0 commit comments