From 7c4b66ccb8182b0fd7de7145bdb9b35577df38cb Mon Sep 17 00:00:00 2001 From: zhaodezan Date: Sat, 31 Jul 2021 17:31:32 +0800 Subject: [PATCH] master fix magic --- .../lite/src/common/dynamic_library_loader.cc | 11 ++- .../lite/src/common/dynamic_library_loader.h | 6 +- mindspore/lite/src/ms_tensor.cc | 1 - .../kernel/arm/fp32/uniform_real_fp32.cc | 86 +++++++++++-------- mindspore/lite/tools/converter/converter.cc | 2 +- .../parser/caffe/caffe_model_parser.cc | 5 +- 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/mindspore/lite/src/common/dynamic_library_loader.cc b/mindspore/lite/src/common/dynamic_library_loader.cc index 68639f9de37..de180f221d3 100644 --- a/mindspore/lite/src/common/dynamic_library_loader.cc +++ b/mindspore/lite/src/common/dynamic_library_loader.cc @@ -15,7 +15,6 @@ */ #include "src/common/dynamic_library_loader.h" -#include #include #ifndef _WIN32 #include @@ -29,11 +28,11 @@ namespace mindspore { namespace lite { -int DynamicLibraryLoader::Open(const char *lib_path) { +int DynamicLibraryLoader::Open(std::string lib_path) { if (handler_ != nullptr) { return RET_ERROR; } - std::string real_path = RealPath(lib_path); + std::string real_path = RealPath(lib_path.c_str()); #ifndef _WIN32 handler_ = dlopen(real_path.c_str(), RTLD_LAZY); @@ -47,11 +46,11 @@ int DynamicLibraryLoader::Open(const char *lib_path) { return RET_OK; } -void *DynamicLibraryLoader::GetFunc(const char *func_name) { +void *DynamicLibraryLoader::GetFunc(std::string func_name) { #ifndef _WIN32 - return dlsym(handler_, func_name); + return dlsym(handler_, func_name.c_str()); #else - auto func = GetProcAddress(reinterpret_cast(handler_), func_name); + auto func = GetProcAddress(reinterpret_cast(handler_), func_name.c_str()); return reinterpret_cast(func); #endif } diff --git a/mindspore/lite/src/common/dynamic_library_loader.h b/mindspore/lite/src/common/dynamic_library_loader.h index bfe5fd86603..2d07dff0fb6 100644 --- a/mindspore/lite/src/common/dynamic_library_loader.h +++ b/mindspore/lite/src/common/dynamic_library_loader.h @@ -17,14 +17,16 @@ #ifndef MINDSPORE_LITE_SRC_COMMON_DYNAMIC_LIBRARY_LOADER_H_ #define MINDSPORE_LITE_SRC_COMMON_DYNAMIC_LIBRARY_LOADER_H_ +#include + namespace mindspore { namespace lite { class DynamicLibraryLoader { public: DynamicLibraryLoader() = default; ~DynamicLibraryLoader(); - int Open(const char *lib_path); - void *GetFunc(const char *func_name); + int Open(std::string lib_path); + void *GetFunc(std::string func_name); int Close(); private: diff --git a/mindspore/lite/src/ms_tensor.cc b/mindspore/lite/src/ms_tensor.cc index c0c34d6f8c4..ea033136034 100644 --- a/mindspore/lite/src/ms_tensor.cc +++ b/mindspore/lite/src/ms_tensor.cc @@ -32,6 +32,5 @@ tensor::MSTensor *tensor::MSTensor::CreateTensor(const std::string &name, TypeId tensor->set_data_type(type); return tensor; } - } // namespace tensor } // namespace mindspore diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/uniform_real_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/uniform_real_fp32.cc index bcce63a7686..1e5b39eff27 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/uniform_real_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/uniform_real_fp32.cc @@ -26,31 +26,49 @@ using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_UniformReal; namespace mindspore::kernel { +namespace { +constexpr size_t kFirstKeyIndex = 0; +constexpr size_t kSecondKeyIndex = 1; +constexpr size_t kFirstCounterIndex = 0; +constexpr size_t kSecondCounterIndex = 1; +constexpr size_t kThirdCounterIndex = 2; +constexpr size_t kFourthCounterIndex = 3; +constexpr size_t kFirstResultIndex = 0; +constexpr size_t kSecondResultIndex = 1; +constexpr size_t kThirdResultIndex = 2; +constexpr size_t kFourthResultIndex = 3; +constexpr size_t kFirstDataIndex = 0; +constexpr size_t kSecondDataIndex = 1; +constexpr size_t kThirdDataIndex = 2; +constexpr size_t kFourthDataIndex = 3; +constexpr size_t kBitWidth = 32; +constexpr size_t kPerSegNum = 4; +} // namespace class PhiloxRandom { public: explicit PhiloxRandom(uint64_t seed_lo, uint64_t seed_hi) { - key_[0] = static_cast(seed_lo); - key_[1] = static_cast(seed_lo >> 32); - counter_[2] = static_cast(seed_hi); - counter_[3] = static_cast(seed_hi >> 32); + key_[kFirstKeyIndex] = static_cast(seed_lo); + key_[kSecondKeyIndex] = static_cast(seed_lo >> kBitWidth); + counter_[kThirdCounterIndex] = static_cast(seed_hi); + counter_[kFourthCounterIndex] = static_cast(seed_hi >> kBitWidth); } ~PhiloxRandom() = default; // Skip the specified number of samples of 128-bits in the current stream. void Skip(uint64_t count) { const uint32_t count_lo = static_cast(count); - uint32_t count_hi = static_cast(count >> 32); + uint32_t count_hi = static_cast(count >> kBitWidth); - counter_[0] += count_lo; - if (counter_[0] < count_lo) { + counter_[kFirstCounterIndex] += count_lo; + if (counter_[kFirstCounterIndex] < count_lo) { ++count_hi; } - counter_[1] += count_hi; - if (counter_[1] < count_hi) { - if (++counter_[2] == 0) { - ++counter_[3]; + counter_[kSecondCounterIndex] += count_hi; + if (counter_[kSecondCounterIndex] < count_hi) { + if (++counter_[kThirdCounterIndex] == 0) { + ++counter_[kFourthCounterIndex]; } } } @@ -95,10 +113,10 @@ class PhiloxRandom { // Helper function to skip the next sample of 128-bits in the current stream. void SkipOne() { - if (++counter_[0] == 0) { - if (++counter_[1] == 0) { - if (++counter_[2] == 0) { - ++counter_[3]; + if (++counter_[kFirstCounterIndex] == 0) { + if (++counter_[kSecondCounterIndex] == 0) { + if (++counter_[kThirdCounterIndex] == 0) { + ++counter_[kFourthCounterIndex]; } } } @@ -107,7 +125,7 @@ class PhiloxRandom { static void MultiplyHighLow(uint32_t a, uint32_t b, uint32_t *result_low, uint32_t *result_high) { const uint64_t product = static_cast(a) * b; *result_low = static_cast(product); - *result_high = static_cast(product >> 32); + *result_high = static_cast(product >> kBitWidth); } // Helper function for a single round of the underlying Philox algorithm. @@ -115,17 +133,17 @@ class PhiloxRandom { const std::vector &key) { uint32_t lo0; uint32_t hi0; - MultiplyHighLow(kPhiloxM4x32A, counter[0], &lo0, &hi0); + MultiplyHighLow(kPhiloxM4x32A, counter[kFirstCounterIndex], &lo0, &hi0); uint32_t lo1; uint32_t hi1; - MultiplyHighLow(kPhiloxM4x32B, counter[2], &lo1, &hi1); + MultiplyHighLow(kPhiloxM4x32B, counter[kThirdCounterIndex], &lo1, &hi1); std::vector result = {0, 0, 0, 0}; - result[0] = hi1 ^ counter[1] ^ key[0]; - result[1] = lo1; - result[2] = hi0 ^ counter[3] ^ key[1]; - result[3] = lo0; + result[kFirstResultIndex] = hi1 ^ counter[kSecondCounterIndex] ^ key[0]; + result[kSecondResultIndex] = lo1; + result[kThirdResultIndex] = hi0 ^ counter[kFourthCounterIndex] ^ key[1]; + result[kFourthResultIndex] = lo0; return result; } @@ -152,29 +170,29 @@ float uint32ToFloat(uint32_t x) { void GetPhiloxRandomFloat(float *data, size_t length, int seed, int seed2) { PhiloxRandom philoxRandom(seed, seed2); - if (length < 4) { + if (length < kPerSegNum) { auto randNum = philoxRandom.operator()(); for (size_t i = 0; i < length; i++) { data[i] = uint32ToFloat(randNum[i]); } } else { auto randNum = philoxRandom.operator()(); - data[0] = uint32ToFloat(randNum[0]); - data[1] = uint32ToFloat(randNum[1]); - data[2] = uint32ToFloat(randNum[2]); - data[3] = uint32ToFloat(randNum[3]); - for (size_t i = 1; i < length / 4; i++) { + data[kFirstDataIndex] = uint32ToFloat(randNum[kFirstDataIndex]); + data[kSecondDataIndex] = uint32ToFloat(randNum[kSecondDataIndex]); + data[kThirdDataIndex] = uint32ToFloat(randNum[kThirdDataIndex]); + data[kFourthDataIndex] = uint32ToFloat(randNum[kFourthDataIndex]); + for (size_t i = 1; i < length / kPerSegNum; i++) { philoxRandom.Skip(0); randNum = philoxRandom.operator()(); - data[4 * i] = uint32ToFloat(randNum[0]); - data[4 * i + 1] = uint32ToFloat(randNum[1]); - data[4 * i + 2] = uint32ToFloat(randNum[2]); - data[4 * i + 3] = uint32ToFloat(randNum[3]); + data[kPerSegNum * i] = uint32ToFloat(randNum[0]); + data[kPerSegNum * i + 1] = uint32ToFloat(randNum[1]); + data[kPerSegNum * i + 2] = uint32ToFloat(randNum[2]); + data[kPerSegNum * i + 3] = uint32ToFloat(randNum[3]); } philoxRandom.Skip(0); randNum = philoxRandom.operator()(); - for (size_t i = 0; i < length % 4; i++) { - data[4 * (length / 4) + i] = uint32ToFloat(randNum[i]); + for (size_t i = 0; i < length % kPerSegNum; i++) { + data[kPerSegNum * (length / kPerSegNum) + i] = uint32ToFloat(randNum[i]); } } } diff --git a/mindspore/lite/tools/converter/converter.cc b/mindspore/lite/tools/converter/converter.cc index 88ecab3165e..feab3a7786a 100644 --- a/mindspore/lite/tools/converter/converter.cc +++ b/mindspore/lite/tools/converter/converter.cc @@ -81,7 +81,7 @@ schema::MetaGraphT *Converter::Convert(const std::unique_ptr & if (!flag->pluginsPath.empty()) { for (auto &path : flag->pluginsPath) { auto dl_loader = std::make_shared(); - auto status = dl_loader->Open(path.c_str()); + auto status = dl_loader->Open(path); if (status != RET_OK) { MS_LOG(ERROR) << "open dynamic library failed. " << path; return nullptr; diff --git a/mindspore/lite/tools/converter/parser/caffe/caffe_model_parser.cc b/mindspore/lite/tools/converter/parser/caffe/caffe_model_parser.cc index 05d3a156fb4..d5d37c9c84f 100644 --- a/mindspore/lite/tools/converter/parser/caffe/caffe_model_parser.cc +++ b/mindspore/lite/tools/converter/parser/caffe/caffe_model_parser.cc @@ -38,6 +38,7 @@ namespace mindspore::lite { namespace { namespace { constexpr size_t kConvWeightIndex = 2; +constexpr size_t kConvWeightShapeSize = 4; } // namespace bool IsSkipedLayer(const caffe::LayerParameter &layer) { if (layer.type() == "Input" || layer.type() == "Dropout" || layer.type() == "Split") { @@ -49,12 +50,12 @@ bool IsSkipedLayer(const caffe::LayerParameter &layer) { void FcSqueezeWeightBias(const caffe::LayerParameter &layer, int blob_index, std::vector *shape) { if (layer.type() == "InnerProduct") { if (blob_index == 0) { - if (shape->size() == 4 && shape->at(0) == 1 && shape->at(1) == 1) { + if (shape->size() == kConvWeightShapeSize && shape->at(0) == 1 && shape->at(1) == 1) { shape->erase(shape->begin()); shape->erase(shape->begin()); } } else if (blob_index == 1) { - if (shape->size() == 4 && shape->at(0) == 1 && shape->at(1) == 1 && shape->at(2) == 1) { + if (shape->size() == kConvWeightShapeSize && shape->at(0) == 1 && shape->at(1) == 1 && shape->at(2) == 1) { shape->erase(shape->begin()); shape->erase(shape->begin()); shape->erase(shape->begin());