!8125 [MSLITE]mix bit pack and unpack
Merge pull request !8125 from ghzl/mix-bit-pack-and-unpackpull/8125/MERGE
commit
9597f73fd8
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* 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_TOOLS_CONVERTER_QUANTIZER__GENERAL_BITPACKING_H
|
||||||
|
#define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER__GENERAL_BITPACKING_H
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stack>
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace lite {
|
||||||
|
class BitPack {
|
||||||
|
public:
|
||||||
|
~BitPack() = default;
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
static void BitPacking(int bit_num, const std::vector<T1> &origin_data_vec, std::vector<T2> *packed_data_vec) {
|
||||||
|
std::stack<bool> bit_data_vec;
|
||||||
|
for (size_t i = 0; i < origin_data_vec.size(); i++) {
|
||||||
|
T2 tmp = origin_data_vec[i] + static_cast<T2>(pow(2, bit_num - 1));
|
||||||
|
DoBinary<T2>(bit_num, tmp, &bit_data_vec, packed_data_vec);
|
||||||
|
}
|
||||||
|
size_t remain_bit_data = bit_data_vec.size();
|
||||||
|
if (sizeof(T1) * 8 > remain_bit_data && remain_bit_data > 0) {
|
||||||
|
for (size_t i = 0; i < sizeof(T1) * 8 - remain_bit_data; i++) {
|
||||||
|
bit_data_vec.push(0);
|
||||||
|
}
|
||||||
|
PackFromOriginToUint<T2>(&bit_data_vec, packed_data_vec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename T2>
|
||||||
|
static void PackFromOriginToUint(std::stack<bool> *ans, std::vector<T2> *packed_data_vec) {
|
||||||
|
uint32_t result = 0;
|
||||||
|
for (size_t i = 0; i < sizeof(T2) * 8; i++) {
|
||||||
|
bool bit_tmp = ans->top();
|
||||||
|
result = (result << 1) + static_cast<int>(bit_tmp);
|
||||||
|
ans->pop();
|
||||||
|
}
|
||||||
|
packed_data_vec->push_back(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T2>
|
||||||
|
static void DoBinary(int bin_num, T2 n, std::stack<bool> *ans, std::vector<T2> *packed_data_vec) {
|
||||||
|
for (int bit_count = 0; bit_count < bin_num; bit_count++) {
|
||||||
|
bool a = n % 2;
|
||||||
|
n = n / 2;
|
||||||
|
ans->push(a);
|
||||||
|
if (ans->size() == sizeof(T2) * 8) {
|
||||||
|
PackFromOriginToUint(ans, packed_data_vec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace lite
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif
|
@ -1,84 +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 "tools/converter/quantizer/general_bitpacking.h"
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace lite {
|
|
||||||
BitPack::BitPack(const uint8_t &bitnum) { this->bitnum = bitnum; }
|
|
||||||
void BitPack::UnPackFromUint8ToOrigin(uint8_t &n, std::queue<bool> &unpackBitData) {
|
|
||||||
int bitCount = 0;
|
|
||||||
while (bitCount < 8) {
|
|
||||||
bool a = n % 2;
|
|
||||||
n = n >> 1;
|
|
||||||
bitCount++;
|
|
||||||
unpackBitData.push(a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void BitPack::UnPack(uint8_t bitnum, uint8_t &packedData, std::vector<uint8_t> &originData,
|
|
||||||
std::queue<bool> &unpackBitData) {
|
|
||||||
UnPackFromUint8ToOrigin(packedData, unpackBitData);
|
|
||||||
// std::queue<bool> unpackBitTmpData;
|
|
||||||
|
|
||||||
while (unpackBitData.size() > bitnum) {
|
|
||||||
uint32_t result = 0;
|
|
||||||
for (int k = 0; k < bitnum; k++) {
|
|
||||||
bool bitTmp = unpackBitData.front();
|
|
||||||
result = (result << 1) + static_cast<int>(bitTmp);
|
|
||||||
unpackBitData.pop();
|
|
||||||
}
|
|
||||||
originData.push_back(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void BitPack::PackFromOriginToUint8(std::stack<bool> &ans, std::vector<uint8_t> &packedDataVec) {
|
|
||||||
uint32_t result = 0;
|
|
||||||
for (size_t i = 0; i < 8; i++) {
|
|
||||||
bool bit_tmp = ans.top();
|
|
||||||
result = (result << 1) + static_cast<int>(bit_tmp);
|
|
||||||
ans.pop();
|
|
||||||
}
|
|
||||||
packedDataVec.push_back(result);
|
|
||||||
}
|
|
||||||
void BitPack::DoBinary(uint8_t &n, std::stack<bool> &ans, std::vector<uint8_t> &packedDataVec) {
|
|
||||||
int bitCount = 0;
|
|
||||||
while (bitCount < bitnum) {
|
|
||||||
bool a = n / (1 << (unsigned int)(bitnum - bitCount - 1));
|
|
||||||
n = n - a * (1 << (unsigned int)(bitnum - bitCount - 1));
|
|
||||||
bitCount++;
|
|
||||||
ans.push(a);
|
|
||||||
if (ans.size() == 8) {
|
|
||||||
PackFromOriginToUint8(ans, packedDataVec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BitPack::BitPacking(const std::vector<uint8_t> &originDataVec, std::vector<uint8_t> &packedDataVec) {
|
|
||||||
std::stack<bool> bitDataVec;
|
|
||||||
for (size_t i = 0; i < originDataVec.size(); i++) {
|
|
||||||
uint8_t tmp = originDataVec[i];
|
|
||||||
DoBinary(tmp, bitDataVec, packedDataVec);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t remainBitData = bitDataVec.size();
|
|
||||||
if (8 > remainBitData && remainBitData > 0) {
|
|
||||||
for (size_t i = 0; i < 8 - remainBitData; i++) {
|
|
||||||
bitDataVec.push(0);
|
|
||||||
}
|
|
||||||
PackFromOriginToUint8(bitDataVec, packedDataVec);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace lite
|
|
||||||
} // namespace mindspore
|
|
@ -1,43 +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_TOOLS_CONVERTER_QUANTIZER__GENERAL_BITPACKING_H
|
|
||||||
#define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER__GENERAL_BITPACKING_H
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stack>
|
|
||||||
#include <queue>
|
|
||||||
#include <vector>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
namespace mindspore {
|
|
||||||
namespace lite {
|
|
||||||
class BitPack {
|
|
||||||
public:
|
|
||||||
explicit BitPack(const uint8_t &bitbum = 8);
|
|
||||||
~BitPack() = default;
|
|
||||||
void BitPacking(const std::vector<uint8_t> &originDataVec, std::vector<uint8_t> &packedDataVec);
|
|
||||||
void UnPack(uint8_t bitnum, uint8_t &packedData, std::vector<uint8_t> &originData, std::queue<bool> &unpackBitData);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void UnPackFromUint8ToOrigin(uint8_t &n, std::queue<bool> &unpackBitData);
|
|
||||||
void PackFromOriginToUint8(std::stack<bool> &ans, std::vector<uint8_t> &packedDataVec);
|
|
||||||
void DoBinary(uint8_t &n, std::stack<bool> &ans, std::vector<uint8_t> &packed_data_vec);
|
|
||||||
uint8_t bitnum;
|
|
||||||
};
|
|
||||||
} // namespace lite
|
|
||||||
} // namespace mindspore
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in new issue