|
| 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_bilinear2d_kernel_impl( |
| 23 | + const Tensor& in, |
| 24 | + bool align_corners, |
| 25 | + const float scale_h, |
| 26 | + const float scale_w, |
| 27 | + Tensor& out) { |
| 28 | + const auto in_data = in.const_data_ptr<CTYPE>(); |
| 29 | + auto out_data = out.mutable_data_ptr<CTYPE>(); |
| 30 | + |
| 31 | + auto in_plane = in_data; |
| 32 | + for (auto n = 0; n < out.size(0); n++) { |
| 33 | + for (auto c = 0; c < out.size(1); c++) { |
| 34 | + for (auto h = 0; h < out.size(2); h++) { |
| 35 | + // Compute source index and weights. |
| 36 | + int64_t in_h1, in_h2; |
| 37 | + float weight_h, inv_weight_h; |
| 38 | + |
| 39 | + compute_source_index_and_lambda( |
| 40 | + in_h1, |
| 41 | + in_h2, |
| 42 | + weight_h, |
| 43 | + inv_weight_h, |
| 44 | + scale_h, |
| 45 | + h, |
| 46 | + in.sizes()[2], |
| 47 | + out.sizes()[2], |
| 48 | + align_corners); |
| 49 | + |
| 50 | + for (auto w = 0; w < out.size(3); w++) { |
| 51 | + int64_t in_w1, in_w2; |
| 52 | + float weight_w, inv_weight_w; |
| 53 | + |
| 54 | + compute_source_index_and_lambda( |
| 55 | + in_w1, |
| 56 | + in_w2, |
| 57 | + weight_w, |
| 58 | + inv_weight_w, |
| 59 | + scale_w, |
| 60 | + w, |
| 61 | + in.sizes()[3], |
| 62 | + out.sizes()[3], |
| 63 | + align_corners); |
| 64 | + |
| 65 | + const auto top_left = |
| 66 | + in_plane[in_h1 * in.strides()[2] + in_w1 * in.strides()[3]]; |
| 67 | + const auto top_right = |
| 68 | + in_plane[in_h1 * in.strides()[2] + in_w2 * in.strides()[3]]; |
| 69 | + const auto bottom_left = |
| 70 | + in_plane[in_h2 * in.strides()[2] + in_w1 * in.strides()[3]]; |
| 71 | + const auto bottom_right = |
| 72 | + in_plane[in_h2 * in.strides()[2] + in_w2 * in.strides()[3]]; |
| 73 | + |
| 74 | + const auto top = top_left * weight_w + top_right * inv_weight_w; |
| 75 | + const auto bottom = |
| 76 | + bottom_left * weight_w + bottom_right * inv_weight_w; |
| 77 | + const auto val = top * weight_h + bottom * inv_weight_h; |
| 78 | + |
| 79 | + *out_data = val; |
| 80 | + out_data++; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + in_plane += in.strides()[1]; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +} // namespace |
| 89 | + |
| 90 | +// Signatures are auto-generated, so disable pass-by-value lint. |
| 91 | +// NOLINTBEGIN(facebook-hte-ConstantArgumentPassByValue, |
| 92 | +// facebook-hte-ParameterMightThrowOnCopy) |
| 93 | +Tensor& upsample_bilinear2d_vec_out( |
| 94 | + KernelRuntimeContext& ctx, |
| 95 | + const Tensor& in, |
| 96 | + const exec_aten::OptionalArrayRef<int64_t> output_size, |
| 97 | + bool align_corners, |
| 98 | + const exec_aten::OptionalArrayRef<double> scale_factors, |
| 99 | + Tensor& out) { |
| 100 | + // Preconditions (checked in check_..._args): |
| 101 | + // In and out tensors have same dtype. |
| 102 | + // In and out tensors are rank 4 and have same dim[0] and dim[1]. |
| 103 | + // In and out tensors are default dim order (NCHW). |
| 104 | + ET_KERNEL_CHECK( |
| 105 | + ctx, |
| 106 | + check_upsample_bilinear2d_args( |
| 107 | + in, output_size, align_corners, scale_factors, out), |
| 108 | + InvalidArgument, |
| 109 | + out); |
| 110 | + |
| 111 | + double scale_h, scale_w; |
| 112 | + |
| 113 | + ET_KERNEL_CHECK_MSG( |
| 114 | + ctx, |
| 115 | + resize_upsample_2d( |
| 116 | + in, output_size, scale_factors, scale_h, scale_w, out) == Error::Ok, |
| 117 | + InvalidArgument, |
| 118 | + out, |
| 119 | + "Failed to resize output tensor"); |
| 120 | + |
| 121 | + const auto kernel_scale_h = area_pixel_compute_scale<double>( |
| 122 | + in.sizes()[2], out.sizes()[2], align_corners, scale_h); |
| 123 | + const auto kernel_scale_w = area_pixel_compute_scale<double>( |
| 124 | + in.sizes()[3], out.sizes()[3], align_corners, scale_w); |
| 125 | + |
| 126 | + ET_SWITCH_REAL_TYPES( |
| 127 | + in.scalar_type(), ctx, "upsample_bilinear2d.out", CTYPE, [&]() { |
| 128 | + upsample_bilinear2d_kernel_impl<CTYPE>( |
| 129 | + in, align_corners, kernel_scale_h, kernel_scale_w, out); |
| 130 | + }); |
| 131 | + |
| 132 | + return out; |
| 133 | +} |
| 134 | +// NOLINTEND(facebook-hte-ConstantArgumentPassByValue, |
| 135 | +// facebook-hte-ParameterMightThrowOnCopy) |
| 136 | + |
| 137 | +} // namespace native |
| 138 | +} // namespace executor |
| 139 | +} // namespace torch |
0 commit comments