|
| 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/utils/KernelUtils.h> |
| 12 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h> |
| 13 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 14 | + |
| 15 | +namespace vkcompute { |
| 16 | + |
| 17 | +using api::utils::ivec3; |
| 18 | +using api::utils::uvec2; |
| 19 | +using api::utils::uvec4; |
| 20 | + |
| 21 | +void check_args( |
| 22 | + const vTensor& in, |
| 23 | + const IntListPtr& permute_dims, |
| 24 | + const vTensor& out) { |
| 25 | + VK_CHECK_COND(check_memory_layout_is(in, api::kChannelsPacked)); |
| 26 | + VK_CHECK_COND(check_memory_layout_is(out, api::kChannelsPacked)); |
| 27 | + |
| 28 | + int64_t in_dim = in.dim(); |
| 29 | + VK_CHECK_COND( |
| 30 | + in_dim == permute_dims->size(), |
| 31 | + "Input tensor dim size must match argument"); |
| 32 | +} |
| 33 | + |
| 34 | +void add_permute_node( |
| 35 | + ComputeGraph& graph, |
| 36 | + ValueRef in, |
| 37 | + ValueRef permute_dims_ref, |
| 38 | + ValueRef out) { |
| 39 | + vTensorPtr t_in = graph.get_tensor(in); |
| 40 | + vTensorPtr t_out = graph.get_tensor(out); |
| 41 | + |
| 42 | + IntListPtr permute_dims = graph.get_int_list(permute_dims_ref); |
| 43 | + |
| 44 | + check_args(*t_in, permute_dims, *t_out); |
| 45 | + |
| 46 | + uvec4 in_size{1u, 1u, 1u, 1u}, out_size{1u, 1u, 1u, 1u}; |
| 47 | + uvec4 out_dims{0u, 1u, 2u, 3u}; |
| 48 | + |
| 49 | + int64_t in_dim = t_in->dim(); |
| 50 | + |
| 51 | + std::vector<bool> seen(in_dim); |
| 52 | + for (int i = 0; i < in_dim; i++) { |
| 53 | + int64_t permute_dim = (*permute_dims)[i]; |
| 54 | + VK_CHECK_COND( |
| 55 | + !seen[permute_dim], "Argument dim ", permute_dim, " is repeated"); |
| 56 | + seen[permute_dim] = true; |
| 57 | + |
| 58 | + // Map to 4D tensor dims. |
| 59 | + in_size.data[(4u - in_dim) + i] = t_in->size(i); |
| 60 | + out_size.data[(4u - in_dim) + i] = t_in->size(permute_dim); |
| 61 | + out_dims.data[(4u - in_dim) + i] = permute_dim + (4u - in_dim); |
| 62 | + } |
| 63 | + |
| 64 | + std::string kernel_name = "permute"; |
| 65 | + kernel_name.reserve(kShaderNameReserve); |
| 66 | + add_dtype_suffix(kernel_name, *t_out); |
| 67 | + |
| 68 | + uint32_t out_channels = out_size.data[1u]; |
| 69 | + uint32_t in_channels = in_size.data[1u]; |
| 70 | + |
| 71 | + uint32_t out_c_aligned = api::utils::align_up(out_channels, 4u); |
| 72 | + uint32_t in_c_aligned = api::utils::align_up(in_channels, 4u); |
| 73 | + |
| 74 | + const struct Block final { |
| 75 | + uvec4 out_ndims; |
| 76 | + uvec2 ch_info; |
| 77 | + } params{ |
| 78 | + out_dims, |
| 79 | + {out_c_aligned, in_c_aligned}, |
| 80 | + }; |
| 81 | + |
| 82 | + api::utils::uvec3 global_size = t_out->virtual_extents(); |
| 83 | + api::utils::uvec3 local_size = adaptive_work_group_size(global_size); |
| 84 | + |
| 85 | + graph.execute_nodes().emplace_back(new ExecuteNode( |
| 86 | + graph, |
| 87 | + VK_KERNEL_FROM_STR(kernel_name), |
| 88 | + global_size, |
| 89 | + local_size, |
| 90 | + {{out, api::MemoryAccessType::WRITE}, {in, api::MemoryAccessType::READ}}, |
| 91 | + {t_out->gpu_sizes_ubo(), graph.create_params_buffer(params)})); |
| 92 | +} |
| 93 | + |
| 94 | +void permute(ComputeGraph& graph, const std::vector<ValueRef>& args) { |
| 95 | + return add_permute_node(graph, args[0], args[1], args[2]); |
| 96 | +} |
| 97 | + |
| 98 | +REGISTER_OPERATORS { |
| 99 | + VK_REGISTER_OP(aten.permute_copy.default, permute); |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace vkcompute |
0 commit comments