|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include <executorch/backends/native/runtime/vulkan/passes/LowerRMSNorm.h> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <cmath> |
| 11 | +#include <string_view> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include <executorch/backends/native/runtime/graph/Argument.h> |
| 15 | +#include <executorch/backends/native/runtime/graph/Node.h> |
| 16 | +#include <executorch/backends/native/runtime/graph/ScalarType.h> |
| 17 | +#include <executorch/backends/native/runtime/graph/Value.h> |
| 18 | +#include <executorch/backends/native/runtime/graph/utils/GraphUtils.h> |
| 19 | + |
| 20 | +namespace ptn::vulkan { |
| 21 | +namespace { |
| 22 | + |
| 23 | +constexpr std::string_view kAtenRMSNorm = "torch.ops.aten.rms_norm.default"; |
| 24 | +constexpr const char* kVulkanRMSNorm = "torch.ops.et_vk.rms_norm.default"; |
| 25 | + |
| 26 | +const Argument* |
| 27 | +get_arg(const Node& node, std::string_view name, size_t position) { |
| 28 | + const auto found = std::ranges::find_if( |
| 29 | + node.inputs, |
| 30 | + [name](const NamedArgument& input) { return input.name == name; }); |
| 31 | + if (found != node.inputs.end()) { |
| 32 | + return &found->arg; |
| 33 | + } |
| 34 | + return position < node.inputs.size() ? &node.inputs[position].arg : nullptr; |
| 35 | +} |
| 36 | + |
| 37 | +bool is_constant(const Value& value) { |
| 38 | + return value.role == ValueRole::Parameter || |
| 39 | + value.role == ValueRole::ConstantTensor; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +size_t lower_rms_norms(Graph& graph) { |
| 45 | + size_t count = 0; |
| 46 | + for (const NodeId node_id : graph.schedule) { |
| 47 | + Node& node = graph.node(node_id); |
| 48 | + if (!node.is_call() || node.target != kAtenRMSNorm || |
| 49 | + node.outputs.size() != 1 || !valid(node.outputs[0].value_id)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + const Argument* input = get_arg(node, "input", 0); |
| 54 | + const Argument* normalized_shape = get_arg(node, "normalized_shape", 1); |
| 55 | + const Argument* weight = get_arg(node, "weight", 2); |
| 56 | + const Argument* eps = get_arg(node, "eps", 3); |
| 57 | + if (input == nullptr || input->kind() != ArgKind::Tensor || |
| 58 | + normalized_shape == nullptr || |
| 59 | + normalized_shape->kind() != ArgKind::IntList || weight == nullptr || |
| 60 | + weight->kind() != ArgKind::Tensor || eps == nullptr || |
| 61 | + eps->kind() != ArgKind::Float || valid(eps->as_float().id) || |
| 62 | + !std::isfinite(eps->as_float().value) || eps->as_float().value < 0) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + const ValueId input_id = input->as_tensor().id; |
| 67 | + const ValueId weight_id = weight->as_tensor().id; |
| 68 | + const ValueId output_id = node.outputs[0].value_id; |
| 69 | + if (!valid(input_id) || !valid(weight_id)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + const Value& input_value = graph.value(input_id); |
| 73 | + const Value& weight_value = graph.value(weight_id); |
| 74 | + const Value& output_value = graph.value(output_id); |
| 75 | + if (!input_value.is_tensor() || !weight_value.is_tensor() || |
| 76 | + !output_value.is_tensor() || !is_constant(weight_value)) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const TensorMeta& input_meta = input_value.tensor_meta(); |
| 81 | + const TensorMeta& weight_meta = weight_value.tensor_meta(); |
| 82 | + const TensorMeta& output_meta = output_value.tensor_meta(); |
| 83 | + const IntListArg& shape = normalized_shape->as_int_list(); |
| 84 | + if (input_meta.sizes.empty() || !shape.ids.empty() || |
| 85 | + shape.values != std::vector<int64_t>{input_meta.sizes.back()} || |
| 86 | + weight_meta.sizes != shape.values || |
| 87 | + (input_meta.dtype != ScalarType::Float && |
| 88 | + input_meta.dtype != ScalarType::Half) || |
| 89 | + weight_meta.dtype != input_meta.dtype || |
| 90 | + output_meta.dtype != input_meta.dtype || |
| 91 | + output_meta.sizes != input_meta.sizes) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + const Argument input_arg = *input; |
| 96 | + const Argument weight_arg = *weight; |
| 97 | + const Argument eps_arg = *eps; |
| 98 | + node.target = kVulkanRMSNorm; |
| 99 | + node.inputs = { |
| 100 | + {.name = "input", .arg = input_arg}, |
| 101 | + {.name = "weight", .arg = weight_arg}, |
| 102 | + {.name = "epsilon", .arg = eps_arg}, |
| 103 | + }; |
| 104 | + ++count; |
| 105 | + } |
| 106 | + if (count > 0) { |
| 107 | + graph.rebuild_def_use(); |
| 108 | + validate_graph(graph); |
| 109 | + } |
| 110 | + return count; |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace ptn::vulkan |
0 commit comments