xref: /aosp_15_r20/external/executorch/kernels/portable/cpu/op_amin.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 <cmath>
10 
11 #include <executorch/kernels/portable/cpu/util/reduce_util.h>
12 #include <executorch/runtime/kernel/kernel_includes.h>
13 #include <executorch/runtime/platform/assert.h>
14 
15 namespace torch {
16 namespace executor {
17 namespace native {
18 
19 using Tensor = exec_aten::Tensor;
20 using ScalarType = exec_aten::ScalarType;
21 
amin_out(KernelRuntimeContext & ctx,const Tensor & in,ArrayRef<int64_t> dim_list,bool keepdim,Tensor & out)22 Tensor& amin_out(
23     KernelRuntimeContext& ctx,
24     const Tensor& in,
25     ArrayRef<int64_t> dim_list,
26     bool keepdim,
27     Tensor& out) {
28   (void)ctx;
29 
30   ET_KERNEL_CHECK(
31       ctx,
32       check_amin_amax_args(in, dim_list, keepdim, out),
33       InvalidArgument,
34       out);
35 
36   ET_KERNEL_CHECK(
37       ctx,
38       resize_reduction_out(in, dim_list, keepdim, out) == Error::Ok,
39       InvalidArgument,
40       out);
41 
42   ET_KERNEL_CHECK(
43       ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
44 
45   ET_SWITCH_REAL_TYPES_AND(
46       Bool, in.scalar_type(), ctx, "amin.out", CTYPE, [&]() {
47         CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
48         for (size_t out_ix = 0; out_ix < out.numel(); ++out_ix) {
49           out_data[out_ix] = reduce_over_dim_list<CTYPE>(
50               [](CTYPE v, CTYPE min_v) {
51                 return std::isnan(v) || v < min_v ? v : min_v;
52               },
53               in,
54               dim_list,
55               out_ix);
56         }
57       });
58 
59   return out;
60 }
61 
62 } // namespace native
63 } // namespace executor
64 } // namespace torch
65