Skip to content

Commit 9d4727d

Browse files
jorgep31415facebook-github-bot
authored andcommitted
Fix binary_op int variant (#3745)
Summary: Pull Request resolved: #3745 We were using the wrong `sampler3D` meant for `float`/`half`, when we should use `isampler3D` for `int`. [The codegen has a `Dict` mapping](https://www.internalfb.com/code/fbsource/[ac566fa2c8cb64cceb5d47fe4a9c97241a73341d]/fbcode/executorch/backends/vulkan/runtime/api/gen_vulkan_spv.py?lines=58-71) to templatize this logic. Since we're already here, modernize to the new layout generation functions. ## Before ``` layout(set = 0, binding = 1) uniform PRECISION sampler3D image_in; layout(set = 0, binding = 2) uniform PRECISION sampler3D image_other; ``` Input values were read incorrectly and got wild results. ``` ET-VK tensor([[-2147483648, -2147483648, 0], [-2147483648, 0, 0]], dtype=torch.int32) Eager-mode tensor([[-123, -131, 182], [ -29, 24, 138]], dtype=torch.int32) ```` ## After ``` layout(set = 0, binding = 1) uniform PRECISION ${SAMPLER_T[NDIM][DTYPE]} image_in; layout(set = 0, binding = 2) uniform PRECISION ${SAMPLER_T[NDIM][DTYPE]} image_other; ``` OR modernize to the new layout generation functions, which already use and abstract `SAMPLER_T` to avoid the same future mistake. ``` ${layout_declare_tensor(1, "r", "t_in", DTYPE, STORAGE)} ${layout_declare_tensor(2, "r", "t_other", DTYPE, STORAGE)} ``` ghstack-source-id: 227761995 Reviewed By: nathanaelsee Differential Revision: D57731470 fbshipit-source-id: f233089e5b4ced6f3d47b90a9c84795cf62bb4de
1 parent b8f92df commit 9d4727d

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,14 @@
1919

2020
layout(std430) buffer;
2121

22-
layout(set = 0, binding = 0, ${IMAGE_FORMAT[DTYPE]}) uniform PRECISION restrict writeonly ${IMAGE_T[NDIM][DTYPE]} image_out;
23-
layout(set = 0, binding = 1) uniform PRECISION sampler3D image_in;
24-
layout(set = 0, binding = 2) uniform PRECISION sampler3D image_other;
25-
26-
layout(set = 0, binding = 3) uniform PRECISION restrict OutSizes {
27-
ivec4 out_sizes;
28-
};
29-
30-
layout(set = 0, binding = 4) uniform PRECISION restrict InSizes {
31-
ivec4 in_sizes;
32-
};
33-
34-
layout(set = 0, binding = 5) uniform PRECISION restrict OtherSizes {
35-
ivec4 other_sizes;
36-
};
37-
38-
layout(set = 0, binding = 6) uniform PRECISION restrict BroadcastParams {
39-
ivec2 broadcast_params;
40-
};
41-
42-
layout(set = 0, binding = 7) uniform PRECISION restrict Alpha {
43-
float alpha;
44-
};
22+
${layout_declare_tensor(0, "w", "t_out", DTYPE, STORAGE)}
23+
${layout_declare_tensor(1, "r", "t_in", DTYPE, STORAGE)}
24+
${layout_declare_tensor(2, "r", "t_other", DTYPE, STORAGE)}
25+
${layout_declare_ubo(3, "ivec4", "out_sizes")}
26+
${layout_declare_ubo(4, "ivec4", "in_sizes")}
27+
${layout_declare_ubo(5, "ivec4", "other_sizes")}
28+
${layout_declare_ubo(6, "ivec2", "broadcast_params")}
29+
${layout_declare_ubo(7, "float", "alpha")}
4530

4631
layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;
4732

@@ -57,13 +42,13 @@ void main() {
5742

5843
ivec4 in_idx = broadcast_indices(idx, in_sizes);
5944
VEC4_T in_texel = VEC4_T(texelFetch(
60-
image_in,
45+
t_in,
6146
to_texture_pos(in_idx, in_sizes, packed_dim),
6247
0));
6348

6449
ivec4 other_idx = broadcast_indices(idx, other_sizes);
6550
VEC4_T other_texel = VEC4_T(texelFetch(
66-
image_other,
51+
t_other,
6752
to_texture_pos(other_idx, other_sizes, packed_dim),
6853
0));
6954

@@ -75,5 +60,5 @@ void main() {
7560
other_texel = other_texel.xxxx;
7661
}
7762

78-
imageStore(image_out, pos, VEC4_T(op(in_texel, other_texel, alpha)));
63+
imageStore(t_out, pos, VEC4_T(op(in_texel, other_texel, alpha)));
7964
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ binary_op:
1010
NDIM: 3
1111
DTYPE: float
1212
PACKING: C_packed
13+
STORAGE: texture3d
1314
generate_variant_forall:
1415
DTYPE:
1516
- VALUE: half

backends/vulkan/test/test_vulkan_delegate.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ def forward(self, x, y, w):
202202

203203
self.lower_module_and_test_output(add_module, sample_inputs)
204204

205+
def test_vulkan_backend_add_int(self):
206+
class AddIntModule(torch.nn.Module):
207+
def __init__(self):
208+
super().__init__()
209+
210+
def forward(self, x, y):
211+
z = x + y
212+
return z
213+
214+
add_int_module = AddIntModule()
215+
sample_inputs = (
216+
torch.randint(low=-100, high=100, size=(2, 3), dtype=torch.int32),
217+
torch.randint(low=-100, high=100, size=(2, 3), dtype=torch.int32),
218+
)
219+
220+
self.lower_module_and_test_output(add_int_module, sample_inputs)
221+
205222
def test_vulkan_backend_zero_dim_tensor(self):
206223
class ZeroDimModule(torch.nn.Module):
207224
def __init__(self):

0 commit comments

Comments
 (0)