Adaptive Huffman coding system and method
By constructing a Huffman binary tree and optimizing node data mapping, the problem of high memory bandwidth usage of adaptive Huffman coding on hardware is solved, and efficient coding storage and memory optimization are achieved, which is suitable for edge model inference.
Patent Information
- Application Number
- CN202210366617.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-08
- Publication Date
- 2025-10-03
- Estimated Expiration
- 2042-04-08
AI Technical Summary
Existing adaptive Huffman coding suffers from hardware issues such as high memory bandwidth usage, a tree-like data structure that is not conducive to memory storage, and high computational costs, making it difficult to efficiently perform lossless data compression during edge model inference.
By constructing a Huffman binary tree and using fixed address access to optimize node data mapping, configuring continuous memory to store internal node and leaf node information, dynamically updating node weights and swapping nodes, and using binary tree memory address remapping technology, memory usage is optimized and bandwidth usage is reduced.
It achieves efficient access to encoded data structures on hardware, optimizes storage space and provides memory flexibility, reduces memory bandwidth usage per unit time, and is suitable for hardware environments with memory constraints.
Smart Images

Figure CN114900193B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of artificial intelligence and data coding technology, and relates to a coding system, and in particular to an adaptive Huffman coding system and method. Background Art
[0002] Lossless data compression is a key element in efficient data storage, and Huffman coding is the most popular variable-length coding algorithm. Given a set of data symbols and their probabilities of occurrence, Huffman coding assigns shorter codes to more frequently occurring symbols, generating codewords in a way that minimizes the average code length. Due to its guaranteed optimality, Huffman coding has been widely adopted in various applications. In modern multi-stage compression designs, it is often used as a system backend to improve the compression performance of domain-specific front-ends, such as JPEG and MP3.
[0003] Adaptive Huffman coding is a dynamic coding technology based on Huffman coding. The difference is that it does not know the probability of each symbol appearing at the beginning. Instead, it dynamically collects and updates the probability of each symbol appearing as the data stream arrives. It generally uses a recursive method to traverse the data from the root to the leaf node of the Huffman tree. In hardware implementation, adaptive Huffman coding has three main disadvantages: (1) Due to the characteristics of the distribution of AI model parameters and feature map data, adaptive Huffman coding often produces a skewed tree data structure, which is not conducive to memory data storage; (2) Recursive access to data requires additional space support, which increases memory usage; (3) In hardware design, the computational cost of traversing the tree for each symbol is very high.
[0004] AI models often contain millions of parameters and feature graph data, requiring significant amounts of memory for storage and transmission during edge model inference. Feature graph operations, in particular, consume memory I / O bandwidth, leading to bandwidth shortages when multiple systems share memory.
[0005] Therefore, how to efficiently transmit data on hardware, reduce memory bandwidth usage per unit time, and maintain the same amount of data information becomes a critical issue. Adaptive Huffman coding is a real-time lossless data compression algorithm; however, due to its unique tree-like data structure, it typically uses recursive methods to access data in subtree nodes, making it difficult to implement efficiently on hardware.
[0006] In view of this, there is an urgent need to design a new encoding method to overcome at least some of the above-mentioned defects of the existing encoding method. Summary of the Invention
[0007] The present invention provides an adaptive Huffman coding system and method, which can efficiently access the coding data structure on hardware and optimize the coding storage space while providing the flexibility of configurable memory.
[0008] In order to solve the above technical problems, according to one aspect of the present invention, the following technical solution is adopted:
[0009] An adaptive Huffman coding system, the adaptive Huffman coding system comprising:
[0010] A module for acquiring data to be encoded, used for acquiring data to be encoded;
[0011] A Huffman binary tree encoding module is used to construct a Huffman binary tree based on the data to be encoded obtained by the data to be encoded acquisition module; index each node of the Huffman binary tree with the memory, and when constructing the Huffman binary tree, calculate the memory index value of the target node based on the unique path from the root node to the target node.
[0012] As an embodiment of the present invention, the Huffman binary tree encoding module includes:
[0013] Huffman binary tree initialization unit, used to initialize the Huffman binary tree;
[0014] A Huffman binary tree update unit, used to update the Huffman binary tree according to set rules;
[0015] The process of updating the Huffman binary tree by the Huffman binary tree updating unit must follow the following specifications:
[0016] (1) Nodes are numbered in ascending order; that is, node numbers increase from top to bottom and from left to right;
[0017] (2) Internal nodes are represented by circles; the weight of an internal node is the sum of the weights of its child nodes;
[0018] (3) Leaf nodes are represented by squares; the weight of a leaf node is initialized to 1. If data is repeatedly input, the weight increases by 1;
[0019] (4) During the tree update process, if the weight of the left subtree is greater than that of the right subtree, the nodes must be swapped.
[0020] As an embodiment of the present invention, the Huffman binary tree encoding module needs to dynamically update node weights and swap nodes during the construction of the Huffman binary tree; optimize the mapping of node data to hardware memory through fixed address access;
[0021] A continuous memory is configured to store all internal nodes and leaf nodes of a full binary tree at a specific depth. If storing internal nodes, the above dynamic operations can be performed by accessing the memory address, without having to access the left and right subtrees one by one through the parent node. If storing leaf nodes, the data to be encoded for the leaf node can be directly accessed and the weight can be updated.
[0022] Based on the unique path from the root node to the target node, calculate the memory index value of the target node: the path record of the left child node of the root node is 0; the path record of the right child node is 1;
[0023] Set the depth of the full binary tree to k; map the root node to memory address 1, and its left and right child nodes are defined as addresses 2 and 3, and so on, according to the full binary tree definition; obtain the memory address mapped by any target node through the left shift operator (<<), which shifts the bits of an integer or enumeration type expression to the left; according to the above definition of left and right child node indexes, for any target node with index j, the memory address index of the left child node is expressed as j<<1, and the memory address index of the right child node is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
[0024] As an embodiment of the present invention, the Huffman binary tree encoding module further includes a binary tree memory address remapping unit, which is used to give a set of continuous memory spaces by remapping the sub-binary tree memory addresses after the Huffman binary tree is constructed, and point the null pointer in the memory to the node that exceeds the memory range, so that the hardware can store the nodes in a balanced tree data structure with higher memory efficiency.
[0025] As an embodiment of the present invention, assuming that the depth of a full binary tree is k, the process of constructing the binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k -1 establishes an address mapping relationship between the parent node of the target node and the parent node of the empty node previously recorded in the memory;
[0026] The memory address remapping of the binary tree includes: accessing the parent node of the empty node originally recorded in the memory, and constructing the address mapping relationship between nodes; first, due to the characteristics of the adaptive Huffman binary tree itself, the leaf node containing the data to be encoded will not have a left or right subtree, so the binary tree space configured with a full binary tree size of depth k is 2 kIn the example, the leaf node with data stored in it has a memory index of j, and its left and right child nodes with indexes 2j and (2j+1) are both empty nodes. The leaf node with the smallest index value is mapped as the parent node of the empty node. Next, in the process of constructing the mapping relationship, the node pointer is pointed to the node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
[0027] As an embodiment of the present invention, the Huffman binary tree encoding module further includes: a binary tree depth acquisition unit, configured to acquire a target binary tree depth;
[0028] The binary tree depth acquisition unit is used to record current encoding information and construct a new Huffman binary tree to perform a new round of encoding when the binary tree depth acquired by the binary tree depth acquisition unit exceeds a set target binary tree depth during the encoding process, until the set data to be encoded is encoded;
[0029] The Huffman binary tree will reinitialize a NYT leaf node, and the data to be encoded will be re-considered as the first appearance of the character node and a new adaptive Huffman binary tree will be constructed.
[0030] According to another aspect of the present invention, the following technical solution is adopted: an adaptive Huffman coding method, the adaptive Huffman coding method comprising:
[0031] A step of obtaining data to be encoded, obtaining data to be encoded;
[0032] The Huffman binary tree encoding step constructs a Huffman binary tree in real time based on the acquired data to be encoded; indexes each node of the Huffman binary tree with the memory, and when constructing the Huffman binary tree, calculates the memory index value of the target node based on the unique path from the root node to the target node.
[0033] As an embodiment of the present invention, in the Huffman binary tree encoding step, it is necessary to dynamically update node weights and exchange nodes during the construction of the Huffman binary tree; optimize the mapping of node data to hardware memory through fixed address access;
[0034] A continuous memory is configured to store the information of all internal nodes and leaf nodes of a full binary tree at a specific depth. If it is an internal node, the above dynamic operations can be performed by accessing the memory address, without having to access the left and right subtrees one by one through the parent node; if it is a leaf node, the data to be encoded of the leaf node can be directly accessed and the weight can be updated;
[0035] Based on the unique path from the root node to the target node, calculate the memory index value of the target node: the path record of the left child node of the root node is 0; the path record of the right child node is 1;
[0036] Set the depth of the full binary tree to k; map the root node to memory address 1, and its left and right child nodes are defined as addresses 2 and 3, and so on, according to the full binary tree definition; obtain the memory address mapped by any target node through the left shift operator (<<), which shifts the bits of an integer or enumeration type expression to the left; according to the above definition of left and right child node indexes, for any target node with index j, the memory address index of the left child node is expressed as j<<1, and the memory address index of the right child node is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
[0037] As an embodiment of the present invention, the Huffman binary tree encoding step includes a binary tree memory address remapping step. After the Huffman binary tree is constructed, a set of continuous memory spaces is given by using the sub-binary tree memory address remapping method, and null pointers in the memory are pointed to nodes that are out of memory range, so that the hardware can store nodes in a balanced tree data structure with higher memory efficiency.
[0038] Assuming that the depth of a full binary tree is k, the process of building a binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k -1 establishes an address mapping relationship between the parent node of the target node and the parent node of the empty node previously recorded in the memory;
[0039] The binary tree memory address remapping step includes: accessing the parent node of the empty node originally recorded in the memory, and constructing the address mapping relationship between nodes; first, due to the characteristics of the adaptive Huffman binary tree itself, the leaf node containing the data to be encoded will not have a left or right subtree, so the binary tree space configured with a full binary tree size of depth k is 2 k In the example, the leaf node with data stored in it has a memory index of j, and its left and right child nodes with indexes 2j and (2j+1) are both empty nodes. The leaf node with the smallest index value is mapped as the parent node of the empty node. Next, in the process of constructing the mapping relationship, the node pointer is pointed to the node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
[0040] As an embodiment of the present invention, the Huffman binary tree encoding step further includes a binary tree depth acquisition step of acquiring a target binary tree depth;
[0041] When the binary tree depth obtained during the encoding process exceeds the set target binary tree depth, the current encoding information is recorded, and a new Huffman binary tree is constructed to perform a new round of encoding until the set data to be encoded is encoded;
[0042] The Huffman binary tree will reinitialize a NYT leaf node, and the data to be encoded will be re-considered as the first appearance of the character node and a new adaptive Huffman binary tree will be constructed.
[0043] The beneficial effects of the present invention are as follows: the adaptive Huffman coding system and method proposed in the present invention can efficiently access the coding data structure on hardware and optimize the coding storage space, while providing the flexibility of configurable memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 FIG. 4 is a schematic diagram of the composition of an adaptive Huffman coding system in one embodiment of the present invention.
[0045] Figure 2 Schematic diagram of the composition of a Huffman binary tree encoding module in one embodiment of the present invention.
[0046] Figure 3 FIG. 4 is a flowchart of an adaptive Huffman coding method according to an embodiment of the present invention.
[0047] Figure 4 Schematic diagram of an adaptive Huffman coding example.
[0048] Figure 5 Schematic diagram of an access mechanism for optimizing Huffman binary tree memory address mapping in one embodiment of the present invention.
[0049] Figure 6 A schematic diagram of a memory example for optimizing Huffman binary tree memory address mapping in one embodiment of the present invention.
[0050] Figure 7 Schematic diagram of memory address remapping of a sub-binary tree in one embodiment of the present invention.
[0051] Figure 8 This is a flowchart of a configurable binary tree depth operation in one embodiment of the present invention.
[0052] Figure 9 FIG. 1 is a schematic diagram illustrating an example of compression parameters using adaptive Huffman coding with high hardware efficiency in one embodiment of the present invention. DETAILED DESCRIPTION
[0053] The preferred embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
[0054] In order to further understand the present invention, preferred embodiments of the present invention are described below in conjunction with examples. However, it should be understood that these descriptions are only for further illustrating the features and advantages of the present invention, rather than limiting the claims of the present invention.
[0055] The description in this section is only for several typical embodiments, and the present invention is not limited to the scope of the embodiments described. The same or similar existing technical means and some technical features of the embodiments are mutually replaced within the scope of the description and protection of the present invention.
[0056] The description of the steps in each embodiment in the specification is only for the convenience of explanation, and the implementation method of this application is not limited by the order of implementation of the steps.
[0057] The term “connection” in the specification includes both direct connection and indirect connection.
[0058] The present invention discloses an adaptive Huffman coding system, Figure 1 FIG is a schematic diagram of the composition of an adaptive Huffman coding system in one embodiment of the present invention; please refer to Figure 1 The adaptive Huffman coding system includes: a data acquisition module 1 for acquiring data to be encoded and a Huffman binary tree encoding module 2. The data acquisition module 1 is configured to acquire data to be encoded. The Huffman binary tree encoding module 2 is configured to construct a Huffman binary tree based on the data to be encoded acquired by the data acquisition module. Each node of the Huffman binary tree is indexed with a memory. When constructing the Huffman binary tree, the memory index value of the target node is calculated based on a unique path from the root node to the target node.
[0059] Figure 2 This is a schematic diagram of the composition of the Huffman binary tree encoding module in one embodiment of the present invention; please refer to Figure 2 In one embodiment of the present invention, the Huffman binary tree encoding module 2 includes: a Huffman binary tree initialization unit 21 and a Huffman binary tree update unit 22. The Huffman binary tree initialization unit 21 is used to initialize the Huffman binary tree; the Huffman binary tree update unit 22 is used to update the Huffman binary tree according to a set rule.
[0060] The process of updating the Huffman binary tree by the Huffman binary tree updating unit 22 must comply with the following specifications:
[0061] (1) Nodes are numbered in ascending order; that is, node numbers increase from top to bottom and from left to right;
[0062] (2) Internal nodes are represented by circles; the weight of an internal node is the sum of the weights of its child nodes;
[0063] (3) Leaf nodes are represented by squares; the weight of a leaf node is initialized to 1. If data is repeatedly input, the weight increases by 1;
[0064] (4) During the tree update process, if the weight of the left subtree is greater than that of the right subtree, the nodes must be swapped.
[0065] The initial state of the Huffman binary tree initialized by the Huffman binary tree initialization unit 21 contains only one leaf node, namely the NYT leaf node (Not Yet Transmitted). NYT is an escape character. When the decoder decodes NYT, it can be known that the content behind it is an uncoded symbol. When a symbol q is inserted, two situations may occur:
[0066] (1) q is the first occurrence of a character node; NYT forms a subtree consisting of two leaf nodes: the NYT symbol and the new symbol q, and determines whether the parent node of the subtree meets the fourth point of the above specification. If not, the nodes must be swapped and the weight value updated;
[0067] (2) q is not the first occurrence of a character node. If the parent node of the node where q is located meets the requirement that the weight of the left subtree is less than that of the right subtree, then the weight of the symbol q node and the parent node weight can be directly increased by 1. If not, the nodes must be exchanged and the weight values updated.
[0068] In one embodiment of the present invention, the Huffman binary tree encoding module 2 needs to dynamically update node weights and swap nodes during the Huffman binary tree construction process. Therefore, quickly acquiring node addresses to perform these operations is crucial to overall encoding and decoding speed. Typically, pointers to each node are accessed recursively to perform these dynamic operations, and each node cannot be accessed through a fixed address. Using fixed address access optimizes the mapping of node data to hardware memory.
[0069] First, a continuous block of memory is configured to store information about all internal nodes and leaf nodes of a full binary tree at a specific depth. If it is an internal node, the above dynamic operations can be performed by accessing the memory address without having to access the left and right subtrees one by one through the parent node; if it is a leaf node, the data to be encoded of the leaf node can be directly accessed and the weight can be updated.
[0070] Next, based on the unique path from the root node to the target node, the memory index value of the target node is calculated: the path record of the left child node of the root node is 0; the path record of the right child node is 1.
[0071] Assume that the full binary tree depth is k. In the usual method, the i-th layer has 2 i-1nodes, the index of the j node on the i-th layer is j, its left child node index is 2j; its right child node index is (2j+1), a total of 2 k of memory.
[0072] Figure 4 is an example of adaptive Huffman coding. Given a set of data symbols "0x110x220x330x110x22", the final corresponding encoding result is 00010001 0100100010 10100110011 0 11 , insert the final Huffman binary tree of the last 0x22 data symbol.
[0073] In the present invention, the root node is mapped to a memory address of 1, and its left and right child nodes are defined as addresses 2 and 3 respectively according to the full binary tree, and so on; obtaining the memory address mapped by any target node is achieved through the left shift operator (<<), which shifts the bits of an integer or enumeration type expression to the left and is often used to accelerate hardware power multiplication; according to the above-mentioned left and right child node index definitions, for any target node with index j, the left child node memory address index is expressed as j<<1, and the right child node memory address index is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
[0074] Figure 5 The invention discloses an access mechanism for optimizing binary tree memory address mapping in one embodiment. The hexadecimal AI model parameters are encoded, and one node stores 1 byte of data, for example: 0x11. During encoding, it is necessary to construct a Huffman binary tree in real time, set the address of the root node (root) to 1, and encode the remaining value after the highest bit 1 of the address. This encoding method is the same as the original adaptive Huffman coding. The path from the target node to its left child node is recorded as 0, and the memory address can be accessed through the method of (1<<1); the path to its right child node is recorded as 1, and the memory address can be accessed through the method of (1<<1)+1. By recording the position of the node relative to the root node, a set of fixed access addresses can be obtained. For example, the position record of node b is (10)2, which means it is the left child of the root node, and its address is (1<<1)=2; the position record of node c is (11)2, which means it is the right child of the root node, and its address is (1<<1)+1=3; the position record of node d is (100)2, which means it is the next two left children of the root node, and its address is ((1<<1)<<1)=4). When compressing AI model parameters in real time on edge hardware, node data can be accessed through hardware-friendly shift operators.
[0075] Figure 6The present invention discloses a memory example of optimizing the memory address mapping of a binary tree in one embodiment. Assume that a memory of 16 bytes is configured for storing data, which can accommodate a binary tree with a depth of 4 at most, with a total of 15 nodes (such as Figure 5 ), Figure 5 The actual address of each node in the memory is as follows Figure 6 shown.
[0076] The Huffman binary tree encoding module 2 further includes a binary tree memory address remapping unit 23, which is used to provide a set of continuous memory spaces by remapping the sub-binary tree memory addresses after the Huffman binary tree is constructed, and point the null pointer in the memory to the node that is out of memory range, so that the hardware can store the nodes in a balanced tree data structure with higher memory efficiency.
[0077] Assuming that the depth of a full binary tree is k, the process of building a binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k An address mapping relationship is established between the parent node of the target node of -1 and the parent node of the empty node originally recorded in the memory.
[0078] The binary tree memory address remapping unit 23 is used to access the parent node of the empty node originally recorded in the memory and to construct the address mapping relationship between nodes. First, due to the characteristics of the adaptive Huffman binary tree itself, the leaf node containing the data to be encoded does not have a left or right subtree, so the binary tree space 2 configured with a full binary tree size of depth k is k In the example, the leaf node with data stored in it has a memory index of j, and its left and right child nodes with indexes 2j and (2j+1) are both empty nodes. The leaf node with the smallest index value is mapped as the parent node of the empty node. Next, in the process of constructing the mapping relationship, the node pointer is pointed to the node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
[0079] Figure 7 The present invention discloses a method for remapping memory addresses in a binary tree in one embodiment of the present invention. Figure 5 、 Figure 6 It can be observed that if the binary tree presents a skewed tree encoding structure, a lot of memory will be wasted (such as Figure 6 Most of the 16-byte memory is empty. A feature map of an AI model is encoded to generate a binary tree with approximately 2 nodes. 17However, in reality, only 513 nodes have stored data (assuming that each pixel in the feature map ranges from 0 to 255, a total of 256×2+1=513 nodes are required), resulting in a large amount of memory waste. Figure 7 The data structure of a skewed tree is presented. Assuming that the memory only allows the binary tree to be up to 4 deep, the node (p,q) that exceeds the depth limit after encoding is stored in the empty node position (f,g) in the memory. That is, the parent node (c) of the position (f,g) is mapped to the parent node (h) of the original node (p,q). When the address of the f and g nodes is to be encoded, if the node is located in the child binary tree, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree. Taking the p node as an example, the p node in the original binary tree address is (10000)2. The p point is mapped to the f node (110)2 of the child binary tree. At the same time, the mapping relationship between the parent nodes is established [h(1000)2→c(11)2]. When the f node needs to be encoded into a codeword, the parent node mapping relationship is used to remap the f node address back to the original binary tree address, (
[11] 0)2→(
[1000] 0)2. Therefore, the codeword of node f is (0000)2 (the remaining value after the highest bit 1). Similarly, the data is gradually arranged into a data structure with a more balanced left and right subtree. By remapping the memory address of the sub-binary tree, the hardware can store nodes in a balanced tree data structure with higher memory efficiency, reducing the number of binary tree nodes generated by encoding a feature map of the AI model to about 2. 10 (i.e., from the original 131KB to 1KB, improving memory storage efficiency by about 100 times).
[0080] In one embodiment of the present invention, the Huffman binary tree encoding module 2 further includes a binary tree depth acquisition unit 24 for acquiring a target binary tree depth. When the binary tree depth acquired by the binary tree depth acquisition unit exceeds the set target binary tree depth during the encoding process, the binary tree depth acquisition unit 24 records the current encoding information and constructs a new Huffman binary tree for a new round of encoding until the set data to be encoded is encoded. The Huffman binary tree will reinitialize an NYT leaf node, and the data to be encoded will be re-considered as the first occurrence of the character node and a new adaptive Huffman binary tree will be constructed according to the above method.
[0081] The present invention further discloses an adaptive Huffman coding method, Figure 3 is a flow chart of an adaptive Huffman coding method according to an embodiment of the present invention; Figure 3 , the adaptive Huffman coding method includes:
[0082] [Step S1] A step of obtaining data to be encoded, obtaining data to be encoded;
[0083] [Step S2] Huffman binary tree encoding step, constructing a Huffman binary tree in real time based on the acquired data to be encoded; indexing each node of the Huffman binary tree with the memory, and when constructing the Huffman binary tree, calculating the memory index value of the target node based on the unique path from the root node to the target node.
[0084] In one embodiment of the present invention, the Huffman binary tree encoding step requires dynamic updating of node weights and node swapping during the Huffman binary tree construction process. Therefore, quickly acquiring node addresses to perform these operations is crucial to overall encoding and decoding speed. Typically, pointers to each node are accessed recursively to perform these dynamic operations, and each node cannot be accessed through a fixed address. Using fixed address access optimizes the mapping of node data to hardware memory.
[0085] First, a continuous block of memory is configured to store information about all internal nodes and leaf nodes of a full binary tree at a specific depth. If it is an internal node, the above dynamic operations can be performed by accessing the memory address without having to access the left and right subtrees one by one through the parent node; if it is a leaf node, the data to be encoded of the leaf node can be directly accessed and the weight can be updated.
[0086] Next, based on the unique path from the root node to the target node, the memory index value of the target node is calculated: the path record of the left child node of the root node is 0; the path record of the right child node is 1.
[0087] Assume that the depth of the full binary tree is k, and according to the definition, the i-th layer has 2 i-1 nodes, the index of the j node on the i-th layer is j, its left child node index is 2j; its right child node index is (2j+1), a total of 2 k of memory.
[0088] The root node is mapped to memory address 1, and its left and right child nodes are defined as addresses 2 and 3 respectively according to the full binary tree, and so on. The memory address mapped by any target node is obtained through the left shift operator (<<). The left shift operator shifts the bits of an integer or enumeration type expression to the left, and is often used to accelerate hardware power multiplication. According to the above definition of left and right child node indexes, the memory address index of the left child node of any target node with index j is expressed as j<<1, and the memory address index of the right child node is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
[0089] The Huffman binary tree encoding step includes a binary tree memory address remapping step. After the Huffman binary tree is constructed, a set of continuous memory spaces is given by using the sub-binary tree memory address remapping method, and the null pointer in the memory points to the node that is out of the memory range, so that the hardware can store the node in a balanced tree data structure with higher memory efficiency.
[0090] Assuming that the depth of a full binary tree is k, the process of building a binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k An address mapping relationship is established between the parent node of the target node of -1 and the parent node of the empty node originally recorded in the memory.
[0091] The binary tree memory address remapping step includes: accessing the parent node of the empty node originally recorded in the memory, and constructing the address mapping relationship between nodes; first, due to the characteristics of the adaptive Huffman binary tree itself, the leaf node containing the data to be encoded will not have a left or right subtree, so the binary tree space configured with a full binary tree size of depth k is 2 k In the example, the leaf node with data stored in it has a memory index of j, and its left and right child nodes with indexes 2j and (2j+1) are both empty nodes. The leaf node with the smallest index value is mapped as the parent node of the empty node. Next, in the process of constructing the mapping relationship, the node pointer is pointed to the node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
[0092] The Huffman binary tree encoding step further includes a binary tree depth acquisition step for acquiring a target binary tree depth. When the binary tree depth acquired during the encoding process exceeds the set target binary tree depth, the current encoding information is recorded, and a new Huffman binary tree is constructed for a new round of encoding until the set data to be encoded is encoded. The Huffman binary tree reinitializes an NYT leaf node, and the data to be encoded is re-considered as the first occurrence of a character node, and a new adaptive Huffman binary tree is constructed according to the above method.
[0093] In one use scenario of the present invention, a large amount of memory is required for the storage and transmission of AI parameters and feature graph data when performing edge model inference. High hardware efficiency adaptive Huffman coding is used for efficient data storage to achieve lossless data compression. During encoding, a Huffman binary tree needs to be constructed in real time. This method replaces the recursive access node with a fixed address access method to optimize the mapping of node data to hardware memory. In the process of constructing the binary tree, due to the characteristics of the distribution of AI model parameters and feature graph data, a skewed tree encoding structure is often caused. By using the sub-binary tree memory address remapping method, the hardware can store nodes in a balanced tree data structure with higher memory efficiency. For hardware with strict memory constraints, a configurable binary tree depth is provided so that the binary tree construction process can meet the memory constraints. Using high hardware efficiency adaptive Huffman coding to compress AI model parameters and feature graph data in real time on edge hardware can significantly reduce the memory bandwidth occupied per unit time and deploy the model on chips with fewer hardware resources.
[0094] During encoding, the Huffman binary tree needs to be constructed in real time. The left child node (left ch i ld) of the target node can access the memory address in the form of (1<<1); the right child node (right ch i ld) can access the memory address in the form of (1<<1)+1. Therefore, by recording the data path, a fixed set of access addresses can be obtained to optimize the mapping of node data to hardware memory. In the process of constructing the binary tree, due to the characteristics of the adaptive Huffman binary tree itself, new symbols are continuously inserted from the NYT nodes of the unilateral subtree, and non-NYT nodes no longer extend downward. In addition, the characteristics of the distribution of AI model parameters and feature map data often lead to a skewed tree encoding structure. By using the method of remapping the memory address of the sub-binary tree, after the Huffman binary tree is constructed, a set of continuous memory space is given, and the null pointer in the memory points to the node that exceeds the memory range, so that the hardware can store nodes in a balanced tree data structure with higher memory efficiency. For hardware with strict memory constraints, a configurable binary tree depth is provided. If the encoding process exceeds the target binary tree depth, the current encoding information is recorded and the binary tree is rebuilt for a new round of encoding until all data is encoded, so that the binary tree construction process can meet the memory constraints.
[0095] Figure 8This is a flowchart for running a configurable binary tree depth. For hardware with strict memory constraints, the allowed binary tree depth and number of nodes N are first calculated based on the memory size. The N data are then encoded sequentially until the N nodes are filled and the corresponding encoding results are stored. If there is still data that has not been encoded, the binary tree is initialized repeatedly and the next N data are re-encoded until all data is encoded. After the above-mentioned sub-binary tree memory address remapping optimization, a feature map of the AI model requires approximately 1KB of memory space. Therefore, if the configurable memory is less than 1KB, this method uses batch encoding to meet the hardware memory requirements.
[0096] Figure 9 This example uses hardware-efficient adaptive Huffman coding compression parameters. Assuming the configurable memory space is 16 bytes (less than 1KB), encoding is performed in batches to meet hardware memory requirements. First, a binary tree is generated using hardware-efficient adaptive Huffman coding, and each node's data is mapped to a hardware memory address based on its relative position to the root node. To avoid skewing the tree's data structure, nodes (p, q, α, β, γ, δ) that exceed the depth limit after encoding are stored in empty memory nodes (f, g, l, m, n, o). Specifically, the root node pointers for p and q are changed to point to c, and the root node pointers for γ and δ are changed to point to q. After encoding a batch of data, the corresponding encoding results are stored. If there is still data to be encoded, the binary tree is initialized again and the next batch of data is encoded again until all data is encoded.
[0097] In summary, the adaptive Huffman coding system and method proposed in the present invention can efficiently access the coding data structure on hardware and optimize the coding storage space while providing the flexibility of configurable memory.
[0098] It should be noted that the present application can be implemented in software and / or a combination of software and hardware; for example, it can be implemented using an application-specific integrated circuit (ASIC), a general-purpose computer, or any other similar hardware device. In some embodiments, the software program of the present application can be executed by a processor to implement the above steps or functions. Similarly, the software program of the present application (including related data structures) can be stored in a computer-readable recording medium; for example, a RAM memory, a magnetic or optical drive, or a floppy disk and the like. In addition, some steps or functions of the present application can be implemented in hardware; for example, as a circuit that cooperates with a processor to perform various steps or functions.
[0099] The technical features of the above-mentioned embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above-mentioned embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0100] The description and application of the present invention here are illustrative and are not intended to limit the scope of the present invention to the above-described embodiments. The effects or advantages involved in the embodiments may not be embodied in the embodiments due to interference from various factors, and the description of the effects or advantages is not used to limit the embodiments. Variations and changes to the embodiments disclosed here are possible, and the replacement of the embodiments and various equivalent components are well known to those of ordinary skill in the art. It should be clear to those skilled in the art that the present invention can be implemented in other forms, structures, arrangements, proportions, and with other components, materials, and parts without departing from the spirit or essential characteristics of the present invention. Other variations and changes can be made to the embodiments disclosed here without departing from the scope and spirit of the present invention.
Claims
1. An adaptive Huffman coding system, characterized in that The adaptive Huffman coding system includes: A module for acquiring data to be encoded, used for acquiring data to be encoded; A Huffman binary tree encoding module is used to construct a Huffman binary tree based on the data to be encoded obtained by the data to be encoded acquisition module; establish an index between each node of the Huffman binary tree and the memory, and when constructing the Huffman binary tree, calculate the memory index value of the target node based on the unique path from the root node to the target node; The Huffman binary tree encoding module further includes a binary tree memory address remapping unit for assigning a set of continuous memory spaces by remapping the memory addresses of the child binary trees after the Huffman binary tree is constructed, and for pointing null pointers in the memory to nodes that are out of memory range, so that the hardware can store the nodes in a balanced tree data structure with higher memory efficiency; Set the depth of a full binary tree to k. The process of building a binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k -1 establishes an address mapping relationship between the parent node of the target node and the parent node of the empty node previously recorded in the memory; The binary tree memory address remapping unit is used to access the parent node of the empty node originally recorded in the memory and to construct the address mapping relationship between nodes; the leaf node of the adaptive Huffman binary tree containing the data to be encoded has no left or right subtree, so the binary tree space configured with the full binary tree size of depth k is 2 k In the example, the leaf node with data stored in the memory is indexed as j, and its left and right child nodes indexed as 2j and (2j+1) are both empty nodes; the leaf node with the smallest index value is mapped as the parent node of the empty node; in the process of constructing the mapping relationship, the leaf node pointer with the smallest index value points to the leaf node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
2. The adaptive Huffman coding system according to claim 1, wherein: The Huffman binary tree encoding module includes: Huffman binary tree initialization unit, used to initialize the Huffman binary tree; A Huffman binary tree update unit, used to update the Huffman binary tree according to set rules; The process of updating the Huffman binary tree by the Huffman binary tree updating unit must follow the following specifications: (1) Nodes are numbered in ascending order; that is, node numbers increase from top to bottom and from left to right; (2) Internal nodes are represented by circles; the weight of an internal node is the sum of the weights of its child nodes; (3) Leaf nodes are represented by squares; the weight of a leaf node is initialized to 1. If data is repeatedly input, the weight increases by 1; (4) During the tree update process, if the weight of the left subtree is greater than that of the right subtree, the nodes must be swapped.
3. The adaptive Huffman coding system according to claim 1, wherein: The Huffman binary tree encoding module dynamically updates node weights and exchanges nodes during the process of constructing the Huffman binary tree; Optimize the mapping of node data to hardware memory through fixed address access; Configure a continuous memory to store the information of all internal nodes and leaf nodes of a full binary tree at a specific depth; If internal nodes are stored, node weights can be dynamically updated and nodes can be swapped by accessing memory addresses, without having to access the left and right subtrees one by one through the parent node. If a leaf node is stored, the data to be encoded of the leaf node is directly accessed and the weight is updated; Based on the unique path from the root node to the target node, calculate the memory index value of the target node: the path record of the left child node of the root node is 0; the path record of the right child node is 1; Assume the depth of the full binary tree is k; map the root node to memory address 1, and its left and right child nodes are defined as addresses 2 and 3, and so on according to the full binary tree definition; obtain the memory address mapped by any target node through the left shift operator <<, which shifts the bits of an integer or enumeration type expression to the left; according to the above definitions of left and right child node indexes, for any target node with index j, the memory address index of the left child node is expressed as j<<1, and the memory address index of the right child node is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
4. The adaptive Huffman coding system according to claim 1, wherein: The Huffman binary tree encoding module further includes a binary tree depth acquisition unit for acquiring a target binary tree depth; The binary tree depth acquisition unit is used to record current encoding information and construct a new Huffman binary tree to perform a new round of encoding when the binary tree depth acquired by the binary tree depth acquisition unit exceeds a set target binary tree depth during the encoding process, until the set data to be encoded is encoded; The Huffman binary tree will reinitialize a NYT leaf node, and the data to be encoded will be re-considered as the first appearance of the character node and a new adaptive Huffman binary tree will be constructed.
5. An adaptive Huffman coding method, characterized in that The adaptive Huffman coding method comprises: A step of obtaining data to be encoded, obtaining data to be encoded; The Huffman binary tree encoding step constructs a Huffman binary tree in real time based on the acquired data to be encoded; indexes each node of the Huffman binary tree with the memory, and when constructing the Huffman binary tree, calculates the memory index value of the target node based on the unique path from the root node to the target node; The Huffman binary tree encoding step includes a binary tree memory address remapping step. After the Huffman binary tree is constructed, a set of continuous memory spaces is given by using the sub-binary tree memory address remapping method, and null pointers in the memory are pointed to nodes that are out of memory range, so that the hardware can store the nodes in a balanced tree data structure with higher memory efficiency. Set the depth of a full binary tree to k. The process of building a binary tree will pre-calculate the memory index value of the target node. Once the index value exceeds 2 k -1, then implement binary tree memory address remapping; the binary tree memory address remapping process will exceed the index value of 2 k -1 establishes an address mapping relationship between the parent node of the target node and the parent node of the empty node previously recorded in the memory; In the binary tree memory address remapping step, the parent node of the empty node originally recorded in the memory and the construction of the address mapping relationship between nodes are accessed; the leaf node of the adaptive Huffman binary tree containing the data to be encoded has no left or right subtree, so the binary tree space configured with the full binary tree size of depth k is 2 k In the example, the leaf node with data stored in the memory is indexed as j, and its left and right child nodes indexed as 2j and (2j+1) are both empty nodes; the leaf node with the smallest index value is mapped as the parent node of the empty node; in the process of constructing the mapping relationship, the leaf node pointer with the smallest index value points to the leaf node with an index value exceeding 2. k -1, and during encoding, the parent node mapping relationship is used to remap the child binary tree address information back to the original binary tree.
6. The adaptive Huffman coding method according to claim 5, wherein: In the Huffman binary tree encoding step, it is necessary to dynamically update node weights and exchange nodes in the process of constructing the Huffman binary tree; Optimize the mapping of node data to hardware memory through fixed address access; Configure a continuous memory to store the information of all internal nodes and leaf nodes of a full binary tree at a specific depth; If internal nodes are stored, node weights can be dynamically updated and nodes can be swapped by accessing memory addresses, without having to access the left and right subtrees one by one through the parent node. If a leaf node is stored, the data to be encoded of the leaf node is directly accessed and the weight is updated; Based on the unique path from the root node to the target node, calculate the memory index value of the target node: the path record of the left child node of the root node is 0; the path record of the right child node is 1; Set the depth of the full binary tree to k; map the root node to memory address 1, and its left and right child nodes are defined as addresses 2 and 3 respectively according to the full binary tree definition, and so on; get the memory address mapped by any target node through the left shift operator <<, which shifts the bits of an integer or enumeration type expression to the left; according to the above definitions of left and right child node indexes, for any target node with index j, the memory address index of the left child node is expressed as j<<1, and the memory address index of the right child node is expressed as (j<<1)+1, where the memory index i of the target node is calculated by the unique path from the root node to the target node.
7. The adaptive Huffman coding method according to claim 5, wherein: The Huffman binary tree encoding step further includes a binary tree depth acquisition step, acquiring a target binary tree depth; When the binary tree depth obtained during the encoding process exceeds the set target binary tree depth, the current encoding information is recorded, and a new Huffman binary tree is constructed to perform a new round of encoding until the set data to be encoded is encoded; The Huffman binary tree will reinitialize a NYT leaf node, and the data to be encoded will be re-considered as the first appearance of the character node and a new adaptive Huffman binary tree will be constructed.
Citation Information
Patent Citations
Multi-task micro-system-oriented adaptive compression storage and decompression extraction method
CN107423397A
Method and access means for determining the storage address of a data value in a memory device
US6415279B1