|
| 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/Logging.h> |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/DimUtils.h> |
| 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 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 17 | + |
| 18 | +namespace vkcompute { |
| 19 | + |
| 20 | +void add_slice_tensor_out_node( |
| 21 | + ComputeGraph& graph, |
| 22 | + ValueRef in, |
| 23 | + ValueRef dim_ref, |
| 24 | + ValueRef opt_start_ref, |
| 25 | + ValueRef opt_end_ref, |
| 26 | + ValueRef step_ref, |
| 27 | + ValueRef out) { |
| 28 | + vTensorPtr t_in = graph.get_tensor(in); |
| 29 | + vTensorPtr t_out = graph.get_tensor(out); |
| 30 | + |
| 31 | + VK_CHECK_COND(check_memory_layout_is(*t_in, api::kChannelsPacked)); |
| 32 | + VK_CHECK_COND(check_memory_layout_is(*t_out, api::kChannelsPacked)); |
| 33 | + |
| 34 | + // Need normalize the dim |
| 35 | + int64_t dim = graph.extract_scalar<int64_t>(dim_ref); |
| 36 | + |
| 37 | + VK_CHECK_COND( |
| 38 | + -t_in->dim() <= dim && dim < t_in->dim(), |
| 39 | + "dim must be in range of [-self.dim(), self.dim()), but current dim's value is ", |
| 40 | + dim, |
| 41 | + " and self.dim() = ", |
| 42 | + t_in->dim()); |
| 43 | + |
| 44 | + dim = normalize(dim, t_in->dim()); |
| 45 | + |
| 46 | + // Create a dim value as in the underlying dim is 4-dimension. |
| 47 | + int64_t nchw_dim = dim + (4 - t_in->dim()); |
| 48 | + |
| 49 | + std::optional<int64_t> opt_start = |
| 50 | + graph.extract_optional_scalar<int64_t>(opt_start_ref); |
| 51 | + std::optional<int64_t> opt_end = |
| 52 | + graph.extract_optional_scalar<int64_t>(opt_end_ref); |
| 53 | + int64_t step = graph.extract_scalar<int64_t>(step_ref); |
| 54 | + |
| 55 | + const auto in_sizes = t_in->sizes(); |
| 56 | + const auto out_sizes = t_out->sizes(); |
| 57 | + |
| 58 | + int64_t start = opt_start.value_or(0); |
| 59 | + int64_t end = opt_end.value_or(in_sizes[dim]); |
| 60 | + |
| 61 | + VK_CHECK_COND((0 <= start) && (start < in_sizes[dim])); |
| 62 | + VK_CHECK_COND((0 <= end) && (end <= in_sizes[dim])); |
| 63 | + |
| 64 | + if (nchw_dim == 1) { |
| 65 | + // slice by channel |
| 66 | + std::string kernel_name = "slice_channel"; |
| 67 | + kernel_name.reserve(kShaderNameReserve); |
| 68 | + add_dtype_suffix(kernel_name, *t_out); |
| 69 | + add_memory_layout_suffix(kernel_name, *t_out); |
| 70 | + |
| 71 | + api::utils::uvec3 global_size = t_out->extents(); |
| 72 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 73 | + |
| 74 | + const struct Block final { |
| 75 | + int offset; |
| 76 | + int step; |
| 77 | + } params{ |
| 78 | + static_cast<int32_t>(start), |
| 79 | + static_cast<int32_t>(step), |
| 80 | + }; |
| 81 | + |
| 82 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 83 | + graph, |
| 84 | + VK_KERNEL_FROM_STR(kernel_name), |
| 85 | + global_size, |
| 86 | + local_size, |
| 87 | + {{out, api::MemoryAccessType::WRITE}, |
| 88 | + {in, api::MemoryAccessType::READ}}, |
| 89 | + {t_out->gpu_sizes_ubo(), |
| 90 | + t_out->cpu_sizes_ubo(), |
| 91 | + t_in->gpu_sizes_ubo(), |
| 92 | + graph.create_params_buffer(params)})); |
| 93 | + |
| 94 | + } else { |
| 95 | + // GPU's coordinate is in x, y, z |
| 96 | + int64_t gpu_dim = -1; |
| 97 | + int64_t stride = 1; |
| 98 | + if (nchw_dim == 3) { |
| 99 | + gpu_dim = 0; // width: x dimension in gpu |
| 100 | + VK_CHECK_COND(out_sizes[dim] == (1 + (end - start - 1) / step)); |
| 101 | + } else if (nchw_dim == 2) { |
| 102 | + gpu_dim = 1; // height: y dimension |
| 103 | + VK_CHECK_COND(out_sizes[dim] == (1 + (end - start - 1) / step)); |
| 104 | + } else if (nchw_dim == 0) { |
| 105 | + gpu_dim = 2; // batch: z dimension |
| 106 | + |
| 107 | + // Due to channel packing, each batch value is span over stride planes |
| 108 | + int64_t n_channels = dim_at<Dim4D::Channel>(in_sizes); |
| 109 | + stride = api::utils::div_up<int64_t>(n_channels, 4ll); |
| 110 | + } else { |
| 111 | + VK_THROW("Unexpected ncwh_dim!"); |
| 112 | + } |
| 113 | + |
| 114 | + std::string kernel_name = "slice_batch_height_width"; |
| 115 | + kernel_name.reserve(kShaderNameReserve); |
| 116 | + add_dtype_suffix(kernel_name, *t_out); |
| 117 | + |
| 118 | + api::utils::uvec3 global_size = t_out->extents(); |
| 119 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 120 | + |
| 121 | + const struct Block final { |
| 122 | + int dim; |
| 123 | + int offset; |
| 124 | + int step; |
| 125 | + int stride; |
| 126 | + } params{ |
| 127 | + static_cast<int32_t>(gpu_dim), |
| 128 | + static_cast<int32_t>(start), |
| 129 | + static_cast<int32_t>(step), |
| 130 | + static_cast<int32_t>(stride), |
| 131 | + }; |
| 132 | + |
| 133 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 134 | + graph, |
| 135 | + VK_KERNEL_FROM_STR(kernel_name), |
| 136 | + global_size, |
| 137 | + local_size, |
| 138 | + {{out, api::MemoryAccessType::WRITE}, |
| 139 | + {in, api::MemoryAccessType::READ}}, |
| 140 | + {t_out->gpu_sizes_ubo(), graph.create_params_buffer(params)})); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +void slice_tensor_out(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 145 | + return add_slice_tensor_out_node( |
| 146 | + graph, |
| 147 | + args[0], |
| 148 | + args[1], // dim |
| 149 | + args[2], // optional start |
| 150 | + args[3], // optional end |
| 151 | + args[4], // step |
| 152 | + args[5]); |
| 153 | +} |
| 154 | + |
| 155 | +REGISTER_OPERATORS { |
| 156 | + VK_REGISTER_OP(aten.slice_copy.Tensor, slice_tensor_out); |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace vkcompute |
0 commit comments