|
| 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/backends/cadence/hifi/kernels/kernels.h> |
| 10 | +#include <executorch/kernels/portable/cpu/scalar_utils.h> |
| 11 | +#include <executorch/kernels/portable/cpu/util/broadcast_util.h> |
| 12 | +#include <executorch/kernels/portable/cpu/util/functional_util.h> |
| 13 | +#include <executorch/kernels/portable/cpu/util/kernel_ops_util.h> |
| 14 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 15 | +#include <executorch/runtime/platform/assert.h> |
| 16 | + |
| 17 | +using exec_aten::Scalar; |
| 18 | +using exec_aten::ScalarType; |
| 19 | +using exec_aten::Tensor; |
| 20 | +using executorch::runtime::can_cast; |
| 21 | +using executorch::runtime::CppTypeToScalarType; |
| 22 | +using executorch::runtime::KernelRuntimeContext; |
| 23 | +using torch::executor::Error; |
| 24 | + |
| 25 | +namespace impl { |
| 26 | +namespace HiFi { |
| 27 | +namespace native { |
| 28 | + |
| 29 | +namespace { |
| 30 | +template < |
| 31 | + bool can_cast, |
| 32 | + typename CTYPE_A, |
| 33 | + typename CTYPE_B, |
| 34 | + typename CTYPE_IN, |
| 35 | + typename CTYPE_OUT> |
| 36 | +struct AddInner; |
| 37 | + |
| 38 | +template < |
| 39 | + typename CTYPE_A, |
| 40 | + typename CTYPE_B, |
| 41 | + typename CTYPE_IN, |
| 42 | + typename CTYPE_OUT> |
| 43 | +struct AddInner<true, CTYPE_A, CTYPE_B, CTYPE_IN, CTYPE_OUT> { |
| 44 | + static void |
| 45 | + run(const Tensor& a, const Tensor& b, CTYPE_IN alpha_val, Tensor& out) { |
| 46 | + torch::executor::apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>( |
| 47 | + // NOLINTNEXTLINE(facebook-hte-ConstantArgumentPassByValue) |
| 48 | + [alpha_val](const CTYPE_A val_a, const CTYPE_B val_b) { |
| 49 | + CTYPE_IN a_casted = static_cast<CTYPE_IN>(val_a); |
| 50 | + CTYPE_IN b_casted = static_cast<CTYPE_IN>(val_b); |
| 51 | + CTYPE_IN value = a_casted + alpha_val * b_casted; |
| 52 | + |
| 53 | + return static_cast<CTYPE_OUT>(value); |
| 54 | + }, |
| 55 | + a, |
| 56 | + b, |
| 57 | + out); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +template <typename CTYPE_IN> |
| 62 | +struct ReportCanCastBug { |
| 63 | + static void run(const Tensor&, const Tensor&, CTYPE_IN, Tensor&) { |
| 64 | + ET_DCHECK_MSG(false, "BUG: canCast should have been checked above"); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +template < |
| 69 | + typename CTYPE_A, |
| 70 | + typename CTYPE_B, |
| 71 | + typename CTYPE_IN, |
| 72 | + typename CTYPE_OUT> |
| 73 | +struct AddInner<false, CTYPE_A, CTYPE_B, CTYPE_IN, CTYPE_OUT> |
| 74 | + : public ReportCanCastBug<CTYPE_IN> {}; |
| 75 | + |
| 76 | +} // namespace |
| 77 | + |
| 78 | +Tensor& add_out( |
| 79 | + KernelRuntimeContext& ctx, |
| 80 | + const Tensor& a, |
| 81 | + const Tensor& b, |
| 82 | + const Scalar& alpha, |
| 83 | + Tensor& out) { |
| 84 | + ET_KERNEL_CHECK( |
| 85 | + ctx, |
| 86 | + torch::executor::resize_to_broadcast_target_size(a, b, out) == Error::Ok, |
| 87 | + InvalidArgument, |
| 88 | + out); |
| 89 | + |
| 90 | + ET_KERNEL_CHECK( |
| 91 | + ctx, |
| 92 | + executorch::runtime::tensor_is_realhbbf16_type(out), |
| 93 | + InvalidArgument, |
| 94 | + out); |
| 95 | + ET_KERNEL_CHECK( |
| 96 | + ctx, |
| 97 | + executorch::runtime::tensors_have_same_dim_order(a, b, out), |
| 98 | + InvalidArgument, |
| 99 | + out); |
| 100 | + |
| 101 | + ScalarType a_type = a.scalar_type(); |
| 102 | + ScalarType b_type = b.scalar_type(); |
| 103 | + ScalarType alpha_type = |
| 104 | + torch::executor::native::utils::get_scalar_dtype(alpha); |
| 105 | + ScalarType common_type = |
| 106 | + executorch::runtime::promoteTypes(a_type, b_type, /*half_to_float*/ true); |
| 107 | + ScalarType out_type = out.scalar_type(); |
| 108 | + |
| 109 | + ET_KERNEL_CHECK( |
| 110 | + ctx, |
| 111 | + executorch::runtime::canCast(common_type, out_type), |
| 112 | + InvalidArgument, |
| 113 | + out); |
| 114 | + ET_KERNEL_CHECK( |
| 115 | + ctx, |
| 116 | + torch::executor::check_alpha_type(alpha_type, common_type), |
| 117 | + InvalidArgument, |
| 118 | + out); |
| 119 | + |
| 120 | + float alpha_val; |
| 121 | + torch::executor::native::utils::extract_scalar(alpha, &alpha_val); |
| 122 | + |
| 123 | + constexpr auto name = "add.out"; |
| 124 | + constexpr int kNnlibMaxDim = 4; /*fallback if broadcast and dim > 4 */ |
| 125 | + |
| 126 | + int a_dim = a.dim(), b_dim = b.dim(), out_dim = out.dim(); |
| 127 | + bool optimized = 1; |
| 128 | + /*find broadcast*/ |
| 129 | + const bool a_is_broadcasted = !out.sizes().equals(a.sizes()); |
| 130 | + const bool b_is_broadcasted = !out.sizes().equals(b.sizes()); |
| 131 | + const bool broadcast = (a_is_broadcasted || b_is_broadcasted); |
| 132 | + int max_dim = a.dim() > b.dim() ? a.dim() : b.dim(); |
| 133 | + max_dim = out.dim() > max_dim ? out.dim() : max_dim; |
| 134 | + |
| 135 | + if ((out_type != ScalarType::Float) || (alpha_val != 1.0)) |
| 136 | + optimized = 0; |
| 137 | + |
| 138 | + if ((a_dim == 0) || (b_dim == 0)) |
| 139 | + optimized = 0; |
| 140 | + |
| 141 | + if ((broadcast == 1) && (max_dim > kNnlibMaxDim)) |
| 142 | + optimized = 0; |
| 143 | + |
| 144 | + if (optimized) { |
| 145 | + const float* const a_data = a.const_data_ptr<float>(); |
| 146 | + const float* const b_data = b.const_data_ptr<float>(); |
| 147 | + float* const out_data = out.mutable_data_ptr<float>(); |
| 148 | + |
| 149 | + if (broadcast == 1) { |
| 150 | + int out_shape[kNnlibMaxDim]; |
| 151 | + int inp1_shape[kNnlibMaxDim]; |
| 152 | + int inp2_shape[kNnlibMaxDim]; |
| 153 | + |
| 154 | + for (int i = 0; i < kNnlibMaxDim; i++) { |
| 155 | + out_shape[i] = 1; |
| 156 | + inp1_shape[i] = 1; |
| 157 | + inp2_shape[i] = 1; |
| 158 | + } |
| 159 | + |
| 160 | + int off_o = kNnlibMaxDim - out.dim(); |
| 161 | + int off_a = kNnlibMaxDim - a.dim(); |
| 162 | + int off_b = kNnlibMaxDim - b.dim(); |
| 163 | + |
| 164 | + for (int i = 0; i < out.dim(); i++) |
| 165 | + out_shape[i + off_o] = out.size(i); |
| 166 | + for (int i = 0; i < a.dim(); i++) |
| 167 | + inp1_shape[i + off_a] = a.size(i); |
| 168 | + for (int i = 0; i < b.dim(); i++) |
| 169 | + inp2_shape[i + off_b] = b.size(i); |
| 170 | + |
| 171 | + xa_nn_elm_add_broadcast_4D_f32xf32_f32( |
| 172 | + out_data, out_shape, a_data, inp1_shape, b_data, inp2_shape); |
| 173 | + } else { |
| 174 | + xa_nn_elm_add_f32xf32_f32(out_data, a_data, b_data, out.numel()); |
| 175 | + } |
| 176 | + |
| 177 | + return out; |
| 178 | + } |
| 179 | + |
| 180 | + ET_SWITCH_REALHBBF16_TYPES(a_type, ctx, name, CTYPE_A, [&]() { |
| 181 | + ET_SWITCH_REALHBBF16_TYPES(b_type, ctx, name, CTYPE_B, [&]() { |
| 182 | + using CTYPE_IN = typename torch::executor:: |
| 183 | + promote_types<CTYPE_A, CTYPE_B, /*half_to_float*/ true>::type; |
| 184 | + ET_DCHECK(CppTypeToScalarType<CTYPE_IN>::value == common_type); |
| 185 | + CTYPE_IN alpha_val; |
| 186 | + torch::executor::native::utils::extract_scalar(alpha, &alpha_val); |
| 187 | + |
| 188 | + ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE_OUT, [&]() { |
| 189 | + AddInner< |
| 190 | + can_cast<CTYPE_IN, CTYPE_OUT>::value, |
| 191 | + CTYPE_A, |
| 192 | + CTYPE_B, |
| 193 | + CTYPE_IN, |
| 194 | + CTYPE_OUT>::run(a, b, alpha_val, out); |
| 195 | + }); |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + return out; |
| 200 | +} |
| 201 | + |
| 202 | +} // namespace native |
| 203 | +} // namespace HiFi |
| 204 | +} // namespace impl |
0 commit comments