|
| 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/api/Utils.h> |
| 10 | + |
| 11 | +#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h> |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h> |
| 14 | + |
| 15 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 16 | + |
| 17 | +namespace vkcompute { |
| 18 | + |
| 19 | +void resize_arange_node( |
| 20 | + ComputeGraph* graph, |
| 21 | + const std::vector<ArgGroup>& args, |
| 22 | + const std::vector<ValueRef>& extra_args) { |
| 23 | + vTensorPtr out = graph->get_tensor(args[0].refs[0]); |
| 24 | + |
| 25 | + int start_val = 0; |
| 26 | + int step_val = 1; |
| 27 | + if (!graph->val_is_none(extra_args[0])) { |
| 28 | + start_val = graph->extract_scalar<int64_t>(extra_args[0]); |
| 29 | + } |
| 30 | + int end_val = graph->extract_scalar<int64_t>(extra_args[1]); |
| 31 | + if (!graph->val_is_none(extra_args[2])) { |
| 32 | + step_val = graph->extract_scalar<int64_t>(extra_args[2]); |
| 33 | + } |
| 34 | + |
| 35 | + std::vector<int64_t> out_sizes = { |
| 36 | + api::utils::div_up(end_val - start_val, step_val)}; |
| 37 | + |
| 38 | + out->virtual_resize(out_sizes); |
| 39 | +} |
| 40 | + |
| 41 | +void check_arange_input( |
| 42 | + ComputeGraph& graph, |
| 43 | + const ValueRef start, |
| 44 | + const ValueRef end, |
| 45 | + const ValueRef step) { |
| 46 | + if (!graph.val_is_none(start) && !graph.val_is_int(end)) { |
| 47 | + VK_THROW("arange: start must be int!"); |
| 48 | + } |
| 49 | + if (!graph.val_is_none(end) && !graph.val_is_int(end)) { |
| 50 | + VK_THROW("arange: end must be int!"); |
| 51 | + } |
| 52 | + if (!graph.val_is_none(step) && !graph.val_is_int(end)) { |
| 53 | + VK_THROW("arange: step must be int!"); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void add_arange_node( |
| 58 | + ComputeGraph& graph, |
| 59 | + const ValueRef start, |
| 60 | + const ValueRef end, |
| 61 | + const ValueRef step, |
| 62 | + const ValueRef out) { |
| 63 | + float start_val = 0.0f; |
| 64 | + float step_val = 1.0f; |
| 65 | + |
| 66 | + if (graph.val_is_none(end)) { |
| 67 | + VK_THROW("arange: end must be specified!"); |
| 68 | + } |
| 69 | + |
| 70 | + if (!graph.val_is_none(start)) { |
| 71 | + if (graph.val_is_int(start)) { |
| 72 | + start_val = static_cast<float>(graph.extract_scalar<int64_t>(start)); |
| 73 | + } else { |
| 74 | + start_val = graph.extract_scalar<float>(start); |
| 75 | + } |
| 76 | + } |
| 77 | + if (!graph.val_is_none(step)) { |
| 78 | + if (graph.val_is_int(step)) { |
| 79 | + step_val = static_cast<float>(graph.extract_scalar<int64_t>(step)); |
| 80 | + } else { |
| 81 | + step_val = graph.extract_scalar<float>(step); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + vTensorPtr t_out = graph.get_tensor(out); |
| 86 | + |
| 87 | + api::utils::uvec3 global_size = t_out->image_extents(); |
| 88 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 89 | + |
| 90 | + std::string kernel_name("arange"); |
| 91 | + kernel_name.reserve(kShaderNameReserve); |
| 92 | + |
| 93 | + add_dtype_suffix(kernel_name, *t_out); |
| 94 | + |
| 95 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 96 | + graph, |
| 97 | + VK_KERNEL_FROM_STR(kernel_name), |
| 98 | + global_size, |
| 99 | + local_size, |
| 100 | + // Inputs and Outputs |
| 101 | + {{out, api::MemoryAccessType::WRITE}}, |
| 102 | + // Shader params buffers |
| 103 | + {t_out->sizes_ubo(), |
| 104 | + graph.create_params_buffer(start_val), |
| 105 | + graph.create_params_buffer(step_val)}, |
| 106 | + // Specialization Constants |
| 107 | + {}, |
| 108 | + // Resizing Logic |
| 109 | + resize_arange_node, |
| 110 | + {start, end, step})); |
| 111 | +} |
| 112 | + |
| 113 | +void arange(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 114 | + return add_arange_node(graph, args[0], args[1], args[2], args[7]); |
| 115 | +} |
| 116 | + |
| 117 | +REGISTER_OPERATORS { |
| 118 | + VK_REGISTER_OP(aten.arange.start_step, arange); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace vkcompute |
0 commit comments