|
| 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 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Staging.h> |
| 13 | + |
| 14 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/KernelUtils.h> |
| 15 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h> |
| 16 | + |
| 17 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 18 | + |
| 19 | +#include <limits> |
| 20 | + |
| 21 | +namespace vkcompute { |
| 22 | + |
| 23 | +void resize_conv1d_dw_node( |
| 24 | + ComputeGraph* graph, |
| 25 | + const std::vector<ArgGroup>& args, |
| 26 | + const std::vector<ValueRef>& extra_args) { |
| 27 | + const ValueRef out = args.at(0).refs.at(0); |
| 28 | + const ValueRef self = args.at(1).refs.at(0); |
| 29 | + |
| 30 | + TensorRefPtr weight_ref = graph->get_tref(extra_args.at(0)); |
| 31 | + |
| 32 | + const int64_t stride = graph->get_int_list(extra_args.at(1))->at(0); |
| 33 | + const int64_t padding = graph->get_int_list(extra_args.at(2))->at(0); |
| 34 | + const int64_t dilation = graph->get_int_list(extra_args.at(3))->at(0); |
| 35 | + |
| 36 | + const std::vector<int64_t> in_sizes = graph->sizes_of(self); |
| 37 | + const int64_t kernel_size = weight_ref->sizes.at(2); |
| 38 | + const int64_t L_in = in_sizes.at(2); |
| 39 | + |
| 40 | + const int64_t L_out = |
| 41 | + calc_out_size(L_in, kernel_size, stride, padding, dilation, false); |
| 42 | + |
| 43 | + graph->virtual_resize(out, {in_sizes.at(0), in_sizes.at(1), L_out}); |
| 44 | +} |
| 45 | + |
| 46 | +struct Conv1dDWParams final { |
| 47 | + int32_t kernel_size; |
| 48 | + int32_t stride; |
| 49 | + int32_t padding; |
| 50 | + int32_t dilation; |
| 51 | +}; |
| 52 | + |
| 53 | +struct Conv1dDWClampParams final { |
| 54 | + float output_min; |
| 55 | + float output_max; |
| 56 | +}; |
| 57 | + |
| 58 | +utils::uvec3 pick_conv1d_dw_global_wg_size( |
| 59 | + ComputeGraph* graph, |
| 60 | + const vkapi::ShaderInfo& shader, |
| 61 | + const std::vector<ArgGroup>& args, |
| 62 | + const std::vector<ValueRef>& resize_args) { |
| 63 | + (void)shader; |
| 64 | + (void)resize_args; |
| 65 | + const ValueRef out = args.at(0).refs.at(0); |
| 66 | + |
| 67 | + // out is [N, C, L_out]; in WHCN: {L_out, C, N, 1} |
| 68 | + const uint32_t C = graph->size_at<uint32_t>(-2, out); |
| 69 | + const uint32_t L_out = graph->size_at<uint32_t>(-1, out); |
| 70 | + const uint32_t N = |
| 71 | + graph->dim_of(out) >= 3 ? graph->size_at<uint32_t>(-3, out) : 1; |
| 72 | + |
| 73 | + return {utils::div_up_4(C), L_out, N}; |
| 74 | +} |
| 75 | + |
| 76 | +void add_conv1d_dw_node( |
| 77 | + ComputeGraph& graph, |
| 78 | + const ValueRef in, |
| 79 | + const ValueRef weight_data, |
| 80 | + const ValueRef bias, |
| 81 | + const ValueRef stride_ref, |
| 82 | + const ValueRef padding_ref, |
| 83 | + const ValueRef dilation_ref, |
| 84 | + const ValueRef out, |
| 85 | + const float output_min = std::numeric_limits<float>::lowest(), |
| 86 | + const float output_max = std::numeric_limits<float>::max()) { |
| 87 | + VK_CHECK_COND(graph.packed_dim_of(in) == WHCN::kHeightDim); |
| 88 | + VK_CHECK_COND(graph.packed_dim_of(out) == WHCN::kHeightDim); |
| 89 | + |
| 90 | + const utils::StorageType storage_type = graph.storage_type_of(out); |
| 91 | + |
| 92 | + // Weight [C, 1, K] prepacked as channels-packed so each vec4 load gives |
| 93 | + // 4 channels at one kernel position. |
| 94 | + ValueRef packed_weight = prepack_standard( |
| 95 | + graph, weight_data, storage_type, utils::kChannelsPacked); |
| 96 | + |
| 97 | + bool has_bias = graph.val_is_not_none(bias); |
| 98 | + ValueRef packed_bias = kDummyValueRef; |
| 99 | + if (has_bias) { |
| 100 | + packed_bias = |
| 101 | + prepack_standard(graph, bias, storage_type, utils::kWidthPacked); |
| 102 | + } |
| 103 | + |
| 104 | + const auto stride_val = graph.get_int_list(stride_ref)->at(0); |
| 105 | + const auto padding_val = graph.get_int_list(padding_ref)->at(0); |
| 106 | + const auto dilation_val = graph.get_int_list(dilation_ref)->at(0); |
| 107 | + |
| 108 | + Conv1dDWParams params{ |
| 109 | + utils::safe_downcast<int32_t>(graph.get_tref(weight_data)->sizes.at(2)), |
| 110 | + utils::safe_downcast<int32_t>(stride_val), |
| 111 | + utils::safe_downcast<int32_t>(padding_val), |
| 112 | + utils::safe_downcast<int32_t>(dilation_val), |
| 113 | + }; |
| 114 | + |
| 115 | + Conv1dDWClampParams clamp_params{ |
| 116 | + output_min, |
| 117 | + output_max, |
| 118 | + }; |
| 119 | + |
| 120 | + std::string kernel_name = has_bias ? "conv1d_dw_bias" : "conv1d_dw"; |
| 121 | + kernel_name.reserve(kShaderNameReserve); |
| 122 | + add_storage_type_suffix(kernel_name, storage_type); |
| 123 | + add_dtype_suffix(kernel_name, graph.dtype_of(out)); |
| 124 | + |
| 125 | + std::vector<ValueRef> read_inputs = {in, packed_weight}; |
| 126 | + if (has_bias) { |
| 127 | + read_inputs.push_back(packed_bias); |
| 128 | + } |
| 129 | + |
| 130 | + graph.execute_nodes().emplace_back(new DynamicDispatchNode( |
| 131 | + graph, |
| 132 | + VK_KERNEL_FROM_STR(kernel_name), |
| 133 | + pick_conv1d_dw_global_wg_size, |
| 134 | + default_pick_local_wg_size, |
| 135 | + // Inputs and Outputs |
| 136 | + {{out, vkapi::kWrite}, {read_inputs, vkapi::kRead}}, |
| 137 | + // Shader params buffers |
| 138 | + {graph.sizes_ubo(in), graph.sizes_ubo(out)}, |
| 139 | + // Push Constants |
| 140 | + {PushConstantDataInfo(¶ms, sizeof(Conv1dDWParams)), |
| 141 | + PushConstantDataInfo(&clamp_params, sizeof(Conv1dDWClampParams))}, |
| 142 | + // Specialization Constants |
| 143 | + {}, |
| 144 | + // Resize Args |
| 145 | + {weight_data, stride_ref, padding_ref, dilation_ref}, |
| 146 | + // Resizing Logic |
| 147 | + resize_conv1d_dw_node)); |
| 148 | +} |
| 149 | + |
| 150 | +// Args: in, weight, bias, stride, padding, dilation, groups, |
| 151 | +// output_min, output_max, out |
| 152 | +// output_min and output_max may be kDummyValueRef (no clamp). |
| 153 | +void conv1d_dw(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 154 | + ValueRef in = args[0]; |
| 155 | + ValueRef weight = args[1]; |
| 156 | + ValueRef bias = args[2]; |
| 157 | + ValueRef stride = args[3]; |
| 158 | + ValueRef padding = args[4]; |
| 159 | + ValueRef dilation = args[5]; |
| 160 | + ValueRef out = args[9]; |
| 161 | + |
| 162 | + float output_min = std::numeric_limits<float>::lowest(); |
| 163 | + float output_max = std::numeric_limits<float>::max(); |
| 164 | + if (is_valid(args[7])) { |
| 165 | + output_min = graph.extract_scalar<float>(args[7]); |
| 166 | + } |
| 167 | + if (is_valid(args[8])) { |
| 168 | + output_max = graph.extract_scalar<float>(args[8]); |
| 169 | + } |
| 170 | + |
| 171 | + add_conv1d_dw_node( |
| 172 | + graph, |
| 173 | + in, |
| 174 | + weight, |
| 175 | + bias, |
| 176 | + stride, |
| 177 | + padding, |
| 178 | + dilation, |
| 179 | + out, |
| 180 | + output_min, |
| 181 | + output_max); |
| 182 | +} |
| 183 | + |
| 184 | +REGISTER_OPERATORS { |
| 185 | + VK_REGISTER_OP(et_vk.conv1d_dw.default, conv1d_dw); |
| 186 | +} |
| 187 | + |
| 188 | +} // namespace vkcompute |
0 commit comments