|
| 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/scalar_utils.h> |
| 10 | +#include <executorch/kernels/portable/cpu/util/broadcast_util.h> |
| 11 | +#include <executorch/kernels/portable/cpu/util/functional_util.h> |
| 12 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 13 | +#include <executorch/runtime/platform/assert.h> |
| 14 | +#include "kernels.h" |
| 15 | + |
| 16 | +namespace torch { |
| 17 | +namespace executor { |
| 18 | +namespace native { |
| 19 | + |
| 20 | +#define NNLIB_MAX_DIM 4 /* Add fallback if broadcast and dim > 4 */ |
| 21 | + |
| 22 | +Tensor& add_out( |
| 23 | + RuntimeContext& ctx, |
| 24 | + const Tensor& a, |
| 25 | + const Tensor& b, |
| 26 | + const Scalar& alpha, |
| 27 | + Tensor& out) { |
| 28 | + (void)ctx; |
| 29 | + |
| 30 | + ScalarType a_type = a.scalar_type(); |
| 31 | + ScalarType b_type = b.scalar_type(); |
| 32 | + ScalarType common_type = promoteTypes(a_type, b_type); |
| 33 | + ScalarType out_type = out.scalar_type(); |
| 34 | + |
| 35 | + ET_CHECK_MSG(a_type == ScalarType::Float, "Input tensor not a float.\n"); |
| 36 | + ET_CHECK_MSG(b_type == ScalarType::Float, "Input tensor not a float.\n"); |
| 37 | + ET_CHECK_MSG(out_type == ScalarType::Float, "Output tensor not a float.\n"); |
| 38 | + |
| 39 | + ET_CHECK(canCast(common_type, out_type)); |
| 40 | + |
| 41 | + using CTYPE_A = float; |
| 42 | + using CTYPE_B = float; |
| 43 | + using CTYPE_IN = float; |
| 44 | + using CTYPE_OUT = float; |
| 45 | + CTYPE_IN alpha_val; |
| 46 | + ET_EXTRACT_SCALAR(alpha, alpha_val); |
| 47 | + |
| 48 | + int a_dim = a.dim(), b_dim = b.dim(), out_dim = out.dim(); |
| 49 | + int fall_back = 0; |
| 50 | + /*find broadcast*/ |
| 51 | + const int a_is_broadcasted = !out.sizes().equals(a.sizes()); |
| 52 | + const int b_is_broadcasted = !out.sizes().equals(b.sizes()); |
| 53 | + const int broadcast = (a_is_broadcasted || b_is_broadcasted); |
| 54 | + int max_dim = a.dim() > b.dim() ? a.dim() : b.dim(); |
| 55 | + max_dim = out.dim() > max_dim ? out.dim() : max_dim; |
| 56 | + |
| 57 | + if( (out_type != ScalarType::Float) || (alpha_val != 1.0)) |
| 58 | + fall_back = 1; |
| 59 | + |
| 60 | + if( (a_dim == 0) || (b_dim == 0) ) |
| 61 | + fall_back = 1; |
| 62 | + |
| 63 | + if((broadcast == 1) && (max_dim > NNLIB_MAX_DIM)) |
| 64 | + fall_back = 1; |
| 65 | + |
| 66 | + |
| 67 | + if (!fall_back) |
| 68 | + { |
| 69 | + const float* const a_data = a.const_data_ptr<float>(); |
| 70 | + const float* const b_data = b.const_data_ptr<float>(); |
| 71 | + float* const out_data = out.mutable_data_ptr<float>(); |
| 72 | + if(broadcast == 1) |
| 73 | + { |
| 74 | + int out_shape[NNLIB_MAX_DIM]; |
| 75 | + int inp1_shape[NNLIB_MAX_DIM]; |
| 76 | + int inp2_shape[NNLIB_MAX_DIM]; |
| 77 | + |
| 78 | + for(int i = 0; i < NNLIB_MAX_DIM; i++) |
| 79 | + { |
| 80 | + out_shape[i] = 1; |
| 81 | + inp1_shape[i] = 1; |
| 82 | + inp2_shape[i] = 1; |
| 83 | + } |
| 84 | + |
| 85 | + int off_o = NNLIB_MAX_DIM - out.dim(); |
| 86 | + int off_a = NNLIB_MAX_DIM - a.dim(); |
| 87 | + int off_b = NNLIB_MAX_DIM - b.dim(); |
| 88 | + |
| 89 | + for(int i = 0; i < out.dim(); i++) |
| 90 | + out_shape[i+off_o] = out.size(i); |
| 91 | + for(int i = 0; i < a.dim(); i++) |
| 92 | + inp1_shape[i+off_a] = a.size(i); |
| 93 | + for(int i = 0; i < b.dim(); i++) |
| 94 | + inp2_shape[i+off_b] = b.size(i); |
| 95 | + |
| 96 | + xa_nn_elm_add_broadcast_4D_f32xf32_f32(out_data, out_shape, a_data, inp1_shape, |
| 97 | + b_data, inp2_shape); |
| 98 | + } |
| 99 | + else |
| 100 | + xa_nn_elm_add_f32xf32_f32(out_data, a_data, b_data, out.numel()); |
| 101 | + |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>( |
| 106 | + [alpha_val](const CTYPE_A val_a, const CTYPE_B val_b) { |
| 107 | + CTYPE_IN a_casted = static_cast<CTYPE_IN>(val_a); |
| 108 | + CTYPE_IN b_casted = static_cast<CTYPE_IN>(val_b); |
| 109 | + CTYPE_IN value = a_casted + alpha_val * b_casted; |
| 110 | + |
| 111 | + return static_cast<CTYPE_OUT>(value); |
| 112 | + }, |
| 113 | + a, |
| 114 | + b, |
| 115 | + out); |
| 116 | + } |
| 117 | + |
| 118 | + return out; |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace native |
| 122 | +} // namespace executor |
| 123 | +} // namespace torch |
0 commit comments