-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathop_atan2.cpp
228 lines (177 loc) · 7.29 KB
/
op_atan2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* 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 <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <executorch/kernels/portable/cpu/util/broadcast_util.h>
#include <executorch/kernels/portable/cpu/util/elementwise_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <cmath>
using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using executorch::runtime::isFloatingType;
using executorch::runtime::KernelRuntimeContext;
using executorch::runtime::promoteTypes;
using executorch::runtime::tensors_have_same_dim_order;
using torch::executor::Error;
using torch::executor::resize_to_broadcast_target_size;
using torch::executor::native::utils::apply_bitensor_elementwise_fn;
using torch::executor::native::utils::get_compute_type;
using torch::executor::native::utils::SupportedTensorDtypes;
namespace cadence {
namespace impl {
namespace HiFi {
namespace native {
namespace {
ScalarType get_common_type(ScalarType a_type, ScalarType b_type) {
if (isFloatingType(a_type) && isFloatingType(b_type)) {
return promoteTypes(a_type, b_type);
} else if (isFloatingType(a_type)) {
return a_type;
} else if (isFloatingType(b_type)) {
return b_type;
}
return ScalarType::Float;
}
} // namespace
Tensor& atan2_out(
KernelRuntimeContext& ctx,
const Tensor& a,
const Tensor& b,
Tensor& out) {
// Common Dtype
ScalarType common_type = get_common_type(a.scalar_type(), b.scalar_type());
// Check Dim Order
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, b, out), InvalidArgument, out);
// Determine output size and resize for dynamic shapes
ET_KERNEL_CHECK(
ctx,
resize_to_broadcast_target_size(a, b, out) == Error::Ok,
InvalidArgument,
out);
ScalarType out_type = out.scalar_type();
ScalarType compute_type = get_compute_type(common_type);
static constexpr const char op_name[] = "atan2.out";
constexpr int kNnlibMaxDim = 16;
int a_dim = a.dim(), b_dim = b.dim(), out_dim = out.dim();
bool optimized = true;
const bool a_is_broadcasted = !out.sizes().equals(a.sizes());
const bool b_is_broadcasted = !out.sizes().equals(b.sizes());
const bool broadcast = (a_is_broadcasted && b_is_broadcasted);
int max_dim = a.dim() > b.dim() ? a.dim() : b.dim();
max_dim = out.dim() > max_dim ? out.dim() : max_dim;
if (out_type != ScalarType::Float)
optimized = false;
if (max_dim > kNnlibMaxDim)
optimized = false;
WORD32 num_elm = out.numel();
if (optimized) {
if (broadcast) {
WORD32* __restrict__ ptr1 =
(WORD32* __restrict__)kernels::allocate_temp_memory(
ctx, num_elm * sizeof(WORD32));
ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out);
WORD32* __restrict__ ptr2 =
(WORD32* __restrict__)kernels::allocate_temp_memory(
ctx, num_elm * sizeof(WORD32));
ET_KERNEL_CHECK(ctx, ptr2 != nullptr, MemoryAllocationFailed, out);
WORD32* __restrict__ pin1 =
(WORD32* __restrict__)a.const_data_ptr<float>();
WORD32* __restrict__ pin2 =
(WORD32* __restrict__)b.const_data_ptr<float>();
WORD32 p_out_shape[kNnlibMaxDim];
WORD32 p_inp1_shape[kNnlibMaxDim];
WORD32 p_inp2_shape[kNnlibMaxDim];
for (int i = 0; i < out_dim; i++)
p_out_shape[i] = out.size(i);
for (int i = 0; i < a_dim; i++)
p_inp1_shape[i] = a.size(i);
for (int i = 0; i < b_dim; i++)
p_inp2_shape[i] = b.size(i);
WORD32 ret_val =
xa_nn_broadcast_32_32(ptr1, p_out_shape, pin1, p_inp1_shape, out_dim);
ET_KERNEL_CHECK(ctx, ret_val == 0, Internal, out);
ret_val =
xa_nn_broadcast_32_32(ptr2, p_out_shape, pin2, p_inp2_shape, out_dim);
ET_KERNEL_CHECK(ctx, ret_val == 0, Internal, out);
FLOAT32* __restrict__ p_out =
(FLOAT32* __restrict__)out.mutable_data_ptr<float>();
const FLOAT32* __restrict__ p_inp1 = (const FLOAT32* __restrict__)ptr1;
const FLOAT32* __restrict__ p_inp2 = (const FLOAT32* __restrict__)ptr2;
xa_nn_elm_atan2_f32(p_out, p_inp1, p_inp2, num_elm);
} else if (a_is_broadcasted && (!b_is_broadcasted)) {
FLOAT32* __restrict__ ptr1 =
(FLOAT32* __restrict__)kernels::allocate_temp_memory(
ctx, num_elm * sizeof(WORD32));
ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out);
FLOAT32* __restrict__ pin1 =
(FLOAT32* __restrict__)a.const_data_ptr<float>();
WORD32 p_out_shape[kNnlibMaxDim];
WORD32 p_inp1_shape[kNnlibMaxDim];
for (int i = 0; i < out_dim; i++)
p_out_shape[i] = out.size(i);
for (int i = 0; i < a_dim; i++)
p_inp1_shape[i] = a.size(i);
WORD32 ret_val = xa_nn_broadcast_32_32(
(WORD32*)ptr1, p_out_shape, (WORD32*)pin1, p_inp1_shape, out_dim);
ET_KERNEL_CHECK(ctx, ret_val == 0, Internal, out);
FLOAT32* __restrict__ p_out =
(FLOAT32* __restrict__)out.mutable_data_ptr<float>();
const FLOAT32* __restrict__ p_inp1 = (const FLOAT32* __restrict__)ptr1;
const FLOAT32* __restrict__ p_inp2 =
(const FLOAT32* __restrict__)b.const_data_ptr<float>();
xa_nn_elm_atan2_f32(p_out, p_inp1, p_inp2, num_elm);
} else if (b_is_broadcasted && (!a_is_broadcasted)) {
WORD32* __restrict__ ptr1 =
(WORD32* __restrict__)kernels::allocate_temp_memory(
ctx, num_elm * sizeof(WORD32));
ET_KERNEL_CHECK(ctx, ptr1 != nullptr, MemoryAllocationFailed, out);
WORD32* __restrict__ pin1 =
(WORD32* __restrict__)b.const_data_ptr<float>();
WORD32 p_out_shape[kNnlibMaxDim];
WORD32 p_inp1_shape[kNnlibMaxDim];
for (int i = 0; i < out_dim; i++)
p_out_shape[i] = out.size(i);
for (int i = 0; i < b_dim; i++)
p_inp1_shape[i] = b.size(i);
xa_nn_broadcast_32_32(ptr1, p_out_shape, pin1, p_inp1_shape, out_dim);
FLOAT32* __restrict__ p_out =
(FLOAT32* __restrict__)out.mutable_data_ptr<float>();
const FLOAT32* __restrict__ p_inp1 =
(const FLOAT32* __restrict__)a.const_data_ptr<float>();
const FLOAT32* __restrict__ p_inp2 = (const FLOAT32* __restrict__)ptr1;
xa_nn_elm_atan2_f32(p_out, p_inp1, p_inp2, num_elm);
} else {
FLOAT32* __restrict__ p_out =
(FLOAT32* __restrict__)out.mutable_data_ptr<float>();
const FLOAT32* __restrict__ p_inp1 =
(const FLOAT32* __restrict__)a.const_data_ptr<float>();
const FLOAT32* __restrict__ p_inp2 =
(const FLOAT32* __restrict__)b.const_data_ptr<float>();
xa_nn_elm_atan2_f32(p_out, p_inp1, p_inp2, num_elm);
}
return out;
}
ET_SWITCH_FLOAT_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() {
apply_bitensor_elementwise_fn<CTYPE_COMPUTE, op_name>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
return std::atan2(val_a, val_b);
},
ctx,
a,
SupportedTensorDtypes::REALHBBF16,
b,
SupportedTensorDtypes::REALHBBF16,
out,
SupportedTensorDtypes::FLOATHBF16);
});
return out;
}
} // namespace native
} // namespace HiFi
} // namespace impl
} // namespace cadence