Skip to content

Commit d04c9d6

Browse files
authored
HiFi4 NNLib added as a third party lib for Cadence DSP backend compilation
Differential Revision: D60987573 Pull Request resolved: #4418
1 parent d3367e6 commit d04c9d6

File tree

11 files changed

+189
-21
lines changed

11 files changed

+189
-21
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
[submodule "third-party/ios-cmake"]
6262
path = third-party/ios-cmake
6363
url = https://github.com/leetal/ios-cmake
64+
[submodule "backends/cadence/hifi/third-party/nnlib/nnlib-hifi4"]
65+
path = backends/cadence/hifi/third-party/nnlib/nnlib-hifi4
66+
url = https://github.com/foss-xtensa/nnlib-hifi4.git
6467
[submodule "third-party/prelude"]
6568
path = third-party/prelude
6669
url = https://github.com/facebook/buck2-prelude.git

backends/cadence/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2323

2424
# Let files say "include <executorch/path/to/header.h>".
2525
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
26+
set(TARGET_DIR reference)
2627

28+
if(EXECUTORCH_NNLIB_OPT)
29+
set(TARGET_DIR hifi)
30+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/third-party/nnlib)
31+
endif()
2732

28-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/reference/operators)
29-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/reference/kernels)
33+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators)
34+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/kernels)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This yaml file contains operators that are also defined by the ATen library.
4+
# For lean mode:
5+
# - Codegen'd target `executorch_generated_lib` will be reading all the information
6+
# from this file, including operator schema and kernel metadata.
7+
# - Selective build target `codegen:executorch_defined_ops` now is selecting all the
8+
# operators in this file, by dumping all the op names into `selected_operators.yaml`.
9+
#
10+
# See the README.md file in executorch/kernels/portable for a description of the syntax used
11+
# by this file.
12+
13+
14+
# aten ops
15+
- op: _to_copy.out
16+
kernels:
17+
- arg_meta: null
18+
kernel_name: torch::executor::to_copy_out
19+
20+
- op: _softmax.out
21+
kernels:
22+
- arg_meta: null
23+
kernel_name: torch::executor::softmax_out
24+
25+
- op: add.out
26+
kernels:
27+
- arg_meta: null
28+
kernel_name: torch::executor::add_out
29+
30+
- op: bmm.out
31+
kernels:
32+
- arg_meta: null
33+
kernel_name: torch::executor::bmm_out
34+
35+
- op: cat.out
36+
kernels:
37+
- arg_meta: null
38+
kernel_name: torch::executor::cat_out
39+
40+
- op: clone.out
41+
kernels:
42+
- arg_meta: null
43+
kernel_name: torch::executor::clone_out
44+
45+
- op: div.out
46+
kernels:
47+
- arg_meta: null
48+
kernel_name: torch::executor::div_out
49+
50+
- op: div.out_mode
51+
kernels:
52+
- arg_meta: null
53+
kernel_name: torch::executor::div_out_mode
54+
55+
- op: embedding.out
56+
kernels:
57+
- arg_meta: null
58+
kernel_name: torch::executor::embedding_out
59+
60+
- op: full.out
61+
kernels:
62+
- arg_meta: null
63+
kernel_name: torch::executor::full_out
64+
65+
- op: mul.out
66+
kernels:
67+
- arg_meta: null
68+
kernel_name: torch::executor::mul_out
69+
70+
- op: permute_copy.out
71+
kernels:
72+
- arg_meta: null
73+
kernel_name: torch::executor::permute_copy_out
74+
75+
- op: sigmoid.out
76+
kernels:
77+
- arg_meta: null
78+
kernel_name: torch::executor::sigmoid_out
79+
80+
- op: slice_copy.Tensor_out
81+
kernels:
82+
- arg_meta: null
83+
kernel_name: torch::executor::slice_copy_Tensor_out
84+
85+
- op: split_with_sizes_copy.out
86+
kernels:
87+
- arg_meta: null
88+
kernel_name: torch::executor::split_with_sizes_copy_out
89+
90+
- op: sub.out
91+
kernels:
92+
- arg_meta: null
93+
kernel_name: torch::executor::sub_out
94+
95+
- op: view_copy.out
96+
kernels:
97+
- arg_meta: null
98+
kernel_name: torch::executor::view_copy_out
99+
100+
- op: where.self_out
101+
kernels:
102+
- arg_meta: null
103+
kernel_name: torch::executor::where_out
104+
105+
# custom ops
106+
- func: cadence::quantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)
107+
variants: function
108+
kernels:
109+
- arg_meta: null
110+
kernel_name: impl::HiFi::quantize_per_tensor_out
111+
112+
- func: cadence::dequantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)
113+
variants: function
114+
kernels:
115+
- arg_meta: null
116+
kernel_name: impl::HiFi::dequantize_per_tensor_out
117+
118+
119+
- func: cadence::quantized_layer_norm.out(Tensor input, Tensor in_scale, Tensor in_zero_point, int[] normalized_shape, Tensor weight, Tensor bias, float eps, float output_scale, int output_zero_point, *, Tensor(a!) out) -> Tensor(a!)
120+
kernels:
121+
- arg_meta: null
122+
kernel_name: impl::HiFi::quantized_layer_norm_out
123+
124+
- func: cadence::quantized_linear.out(Tensor src, Tensor weight, Tensor bias, int src_zero_point, Tensor weight_zero_point, Tensor out_multiplier, Tensor out_shift, int out_zero_point, Tensor? offset, *, Tensor(a!) out) -> Tensor(a!)
125+
kernels:
126+
- arg_meta: null
127+
kernel_name: impl::HiFi::quantized_linear_out

backends/cadence/hifi/kernels/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ add_library(
1414
target_include_directories(
1515
cadence_kernels
1616
PUBLIC .
17-
${NN_LIB_BASE_DIR}/xa_nnlib/algo/common/include/
18-
${NN_LIB_BASE_DIR}/xa_nnlib/include/nnlib
19-
${NN_LIB_BASE_DIR}/xa_nnlib/include
20-
${NN_LIB_BASE_DIR}/xa_nnlib/algo/ndsp/hifi4/include/
21-
${NXP_SDK_ROOT_DIR}/middleware/dsp/naturedsp/hifi4/include/
17+
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/algo/common/include/
18+
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/include/nnlib
19+
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/include
20+
${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/algo/ndsp/hifi4/include/
2221
)
22+
23+
target_link_libraries(cadence_kernels PRIVATE xa_nnlib)

backends/cadence/hifi/kernels/kernels.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*/
88

99
#include "kernels.h"
10-
#include "NatureDSP_Signal.h"
11-
#include "NatureDSP_Signal_vector.h"
1210
#include "xa_nnlib_common.h"
1311
#include "xa_nnlib_common_macros.h"
1412

backends/cadence/hifi/kernels/kernels.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "stddef.h"
1313
#include "xa_type_def.h"
1414

15+
/* For NNLIB APIs */
16+
#include "xa_nnlib_kernels_api.h"
17+
1518
namespace impl {
1619
namespace HiFi {
1720
namespace kernels {

backends/cadence/hifi/operators/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ endif()
2020

2121
# ATen compliant ops that are needed to run this model.
2222
set(_aten_ops__srcs
23-
"${CMAKE_CURRENT_SOURCE_DIR}/op_add.cpp"
24-
"${CMAKE_CURRENT_SOURCE_DIR}/op_embedding.cpp"
25-
"${CMAKE_CURRENT_SOURCE_DIR}/op_full.cpp"
26-
"${CMAKE_CURRENT_SOURCE_DIR}/op_view_copy.cpp"
2723
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/activation_ops_util.cpp"
2824
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/copy_ops_util.cpp"
2925
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/broadcast_util.cpp"
@@ -32,10 +28,13 @@ set(_aten_ops__srcs
3228
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/matmul_ops_util.cpp"
3329
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/reduce_util.cpp"
3430
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/repeat_util.cpp"
31+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_add.cpp"
3532
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_bmm.cpp"
3633
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_cat.cpp"
3734
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_clone.cpp"
3835
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_div.cpp"
36+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_embedding.cpp"
37+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_full.cpp"
3938
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_mul.cpp"
4039
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_permute_copy.cpp"
4140
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_sigmoid.cpp"
@@ -44,6 +43,7 @@ set(_aten_ops__srcs
4443
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_split_with_sizes_copy.cpp"
4544
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_sub.cpp"
4645
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_to_copy.cpp"
46+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_view_copy.cpp"
4747
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_where.cpp")
4848
add_library(aten_ops_cadence ${_aten_ops__srcs})
4949
target_link_libraries(aten_ops_cadence PUBLIC executorch)
@@ -58,8 +58,8 @@ target_include_directories(aten_ops_cadence PUBLIC ${ROOT_DIR}/..
5858

5959
# Custom ops that are needed to run the test model.
6060
add_library(
61-
custom_ops "quantized_linear_out.cpp" "quantized_conv_out.cpp"
62-
"quantized_relu_out.cpp" "quantized_layer_norm.cpp"
61+
custom_ops "quantized_linear_out.cpp"
62+
"quantized_layer_norm.cpp"
6363
"quantize_per_tensor.cpp" "dequantize_per_tensor.cpp")
6464
target_include_directories(custom_ops PUBLIC ${ROOT_DIR}/..
6565
${CMAKE_BINARY_DIR}
@@ -72,11 +72,11 @@ target_link_libraries(custom_ops PRIVATE cadence_kernels)
7272
# Executorch (for runtime). Here select all ops in functions.yaml
7373
gen_selected_ops(
7474
LIB_NAME "cadence_ops_lib" OPS_SCHEMA_YAML
75-
"${CMAKE_CURRENT_LIST_DIR}/../../aot/functions.yaml" "" ""
75+
"${CMAKE_CURRENT_LIST_DIR}/../../aot/functions_hifi.yaml" "" ""
7676
)
7777
generate_bindings_for_kernels(
7878
LIB_NAME "cadence_ops_lib" OPS_SCHEMA_YAML
79-
FUNCTIONS_YAML ${CMAKE_CURRENT_SOURCE_DIR}/../../aot/functions.yaml
79+
FUNCTIONS_YAML ${CMAKE_CURRENT_SOURCE_DIR}/../../aot/functions_hifi.yaml
8080
)
8181
message("Generated files ${gen_command_sources}")
8282

backends/cadence/hifi/operators/dequantize_per_tensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void dequantize_per_tensor_out(
3535
out_data, input_data, scale, zero_point, numel);
3636
} else if (input.scalar_type() == ScalarType::Char) {
3737
const int8_t* input_data = input.const_data_ptr<int8_t>();
38-
impl::HiFi::kernels::dequantize<int8_t>(
39-
out_data, input_data, scale, zero_point, numel);
38+
xa_nn_elm_dequantize_asym8s_f32(
39+
out_data, input_data, zero_point, scale, numel);
4040
} else if (input.scalar_type() == ScalarType::Int) {
4141
const int32_t* input_data = input.const_data_ptr<int32_t>();
4242
impl::HiFi::kernels::dequantize<int32_t>(

backends/cadence/hifi/operators/quantize_per_tensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void quantize_per_tensor_out(
3737
out_data, input_data, 1. / scale, zero_point, numel);
3838
} else if (out.scalar_type() == ScalarType::Char) {
3939
int8_t* out_data = out.mutable_data_ptr<int8_t>();
40-
impl::HiFi::kernels::quantize<int8_t>(
41-
out_data, input_data, 1. / scale, zero_point, numel);
40+
xa_nn_elm_quantize_f32_asym8s(
41+
out_data, input_data, scale, zero_point, numel);
4242
} else if (out.scalar_type() == ScalarType::Int) {
4343
int32_t* out_data = out.mutable_data_ptr<int32_t>();
4444
impl::HiFi::kernels::quantize<int32_t>(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
cmake_minimum_required(VERSION 3.10.0)
3+
project(cadence_nnlib)
4+
5+
6+
add_custom_target( nnlib_target ALL COMMAND
7+
make install_nnlib -f makefile -C ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/build
8+
OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/obj
9+
LIBDIR=${CMAKE_CURRENT_BINARY_DIR}/lib
10+
-j8 )
11+
12+
add_library(xa_nnlib STATIC IMPORTED GLOBAL)
13+
add_dependencies(xa_nnlib nnlib_target)
14+
15+
set_property(
16+
TARGET xa_nnlib
17+
PROPERTY
18+
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lib/xa_nnlib.a"
19+
)
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+

0 commit comments

Comments
 (0)