|
| 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/util/scalar_type_util.h> |
| 12 | + |
| 13 | +#ifdef ET_USE_PYTORCH_HEADERS |
| 14 | +#include <ATen/cpu/vec/vec.h> |
| 15 | +#endif // ET_USE_PYTORCH_HEADERS |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <type_traits> |
| 19 | + |
| 20 | +#ifdef ET_USE_PYTORCH_HEADERS |
| 21 | +namespace executorch { |
| 22 | +inline namespace math { |
| 23 | +namespace internal { |
| 24 | +template <typename T> |
| 25 | +auto convert_to_vectorized_n_of_float(at::vec::Vectorized<T> vec) { |
| 26 | + static constexpr auto float_vec_size = at::vec::Vectorized<float>::size(); |
| 27 | + static constexpr auto t_vec_size = at::vec::Vectorized<T>::size(); |
| 28 | + static constexpr auto result_size = |
| 29 | + t_vec_size < float_vec_size ? 1 : t_vec_size / float_vec_size; |
| 30 | + static_assert(result_size >= 1); |
| 31 | + return at::vec::convert<float, result_size, T, 1, /*keep=*/true>( |
| 32 | + at::vec::VectorizedN<T, 1>(vec)); |
| 33 | +} |
| 34 | +} // namespace internal |
| 35 | +} // namespace math |
| 36 | +} // namespace executorch |
| 37 | +#endif // ET_USE_PYTORCH_HEADERS |
| 38 | + |
| 39 | +#define _ET_INTERNAL_STD_MATH_FUNC(name) \ |
| 40 | + namespace executorch { \ |
| 41 | + inline namespace math { \ |
| 42 | + using std::name; \ |
| 43 | + } \ |
| 44 | + } // namespace executorch |
| 45 | + |
| 46 | +#ifdef ET_USE_PYTORCH_HEADERS |
| 47 | +/** |
| 48 | + * Internal-usage macro for making a vectorized variant of a unary |
| 49 | + * function available in the executorch::math namespace. |
| 50 | + */ |
| 51 | +#define ET_INTERNAL_VECTORIZED_FLOAT_UNARY_FUNC(func_name) \ |
| 52 | + namespace executorch { \ |
| 53 | + inline namespace math { \ |
| 54 | + template <typename T> \ |
| 55 | + auto func_name(at::vec::Vectorized<T> vec) { \ |
| 56 | + if constexpr (!::executorch::runtime::is_floating_point<T>::value) { \ |
| 57 | + return internal::convert_to_vectorized_n_of_float(vec).func_name(); \ |
| 58 | + } else { \ |
| 59 | + return vec.func_name(); \ |
| 60 | + } \ |
| 61 | + } \ |
| 62 | + } \ |
| 63 | + } |
| 64 | + |
| 65 | +#define ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(func_name) \ |
| 66 | + namespace executorch { \ |
| 67 | + inline namespace math { \ |
| 68 | + template <typename T> \ |
| 69 | + auto func_name(at::vec::Vectorized<T> vec0, at::vec::Vectorized<T> vec1) { \ |
| 70 | + if constexpr (!::executorch::runtime::is_floating_point<T>::value) { \ |
| 71 | + const auto vec_float0 = \ |
| 72 | + internal::convert_to_vectorized_n_of_float(vec0); \ |
| 73 | + const auto vec_float1 = \ |
| 74 | + internal::convert_to_vectorized_n_of_float(vec1); \ |
| 75 | + return vec_float0.func_name(vec_float1); \ |
| 76 | + } else { \ |
| 77 | + return vec0.func_name(vec1); \ |
| 78 | + } \ |
| 79 | + } \ |
| 80 | + } \ |
| 81 | + } |
| 82 | + |
| 83 | +/** |
| 84 | + * Internal-usage macro for making a C++ standard library |
| 85 | + * floating-point function and a vectorized variant of it available in |
| 86 | + * the c10::math namespace. Should be used with functions where the |
| 87 | + * corresponding operator is a "float op" in TensorIterator parlance |
| 88 | + * (i.e., uses something like build_borrowing_binary_float_op()), |
| 89 | + * because it converts non-floating-point arguments to floating point. |
| 90 | + */ |
| 91 | +#define ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(func_name) \ |
| 92 | + _ET_INTERNAL_STD_MATH_FUNC(func_name) \ |
| 93 | + ET_INTERNAL_VECTORIZED_FLOAT_UNARY_FUNC(func_name) |
| 94 | + |
| 95 | +#define ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(func_name) \ |
| 96 | + _ET_INTERNAL_STD_MATH_FUNC(func_name) \ |
| 97 | + ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(func_name) |
| 98 | + |
| 99 | +#else // ET_USE_PYTORCH_HEADERS |
| 100 | +#define ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(name) \ |
| 101 | + _ET_INTERNAL_STD_MATH_FUNC(name) |
| 102 | +#define ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(name) \ |
| 103 | + _ET_INTERNAL_STD_MATH_FUNC(name) |
| 104 | +#endif // ET_USE_PYTORCH_HEADERS |
| 105 | + |
| 106 | +// To simplify client code, we provide coverage for a bunch of float ops (the |
| 107 | +// same ones listed in ATen vml.h) here. |
| 108 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(abs) |
| 109 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(acos) |
| 110 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(asin) |
| 111 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(atan) |
| 112 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(ceil) |
| 113 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(cos) |
| 114 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(cosh) |
| 115 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(erf) |
| 116 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(erfc) |
| 117 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(exp) |
| 118 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(expm1) |
| 119 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(floor) |
| 120 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log) |
| 121 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log10) |
| 122 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log1p) |
| 123 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(log2) |
| 124 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sin) |
| 125 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sinh) |
| 126 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(sqrt) |
| 127 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(round) |
| 128 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(tan) |
| 129 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(tanh) |
| 130 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(trunc) |
| 131 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_UNARY_FUNC(lgamma) |
| 132 | + |
| 133 | +#ifdef ET_USE_PYTORCH_HEADERS |
| 134 | +ET_INTERNAL_VECTORIZED_FLOAT_BINARY_FUNC(rsqrt) |
| 135 | +#endif // ET_USE_PYTORCH_HEADERS |
| 136 | + |
| 137 | +namespace executorch { |
| 138 | +inline namespace math { |
| 139 | +template <typename T, std::enable_if_t<std::is_floating_point_v<T>>> |
| 140 | +T rsqrt(T x) { |
| 141 | + return T(1) / std::sqrt(x); |
| 142 | +} |
| 143 | +} // namespace math |
| 144 | +} // namespace executorch |
| 145 | + |
| 146 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(atan2) |
| 147 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(fmod) |
| 148 | +ET_INTERNAL_VECTORIZED_STD_FLOAT_BINARY_FUNC(pow) |
0 commit comments