Skip to content

Commit

Permalink
Adding atan2 operator kernel optimization (#26)
Browse files Browse the repository at this point in the history
* Adding atan2 operator kernel optimization

* Adding atan2 operator kernel optimization

---------

Co-authored-by: dijopaul <[email protected]>
  • Loading branch information
Rushi-cad and dijopaul authored Oct 23, 2024
1 parent b2de35c commit 71a6da9
Show file tree
Hide file tree
Showing 7 changed files with 1,394 additions and 1 deletion.
5 changes: 5 additions & 0 deletions backends/cadence/aot/functions_hifi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
kernels:
- arg_meta: null
kernel_name: impl::HiFi::add_out

- op: atan2.out
kernels:
- arg_meta: null
kernel_name: impl::HiFi::atan2_out

- op: bmm.out
kernels:
Expand Down
3 changes: 2 additions & 1 deletion backends/cadence/hifi/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ add_library(
cadence_kernels
kernels.cpp
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/matmul_asym8uxasym8u_asym8u.cpp
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_broadcast_32_32.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_broadcast_32.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_elm_add_f32_broadcast.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_elm_atan2_f32.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_elm_clamp_f32_broadcast.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_elm_div_f32_broadcast.c
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/xa_nn_elm_div_mode_f32_broadcast.c
Expand Down
6 changes: 6 additions & 0 deletions backends/cadence/hifi/kernels/kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "xa_nnlib_kernels_api.h"

/* Potential NNLIB function/APIs */

extern "C" WORD32 xa_nn_broadcast_32_32(
WORD32* __restrict__ p_out,
const int* const out_shape,
Expand All @@ -31,6 +32,11 @@ extern "C" WORD32 xa_nn_elm_add_broadcast_4D_f32xf32_f32(
const FLOAT32* __restrict__ p_inp2,
const WORD32* const p_inp2_shape);

extern "C" void xa_nn_elm_atan2_f32(FLOAT32 * z,
const FLOAT32 * y,
const FLOAT32 * x,
WORD32 N );

extern "C" WORD32 xa_nn_elm_clamp_f32xf32xf32_f32(
FLOAT32* __restrict__ p_out,
const FLOAT32* __restrict__ p_inp,
Expand Down
1 change: 1 addition & 0 deletions backends/cadence/hifi/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ endif()
# ATen compliant ops that are needed to run this model.
set(_aten_ops__srcs
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_add.cpp"
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_atan2.cpp"
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_bmm.cpp"
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_clamp.cpp"
"${EXECUTORCH_ROOT}/backends/cadence/hifi/operators/op_div.cpp"
Expand Down
185 changes: 185 additions & 0 deletions backends/cadence/hifi/operators/op_atan2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* 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/runtime/kernel/kernel_includes.h>
#include <cmath>

using exec_aten::ScalarType;
using exec_aten::Tensor;
using executorch::runtime::KernelRuntimeContext;
using torch::executor::Error;

namespace impl {
namespace HiFi {
namespace native {

Tensor& atan2_out(
KernelRuntimeContext& ctx,
const Tensor& a,
const Tensor& b,
Tensor& 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);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, b, out), InvalidArgument, out);

ScalarType a_type = a.scalar_type();
ScalarType b_type = b.scalar_type();
ScalarType out_type = out.scalar_type();

constexpr auto name = "atan2.out";
constexpr int kNnlibMaxDim = 16;
int a_dim = a.dim(), b_dim = b.dim(), out_dim = out.dim();
bool optimized = 1;

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 = 0;

if ((broadcast == 1) && (max_dim > kNnlibMaxDim))
optimized = 0;

WORD32 num_elm = out.numel();

if (optimized) {
if (broadcast) {
WORD32* __restrict__ ptr1 =
(WORD32* __restrict__)malloc(num_elm * sizeof(WORD32));
WORD32* __restrict__ ptr2 =
(WORD32* __restrict__)malloc(num_elm * sizeof(WORD32));

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);

xa_nn_broadcast_32_32(ptr1, p_out_shape, pin1, p_inp1_shape, out_dim);

xa_nn_broadcast_32_32(ptr2, p_out_shape, pin2, p_inp2_shape, out_dim);

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);

free(ptr1);
free(ptr2);
} else if (a_is_broadcasted && (!b_is_broadcasted)) {
FLOAT32* __restrict__ ptr1 =
(FLOAT32* __restrict__)malloc(num_elm * sizeof(WORD32));

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);

xa_nn_broadcast_32_32(
(WORD32*)ptr1, p_out_shape, (WORD32*)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__)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);

free(ptr1);
} else if (b_is_broadcasted && (!a_is_broadcasted)) {
WORD32* __restrict__ ptr1 =
(WORD32* __restrict__)malloc(num_elm * sizeof(WORD32));

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);

free(ptr1);
} 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_REALHB_TYPES(a_type, ctx, name, CTYPE_A, [&]() {
ET_SWITCH_REALHB_TYPES(b_type, ctx, name, CTYPE_B, [&]() {
ET_SWITCH_FLOATH_TYPES(out_type, ctx, name, CTYPE_OUT, [&]() {
torch::executor::
apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>(
[](const CTYPE_A val_a, const CTYPE_B val_b) {
CTYPE_OUT casted_a = static_cast<CTYPE_OUT>(val_a);
CTYPE_OUT casted_b = static_cast<CTYPE_OUT>(val_b);
return static_cast<CTYPE_OUT>(std::atan2(casted_a, casted_b));
},
a,
b,
out);
});
});
});

return out;
}

} // namespace native
} // namespace HiFi
} // namespace impl
Loading

0 comments on commit 71a6da9

Please sign in to comment.