From b195abad5a991461c13e6408fd107a49cc0b6364 Mon Sep 17 00:00:00 2001 From: jianghui58 Date: Fri, 2 Apr 2021 11:46:54 +0800 Subject: [PATCH] fix bitpack judgment bug --- .../lite/tools/anf_exporter/anf_exporter.cc | 39 +---------------- .../lite/tools/anf_exporter/anf_exporter.h | 1 - mindspore/lite/tools/common/graph_util.cc | 43 +++++++++++++++++++ mindspore/lite/tools/common/graph_util.h | 2 + 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/mindspore/lite/tools/anf_exporter/anf_exporter.cc b/mindspore/lite/tools/anf_exporter/anf_exporter.cc index 63db830bd6..ab02d82c47 100644 --- a/mindspore/lite/tools/anf_exporter/anf_exporter.cc +++ b/mindspore/lite/tools/anf_exporter/anf_exporter.cc @@ -235,43 +235,6 @@ void AnfExporter::RemoveIfDepend(const CNodePtr &cnode) { } } -int AnfExporter::DoBitPack(const int &bit_num, schema::TensorT *tensor_input) { - if (bit_num > 0 && bit_num < 8) { - std::vector origin_data(tensor_input->data.size()); - if (memcpy_s(origin_data.data(), origin_data.size() * sizeof(int8_t), tensor_input->data.data(), - tensor_input->data.size() * sizeof(uint8_t)) != EOK) { - MS_LOG(ERROR) << "memcpy failed."; - return RET_ERROR; - } - std::vector pack_data{}; - BitPack::BitPacking(bit_num, origin_data, &pack_data); - tensor_input->data.resize(pack_data.size() * sizeof(uint8_t)); - if (memcpy_s(tensor_input->data.data(), tensor_input->data.size() * sizeof(uint8_t), pack_data.data(), - pack_data.size() * sizeof(uint8_t)) != EOK) { - MS_LOG(ERROR) << "memcpy_s failed."; - return RET_ERROR; - } - } else if (bit_num > 9 && bit_num < 16) { - auto shape_size = - std::accumulate(tensor_input->dims.begin(), tensor_input->dims.end(), size_t(1), std::multiplies()); - std::vector origin_data(shape_size); - if (memcpy_s(origin_data.data(), origin_data.size() * sizeof(int16_t), tensor_input->data.data(), - tensor_input->data.size() * sizeof(uint8_t)) != EOK) { - MS_LOG(ERROR) << "memcpy failed."; - return RET_ERROR; - } - std::vector pack_data{}; - BitPack::BitPacking(bit_num, origin_data, &pack_data); - tensor_input->data.resize(pack_data.size() * sizeof(uint16_t)); - if (memcpy_s(tensor_input->data.data(), tensor_input->data.size() * sizeof(uint8_t), pack_data.data(), - pack_data.size() * sizeof(uint16_t)) != EOK) { - MS_LOG(ERROR) << "memcpy_s failed."; - return RET_ERROR; - } - } - return RET_OK; -} - int AnfExporter::SetQuantOutputTensorType(const std::unique_ptr &meta_graph, const std::shared_ptr &primitive, const std::unique_ptr &dst_node) { @@ -341,7 +304,7 @@ int AnfExporter::ConvertQuantParam(const std::unique_ptr &me if (bit_num != 8 && bit_num != 16) { auto status = DoBitPack(bit_num, tensor_input); if (status != RET_OK) { - MS_LOG(ERROR) << "do bit pack failed."; + MS_LOG(ERROR) << "do bit pack failed. " << status; return RET_ERROR; } } diff --git a/mindspore/lite/tools/anf_exporter/anf_exporter.h b/mindspore/lite/tools/anf_exporter/anf_exporter.h index 461e5d2ebf..00b21891b0 100644 --- a/mindspore/lite/tools/anf_exporter/anf_exporter.h +++ b/mindspore/lite/tools/anf_exporter/anf_exporter.h @@ -75,7 +75,6 @@ class AnfExporter { int SetGraphInputIndex(const std::unique_ptr &meta_graphT, const size_t &subgraph_index); int SetGraphoutputIndex(const CNodePtr &cnode, size_t subgraph_index, const std::unique_ptr &meta_graphT, schema::CNodeT *return_node); - static int DoBitPack(const int &bit_num, schema::TensorT *tensor_input); static int SetQuantOutputTensorType(const std::unique_ptr &meta_graph, const std::shared_ptr &primitive, const std::unique_ptr &dst_node); diff --git a/mindspore/lite/tools/common/graph_util.cc b/mindspore/lite/tools/common/graph_util.cc index a99568c60c..d537c575b1 100644 --- a/mindspore/lite/tools/common/graph_util.cc +++ b/mindspore/lite/tools/common/graph_util.cc @@ -16,11 +16,13 @@ #include "tools/common/graph_util.h" #include +#include #include #include #include #include "schema/inner/model_generated.h" #include "tools/common/tensor_util.h" +#include "tools/converter/quantizer/bitpacking.h" #include "tools/common/node_util.h" #include "src/common/log_adapter.h" #include "src/common/utils.h" @@ -389,6 +391,47 @@ STATUS ReplaceTensorOfNode(schema::MetaGraphT *graphT, uint32_t nodeIdx, uint32_ return RET_OK; } +int DoBitPack(const int &bit_num, schema::TensorT *tensor_input) { + if (bit_num > 0 && bit_num < 8) { + std::vector origin_data(tensor_input->data.size()); + auto status = memcpy_s(origin_data.data(), origin_data.size() * sizeof(int8_t), tensor_input->data.data(), + tensor_input->data.size() * sizeof(uint8_t)); + if (status != EOK) { + MS_LOG(ERROR) << "memcpy failed. " << status; + return RET_ERROR; + } + std::vector pack_data{}; + BitPack::BitPacking(bit_num, origin_data, &pack_data); + tensor_input->data.resize(pack_data.size() * sizeof(uint8_t)); + status = memcpy_s(tensor_input->data.data(), tensor_input->data.size() * sizeof(uint8_t), pack_data.data(), + pack_data.size() * sizeof(uint8_t)); + if (status != EOK) { + MS_LOG(ERROR) << "memcpy_s failed. " << status; + return RET_ERROR; + } + } else if (bit_num > 8 && bit_num < 16) { + auto shape_size = + std::accumulate(tensor_input->dims.begin(), tensor_input->dims.end(), size_t(1), std::multiplies()); + std::vector origin_data(shape_size); + auto status = memcpy_s(origin_data.data(), origin_data.size() * sizeof(int16_t), tensor_input->data.data(), + tensor_input->data.size() * sizeof(uint8_t)); + if (status != EOK) { + MS_LOG(ERROR) << "memcpy failed. " << status; + return RET_ERROR; + } + std::vector pack_data{}; + BitPack::BitPacking(bit_num, origin_data, &pack_data); + tensor_input->data.resize(pack_data.size() * sizeof(uint16_t)); + status = memcpy_s(tensor_input->data.data(), tensor_input->data.size() * sizeof(uint8_t), pack_data.data(), + pack_data.size() * sizeof(uint16_t)); + if (status != EOK) { + MS_LOG(ERROR) << "memcpy_s failed. " << status; + return RET_ERROR; + } + } + return RET_OK; +} + NodeIter InsertNode(schema::MetaGraphT *graphT, uint32_t existNodeIdx, InsertPlace place, size_t inoutIndex, std::unique_ptr toAddNode, STATUS *errorCode, int *insert_num, const OpDefCopyer &opDefCopyer) { diff --git a/mindspore/lite/tools/common/graph_util.h b/mindspore/lite/tools/common/graph_util.h index 13cf28d24f..9d72e4ddef 100644 --- a/mindspore/lite/tools/common/graph_util.h +++ b/mindspore/lite/tools/common/graph_util.h @@ -70,6 +70,8 @@ STATUS AddTensor2Node(schema::MetaGraphT *graphT, uint32_t nodeIdx, std::unique_ STATUS ReplaceTensorOfNode(schema::MetaGraphT *graphT, uint32_t nodeIdx, uint32_t inTensorIdx, std::unique_ptr tensor); +int DoBitPack(const int &bit_num, schema::TensorT *tensor_input); + NodeIter InsertNode(schema::MetaGraphT *graphT, uint32_t existNodeIdx, InsertPlace place, size_t inoutIndex, std::unique_ptr toAddNode, STATUS *errorCode, int *insert_num, const OpDefCopyer &opDefCopyer = GetSimpleOpCopyer());