• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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_HUFFMANCODE_HUFFMAN_H
18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_HUFFMANCODE_HUFFMAN_H
19 
20 #include <cstdlib>
21 #include <cstring>
22 #include <string>
23 #include <vector>
24 #include <queue>
25 #include <map>
26 #include <memory>
27 #include "ir/func_graph.h"
28 #include "ir/primitive.h"
29 #include "schema/inner/model_generated.h"
30 #include "securec/include/securec.h"
31 #include "src/common/log_adapter.h"
32 
33 namespace mindspore {
34 namespace lite {
35 
36 using STATUS = int;
37 
38 const int PSEUDO_EOF = 128;
39 
40 struct HuffmanNode {
41   int key;
42   unsigned int freq;
43   std::string code;
44   HuffmanNode *left, *right, *parent;
45 };
46 using HuffmanNodePtr = HuffmanNode *;
47 
48 struct cmp {
49  public:
operatorcmp50   bool operator()(const HuffmanNodePtr &c1, const HuffmanNodePtr &c2) const { return c1->freq > c2->freq; }
51 };
52 using HuffmanPriorityQueue = std::priority_queue<HuffmanNodePtr, std::vector<HuffmanNodePtr>, cmp>;
53 
54 class HuffmanEncode {
55  public:
56   HuffmanEncode() = default;
57 
58   ~HuffmanEncode();
59 
60   STATUS DoHuffmanEncode(const tensor::TensorPtr &weight, const PrimitivePtr &primitive, void *quant_datas,
61                          const size_t &bit_num);
62 
63  private:
64   std::map<int, std::string> huffman_table_;
65   std::string huffman_encoded_str_;
66   std::vector<HuffmanNodePtr> huffman_nodes_;
67 
68   STATUS GetHuffmanPriorityQueue(const int8_t *input_datas, size_t input_data_size, HuffmanPriorityQueue *pq);
69 
70   void GenerateHuffmanTable(HuffmanNodePtr node, bool is_left_node);
71 
72   STATUS BuildHuffmanTree(HuffmanPriorityQueue *pq);
73 
74   STATUS DoHuffmanCompress(const int8_t *input_datas, size_t data_size);
75 };
76 
77 }  // namespace lite
78 }  // namespace mindspore
79 
80 #endif  // MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_HUFFMANCODE_HUFFMAN_H
81