1 /** 2 * Copyright 2020-2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_BITPACKING_H_ 18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_BITPACKING_H_ 19 #include <cmath> 20 #include <cstdint> 21 #include <stack> 22 #include <queue> 23 #include <vector> 24 #include <cassert> 25 #include "tools/converter/quantizer/quant_params.h" 26 27 using mindspore::lite::quant::k8Bit; 28 namespace mindspore::lite { 29 class BitPack { 30 public: 31 ~BitPack() = default; 32 33 template <typename T1, typename T2> BitPacking(int bit_num,const std::vector<T1> & origin_data_vec,std::vector<T2> * packed_data_vec)34 static void BitPacking(int bit_num, const std::vector<T1> &origin_data_vec, std::vector<T2> *packed_data_vec) { 35 MS_ASSERT(packed_data_vec != nullptr); 36 std::stack<bool> bit_data_vec; 37 for (size_t i = 0; i < origin_data_vec.size(); i++) { 38 T2 tmp = origin_data_vec[i] + static_cast<T2>(pow(2, bit_num - 1)); 39 DoBinary<T2>(bit_num, tmp, &bit_data_vec, packed_data_vec); 40 } 41 size_t remain_bit_data = bit_data_vec.size(); 42 if (sizeof(T1) * k8Bit > remain_bit_data && remain_bit_data > 0) { 43 for (size_t i = 0; i < sizeof(T1) * k8Bit - remain_bit_data; i++) { 44 bit_data_vec.push(false); 45 } 46 PackFromOriginToUint<T2>(&bit_data_vec, packed_data_vec); 47 } 48 } 49 50 private: 51 template <typename T2> PackFromOriginToUint(std::stack<bool> * ans,std::vector<T2> * packed_data_vec)52 static void PackFromOriginToUint(std::stack<bool> *ans, std::vector<T2> *packed_data_vec) { 53 if (ans == nullptr || packed_data_vec == nullptr) { 54 MS_LOG(ERROR) << "The pointer is nullptr."; 55 return; 56 } 57 uint32_t result = 0; 58 for (size_t i = 0; i < sizeof(T2) * k8Bit; i++) { 59 bool bit_tmp = ans->top(); 60 result = (result << 1) + static_cast<size_t>(bit_tmp); 61 ans->pop(); 62 } 63 packed_data_vec->push_back(result); 64 } 65 66 template <typename T2> DoBinary(int bin_num,T2 n,std::stack<bool> * ans,std::vector<T2> * packed_data_vec)67 static void DoBinary(int bin_num, T2 n, std::stack<bool> *ans, std::vector<T2> *packed_data_vec) { 68 if (ans == nullptr || packed_data_vec == nullptr) { 69 MS_LOG(ERROR) << "The pointer is nullptr."; 70 return; 71 } 72 for (int bit_count = 0; bit_count < bin_num; bit_count++) { 73 bool a = n % 2; 74 n = n / 2; 75 ans->push(a); 76 if (ans->size() == sizeof(T2) * k8Bit) { 77 PackFromOriginToUint(ans, packed_data_vec); 78 } 79 } 80 } 81 }; 82 } // namespace mindspore::lite 83 #endif 84