Skip to content

Commit 4888851

Browse files
ssjiaSS-JIA
authored andcommitted
[ET-VK] Modernize view op with indexing.glslh and consolidate dispatch
Pull Request resolved: pytorch#18451 Modernizes the view operator implementation: Shader changes: - Renames view.glsl to view_texture.glsl to follow the naming convention for texture-path shaders - Rewrites the shader to use indexing.glslh utilities and TextureMetadata UBOs instead of the deprecated indexing_utils.h with push constant sizes - Adds safe_idx() to indexing.glslh for Adreno 740-safe UBO ivec4 component access via if/else chains instead of dynamic v[index] C++ dispatch changes: - Consolidates 4 separate add_*_node functions into 2: add_view_copy_node (handles both buffer and texture) and add_view_copy_convert_node (handles both buffer and texture) - Each function conditionally builds kernel name, UBOs, and spec constants before a single DynamicDispatchNode creation - Uses graph.meta_ubo() to auto-select buffer vs texture UBO - Updates all callers: Clone, Squeeze, Unsqueeze, ToCopy ghstack-source-id: 357266949 @exported-using-ghexport Differential Revision: [D97959284](https://our.internmc.facebook.com/intern/diff/D97959284/)
1 parent 60909ad commit 4888851

9 files changed

Lines changed: 133 additions & 173 deletions

File tree

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

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#version 450 core
10+
11+
${define_required_extensions(STORAGE, DTYPE)}
12+
13+
#define PRECISION ${PRECISION}
14+
15+
#define VEC4_T ${texel_type(DTYPE)}
16+
17+
${define_active_storage_type(STORAGE)}
18+
19+
layout(std430) buffer;
20+
21+
#include "indexing.glslh"
22+
23+
${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)}
24+
${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)}
25+
26+
${layout_declare_ubo(B, "TextureMetadata", "outp")}
27+
${layout_declare_ubo(B, "TextureMetadata", "inp")}
28+
29+
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
30+
31+
${layout_declare_spec_const(C, "int", "in_layout", "CONTIG_LAYOUT_INT")}
32+
${layout_declare_spec_const(C, "int", "out_layout", "CONTIG_LAYOUT_INT")}
33+
34+
void main() {
35+
const ivec3 out_pos = ivec3(gl_GlobalInvocationID);
36+
37+
if (out_of_bounds(out_pos, outp)) {
38+
return;
39+
}
40+
41+
TensorIndex4D out_tidx =
42+
texture_pos_to_tensor4d_idx_simple(outp, out_pos, out_layout);
43+
44+
ivec4 nchw_strides = ivec4(
45+
1,
46+
outp.sizes.x,
47+
outp.sizes.x * outp.sizes.y,
48+
outp.sizes.x * outp.sizes.y * outp.sizes.z);
49+
50+
int base_nchw = out_tidx.data.x * nchw_strides.x +
51+
out_tidx.data.y * nchw_strides.y +
52+
out_tidx.data.z * nchw_strides.z +
53+
out_tidx.data.w * nchw_strides.w;
54+
55+
const int out_packed_dim = get_packed_dim(out_layout);
56+
int packed_stride = nchw_strides[out_packed_dim];
57+
int limit = min(4,
58+
safe_idx(outp.sizes, out_packed_dim) -
59+
out_tidx.data[out_packed_dim]);
60+
61+
VEC4_T value = VEC4_T(0);
62+
for (int i = 0; i < 4; i++) {
63+
if (i >= limit) break;
64+
65+
int nchw_idx = base_nchw + i * packed_stride;
66+
67+
int div_x = nchw_idx / inp.sizes.x;
68+
int div_y = div_x / inp.sizes.y;
69+
TensorIndex4D in_tidx;
70+
in_tidx.data = ivec4(
71+
nchw_idx % inp.sizes.x,
72+
div_x % inp.sizes.y,
73+
div_y % inp.sizes.z,
74+
div_y / inp.sizes.z);
75+
76+
TextureElementIndex in_elem =
77+
tensor4d_idx_to_texture_element_idx_simple(inp, in_tidx, in_layout);
78+
VEC4_T intex = texelFetch(t_in, in_elem.pos, 0);
79+
value[i] = intex[in_elem.comp];
80+
}
81+
82+
imageStore(t_out, out_pos, value);
83+
}

backends/vulkan/runtime/graph/ops/glsl/view.yaml renamed to backends/vulkan/runtime/graph/ops/glsl/view_texture.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
view:
1+
view_texture:
22
parameter_names_with_default_values:
33
DTYPE: float
44
NDIM: 3
@@ -10,4 +10,4 @@ view:
1010
- VALUE: int32
1111
- VALUE: uint8
1212
shader_variants:
13-
- NAME: view
13+
- NAME: view_texture

‎backends/vulkan/runtime/graph/ops/impl/Clone.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void clone(ComputeGraph& graph, const std::vector<ValueRef>& args) {
146146
if (graph.hashed_layout_of(src) == graph.hashed_layout_of(dst)) {
147147
return add_clone_node(graph, src, dst);
148148
} else {
149-
return add_view_node(graph, src, kDummyValueRef, dst);
149+
return add_view_copy_node(graph, src, dst, {}, resize_clone_node);
150150
}
151151
}
152152
if (src_storage == utils::kTexture3D && dst_storage == utils::kBuffer) {
@@ -158,8 +158,7 @@ void clone(ComputeGraph& graph, const std::vector<ValueRef>& args) {
158158

159159
std::vector<ValueRef> extra_args = {};
160160
// Buffer to buffer copy
161-
return add_view_copy_buffer_node(
162-
graph, src, dst, extra_args, resize_clone_node);
161+
return add_view_copy_node(graph, src, dst, extra_args, resize_clone_node);
163162
}
164163

165164
// Clone node is not the most efficient implementation for the aten.clone

‎backends/vulkan/runtime/graph/ops/impl/Squeeze.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ void squeeze_copy_dims(ComputeGraph& graph, const std::vector<ValueRef>& args) {
100100
std::vector<ValueRef> resize_args = {dims};
101101

102102
if (graph.is_buffer_storage(in)) {
103-
return add_view_copy_buffer_node(
104-
graph, in, out, resize_args, resize_squeeze_node);
103+
return add_view_copy_node(graph, in, out, resize_args, resize_squeeze_node);
105104
}
106105
return add_squeeze_copy_dims_node(graph, in, dims, out);
107106
}

‎backends/vulkan/runtime/graph/ops/impl/ToCopy.cpp‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ void add_to_copy_node(ComputeGraph& graph, ValueRef in, ValueRef out) {
4141
}
4242

4343
// Other conversions (e.g. int<->float) use view_convert shaders
44-
if (graph.is_buffer_storage(in)) {
45-
add_view_copy_convert_buffer_node(
46-
graph, in, out, {}, resize_to_copy_op_node);
47-
} else {
48-
add_view_copy_convert_texture_node(
49-
graph, in, out, {}, resize_to_copy_op_node);
50-
}
44+
add_view_copy_convert_node(graph, in, out, {}, resize_to_copy_op_node);
5145
}
5246

5347
void to_copy(ComputeGraph& graph, const std::vector<ValueRef>& args) {

‎backends/vulkan/runtime/graph/ops/impl/Unsqueeze.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void unsqueeze(ComputeGraph& graph, const std::vector<ValueRef>& args) {
9999

100100
std::vector<ValueRef> resize_args = {dims};
101101
if (graph.is_buffer_storage(in)) {
102-
return add_view_copy_buffer_node(
102+
return add_view_copy_node(
103103
graph, in, out, resize_args, resize_unsqueeze_node);
104104
}
105105
return add_unsqueeze_node(graph, in, dims, out);

‎backends/vulkan/runtime/graph/ops/impl/View.cpp‎

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -70,72 +70,25 @@ void resize_to_dim_order_copy_node(
7070
graph->virtual_resize(out, in_sizes);
7171
}
7272

73-
void add_view_node(
74-
ComputeGraph& graph,
75-
ValueRef in,
76-
ValueRef sizes,
77-
ValueRef out) {
78-
std::string kernel_name = "view";
79-
kernel_name.reserve(kShaderNameReserve);
80-
add_dtype_suffix(kernel_name, graph.dtype_of(out));
81-
82-
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
83-
graph,
84-
VK_KERNEL_FROM_STR(kernel_name),
85-
default_pick_global_wg_size,
86-
default_pick_local_wg_size,
87-
// Inputs and Outputs
88-
{{out, vkapi::MemoryAccessType::WRITE},
89-
{in, vkapi::MemoryAccessType::READ}},
90-
// Parameter Buffers
91-
{},
92-
// Push Constants
93-
{{graph.sizes_pc_of(out), graph.sizes_pc_of(in)}},
94-
// Specialization Constants
95-
{graph.packed_dim_of(in), graph.packed_dim_of(out)},
96-
// Resize Args
97-
{sizes},
98-
// Resizing Logic
99-
resize_view_node));
100-
}
101-
102-
void add_view_copy_buffer_node(
73+
void add_view_copy_node(
10374
ComputeGraph& graph,
10475
ValueRef in,
10576
ValueRef out,
10677
const std::vector<ValueRef>& resize_args,
10778
const ExecuteNode::ResizeFunction& resize_fn) {
108-
std::string kernel_name = "view_buffer";
79+
std::string kernel_name =
80+
graph.is_buffer_storage(out) ? "view_buffer" : "view_texture";
81+
kernel_name.reserve(kShaderNameReserve);
10982
add_dtype_suffix(kernel_name, graph.dtype_of(out));
11083

111-
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
112-
graph,
113-
VK_KERNEL_FROM_STR(kernel_name),
114-
default_pick_global_wg_size,
115-
default_pick_local_wg_size,
116-
// Inputs and Outputs
117-
{{out, vkapi::kWrite}, {in, vkapi::kRead}},
118-
// Parameter Buffers
119-
{graph.buffer_meta_ubo(out), graph.buffer_meta_ubo(in)},
120-
// Push Constants
121-
{},
122-
// Specialization Constants
123-
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)},
124-
// Resize Args
125-
resize_args,
126-
// Resizing Logic
127-
resize_fn));
128-
}
84+
vkapi::ParamsBindList ubos = {graph.meta_ubo(out), graph.meta_ubo(in)};
12985

130-
void add_view_copy_convert_texture_node(
131-
ComputeGraph& graph,
132-
ValueRef in,
133-
ValueRef out,
134-
const std::vector<ValueRef>& resize_args,
135-
const ExecuteNode::ResizeFunction& resize_fn) {
136-
std::string kernel_name = "view_convert_texture";
137-
add_dtype_suffix(kernel_name, graph.dtype_of(in));
138-
add_dtype_suffix(kernel_name, graph.dtype_of(out));
86+
// Spec constant order differs between buffer and texture shaders.
87+
vkapi::SpecVarList spec_constants = graph.is_buffer_storage(out)
88+
? vkapi::
89+
SpecVarList{graph.hashed_layout_of(out), graph.hashed_layout_of(in)}
90+
: vkapi::SpecVarList{
91+
graph.hashed_layout_of(in), graph.hashed_layout_of(out)};
13992

14093
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
14194
graph,
@@ -145,24 +98,37 @@ void add_view_copy_convert_texture_node(
14598
// Inputs and Outputs
14699
{{out, vkapi::kWrite}, {in, vkapi::kRead}},
147100
// Parameter Buffers
148-
{},
101+
ubos,
149102
// Push Constants
150-
{{graph.sizes_pc_of(out)}},
103+
{},
151104
// Specialization Constants
152-
{graph.packed_dim_of(out)},
105+
spec_constants,
153106
// Resize Args
154107
resize_args,
155108
// Resizing Logic
156109
resize_fn));
157110
}
158111

159-
void add_view_copy_convert_buffer_node(
112+
void add_view_copy_convert_node(
160113
ComputeGraph& graph,
161114
ValueRef in,
162115
ValueRef out,
163116
const std::vector<ValueRef>& resize_args,
164117
const ExecuteNode::ResizeFunction& resize_fn) {
165-
std::string kernel_name = "view_convert_buffer";
118+
std::string kernel_name;
119+
vkapi::ParamsBindList ubos;
120+
std::vector<PushConstantDataInfo> push_constants;
121+
vkapi::SpecVarList spec_constants;
122+
123+
if (graph.is_buffer_storage(out)) {
124+
kernel_name = "view_convert_buffer";
125+
ubos = {graph.buffer_meta_ubo(out), graph.buffer_meta_ubo(in)};
126+
spec_constants = {graph.hashed_layout_of(out), graph.hashed_layout_of(in)};
127+
} else {
128+
kernel_name = "view_convert_texture";
129+
push_constants = {graph.sizes_pc_of(out)};
130+
spec_constants = {graph.packed_dim_of(out)};
131+
}
166132
add_dtype_suffix(kernel_name, graph.dtype_of(in));
167133
add_dtype_suffix(kernel_name, graph.dtype_of(out));
168134

@@ -174,11 +140,11 @@ void add_view_copy_convert_buffer_node(
174140
// Inputs and Outputs
175141
{{out, vkapi::kWrite}, {in, vkapi::kRead}},
176142
// Parameter Buffers
177-
{graph.buffer_meta_ubo(out), graph.buffer_meta_ubo(in)},
143+
ubos,
178144
// Push Constants
179-
{},
145+
{push_constants},
180146
// Specialization Constants
181-
{graph.hashed_layout_of(out), graph.hashed_layout_of(in)},
147+
spec_constants,
182148
// Resize Args
183149
resize_args,
184150
// Resizing Logic
@@ -191,13 +157,7 @@ void view(ComputeGraph& graph, const std::vector<ValueRef>& args) {
191157
const ValueRef sizes = args.at(idx++);
192158
const ValueRef out = args.at(idx++);
193159

194-
std::vector<ValueRef> resize_args = {sizes};
195-
196-
if (graph.is_buffer_storage(out)) {
197-
return add_view_copy_buffer_node(
198-
graph, in, out, resize_args, resize_view_node);
199-
}
200-
return add_view_node(graph, in, sizes, out);
160+
return add_view_copy_node(graph, in, out, {sizes}, resize_view_node);
201161
}
202162

203163
void to_dim_order_copy(ComputeGraph& graph, const std::vector<ValueRef>& args) {
@@ -221,11 +181,11 @@ void to_dim_order_copy(ComputeGraph& graph, const std::vector<ValueRef>& args) {
221181
VK_CHECK_COND(graph.is_buffer_storage(in) && graph.is_buffer_storage(out));
222182

223183
if (graph.dtype_of(in) == graph.dtype_of(out)) {
224-
return add_view_copy_buffer_node(
184+
return add_view_copy_node(
225185
graph, in, out, {}, resize_to_dim_order_copy_node);
226186
}
227187

228-
return add_view_copy_convert_buffer_node(
188+
return add_view_copy_convert_node(
229189
graph, in, out, {}, resize_to_dim_order_copy_node);
230190
}
231191

0 commit comments

Comments
 (0)