From 83b59e64ee8ad8284d4a6f52bbea1f4ceadbc676 Mon Sep 17 00:00:00 2001 From: Vivek Trivedi <5340687+trivedivivek@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:38:50 -0800 Subject: [PATCH] [ET-VK] Replace Uniform buffers with push constants for binary op This diff replaces uniform buffers with push constants for binary op in the Vulkan backend of Executorch. The changes include updating the GLSL code to use push constants instead of uniform buffers and updating the C++ code to pass the sizes as push constants to the shader. Differential Revision: [D66853542](https://our.internmc.facebook.com/intern/diff/D66853542/) [ghstack-poisoned] --- .../runtime/graph/ops/glsl/binary_op.glsl | 13 ++++++++----- .../vulkan/runtime/graph/ops/impl/BinaryOp.cpp | 17 ++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/backends/vulkan/runtime/graph/ops/glsl/binary_op.glsl b/backends/vulkan/runtime/graph/ops/glsl/binary_op.glsl index be0e1bfa20a..62aa2f810dc 100644 --- a/backends/vulkan/runtime/graph/ops/glsl/binary_op.glsl +++ b/backends/vulkan/runtime/graph/ops/glsl/binary_op.glsl @@ -19,11 +19,6 @@ layout(std430) buffer; ${layout_declare_tensor(B, "w", "t_out", DTYPE, STORAGE)} ${layout_declare_tensor(B, "r", "t_in", DTYPE, STORAGE)} ${layout_declare_tensor(B, "r", "t_other", DTYPE, STORAGE)} -${layout_declare_ubo(B, "ivec4", "out_sizes")} -${layout_declare_ubo(B, "ivec4", "in_sizes")} -${layout_declare_ubo(B, "ivec4", "other_sizes")} -${layout_declare_ubo(B, "ivec2", "broadcast_params")} -${layout_declare_ubo(B, "float", "alpha")} #include "broadcasting_utils.h" #include "indexing_utils.h" @@ -40,6 +35,14 @@ const lowp ivec4 in_axis_map = unhash_axis_map(in_layout); ${layout_declare_spec_const(C, "int", "other_layout", "DEFAULT_LAYOUT")} const lowp ivec4 other_axis_map = unhash_axis_map(other_layout); +layout(push_constant) uniform restrict Block { + ivec4 out_sizes; + ivec4 in_sizes; + ivec4 other_sizes; + ivec2 broadcast_params; + float alpha; +}; + void main() { const ivec3 lpos = ivec3(gl_GlobalInvocationID); const ivec4 tidx = lpos_to_tidx(lpos, out_sizes, out_axis_map.w, packed_dim); diff --git a/backends/vulkan/runtime/graph/ops/impl/BinaryOp.cpp b/backends/vulkan/runtime/graph/ops/impl/BinaryOp.cpp index 33f73cd6dad..7e88982aaee 100644 --- a/backends/vulkan/runtime/graph/ops/impl/BinaryOp.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/BinaryOp.cpp @@ -67,7 +67,10 @@ void add_binary_op_node( alpha_val = graph.extract_scalar(alpha); } - const utils::ivec2 broadcast_params = create_broadcast_params(*t_in1, *t_in2); + const struct BinaryOpsParams { + const utils::ivec2 broadcast_params; + const float alpha_val; + } binary_ops_params{create_broadcast_params(*t_in1, *t_in2), alpha_val}; std::string kernel_name("binary_"); kernel_name.reserve(kShaderNameReserve); @@ -83,16 +86,16 @@ void add_binary_op_node( {{out, vkapi::MemoryAccessType::WRITE}, {{arg1, arg2}, vkapi::MemoryAccessType::READ}}, // Shader params buffers - {t_out->sizes_ubo(), - t_in1->sizes_ubo(), - t_in2->sizes_ubo(), - graph.create_params_buffer(broadcast_params), - graph.create_params_buffer(alpha_val)}, + {}, // Specialization Constants {t_out->hashed_layout(), t_in1->hashed_layout(), t_in2->hashed_layout()}, // Resizing Logic resize_binary_op_node, - {})); + {}, + {{graph.sizes_pc_of(out), + graph.sizes_pc_of(arg1), + graph.sizes_pc_of(arg2), + PushConstantDataInfo(&binary_ops_params, sizeof(binary_ops_params))}})); } #define DEFINE_BINARY_OP_WITH_ALPHA_FN(op_name) \