Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QualcommQnn] add ops #9505

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ namespace operation {
} \
/* Keep_dim */ \
auto keep_dim_operand = input_operands[2]; \
auto keep_dims = *reinterpret_cast<int8_t*>(keep_dim_operand->buffer); \
NNADAPTER_VLOG(5) << "keep_dims: " << keep_dims; \
bool keep_dim = keep_dims ? true : false; \
auto keep_dim = *reinterpret_cast<bool*>(keep_dim_operand->buffer); \
NNADAPTER_VLOG(5) << "keep_dim: " << keep_dim; \
/* Output */ \
auto output_operand = output_operands[0]; \
NNADAPTER_VLOG(5) << "output_operand: " << OperandToString(output_operand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class NCHW2NHWCDataLayoutConverter {
void ConvertShape(core::Operation* operation);
void ConvertSlice(core::Operation* operation);
void ConvertSoftmax(core::Operation* operation);
void ConvertSoftplus(core::Operation* operation);
void ConvertSplit(core::Operation* operation);
void ConvertSqueeze(core::Operation* operation);
void ConvertStack(core::Operation* operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,19 +728,36 @@ void NCHW2NHWCDataLayoutConverter::ConvertReduce(core::Operation* operation) {
NNADAPTER_CHECK_EQ(output_count, 1);
auto input_operand = input_operands[0];
int input_dimensions_count = input_operand->type.dimensions.count;
auto axis = reinterpret_cast<int32_t*>(input_operands[1]->buffer);
if (*axis < 0) {
*axis += input_dimensions_count;
size_t axes_size = input_operands[1]->length / sizeof(int32_t);
auto axes = reinterpret_cast<int32_t*>(input_operands[1]->buffer);
for (size_t i = 0; i < axes_size; i++) {
if (axes[i] < 0) axes[i] += input_dimensions_count;
}
bool keepdim = *reinterpret_cast<bool*>(input_operands[2]->buffer);
auto output_operand = output_operands[0];
// Recalculate the axis according to the dimorder vector of the input operand
auto input_permutation = GetPermutation(input_operand);
*axis = TransposeAxis(*axis, input_permutation);
auto output_dimensions_count = output_operand->type.dimensions.count;
// Skip NCHW2NHWC conversion
// TODO(hong19860320) Transpose output operand according to the dimorder
// vector of the input operand
SetPermutation(output_operand, IdentityPermutation(output_dimensions_count));
if (keepdim) {
for (size_t i = 0; i < axes_size; i++) {
axes[i] = TransposeAxis(axes[i], input_permutation);
}
TransposeOperand(output_operand, input_permutation);
SetPermutation(output_operand, input_permutation);
} else {
// Force to restore the dimorder vector of the input operand
auto transpose_input_permutation = InversePermutation(input_permutation);
if (!IsIdentityPermutation(transpose_input_permutation)) {
auto transpose_input_operand = AppendTransposeOperation(
model_, input_operand, transpose_input_permutation);
UpdateOperationInputOperands(
{operation}, input_operand, transpose_input_operand);
SetPermutation(transpose_input_operand,
IdentityPermutation(input_dimensions_count));
}
int output_dimensions_count = output_operand->type.dimensions.count;
SetPermutation(output_operand,
IdentityPermutation(output_dimensions_count));
}
}

void NCHW2NHWCDataLayoutConverter::ConvertReshape(core::Operation* operation) {
Expand Down Expand Up @@ -951,6 +968,21 @@ void NCHW2NHWCDataLayoutConverter::ConvertSoftmax(core::Operation* operation) {
SetPermutation(output_operand, input_permutation);
}

void NCHW2NHWCDataLayoutConverter::ConvertSoftplus(core::Operation* operation) {
auto& input_operands = operation->input_operands;
auto& output_operands = operation->output_operands;
auto input_count = input_operands.size();
auto output_count = output_operands.size();
NNADAPTER_CHECK_GE(input_count, 3);
NNADAPTER_CHECK_EQ(output_count, 1);
auto input_operand = input_operands[0];
auto output_operand = output_operands[0];
// The input and output operands share the same dimorder vector
auto input_permutation = GetPermutation(input_operand);
TransposeOperand(output_operand, input_permutation);
SetPermutation(output_operand, input_permutation);
}

void NCHW2NHWCDataLayoutConverter::ConvertSqueeze(core::Operation* operation) {
auto& input_operands = operation->input_operands;
auto& output_operands = operation->output_operands;
Expand Down Expand Up @@ -1343,6 +1375,7 @@ void NCHW2NHWCDataLayoutConverter::Apply(core::Model* model) {
case NNADAPTER_QUANTIZE:
ConvertQuantize(operation);
break;
case NNADAPTER_REDUCE_MAX:
case NNADAPTER_REDUCE_MEAN:
case NNADAPTER_REDUCE_SUM:
ConvertReduce(operation);
Expand All @@ -1353,6 +1386,7 @@ void NCHW2NHWCDataLayoutConverter::Apply(core::Model* model) {
case NNADAPTER_RESIZE_LINEAR:
ConvertResizeLinear(operation);
break;
case NNADAPTER_FLOOR:
case NNADAPTER_RELU:
case NNADAPTER_RELU6:
case NNADAPTER_TANH:
Expand All @@ -1377,6 +1411,9 @@ void NCHW2NHWCDataLayoutConverter::Apply(core::Model* model) {
case NNADAPTER_SOFTMAX:
ConvertSoftmax(operation);
break;
case NNADAPTER_SOFTPLUS:
ConvertSoftplus(operation);
break;
case NNADAPTER_SQUEEZE:
ConvertSqueeze(operation);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,28 @@ NNADAPTER_EXPORT void ConvertQuantizationSymmToAsymm(core::Model* model) {
case NNADAPTER_CHANNEL_SHUFFLE:
case NNADAPTER_CLIP:
case NNADAPTER_CUM_SUM:
case NNADAPTER_EXP:
case NNADAPTER_FILL_LIKE:
case NNADAPTER_FLATTEN:
case NNADAPTER_FLOOR:
case NNADAPTER_GELU:
case NNADAPTER_HARD_SIGMOID:
case NNADAPTER_HARD_SWISH:
case NNADAPTER_LAYER_NORMALIZATION:
case NNADAPTER_LEAKY_RELU:
case NNADAPTER_LOG:
case NNADAPTER_MAX_POOL_2D:
case NNADAPTER_PAD:
case NNADAPTER_REDUCE_MAX:
case NNADAPTER_REDUCE_MEAN:
case NNADAPTER_REDUCE_SUM:
case NNADAPTER_RELU:
case NNADAPTER_RELU6:
case NNADAPTER_RESHAPE:
case NNADAPTER_RESIZE_NEAREST:
case NNADAPTER_RESIZE_LINEAR:
case NNADAPTER_SLICE:
case NNADAPTER_SOFTPLUS:
case NNADAPTER_SQUEEZE:
case NNADAPTER_SWISH:
case NNADAPTER_TANH:
Expand Down
1 change: 1 addition & 0 deletions lite/backends/nnadapter/nnadapter/src/utility/debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ NNADAPTER_EXPORT std::string OperationTypeToString(
NNADAPTER_TYPE_TO_STRING(RELU);
NNADAPTER_TYPE_TO_STRING(RELU6);
NNADAPTER_TYPE_TO_STRING(RANGE);
NNADAPTER_TYPE_TO_STRING(REDUCE_MAX);
NNADAPTER_TYPE_TO_STRING(REDUCE_MEAN);
NNADAPTER_TYPE_TO_STRING(REDUCE_SUM);
NNADAPTER_TYPE_TO_STRING(RESHAPE);
Expand Down
17 changes: 9 additions & 8 deletions lite/kernels/nnadapter/converter/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ REGISTER_CONVERTER(abs,
REGISTER_CONVERTER(exp,
ConvertUnaryActivations,
"huawei_ascend_npu,huawei_kirin_npu,intel_openvino,nvidia_"
"tensorrt,cambricon_mlu,kunlunxin_xtcl");
"tensorrt,cambricon_mlu,kunlunxin_xtcl,qualcomm_qnn");
REGISTER_CONVERTER(instance_norm,
ConvertInstanceNorm,
"huawei_ascend_npu,kunlunxin_xtcl");
Expand All @@ -239,7 +239,7 @@ REGISTER_CONVERTER(group_norm,
REGISTER_CONVERTER(log,
ConvertUnaryActivations,
"huawei_ascend_npu,huawei_kirin_npu,cambricon_mlu,nvidia_"
"tensorrt,intel_openvino,kunlunxin_xtcl");
"tensorrt,intel_openvino,kunlunxin_xtcl,qualcomm_qnn");
REGISTER_CONVERTER(swish,
ConvertUnaryActivations,
"huawei_ascend_npu,huawei_kirin_npu,nvidia_tensorrt,intel_"
Expand Down Expand Up @@ -307,13 +307,13 @@ REGISTER_CONVERTER(
reduce_mean,
ConvertReduce,
"huawei_ascend_npu,cambricon_mlu,huawei_kirin_npu,intel_openvino,"
"kunlunxin_xtcl");
REGISTER_CONVERTER(reduce_max, ConvertReduce, "intel_openvino");
"kunlunxin_xtcl,qualcomm_qnn");
REGISTER_CONVERTER(reduce_max, ConvertReduce, "intel_openvino,qualcomm_qnn");
REGISTER_CONVERTER(
reduce_sum,
ConvertReduce,
"huawei_ascend_npu,cambricon_mlu,huawei_kirin_npu,nvidia_tensorrt,"
"kunlunxin_xtcl");
"kunlunxin_xtcl,qualcomm_qnn");
REGISTER_CONVERTER(top_k,
ConvertTopK,
"huawei_ascend_npu,cambricon_mlu,kunlunxin_xtcl");
Expand Down Expand Up @@ -459,9 +459,10 @@ REGISTER_CONVERTER(
logical_and,
ConvertBinaryLogicalOp,
"huawei_ascend_npu,huawei_kirin_npu,cambricon_mlu,kunlunxin_xtcl");
REGISTER_CONVERTER(floor,
ConvertUnaryActivations,
"huawei_ascend_npu,huawei_kirin_npu,kunlunxin_xtcl");
REGISTER_CONVERTER(
floor,
ConvertUnaryActivations,
"huawei_ascend_npu,huawei_kirin_npu,kunlunxin_xtcl,qualcomm_qnn");
REGISTER_CONVERTER(meshgrid,
ConvertMeshgrid,
"huawei_ascend_npu,kunlunxin_xtcl");
Expand Down
10 changes: 10 additions & 0 deletions lite/tests/kernels/activation_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,8 @@ TEST(Activation_log, precision) {
dims.push_back(1);
}
}
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-2;
#else
return;
#endif
Expand Down Expand Up @@ -929,6 +931,8 @@ TEST(Activation_exp, precision) {
}
#elif defined(NNADAPTER_WITH_CAMBRICON_MLU)
abs_error = 1e-3;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-3;
#else
return;
#endif
Expand Down Expand Up @@ -961,6 +965,8 @@ TEST(Activation_floor, precision) {
abs_error = 1e-2;
#elif defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
abs_error = 5e-2;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-2;
#else
return;
#endif
Expand Down Expand Up @@ -1179,6 +1185,10 @@ TEST(Activation_softplus, precision) {
abs_error = 5e-2;
#elif defined(NNADAPTER_WITH_INTEL_OPENVINO)
abs_error = 1e-5;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-2;
// Not support htp
return;
#else
return;
#endif
Expand Down
17 changes: 8 additions & 9 deletions lite/tests/kernels/cast_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,16 @@ TEST(Cast, precision) {
place = TARGET(kNNAdapter);
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
abs_error = 1e-2;
TestCast(place, abs_error, 2, 5);
TestCast(place, abs_error, 3, 5);
TestCast(place, abs_error, 5, 3);
return;
#elif defined(NNADAPTER_WITH_CAMBRICON_MLU)
abs_error = 1e-5;
#elif defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
abs_error = 1e-5;
TestCast(place, abs_error, 2, 5);
return;
#elif defined(NNADAPTER_WITH_NVIDIA_TENSORRT)
abs_error = 1e-5;
#elif defined(NNADAPTER_WITH_INTEL_OPENVINO)
Expand All @@ -181,17 +187,10 @@ TEST(Cast, precision) {
return;
#endif

// BOOL = 0;INT16 = 1;INT32 = 2;INT64 = 3;FP16 = 4;FP32 = 5;FP64 = 6;
// SIZE_T = 19;UINT8 = 20;INT8 = 21;
#if !defined(LITE_WITH_XPU) && !defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU) && \
!defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
// BOOL = 0;INT16 = 1;INT32 = 2;INT64 = 3;FP16 = 4;FP32 = 5;FP64 = 6;
// SIZE_T = 19;UINT8 = 20;INT8 = 21;
TestCast(place, abs_error, 20, 5);
#endif
TestCast(place, abs_error, 2, 5);
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU)
TestCast(place, abs_error, 3, 5);
TestCast(place, abs_error, 5, 3);
#endif
}

} // namespace lite
Expand Down
6 changes: 6 additions & 0 deletions lite/tests/kernels/reduce_max_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ void test_reduce_max_4d(Place place) {
for (auto w : {1, 3}) {
for (bool keep_dim : {false, true}) {
for (auto dim : reduce_dim) {
#ifdef NNADAPTER_WITH_QUALCOMM_QNN
if (std::find(dim.begin(), dim.end(), 0) == dim.end() &&
!keep_dim)
continue;
#endif
auto x_dims = DDim(std::vector<int64_t>({n, c, h, w}));
std::unique_ptr<arena::TestCase> tester(
new ReduceMaxComputeTester(
Expand Down Expand Up @@ -449,6 +454,7 @@ TEST(ReduceMax, precision) {
#if defined(LITE_WITH_NNADAPTER)
place = TARGET(kNNAdapter);
#if defined(NNADAPTER_WITH_INTEL_OPENVINO)
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
#else
return;
#endif
Expand Down
7 changes: 7 additions & 0 deletions lite/tests/kernels/reduce_mean_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ void test_reduce_mean(Place place,
if (last_dim < 1) continue;
}
if (last_dim > x_dims.size() - 1) continue;
#ifdef NNADAPTER_WITH_QUALCOMM_QNN
if (std::find(dim.begin(), dim.end(), 0) == dim.end() &&
!keep_dim && x_dims.size() == 4)
continue;
#endif
std::unique_ptr<arena::TestCase> tester(
new ReduceMeanComputeTester(
place, "def", dim, keep_dim, x_dims));
Expand Down Expand Up @@ -396,6 +401,8 @@ TEST(ReduceMean, precision) {
keep_dim_vec = std::vector<bool>{false};
#elif defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
abs_err = 1e-3;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_err = 1e-3;
#else
return;
#endif
Expand Down
16 changes: 12 additions & 4 deletions lite/tests/kernels/reduce_sum_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,17 @@ void test_reduce_sum(Place place,
for (auto w : {1, 3}) {
for (bool keep_dim : keep_dim_vec) {
for (bool reduce_all : {false, true}) {
#if defined(LITE_WITH_NNADAPTER)
if (reduce_all == true) continue;
if (n == 3 && c == 2 && h == 3 && w == 3) continue;
#endif
for (auto dim : reduce_dim) {
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU) || \
defined(NNADAPTER_WITH_CAMBRICON_MLU) || \
defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
if (reduce_all) continue;
if (n == 3 && c == 2 && h == 3 && w == 3) continue;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
if (std::find(dim.begin(), dim.end(), 0) == dim.end() &&
!keep_dim)
continue;
#endif
auto x_dims = DDim(std::vector<int64_t>({n, c, h, w}));
std::unique_ptr<arena::TestCase> tester(
new ReduceSumComputeTester(
Expand Down Expand Up @@ -354,6 +360,8 @@ TEST(ReduceSum, precision) {
keep_dim_vec = std::vector<bool>{false};
#elif defined(NNADAPTER_WITH_HUAWEI_KIRIN_NPU)
abs_error = 1e-2;
#elif defined(NNADAPTER_WITH_QUALCOMM_QNN)
abs_error = 1e-3;
#else
return;
#endif
Expand Down