From fde4146f4eb32dc97a2e61081fc3f239772be33f Mon Sep 17 00:00:00 2001 From: Vivek Trivedi <5340687+trivedivivek@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:51:25 -0800 Subject: [PATCH] [ET-VK] Using shared memory to save position in conv2d dw output op. This diff introduces a change to conv2d dw op to save output positions in shared memory, which reduces register usage and improves performance. Differential Revision: [D68400890](https://our.internmc.facebook.com/intern/diff/D68400890/) [ghstack-poisoned] --- .../graph/ops/glsl/conv2d_dw_output_tile.glsl | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backends/vulkan/runtime/graph/ops/glsl/conv2d_dw_output_tile.glsl b/backends/vulkan/runtime/graph/ops/glsl/conv2d_dw_output_tile.glsl index c05c7e4450d..a96c102ead1 100644 --- a/backends/vulkan/runtime/graph/ops/glsl/conv2d_dw_output_tile.glsl +++ b/backends/vulkan/runtime/graph/ops/glsl/conv2d_dw_output_tile.glsl @@ -38,6 +38,13 @@ ${layout_declare_ubo(8, "float", "out_min", "float", "out_max")} layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in; +// macro to offset shared memory access index. Padding position index by 1 offset per 16 positions avoidd bank access conflict and thus improves performance. +#define offset_pos_index(index) (index + ((index) >> 4)) + +// shared memory to hold calculated positions, this would reduce register usage thus improving performance. +// 64 is the number of threads in the local wg +shared ivec3 pos_shared[offset_pos_index(64)]; + /* * Computes a depthwise convolution. Each shader invocation calculates the * output at a single output location. @@ -63,6 +70,8 @@ void main() { return; } + pos_shared[offset_pos_index(gl_LocalInvocationIndex)] = pos; + // Compute the index of the top-left element of the overlay region. Negative // indices indicate that the top-left element is in a region added by padding. const ivec2 ipos = pos.xy * stride - padding; @@ -109,18 +118,19 @@ void main() { for (int j = 0; j < TILE_SIZE; j++, kx++) { prev_kernel_line[j] = texelFetch(t_kernel, ivec2(kx, pos.z), 0); for (int s = 0; s < BATCH_SIZE_X; s++) { - sum[0][s] = fma(in_texels[j + s], prev_kernel_line[j], sum[0][s]); + sum[0][s] = fma(in_texels[j + s], prev_kernel_line[j], sum[0][s]); } } } } + const ivec3 out_pos = pos_shared[offset_pos_index(gl_LocalInvocationIndex)]; for (int y = 0; y < BATCH_SIZE_Y; y++) { for (int x = 0; x < BATCH_SIZE_X; x++) { - if (any(greaterThanEqual(ivec3(pos.x + x, pos.y + y, pos.z), out_limits))) { + if (any(greaterThanEqual(ivec3(out_pos.x + x, out_pos.y + y, out_pos.z), out_limits))) { continue; } - imageStore(t_out, ivec3(pos.x + x, pos.y + y, pos.z), op(sum[y][x], out_min, out_max)); + imageStore(t_out, ivec3(out_pos.x + x, out_pos.y + y, out_pos.z), op(sum[y][x], out_min, out_max)); } } }