|
| 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/DynamicDispatchNode.h> |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h> |
| 14 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h> |
| 15 | + |
| 16 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h> |
| 17 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 18 | + |
| 19 | +namespace vkcompute { |
| 20 | + |
| 21 | +using namespace utils; |
| 22 | + |
| 23 | +utils::uvec3 fused_ce_global_wg_size( |
| 24 | + ComputeGraph* graph, |
| 25 | + const vkapi::ShaderInfo& shader, |
| 26 | + const std::vector<ArgGroup>& args, |
| 27 | + const std::vector<ValueRef>& resize_args) { |
| 28 | + (void)shader; |
| 29 | + (void)resize_args; |
| 30 | + const ValueRef loss_partial = args.at(0).refs.at(1); |
| 31 | + return { |
| 32 | + 1u, utils::safe_downcast<uint32_t>(graph->numel_of(loss_partial)), 1u}; |
| 33 | +} |
| 34 | + |
| 35 | +utils::uvec3 fused_ce_local_wg_size( |
| 36 | + ComputeGraph* graph, |
| 37 | + const vkapi::ShaderInfo& shader, |
| 38 | + const utils::uvec3& global_workgroup_size, |
| 39 | + const std::vector<ArgGroup>& args, |
| 40 | + const std::vector<ValueRef>& resize_args) { |
| 41 | + (void)graph; |
| 42 | + (void)shader; |
| 43 | + (void)global_workgroup_size; |
| 44 | + (void)args; |
| 45 | + (void)resize_args; |
| 46 | + return {64u, 1u, 1u}; |
| 47 | +} |
| 48 | + |
| 49 | +utils::uvec3 fused_ce_sum_global_wg_size( |
| 50 | + ComputeGraph* graph, |
| 51 | + const vkapi::ShaderInfo& shader, |
| 52 | + const std::vector<ArgGroup>& args, |
| 53 | + const std::vector<ValueRef>& resize_args) { |
| 54 | + (void)graph; |
| 55 | + (void)shader; |
| 56 | + (void)args; |
| 57 | + (void)resize_args; |
| 58 | + return {1u, 1u, 1u}; |
| 59 | +} |
| 60 | + |
| 61 | +utils::uvec3 fused_ce_sum_local_wg_size( |
| 62 | + ComputeGraph* graph, |
| 63 | + const vkapi::ShaderInfo& shader, |
| 64 | + const utils::uvec3& global_workgroup_size, |
| 65 | + const std::vector<ArgGroup>& args, |
| 66 | + const std::vector<ValueRef>& resize_args) { |
| 67 | + (void)graph; |
| 68 | + (void)shader; |
| 69 | + (void)global_workgroup_size; |
| 70 | + (void)args; |
| 71 | + (void)resize_args; |
| 72 | + return {64u, 1u, 1u}; |
| 73 | +} |
| 74 | + |
| 75 | +void resize_fused_ce_node( |
| 76 | + ComputeGraph* graph, |
| 77 | + const std::vector<ArgGroup>& args, |
| 78 | + const std::vector<ValueRef>& resize_args) { |
| 79 | + (void)resize_args; |
| 80 | + const ValueRef dlogits = args.at(0).refs.at(0); |
| 81 | + const ValueRef loss_partial = args.at(0).refs.at(1); |
| 82 | + const ValueRef logits = args.at(1).refs.at(0); |
| 83 | + |
| 84 | + const std::vector<int64_t> logits_sizes = graph->sizes_of(logits); |
| 85 | + graph->virtual_resize(dlogits, logits_sizes); |
| 86 | + graph->virtual_resize(loss_partial, {logits_sizes.at(0)}); |
| 87 | +} |
| 88 | + |
| 89 | +void resize_fused_ce_sum_node( |
| 90 | + ComputeGraph* graph, |
| 91 | + const std::vector<ArgGroup>& args, |
| 92 | + const std::vector<ValueRef>& resize_args) { |
| 93 | + (void)resize_args; |
| 94 | + const ValueRef loss = args.at(0).refs.at(0); |
| 95 | + // Rank-0 scalar, matching fused_ce_meta's logits.new_empty([]). |
| 96 | + graph->virtual_resize(loss, {}); |
| 97 | +} |
| 98 | + |
| 99 | +void fused_ce(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 100 | + int arg_idx = 0; |
| 101 | + const ValueRef logits = args[arg_idx++]; |
| 102 | + const ValueRef labels = args[arg_idx++]; |
| 103 | + const ValueRef n_valid_ref = args[arg_idx++]; |
| 104 | + const ValueRef out_tuple_ref = args[arg_idx]; |
| 105 | + |
| 106 | + // Read loss/dlogits via short-lived get_value_list() temporaries: no get_*() |
| 107 | + // pointer may be alive when loss_partial is added below, else adding Values |
| 108 | + // can reallocate values_ and check_no_active_value_ptrs() throws. |
| 109 | + const ValueRef loss = graph.get_value_list(out_tuple_ref)->at(0); |
| 110 | + const ValueRef dlogits = graph.get_value_list(out_tuple_ref)->at(1); |
| 111 | + |
| 112 | + VK_CHECK_COND( |
| 113 | + graph.is_buffer_storage(logits) && graph.is_buffer_storage(dlogits), |
| 114 | + "fused_ce: logits and dlogits must use buffer storage"); |
| 115 | + VK_CHECK_COND( |
| 116 | + graph.dim_of(logits) == 2, "fused_ce: logits must be 2D [n_rows, vocab]"); |
| 117 | + VK_CHECK_COND( |
| 118 | + graph.sizes_of(dlogits) == graph.sizes_of(logits), |
| 119 | + "fused_ce: dlogits must match logits shape"); |
| 120 | + VK_CHECK_COND( |
| 121 | + graph.dtype_of(labels) == vkapi::kInt, "fused_ce: labels must be int32"); |
| 122 | + VK_CHECK_COND(graph.dim_of(labels) == 1, "fused_ce: labels must be 1D [N]"); |
| 123 | + VK_CHECK_COND( |
| 124 | + graph.size_at<int64_t>(0, labels) == graph.size_at<int64_t>(0, logits), |
| 125 | + "fused_ce: labels length must equal number of rows"); |
| 126 | + VK_CHECK_COND(graph.numel_of(loss) == 1, "fused_ce: loss must be a scalar"); |
| 127 | + |
| 128 | + const int32_t n_rows = graph.size_at<int32_t>(0, logits); |
| 129 | + const int32_t vocab = graph.size_at<int32_t>(1, logits); |
| 130 | + const float n_valid = graph.extract_scalar<float>(n_valid_ref); |
| 131 | + |
| 132 | + // One workgroup per row; the Vulkan spec guarantees maxComputeWorkGroupCount |
| 133 | + // >= 65535 per dimension. |
| 134 | + VK_CHECK_COND( |
| 135 | + n_rows <= 65535, "fused_ce: n_rows exceeds max workgroup count"); |
| 136 | + |
| 137 | + // Per-row loss contributions, reduced to the scalar loss by the sum node. |
| 138 | + TmpTensor loss_partial( |
| 139 | + &graph, {n_rows}, graph.dtype_of(logits), utils::kBuffer); |
| 140 | + |
| 141 | + std::string kernel_name = "fused_ce"; |
| 142 | + kernel_name.reserve(kShaderNameReserve); |
| 143 | + add_storage_type_suffix(kernel_name, graph.storage_type_of(dlogits)); |
| 144 | + add_dtype_suffix(kernel_name, graph.dtype_of(dlogits)); |
| 145 | + |
| 146 | + graph.execute_nodes().emplace_back(new DynamicDispatchNode( |
| 147 | + graph, |
| 148 | + VK_KERNEL_FROM_STR(kernel_name), |
| 149 | + fused_ce_global_wg_size, |
| 150 | + fused_ce_local_wg_size, |
| 151 | + // Inputs and Outputs |
| 152 | + {{{dlogits, loss_partial}, vkapi::kWrite}, |
| 153 | + {{logits, labels}, vkapi::kRead}}, |
| 154 | + // Shader params buffers |
| 155 | + {graph.create_params_buffer(vocab), |
| 156 | + graph.create_params_buffer(n_rows), |
| 157 | + graph.create_params_buffer(n_valid)}, |
| 158 | + // Push Constants |
| 159 | + {}, |
| 160 | + // Specialization Constants |
| 161 | + {}, |
| 162 | + // Resize Args |
| 163 | + {}, |
| 164 | + // Resizing Logic |
| 165 | + resize_fused_ce_node)); |
| 166 | + |
| 167 | + std::string sum_kernel_name = "fused_ce_sum"; |
| 168 | + sum_kernel_name.reserve(kShaderNameReserve); |
| 169 | + add_storage_type_suffix(sum_kernel_name, graph.storage_type_of(loss)); |
| 170 | + add_dtype_suffix(sum_kernel_name, graph.dtype_of(loss)); |
| 171 | + |
| 172 | + // Separate dispatch node so the loss_partial write (above) is ordered before |
| 173 | + // this read: the graph inserts a per-tensor pipeline barrier between them. |
| 174 | + graph.execute_nodes().emplace_back(new DynamicDispatchNode( |
| 175 | + graph, |
| 176 | + VK_KERNEL_FROM_STR(sum_kernel_name), |
| 177 | + fused_ce_sum_global_wg_size, |
| 178 | + fused_ce_sum_local_wg_size, |
| 179 | + // Inputs and Outputs |
| 180 | + {{loss, vkapi::kWrite}, {loss_partial, vkapi::kRead}}, |
| 181 | + // Shader params buffers |
| 182 | + {graph.create_params_buffer(n_rows)}, |
| 183 | + // Push Constants |
| 184 | + {}, |
| 185 | + // Specialization Constants |
| 186 | + {}, |
| 187 | + // Resize Args |
| 188 | + {}, |
| 189 | + // Resizing Logic |
| 190 | + resize_fused_ce_sum_node)); |
| 191 | +} |
| 192 | + |
| 193 | +REGISTER_OPERATORS { |
| 194 | + VK_REGISTER_OP(et_vk.fused_ce.default, fused_ce); |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace vkcompute |
0 commit comments