Skip to content

Commit 40dd047

Browse files
committed
[ET-VK][runtime][1/3] Prepare linear shaders for 2D dispatch
Pull Request resolved: pytorch#22223 Add `dispatch.glslh` with `linear_idx_from_gid()`, which flattens two-dimensional global invocation IDs. Migrate shaders whose global X coordinate represents a linear logical index. Existing one-dimensional dispatch geometry remains unchanged, making this a behavior-neutral preparation for centralized dispatch wrapping. Authored with Codex. ghstack-source-id: 421281480 @exported-using-ghexport Differential Revision: [D117706349](https://our.internmc.facebook.com/intern/diff/D117706349/)
1 parent 3353bd5 commit 40dd047

50 files changed

Lines changed: 174 additions & 56 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ layout(push_constant) uniform restrict Block {
3636

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

39+
#include "dispatch.glslh"
40+
3941
void main() {
40-
const int i = int(gl_GlobalInvocationID.x);
42+
const int i = int(linear_idx_from_gid());
4143
if (i >= numel) {
4244
return;
4345
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ ${layout_declare_ubo(B, "float", "step")}
2828

2929
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3030

31+
#include "dispatch.glslh"
32+
3133
void main() {
32-
const uint out_bufi = gl_GlobalInvocationID.x;
34+
const uint out_bufi = linear_idx_from_gid();
3335
if (out_of_bounds(out_bufi, outp)) {
3436
return;
3537
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ layout(push_constant) uniform restrict Block {
5353

5454
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
5555

56+
#include "dispatch.glslh"
57+
5658
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
5759
${layout_declare_spec_const(C, "int", "in_layout", "CONTIG_LAYOUT_INT")}
5860
${layout_declare_spec_const(C, "int", "other_layout", "CONTIG_LAYOUT_INT")}
@@ -61,7 +63,7 @@ ${layout_declare_spec_const(C, "int", "in_broadcast_packed_dim", "0")}
6163
${layout_declare_spec_const(C, "int", "other_broadcast_packed_dim", "0")}
6264

6365
void main() {
64-
const uint out_bufi = gl_GlobalInvocationID.x;
66+
const uint out_bufi = linear_idx_from_gid();
6567
if (out_of_bounds(out_bufi, outp)) {
6668
return;
6769
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ layout(push_constant) uniform restrict Block {
5757

5858
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
5959

60+
#include "dispatch.glslh"
61+
6062
$if not IS_COMPARISON_OP:
6163
#include "binary_op_defs.glslh"
6264

6365
void main() {
64-
const uint out_bufi = gl_GlobalInvocationID.x;
66+
const uint out_bufi = linear_idx_from_gid();
6567
if (out_of_bounds(out_bufi, outp)) {
6668
return;
6769
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ $else:
3232

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

35+
#include "dispatch.glslh"
36+
3537
${layout_declare_spec_const(C, "int", "t_layout", "DEFAULT_LAYOUT")}
3638
const lowp ivec4 axis_map = unhash_axis_map(t_layout);
3739
const lowp int packed_dim = unhash_packed_dim(t_layout);
3840

3941
void main() {
40-
const int out_buf_idx = int(gl_GlobalInvocationID.x);
42+
const int out_buf_idx = int(linear_idx_from_gid());
4143
// On the CPU, the number of elements is determined based on a buffer of int8
4244
// elements. However, on the GPU, since the int8 data type is not supported
4345
// each group of 4 elements is interepreted as 1 int32 element. Thus each

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ ${layout_declare_ubo(2, "int", "numel")}
1414

1515
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
1616

17+
#include "dispatch.glslh"
18+
1719
void main() {
18-
int tid = int(gl_GlobalInvocationID.x);
20+
int tid = int(linear_idx_from_gid());
1921
if (tid >= numel) {
2022
return;
2123
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ ${layout_declare_ubo(B, "BufferMetadata", "inp")}
1919

2020
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
2121

22+
#include "dispatch.glslh"
23+
2224
// This constant is unused in this shader but is kept so that the signature is
2325
// consistent with image_to_nchw.
2426
${layout_declare_spec_const(C, "int", "unused", "0")}
2527

2628
void main() {
27-
uint inp_bufi = gl_GlobalInvocationID.x;
29+
uint inp_bufi = linear_idx_from_gid();
2830
if (inp_bufi>= numel(inp)) {
2931
return;
3032
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ ${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
3737

3838
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3939

40+
#include "dispatch.glslh"
41+
4042
#define NUM_INPUTS ${NUM_INPUTS}
4143

4244
/*
@@ -45,7 +47,7 @@ layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
4547
* be inserted along the output's concat_dim starting at concat_offset.
4648
*/
4749
void main() {
48-
const int tid = int(gl_GlobalInvocationID.x);
50+
const int tid = int(linear_idx_from_gid());
4951

5052
// The 1-3 input tensors are interpreted as one concatenated tensor ("volume")
5153
// along the concat_dim for the purposes of tensor indexing. Each thread is

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const int out_packed_dim = get_packed_dim(out_layout);
4343

4444
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
4545

46+
#include "dispatch.glslh"
47+
4648
#define NUM_INPUTS ${NUM_INPUTS}
4749

4850
/*
@@ -55,7 +57,7 @@ layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
5557
* input tensor.
5658
*/
5759
void main() {
58-
const int tid = int(gl_GlobalInvocationID.x);
60+
const int tid = int(linear_idx_from_gid());
5961

6062
// Compute inp_volume_sizes from output sizes, replacing concat_dim with the
6163
// sum of all input sizes along that dimension.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ ${layout_declare_ubo(8, "float", "out_min", "float", "out_max")}
3030

3131
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
3232

33+
#include "dispatch.glslh"
34+
3335
${layout_declare_spec_const(C, "int", "ngroups", "1")}
3436

3537
/*
3638
* Computes a depthwise convolution. Each shader invocation calculates the
3739
* output at a single output location.
3840
*/
3941
void main() {
40-
const uint div_by_x = gl_GlobalInvocationID.x / out_limits.x;
42+
const uint linear_idx = linear_idx_from_gid();
43+
const uint div_by_x = linear_idx / out_limits.x;
4144
const ivec3 pos = ivec3(
42-
gl_GlobalInvocationID.x % out_limits.x,
45+
linear_idx % out_limits.x,
4346
div_by_x % out_limits.y,
4447
div_by_x / out_limits.y);
4548

0 commit comments

Comments
 (0)