xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/pattern/unary_ufunc_realhb_to_bool.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_realhb_to_bool(bool (* fn)(double),KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)18 Tensor& unary_ufunc_realhb_to_bool(
19     bool (*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_MSG(
34       ctx,
35       out.scalar_type() == exec_aten::ScalarType::Bool,
36       InvalidArgument,
37       out,
38       "Expected out tensor to have dtype Bool, but got %" PRId8 " instead.",
39       static_cast<int8_t>(out.scalar_type()));
40 
41   ET_KERNEL_CHECK(
42       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
43 
44   const auto in_type = in.scalar_type();
45 
46   ET_SWITCH_REALHBBF16_TYPES(in_type, ctx, __func__, CTYPE_IN, [&] {
47     apply_unary_map_fn(
48         [fn](const CTYPE_IN val_in) { return fn(val_in); },
49         in.const_data_ptr<CTYPE_IN>(),
50         out.mutable_data_ptr<bool>(),
51         in.numel());
52   });
53 
54   return out;
55 }
56 
57 } // namespace internal
58 } // namespace native
59 } // namespace executor
60 } // namespace torch
61