|
| 1 | +/* |
| 2 | + * Copyright 2026 Arm Limited and/or its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the BSD-style license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <executorch/kernels/portable/cpu/util/upsample_util.h> |
| 9 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 10 | +#include <executorch/runtime/kernel/thread_parallel_interface.h> |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | + |
| 14 | +#if defined(__aarch64__) |
| 15 | +#include <executorch/kernels/optimized/cpu/op_upsample_nearest2d_neon.h> |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace torch { |
| 19 | +namespace executor { |
| 20 | +namespace native { |
| 21 | + |
| 22 | +using executorch::aten::ScalarType; |
| 23 | +using executorch::aten::Tensor; |
| 24 | + |
| 25 | +Tensor& upsample_nearest2d_vec_out( |
| 26 | + KernelRuntimeContext& ctx, |
| 27 | + const Tensor& in, |
| 28 | + executorch::aten::OptionalArrayRef<int64_t> output_size, |
| 29 | + executorch::aten::OptionalArrayRef<double> scale_factors, |
| 30 | + Tensor& out); |
| 31 | + |
| 32 | +Tensor& opt_upsample_nearest2d_vec_out( |
| 33 | + KernelRuntimeContext& ctx, |
| 34 | + const Tensor& in, |
| 35 | + const executorch::aten::OptionalArrayRef<int64_t> output_size, |
| 36 | + const executorch::aten::OptionalArrayRef<double> scale_factors, |
| 37 | + Tensor& out) { |
| 38 | +#if !defined(__aarch64__) |
| 39 | + return upsample_nearest2d_vec_out(ctx, in, output_size, scale_factors, out); |
| 40 | +#else |
| 41 | + if (in.scalar_type() != ScalarType::Half || |
| 42 | + !tensor_is_default_dim_order(in) || !tensor_is_default_dim_order(out) || |
| 43 | + !tensor_is_contiguous(in) || !tensor_is_contiguous(out) || |
| 44 | + !check_upsample_nearest2d_args(in, output_size, scale_factors, out)) { |
| 45 | + return upsample_nearest2d_vec_out(ctx, in, output_size, scale_factors, out); |
| 46 | + } |
| 47 | + |
| 48 | + double scale_h; |
| 49 | + double scale_w; |
| 50 | + ET_KERNEL_CHECK_MSG( |
| 51 | + ctx, |
| 52 | + resize_upsample_2d( |
| 53 | + in, output_size, scale_factors, scale_h, scale_w, out) == Error::Ok, |
| 54 | + InvalidArgument, |
| 55 | + out, |
| 56 | + "Failed to resize output tensor"); |
| 57 | + |
| 58 | + const auto kernel_scale_h = |
| 59 | + area_pixel_compute_scale<double>(in.size(2), out.size(2), false, scale_h); |
| 60 | + const auto kernel_scale_w = |
| 61 | + area_pixel_compute_scale<double>(in.size(3), out.size(3), false, scale_w); |
| 62 | + if (!tensor_is_contiguous(out) || kernel_scale_h != 0.5 || |
| 63 | + kernel_scale_w != 0.5 || out.size(2) != in.size(2) * 2 || |
| 64 | + out.size(3) != in.size(3) * 2) { |
| 65 | + return upsample_nearest2d_vec_out(ctx, in, output_size, scale_factors, out); |
| 66 | + } |
| 67 | + |
| 68 | + const int64_t output_plane_size = out.size(2) * out.size(3); |
| 69 | + const int64_t grain_size = std::max<int64_t>( |
| 70 | + 1, ::executorch::extension::internal::GRAIN_SIZE / output_plane_size); |
| 71 | + const int64_t planes = in.size(0) * in.size(1); |
| 72 | + const auto* input = reinterpret_cast<const uint16_t*>(in.const_data_ptr()); |
| 73 | + auto* output = reinterpret_cast<uint16_t*>(out.mutable_data_ptr()); |
| 74 | + |
| 75 | + const bool success = ::executorch::extension::parallel_for( |
| 76 | + 0, planes, grain_size, [&](const auto begin, const auto end) { |
| 77 | + opt_upsample_nearest2d_internal::upsample_nearest2d_2x_nchw_u16( |
| 78 | + input, output, begin, end, in.size(2), in.size(3)); |
| 79 | + }); |
| 80 | + if (!success) { |
| 81 | + return upsample_nearest2d_vec_out(ctx, in, output_size, scale_factors, out); |
| 82 | + } |
| 83 | + return out; |
| 84 | +#endif |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace native |
| 88 | +} // namespace executor |
| 89 | +} // namespace torch |
0 commit comments