Repository navigation
Commit 2512896
authored
[ET-VK] Do not return early before the barrier in softmax and var (#22326)
Fixes #22325.
`main()` in `reduce.glsl` bounds-checks the global position and returns
early, but both `reduce_nonpacked_dim()` and `reduce_packed_dim()` call
`barrier()`. Vulkan requires `barrier()` to be reached by every
invocation in the work group under uniform control flow, so an
invocation that returns leaves the remaining ones waiting on a barrier
that can never complete. On a Mali-G76 that hangs the GPU and the submit
fails with `VK_ERROR_DEVICE_LOST`.
The work group is always sized with `ngroups = 4` along `group_dim`, but
`group_dim` is the larger of the two non-reduce dims of the output and
is often smaller than 4. Reducing a 2D tensor along dim 1 gives an
output whose two candidate group dims both have extent 1, so 12 of 16
invocations return early.
This carries the bounds check as a flag instead. Out of bounds
invocations skip the loads and the accumulation, still write their
unused shared memory slot, reach the barrier, and skip the output write.
Their shared memory contents are never read by an in bounds group:
within a work group the bounds check varies only along `group_dim`,
which is exactly `tid.y`, and a group aggregates only its own slots.
### Verification
Samsung Galaxy S10+ (Mali-G76), `torch.sum(x, dim=1, keepdim=True)` over
`(b, 384)`:
| b | before | after |
| --- | --- | --- |
| 1 | device lost | passes, max abs diff 0 |
| 2 | device lost | passes, 1.9e-06 |
| 3 | passes | passes, 3.8e-06 |
| 4 | passes | passes, 2.9e-06 |
| 5 | device lost | passes, 3.8e-06 |
| 8 | passes | passes, 2.9e-06 |
`sentence-transformers/all-MiniLM-L6-v2` ends in `F.normalize(x, p=2,
dim=1)` over a `(1, 384)` tensor and previously lost the device on its
first execution. It now runs with cosine 0.99999720 against the CPU
reference, bit-identical across 10 executions.
No regression on the other models I have on device: the Whisper-tiny
encoder is unchanged at cosine 0.99999702, and selfie segmentation stays
bit-exact at 1.19e-07.
---
## Update: extended to every shader with the same defect
I audited all 26 shaders that call `barrier()` and checked, for each
`return` in `main()`, whether the guard is uniform across the work
group. Three more had the same defect and are now fixed the same way:
| shader | dispatch | why it diverges |
| --- | --- | --- |
| `reduce2d.glsl` | `reduce_gwg`, the same function `reduce.glsl` uses |
4 groups along a `group_dim` whose extent can be 1 |
| `var_texture3d.glsl` | `var_texture_gwg` | same construction,
`lwg_extents[group_dim] = 4u` |
| `softmax.glsl` | `pick_softmax_gwg`, texture path | same construction
|
The remaining barrier-using shaders are correct and are left alone.
Their guards are uniform across the work group, either because they test
`gl_WorkGroupID` (`coopmat_mm`), because they test a global id component
whose local size is 1 (`reduce_per_row_buffer`,
`native_layer_norm_buffer`, `rms_norm_buffer`, `fused_ce`,
`linear_q4gsw_coop`), because the tested dimension is zeroed first
(`softmax_buffer`), or because the varying dimension is not in the guard
(the three `sdpa` shaders). `q4gsw_linear_gemv_coop__w_4x8` returns only
when the whole work group is out of bounds.
One shader is deliberately not touched:
`quantize_and_pack_4h4w_with_group_sums.glsl` guards on
`gl_GlobalInvocationID.x` while dispatching `(4, 1, 16)` or `(2, 1, 32)`
local, so its guard looks divergent ahead of two barriers. It is an int4
quantized path I have no model to exercise, and I would rather not ship
an untested change to it. Flagged in the issue.
### Verification of the three added here
On a Galaxy S26 Ultra (Adreno 840):
- `all-MiniLM-L6-v2`, whose attention exercises `softmax`: unchanged at
cosine 0.99999243 against the CPU reference, 1 distinct output over 20
executions.
- Whisper-tiny encoder: unchanged. It stays nondeterministic on Adreno,
but that is #22327 in the fp32 `linear` path, unrelated to this; its
best replay still reaches cosine 0.999989.
---
## Update: rebased
`reduce.glsl` and `reduce2d.glsl` landed with #22348, so this is now
only `softmax.glsl` and `var_texture3d.glsl`. Rebased on main.
cc @SS-JIA @manuelcandales @digantdesai @cbilgin1 parent 4891009 commit 2512896
2 files changed
Lines changed: 75 additions & 58 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| |||
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
72 | | - | |
73 | 75 | | |
74 | | - | |
75 | | - | |
76 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
77 | 82 | | |
78 | 83 | | |
79 | 84 | | |
| |||
88 | 93 | | |
89 | 94 | | |
90 | 95 | | |
91 | | - | |
| 96 | + | |
| 97 | + | |
92 | 98 | | |
93 | 99 | | |
94 | 100 | | |
| |||
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
123 | | - | |
| 129 | + | |
124 | 130 | | |
125 | 131 | | |
126 | 132 | | |
| |||
129 | 135 | | |
130 | 136 | | |
131 | 137 | | |
132 | | - | |
| 138 | + | |
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
136 | | - | |
| 142 | + | |
137 | 143 | | |
138 | 144 | | |
139 | 145 | | |
| |||
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
156 | | - | |
| 162 | + | |
157 | 163 | | |
158 | 164 | | |
159 | 165 | | |
160 | | - | |
| 166 | + | |
161 | 167 | | |
162 | 168 | | |
163 | 169 | | |
| |||
177 | 183 | | |
178 | 184 | | |
179 | 185 | | |
180 | | - | |
| 186 | + | |
181 | 187 | | |
182 | 188 | | |
183 | 189 | | |
184 | 190 | | |
185 | | - | |
| 191 | + | |
186 | 192 | | |
187 | 193 | | |
188 | 194 | | |
| |||
200 | 206 | | |
201 | 207 | | |
202 | 208 | | |
203 | | - | |
204 | | - | |
205 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
206 | 213 | | |
207 | 214 | | |
208 | | - | |
| 215 | + | |
209 | 216 | | |
210 | | - | |
| 217 | + | |
211 | 218 | | |
212 | 219 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
83 | 85 | | |
84 | 86 | | |
85 | 87 | | |
| |||
89 | 91 | | |
90 | 92 | | |
91 | 93 | | |
92 | | - | |
| 94 | + | |
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
| |||
132 | 134 | | |
133 | 135 | | |
134 | 136 | | |
135 | | - | |
| 137 | + | |
136 | 138 | | |
137 | 139 | | |
138 | 140 | | |
| |||
142 | 144 | | |
143 | 145 | | |
144 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
145 | 152 | | |
146 | 153 | | |
147 | 154 | | |
148 | 155 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
168 | 177 | | |
169 | 178 | | |
170 | 179 | | |
| |||
175 | 184 | | |
176 | 185 | | |
177 | 186 | | |
178 | | - | |
| 187 | + | |
179 | 188 | | |
180 | 189 | | |
181 | 190 | | |
| |||
211 | 220 | | |
212 | 221 | | |
213 | 222 | | |
214 | | - | |
215 | | - | |
216 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
217 | 227 | | |
218 | 228 | | |
219 | | - | |
| 229 | + | |
220 | 230 | | |
221 | | - | |
| 231 | + | |
222 | 232 | | |
223 | 233 | | |
0 commit comments