Skip to content

Commit 1fcd773

Browse files
ssjiaSS-JIA
authored andcommitted
[ET-VK] Modernize binary ops with split shaders and safe UBO indexing
Pull Request resolved: pytorch#18452 Modernize the binary op implementation to use current best practices: - Split binary_op.glsl into binary_op_buffer.glsl and binary_op_texture.glsl with separate YAML configs, eliminating #ifdef USING_BUFFER branching - Rewrite the texture shader to use element-by-element processing with TextureMetadata UBOs and indexing.glslh (replaces legacy push-constant sizes, broadcasting_utils.h, indexing_utils.h, and MASK_PADDING mechanism) - Unify comparison op variants — scalar comparisons (X < Y) work for both buffer and texture in element-by-element mode, removing the need for separate lessThan/greaterThan texture variants - Merge C++ dispatch into a single add_binary_op_node() using graph.meta_ubo() which auto-selects BufferMetadata or TextureMetadata - Apply safe_idx/safe_set helpers to avoid Adreno 740 SPIR-V compiler crash caused by dynamic UBO ivec4 indexing with specialization constants - Add safe_set() to indexing.glslh as companion to existing safe_idx() ghstack-source-id: 357266948 @exported-using-ghexport Differential Revision: [D97959282](https://our.internmc.facebook.com/intern/diff/D97959282/)
1 parent 4888851 commit 1fcd773

8 files changed

Lines changed: 350 additions & 375 deletions

File tree

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

Lines changed: 0 additions & 170 deletions
This file was deleted.

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

Lines changed: 0 additions & 126 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
// Binary comparison ops require that the output is boolean and not the same as input.
10+
$IS_COMPARISON_OP = (any([name in VARIANT_NAME for name in ["binary_eq", "binary_lt", "binary_le", "binary_gt", "binary_ge"]]))
11+
12+
#version 450 core
13+
14+
${define_required_extensions("buffer", DTYPE)}
15+
$if IS_COMPARISON_OP:
16+
${define_required_extensions("buffer", "uint8")}
17+
18+
#define PRECISION ${PRECISION}
19+
20+
#define NAME ${VARIANT_NAME}
21+
22+
#define VEC4_T ${texel_load_type(DTYPE, "buffer")}
23+
$if IS_COMPARISON_OP:
24+
#define T ${buffer_scalar_type("uint8")}
25+
#define VEC4_OUT_T ${texel_load_type("uint8", "buffer")}
26+
$else:
27+
#define T ${buffer_scalar_type(DTYPE)}
28+
#define VEC4_OUT_T VEC4_T
29+
30+
#define op(X, Y, A) ${OPERATOR}
31+
32+
${define_active_storage_type("buffer")}
33+
34+
layout(std430) buffer;
35+
36+
#include "indexing.glslh"
37+
38+
$if IS_COMPARISON_OP:
39+
${layout_declare_tensor(B, "w", "t_out", "uint8", "buffer")}
40+
$else:
41+
${layout_declare_tensor(B, "w", "t_out", DTYPE, "buffer")}
42+
43+
${layout_declare_tensor(B, "r", "t_in", DTYPE, "buffer")}
44+
${layout_declare_tensor(B, "r", "t_other", DTYPE, "buffer")}
45+
46+
${layout_declare_ubo(B, "BufferMetadata", "outp")}
47+
${layout_declare_ubo(B, "BufferMetadata", "inp")}
48+
${layout_declare_ubo(B, "BufferMetadata", "otherp")}
49+
50+
layout(push_constant) uniform restrict Block {
51+
float alpha;
52+
};
53+
54+
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
55+
56+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
57+
${layout_declare_spec_const(C, "int", "in_layout", "CONTIG_LAYOUT_INT")}
58+
${layout_declare_spec_const(C, "int", "other_layout", "CONTIG_LAYOUT_INT")}
59+
// Unused by buffer path, declared for spec constant ID consistency with texture
60+
${layout_declare_spec_const(C, "int", "in_broadcast_packed_dim", "0")}
61+
${layout_declare_spec_const(C, "int", "other_broadcast_packed_dim", "0")}
62+
63+
void main() {
64+
const uint out_bufi = gl_GlobalInvocationID.x;
65+
if (out_of_bounds(out_bufi, outp)) {
66+
return;
67+
}
68+
69+
// Simple case; no broadcasting
70+
if (are_equal(inp, otherp)) {
71+
t_out[out_bufi] = T(op(t_in[out_bufi], t_other[out_bufi], T(alpha)));
72+
return;
73+
}
74+
75+
TensorIndex outp_tidx = linear_idx_to_tensor_idx(outp, out_bufi);
76+
77+
TensorIndex inp_tidx = outp_tidx;
78+
clamp_tensor_idx(inp, inp_tidx);
79+
80+
TensorIndex other_tidx = outp_tidx;
81+
clamp_tensor_idx(otherp, other_tidx);
82+
83+
uint inp_bufi = tensor_idx_to_linear_idx(inp, inp_tidx);
84+
uint other_bufi = tensor_idx_to_linear_idx(otherp, other_tidx);
85+
86+
t_out[out_bufi] = T(op(t_in[inp_bufi], t_other[other_bufi], T(alpha)));
87+
}

0 commit comments

Comments
 (0)