Skip to content

Commit

Permalink
Buckify Cadence HiFi4 Operators. (#5154)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #5154

Add c++ code under cadence/hifi to buck build targets.

Reviewed By: zonglinpeng

Differential Revision: D62317545

fbshipit-source-id: e91cf9167fd6a701cd2e2ed52ea2cc6d07517cf4
  • Loading branch information
hsharma35 authored and facebook-github-bot committed Sep 18, 2024
1 parent 861a7bf commit 444480b
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 17 deletions.
5 changes: 5 additions & 0 deletions backends/cadence/hifi/kernels/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("targets.bzl", "define_common_targets")

oncall("odai_jarvis")

define_common_targets()
6 changes: 3 additions & 3 deletions backends/cadence/hifi/kernels/kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/cadence/reference/kernels/kernels.h>
#include "xa_nnlib_common.h"
#include "xa_nnlib_common_macros.h"
#include <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <xa_nnlib_common.h>
#include <xa_nnlib_common_macros.h>

namespace impl {
namespace HiFi {
Expand Down
9 changes: 3 additions & 6 deletions backends/cadence/hifi/kernels/kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

#pragma once

#include "inttypes.h"
#include "stddef.h"
#include "xa_type_def.h"

/* For NNLIB APIs */
#include "xa_nnlib_kernels_api.h"
#include <inttypes.h>
#include <stddef.h>
#include <xa_type_def.h>

namespace impl {
namespace HiFi {
Expand Down
18 changes: 18 additions & 0 deletions backends/cadence/hifi/kernels/targets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@fbsource//tools/build_defs:platform_defs.bzl", "CXX")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
runtime.cxx_library(
name = "kernels",
srcs = ["kernels.cpp"],
exported_headers = [
"kernels.h",
],
visibility = [
"//executorch/backends/cadence/...",
],
exported_deps = [
"fbsource//third-party/nnlib-hifi4/xa_nnlib:libxa_nnlib_common",
],
platforms = CXX,
)
5 changes: 5 additions & 0 deletions backends/cadence/hifi/operators/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("targets.bzl", "define_common_targets")

oncall("odai_jarvis")

define_common_targets()
3 changes: 2 additions & 1 deletion backends/cadence/hifi/operators/dequantize_per_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/cadence/reference/kernels/kernels.h>
#include <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <xa_nnlib_kernels_api.h>

namespace impl {
namespace HiFi {
Expand Down
3 changes: 2 additions & 1 deletion backends/cadence/hifi/operators/quantize_per_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/cadence/reference/kernels/kernels.h>
#include <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <xa_nnlib_kernels_api.h>

namespace impl {
namespace HiFi {
Expand Down
9 changes: 5 additions & 4 deletions backends/cadence/hifi/operators/quantized_layer_norm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/cadence/reference/kernels/kernels.h>
#include <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <executorch/runtime/kernel/kernel_includes.h>

#include <algorithm>
#include <cmath>
#include <tuple>
Expand Down Expand Up @@ -76,9 +75,11 @@ void quantized_layer_norm_(
for (size_t j = 0; j < last_dim; ++j) {
// Since X is quantized, we dequantize it, compute fp32 result, and
// quantize the result to an int8/uint8 value.
float val = kernels::dequantize<T>(x[j], input_scale, input_zero_point);
float val = impl::HiFi::kernels::dequantize<T>(
x[j], input_scale, input_zero_point);
val = (val - mean) * inv_std * weight_data[j] + bias_data[j];
y[j] = kernels::quantize<T>(val, output_inv_scale, output_zero_point);
y[j] = impl::HiFi::kernels::quantize<T>(
val, output_inv_scale, output_zero_point);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions backends/cadence/hifi/operators/quantized_linear_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/cadence/reference/kernels/kernels.h>

#include <executorch/backends/cadence/hifi/kernels/kernels.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <algorithm>
#include <cmath>
Expand Down
30 changes: 30 additions & 0 deletions backends/cadence/hifi/operators/targets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@fbsource//tools/build_defs:platform_defs.bzl", "CXX")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
"""Defines targets that should be shared between fbcode and xplat.
The directory containing this targets.bzl file should also contain both
TARGETS and BUCK files that call this function.
"""

# Define build targets for all operators registered in the tables above.

runtime.cxx_library(
name = "cadence_hifi_ops",
srcs = glob([
"*.cpp",
]),
platforms = CXX,
deps = [
"//executorch/kernels/portable/cpu/util:broadcast_util",
"//executorch/runtime/kernel:kernel_includes",
"//executorch/kernels/portable/cpu:scalar_utils",
"fbsource//third-party/nnlib-hifi4/xa_nnlib:libxa_nnlib",
"fbsource//third-party/nnlib-hifi4/xa_nnlib:libxa_nnlib_common",
"//executorch/backends/cadence/hifi/kernels:kernels",
],
visibility = [
"//executorch/backends/cadence/...",
],
)

0 comments on commit 444480b

Please sign in to comment.