xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/pattern/unary_ufunc_realh.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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/kernels/portable/cpu/util/functional_util.h>
11 #include <executorch/runtime/kernel/kernel_includes.h>
12 
13 namespace torch {
14 namespace executor {
15 namespace native {
16 namespace internal {
17 
unary_ufunc_realh(double (* fn)(double),KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)18 Tensor& unary_ufunc_realh(
19     double (*fn)(double),
20     KernelRuntimeContext& ctx,
21     const Tensor& in,
22     Tensor& out) {
23   (void)ctx;
24 
25   // Resize for dynamic shape
26   ET_KERNEL_CHECK_MSG(
27       ctx,
28       resize_tensor(out, in.sizes()) == Error::Ok,
29       InvalidArgument,
30       out,
31       "Failed to resize output tensor.");
32 
33   ET_KERNEL_CHECK(
34       ctx, tensors_have_same_shape_and_dtype(in, out), InvalidArgument, out);
35 
36   ET_KERNEL_CHECK(
37       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
38 
39   ET_SWITCH_REALH_TYPES(in.scalar_type(), ctx, __func__, CTYPE, [&] {
40     apply_unary_map_fn(
41         [fn](const CTYPE val_in) { return static_cast<CTYPE>(fn(val_in)); },
42         in.const_data_ptr<CTYPE>(),
43         out.mutable_data_ptr<CTYPE>(),
44         in.numel());
45   });
46 
47   return out;
48 }
49 
50 } // namespace internal
51 } // namespace native
52 } // namespace executor
53 } // namespace torch
54