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_realhbbf16_to_floathbf16(double (* fn)(double),KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)18 Tensor& unary_ufunc_realhbbf16_to_floathbf16(
19     double (*fn)(double),
20     KernelRuntimeContext& ctx,
21     const Tensor& in,
22     Tensor& out) {
23   (void)ctx;
24 
25   ET_KERNEL_CHECK(ctx, tensor_is_floating_type(out), InvalidArgument, out);
26 
27   // Resize for dynamic shape
28   ET_KERNEL_CHECK_MSG(
29       ctx,
30       resize_tensor(out, in.sizes()) == Error::Ok,
31       InvalidArgument,
32       out,
33       "Failed to resize output tensor.");
34 
35   ET_KERNEL_CHECK(
36       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
37 
38   const auto in_type = in.scalar_type();
39   const auto out_type = out.scalar_type();
40 
41   ET_SWITCH_REALHBBF16_TYPES(in_type, ctx, __func__, CTYPE_IN, [&] {
42     ET_SWITCH_FLOATHBF16_TYPES(out_type, ctx, __func__, CTYPE_OUT, [&] {
43       apply_unary_map_fn(
44           [fn](const CTYPE_IN val_in) {
45             CTYPE_OUT xi = static_cast<CTYPE_OUT>(val_in);
46             return static_cast<CTYPE_OUT>(fn(xi));
47           },
48           in.const_data_ptr<CTYPE_IN>(),
49           out.mutable_data_ptr<CTYPE_OUT>(),
50           in.numel());
51     });
52   });
53 
54   return out;
55 }
56 
57 } // namespace internal
58 } // namespace native
59 } // namespace executor
60 } // namespace torch
61