Skip to content

Commit b1c94ab

Browse files
zonglinpengfacebook-github-bot
authored andcommitted
migrate jarvis quant-per-tensor hifi ops to oss (#6293)
Summary: Pull Request resolved: #6293 - only migrated **quant-per-tensor** in this diff. will do the rest of 3 in the stack - solved the --start-lib --end-lib option not recognized in here - need to import libs from //executorch. Nothing do with the cxx wrapper in buck - aligned namespace to **cadence::impl:HIFI::native and cadence::impl:HIFI::kernel** - kernel to be removed after all ops are migrated Reviewed By: skrtskrtfb, mcremon-meta Differential Revision: D64194227 fbshipit-source-id: 180ed472d9d9c5139064eb2e9b47480d80598f9d
1 parent fad26af commit b1c94ab

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

backends/cadence/aot/functions_hifi.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@
107107
variants: function
108108
kernels:
109109
- arg_meta: null
110-
kernel_name: impl::HiFi::quantize_per_tensor_out
110+
kernel_name: cadence::impl::HiFi::quantize_per_tensor_out
111111

112112
- 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!)
113113
variants: function
114114
kernels:
115115
- arg_meta: null
116-
kernel_name: impl::HiFi::dequantize_per_tensor_out
116+
kernel_name: cadence::impl::HiFi::dequantize_per_tensor_out
117117

118118

119119
- 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!)
120120
kernels:
121121
- arg_meta: null
122-
kernel_name: impl::HiFi::quantized_layer_norm_out
122+
kernel_name: cadence::impl::HiFi::quantized_layer_norm_out
123123

124124
- 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!)
125125
kernels:
126126
- arg_meta: null
127-
kernel_name: impl::HiFi::quantized_linear_out
127+
kernel_name: cadence::impl::HiFi::quantized_linear_out

backends/cadence/hifi/kernels/kernels.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <xa_nnlib_common.h>
1111
#include <xa_nnlib_common_macros.h>
1212

13+
namespace cadence {
1314
namespace impl {
1415
namespace HiFi {
1516
namespace kernels {
@@ -231,3 +232,4 @@ typed_requantize_vec(uint8_t, int8_t);
231232
}; // namespace kernels
232233
}; // namespace HiFi
233234
}; // namespace impl
235+
}; // namespace cadence

backends/cadence/hifi/kernels/kernels.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stddef.h>
1313
#include <xa_type_def.h>
1414

15+
namespace cadence {
1516
namespace impl {
1617
namespace HiFi {
1718
namespace kernels {
@@ -63,3 +64,4 @@ void dequantize(
6364
}; // namespace kernels
6465
}; // namespace HiFi
6566
}; // namespace impl
67+
}; // namespace cadence

backends/cadence/hifi/operators/dequantize_per_tensor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <executorch/runtime/kernel/kernel_includes.h>
1111
#include <xa_nnlib_kernels_api.h>
1212

13+
namespace cadence {
1314
namespace impl {
1415
namespace HiFi {
1516
namespace native {
@@ -50,3 +51,4 @@ void dequantize_per_tensor_out(
5051
}; // namespace native
5152
}; // namespace HiFi
5253
}; // namespace impl
54+
}; // namespace cadence

backends/cadence/hifi/operators/quantize_per_tensor.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <executorch/runtime/kernel/kernel_includes.h>
1111
#include <xa_nnlib_kernels_api.h>
1212

13+
namespace cadence {
1314
namespace impl {
1415
namespace HiFi {
1516
namespace native {
@@ -21,28 +22,32 @@ using executorch::runtime::KernelRuntimeContext;
2122
// Quantize the input tensor (PT2 version). Note that quant_<min,max> are not
2223
// used in any computation.
2324
void quantize_per_tensor_out(
24-
KernelRuntimeContext& context,
25+
KernelRuntimeContext& ctx,
2526
const Tensor& input,
2627
double scale,
2728
int64_t zero_point,
28-
int64_t quant_min,
29-
int64_t quant_max,
29+
__ET_UNUSED int64_t quant_min,
30+
__ET_UNUSED int64_t quant_max,
3031
ScalarType dtype,
3132
Tensor& out) {
3233
const float* input_data = input.const_data_ptr<float>();
33-
size_t numel = out.numel();
34+
const size_t numel = out.numel();
3435

3536
if (out.scalar_type() == ScalarType::Byte) {
3637
uint8_t* out_data = out.mutable_data_ptr<uint8_t>();
37-
impl::HiFi::kernels::quantize<uint8_t>(
38+
cadence::impl::HiFi::kernels::quantize<uint8_t>(
3839
out_data, input_data, 1. / scale, zero_point, numel);
3940
} else if (out.scalar_type() == ScalarType::Char) {
4041
int8_t* out_data = out.mutable_data_ptr<int8_t>();
4142
xa_nn_elm_quantize_f32_asym8s(
4243
out_data, input_data, scale, zero_point, numel);
44+
} else if (out.scalar_type() == ScalarType::Short) {
45+
int16_t* out_data = out.mutable_data_ptr<int16_t>();
46+
cadence::impl::HiFi::kernels::quantize<int16_t>(
47+
out_data, input_data, 1. / scale, zero_point, numel);
4348
} else if (out.scalar_type() == ScalarType::Int) {
4449
int32_t* out_data = out.mutable_data_ptr<int32_t>();
45-
impl::HiFi::kernels::quantize<int32_t>(
50+
cadence::impl::HiFi::kernels::quantize<int32_t>(
4651
out_data, input_data, 1. / scale, zero_point, numel);
4752
} else {
4853
ET_CHECK_MSG(false, "Unhandled input dtype %hhd", out.scalar_type());
@@ -52,3 +57,4 @@ void quantize_per_tensor_out(
5257
}; // namespace native
5358
}; // namespace HiFi
5459
}; // namespace impl
60+
}; // namespace cadence

backends/cadence/hifi/operators/quantized_layer_norm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using executorch::aten::Tensor;
1616
using executorch::runtime::getLeadingDims;
1717
using executorch::runtime::KernelRuntimeContext;
1818

19+
namespace cadence {
1920
namespace impl {
2021
namespace HiFi {
2122
namespace native {
@@ -76,10 +77,10 @@ void quantized_layer_norm_(
7677
for (size_t j = 0; j < last_dim; ++j) {
7778
// Since X is quantized, we dequantize it, compute fp32 result, and
7879
// quantize the result to an int8/uint8 value.
79-
float val = impl::HiFi::kernels::dequantize<T>(
80+
float val = cadence::impl::HiFi::kernels::dequantize<T>(
8081
x[j], input_scale, input_zero_point);
8182
val = (val - mean) * inv_std * weight_data[j] + bias_data[j];
82-
y[j] = impl::HiFi::kernels::quantize<T>(
83+
y[j] = cadence::impl::HiFi::kernels::quantize<T>(
8384
val, output_inv_scale, output_zero_point);
8485
}
8586
}
@@ -157,3 +158,4 @@ void quantized_layer_norm_out(
157158
}; // namespace native
158159
}; // namespace HiFi
159160
}; // namespace impl
161+
}; // namespace cadence

backends/cadence/hifi/operators/quantized_linear_out.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <algorithm>
1212
#include <cmath>
1313

14+
namespace cadence {
1415
namespace impl {
1516
namespace HiFi {
1617
namespace native {
@@ -45,7 +46,7 @@ void quantized_linear_out(
4546
uint8_t* __restrict__ out_data = out.mutable_data_ptr<uint8_t>();
4647

4748
// The nnlib kernel to compute quantized linear via matmul.
48-
int32_t ret = impl::HiFi::kernels::matmul_asym8uxasym8u_asym8u(
49+
int32_t ret = cadence::impl::HiFi::kernels::matmul_asym8uxasym8u_asym8u(
4950
out_data, // p_out
5051
weight_data, // p_mat1,
5152
in_data, // p_mat2,
@@ -69,3 +70,4 @@ void quantized_linear_out(
6970
}; // namespace native
7071
}; // namespace HiFi
7172
}; // namespace impl
73+
}; // namespace cadence

backends/cadence/hifi/operators/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ def define_common_targets():
2626
],
2727
visibility = [
2828
"//executorch/backends/cadence/...",
29+
"@EXECUTORCH_CLIENTS",
2930
],
3031
)

backends/cadence/hifi/third-party/nnlib/matmul_asym8uxasym8u_asym8u.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
/*----------------------------Main function---------------------------------*/
4545

46+
namespace cadence {
4647
namespace impl {
4748
namespace HiFi {
4849
namespace kernels {
@@ -436,3 +437,4 @@ WORD32 matmul_asym8uxasym8u_asym8u(
436437
}; // namespace kernels
437438
}; // namespace HiFi
438439
}; // namespace impl
440+
}; // namespace cadence

0 commit comments

Comments
 (0)