From f1637f11570470c8fc9895c005a6d739c4b6622a Mon Sep 17 00:00:00 2001 From: hangq Date: Fri, 4 Sep 2020 14:32:16 +0800 Subject: [PATCH] 1. clean review bot 2. fix nan bug --- mindspore/lite/java/build_aar.sh | 6 ++--- mindspore/lite/nnacl/fp16/pack_fp16.c | 8 ++++-- mindspore/lite/nnacl/pack.c | 8 ++++-- .../lite/tools/converter/converter_flags.cc | 4 +-- .../legacy_optimizer/graph/infershape_pass.cc | 25 ++++++++----------- .../parser/tflite/tflite_custom_parser.cc | 15 +++++------ .../tools/converter/quantizer/quantize_util.h | 7 ++---- .../converter/quantizer/weight_quantizer.cc | 1 - .../lite/tools/optimizer/common/gllo_utils.cc | 16 ++++++------ 9 files changed, 43 insertions(+), 47 deletions(-) diff --git a/mindspore/lite/java/build_aar.sh b/mindspore/lite/java/build_aar.sh index af3d1c3ca..bead3d840 100644 --- a/mindspore/lite/java/build_aar.sh +++ b/mindspore/lite/java/build_aar.sh @@ -5,9 +5,9 @@ BASE_PATH=$(cd "$(dirname $0)"; pwd) TOP_PATH="${BASE_PATH}/../../.." get_version() { - VERSION_MAJOR=`grep "#define MS_VERSION_MAJOR" ../../include/version.h | tr -dc "[0-9]"` - VERSION_MINOR=`grep "#define MS_VERSION_MINOR" ../../include/version.h | tr -dc "[0-9]"` - VERSION_REVISION=`grep "#define MS_VERSION_REVISION" ../../include/version.h | tr -dc "[0-9]"` + VERSION_MAJOR=`grep "#define MS_VERSION_MAJOR" ../include/version.h | tr -dc "[0-9]"` + VERSION_MINOR=`grep "#define MS_VERSION_MINOR" ../include/version.h | tr -dc "[0-9]"` + VERSION_REVISION=`grep "#define MS_VERSION_REVISION" ../include/version.h | tr -dc "[0-9]"` VERSION_STR=${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION} } diff --git a/mindspore/lite/nnacl/fp16/pack_fp16.c b/mindspore/lite/nnacl/fp16/pack_fp16.c index 4ae77f72a..12e824dc4 100644 --- a/mindspore/lite/nnacl/fp16/pack_fp16.c +++ b/mindspore/lite/nnacl/fp16/pack_fp16.c @@ -232,6 +232,7 @@ void PackNCHWToNHWCFp16(const void *src, void *dst, int batch, int plane, int ch void PackNHWCToNHWC4Fp16(const void *src, void *dst, int batch, int plane, int channel) { int ic4 = UP_DIV(channel, C4NUM); + int c4_channel = ic4 * C4NUM; int nhwc4_batch_unit_offset = ic4 * C4NUM * plane; int ic_remainder_ = channel % C4NUM; if (ic_remainder_ != 0) { @@ -239,8 +240,11 @@ void PackNHWCToNHWC4Fp16(const void *src, void *dst, int batch, int plane, int c for (int b = 0; b < batch; b++) { int batch_offset = b * channel * plane; for (int i = 0; i < plane; i++) { - memcpy((float16_t *)dst + nhwc4_batch_offset + i * ic4 * C4NUM, (float16_t *)src + batch_offset + i * channel, - channel * sizeof(float16_t)); + float16_t *dst_per_plane = (float16_t *)dst + nhwc4_batch_offset + i * c4_channel; + memcpy(dst_per_plane, (float16_t *)src + batch_offset + i * channel, channel * sizeof(float16_t)); + for (int j = channel; j < c4_channel; ++j) { + dst_per_plane[j] = 0; + } } nhwc4_batch_offset += nhwc4_batch_unit_offset; } diff --git a/mindspore/lite/nnacl/pack.c b/mindspore/lite/nnacl/pack.c index 4a94118da..d2568147c 100644 --- a/mindspore/lite/nnacl/pack.c +++ b/mindspore/lite/nnacl/pack.c @@ -611,6 +611,7 @@ void PackNCHWToNC4HW4Fp32(const void *src, void *dst, int batch, int plane, int void PackNHWCToNHWC4Fp32(const void *src, void *dst, int batch, int plane, int channel) { int c4 = UP_DIV(channel, C4NUM); + int c4_channel = c4 * C4NUM; int nhwc4_batch_unit_offset = c4 * C4NUM * plane; int ic_remainder_ = channel % C4NUM; if (ic_remainder_ != 0) { @@ -618,8 +619,11 @@ void PackNHWCToNHWC4Fp32(const void *src, void *dst, int batch, int plane, int c for (int b = 0; b < batch; b++) { int batch_offset = b * channel * plane; for (int i = 0; i < plane; i++) { - memcpy((float *)dst + nhwc4_batch_offset + i * c4 * C4NUM, (float *)src + batch_offset + i * channel, - channel * sizeof(float)); + float *dst_per_plane = (float *)dst + nhwc4_batch_offset + i * c4_channel; + memcpy(dst_per_plane, (float *)src + batch_offset + i * channel, channel * sizeof(float)); + for (int j = channel; j < c4_channel; ++j) { + dst_per_plane[j] = 0; + } } nhwc4_batch_offset += nhwc4_batch_unit_offset; } diff --git a/mindspore/lite/tools/converter/converter_flags.cc b/mindspore/lite/tools/converter/converter_flags.cc index c2ddb9bbc..0afb39fda 100644 --- a/mindspore/lite/tools/converter/converter_flags.cc +++ b/mindspore/lite/tools/converter/converter_flags.cc @@ -37,8 +37,8 @@ Flags::Flags() { AddFlag(&Flags::mean, "mean", "Mean value for aware-quantization", "-0.5"); AddFlag(&Flags::bitNum, "bitNum", "Weight quantization bitNum", "8"); AddFlag(&Flags::quantSize, "quantSize", "Weight quantization size threshold", "0"); - AddFlag(&Flags::convWeightQuantChannelThreshold, "convWeightQuantChannelThreshold", - "convWeightQuantChannelThreshold", "16"); + AddFlag(&Flags::convWeightQuantChannelThreshold, "convWeightQuantChannelThreshold", "convWeightQuantChannelThreshold", + "16"); AddFlag(&Flags::configFile, "config_file", "Configuration for post-training.", ""); AddFlag(&Flags::formatTrans, "formatTrans", "whether transform format. true | false", "true"); } diff --git a/mindspore/lite/tools/converter/legacy_optimizer/graph/infershape_pass.cc b/mindspore/lite/tools/converter/legacy_optimizer/graph/infershape_pass.cc index 90daca519..a2f8f2f85 100644 --- a/mindspore/lite/tools/converter/legacy_optimizer/graph/infershape_pass.cc +++ b/mindspore/lite/tools/converter/legacy_optimizer/graph/infershape_pass.cc @@ -21,8 +21,8 @@ #include "src/ir/tensor.h" #include "src/ops/primitive_c.h" -using mindspore::lite::tensor::Tensor; using mindspore::lite::PrimitiveC; +using mindspore::lite::tensor::Tensor; namespace mindspore { namespace lite { namespace { @@ -33,7 +33,7 @@ std::vector ConvertTensorToLiteTensor(MetaGraphT *graph, const auto &tensorT = graph->allTensors.at(tensor_indexs[i]); auto tensor_shape = tensorT->dims; auto lite_tensor = - new(std::nothrow) tensor::Tensor(TypeId(tensorT->dataType), tensor_shape, tensorT->format, tensorT->nodeType); + std::make_unique(TypeId(tensorT->dataType), tensor_shape, tensorT->format, tensorT->nodeType); if (lite_tensor == nullptr) { MS_LOG(ERROR) << "lite tensor is nullptr"; return std::vector(); @@ -43,27 +43,23 @@ std::vector ConvertTensorToLiteTensor(MetaGraphT *graph, const auto lite_tensor_size = tensorT->data.size() * sizeof(uint8_t); // when tensorT as param input if (lite_tensor_size == 0) { - delete lite_tensor; return std::vector(); } - auto tensor_data = new(std::nothrow) char[lite_tensor_size / sizeof(char)]; + auto tensor_data = std::unique_ptr(new (std::nothrow) char[lite_tensor_size / sizeof(char)]); if (tensor_data == nullptr) { MS_LOG(ERROR) << "tensor_data is nullptr"; - delete lite_tensor; return std::vector(); } - auto ret = memcpy_s(tensor_data, lite_tensor_size, tensorT->data.data(), lite_tensor_size); + auto ret = memcpy_s(tensor_data.get(), lite_tensor_size, tensorT->data.data(), lite_tensor_size); if (ret != EOK) { - delete lite_tensor; - delete[] tensor_data; MS_LOG(ERROR) << "memcpy error: " << ret; return std::vector(); } - lite_tensor->SetData(tensor_data); - lite_tensors.emplace_back(lite_tensor); + lite_tensor->SetData(tensor_data.release()); + lite_tensors.emplace_back(lite_tensor.release()); continue; } - lite_tensors.emplace_back(lite_tensor); + lite_tensors.emplace_back(lite_tensor.release()); } return lite_tensors; } @@ -95,17 +91,16 @@ STATUS InferShapePass::Run(MetaGraphT *graph) { auto ret = primitiveC->InferShape(input_tensors, output_tensors); if (ret == RET_INFER_INVALID) { MS_LOG(INFO) << "InferShape shouldn't be done before runtime, name: " << node->name - << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type) - << "flag set to false."; + << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type) << "flag set to false."; } else if (ret != RET_OK) { MS_LOG(WARNING) << "InferShape failed, name: " << node->name - << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type); + << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type); return RET_INFER_ERR; } // copy output shape to tensorT for (size_t i = 0; i < output_tensors.size(); i++) { auto output_dims = output_tensors[i]->shape(); - auto &output_tensor = graph->allTensors.at(node->outputIndex[i]); + auto &output_tensor = graph->allTensors.at(node->outputIndex[i]); output_tensor->dims.swap(output_dims); output_tensor->format = output_tensors[i]->GetFormat(); output_tensor->dataType = output_tensors[i]->data_type(); diff --git a/mindspore/lite/tools/converter/parser/tflite/tflite_custom_parser.cc b/mindspore/lite/tools/converter/parser/tflite/tflite_custom_parser.cc index 1ee985cf0..2d0598de7 100644 --- a/mindspore/lite/tools/converter/parser/tflite/tflite_custom_parser.cc +++ b/mindspore/lite/tools/converter/parser/tflite/tflite_custom_parser.cc @@ -26,10 +26,8 @@ namespace lite { STATUS TfliteCustomParser::Parse(const std::unique_ptr &tflite_op, const std::vector> &tflite_tensors, const std::vector> &tflite_model_buffer, - schema::CNodeT *op, - std::vector *tensors_id, - std::vector *tensors_format, - std::map *tensors_id_map) { + schema::CNodeT *op, std::vector *tensors_id, + std::vector *tensors_format, std::map *tensors_id_map) { MS_LOG(DEBUG) << "parse TfliteCustomParser"; if (op == nullptr) { MS_LOG(ERROR) << "op is null"; @@ -80,12 +78,12 @@ STATUS TfliteCustomParser::Parse(const std::unique_ptr &tflit op->primitive->value.value = attr.release(); for (size_t i = 0; i < tflite_op->inputs.size(); ++i) { - AddOpInput(op, tensors_id, tensors_format, tensors_id_map, - tflite_op->inputs[i], tensors_id->size(), tflite_tensors.size(), schema::Format_NHWC); + AddOpInput(op, tensors_id, tensors_format, tensors_id_map, tflite_op->inputs[i], tensors_id->size(), + tflite_tensors.size(), schema::Format_NHWC); } for (size_t i = 0; i < tflite_op->outputs.size(); ++i) { - AddOpOutput(op, tensors_id, tensors_format, tensors_id_map, - tflite_op->outputs[i], tensors_id->size(), tflite_tensors.size(), schema::Format_NHWC); + AddOpOutput(op, tensors_id, tensors_format, tensors_id_map, tflite_op->outputs[i], tensors_id->size(), + tflite_tensors.size(), schema::Format_NHWC); } return RET_OK; } @@ -93,4 +91,3 @@ STATUS TfliteCustomParser::Parse(const std::unique_ptr &tflit TfliteNodeRegister g_tfliteCustomParser("Custom", new TfliteCustomParser()); } // namespace lite } // namespace mindspore - diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.h b/mindspore/lite/tools/converter/quantizer/quantize_util.h index d5e1dd5ee..e64e40bb9 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.h +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.h @@ -190,8 +190,7 @@ STATUS QuantFilter(ParamValueLitePtr weight, std::shared_ptr primiti quant_datas[index] = quant_data; } } - auto ret = memcpy_s(raw_datas, weight->tensor_size(), quant_datas.data(), - elem_count * sizeof(T)); + auto ret = memcpy_s(raw_datas, weight->tensor_size(), quant_datas.data(), elem_count * sizeof(T)); if (ret != EOK) { MS_LOG(ERROR) << "memcpy error: " << ret; return RET_ERROR; @@ -238,15 +237,13 @@ STATUS QuantFilter(ParamValueLitePtr weight, std::shared_ptr primiti quant_datas[index] = quant_data; } } - auto ret = - memcpy_s(raw_datas, weight->tensor_size(), quant_datas.data(), elem_count * sizeof(int8_t)); + auto ret = memcpy_s(raw_datas, weight->tensor_size(), quant_datas.data(), elem_count * sizeof(int8_t)); if (ret != EOK) { MS_LOG(ERROR) << "memcpy error: " << ret; return RET_ERROR; } weight->set_tensor_size(elem_count * sizeof(T)); } - } else { // per layer float min = FLT_MAX; diff --git a/mindspore/lite/tools/converter/quantizer/weight_quantizer.cc b/mindspore/lite/tools/converter/quantizer/weight_quantizer.cc index ac06dbf9a..e5895008f 100644 --- a/mindspore/lite/tools/converter/quantizer/weight_quantizer.cc +++ b/mindspore/lite/tools/converter/quantizer/weight_quantizer.cc @@ -27,7 +27,6 @@ using std::vector; namespace mindspore { namespace lite { namespace quant { - WeightQuantizer::WeightQuantizer(FuncGraphPtr graph, const string &weightSize, const std::string &convWeightChannelThreshold, const std::string &bitNum) : Quantizer(graph) { diff --git a/mindspore/lite/tools/optimizer/common/gllo_utils.cc b/mindspore/lite/tools/optimizer/common/gllo_utils.cc index db1d538d0..706fb9ebb 100644 --- a/mindspore/lite/tools/optimizer/common/gllo_utils.cc +++ b/mindspore/lite/tools/optimizer/common/gllo_utils.cc @@ -48,11 +48,11 @@ bool IsRealKernel(const AnfNodePtr &node) { } auto input = cnode->inputs()[0]; bool is_virtual_node = IsPrimitive(input, prim::kPrimImageSummary) || IsPrimitive(input, prim::kPrimScalarSummary) || - IsPrimitive(input, prim::kPrimTensorSummary) || - IsPrimitive(input, prim::kPrimHistogramSummary) || IsPrimitive(input, prim::kPrimMakeTuple) || - IsPrimitive(input, prim::kPrimStateSetItem) || IsPrimitive(input, prim::kPrimDepend) || - IsPrimitive(input, prim::kPrimTupleGetItem) || IsPrimitive(input, prim::kPrimControlDepend) || - IsPrimitive(input, prim::kPrimReturn) || IsPrimitive(input, prim::kPrimPartial); + IsPrimitive(input, prim::kPrimTensorSummary) || + IsPrimitive(input, prim::kPrimHistogramSummary) || IsPrimitive(input, prim::kPrimMakeTuple) || + IsPrimitive(input, prim::kPrimStateSetItem) || IsPrimitive(input, prim::kPrimDepend) || + IsPrimitive(input, prim::kPrimTupleGetItem) || IsPrimitive(input, prim::kPrimControlDepend) || + IsPrimitive(input, prim::kPrimReturn) || IsPrimitive(input, prim::kPrimPartial); return !is_virtual_node; } @@ -159,8 +159,8 @@ bool AnfEqual(const BaseRef &a, const BaseRef &b) { } if (utils::isa(a_value_ptr) && utils::isa(b_value_ptr)) { - auto a_obj = (lite::PrimitiveC *) (a_value_ptr.get()); - auto b_obj = (lite::PrimitiveC *) (b_value_ptr.get()); + auto a_obj = (lite::PrimitiveC *)(a_value_ptr.get()); + auto b_obj = (lite::PrimitiveC *)(b_value_ptr.get()); return (*a_obj) == (*b_obj); } else { return (*a_value_ptr) == (*b_value_ptr); @@ -319,7 +319,7 @@ schema::PrimitiveType GetCNodeType(const BaseRef &n) { if (utils::isa(value)) { auto primitive = value->cast(); MS_ASSERT(primitive != nullptr); - return (schema::PrimitiveType) primitive->Type(); + return (schema::PrimitiveType)primitive->Type(); } else if (utils::isa(value)) { auto primitive = value->cast(); MS_ASSERT(primitive != nullptr); -- GitLab