From aa704f51e781103a7f1075b0861e4dde44be8072 Mon Sep 17 00:00:00 2001 From: hangangqiang Date: Fri, 20 Nov 2020 12:09:45 +0800 Subject: [PATCH] fix large functions & fix large Cyclomatic Complexity & remove redundant --- mindspore/lite/src/CMakeLists.txt | 1 - mindspore/lite/src/common/file_utils.cc | 2 - mindspore/lite/src/common/utils.cc | 139 +--------------- mindspore/lite/src/common/utils.h | 10 +- mindspore/lite/src/executor.cc | 53 ------ mindspore/lite/src/executor.h | 6 - mindspore/lite/src/kernel_registry.h | 2 +- mindspore/lite/src/lite_session.cc | 135 ++++++++------- mindspore/lite/src/lite_session.h | 5 + mindspore/lite/src/ops/primitive_c.cc | 115 +++++++------ mindspore/lite/src/ops/primitive_c.h | 4 + mindspore/lite/src/runtime/runtime_api.cc | 21 --- mindspore/lite/src/runtime/runtime_api.h | 3 - mindspore/lite/src/runtime/thread_pool.c | 153 +++++++---------- mindspore/lite/src/runtime/thread_pool.h | 15 -- mindspore/lite/src/runtime/workspace_pool.cc | 154 ------------------ mindspore/lite/src/runtime/workspace_pool.h | 44 ----- mindspore/lite/src/scheduler.cc | 47 +++--- mindspore/lite/src/scheduler.h | 4 + mindspore/lite/src/tensor.cc | 45 +---- mindspore/lite/src/tensor.h | 14 ++ mindspore/lite/test/CMakeLists.txt | 1 - mindspore/lite/tools/converter/CMakeLists.txt | 1 - 23 files changed, 260 insertions(+), 714 deletions(-) delete mode 100644 mindspore/lite/src/runtime/workspace_pool.cc delete mode 100644 mindspore/lite/src/runtime/workspace_pool.h diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 4ebcf18055..20c71df732 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -24,7 +24,6 @@ set(LITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/allocator.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.c - ${CMAKE_CURRENT_SOURCE_DIR}/runtime/workspace_pool.cc ${CMAKE_CURRENT_SOURCE_DIR}/tensor.cc ${CMAKE_CURRENT_SOURCE_DIR}/executor.cc ${CMAKE_CURRENT_SOURCE_DIR}/inner_context.cc diff --git a/mindspore/lite/src/common/file_utils.cc b/mindspore/lite/src/common/file_utils.cc index 6683e2a3fd..8b25f0dbd6 100644 --- a/mindspore/lite/src/common/file_utils.cc +++ b/mindspore/lite/src/common/file_utils.cc @@ -18,12 +18,10 @@ #include #include #include -#include #include "securec/include/securec.h" namespace mindspore { namespace lite { -#define MAX_FILENAME_LEN 1024 char *ReadFile(const char *file, size_t *size) { if (file == nullptr) { MS_LOG(ERROR) << "file is nullptr"; diff --git a/mindspore/lite/src/common/utils.cc b/mindspore/lite/src/common/utils.cc index ed54fe2c67..cef0f0cf36 100644 --- a/mindspore/lite/src/common/utils.cc +++ b/mindspore/lite/src/common/utils.cc @@ -48,131 +48,10 @@ uint64_t GetTimeUs() { return 0; } // USECS_IN_SEC *NSECS_IN_USEC; - uint64_t retval = static_cast((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC)); + auto retval = static_cast((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC)); return retval; } -static const unsigned int FP32_BIT_SIZE = 32; -static const unsigned int FP32_EXPONENT_BIAS = 127; -static const unsigned int FP32_SIGNIFICAND = 23; - -static const unsigned int FP32_EXPONENT_MAX = 255; - -static const unsigned int FP16_BIT_SIZE = 16; -static const unsigned int FP16_EXPONENT_BIAS = 15; -static const unsigned int FP16_SIGNIFICAND = 10; - -static const int FP16_EXPONENT_MAX = 30; -static const int FP16_EXPONENT_MIN = -10; - -// fp16.c -float ShortToFloat32(int16_t srcValue) { - uint16_t expHalf16 = srcValue & 0x7C00; - int exp1 = static_cast(expHalf16); - uint16_t mantissa16 = srcValue & 0x03FF; - int mantissa1 = static_cast(mantissa16); - int sign = static_cast(srcValue & 0x8000); - sign = sign << FP16_BIT_SIZE; - - // nan or inf - if (expHalf16 == 0x7C00) { - // nan - if (mantissa16 > 0) { - int res = (0x7FC00000 | sign); - int *iRes = &res; - auto fres = static_cast(*iRes); - return fres; - } - // inf - int res = (0x7F800000 | sign); - int *iRes = &res; - auto fres = static_cast(*iRes); - return fres; - } - if (expHalf16 != 0) { - exp1 += ((FP32_EXPONENT_BIAS - FP16_EXPONENT_BIAS) << FP16_SIGNIFICAND); // exponents converted to float32 bias - int res = (exp1 | mantissa1); - res = res << (FP32_SIGNIFICAND - FP16_SIGNIFICAND); - res = (res | sign); - int *iRes = &res; - auto fres = static_cast(*iRes); - return fres; - } - - int xmm1 = exp1 > (1 << FP16_SIGNIFICAND) ? exp1 : (1 << FP16_SIGNIFICAND); - xmm1 = (xmm1 << (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - xmm1 += ((FP32_EXPONENT_BIAS - FP16_EXPONENT_BIAS - FP16_SIGNIFICAND) - << FP32_SIGNIFICAND); // add the bias difference to xmm1 - xmm1 = xmm1 | sign; // Combine with the sign mask - - auto res = static_cast(mantissa1); // Convert mantissa to float - int *ixmm1 = nullptr; - ixmm1 = &xmm1; - res *= static_cast(*ixmm1); - - return res; -} - -// __gnu_f2h_ieee -int16_t Float32ToShort(float srcValue) { - float *psrcValue = nullptr; - psrcValue = &srcValue; - auto srcValueBit = static_cast(*psrcValue); - int sign = srcValueBit >> (FP32_BIT_SIZE - 1); - int mantissa = srcValueBit & 0x007FFFFF; - // exponent - int exp = ((srcValueBit & 0x7F800000) >> FP32_SIGNIFICAND) + FP16_EXPONENT_BIAS - FP32_EXPONENT_BIAS; - int16_t res; - if (exp > 0 && exp < FP16_EXPONENT_MAX) { - // use rte rounding mode, round the significand, combine sign, exponent and significand into a short. - res = (sign << (FP16_BIT_SIZE - 1)) | (exp << FP16_SIGNIFICAND) | - ((mantissa + 0x00001000) >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } else if (srcValueBit == 0) { - res = 0; - } else { - if (exp <= 0) { - if (exp < FP16_EXPONENT_MIN) { - // value is less than min half float point - res = 0; - } else { - // normalized single, magnitude is less than min normal half float point. - mantissa = (mantissa | 0x00800000) >> (1 - exp); - // round to nearest - if ((mantissa & 0x00001000) > 0) { - mantissa = mantissa + 0x00002000; - } - // combine sign & mantissa (exp is zero to get denormalized number) - res = (sign << FP16_EXPONENT_BIAS) | (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } else if (exp == (FP32_EXPONENT_MAX - FP32_EXPONENT_BIAS + FP16_EXPONENT_BIAS)) { - if (mantissa == 0) { - // input float is infinity, return infinity half - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00; - } else { - // input float is NaN, return half NaN - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00 | (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } else { - // exp > 0, normalized single, round to nearest - if ((mantissa & 0x00001000) > 0) { - mantissa = mantissa + 0x00002000; - if ((mantissa & 0x00800000) > 0) { - mantissa = 0; - exp = exp + 1; - } - } - if (exp > FP16_EXPONENT_MAX) { - // exponent overflow - return infinity half - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00; - } else { - // combine sign, exp and mantissa into normalized half - res = (sign << FP16_EXPONENT_BIAS) | (exp << FP16_SIGNIFICAND) | - (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } - } - return res; -} std::string Remove(const std::string &from, const std::string &subStr, Mode mode) { std::string result = from; if (mode == PREFIX) { @@ -236,22 +115,6 @@ std::vector Tokenize(const std::string &src, const std::string &del return tokens; } -void ShortToFloat32(const int16_t *srcdata, float *dstdata, size_t elementSize) { - MS_ASSERT(srcdata != nullptr); - MS_ASSERT(dstdata != nullptr); - for (size_t i = 0; i < elementSize; i++) { - dstdata[i] = ShortToFloat32(srcdata[i]); - } -} - -void Float32ToShort(const float *srcdata, int16_t *dstdata, size_t elementSize) { - MS_ASSERT(srcdata != nullptr); - MS_ASSERT(dstdata != nullptr); - for (size_t i = 0; i < elementSize; i++) { - dstdata[i] = Float32ToShort(srcdata[i]); - } -} - #if defined(__ANDROID__) uint32_t getHwCap(int hwcap_type) { uint32_t ret = getauxval(hwcap_type); diff --git a/mindspore/lite/src/common/utils.h b/mindspore/lite/src/common/utils.h index dd4b12bdf4..47dad28dc3 100644 --- a/mindspore/lite/src/common/utils.h +++ b/mindspore/lite/src/common/utils.h @@ -34,15 +34,7 @@ const int USEC = 1000000; const int MSEC = 1000; std::vector StringSplit(std::string str, const std::string &pattern); -uint64_t GetTimeUs(void); - -int16_t Float32ToShort(float srcValue); - -float ShortToFloat32(int16_t srcValue); - -void ShortToFloat32(const int16_t *srcdata, float *dstdata, size_t elementSize); - -void Float32ToShort(const float *srcdata, int16_t *dstdata, size_t elementSize); +uint64_t GetTimeUs(); bool IsSupportSDot(); diff --git a/mindspore/lite/src/executor.cc b/mindspore/lite/src/executor.cc index 96d5d4b383..92b5d0c049 100644 --- a/mindspore/lite/src/executor.cc +++ b/mindspore/lite/src/executor.cc @@ -68,57 +68,4 @@ int Executor::Run(std::vector &in_tensors, std::vector &out_ } return RET_OK; } - -int Executor::TransformTensorLayout(Tensor *tensor, schema::Format dst_format, Allocator *allocator) { - MS_ASSERT(nullptr != tensor); - MS_ASSERT(nullptr != allocator); - MS_ASSERT(4 == tensor->shape().size()); - auto data_type = tensor->data_type(); - switch (data_type) { - case kNumberTypeInt8: - return TransformTensorLayoutUint8(tensor, dst_format, allocator); - case kNumberTypeFloat32: - return TransformTensorLayoutFp32(tensor, dst_format, allocator); - default: - return RET_ERROR; - } - return RET_OK; -} - -int Executor::TransformTensorLayoutFp32(Tensor *tensor, schema::Format dst_format, Allocator *allocator) { - MS_ASSERT(nullptr != tensor); - MS_ASSERT(nullptr != allocator); - MS_ASSERT(4 == tensor->shape().size()); - auto src_format = tensor->GetFormat(); - if (src_format == schema::Format::Format_NC4HW4 && dst_format == schema::Format::Format_NHWC) { - auto *src_data = tensor->data_c(); - if (src_data == nullptr) { - MS_LOG(ERROR) << "data of tensor is nullptr"; - return RET_ERROR; - } - auto *dst_data = allocator->Malloc(tensor->Size()); - if (dst_data == nullptr) { - MS_LOG(ERROR) << "Malloc data failed"; - return RET_ERROR; - } - PackNC4HW4ToNHWCFp32(src_data, dst_data, tensor->Batch(), tensor->Height() * tensor->Width(), tensor->Channel()); - tensor->set_data(dst_data); - tensor->SetFormat(dst_format); - allocator->Free(src_data); - return RET_OK; - } else { - MS_LOG(ERROR) << "Unsupported layout transform: " << EnumNameFormat(tensor->GetFormat()) << " to " - << EnumNameFormat(dst_format) << " in float32"; - return RET_ERROR; - } -} - -int Executor::TransformTensorLayoutUint8(Tensor *tensor, schema::Format dst_format, Allocator *allocator) { - MS_ASSERT(nullptr != tensor); - MS_ASSERT(nullptr != allocator); - MS_ASSERT(4 == tensor->shape().size()); - MS_LOG(ERROR) << "Unsupported layout transform: " << EnumNameFormat(tensor->GetFormat()) << " to " - << EnumNameFormat(dst_format) << " in uint8"; - return RET_ERROR; -} } // namespace mindspore::lite diff --git a/mindspore/lite/src/executor.h b/mindspore/lite/src/executor.h index d951ffb6e0..199f92f7db 100644 --- a/mindspore/lite/src/executor.h +++ b/mindspore/lite/src/executor.h @@ -36,12 +36,6 @@ class Executor { protected: int CheckInputs(std::vector &in_tensors); - - int TransformTensorLayoutFp32(Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); - - int TransformTensorLayoutUint8(Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); - - int TransformTensorLayout(Tensor *tensor, schema::Format dst_format, Allocator *allocator = nullptr); }; } // namespace mindspore::lite diff --git a/mindspore/lite/src/kernel_registry.h b/mindspore/lite/src/kernel_registry.h index d0fc1911f5..e9840afb08 100644 --- a/mindspore/lite/src/kernel_registry.h +++ b/mindspore/lite/src/kernel_registry.h @@ -50,7 +50,7 @@ class KernelRegistry { static const int data_type_length_{kNumberTypeEnd - kNumberTypeBegin + 1}; static const int op_type_length_{PrimitiveType_MAX - PrimitiveType_MIN + 1}; static const int array_size_{device_type_length_ * data_type_length_ * op_type_length_}; - kernel::KernelCreator creator_arrays_[array_size_] = {0}; + kernel::KernelCreator creator_arrays_[array_size_] = {nullptr}; std::vector op_parameters_; }; diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index 9105fd9906..e3f098329a 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -52,6 +52,67 @@ static bool WeightTensorNeedCopy(const lite::Model *model, const uint32_t tensor LiteSession::LiteSession() { this->is_running_.store(false); } +void LiteSession::ConvertTensorsQuantParam(const schema::Tensor *src_tensor, lite::Tensor *dst_tensor) { + MS_ASSERT(src_tensor != nullptr); + MS_ASSERT(dst_tensor != nullptr); + auto quant_params = src_tensor->quantParams(); + if (quant_params != nullptr) { + for (size_t j = 0; j < quant_params->size(); j++) { + QuantArg quant_arg{}; + quant_arg.bitNum = quant_params->Get(j)->numBits(); + quant_arg.scale = quant_params->Get(j)->scale(); + quant_arg.zeroPoint = quant_params->Get(j)->zeroPoint(); + quant_arg.var_corr = quant_params->Get(j)->varCorr(); + quant_arg.mean_corr = quant_params->Get(j)->meanCorr(); + quant_arg.inited = quant_params->Get(j)->inited(); + dst_tensor->AddQuantParam(quant_arg); + } + } + auto quant_clusters = src_tensor->quantClusters(); + if (quant_clusters != nullptr) { + std::vector clusters; + for (size_t j = 0; j < quant_clusters->size(); j++) { + clusters.push_back(quant_clusters->Get(j)); + } + dst_tensor->SetQuantClusters(clusters); + } +} + +int LiteSession::ConvertTensorsData(const lite::Model *model, size_t tensor_index, const schema::Tensor *src_tensor, + lite::Tensor *dst_tensor) { + MS_ASSERT(src_tensor != nullptr); + MS_ASSERT(dst_tensor != nullptr); + auto src_category = TensorCategory(src_tensor); + auto data_type = src_tensor->dataType(); + if ((src_category == Tensor::Category::CONST_TENSOR || src_category == Tensor::Category::CONST_SCALAR) && + src_tensor->data() != nullptr && src_tensor->data()->size() > 0) { + MS_ASSERT(dst_tensor->Size() == src_tensor->data()->size()); + if (WeightTensorNeedCopy(model, tensor_index)) { + auto dst_data = dst_tensor->MutableData(); + if (dst_data == nullptr) { + MS_LOG(ERROR) << "Data from tensor is nullptr"; + return RET_NULL_PTR; + } + memcpy(dst_data, src_tensor->data()->data(), dst_tensor->Size()); + copyed_tensor_idxes_.emplace_back(tensor_index); + } else { + int pack_size = src_tensor->data()->size(); + int org_size = dst_tensor->Size(); + if (pack_size != org_size && (data_type == kNumberTypeInt8 || data_type == kNumberTypeInt16)) { + auto ret = dst_tensor->MallocData(); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Malloc data for tensor failed "; + return RET_ERROR; + } + kernel::DequantUtil::UnPackToInt(src_tensor, dst_tensor->MutableData()); + } else { + dst_tensor->set_data(const_cast(src_tensor->data()->data())); + } + } + } + return RET_OK; +} + int LiteSession::ConvertTensors(const lite::Model *model) { MS_ASSERT(model != nullptr); copyed_tensor_idxes_.clear(); @@ -66,75 +127,31 @@ int LiteSession::ConvertTensors(const lite::Model *model) { std::vector shape; if (srcTensor->dims() == nullptr) { MS_LOG(DEBUG) << "Dims of " << i << "th tensor is nullptr"; - } else { - if (src_category == Tensor::Category::CONST_TENSOR) { - if (srcTensor->dataType() == kObjectTypeString && srcTensor->data() != nullptr) { - shape.push_back(srcTensor->data()->size()); - } else { - for (size_t j = 0; j < srcTensor->dims()->size(); j++) { - shape.push_back(srcTensor->dims()->data()[j]); - } + } + if (srcTensor->dims() != nullptr && src_category == Tensor::Category::CONST_TENSOR) { + if (srcTensor->dataType() == kObjectTypeString && srcTensor->data() != nullptr) { + shape.push_back(srcTensor->data()->size()); + } else { + for (size_t j = 0; j < srcTensor->dims()->size(); j++) { + shape.push_back(srcTensor->dims()->data()[j]); } } } - int dataType = srcTensor->dataType(); - auto *dstTensor = new (std::nothrow) Tensor(TypeId(dataType), shape, srcTensor->format(), src_category); + auto *dstTensor = + new (std::nothrow) Tensor(TypeId(srcTensor->dataType()), shape, srcTensor->format(), src_category); if (dstTensor == nullptr) { MS_LOG(ERROR) << "new " << i << "th tensor failed"; return RET_NULL_PTR; } - if ((src_category == Tensor::Category::CONST_TENSOR || src_category == Tensor::Category::CONST_SCALAR) && - srcTensor->data() != nullptr && srcTensor->data()->size() > 0) { - MS_ASSERT(dstTensor->Size() == srcTensor->data()->size()); - if (WeightTensorNeedCopy(model, i)) { - auto dst_data = dstTensor->MutableData(); - if (dst_data == nullptr) { - MS_LOG(ERROR) << "MutableData from " << i << "th tensor is nullptr"; - delete dstTensor; - return RET_ERROR; - } - memcpy(dst_data, srcTensor->data()->data(), dstTensor->Size()); - copyed_tensor_idxes_.emplace_back(i); - } else { - int pack_size = srcTensor->data()->size(); - int org_size = dstTensor->Size(); - if (pack_size != org_size && (dataType == kNumberTypeInt8 || dataType == kNumberTypeInt16)) { - auto ret = dstTensor->MallocData(); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Malloc data for " << i << "tensor failed "; - delete dstTensor; - return RET_ERROR; - } - kernel::DequantUtil::UnPackToInt(srcTensor, dstTensor->MutableData()); - } else { - dstTensor->set_data(const_cast(srcTensor->data()->data())); - } - } - } - auto quant_params = srcTensor->quantParams(); - if (quant_params != nullptr) { - for (size_t j = 0; j < quant_params->size(); j++) { - QuantArg quant_arg{}; - quant_arg.bitNum = quant_params->Get(j)->numBits(); - quant_arg.scale = quant_params->Get(j)->scale(); - quant_arg.zeroPoint = quant_params->Get(j)->zeroPoint(); - quant_arg.var_corr = quant_params->Get(j)->varCorr(); - quant_arg.mean_corr = quant_params->Get(j)->meanCorr(); - quant_arg.inited = quant_params->Get(j)->inited(); - dstTensor->AddQuantParam(quant_arg); - } - } - auto quant_clusters = srcTensor->quantClusters(); - if (quant_clusters != nullptr) { - std::vector clusters; - for (size_t j = 0; j < quant_clusters->size(); j++) { - clusters.push_back(quant_clusters->Get(j)); - } - dstTensor->SetQuantClusters(clusters); + auto ret = ConvertTensorsData(model, i, srcTensor, dstTensor); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Convert data of " << i << "th tensor failed"; + delete (dstTensor); + return ret; } + ConvertTensorsQuantParam(srcTensor, dstTensor); this->tensors_.emplace_back(dstTensor); } - return RET_OK; } diff --git a/mindspore/lite/src/lite_session.h b/mindspore/lite/src/lite_session.h index e4b5996c75..2479784e5c 100644 --- a/mindspore/lite/src/lite_session.h +++ b/mindspore/lite/src/lite_session.h @@ -66,6 +66,11 @@ class LiteSession : public session::LiteSession { const std::vector> &dims) override; protected: + void ConvertTensorsQuantParam(const schema::Tensor *src_tensor, lite::Tensor *dst_tensor); + + int ConvertTensorsData(const lite::Model *model, size_t tensor_index, const schema::Tensor *src_tensor, + lite::Tensor *dst_tensor); + int ConvertTensors(const lite::Model *model); void InitGraphInOutTensors(const lite::Model *model); diff --git a/mindspore/lite/src/ops/primitive_c.cc b/mindspore/lite/src/ops/primitive_c.cc index 71f3235e48..ab56dbae83 100644 --- a/mindspore/lite/src/ops/primitive_c.cc +++ b/mindspore/lite/src/ops/primitive_c.cc @@ -184,40 +184,49 @@ void PrimitiveC::CalFloatScopeByMeanAndStddev(const double &mean, const double & *mMax = static_cast((qmax - mean) / stdDev); } -void PrimitiveC::PopulaterQuantParam(const Primitive &prim, const std::vector &inputs) { - auto narrow_range = prim.GetAttr("narrow_range"); - bool narrowRangeQuantParam = narrow_range != nullptr ? GetValue(narrow_range) : false; - auto num_bits = prim.GetAttr("num_bits"); - int32_t numbitsRangeQuantParam = num_bits != nullptr ? GetValue(num_bits) : 8; +void PrimitiveC::FillDefaultInputQuantParamIfNeed(const size_t &inputSize) { + std::vector quants; + schema::QuantParamT quantParam; + // fill input_quant_param_ by not inited quant_parm + if (input_quant_param_.size() < inputSize) { + schema::QuantParamT tmpQuantParam; + quants.emplace_back(tmpQuantParam); + input_quant_param_.insert(input_quant_param_.end(), inputSize - input_quant_param_.size(), quants); + } + if (input_quant_param_.size() == kDoubleNum) { + quants.clear(); + quantParam.min = 0.0; + quantParam.max = 0.0; + quantParam.zeroPoint = 0; + quantParam.scale = input_quant_param_.at(0).at(0).scale * input_quant_param_.at(1).at(0).scale; + quants.emplace_back(quantParam); + input_quant_param_.emplace_back(quants); + } +} + +void PrimitiveC::PopulaterInputQuantParam(const Primitive &prim, const std::vector &inputs, + bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam) { std::vector quants; schema::QuantParamT quantParam; - auto mean = prim.GetAttr("mean"); - auto std_dev = prim.GetAttr("std_dev"); - if (mean != nullptr && std_dev != nullptr) { - auto meanValue = GetValue(mean); - auto stddevValue = GetValue(std_dev); - float mMin = 0.0; - float mMax = 0.0; - CalFloatScopeByMeanAndStddev(meanValue, stddevValue, &mMin, &mMax); - quantParam.min = mMin; - quantParam.max = mMax; - } else { - auto inputMin = prim.GetAttr("input_minq"); - auto inputMax = prim.GetAttr("input_maxq"); - if (inputMin != nullptr && inputMax != nullptr) { - auto inputMinPtr = inputMin->cast(); - auto inputMaxPtr = inputMax->cast(); - auto *minBuf = static_cast(inputMinPtr->data_c()); - auto *maxBuf = static_cast(inputMaxPtr->data_c()); - quantParam.min = *minBuf; - quantParam.max = *maxBuf; + auto inputMin = prim.GetAttr("input_minq"); + auto inputMax = prim.GetAttr("input_maxq"); + if (inputMin != nullptr && inputMax != nullptr) { + auto inputMinPtr = inputMin->cast(); + auto inputMaxPtr = inputMax->cast(); + auto *minBuf = static_cast(inputMinPtr->data_c()); + auto *maxBuf = static_cast(inputMaxPtr->data_c()); + quantParam.min = *minBuf; + quantParam.max = *maxBuf; + auto ret = quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, + numbitsRangeQuantParam); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Can't calculate quant parameters"; + return; } + quants.emplace_back(quantParam); + input_quant_param_.emplace_back(quants); } - quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, - numbitsRangeQuantParam); - quants.emplace_back(quantParam); - input_quant_param_.emplace_back(quants); quants.clear(); auto filterMin = prim.GetAttr("filter_minq"); @@ -235,30 +244,21 @@ void PrimitiveC::PopulaterQuantParam(const Primitive &prim, const std::vector quants; + schema::QuantParamT quantParam; auto outputMin = prim.GetAttr("output_minq"); auto outputMax = prim.GetAttr("output_maxq"); if (outputMin != nullptr && outputMax != nullptr) { @@ -268,8 +268,12 @@ void PrimitiveC::PopulaterQuantParam(const Primitive &prim, const std::vector(outputMaxPtr->data_c()); quantParam.min = *minBuf; quantParam.max = *maxBuf; - quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, - numbitsRangeQuantParam); + auto ret = quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, + numbitsRangeQuantParam); + if (ret != RET_OK) { + MS_LOG(ERROR) << "Can't calculate quant parameters"; + return; + } quants.emplace_back(quantParam); output_quant_param_.emplace_back(quants); } else { @@ -279,6 +283,15 @@ void PrimitiveC::PopulaterQuantParam(const Primitive &prim, const std::vector &inputs) { + auto narrow_range = prim.GetAttr("narrow_range"); + bool narrowRangeQuantParam = narrow_range != nullptr ? GetValue(narrow_range) : false; + auto num_bits = prim.GetAttr("num_bits"); + int32_t numbitsRangeQuantParam = num_bits != nullptr ? GetValue(num_bits) : 8; + PopulaterInputQuantParam(prim, inputs, narrowRangeQuantParam, numbitsRangeQuantParam); + PopulaterOutputQuantParam(prim, narrowRangeQuantParam, numbitsRangeQuantParam); +} + void PrimitiveC::GetAttrDataFromInput(const AnfNodePtr inputNode, std::vector *data) { if (inputNode->isa()) { auto valNode = inputNode->cast(); diff --git a/mindspore/lite/src/ops/primitive_c.h b/mindspore/lite/src/ops/primitive_c.h index 542157d231..9dc0519958 100644 --- a/mindspore/lite/src/ops/primitive_c.h +++ b/mindspore/lite/src/ops/primitive_c.h @@ -128,6 +128,10 @@ class PrimitiveC : public mindspore::Primitive { static std::shared_ptr Create(const Primitive &prim, const std::vector &inputs, const schema::QuantType &quantType); + void FillDefaultInputQuantParamIfNeed(const size_t &inputSize); + void PopulaterInputQuantParam(const Primitive &prim, const std::vector &inputs, + bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam); + void PopulaterOutputQuantParam(const Primitive &prim, bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam); void PopulaterQuantParam(const Primitive &prim, const std::vector &inputs); void CalFloatScopeByMeanAndStddev(const double &mean, const double &stdDev, float *mMin, float *mMax); diff --git a/mindspore/lite/src/runtime/runtime_api.cc b/mindspore/lite/src/runtime/runtime_api.cc index 27fbe32449..3045e64c62 100644 --- a/mindspore/lite/src/runtime/runtime_api.cc +++ b/mindspore/lite/src/runtime/runtime_api.cc @@ -17,7 +17,6 @@ #include "src/runtime/runtime_api.h" #include #include -#include "src/runtime/workspace_pool.h" #include "src/common/log_adapter.h" static std::mutex gWorkspaceMutex; @@ -28,26 +27,6 @@ extern "C" { ThreadPool *CreateLiteThreadPool(int thread_num, int mode) { return CreateThreadPool(thread_num, mode); } void LiteAPISetLastError(const char *msg) { MS_LOG(ERROR) << "The lite api set last error is " << msg; } - -void *LiteBackendAllocWorkspace(int deviceType, int deviceId, uint64_t size, int dtypeCode, int dtypeBits) { - std::lock_guard lock(gWorkspaceMutex); - auto p = mindspore::predict::WorkspacePool::GetInstance(); - if (p == nullptr) { - MS_LOG(ERROR) << "Get thread pool instance failed"; - return nullptr; - } - return p->AllocWorkSpaceMem(size); -} - -int LiteBackendFreeWorkspace(int deviceType, int deviceId, const void *ptr) { - std::lock_guard lock(gWorkspaceMutex); - auto p = mindspore::predict::WorkspacePool::GetInstance(); - if (p == nullptr) { - return -1; - } - p->FreeWorkSpaceMem(ptr); - return 0; -} #ifdef __cplusplus } #endif diff --git a/mindspore/lite/src/runtime/runtime_api.h b/mindspore/lite/src/runtime/runtime_api.h index f754e40ae5..29083aa5c1 100644 --- a/mindspore/lite/src/runtime/runtime_api.h +++ b/mindspore/lite/src/runtime/runtime_api.h @@ -36,9 +36,6 @@ struct ThreadPool; #endif INTERNAL_API_DLL ThreadPool *CreateLiteThreadPool(int thread_num, int mode); INTERNAL_API_DLL void LiteAPISetLastError(const char *msg); -INTERNAL_API_DLL void *LiteBackendAllocWorkspace(int deviceType, int deviceId, uint64_t size, int dtypeCode, - int dtypeBits); -INTERNAL_API_DLL int LiteBackendFreeWorkspace(int deviceType, int deviceId, const void *ptr); INTERNAL_API_DLL int LiteBackendRegisterSystemLibSymbol(const char *name, void *ptr); #ifdef __cplusplus } diff --git a/mindspore/lite/src/runtime/thread_pool.c b/mindspore/lite/src/runtime/thread_pool.c index 90c326d2ac..45b8d84b90 100644 --- a/mindspore/lite/src/runtime/thread_pool.c +++ b/mindspore/lite/src/runtime/thread_pool.c @@ -44,7 +44,6 @@ #define RET_TP_SYSTEM_ERROR (-1) #define MAX_THREAD_NUM (8) -#define MAX_THREAD_POOL_NUM (4) #define DEFAULT_SPIN_COUNT (30000) typedef struct { @@ -509,60 +508,74 @@ int BindMasterThread(struct ThreadPool *thread_pool, bool is_bind) { return RET_TP_OK; } -int BindSalverThreads(struct ThreadPool *thread_pool, bool is_bind) { - if (thread_pool == NULL) { - LOG_ERROR("get thread pool instane failed"); - return RET_TP_ERROR; +int FreeBindSalverThreads(struct ThreadPool *thread_pool) { + cpu_set_t mask; + CPU_ZERO(&mask); + for (int i = 0; i < gHigNum + gMidNum; ++i) { + CPU_SET(cpu_cores[i], &mask); + } + for (int i = 0; i < thread_pool->thread_num - 1; ++i) { + Thread *thread = GetThread(thread_pool, i); + if (thread == NULL) { + LOG_ERROR("get thread failed, thread_id: %d", i); + return false; + } + int ret = SetAffinity(thread->pthread, &mask); + if (ret != RET_TP_OK) { + LOG_ERROR("set thread affinity failed"); + return RET_TP_ERROR; + } } + return RET_TP_OK; +} + +int DoBindSalverThreads(struct ThreadPool *thread_pool) { cpu_set_t mask; - if (is_bind && thread_pool->mode != NO_BIND_MODE) { - unsigned int attach_id; - for (int i = 0; i < thread_pool->thread_num - 1; ++i) { - if (thread_pool->mode == MID_MODE) { - int core_id = gHigNum + gMidNum - i - 2; - if (core_id >= 0) { - attach_id = cpu_cores[core_id]; - } else { - attach_id = cpu_cores[0]; - } + unsigned int attach_id; + for (int i = 0; i < thread_pool->thread_num - 1; ++i) { + if (thread_pool->mode == MID_MODE) { + int core_id = gHigNum + gMidNum - i - 2; + if (core_id >= 0) { + attach_id = cpu_cores[core_id]; } else { - attach_id = cpu_cores[i + 1]; - } - LOG_INFO("mode: %d, attach id: %u", thread_pool->mode, attach_id); - CPU_ZERO(&mask); - CPU_SET(attach_id, &mask); - Thread *thread = GetThread(thread_pool, i); - if (thread == NULL) { - LOG_ERROR("get thread failed, thread_id: %d", i); - return false; - } - int ret = SetAffinity(thread->pthread, &mask); - if (ret != RET_TP_OK) { - LOG_ERROR("set thread affinity failed"); - return RET_TP_ERROR; + attach_id = cpu_cores[0]; } + } else { + attach_id = cpu_cores[i + 1]; } - } else { + LOG_INFO("mode: %d, attach id: %u", thread_pool->mode, attach_id); CPU_ZERO(&mask); - for (int i = 0; i < gHigNum + gMidNum; ++i) { - CPU_SET(cpu_cores[i], &mask); + CPU_SET(attach_id, &mask); + Thread *thread = GetThread(thread_pool, i); + if (thread == NULL) { + LOG_ERROR("get thread failed, thread_id: %d", i); + return false; } - for (int i = 0; i < thread_pool->thread_num - 1; ++i) { - Thread *thread = GetThread(thread_pool, i); - if (thread == NULL) { - LOG_ERROR("get thread failed, thread_id: %d", i); - return false; - } - int ret = SetAffinity(thread->pthread, &mask); - if (ret != RET_TP_OK) { - LOG_ERROR("set thread affinity failed"); - return RET_TP_ERROR; - } + int ret = SetAffinity(thread->pthread, &mask); + if (ret != RET_TP_OK) { + LOG_ERROR("set thread affinity failed"); + return RET_TP_ERROR; } } - LOG_INFO("BindSalverThreads success"); return RET_TP_OK; } + +int BindSalverThreads(struct ThreadPool *thread_pool, bool is_bind) { + if (thread_pool == NULL) { + LOG_ERROR("get thread pool instane failed"); + return RET_TP_ERROR; + } + int ret; + if (is_bind && thread_pool->mode != NO_BIND_MODE) { + ret = DoBindSalverThreads(thread_pool); + } else { + ret = FreeBindSalverThreads(thread_pool); + } + if (ret == RET_TP_OK) { + LOG_INFO("BindSalverThreads success"); + } + return ret; +} #endif int BindThreads(struct ThreadPool *thread_pool, bool is_bind, int mode) { @@ -782,46 +795,6 @@ int CreateNewThread(struct ThreadPool *thread_pool, int thread_id) { return RET_TP_OK; } -int ReConfigThreadPool(struct ThreadPool *thread_pool, int thread_num, int mode) { - LOG_INFO("reconfig thread pool, thread_num: %d, mode: %d", thread_num, mode); - if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { - LOG_ERROR("invalid thread num: %d", thread_num); - return RET_TP_ERROR; - } - if (thread_pool == NULL) { - LOG_ERROR("get thread pool instane failed"); - return RET_TP_ERROR; - } - if (thread_num <= thread_pool->thread_num) { - LOG_INFO("no need to add thread"); - return RET_TP_OK; - } - int curr_thread_num = thread_pool->thread_num; - thread_pool->thread_num = thread_num > MAX_THREAD_NUM ? MAX_THREAD_NUM : thread_num; - thread_pool->mode = mode; - if (thread_pool->thread_list == NULL) { - thread_pool->thread_list = (ThreadList *)malloc(sizeof(ThreadList)); - if (thread_pool->thread_list == NULL) { - LOG_ERROR("create thread list failed"); - DestroyThreadPool(thread_pool); - return RET_TP_ERROR; - } - thread_pool->thread_list->head = NULL; - thread_pool->thread_list->tail = NULL; - thread_pool->thread_list->size = 0; - pthread_mutex_init(&thread_pool->thread_list->lock, NULL); - } - int add_thread_num = thread_pool->thread_num - curr_thread_num; - for (int i = curr_thread_num - 1, j = 0; j < add_thread_num; ++i, ++j) { - int ret = CreateNewThread(thread_pool, i); - if (ret != RET_TP_OK) { - LOG_ERROR("create new thread failed"); - return RET_TP_ERROR; - } - } - return BindThreads(thread_pool, true, mode); -} - ThreadPool *CreateThreadPool(int thread_num, int mode) { LOG_INFO("create thread pool, thread_num: %d, mode: %d", thread_num, mode); if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { @@ -873,18 +846,6 @@ ThreadPool *CreateThreadPool(int thread_num, int mode) { return thread_pool; } -int ConfigThreadPool(struct ThreadPool *thread_pool, int thread_num, int mode) { - if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { - LOG_ERROR("invalid thread num: %d", thread_num); - return RET_TP_ERROR; - } - int ret = ReConfigThreadPool(thread_pool, thread_num, mode); - if (ret != RET_TP_OK) { - LOG_ERROR("reconfig thread pool failed, thread_num: %d, mode: %d", thread_num, mode); - } - return ret; -} - void ActivateThreadPool(struct ThreadPool *thread_pool) { if (thread_pool == NULL) { LOG_ERROR("get thread pool instane failed"); diff --git a/mindspore/lite/src/runtime/thread_pool.h b/mindspore/lite/src/runtime/thread_pool.h index e848b6be71..cfde32f7d3 100644 --- a/mindspore/lite/src/runtime/thread_pool.h +++ b/mindspore/lite/src/runtime/thread_pool.h @@ -28,25 +28,10 @@ typedef enum { MID_MODE = 2 /**< bind middle cpu first */ } BindMode; -/// \brief ThreadPoolId defined for specifying which thread pool to use. -typedef enum { - THREAD_POOL_DEFAULT = 0, /**< default thread pool id */ - THREAD_POOL_SECOND = 1, /**< the second thread pool id */ - THREAD_POOL_THIRD = 2, /**< the third thread pool id */ - THREAD_POOL_FOURTH = 3 /**< the fourth thread pool id */ -} ThreadPoolId; - struct ThreadPool; struct ThreadPool *CreateThreadPool(int thread_num, int mode); -/** - * create thread pool and init - * @param thread_num - * @param mode - */ -int ConfigThreadPool(struct ThreadPool *thread_pool, int thread_num, int mode); - /** * * @param session_index, support multi session diff --git a/mindspore/lite/src/runtime/workspace_pool.cc b/mindspore/lite/src/runtime/workspace_pool.cc deleted file mode 100644 index aa3b83a34e..0000000000 --- a/mindspore/lite/src/runtime/workspace_pool.cc +++ /dev/null @@ -1,154 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/runtime/workspace_pool.h" -#ifdef __APPLE__ -#include -#else -#include -#endif -#include -#include "src/common/log_adapter.h" - -namespace mindspore { -namespace predict { -static constexpr size_t kWorkspacePageSize = 4096; -static constexpr int kTempAllocaAlignment = 64; -WorkspacePool *WorkspacePool::GetInstance() { - static WorkspacePool instance; - return &instance; -} - -void *WorkspacePool::AllocWorkSpaceMem(size_t size) { - size_t nbytes = (size + (kWorkspacePageSize - 1)) / kWorkspacePageSize * kWorkspacePageSize; - if (nbytes == 0) { - nbytes = kWorkspacePageSize; - } - std::pair alloc; - // fist alloc - if (freeList.empty()) { - alloc.first = nbytes; -#ifdef __APPLE__ - int err = posix_memalign(&alloc.second, kTempAllocaAlignment, nbytes); - if (err != 0) { - MS_LOGE("posix_memalign failed, error code:%d", err); - return alloc.second; - } -#else -#ifdef _WIN32 - alloc.second = _aligned_malloc(nbytes, kTempAllocaAlignment); -#else - alloc.second = memalign(kTempAllocaAlignment, nbytes); -#endif -#endif - } else if (freeList.size() == 1) { // one element - alloc = *(freeList.begin()); - freeList.erase(freeList.begin()); - if (alloc.first < nbytes) { - free(alloc.second); - alloc.first = nbytes; -#ifdef __APPLE__ - int err = posix_memalign(&alloc.second, kTempAllocaAlignment, nbytes); - if (err != 0) { - MS_LOGE("posix_memalign failed, error code:%d", err); - return alloc.second; - } -#else -#ifdef _WIN32 - alloc.second = _aligned_malloc(nbytes, kTempAllocaAlignment); -#else - alloc.second = memalign(kTempAllocaAlignment, nbytes); -#endif -#endif - } - } else { - if ((*(freeList.begin())).first >= nbytes) { - auto iter = freeList.begin(); - for (; iter != freeList.end(); ++iter) { - if ((*iter).first < size) { - alloc = *(--iter); - freeList.erase(iter); - break; - } - } - if (iter == freeList.end()) { - alloc = *(freeList.rbegin()); - freeList.erase(--freeList.end()); - } - } else { - alloc = *(freeList.begin()); - freeList.erase(freeList.begin()); - free(alloc.second); - alloc.first = nbytes; -#ifdef __APPLE__ - int err = posix_memalign(&alloc.second, kTempAllocaAlignment, nbytes); - if (err != 0) { - MS_LOGE("posix_memalign failed, error code:%d", err); - return alloc.second; - } -#else -#ifdef _WIN32 - alloc.second = _aligned_malloc(nbytes, kTempAllocaAlignment); -#else - alloc.second = memalign(kTempAllocaAlignment, nbytes); -#endif -#endif - } - } - allocList.emplace_back(alloc); - return alloc.second != nullptr ? alloc.second : nullptr; -} - -void WorkspacePool::FreeWorkSpaceMem(const void *ptr) { - if (ptr == nullptr) { - return; - } - std::pair alloc; - if (allocList.empty()) { - MS_LOG(ERROR) << "no mem have been alloc"; - return; - } else if (allocList.back().second == ptr) { - alloc = allocList.back(); - allocList.pop_back(); - } else { - auto iter = allocList.begin(); - for (; iter != allocList.end(); ++iter) { - if ((*iter).second == ptr) { - alloc = *iter; - allocList.erase(iter); - break; - } - } - if (iter == allocList.end()) { - MS_LOG(ERROR) << "no value ptr have been alloc"; - return; - } - } - freeList.insert(alloc); -} - -WorkspacePool::~WorkspacePool() { - for (auto &a : allocList) { - free(a.second); - } - allocList.clear(); - for (auto &f : freeList) { - free(f.second); - } - freeList.clear(); -} -} // namespace predict -} // namespace mindspore diff --git a/mindspore/lite/src/runtime/workspace_pool.h b/mindspore/lite/src/runtime/workspace_pool.h deleted file mode 100644 index f4e13aeb88..0000000000 --- a/mindspore/lite/src/runtime/workspace_pool.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_LITE_SRC_RUNTIME_WORKSPACE_POOL_H_ -#define MINDSPORE_LITE_SRC_RUNTIME_WORKSPACE_POOL_H_ -#include -#include -#include -#include -#include -#include - -namespace mindspore { -namespace predict { -class WorkspacePool { - public: - WorkspacePool() = default; - ~WorkspacePool(); - WorkspacePool(const WorkspacePool &) = delete; - WorkspacePool &operator=(const WorkspacePool &) = delete; - static WorkspacePool *GetInstance(); - void *AllocWorkSpaceMem(size_t size); - void FreeWorkSpaceMem(const void *ptr); - - private: - std::vector> allocList{}; - std::set, std::greater>> freeList{}; -}; -} // namespace predict -} // namespace mindspore -#endif // MINDSPORE_LITE_SRC_RUNTIME_WORKSPACE_POOL_H_ diff --git a/mindspore/lite/src/scheduler.cc b/mindspore/lite/src/scheduler.cc index 3970b1950d..be2486f008 100644 --- a/mindspore/lite/src/scheduler.cc +++ b/mindspore/lite/src/scheduler.cc @@ -171,6 +171,33 @@ int Scheduler::BuildKernels(const lite::Model *model, std::vector *ten return RET_OK; } +std::vector Scheduler::FindAllSubGraphKernels( + kernel::LiteKernel *head_kernel, std::map *sinked_kernel_map) { + MS_ASSERT(head_kernel != nullptr); + MS_ASSERT(sinked_kernel_map != nullptr); + std::vector sub_kernels; + std::queue kernel_queue; + kernel_queue.emplace(head_kernel); + auto cur_sub_graph_type = mindspore::lite::Scheduler::GetKernelSubGraphType(head_kernel); + while (!kernel_queue.empty()) { + auto cur_kernel = kernel_queue.front(); + kernel_queue.pop(); + (*sinked_kernel_map)[cur_kernel] = true; + sub_kernels.emplace_back(cur_kernel); + auto post_kernels = cur_kernel->out_kernels(); + for (auto post_kernel : post_kernels) { + if (cur_sub_graph_type == mindspore::lite::Scheduler::GetKernelSubGraphType(post_kernel)) { + auto post_kernel_inputs = post_kernel->in_kernels(); + if (std::all_of(post_kernel_inputs.begin(), post_kernel_inputs.end(), + [&](kernel::LiteKernel *kernel) { return (*sinked_kernel_map)[kernel]; })) { + kernel_queue.emplace(post_kernel); + } + } + } + } + return sub_kernels; +} + int Scheduler::ConstructSubGraphs(std::vector *kernels) { auto old_kernels = *kernels; kernels->clear(); @@ -195,26 +222,8 @@ int Scheduler::ConstructSubGraphs(std::vector *kernels) { return RET_NOT_SUPPORT; } - std::vector sub_kernels; - std::queue kernel_queue; - kernel_queue.emplace(head_kernel); auto cur_sub_graph_type = mindspore::lite::Scheduler::GetKernelSubGraphType(head_kernel); - while (!kernel_queue.empty()) { - auto cur_kernel = kernel_queue.front(); - kernel_queue.pop(); - is_kernel_sinked[cur_kernel] = true; - sub_kernels.emplace_back(cur_kernel); - auto post_kernels = cur_kernel->out_kernels(); - for (auto post_kernel : post_kernels) { - if (cur_sub_graph_type == mindspore::lite::Scheduler::GetKernelSubGraphType(post_kernel)) { - auto post_kernel_inputs = post_kernel->in_kernels(); - if (std::all_of(post_kernel_inputs.begin(), post_kernel_inputs.end(), - [&](kernel::LiteKernel *kernel) { return is_kernel_sinked[kernel]; })) { - kernel_queue.emplace(post_kernel); - } - } - } - } + auto sub_kernels = FindAllSubGraphKernels(head_kernel, &is_kernel_sinked); auto subgraph = CreateSubGraphKernel(sub_kernels, cur_sub_graph_type); if (subgraph == nullptr) { MS_LOG(ERROR) << "Create SubGraphKernel failed"; diff --git a/mindspore/lite/src/scheduler.h b/mindspore/lite/src/scheduler.h index b2e8d3fefb..0b57c63be1 100644 --- a/mindspore/lite/src/scheduler.h +++ b/mindspore/lite/src/scheduler.h @@ -18,6 +18,7 @@ #define MINDSPORE_LITE_SRC_SCHEDULER_H_ #include +#include #include "src/sub_graph_kernel.h" #include "src/inner_context.h" #include "include/model.h" @@ -42,6 +43,9 @@ class Scheduler { static int InferShape(const lite::Model *model, std::vector *tensors); + std::vector FindAllSubGraphKernels( + kernel::LiteKernel *head_kernel, std::map *sinked_kernel_map); + int ConstructSubGraphs(std::vector *kernels); kernel::SubGraphKernel *CreateSubGraphKernel(const std::vector &kernels, diff --git a/mindspore/lite/src/tensor.cc b/mindspore/lite/src/tensor.cc index e78e5ec969..ac49004506 100644 --- a/mindspore/lite/src/tensor.cc +++ b/mindspore/lite/src/tensor.cc @@ -256,54 +256,19 @@ std::string Tensor::ToString() const { oss << std::endl << "Data:"; switch (this->data_type_) { case kNumberTypeFloat32: { - auto data = static_cast(this->data_); - if (data == nullptr) { - return "Data of tensor is nullptr"; - } else { - for (int i = 0; i < 40 && i < this->ElementsNum(); i++) { - oss << " " << data[i]; - } - } + oss << DataToString(this->data_c(), this->ElementsNum()); } break; case kNumberTypeFloat16: { - auto data = static_cast(this->data_); - if (data == nullptr) { - oss << " Data of tensor is nullptr"; - } else { - for (int i = 0; i < 40 && i < this->ElementsNum(); i++) { - oss << " " << data[i]; - } - } + oss << DataToString(this->data_c(), this->ElementsNum()); } break; case kNumberTypeInt32: { - auto data = static_cast(this->data_); - if (data == nullptr) { - oss << " Data of tensor is nullptr"; - } else { - for (int i = 0; i < 40 && i < this->ElementsNum(); i++) { - oss << " " << data[i]; - } - } + oss << DataToString(this->data_c(), this->ElementsNum()); } break; case kNumberTypeInt16: { - auto data = static_cast(this->data_); - if (data == nullptr) { - oss << " Data of tensor is nullptr"; - } else { - for (int i = 0; i < 40 && i < this->ElementsNum(); i++) { - oss << " " << data[i]; - } - } + oss << DataToString(this->data_c(), this->ElementsNum()); } break; case kNumberTypeInt8: { - auto data = static_cast(this->data_); - if (data == nullptr) { - oss << " Data of tensor is nullptr"; - } else { - for (int i = 0; i < 40 && i < this->ElementsNum(); i++) { - oss << " " << static_cast(data[i]); - } - } + oss << DataToString(this->data_c(), this->ElementsNum()); } break; default: oss << "Unsupported data type to print"; diff --git a/mindspore/lite/src/tensor.h b/mindspore/lite/src/tensor.h index 2f3b7fc310..d5d16dc51c 100644 --- a/mindspore/lite/src/tensor.h +++ b/mindspore/lite/src/tensor.h @@ -134,6 +134,20 @@ class Tensor : public mindspore::tensor::MSTensor { } } + private: + template + std::string DataToString(void *data, size_t data_number) const { + if (data == nullptr) { + return "Data of tensor is nullptr"; + } + std::ostringstream oss; + auto casted_data = static_cast(data); + for (size_t i = 0; i < 40 && i < data_number; i++) { + oss << " " << casted_data[i]; + } + return oss.str(); + } + protected: void *data_ = nullptr; void *device_data_ = nullptr; diff --git a/mindspore/lite/test/CMakeLists.txt b/mindspore/lite/test/CMakeLists.txt index 4501c3d001..0d708e69ac 100644 --- a/mindspore/lite/test/CMakeLists.txt +++ b/mindspore/lite/test/CMakeLists.txt @@ -119,7 +119,6 @@ set(TEST_LITE_SRC ${LITE_DIR}/src/runtime/allocator.cc ${LITE_DIR}/src/runtime/runtime_api.cc ${LITE_DIR}/src/runtime/thread_pool.c - ${LITE_DIR}/src/runtime/workspace_pool.cc ${LITE_DIR}/src/runtime/parallel_executor.cc ${LITE_DIR}/src/tensor.cc ${LITE_DIR}/src/executor.cc diff --git a/mindspore/lite/tools/converter/CMakeLists.txt b/mindspore/lite/tools/converter/CMakeLists.txt index 51413a7506..3a25dc9818 100644 --- a/mindspore/lite/tools/converter/CMakeLists.txt +++ b/mindspore/lite/tools/converter/CMakeLists.txt @@ -68,7 +68,6 @@ set(LITE_SRC ${SRC_DIR}/runtime/allocator.cc ${SRC_DIR}/runtime/runtime_api.cc ${SRC_DIR}/runtime/thread_pool.c - ${SRC_DIR}/runtime/workspace_pool.cc ${SRC_DIR}/inner_context.cc ${SRC_DIR}/tensor.cc ${SRC_DIR}/kernel_registry.cc