|
| 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 | +#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h> |
| 10 | + |
| 11 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h> |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 14 | + |
| 15 | +namespace vkcompute { |
| 16 | + |
| 17 | +void resize_linear_dW_node( |
| 18 | + ComputeGraph* graph, |
| 19 | + const std::vector<ArgGroup>& args, |
| 20 | + const std::vector<ValueRef>& resize_args) { |
| 21 | + (void)resize_args; |
| 22 | + const ValueRef dW = args.at(0).refs.at(0); |
| 23 | + const ValueRef d_out = args.at(1).refs.at(0); |
| 24 | + const ValueRef x = args.at(1).refs.at(1); |
| 25 | + const int64_t N = graph->size_at<int64_t>(-1, d_out); |
| 26 | + const int64_t K = graph->size_at<int64_t>(-1, x); |
| 27 | + graph->virtual_resize(dW, {N, K}); |
| 28 | +} |
| 29 | + |
| 30 | +utils::uvec3 linear_dW_global_wg_size( |
| 31 | + ComputeGraph* graph, |
| 32 | + const vkapi::ShaderInfo& shader, |
| 33 | + const std::vector<ArgGroup>& args, |
| 34 | + const std::vector<ValueRef>& resize_args) { |
| 35 | + (void)shader; |
| 36 | + (void)resize_args; |
| 37 | + const ValueRef dW = args.at(0).refs.at(0); |
| 38 | + const uint32_t N = graph->size_at<uint32_t>(-2, dW); |
| 39 | + const uint32_t K = graph->size_at<uint32_t>(-1, dW); |
| 40 | + const uint32_t tiles = utils::div_up_4(N) * utils::div_up_4(K); |
| 41 | + return {tiles, 1u, 1u}; |
| 42 | +} |
| 43 | + |
| 44 | +utils::uvec3 linear_dW_local_wg_size( |
| 45 | + ComputeGraph* graph, |
| 46 | + const vkapi::ShaderInfo& shader, |
| 47 | + const utils::uvec3& global_workgroup_size, |
| 48 | + const std::vector<ArgGroup>& args, |
| 49 | + const std::vector<ValueRef>& resize_args) { |
| 50 | + (void)graph; |
| 51 | + (void)shader; |
| 52 | + (void)global_workgroup_size; |
| 53 | + (void)args; |
| 54 | + (void)resize_args; |
| 55 | + return {64u, 1u, 1u}; |
| 56 | +} |
| 57 | + |
| 58 | +void linear_dW(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 59 | + int32_t i = 0; |
| 60 | + const ValueRef d_out = args.at(i++); |
| 61 | + const ValueRef x = args.at(i++); |
| 62 | + const ValueRef dW = args.at(i); |
| 63 | + |
| 64 | + VK_CHECK_COND(graph.dtype_of(d_out) == vkapi::kFloat); |
| 65 | + VK_CHECK_COND(graph.dtype_of(x) == vkapi::kFloat); |
| 66 | + VK_CHECK_COND(graph.dtype_of(dW) == vkapi::kFloat); |
| 67 | + VK_CHECK_COND(graph.is_buffer_storage(d_out)); |
| 68 | + VK_CHECK_COND(graph.is_buffer_storage(x)); |
| 69 | + VK_CHECK_COND(graph.is_buffer_storage(dW)); |
| 70 | + |
| 71 | + const int64_t N = graph.size_at<int64_t>(-1, d_out); |
| 72 | + const int64_t K = graph.size_at<int64_t>(-1, x); |
| 73 | + VK_CHECK_COND(N > 0 && K > 0); |
| 74 | + VK_CHECK_COND(graph.numel_of(d_out) % N == 0); |
| 75 | + VK_CHECK_COND(graph.numel_of(x) % K == 0); |
| 76 | + VK_CHECK_COND(graph.numel_of(d_out) / N == graph.numel_of(x) / K); |
| 77 | + |
| 78 | + const uint32_t tiles = utils::div_up_4(static_cast<uint32_t>(N)) * |
| 79 | + utils::div_up_4(static_cast<uint32_t>(K)); |
| 80 | + VK_CHECK_COND( |
| 81 | + (tiles + 63u) / 64u <= 65535u, |
| 82 | + "linear_dW: tile count exceeds max workgroup count"); |
| 83 | + |
| 84 | + std::string kernel_name = "linear_dW"; |
| 85 | + kernel_name.reserve(kShaderNameReserve); |
| 86 | + add_storage_type_suffix(kernel_name, graph.storage_type_of(dW)); |
| 87 | + add_dtype_suffix(kernel_name, graph.dtype_of(dW)); |
| 88 | + |
| 89 | + graph.execute_nodes().emplace_back(new DynamicDispatchNode( |
| 90 | + graph, |
| 91 | + VK_KERNEL_FROM_STR(kernel_name), |
| 92 | + linear_dW_global_wg_size, |
| 93 | + linear_dW_local_wg_size, |
| 94 | + // Inputs and Outputs |
| 95 | + {{dW, vkapi::kWrite}, {{d_out, x}, vkapi::kRead}}, |
| 96 | + // Shader params buffers |
| 97 | + {graph.sizes_ubo(d_out), graph.sizes_ubo(x)}, |
| 98 | + // Push Constants |
| 99 | + {}, |
| 100 | + // Specialization Constants |
| 101 | + {}, |
| 102 | + // Resize Args |
| 103 | + {}, |
| 104 | + // Resizing Logic |
| 105 | + resize_linear_dW_node)); |
| 106 | +} |
| 107 | + |
| 108 | +REGISTER_OPERATORS { |
| 109 | + VK_REGISTER_OP(et_vk.linear_dW.default, linear_dW); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace vkcompute |
0 commit comments