Skip to content

Commit c0b1005

Browse files
committed
Adding tanh optimizations
1 parent 6ad490a commit c0b1005

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

backends/cadence/aot/functions_hifi.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
- arg_meta: null
9393
kernel_name: torch::executor::sub_out
9494

95+
- op: tanh.out
96+
kernels:
97+
- arg_meta: null
98+
kernel_name: torch::executor::tanh_out
99+
95100
- op: view_copy.out
96101
kernels:
97102
- arg_meta: null

backends/cadence/hifi/operators/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(_aten_ops__srcs
2525
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_mul.cpp"
2626
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_sigmoid.cpp"
2727
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_sub.cpp"
28+
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_tanh.cpp"
2829
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_bmm.cpp"
2930
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_cat.cpp"
3031
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_clone.cpp"
@@ -37,6 +38,7 @@ set(_aten_ops__srcs
3738
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_to_copy.cpp"
3839
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_view_copy.cpp"
3940
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_where.cpp"
41+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/pattern/unary_ufunc_realhb_to_floath.cpp"
4042
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/activation_ops_util.cpp"
4143
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/broadcast_util.cpp"
4244
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/copy_ops_util.cpp"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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/pattern/pattern.h>
10+
#include <executorch/runtime/kernel/kernel_includes.h>
11+
#include <cmath>
12+
#include "kernels.h"
13+
14+
namespace torch {
15+
namespace executor {
16+
namespace native {
17+
18+
Tensor& tanh_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
19+
20+
int fall_back = 0;
21+
if((in.scalar_type() != ScalarType::Float) || (out.scalar_type() != ScalarType::Float))
22+
fall_back = 1;
23+
24+
if(!fall_back)
25+
{
26+
float* data_in = in.mutable_data_ptr<float>();
27+
float* data_out = out.mutable_data_ptr<float>();
28+
xa_nn_vec_tanh_f32_f32(data_out, data_in, (int)in.numel());
29+
return out;
30+
}
31+
else
32+
{
33+
return internal::unary_ufunc_realhb_to_floath(std::tanh, ctx, in, out);
34+
}
35+
36+
}
37+
38+
} // namespace native
39+
} // namespace executor
40+
} // namespace torch

0 commit comments

Comments
 (0)