|
| 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/DimUtils.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 | +void check_index_select_args( |
| 21 | + const vTensor& in, |
| 22 | + const vTensor& idx, |
| 23 | + const vTensor& out) { |
| 24 | + VK_CHECK_COND(check_memory_layout_is(in, api::kChannelsPacked)); |
| 25 | + VK_CHECK_COND(check_memory_layout_is(idx, api::kChannelsPacked)); |
| 26 | + VK_CHECK_COND(check_memory_layout_is(out, api::kChannelsPacked)); |
| 27 | +} |
| 28 | + |
| 29 | +void add_index_select_channel_node( |
| 30 | + ComputeGraph& graph, |
| 31 | + ValueRef in, |
| 32 | + ValueRef idx, |
| 33 | + ValueRef out) { |
| 34 | + vTensorPtr t_in = graph.get_tensor(in); |
| 35 | + vTensorPtr t_idx = graph.get_tensor(idx); |
| 36 | + vTensorPtr t_out = graph.get_tensor(out); |
| 37 | + |
| 38 | + check_index_select_args(*t_in, *t_idx, *t_out); |
| 39 | + |
| 40 | + std::string kernel_name = "index_select_channel"; |
| 41 | + kernel_name.reserve(kShaderNameReserve); |
| 42 | + add_dtype_suffix(kernel_name, *t_out); |
| 43 | + |
| 44 | + api::utils::uvec3 global_size = t_out->image_extents(); |
| 45 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 46 | + |
| 47 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 48 | + graph, |
| 49 | + VK_KERNEL_FROM_STR(kernel_name), |
| 50 | + global_size, |
| 51 | + local_size, |
| 52 | + {{out, api::MemoryAccessType::WRITE}, |
| 53 | + {{in, idx}, api::MemoryAccessType::READ}}, |
| 54 | + {t_out->sizes_ubo(), t_in->sizes_ubo()})); |
| 55 | +} |
| 56 | + |
| 57 | +struct IndexSelectParams final { |
| 58 | + int32_t gpu_dim; |
| 59 | + int32_t stride; |
| 60 | +}; |
| 61 | + |
| 62 | +IndexSelectParams create_index_select_params( |
| 63 | + const int64_t dim_idx, |
| 64 | + const vTensor& in) { |
| 65 | + if (dim_idx == kWidth4D) { |
| 66 | + return {0, 1}; |
| 67 | + } else if (dim_idx == kHeight4D) { |
| 68 | + return {1, 1}; |
| 69 | + } else if (dim_idx == kBatch4D) { |
| 70 | + int64_t n_channels = dim_at(in.sizes(), kChannel4D); |
| 71 | + int64_t stride = api::utils::div_up_4(n_channels); |
| 72 | + return {2, static_cast<int32_t>(stride)}; |
| 73 | + } else { |
| 74 | + VK_THROW("Unexpected dim_idx!"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void add_index_select_node( |
| 79 | + ComputeGraph& graph, |
| 80 | + ValueRef in, |
| 81 | + const int64_t dim_idx, |
| 82 | + ValueRef idx, |
| 83 | + ValueRef out) { |
| 84 | + vTensorPtr t_in = graph.get_tensor(in); |
| 85 | + vTensorPtr t_idx = graph.get_tensor(idx); |
| 86 | + vTensorPtr t_out = graph.get_tensor(out); |
| 87 | + |
| 88 | + check_index_select_args(*t_in, *t_idx, *t_out); |
| 89 | + |
| 90 | + IndexSelectParams params = create_index_select_params(dim_idx, *t_in); |
| 91 | + |
| 92 | + std::string kernel_name = "index_select"; |
| 93 | + kernel_name.reserve(kShaderNameReserve); |
| 94 | + add_dtype_suffix(kernel_name, *t_out); |
| 95 | + |
| 96 | + api::utils::uvec3 global_size = t_out->image_extents(); |
| 97 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 98 | + |
| 99 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 100 | + graph, |
| 101 | + VK_KERNEL_FROM_STR(kernel_name), |
| 102 | + global_size, |
| 103 | + local_size, |
| 104 | + {{out, api::MemoryAccessType::WRITE}, |
| 105 | + {{in, idx}, api::MemoryAccessType::READ}}, |
| 106 | + {t_out->sizes_ubo(), graph.create_params_buffer(params)})); |
| 107 | +} |
| 108 | + |
| 109 | +int64_t get_dim_idx(ComputeGraph& graph, ValueRef in, ValueRef dim_ref) { |
| 110 | + vTensorPtr t_in = graph.get_tensor(in); |
| 111 | + int64_t dim = graph.extract_scalar<int64_t>(dim_ref); |
| 112 | + dim = normalize(dim, t_in->dim()); |
| 113 | + return normalize_to_dim_index(*t_in, dim); |
| 114 | +} |
| 115 | + |
| 116 | +void index_select(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 117 | + ValueRef in = prepack_if_tensor_ref(graph, args[0]); |
| 118 | + ValueRef dim_ref = args[1]; |
| 119 | + ValueRef idx = prepack_if_tensor_ref(graph, args[2]); |
| 120 | + ValueRef out = args[3]; |
| 121 | + |
| 122 | + const int64_t dim_idx = get_dim_idx(graph, in, dim_ref); |
| 123 | + if (dim_idx == kChannel4D) { |
| 124 | + add_index_select_channel_node(graph, in, idx, out); |
| 125 | + } else { |
| 126 | + add_index_select_node(graph, in, dim_idx, idx, out); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +REGISTER_OPERATORS { |
| 131 | + VK_REGISTER_OP(aten.index_select.default, index_select); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace vkcompute |
0 commit comments