|
| 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/Common.h> |
| 12 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 13 | + |
| 14 | +namespace vkcompute { |
| 15 | + |
| 16 | +int64_t normalize_unfold_dim(const int64_t dim, const int64_t ndim) { |
| 17 | + const int64_t normalized_dim = dim < 0 ? dim + ndim : dim; |
| 18 | + VK_CHECK_COND( |
| 19 | + normalized_dim >= 0 && normalized_dim < ndim, |
| 20 | + "unfold_copy: dimension must be in range [-input.dim(), input.dim())"); |
| 21 | + return normalized_dim; |
| 22 | +} |
| 23 | + |
| 24 | +std::vector<int64_t> get_unfold_sizes( |
| 25 | + ComputeGraph& graph, |
| 26 | + const ValueRef input, |
| 27 | + const ValueRef dim_ref, |
| 28 | + const ValueRef size_ref, |
| 29 | + const ValueRef step_ref) { |
| 30 | + const std::vector<int64_t> input_sizes = graph.sizes_of(input); |
| 31 | + VK_CHECK_COND(!input_sizes.empty(), "unfold_copy: input must have rank >= 1"); |
| 32 | + |
| 33 | + const int64_t dim = normalize_unfold_dim( |
| 34 | + graph.extract_scalar<int64_t>(dim_ref), input_sizes.size()); |
| 35 | + const int64_t size = graph.extract_scalar<int64_t>(size_ref); |
| 36 | + const int64_t step = graph.extract_scalar<int64_t>(step_ref); |
| 37 | + |
| 38 | + VK_CHECK_COND(size > 0, "unfold_copy: size must be greater than zero"); |
| 39 | + VK_CHECK_COND(step > 0, "unfold_copy: step must be greater than zero"); |
| 40 | + VK_CHECK_COND( |
| 41 | + size <= input_sizes.at(dim), |
| 42 | + "unfold_copy: size must not exceed selected input dimension"); |
| 43 | + |
| 44 | + std::vector<int64_t> output_sizes = input_sizes; |
| 45 | + output_sizes.at(dim) = (input_sizes.at(dim) - size) / step + 1; |
| 46 | + output_sizes.push_back(size); |
| 47 | + return output_sizes; |
| 48 | +} |
| 49 | + |
| 50 | +void resize_unfold_copy_node( |
| 51 | + ComputeGraph* graph, |
| 52 | + const std::vector<ArgGroup>& args, |
| 53 | + const std::vector<ValueRef>& resize_args) { |
| 54 | + const ValueRef output = args.at(0).refs.at(0); |
| 55 | + const ValueRef input = args.at(1).refs.at(0); |
| 56 | + graph->virtual_resize( |
| 57 | + output, |
| 58 | + get_unfold_sizes( |
| 59 | + *graph, |
| 60 | + input, |
| 61 | + resize_args.at(0), |
| 62 | + resize_args.at(1), |
| 63 | + resize_args.at(2))); |
| 64 | +} |
| 65 | + |
| 66 | +void add_unfold_copy_node( |
| 67 | + ComputeGraph& graph, |
| 68 | + const ValueRef input, |
| 69 | + const ValueRef dim_ref, |
| 70 | + const ValueRef size_ref, |
| 71 | + const ValueRef step_ref, |
| 72 | + const ValueRef output) { |
| 73 | + const int64_t input_ndim = graph.dim_of(input); |
| 74 | + const int64_t dim = |
| 75 | + normalize_unfold_dim(graph.extract_scalar<int64_t>(dim_ref), input_ndim); |
| 76 | + const int64_t dim_whcn = input_ndim - dim - 1; |
| 77 | + const int64_t step = graph.extract_scalar<int64_t>(step_ref); |
| 78 | + |
| 79 | + get_unfold_sizes(graph, input, dim_ref, size_ref, step_ref); |
| 80 | + |
| 81 | + std::string kernel_name = "unfold_copy_buffer"; |
| 82 | + add_dtype_suffix(kernel_name, graph.dtype_of(output)); |
| 83 | + |
| 84 | + graph.execute_nodes().emplace_back(new DynamicDispatchNode( |
| 85 | + graph, |
| 86 | + VK_KERNEL_FROM_STR(kernel_name), |
| 87 | + default_pick_global_wg_size, |
| 88 | + default_pick_local_wg_size, |
| 89 | + {{output, vkapi::kWrite}, {input, vkapi::kRead}}, |
| 90 | + {graph.meta_ubo(output), graph.meta_ubo(input)}, |
| 91 | + {}, |
| 92 | + { |
| 93 | + utils::safe_downcast<int32_t>(dim_whcn), |
| 94 | + utils::safe_downcast<int32_t>(step), |
| 95 | + }, |
| 96 | + {dim_ref, size_ref, step_ref}, |
| 97 | + resize_unfold_copy_node)); |
| 98 | +} |
| 99 | + |
| 100 | +void unfold_copy(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 101 | + add_unfold_copy_node( |
| 102 | + graph, args.at(0), args.at(1), args.at(2), args.at(3), args.at(4)); |
| 103 | +} |
| 104 | + |
| 105 | +REGISTER_OPERATORS { |
| 106 | + VK_REGISTER_OP(aten.unfold_copy.default, unfold_copy); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace vkcompute |
0 commit comments