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 <cmath>
10
11 #include <executorch/kernels/portable/cpu/scalar_utils.h>
12 #include <executorch/kernels/portable/cpu/util/functional_util.h>
13 #include <executorch/runtime/kernel/kernel_includes.h>
14 #include <executorch/runtime/platform/assert.h>
15
16 namespace torch {
17 namespace executor {
18 namespace native {
19
20 using Tensor = exec_aten::Tensor;
21 using ScalarType = exec_aten::ScalarType;
22
leaky_relu_out(KernelRuntimeContext & ctx,const Tensor & in,const Scalar & negative_slope,Tensor & out)23 Tensor& leaky_relu_out(
24 KernelRuntimeContext& ctx,
25 const Tensor& in,
26 const Scalar& negative_slope,
27 Tensor& out) {
28 (void)ctx;
29
30 // Resize for dynamic shape
31 ET_KERNEL_CHECK_MSG(
32 ctx,
33 resize_tensor(out, in.sizes()) == Error::Ok,
34 InvalidArgument,
35 out,
36 "Failed to resize output tensor.");
37
38 ET_KERNEL_CHECK(
39 ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
40
41 ScalarType in_type = in.scalar_type();
42 ScalarType sc_type = utils::get_scalar_dtype(negative_slope);
43 ScalarType out_type = out.scalar_type();
44
45 ET_KERNEL_CHECK(ctx, in_type == out_type, InvalidArgument, out);
46
47 ET_SWITCH_FLOAT_TYPES(in_type, ctx, "leaky_relu.out", CTYPE, [&]() {
48 CTYPE negative_slope_casted;
49 ET_SWITCH_SCALAR_OBJ_TYPES(
50 sc_type, ctx, "leaky_relu.out", CTYPE_MIN, [&]() {
51 CTYPE_MIN negative_slope_val;
52 utils::extract_scalar(negative_slope, &negative_slope_val);
53 negative_slope_casted = static_cast<CTYPE>(negative_slope_val);
54 });
55
56 apply_unary_map_fn(
57 [negative_slope_casted](const CTYPE val_in) {
58 if (val_in >= 0) {
59 return val_in;
60 } else {
61 return val_in * negative_slope_casted;
62 }
63 },
64 in.const_data_ptr<CTYPE>(),
65 out.mutable_data_ptr<CTYPE>(),
66 in.numel());
67 });
68
69 return out;
70 }
71
72 } // namespace native
73 } // namespace executor
74 } // namespace torch
75