From 2b895074ce462d54441ccd5820d0da00f765511c Mon Sep 17 00:00:00 2001 From: zhaozhenlong Date: Thu, 13 Aug 2020 19:23:58 +0800 Subject: [PATCH] pad int8 add multi thread --- .../src/runtime/kernel/arm/int8/pad_int8.cc | 27 +++++++++++++++---- .../src/runtime/kernel/arm/int8/pad_int8.h | 3 +++ .../src/runtime/kernel/arm/nnacl/int8/pad.c | 9 ++++--- .../src/runtime/kernel/arm/nnacl/int8/pad.h | 4 +-- .../runtime/kernel/arm/int8/pad_int8_tests.cc | 1 + 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc index 825f1da9ff..5c1294d390 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.cc @@ -111,18 +111,35 @@ int PadInt8CPUKernel::Init() { return RET_OK; } +int PadInt8CPUKernel::RunImpl(int task_id) { + return PadConstant4D(in_data_, out_data_, in_dims_, out_dims_, pad_param_->paddings_, task_id, context_->thread_num_); +} + +int PadInt8Impl(int task_id, LiteParallelGroupEnv *penv, void *cdata) { + auto resize = reinterpret_cast(cdata); + auto error_code = resize->RunImpl(task_id); + if (error_code != RET_OK) { + MS_LOG(ERROR) << "Resize Run error task_id[" << task_id << "] error_code[" << error_code << "]"; + return RET_ERROR; + } + return RET_OK; +} + int PadInt8CPUKernel::Run() { auto ret = Prepare(); if (ret != RET_OK) { MS_LOG(ERROR) << "Prepare failed."; return RET_ERROR; } - int8_t *in_data = reinterpret_cast(in_tensors_[0]->Data()); - int8_t *out_data = reinterpret_cast(out_tensors_[0]->Data()); + in_data_ = reinterpret_cast(in_tensors_[0]->Data()); + out_data_ = reinterpret_cast(out_tensors_[0]->Data()); - memset(out_data, pad_param_->pad_quant_arg_.constant_value_[0], out_tensors_[0]->ElementsNum() * sizeof(int8_t)); - PadConstant4D(in_data, out_data, in_dims_, out_dims_, pad_param_->paddings_); + memset(out_data_, pad_param_->pad_quant_arg_.constant_value_[0], out_tensors_[0]->ElementsNum() * sizeof(int8_t)); + int error_code = LiteBackendParallelLaunch(PadInt8Impl, this, context_->thread_num_); + if (error_code != RET_OK) { + MS_LOG(ERROR) << "Resize run error, error_code[" << error_code << "]"; + return RET_ERROR; + } return RET_OK; } - } // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.h b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.h index bda548944c..76bff714bc 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.h +++ b/mindspore/lite/src/runtime/kernel/arm/int8/pad_int8.h @@ -38,6 +38,7 @@ class PadInt8CPUKernel : public LiteKernel { int Init() override; int ReSize() override; int Run() override; + int RunImpl(int task_id); private: int SetQuantParam(); @@ -46,6 +47,8 @@ class PadInt8CPUKernel : public LiteKernel { private: PadParameter *pad_param_; + int8_t *in_data_; + int8_t *out_data_; int in_dims_[DEFAULT_PAD_NDIMS]; int out_dims_[DEFAULT_PAD_NDIMS]; }; diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.c b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.c index 8a9b2858e6..90d7bf5624 100644 --- a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.c +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.c @@ -16,12 +16,13 @@ #include "nnacl/int8/pad.h" #include "nnacl/common_func.h" +#include "nnacl/errorcode.h" -void PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims, - const int32_t *paddings) { +int PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims, + const int32_t *paddings, const int tid, const int thread_num) { int32_t copy_size = in_dims[3]; for (int n = 0; n < in_dims[0]; n++) { - for (int h = 0; h < in_dims[1]; h++) { + for (int h = tid; h < in_dims[1]; h += thread_num) { for (int w = 0; w < in_dims[2]; w++) { const int8_t *in = in_data + offset(in_dims, n, h, w, 0); int8_t *out = out_data + offset(out_dims, n + paddings[0], h + paddings[2], w + paddings[4], paddings[6]); @@ -29,5 +30,5 @@ void PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_di } } } - return; + return NNACL_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.h b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.h index e670082f23..5f8a422d87 100644 --- a/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.h +++ b/mindspore/lite/src/runtime/kernel/arm/nnacl/int8/pad.h @@ -24,8 +24,8 @@ #ifdef __cplusplus extern "C" { #endif -void PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims, - const int32_t *paddings); +int PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dims, const int32_t *out_dims, + const int32_t *paddings, const int tid, const int thread_num); #ifdef __cplusplus } #endif diff --git a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/pad_int8_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/pad_int8_tests.cc index f3b8c27ae8..4676f4cb98 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/pad_int8_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/arm/int8/pad_int8_tests.cc @@ -183,6 +183,7 @@ TEST_F(TestPadInt8, PadInt8TestInit4) { std::vector outputs_; auto pad_param = new PadParameter(); lite::Context *ctx = new lite::Context; + ctx->thread_num_ = 2; int8_t *correct; int total_size = PadInt8TestInit2(&inputs_, &outputs_, pad_param, &correct); kernel::PadInt8CPUKernel *pad =