|
| 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/Staging.h> |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/KernelUtils.h> |
| 14 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h> |
| 15 | + |
| 16 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 17 | + |
| 18 | +namespace vkcompute { |
| 19 | + |
| 20 | +std::vector<int64_t> calc_out_mean_sizes( |
| 21 | + vTensor& self, |
| 22 | + int64_t normalized_shape_dim) { |
| 23 | + std::vector<int64_t> output_size = self.sizes(); |
| 24 | + int64_t self_dim = self.sizes().size(); |
| 25 | + for (int64_t i = 0; i < normalized_shape_dim; ++i) { |
| 26 | + output_size.at(self_dim - i - 1) = 1; |
| 27 | + } |
| 28 | + return output_size; |
| 29 | +} |
| 30 | + |
| 31 | +void resize_native_layer_norm_node( |
| 32 | + ComputeGraph* graph, |
| 33 | + const std::vector<ArgGroup>& args, |
| 34 | + const std::vector<ValueRef>& extra_args) { |
| 35 | + vTensorPtr out = graph->get_tensor(args[0].refs[0]); |
| 36 | + vTensorPtr mean = graph->get_tensor(args[0].refs[1]); |
| 37 | + vTensorPtr rstd = graph->get_tensor(args[0].refs[2]); |
| 38 | + vTensorPtr in = graph->get_tensor(args[1].refs[0]); |
| 39 | + std::vector<int64_t> in_sizes = in->sizes(); |
| 40 | + |
| 41 | + const auto normalized_shape_dim = graph->get_int_list(extra_args[0]).size(); |
| 42 | + |
| 43 | + std::vector<int64_t> mean_size = |
| 44 | + calc_out_mean_sizes(*in, normalized_shape_dim); |
| 45 | + |
| 46 | + out->virtual_resize(in_sizes); |
| 47 | + mean->virtual_resize(mean_size); |
| 48 | + rstd->virtual_resize(mean_size); |
| 49 | +} |
| 50 | + |
| 51 | +void check_args(const vTensor& in, const vTensor& out) { |
| 52 | + VK_CHECK_COND(check_memory_layout_is(in, api::kChannelsPacked)); |
| 53 | + VK_CHECK_COND(check_memory_layout_is(out, api::kChannelsPacked)); |
| 54 | +} |
| 55 | + |
| 56 | +void add_native_layer_norm_node( |
| 57 | + ComputeGraph& graph, |
| 58 | + const ValueRef in, |
| 59 | + const ValueRef normalized_shape, |
| 60 | + const ValueRef weight, |
| 61 | + const ValueRef bias, |
| 62 | + const ValueRef eps, |
| 63 | + const ValueRef out) { |
| 64 | + const auto normalized_shape_dim = graph.get_int_list(normalized_shape).size(); |
| 65 | + if (normalized_shape_dim > 1) { |
| 66 | + VK_THROW("native_layer_norm only supports normalized_shape with dim == 1"); |
| 67 | + } |
| 68 | + |
| 69 | + ValueRef arg_in = prepack_if_tensor_ref(graph, in); |
| 70 | + ValueRef arg_weight = |
| 71 | + prepack_if_tensor_ref(graph, weight, graph.memory_layout_of(arg_in)); |
| 72 | + ValueRef arg_bias = |
| 73 | + prepack_if_tensor_ref(graph, bias, graph.memory_layout_of(arg_in)); |
| 74 | + |
| 75 | + const auto& out_val = graph.get_value_list(out); |
| 76 | + vTensorPtr t_out = graph.get_tensor(out_val[0]); |
| 77 | + vTensorPtr t_mean = graph.get_tensor(out_val[1]); |
| 78 | + vTensorPtr t_input = graph.get_tensor(in); |
| 79 | + vTensorPtr t_weight = graph.get_tensor(weight); |
| 80 | + float epsilon = graph.extract_scalar<float>(eps); |
| 81 | + |
| 82 | + check_args(*t_input, *t_out); |
| 83 | + |
| 84 | + std::vector<int64_t> in_sizes = t_input->sizes(); |
| 85 | + |
| 86 | + api::utils::uvec3 global_size = t_mean->extents(); |
| 87 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 88 | + |
| 89 | + std::string kernel_name("native_layer_norm"); |
| 90 | + kernel_name.reserve(kShaderNameReserve); |
| 91 | + |
| 92 | + add_dtype_suffix(kernel_name, *t_out); |
| 93 | + |
| 94 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 95 | + graph, |
| 96 | + VK_KERNEL_FROM_STR(kernel_name), |
| 97 | + global_size, |
| 98 | + local_size, |
| 99 | + // Inputs and Outputs |
| 100 | + {{{out_val[0], out_val[1], out_val[2]}, api::MemoryAccessType::WRITE}, |
| 101 | + {{arg_in, arg_weight, arg_bias}, api::MemoryAccessType::READ}}, |
| 102 | + // Shader params buffers |
| 103 | + {t_out->gpu_sizes_ubo(), graph.create_params_buffer(epsilon)}, |
| 104 | + // Resizing |
| 105 | + resize_native_layer_norm_node, |
| 106 | + {normalized_shape})); |
| 107 | +} |
| 108 | + |
| 109 | +void native_layer_norm(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 110 | + return add_native_layer_norm_node( |
| 111 | + graph, args[0], args[1], args[2], args[3], args[4], args[5]); |
| 112 | +} |
| 113 | + |
| 114 | +REGISTER_OPERATORS { |
| 115 | + VK_REGISTER_OP(aten.native_layer_norm.default, native_layer_norm); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace vkcompute |
0 commit comments