• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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__GENERAL_BITPACKING_H
18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER__GENERAL_BITPACKING_H
19 #include <stdint.h>
20 #include <stack>
21 #include <queue>
22 #include <vector>
23 #include <cassert>
24 
25 namespace mindspore::lite {
26 class BitPack {
27  public:
28   ~BitPack() = default;
29 
30   template <typename T1, typename T2>
BitPacking(int bit_num,const std::vector<T1> & origin_data_vec,std::vector<T2> * packed_data_vec)31   static void BitPacking(int bit_num, const std::vector<T1> &origin_data_vec, std::vector<T2> *packed_data_vec) {
32     MS_ASSERT(packed_data_vec != nullptr);
33     std::stack<bool> bit_data_vec;
34     for (size_t i = 0; i < origin_data_vec.size(); i++) {
35       T2 tmp = origin_data_vec[i] + static_cast<T2>(pow(2, bit_num - 1));
36       DoBinary<T2>(bit_num, tmp, &bit_data_vec, packed_data_vec);
37     }
38     size_t remain_bit_data = bit_data_vec.size();
39     if (sizeof(T1) * 8 > remain_bit_data && remain_bit_data > 0) {
40       for (size_t i = 0; i < sizeof(T1) * 8 - remain_bit_data; i++) {
41         bit_data_vec.push(false);
42       }
43       PackFromOriginToUint<T2>(&bit_data_vec, packed_data_vec);
44     }
45   }
46 
47  private:
48   template <typename T2>
PackFromOriginToUint(std::stack<bool> * ans,std::vector<T2> * packed_data_vec)49   static void PackFromOriginToUint(std::stack<bool> *ans, std::vector<T2> *packed_data_vec) {
50     MS_ASSERT(ans != nullptr);
51     uint32_t result = 0;
52     for (size_t i = 0; i < sizeof(T2) * 8; i++) {
53       bool bit_tmp = ans->top();
54       result = (result << 1) + static_cast<int>(bit_tmp);
55       ans->pop();
56     }
57     packed_data_vec->push_back(result);
58   }
59 
60   template <typename T2>
DoBinary(int bin_num,T2 n,std::stack<bool> * ans,std::vector<T2> * packed_data_vec)61   static void DoBinary(int bin_num, T2 n, std::stack<bool> *ans, std::vector<T2> *packed_data_vec) {
62     MS_ASSERT(ans != nullptr);
63     for (int bit_count = 0; bit_count < bin_num; bit_count++) {
64       bool a = n % 2;
65       n = n / 2;
66       ans->push(a);
67       if (ans->size() == sizeof(T2) * 8) {
68         PackFromOriginToUint(ans, packed_data_vec);
69       }
70     }
71   }
72 };
73 }  // namespace mindspore::lite
74 #endif
75