Skip to content

Commit 60909ad

Browse files
ssjiaSS-JIA
authored andcommitted
[ET-VK] Modernize staging shaders to use indexing.glslh and TextureMetadata
Pull Request resolved: pytorch#18450 Rewrites nchw_to_image.glsl and image_to_nchw.glsl from scratch using the modern indexing.glslh header with TextureMetadata UBOs. This eliminates all dynamic array indexing patterns (e.g. tidx[packed_dim], sizes[packed_dim]) from the old indexing_utils.h header that triggered SIGSEGV crashes in the Adreno 740 GPU driver's shader compiler during vkCreateComputePipelines. The new shaders follow the same component loop pattern used by gather_texture.glsl: convert texture position to TensorIndex4D via texture_pos_to_tensor4d_idx_simple(), then iterate over packed components. Also removes the USE_PUSH_CONST/no_pc shader variants since the rewritten shaders always use UBOs via TextureMetadata. C++ dispatch code (Staging.cpp, Clone.cpp) updated to pass TextureMetadata UBOs for texture tensors and BufferMetadata UBOs for the clone buffer↔image paths. ghstack-source-id: 357266946 @exported-using-ghexport Differential Revision: [D97959285](https://our.internmc.facebook.com/intern/diff/D97959285/)
1 parent 5744fff commit 60909ad

15 files changed

Lines changed: 312 additions & 417 deletions

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

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,81 @@ ${define_active_storage_type(STORAGE)}
2121

2222
layout(std430) buffer;
2323

24+
#include "indexing.glslh"
25+
2426
${layout_declare_buffer(B, "w", "buf_out", BUF_DTYPE)}
2527
${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)}
2628

27-
$if USE_PUSH_CONST:
28-
layout(push_constant) uniform restrict Block {
29-
ivec4 sizes;
30-
$if not TO_STAGING:
31-
ivec4 buf_strides;
32-
};
33-
$else:
34-
${layout_declare_ubo(B, "ivec4", "sizes")}
35-
$if not TO_STAGING:
36-
${layout_declare_ubo(B, "ivec4", "buf_strides")}
29+
${layout_declare_ubo(B, "TextureMetadata", "inp")}
3730

38-
#include "indexing_utils.h"
31+
$if not TO_STAGING:
32+
${layout_declare_ubo(B, "BufferMetadata", "buf_meta")}
3933

4034
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
4135

42-
${layout_declare_spec_const(C, "int", "t_layout", "DEFAULT_LAYOUT")}
43-
const lowp ivec4 axis_map = unhash_axis_map(t_layout);
44-
const lowp int packed_dim = unhash_packed_dim(t_layout);
36+
${layout_declare_spec_const(C, "int", "in_layout", "CONTIG_LAYOUT_INT")}
4537

46-
void write_out_texel(VEC4_T texel, ivec4 tidx) {
47-
$if TO_STAGING:
48-
const ivec4 buf_indices = tidx_to_nchwi(tidx, sizes, packed_dim);
49-
$else:
50-
const ivec4 buf_indices = tidx_to_4bufi(tidx, buf_strides, packed_dim);
38+
$if not TO_STAGING:
39+
${layout_declare_spec_const(C, "int", "buf_layout", "CONTIG_LAYOUT_INT")}
5140

52-
if (tidx[packed_dim] < sizes[packed_dim]) {
53-
buf_out[buf_indices.x] = BUF_T(texel.x);
54-
}
55-
if (tidx[packed_dim] + 1 < sizes[packed_dim]) {
56-
buf_out[buf_indices.y] = BUF_T(texel.y);
57-
}
58-
if (tidx[packed_dim] + 2 < sizes[packed_dim]) {
59-
buf_out[buf_indices.z] = BUF_T(texel.z);
60-
}
61-
if (tidx[packed_dim] + 3 < sizes[packed_dim]) {
62-
buf_out[buf_indices.w] = BUF_T(texel.w);
41+
void main() {
42+
const ivec3 pos = ivec3(gl_GlobalInvocationID);
43+
if (out_of_bounds(pos, inp)) {
44+
return;
6345
}
64-
}
6546

66-
void main() {
67-
const ivec3 lpos = ivec3(gl_GlobalInvocationID);
68-
const ivec4 tidx = lpos_to_tidx(lpos, sizes, axis_map.w, packed_dim);
47+
TensorIndex4D tidx = texture_pos_to_tensor4d_idx_simple(inp, pos, in_layout);
48+
#ifdef USING_TEXTURE2D
49+
const VEC4_T intex = texelFetch(t_in, pos.xy, 0);
50+
#else
51+
const VEC4_T intex = texelFetch(t_in, pos, 0);
52+
#endif
53+
54+
const int packed_dim = get_packed_dim(in_layout);
55+
int packed_dim_val;
56+
if (packed_dim == 0) {
57+
packed_dim_val = tidx.data.x;
58+
} else if (packed_dim == 1) {
59+
packed_dim_val = tidx.data.y;
60+
} else if (packed_dim == 2) {
61+
packed_dim_val = tidx.data.z;
62+
} else {
63+
packed_dim_val = tidx.data.w;
64+
}
6965

70-
if (any(greaterThanEqual(tidx, sizes))) {
71-
return;
66+
int packed_dim_size;
67+
if (packed_dim == 0) {
68+
packed_dim_size = inp.sizes.x;
69+
} else if (packed_dim == 1) {
70+
packed_dim_size = inp.sizes.y;
71+
} else if (packed_dim == 2) {
72+
packed_dim_size = inp.sizes.z;
73+
} else {
74+
packed_dim_size = inp.sizes.w;
7275
}
7376

74-
const VEC4_T intex = load_texel(t_in, lpos_to_pos(lpos, axis_map));
75-
write_out_texel(intex, tidx);
77+
int limit = min(4, packed_dim_size - packed_dim_val);
78+
79+
for (int comp = 0; comp < limit; comp++) {
80+
$if TO_STAGING:
81+
// Compute contiguous NCHW index
82+
int nchwi = tidx.data.x
83+
+ tidx.data.y * inp.sizes.x
84+
+ tidx.data.z * inp.sizes.x * inp.sizes.y
85+
+ tidx.data.w * inp.sizes.x * inp.sizes.y * inp.sizes.z;
86+
buf_out[nchwi] = BUF_T(intex[comp]);
87+
$else:
88+
int bufi = tensor4d_idx_to_buf_idx(buf_meta, tidx, buf_layout);
89+
buf_out[bufi] = BUF_T(intex[comp]);
90+
91+
if (packed_dim == 0) {
92+
tidx.data.x++;
93+
} else if (packed_dim == 1) {
94+
tidx.data.y++;
95+
} else if (packed_dim == 2) {
96+
tidx.data.z++;
97+
} else {
98+
tidx.data.w++;
99+
}
100+
}
76101
}

‎backends/vulkan/runtime/graph/ops/glsl/image_to_nchw.yaml‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ image_to_nchw:
1010
BUF_DTYPE: float
1111
STORAGE: texture3d
1212
TO_STAGING: True
13-
USE_PUSH_CONST: True
1413
generate_variant_forall:
1514
combination:
1615
parameter_names: [DTYPE, BUF_DTYPE]
@@ -28,11 +27,3 @@ image_to_nchw:
2827
STORAGE: texture2d
2928
- NAME: clone_image_to_buffer
3029
TO_STAGING: False
31-
- NAME: image_to_nchw_no_pc_texture3d
32-
USE_PUSH_CONST: False
33-
- NAME: image_to_nchw_no_pc_texture2d
34-
STORAGE: texture2d
35-
USE_PUSH_CONST: False
36-
- NAME: clone_image_to_buffer_no_pc
37-
TO_STAGING: False
38-
USE_PUSH_CONST: False

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bool is_channels_last(const int hashed_layout) {
7878
#define get_outer_packed_dim(layout) (((layout) >> 20) & 0xF)
7979
#define get_packed_dim_block_size(layout) (((layout) >> 24) & 0xF)
8080
#define get_outer_packed_dim_block_size(layout) (((layout) >> 28) & 0xF)
81+
#define get_batch_concat_dim(layout) (((layout) >> 12) & 0xF)
8182

8283
// Safe ivec4 component access via if/else chain. Avoids dynamic vector
8384
// indexing (v[index]) on UBO-backed ivec4 members, which crashes on Adreno 740
@@ -458,15 +459,16 @@ TensorIndex4D texture_pos_to_tensor4d_idx_simple(
458459
tidx.data.w = 0;
459460
safe_set(tidx.data, packed_dim, safe_idx(tidx.data, packed_dim) * 4);
460461

461-
// Compute batch idx accounting for batch concatenation, assuming channels as
462-
// the concatenation dim.
462+
// Compute batch idx using batch_concat_dim from hashed_layout
463463
if (meta.sizes.w > 1) {
464-
int channels = meta.sizes.z;
465-
if (packed_dim == 2) {
466-
channels = align_up_4(channels);
464+
const int batch_concat_dim = get_batch_concat_dim(hashed_layout);
465+
int concat_dim_size = safe_idx(meta.sizes, batch_concat_dim);
466+
if (batch_concat_dim == packed_dim) {
467+
concat_dim_size = align_up_4(concat_dim_size);
467468
}
468-
tidx.data.w = tidx.data.z / channels;
469-
tidx.data.z = tidx.data.z % channels;
469+
int concat_dim_val = safe_idx(tidx.data, batch_concat_dim);
470+
tidx.data.w = concat_dim_val / concat_dim_size;
471+
safe_set(tidx.data, batch_concat_dim, concat_dim_val % concat_dim_size);
470472
}
471473
return tidx;
472474
}
@@ -489,13 +491,20 @@ ivec3 tensor4d_idx_to_texel_pos_simple(
489491
texel_pos.z = div_4(packed_dim_idx);
490492
}
491493

492-
// Account for batch concatenation, assuming channels as the concatenation dim
494+
// Account for batch concatenation
493495
if (meta.sizes.w > 1) {
494-
int channels_ntexels = meta.sizes.z;
495-
if (packed_dim == 2) {
496-
channels_ntexels = div_up_4(channels_ntexels);
496+
const int batch_concat_dim = get_batch_concat_dim(hashed_layout);
497+
int concat_dim_ntexels = safe_idx(meta.sizes, batch_concat_dim);
498+
if (batch_concat_dim == packed_dim) {
499+
concat_dim_ntexels = div_up_4(concat_dim_ntexels);
500+
}
501+
if (batch_concat_dim == 0) {
502+
texel_pos.x += tidx.data.w * concat_dim_ntexels;
503+
} else if (batch_concat_dim == 1) {
504+
texel_pos.y += tidx.data.w * concat_dim_ntexels;
505+
} else {
506+
texel_pos.z += tidx.data.w * concat_dim_ntexels;
497507
}
498-
texel_pos.z += tidx.data.w * channels_ntexels;
499508
}
500509

501510
return texel_pos;
@@ -521,13 +530,20 @@ TextureElementIndex tensor4d_idx_to_texture_element_idx_simple(
521530
}
522531
tex_idx.comp = mod_4(packed_dim_idx);
523532

524-
// Account for batch concatenation, assuming channels as the concatenation dim
533+
// Account for batch concatenation
525534
if (meta.sizes.w > 1) {
526-
int channels_ntexels = meta.sizes.z;
527-
if (packed_dim == 2) {
528-
channels_ntexels = div_up_4(channels_ntexels);
535+
const int batch_concat_dim = get_batch_concat_dim(hashed_layout);
536+
int concat_dim_ntexels = safe_idx(meta.sizes, batch_concat_dim);
537+
if (batch_concat_dim == packed_dim) {
538+
concat_dim_ntexels = div_up_4(concat_dim_ntexels);
539+
}
540+
if (batch_concat_dim == 0) {
541+
tex_idx.pos.x += tidx.data.w * concat_dim_ntexels;
542+
} else if (batch_concat_dim == 1) {
543+
tex_idx.pos.y += tidx.data.w * concat_dim_ntexels;
544+
} else {
545+
tex_idx.pos.z += tidx.data.w * concat_dim_ntexels;
529546
}
530-
tex_idx.pos.z += tidx.data.w * channels_ntexels;
531547
}
532548

533549
return tex_idx;

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

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,95 @@ ${define_active_storage_type(STORAGE)}
2020

2121
layout(std430) buffer;
2222

23+
#include "indexing.glslh"
24+
2325
${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)}
2426
${layout_declare_buffer(B, "r", "buf_in", BUF_DTYPE)}
2527

26-
$if USE_PUSH_CONST:
27-
layout(push_constant) uniform restrict Block {
28-
ivec4 sizes;
29-
$if not FROM_STAGING:
30-
ivec4 buf_strides;
31-
};
32-
$else:
33-
${layout_declare_ubo(B, "ivec4", "sizes")}
34-
$if not FROM_STAGING:
35-
${layout_declare_ubo(B, "ivec4", "buf_strides")}
28+
${layout_declare_ubo(B, "TextureMetadata", "outp")}
3629

37-
#include "indexing_utils.h"
30+
$if not FROM_STAGING:
31+
${layout_declare_ubo(B, "BufferMetadata", "buf_meta")}
3832

3933
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
4034

41-
${layout_declare_spec_const(C, "int", "t_layout", "DEFAULT_LAYOUT")}
35+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
4236
${layout_declare_spec_const(C, "int", "transpose_hw", "0")}
4337

44-
const lowp ivec4 axis_map = unhash_axis_map(t_layout);
45-
const lowp int packed_dim = unhash_packed_dim(t_layout);
38+
$if not FROM_STAGING:
39+
${layout_declare_spec_const(C, "int", "buf_layout", "CONTIG_LAYOUT_INT")}
4640

47-
VEC4_T read_texel(ivec4 tidx) {
48-
ivec4 tidx_to_use = tidx;
49-
ivec4 sizes_to_use = sizes;
50-
int packed_dim_to_use = packed_dim;
51-
if (transpose_hw == 1) {
52-
sizes_to_use.xy = sizes_to_use.yx;
53-
tidx_to_use.xy = tidx.yx;
41+
void main() {
42+
const ivec3 pos = ivec3(gl_GlobalInvocationID);
43+
if (out_of_bounds(pos, outp)) {
44+
return;
45+
}
5446

55-
if (packed_dim == 1) {
56-
packed_dim_to_use = 0;
57-
}
58-
if (packed_dim == 0) {
59-
packed_dim_to_use = 1;
60-
}
47+
TensorIndex4D tidx = texture_pos_to_tensor4d_idx_simple(outp, pos, out_layout);
48+
49+
const int packed_dim = get_packed_dim(out_layout);
50+
int packed_dim_val;
51+
if (packed_dim == 0) {
52+
packed_dim_val = tidx.data.x;
53+
} else if (packed_dim == 1) {
54+
packed_dim_val = tidx.data.y;
55+
} else if (packed_dim == 2) {
56+
packed_dim_val = tidx.data.z;
57+
} else {
58+
packed_dim_val = tidx.data.w;
6159
}
6260

63-
$if FROM_STAGING:
64-
const ivec4 buf_indices = tidx_to_nchwi(tidx_to_use, sizes_to_use, packed_dim_to_use);
65-
$else:
66-
const ivec4 buf_indices = tidx_to_4bufi(tidx_to_use, buf_strides, packed_dim_to_use);
61+
int packed_dim_size;
62+
if (packed_dim == 0) {
63+
packed_dim_size = outp.sizes.x;
64+
} else if (packed_dim == 1) {
65+
packed_dim_size = outp.sizes.y;
66+
} else if (packed_dim == 2) {
67+
packed_dim_size = outp.sizes.z;
68+
} else {
69+
packed_dim_size = outp.sizes.w;
70+
}
6771

6872
VEC4_T texel = VEC4_T(0);
69-
if (tidx[packed_dim] < sizes[packed_dim]) {
70-
texel.x = SCALAR_T(buf_in[buf_indices.x]);
71-
}
72-
if (tidx[packed_dim] + 1 < sizes[packed_dim]) {
73-
texel.y = SCALAR_T(buf_in[buf_indices.y]);
74-
}
75-
if (tidx[packed_dim] + 2 < sizes[packed_dim]) {
76-
texel.z = SCALAR_T(buf_in[buf_indices.z]);
77-
}
78-
if (tidx[packed_dim] + 3 < sizes[packed_dim]) {
79-
texel.w = SCALAR_T(buf_in[buf_indices.w]);
80-
}
81-
return texel;
82-
}
73+
int limit = min(4, packed_dim_size - packed_dim_val);
74+
75+
for (int comp = 0; comp < limit; comp++) {
76+
TensorIndex4D buf_tidx = tidx;
77+
if (transpose_hw == 1) {
78+
int tmp = buf_tidx.data.x;
79+
buf_tidx.data.x = buf_tidx.data.y;
80+
buf_tidx.data.y = tmp;
81+
}
8382

84-
void main() {
85-
const ivec3 lpos = ivec3(gl_GlobalInvocationID);
86-
const ivec4 tidx = lpos_to_tidx(lpos, sizes, axis_map.w, packed_dim);
87-
if (any(greaterThanEqual(tidx, sizes))) {
88-
return;
83+
$if FROM_STAGING:
84+
// Compute contiguous NCHW index
85+
ivec4 s = outp.sizes;
86+
if (transpose_hw == 1) {
87+
s.xy = s.yx;
88+
}
89+
int nchwi = buf_tidx.data.x
90+
+ buf_tidx.data.y * s.x
91+
+ buf_tidx.data.z * s.x * s.y
92+
+ buf_tidx.data.w * s.x * s.y * s.z;
93+
texel[comp] = SCALAR_T(buf_in[nchwi]);
94+
$else:
95+
int bufi = tensor4d_idx_to_buf_idx(buf_meta, buf_tidx, buf_layout);
96+
texel[comp] = SCALAR_T(buf_in[bufi]);
97+
98+
if (packed_dim == 0) {
99+
tidx.data.x++;
100+
} else if (packed_dim == 1) {
101+
tidx.data.y++;
102+
} else if (packed_dim == 2) {
103+
tidx.data.z++;
104+
} else {
105+
tidx.data.w++;
106+
}
89107
}
90108

91-
$if DTYPE == "double" and DTYPE == "int64":
92-
VEC4_T texel = read_texel(tidx);
93-
write_texel(t_out, lpos_to_pos(lpos, axis_map), texel);
94-
$else:
95-
write_texel(t_out, lpos_to_pos(lpos, axis_map), read_texel(tidx));
109+
#ifdef USING_TEXTURE2D
110+
imageStore(t_out, pos.xy, texel);
111+
#else
112+
imageStore(t_out, pos, texel);
113+
#endif
96114
}

‎backends/vulkan/runtime/graph/ops/glsl/nchw_to_image.yaml‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ nchw_to_image:
99
STORAGE: texture3d
1010
DTYPE: float
1111
FROM_STAGING: True
12-
USE_PUSH_CONST: True
1312
generate_variant_forall:
1413
combination:
1514
parameter_names: [DTYPE, BUF_DTYPE]
@@ -27,11 +26,3 @@ nchw_to_image:
2726
STORAGE: texture2d
2827
- NAME: clone_buffer_to_image
2928
FROM_STAGING: False
30-
- NAME: nchw_to_image_no_pc_texture3d
31-
USE_PUSH_CONST: False
32-
- NAME: nchw_to_image_no_pc_texture2d
33-
STORAGE: texture2d
34-
USE_PUSH_CONST: False
35-
- NAME: clone_buffer_to_image_no_pc
36-
FROM_STAGING: False
37-
USE_PUSH_CONST: False

0 commit comments

Comments
 (0)