-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathop_amax.cpp
67 lines (56 loc) · 1.82 KB
/
op_amax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <c10/util/irange.h>
#include <cmath>
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <executorch/runtime/platform/assert.h>
namespace torch {
namespace executor {
namespace native {
using Tensor = executorch::aten::Tensor;
using ScalarType = executorch::aten::ScalarType;
Tensor& amax_out(
KernelRuntimeContext& ctx,
const Tensor& in,
ArrayRef<int64_t> dim_list,
bool keepdim,
Tensor& out) {
(void)ctx;
ET_KERNEL_CHECK(
ctx,
check_amin_amax_args(in, dim_list, keepdim, out),
InvalidArgument,
out);
ET_KERNEL_CHECK(
ctx,
resize_reduction_out(in, dim_list, keepdim, out) == Error::Ok,
InvalidArgument,
out);
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
ReduceOverDimListPlan plan(in, dim_list);
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, "amax.out", CTYPE, [&]() {
CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
const bool success = parallel_for_each_reduce_over_dim_list_output_index(
in, dim_list, out, [&](const auto begin, const auto end) {
for (const auto out_ix : c10::irange(begin, end)) {
out_data[out_ix] = plan.execute<CTYPE>(
[](CTYPE v, CTYPE max_v) {
return std::isnan(v) || v > max_v ? v : max_v;
},
out_ix);
}
});
ET_KERNEL_CHECK_MSG(ctx, success, Internal, , "parallel_for failed");
});
return out;
}
} // namespace native
} // namespace executor
} // namespace torch