Skip to content

Commit 5744fff

Browse files
ssjiaSS-JIA
authored andcommitted
[ET-VK] Add hashed_layout parameter to indexing.glslh texture functions
Pull Request resolved: pytorch#18449 The Adreno GPU driver miscompiles dynamic vector component access patterns (`v[runtime_index]`) in GLSL shaders. This manifests as SIGSEGV crashes on Adreno 740 and silent data corruption on Adreno 750 when accessing vector components using a UBO-derived index (e.g. `tidx.data[meta.packed_dim]`). This diff updates `texture_pos_to_tensor4d_idx_simple`, `tensor4d_idx_to_texel_pos_simple`, and `tensor4d_idx_to_texture_element_idx_simple` to accept a `hashed_layout` parameter (typically a spec constant). The packed_dim is extracted from the hashed layout and used in if/else chains with literal swizzles instead of dynamic vector indexing, which the driver handles correctly. Legacy 2-arg overloads are provided for shaders that don't yet have a hashed_layout spec constant. These delegate to the 3-arg versions using `meta.packed_dim << 16` to construct the layout at runtime. While this doesn't enable compile-time branch elimination, the if/else pattern still avoids the driver bug. Shaders that already have hashed_layout spec constants (arange, full, expand, repeat, pad, block_load, block_store) are updated to use the 3-arg versions. ghstack-source-id: 357266950 @exported-using-ghexport Differential Revision: [D97959286](https://our.internmc.facebook.com/intern/diff/D97959286/)
1 parent 8f1b5ee commit 5744fff

24 files changed

Lines changed: 175 additions & 67 deletions

‎backends/vulkan/runtime/graph/ops/glsl/arange_texture.glsl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ void main() {
3838
return;
3939
}
4040

41-
TensorIndex4D out_tidx = texture_pos_to_tensor4d_idx_simple(outp, out_pos);
41+
TensorIndex4D out_tidx =
42+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
4243

4344
// arange output is 1D, so the W dimension holds the element index.
4445
// Compute the value for each element in the texel along the packed dim.

‎backends/vulkan/runtime/graph/ops/glsl/block_load.glslh‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
const int block_outer_dim) { \
8686
/* Convert tensor index to texture position */ \
8787
/* Use tensor4d_idx_to_texel_pos_simple to properly map the packed dim */ \
88-
ivec3 tex_pos = tensor4d_idx_to_texel_pos_simple(meta, tidx_base); \
88+
ivec3 tex_pos = \
89+
tensor4d_idx_to_texel_pos_simple(meta, tidx_base, hashed_layout); \
8990
const int tex_outer_dim = mod_4(block_outer_dim); \
9091
const int outer_size = meta.sizes[block_outer_dim]; \
9192
const int base_outer_idx = tidx_base.data[block_outer_dim]; \
@@ -160,7 +161,8 @@
160161
/* Convert to 4D for texture operations (textures are always <= 4D) */ \
161162
TensorIndex4D tidx4d; \
162163
tidx4d.data = ivec4(tidx_base.data[0]); \
163-
ivec3 tex_pos = tensor4d_idx_to_texel_pos_simple(meta, tidx4d); \
164+
ivec3 tex_pos = \
165+
tensor4d_idx_to_texel_pos_simple(meta, tidx4d, hashed_layout); \
164166
const int tex_outer_dim = mod_4(block_outer_dim); \
165167
const int outer_size = meta.sizes[block_outer_dim]; \
166168
const int base_outer_idx = tidx4d.data[block_outer_dim]; \

‎backends/vulkan/runtime/graph/ops/glsl/block_store.glslh‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
const mat4 block) { \
8282
/* Convert tensor index to texture position */ \
8383
/* Use tensor4d_idx_to_texel_pos_simple to properly map the packed dim */ \
84-
ivec3 tex_pos = tensor4d_idx_to_texel_pos_simple(meta, tidx_base); \
84+
ivec3 tex_pos = \
85+
tensor4d_idx_to_texel_pos_simple(meta, tidx_base, hashed_layout); \
8586
const int tex_outer_dim = mod_4(block_outer_dim); \
8687
const int outer_size = meta.sizes[block_outer_dim]; \
8788
const int base_outer_idx = tidx_base.data[block_outer_dim]; \
@@ -149,7 +150,8 @@
149150
/* Convert to 4D for texture operations (textures are always <= 4D) */ \
150151
TensorIndex4D tidx4d; \
151152
tidx4d.data = ivec4(tidx_base.data[0]); \
152-
ivec3 tex_pos = tensor4d_idx_to_texel_pos_simple(meta, tidx4d); \
153+
ivec3 tex_pos = \
154+
tensor4d_idx_to_texel_pos_simple(meta, tidx4d, hashed_layout); \
153155
const int tex_outer_dim = mod_4(block_outer_dim); \
154156
const int outer_size = meta.sizes[block_outer_dim]; \
155157
const int base_outer_idx = tidx4d.data[block_outer_dim]; \

‎backends/vulkan/runtime/graph/ops/glsl/embedding_texture.glsl‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ ${layout_declare_ubo(B, "BufferMetadata", "weight")}
3535

3636
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3737

38+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
39+
${layout_declare_spec_const(C, "int", "indices_layout", "CONTIG_LAYOUT_INT")}
40+
3841
int load_embedding_idx(const TensorIndex4D out_tidx) {
3942
TensorIndex4D indices_tidx;
4043
indices_tidx.data.xyz = out_tidx.data.yzw;
4144
indices_tidx.data.w = 0;
4245

4346
TextureElementIndex elem_pos = tensor4d_idx_to_texture_element_idx_simple(
44-
indices, indices_tidx);
47+
indices, indices_tidx, indices_layout);
4548

4649
const ivec4 in_texel = texelFetch(t_indices, elem_pos.pos, 0);
4750
return in_texel[elem_pos.comp];
@@ -63,7 +66,8 @@ void main() {
6366
return;
6467
}
6568

66-
TensorIndex4D out_tidx = texture_pos_to_tensor4d_idx_simple(outp, out_pos);
69+
TensorIndex4D out_tidx =
70+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
6771
const int embedding_idx = load_embedding_idx(out_tidx);
6872

6973
const VEC4_T weight_texel = load_weight_texel(embedding_idx, out_tidx.data.x);

‎backends/vulkan/runtime/graph/ops/glsl/expand_texture.glsl‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ void main() {
3939
return;
4040
}
4141

42-
TensorIndex4D out_tidx = texture_pos_to_tensor4d_idx_simple(outp, out_pos);
42+
TensorIndex4D out_tidx =
43+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
4344

4445
VEC4_T out_texel = VEC4_T(0);
4546

@@ -58,7 +59,8 @@ void main() {
5859
inp_tidx.data.w = out_tidx.data.w % inp.sizes.w;
5960

6061
TextureElementIndex inp_elem =
61-
tensor4d_idx_to_texture_element_idx_simple(inp, inp_tidx);
62+
tensor4d_idx_to_texture_element_idx_simple(
63+
inp, inp_tidx, out_layout);
6264

6365
VEC4_T inp_texel = texelFetch(t_inp, inp_elem.pos, 0);
6466
out_texel[comp] = inp_texel[inp_elem.comp];

‎backends/vulkan/runtime/graph/ops/glsl/full_texture.glsl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ void main() {
3838

3939
VEC4_T outtex = VEC4_T(fill_value);
4040

41-
TensorIndex4D tidx = texture_pos_to_tensor4d_idx_simple(outp, pos);
41+
TensorIndex4D tidx =
42+
texture_pos_to_tensor4d_idx_simple(outp, pos, out_layout);
4243
const int packed_dim_size = outp.sizes[packed_dim];
4344
int packed_idx = tidx.data[packed_dim];
4445

‎backends/vulkan/runtime/graph/ops/glsl/gather_buffer.glsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ${layout_declare_ubo(B, "BufferMetadata", "index")}
3232

3333
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3434

35-
layout(constant_id = 3) const int gather_dim = 0;
35+
${layout_declare_spec_const(C, "int", "gather_dim", "0")}
3636

3737
void main() {
3838
const uint out_bufi = gl_GlobalInvocationID.x;

‎backends/vulkan/runtime/graph/ops/glsl/gather_texture.glsl‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ ${layout_declare_ubo(B, "TextureMetadata", "index")}
3434

3535
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3636

37-
layout(constant_id = 3) const int gather_dim = 0;
37+
${layout_declare_spec_const(C, "int", "gather_dim", "0")}
38+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
39+
${layout_declare_spec_const(C, "int", "inp_layout", "CONTIG_LAYOUT_INT")}
40+
const int out_packed_dim = get_packed_dim(out_layout);
3841

3942
void main() {
4043
const ivec3 out_pos = ivec3(gl_GlobalInvocationID);
@@ -43,25 +46,26 @@ void main() {
4346
return;
4447
}
4548

46-
TensorIndex4D out_tidx = texture_pos_to_tensor4d_idx_simple(outp, out_pos);
49+
TensorIndex4D out_tidx =
50+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
4751
ivec4 idx_texel = texelFetch(t_index, out_pos, 0);
4852

4953
VEC4_T out_texel = VEC4_T(0);
5054

5155
int limit = min(
52-
4, outp.sizes[outp.packed_dim] - out_tidx.data[outp.packed_dim]);
56+
4, outp.sizes[out_packed_dim] - out_tidx.data[out_packed_dim]);
5357
for (int comp = 0; comp < 4; comp++) {
5458
TensorIndex4D input_tidx = out_tidx;
5559
int gather_idx = idx_texel[comp];
5660
input_tidx.data[gather_dim] = gather_idx;
5761

5862
TextureElementIndex input_elem_pos = tensor4d_idx_to_texture_element_idx_simple(
59-
inp, input_tidx);
63+
inp, input_tidx, inp_layout);
6064

6165
VEC4_T input_texel = texelFetch(t_input, input_elem_pos.pos, 0);
6266
out_texel[comp] = input_texel[input_elem_pos.comp];
6367

64-
out_tidx.data[outp.packed_dim]++;
68+
out_tidx.data[out_packed_dim]++;
6569
}
6670

6771
imageStore(t_out, out_pos, out_texel);

‎backends/vulkan/runtime/graph/ops/glsl/index_tensor_texture.glsl‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ ${layout_declare_ubo(B, "TextureMetadata", "index")}
3333

3434
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3535

36+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
37+
${layout_declare_spec_const(C, "int", "inp_layout", "CONTIG_LAYOUT_INT")}
38+
const int out_packed_dim = get_packed_dim(out_layout);
39+
3640
// Implements aten.index.Tensor for the case where self is 1D and there is
3741
// exactly one index tensor. Each output element is:
3842
// output[...] = self[index[...]]
@@ -44,13 +48,14 @@ void main() {
4448
return;
4549
}
4650

47-
TensorIndex4D out_tidx = texture_pos_to_tensor4d_idx_simple(outp, out_pos);
51+
TensorIndex4D out_tidx =
52+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
4853
ivec4 idx_texel = texelFetch(t_index, out_pos, 0);
4954

5055
VEC4_T out_texel = VEC4_T(0);
5156

5257
int limit = min(
53-
4, outp.sizes[outp.packed_dim] - out_tidx.data[outp.packed_dim]);
58+
4, outp.sizes[out_packed_dim] - out_tidx.data[out_packed_dim]);
5459
for (int comp = 0; comp < limit; comp++) {
5560
int idx = idx_texel[comp];
5661

@@ -60,12 +65,12 @@ void main() {
6065
self_tidx.data = ivec4(idx, 0, 0, 0);
6166

6267
TextureElementIndex self_elem =
63-
tensor4d_idx_to_texture_element_idx_simple(inp, self_tidx);
68+
tensor4d_idx_to_texture_element_idx_simple(inp, self_tidx, inp_layout);
6469

6570
VEC4_T self_texel = texelFetch(t_self, self_elem.pos, 0);
6671
out_texel[comp] = self_texel[self_elem.comp];
6772

68-
out_tidx.data[outp.packed_dim]++;
73+
out_tidx.data[out_packed_dim]++;
6974
}
7075

7176
imageStore(t_out, out_pos, out_texel);

‎backends/vulkan/runtime/graph/ops/glsl/indexing.glslh‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ bool is_channels_last(const int hashed_layout) {
7979
#define get_packed_dim_block_size(layout) (((layout) >> 24) & 0xF)
8080
#define get_outer_packed_dim_block_size(layout) (((layout) >> 28) & 0xF)
8181

82+
// Safe ivec4 component access via if/else chain. Avoids dynamic vector
83+
// indexing (v[index]) on UBO-backed ivec4 members, which crashes on Adreno 740
84+
// when the index is a specialization constant with value 1 or 2.
85+
int safe_idx(const ivec4 v, const int idx) {
86+
if (idx == 0) return v.x;
87+
if (idx == 1) return v.y;
88+
if (idx == 2) return v.z;
89+
return v.w;
90+
}
91+
92+
// Safe uvec4 component access via if/else chain. Same rationale as safe_idx
93+
// for ivec4 — avoids dynamic vector indexing on UBO-backed uvec4 members.
94+
uint safe_idx(const uvec4 v, const int idx) {
95+
if (idx == 0) return v.x;
96+
if (idx == 1) return v.y;
97+
if (idx == 2) return v.z;
98+
return v.w;
99+
}
100+
101+
// Safe ivec4 component write via if/else chain. Companion to safe_idx for
102+
// cases where we need to set a component by a spec-const-derived index.
103+
void safe_set(inout ivec4 v, const int idx, const int val) {
104+
if (idx == 0) { v.x = val; }
105+
else if (idx == 1) { v.y = val; }
106+
else if (idx == 2) { v.z = val; }
107+
else { v.w = val; }
108+
}
109+
82110
//
83111
// BufferMetadata
84112
//
@@ -420,17 +448,21 @@ void clamp_tensor_idx(const BufferMetadata meta, inout TensorIndex tidx) {
420448

421449
// Does not account for axis mapping
422450
TensorIndex4D texture_pos_to_tensor4d_idx_simple(
423-
const TextureMetadata meta, const ivec3 pos) {
451+
const TextureMetadata meta,
452+
const ivec3 pos,
453+
const int hashed_layout) {
454+
const int packed_dim = get_packed_dim(hashed_layout);
455+
424456
TensorIndex4D tidx;
425457
tidx.data.xyz = pos;
426458
tidx.data.w = 0;
427-
tidx.data[meta.packed_dim] *= 4;
459+
safe_set(tidx.data, packed_dim, safe_idx(tidx.data, packed_dim) * 4);
428460

429461
// Compute batch idx accounting for batch concatenation, assuming channels as
430462
// the concatenation dim.
431463
if (meta.sizes.w > 1) {
432464
int channels = meta.sizes.z;
433-
if (meta.packed_dim == 2) {
465+
if (packed_dim == 2) {
434466
channels = align_up_4(channels);
435467
}
436468
tidx.data.w = tidx.data.z / channels;
@@ -441,18 +473,26 @@ TensorIndex4D texture_pos_to_tensor4d_idx_simple(
441473

442474
// Does not account for axis mapping
443475
ivec3 tensor4d_idx_to_texel_pos_simple(
444-
const TextureMetadata meta, const TensorIndex4D tidx) {
445-
ivec3 texel_pos;
476+
const TextureMetadata meta,
477+
const TensorIndex4D tidx,
478+
const int hashed_layout) {
479+
const int packed_dim = get_packed_dim(hashed_layout);
446480

447-
const int packed_dim_idx = tidx.data[meta.packed_dim];
481+
const int packed_dim_idx = safe_idx(tidx.data, packed_dim);
448482

449-
texel_pos = tidx.data.xyz;
450-
texel_pos[meta.packed_dim] = div_4(packed_dim_idx);
483+
ivec3 texel_pos = tidx.data.xyz;
484+
if (packed_dim == 0) {
485+
texel_pos.x = div_4(packed_dim_idx);
486+
} else if (packed_dim == 1) {
487+
texel_pos.y = div_4(packed_dim_idx);
488+
} else {
489+
texel_pos.z = div_4(packed_dim_idx);
490+
}
451491

452492
// Account for batch concatenation, assuming channels as the concatenation dim
453493
if (meta.sizes.w > 1) {
454494
int channels_ntexels = meta.sizes.z;
455-
if (meta.packed_dim == 2) {
495+
if (packed_dim == 2) {
456496
channels_ntexels = div_up_4(channels_ntexels);
457497
}
458498
texel_pos.z += tidx.data.w * channels_ntexels;
@@ -463,17 +503,28 @@ ivec3 tensor4d_idx_to_texel_pos_simple(
463503

464504
// Does not account for axis mapping
465505
TextureElementIndex tensor4d_idx_to_texture_element_idx_simple(
466-
const TextureMetadata meta, const TensorIndex4D tidx) {
467-
const int packed_dim_idx = tidx.data[meta.packed_dim];
506+
const TextureMetadata meta,
507+
const TensorIndex4D tidx,
508+
const int hashed_layout) {
509+
const int packed_dim = get_packed_dim(hashed_layout);
510+
511+
const int packed_dim_idx = safe_idx(tidx.data, packed_dim);
512+
468513
TextureElementIndex tex_idx;
469514
tex_idx.pos = tidx.data.xyz;
470-
tex_idx.pos[meta.packed_dim] = div_4(packed_dim_idx);
515+
if (packed_dim == 0) {
516+
tex_idx.pos.x = div_4(packed_dim_idx);
517+
} else if (packed_dim == 1) {
518+
tex_idx.pos.y = div_4(packed_dim_idx);
519+
} else {
520+
tex_idx.pos.z = div_4(packed_dim_idx);
521+
}
471522
tex_idx.comp = mod_4(packed_dim_idx);
472523

473524
// Account for batch concatenation, assuming channels as the concatenation dim
474525
if (meta.sizes.w > 1) {
475526
int channels_ntexels = meta.sizes.z;
476-
if (meta.packed_dim == 2) {
527+
if (packed_dim == 2) {
477528
channels_ntexels = div_up_4(channels_ntexels);
478529
}
479530
tex_idx.pos.z += tidx.data.w * channels_ntexels;

0 commit comments

Comments
 (0)