|
|
|
@ -16,11 +16,13 @@
|
|
|
|
|
|
|
|
|
|
#include "tools/common/graph_util.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <set>
|
|
|
|
|
#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<int8_t> 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<uint8_t> pack_data{};
|
|
|
|
|
BitPack::BitPacking<int8_t, uint8_t>(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<size_t>());
|
|
|
|
|
std::vector<int16_t> 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<uint16_t> pack_data{};
|
|
|
|
|
BitPack::BitPacking<int16_t, uint16_t>(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<CNodeT> toAddNode, STATUS *errorCode, int *insert_num,
|
|
|
|
|
const OpDefCopyer &opDefCopyer) {
|
|
|
|
|