Commit 145e0c9
committed
[ET-VK][sdpa] Reuse shared V cache across GQA query heads in AV coop-GEMV
Pull Request resolved: #21063
The LLM decode AV coop-GEMV reloads the shared V cache once per query head. In
grouped-query attention Hq = G * Hkv query heads share each KV head (Llama G=4,
Phi G=3, Qwen G=2), and out[q_h, d] = sum_c attn[c, q_h] * V[c, kv_h, d] reads
the SAME V texel for every query head in a group. The per-query-head coop shader
gives each of the Hq heads its own workgroup, so V -- the dominant traffic
(head_dim-wide per context texel, vs a scalar attn weight) -- is read G times.
This adds a GQA-reuse AV variant that assigns ONE workgroup per (d4, kv_h): it
loads each V texel once and reuses it across all G query heads in the group,
producing G output texels. For this bandwidth-bound kernel that cuts V-cache
traffic ~Gx.
Implementation:
- The variant is a codegen flag (`GQA`) on the existing
`sdpa_compute_out_coop.glsl` template, not a separate file: one shared header
plus two `#ifdef GQA` `main()`s (per-head and GQA-reuse), so the shared setup
lives in one place while each algorithm reads end-to-end. It emits the shader
`sdpa_compute_out_gqa_coop`.
- Reduction reuses the per-head coop shader's shared-memory tree reduction (no
subgroup arithmetic), so the variant runs on any Vulkan device -- Adreno and
Mali alike -- with no capability gate.
- Each thread holds G output accumulators; the array is sized to a compile-time
`MAX_GROUP_SIZE` = 8 and the group loop is bounded by the `group_size` =
Hq/Hkv spec constant, so the driver fully unrolls it at pipeline creation.
- Dispatch (`pick_sdpa_av_shader` + global-wg picker + spec-const wiring in
`add_sdpa_compute_out_node`): the GQA variant is selected on the LLM decode
coop path when Hq > Hkv, evenly divisible, and G <= 8 (`use_gqa_av_coop`); it
sets `group_size` and changes the global workgroup z-dim from Hq to Hkv.
Everything else -- MHA (Hq == Hkv), groups exceeding the cap (G > 8, e.g. MQA
with Hq > 8), and non-divisible shapes -- falls back to the unchanged per-head
`sdpa_compute_out_coop`. (Low-ratio MQA -- Hkv == 1 with Hq <= 8 -- is
eligible and takes the GQA path.)
- A test-only `gqa_override` knob is threaded through `add_sdpa_compute_out_node`
(declared in the new `SDPA.h`): -1 auto-select, 0 force per-head, 1 force GQA,
so a benchmark can exercise both AV shaders on the same shape; forcing GQA is
VK_CHECK'd against shape eligibility.
ghstack-source-id: 405400503
@exported-using-ghexport
Differential Revision: [D112906311](https://our.internmc.facebook.com/intern/diff/D112906311/)1 parent 5dfd37f commit 145e0c9
7 files changed
Lines changed: 547 additions & 66 deletions
File tree
- backends/vulkan
- runtime/graph/ops
- glsl
- impl
- test
- custom_ops
- impl
- op_tests
Lines changed: 185 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
35 | 45 | | |
36 | 46 | | |
37 | 47 | | |
| |||
49 | 59 | | |
50 | 60 | | |
51 | 61 | | |
| 62 | + | |
| 63 | + | |
52 | 64 | | |
53 | 65 | | |
54 | 66 | | |
| |||
57 | 69 | | |
58 | 70 | | |
59 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
60 | 243 | | |
61 | 244 | | |
62 | 245 | | |
| |||
197 | 380 | | |
198 | 381 | | |
199 | 382 | | |
| 383 | + | |
| 384 | + | |
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
| 28 | + | |
0 commit comments