|
| 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 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/util/tensor_util.h> |
| 13 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 14 | + |
| 15 | +namespace torch { |
| 16 | +namespace executor { |
| 17 | + |
| 18 | +bool check_upsample_2d_common_args( |
| 19 | + const Tensor& in, |
| 20 | + const exec_aten::OptionalArrayRef<int64_t>& output_size, |
| 21 | + const exec_aten::OptionalArrayRef<double>& scale_factors, |
| 22 | + Tensor& out); |
| 23 | + |
| 24 | +bool check_upsample_bilinear2d_args( |
| 25 | + const Tensor& in, |
| 26 | + const exec_aten::OptionalArrayRef<int64_t>& output_size, |
| 27 | + const bool align_corners, |
| 28 | + const exec_aten::OptionalArrayRef<double>& scale_factors, |
| 29 | + Tensor& out); |
| 30 | + |
| 31 | +Error resize_upsample_2d( |
| 32 | + const Tensor& in, |
| 33 | + const exec_aten::OptionalArrayRef<int64_t>& output_size, |
| 34 | + const exec_aten::OptionalArrayRef<double>& scale_factors, |
| 35 | + double& scale_h_out, |
| 36 | + double& scale_w_out, |
| 37 | + Tensor& out); |
| 38 | + |
| 39 | +// Ported from aten/src/ATen/native/UpSample.h |
| 40 | +template <typename scalar_t> |
| 41 | +inline scalar_t compute_scales_value( |
| 42 | + const exec_aten::optional<double>& scale, |
| 43 | + int64_t input_size, |
| 44 | + int64_t output_size) { |
| 45 | + return scale.has_value() ? static_cast<scalar_t>(1.0 / scale.value()) |
| 46 | + : (static_cast<scalar_t>(input_size) / output_size); |
| 47 | +} |
| 48 | + |
| 49 | +// Ported from aten/src/ATen/native/UpSample.h |
| 50 | +template <typename scalar_t> |
| 51 | +inline scalar_t area_pixel_compute_scale( |
| 52 | + int64_t input_size, |
| 53 | + int64_t output_size, |
| 54 | + bool align_corners, |
| 55 | + const exec_aten::optional<double>& scale) { |
| 56 | + // see Note [area_pixel_compute_scale] |
| 57 | + if (align_corners) { |
| 58 | + if (output_size > 1) { |
| 59 | + return static_cast<scalar_t>(input_size - 1) / (output_size - 1); |
| 60 | + } else { |
| 61 | + return static_cast<scalar_t>(0); |
| 62 | + } |
| 63 | + } else { |
| 64 | + return compute_scales_value<scalar_t>(scale, input_size, output_size); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Ported from aten/src/ATen/native/UpSample.h |
| 69 | +template <typename scalar_t> |
| 70 | +inline scalar_t area_pixel_compute_source_index( |
| 71 | + scalar_t scale, |
| 72 | + int64_t dst_index, |
| 73 | + bool align_corners, |
| 74 | + bool cubic) { |
| 75 | + if (align_corners) { |
| 76 | + return scale * dst_index; |
| 77 | + } else { |
| 78 | + scalar_t src_idx = scale * (dst_index + static_cast<scalar_t>(0.5)) - |
| 79 | + static_cast<scalar_t>(0.5); |
| 80 | + return (!cubic && src_idx < static_cast<scalar_t>(0)) ? scalar_t(0) |
| 81 | + : src_idx; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Ported from aten/src/ATen/native/UpSample.h |
| 86 | +// when `real_input_index` becomes larger than the range the floating point |
| 87 | +// type can accurately represent, the type casting to `int64_t` might exceed |
| 88 | +// `input_size`, causing overflow. So we guard it with `std::min` below. |
| 89 | +template <typename scalar_t, typename opmath_t> |
| 90 | +inline void guard_index_and_lambda( |
| 91 | + const opmath_t& real_input_index, |
| 92 | + const int64_t& input_size, |
| 93 | + int64_t& input_index, |
| 94 | + scalar_t& lambda) { |
| 95 | + input_index = |
| 96 | + std::min(static_cast<int64_t>(floorf(real_input_index)), input_size - 1); |
| 97 | + lambda = std::min( |
| 98 | + std::max(real_input_index - input_index, static_cast<opmath_t>(0)), |
| 99 | + static_cast<opmath_t>(1)); |
| 100 | +} |
| 101 | + |
| 102 | +// Ported from aten/src/ATen/native/UpSample.h |
| 103 | +template <typename scalar_t, typename opmath_t> |
| 104 | +inline void compute_source_index_and_lambda( |
| 105 | + int64_t& input_index0, |
| 106 | + int64_t& input_index1, |
| 107 | + scalar_t& lambda0, |
| 108 | + scalar_t& lambda1, |
| 109 | + opmath_t ratio, |
| 110 | + int64_t output_index, |
| 111 | + int64_t input_size, |
| 112 | + int64_t output_size, |
| 113 | + bool align_corners) { |
| 114 | + if (output_size == input_size) { |
| 115 | + // scale_factor = 1, simply copy |
| 116 | + input_index0 = output_index; |
| 117 | + input_index1 = output_index; |
| 118 | + lambda0 = static_cast<scalar_t>(1); |
| 119 | + lambda1 = static_cast<scalar_t>(0); |
| 120 | + } else { |
| 121 | + const auto real_input_index = area_pixel_compute_source_index<opmath_t>( |
| 122 | + ratio, output_index, align_corners, /*cubic=*/false); |
| 123 | + guard_index_and_lambda(real_input_index, input_size, input_index0, lambda1); |
| 124 | + int64_t offset = (input_index0 < input_size - 1) ? 1 : 0; |
| 125 | + input_index1 = input_index0 + offset; |
| 126 | + lambda0 = static_cast<scalar_t>(1.) - lambda1; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace executor |
| 131 | +} // namespace torch |
0 commit comments