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/runtime/kernel/kernel_includes.h>
12 #include <executorch/runtime/platform/assert.h>
13
14 namespace torch {
15 namespace executor {
16 namespace native {
17
add_out(KernelRuntimeContext & ctx,const Tensor & a,const Tensor & b,const Scalar & alpha,Tensor & out)18 Tensor& add_out(
19 KernelRuntimeContext& ctx,
20 const Tensor& a,
21 const Tensor& b,
22 const Scalar& alpha,
23 Tensor& out) {
24 (void)ctx;
25
26 ScalarType a_type = a.scalar_type();
27 ScalarType b_type = b.scalar_type();
28 ScalarType common_type = promoteTypes(a_type, b_type);
29 ScalarType out_type = out.scalar_type();
30
31 ET_CHECK_MSG(a_type == ScalarType::Float, "Input tensor not a float.\n");
32 ET_CHECK_MSG(b_type == ScalarType::Float, "Input tensor not a float.\n");
33 ET_CHECK_MSG(out_type == ScalarType::Float, "Output tensor not a float.\n");
34
35 ET_CHECK(canCast(common_type, out_type));
36
37 using CTYPE_A = float;
38 using CTYPE_B = float;
39 using CTYPE_IN = float;
40 using CTYPE_OUT = float;
41 CTYPE_IN alpha_val;
42 ET_EXTRACT_SCALAR(alpha, alpha_val);
43
44 apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>(
45 [alpha_val](const CTYPE_A val_a, const CTYPE_B val_b) {
46 CTYPE_IN a_casted = static_cast<CTYPE_IN>(val_a);
47 CTYPE_IN b_casted = static_cast<CTYPE_IN>(val_b);
48 CTYPE_IN value = a_casted + alpha_val * b_casted;
49
50 return static_cast<CTYPE_OUT>(value);
51 },
52 a,
53 b,
54 out);
55
56 return out;
57 }
58
59 } // namespace native
60 } // namespace executor
61 } // namespace torch
62