|
| 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/kernels/portable/cpu/util/upsample_util.h> |
| 10 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 11 | + |
| 12 | +namespace torch { |
| 13 | +namespace executor { |
| 14 | +namespace native { |
| 15 | + |
| 16 | +using exec_aten::ArrayRef; |
| 17 | +using exec_aten::optional; |
| 18 | +using exec_aten::SizesType; |
| 19 | + |
| 20 | +namespace { |
| 21 | +template <typename CTYPE> |
| 22 | +void upsample_nearest2d_kernel_impl( |
| 23 | + const Tensor& in, |
| 24 | + const float scale_h, |
| 25 | + const float scale_w, |
| 26 | + Tensor& out) { |
| 27 | + const auto in_data = in.const_data_ptr<CTYPE>(); |
| 28 | + auto out_data = out.mutable_data_ptr<CTYPE>(); |
| 29 | + |
| 30 | + auto in_plane = in_data; |
| 31 | + for (auto n = 0; n < out.size(0); n++) { |
| 32 | + for (auto c = 0; c < out.size(1); c++) { |
| 33 | + for (auto h = 0; h < out.size(2); h++) { |
| 34 | + for (auto w = 0; w < out.size(3); w++) { |
| 35 | + const auto in_h = |
| 36 | + nearest_neighbor_compute_source_index(scale_h, h, in.sizes()[2]); |
| 37 | + const auto in_w = |
| 38 | + nearest_neighbor_compute_source_index(scale_w, w, in.sizes()[3]); |
| 39 | + |
| 40 | + *out_data = in_plane[in_h * in.strides()[2] + in_w * in.strides()[3]]; |
| 41 | + out_data++; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + in_plane += in.strides()[1]; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +} // namespace |
| 50 | + |
| 51 | +Tensor& upsample_nearest2d_vec_out( |
| 52 | + KernelRuntimeContext& ctx, |
| 53 | + const Tensor& in, |
| 54 | + const exec_aten::OptionalArrayRef<int64_t> output_size, |
| 55 | + const exec_aten::OptionalArrayRef<double> scale_factors, |
| 56 | + Tensor& out) { |
| 57 | + // Preconditions (checked in check_..._args): |
| 58 | + // In and out tensors have same dtype. |
| 59 | + // In and out tensors are rank 4 and have same dim[0] and dim[1]. |
| 60 | + // In and out tensors are default dim order (NCHW). |
| 61 | + ET_KERNEL_CHECK( |
| 62 | + ctx, |
| 63 | + check_upsample_nearest2d_args(in, output_size, scale_factors, out), |
| 64 | + InvalidArgument, |
| 65 | + out); |
| 66 | + |
| 67 | + double scale_h, scale_w; |
| 68 | + |
| 69 | + ET_KERNEL_CHECK_MSG( |
| 70 | + ctx, |
| 71 | + resize_upsample_2d( |
| 72 | + in, output_size, scale_factors, scale_h, scale_w, out) == Error::Ok, |
| 73 | + InvalidArgument, |
| 74 | + out, |
| 75 | + "Failed to resize output tensor"); |
| 76 | + |
| 77 | + const auto kernel_scale_h = area_pixel_compute_scale<double>( |
| 78 | + in.sizes()[2], out.sizes()[2], false, scale_h); |
| 79 | + const auto kernel_scale_w = area_pixel_compute_scale<double>( |
| 80 | + in.sizes()[3], out.sizes()[3], false, scale_w); |
| 81 | + |
| 82 | + ET_SWITCH_REAL_TYPES( |
| 83 | + in.scalar_type(), ctx, "upsample_nearest2d.out", CTYPE, [&]() { |
| 84 | + upsample_nearest2d_kernel_impl<CTYPE>( |
| 85 | + in, kernel_scale_h, kernel_scale_w, out); |
| 86 | + }); |
| 87 | + |
| 88 | + return out; |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace native |
| 92 | +} // namespace executor |
| 93 | +} // namespace torch |
0 commit comments