Model quantization method, device, electronic device and storage medium
By dividing and sorting the weight matrix of the neural network model and adjusting the quantization deviation, combined with bit-wise interleaved mapping, the problem of poor weight bit width selectivity in the existing technology is solved, efficient model quantization is achieved, and accuracy loss and performance are reduced. Consumption.
Patent Information
- Application Number
- CN202311552181.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-21
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2043-11-21
AI Technical Summary
Existing quantization methods cannot flexibly select the weight bit width of the neural network model, resulting in huge resource usage during high-precision calculations and inability to adapt to different compression rate requirements and accuracy.
By obtaining the weight matrix of the network layer, dividing, sorting and reorganizing, the quantization accuracy is adjusted according to the quantization deviation and threshold, and the quantization weight block is mapped to the cross array unit using bitwise interleaved mapping, and the weight bit width is flexibly selected to reduce accuracy loss. .
It achieves the reduction of accuracy loss while ensuring compression efficiency, avoids retraining, and improves the accuracy and hardware efficiency of model inference.
Smart Images

Figure CN117574966B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and more specifically, to a model quantization method, device, electronic device and storage medium. Background Art
[0002] Deep neural networks (DNNs) have made great progress in fields such as computer vision and natural language processing. However, DNNs usually lead to high power consumption due to their intensive computation and massive data movement. Processing-in-memory (PIM) reduces data movement between memory and processor by performing computations in an in-memory computing array.
[0003] However, PIM is not suitable for high-precision calculations because to represent high-precision data, multiple bit cells need to be included in a row / column to provide sufficient data accuracy, which results in huge resource usage. Therefore, quantization is usually used to compress neural network models to reduce data accuracy.
[0004] Related technologies only reduce the storage bit width. Cluster-based quantization methods use the cluster center of the group to which they belong to represent the weight, but they also need to be restored to the original value through the index during calculation, which not only fails to save computing resources but also increases latency. Binary / ternary quantization methods and integer / fixed-point quantization methods require specific hardware support during inference and cannot be applied to different compression rate requirements and accuracy.
[0005] At present, some quantization methods have been proposed to solve the above problems, such as using array granularity to quantize the model with mixed precision of 1-8 bits; applying genetic algorithms to quantization of layer granularity; quantizing the model to 2 bits and mapping it to the 3D-ReRAM array; or applying mixed precision quantization to each layer of DNN to reduce the energy consumption of PIM. However, these quantization methods cannot adapt to the bit width of the model. Summary of the invention
[0006] In view of the above problems, the object of the present invention is to provide a model quantization method, device, electronic device and storage medium, which can flexibly select the bit width of the weight and reduce the loss of precision while ensuring compression efficiency.
[0007] According to a first aspect of the present invention, a model quantization method is provided, comprising: obtaining a weight matrix of a current network layer, dividing the weight matrix into blocks to obtain a plurality of weight blocks; reorganizing the absolute values of weights in the plurality of weight blocks by column sorting to obtain a plurality of target weight blocks; determining the quantization accuracy of each target weight block according to the quantization deviation and quantization threshold of each target weight block; adjusting the quantization threshold according to the weight distribution deviation and distribution threshold of the plurality of target weight blocks before and after quantization to obtain the target quantization accuracy of the plurality of target weight blocks; quantizing the plurality of target weight blocks according to the target quantization accuracy of the plurality of target weight blocks to obtain a plurality of quantized weight blocks; and mapping the plurality of quantized weight blocks to a plurality of cross array units using a bit-interleaved mapping method.
[0008] Preferably, adjusting the quantization threshold according to the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain the target quantization accuracy of multiple target weight blocks includes: obtaining the weight distribution deviation of multiple target weight blocks before and after quantization; judging whether the weight distribution deviation is less than the distribution threshold; when the weight distribution deviation is less than the distribution threshold, taking the current quantization accuracy of each target weight block as the target quantization accuracy of each target weight block; when the weight distribution deviation is greater than or equal to the distribution threshold, adjusting the quantization threshold by binary search to obtain a new quantization threshold; redetermining the quantization accuracy of each target weight block according to the new quantization threshold; repeating the above steps until the weight distribution deviation is less than the distribution threshold.
[0009] Preferably, the calculation formula of the quantization deviation is: Among them, W is the original weight value in the target weight block; Wq is the quantized weight value of the target weight block.
[0010] Preferably, when the quantization deviation of the target weight block is less than the quantization threshold, the quantization accuracy of the target weight block is the first accuracy; when the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization accuracy of the target weight block is the second accuracy, wherein the second accuracy is greater than the first accuracy.
[0011] Preferably, mapping the multiple quantization weight blocks to the multiple cross array units in a bit-interleaved mapping manner includes: mapping different bits of the same weight value to the same position of the multiple cross array units respectively.
[0012] Preferably, the model quantization method further comprises: controlling the shift operation of the output results of the plurality of cross array units according to the quantization precision of different quantization weight blocks.
[0013] Preferably, when the quantization accuracy of the quantization weight block is the first accuracy, the output results of multiple cross array units are directly output; when the quantization accuracy of the quantization weight block is the second accuracy, the output results of some cross array units are shifted and merged with the output results of other cross array units before output.
[0014] According to a second aspect of the present invention, a model quantization device is provided, comprising: a block division module, used to obtain a weight matrix of a current network layer, and divide the weight matrix into blocks to obtain a plurality of weight blocks; a reorganization module, used to reorganize the absolute values of weights in the plurality of weight blocks in column sorting to obtain a plurality of target weight blocks; a quantization accuracy determination module, used to determine the quantization accuracy of each target weight block according to the quantization deviation and the initial quantization threshold of each target weight block; a quantization accuracy selection module, used to adjust the initial quantization threshold according to the weight distribution deviation of the plurality of target weight blocks before and after quantization to obtain the target quantization accuracy of the plurality of target weight blocks; a quantization module, used to quantize the plurality of target weight blocks according to the target quantization accuracy of the plurality of target weight blocks to obtain a plurality of quantized weight blocks; a mapping module, used to map the plurality of quantized weight blocks to a plurality of cross array units using a bit-interleaved mapping method.
[0015] Preferably, the quantization accuracy selection module includes: a distribution deviation unit, used to obtain the weight distribution deviation of multiple target weight blocks before and after quantization; a judgment unit, used to judge whether the weight distribution deviation is less than the distribution threshold; a quantization accuracy selection unit, used to use the current quantization accuracy of each target weight block as the target quantization accuracy of each target weight block when the weight distribution deviation is less than the distribution threshold; a threshold adjustment unit, used to use the binary method to adjust the initial quantization threshold to obtain the quantization threshold when the weight distribution deviation is greater than or equal to the distribution threshold; the quantization accuracy determination module is also used to redetermine the quantization accuracy of each target weight block according to the new quantization threshold.
[0016] Preferably, the calculation formula of the quantization deviation is: Among them, W is the original weight value in the target weight block; Wq is the quantized weight value of the target weight block.
[0017] Preferably, when the quantization deviation of the target weight block is less than the quantization threshold, the quantization accuracy of the target weight block is the first accuracy; when the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization accuracy of the target weight block is the second accuracy, wherein the second accuracy is greater than the first accuracy.
[0018] Preferably, the mapping module is further used to map different bits of the same weight value to the same position of a plurality of cross array units respectively.
[0019] Preferably, the model quantization device further comprises: a shift control module, which is used to control the shift operation of the output results of the plurality of cross array units according to the quantization precision of different quantization weight blocks.
[0020] Preferably, the shift control module is also used to directly output the output results of multiple cross array units when the quantization accuracy of the quantization weight block is the first accuracy; when the quantization accuracy of the quantization weight block is the second accuracy, shift the output results of some cross array units and merge the output results of other cross array units before output.
[0021] According to a third aspect of the present invention, there is provided a computer-readable storage medium storing a computer program, wherein the computer program implements the model quantization method as described above when executed by a processor.
[0022] According to a fourth aspect of the present invention, there is provided an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the model quantization method as described above when executing the computer program.
[0023] The model quantization method, device, electronic device and storage medium provided by the present invention obtain the weight matrix of the current network layer, divide the weight matrix into blocks to obtain multiple weight blocks; reorganize the absolute values of the weights in the multiple weight blocks in column sorting to obtain multiple target weight blocks; determine the quantization accuracy of each target weight block according to the quantization deviation and quantization threshold of each target weight block; adjust the initial quantization threshold according to the weight distribution deviation and distribution threshold of the multiple target weight blocks before and after quantization to obtain the target quantization accuracy of the multiple target weight blocks; quantize the multiple target weight blocks according to the target quantization accuracy of the multiple target weight blocks to obtain multiple quantized weight blocks; and map the multiple quantized weight blocks to multiple cross array units using a bit-interleaved mapping method. The present application performs quantization through weight blocks, and can flexibly select the bit width of the weights, while ensuring compression efficiency and reducing precision loss.
[0024] Furthermore, the present application adopts an offline quantization method to quantize the model, which can avoid expensive retraining and has better performance in accuracy and energy consumption.
[0025] Furthermore, the absolute values of weights in multiple weight blocks are reorganized by sorting the columns to obtain multiple target weight blocks, so that the weight values in the target weight blocks can be made closer, thereby reducing the quantization error.
[0026] Furthermore, the quantization accuracy of each target weight block is determined according to the quantization deviation and quantization threshold of each target weight block, and the quantization threshold is adjusted through the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain the optimal quantization threshold, which can achieve a balance between the accuracy of model inference and hardware.
[0027] Furthermore, the quantization threshold is adjusted by using the binary method, and the quantization threshold is adjusted by the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain a suitable quantization threshold. This can quickly determine the optimal quantization threshold and improve the efficiency of the quantization strategy search.
[0028] Furthermore, a plurality of quantized weight blocks are mapped to a plurality of cross array units in a bit-interleaved mapping manner, that is, different bits of the same weight value are respectively mapped to the same position of a plurality of cross array units, which can reduce peripheral circuit overhead. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The above and other objects, features and advantages of the present invention will become more apparent through the following description of the embodiments of the present invention with reference to the accompanying drawings, in which:
[0030] Figure 1 A flow chart showing a model quantization method provided by an embodiment of the present invention;
[0031] Figure 2 A schematic diagram showing weight changes corresponding to the model quantization method provided in an embodiment of the present invention is shown;
[0032] Figure 3 A flowchart showing step S140 in the model quantization method provided by an embodiment of the present invention is shown;
[0033] Figure 4 A schematic diagram showing distribution deviation of a weight matrix before and after quantization provided by an embodiment of the present invention is shown;
[0034] Figure 5 A schematic diagram showing the relationship between the number of cross arrays in the model quantization process provided by an embodiment of the present invention and the accuracy of the model after quantization;
[0035] Figure 6 A schematic diagram showing the mapping of the quantized weights of the model provided by an embodiment of the present invention to a cross array;
[0036] Figure 7 A schematic diagram showing the structure of a model quantization device provided by an embodiment of the present invention is shown;
[0037] Figure 8 A schematic structural diagram of an electronic device provided by an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0038] Various embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. In each of the accompanying drawings, the same elements are represented by the same or similar reference numerals. For the sake of clarity, the various parts in the accompanying drawings are not drawn to scale.
[0039] The specific implementation of the present invention is further described in detail below in conjunction with the drawings and examples.
[0040] Figure 1 FIG. 1 is a flow chart showing a model quantization method provided by an embodiment of the present invention. Figure 1 As shown, the model quantization method includes the following steps.
[0041] In step S110, a weight matrix of the current network layer is obtained, and the weight matrix is divided into blocks to obtain a plurality of weight blocks.
[0042] In this embodiment, the weight values of the current network layer of the deep neural network are expanded into a weight matrix, and the size of the weight matrix is: (Cin×d×d)×Cout, Cin is the number of input channels, Cout is the number of output channels, and d is the width and height of the convolution kernel; when the size of the weight block is m*n, the number of weight blocks is (Cin×d×d / m)×(Cout / n).
[0043] See also Figure 2 , take Cin=2,d=2,Cout=4,m=2,n=2 as an example, the size of the convolution kernel of the current network layer is 2*2, the input channel is 2, and the output channel is 4, then the weight value of the current network layer is expanded into an 8*4 weight matrix; and the 8*4 weight matrix is divided into multiple 2*2 weight blocks (see Figure 2 (Figure on both sides of arrow 1).
[0044] In step S120, the absolute values of weights in the plurality of weight blocks are reorganized by sorting the columns to obtain a plurality of target weight blocks.
[0045] In this embodiment, each weight block is divided into multiple weight sub-blocks by rows, the weight values of multiple weight sub-blocks in the same column are sorted according to the size of their absolute values, and the weight sub-blocks with similar weight values are reorganized to obtain multiple target weight blocks, and the weight values of the target weight blocks are similar.
[0046] See also Figure 2 , divide the 2*2 weight block into 1*2 weight sub-blocks, and obtain the absolute value of the weight value of each column of the weight sub-block (see Figure 2 Then, the weight sub-blocks in each column are sorted according to their absolute values, and two similar 1*2 weight sub-blocks are reorganized into a 2*2 target weight block (see Figure 2 (see Figure 3 on both sides of arrow 3).
[0047] In step S130 , the quantization accuracy of each target weight block is determined according to the quantization deviation and the quantization threshold of each target weight block.
[0048] In this embodiment, the quantization deviation of each target weight block is obtained, and the calculation formula of the quantization deviation is: Wherein, W is the original weight value in the weight block; Wq is the weight value of the weight block after quantization with the first precision. The quantization deviation of each target weight block is compared with the quantization threshold. When the quantization deviation of the target weight block is less than the quantization threshold, the quantization precision of the target weight block is the first precision; when the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization precision of the target weight block is the second precision, wherein the second precision is greater than the first precision. In this embodiment, the first precision is INT4 and the second precision is INT8, but it is not limited to this.
[0049] In step S140, the quantization threshold is adjusted according to the weight distribution deviation and distribution threshold of the multiple target weight blocks before and after quantization to obtain the target quantization accuracy of the multiple target weight blocks.
[0050] In this embodiment, see Figure 3 , step S140 includes the following steps.
[0051] In step S141, weight distribution deviations of multiple target weight blocks before and after quantization are obtained.
[0052] In this embodiment, see Figure 4 , obtain distribution diagrams of multiple target weight blocks before and after quantization, and obtain weight distribution deviations according to the distribution diagrams.
[0053] In step S142, it is determined whether the weight distribution deviation is less than a distribution threshold.
[0054] In step S143, when the weight distribution deviation is less than the distribution threshold, the current quantization accuracy of each target weight block is used as the target quantization accuracy of each target weight block.
[0055] In step S144, when the weight distribution deviation is greater than or equal to the distribution threshold, the quantization threshold is adjusted using a binary method to obtain a new quantization threshold.
[0056] In this embodiment, half of the current quantization threshold is used as the new quantization threshold. After multiple rounds of threshold adjustment, the optimal quantization threshold can be obtained. At this time, the quantization accuracy of each target weight block is the optimal quantization strategy, which can achieve a better trade-off between the number of crossbar arrays and the accuracy of the model after quantization in terms of hardware (see Figure 5 ).
[0057] In step S145, the quantization accuracy of each target weight block is re-determined according to the new quantization threshold, and then step S141 is continued until the weight distribution deviation is less than the distribution threshold.
[0058] In step S150, multiple target weight blocks are quantized according to target quantization precisions of the multiple target weight blocks to obtain multiple quantized weight blocks.
[0059] In step S160, a plurality of quantization weight blocks are mapped to a plurality of cross array units in a bit-interleaved mapping manner.
[0060] In this embodiment, different bits of the same weight value are respectively mapped to the same position of a plurality of cross array units.
[0061] See also Figure 6 The storage and calculation device includes a plurality of storage and calculation modules, and the weight value of each network layer is mapped to a corresponding storage and calculation module. Each storage and calculation module includes a first storage and calculation unit, a second storage and calculation unit, a shifter, an adder and a shift accumulator. The first storage and calculation unit and the second storage and calculation unit each include 4 cross array units, and the peripheral circuit of each cross array unit includes a digital-to-analog converter, an analog-to-digital converter and a shifter. Taking the quantization accuracy of the target weight block as INT4 as an example, the quantization weight block corresponding to the target weight block includes four weight values X1-X4, each weight value is 4 bits, and different bits of each weight value are sequentially mapped to the same position of different cross arrays. For example, X1-b0 is mapped to a certain position of cross array unit 1, then X1-b1 is mapped to the same position of cross array unit 2, X1-b2 is mapped to the same position of cross array unit 3, and X1-b3 is mapped to the same position of cross array unit 4. Taking the quantization accuracy of the target weight block as INT8 as an example, the target weight block includes four weight values X5-X8, each weight value is 8 bits, and different bits of each weight value are mapped to the same position of different cross arrays in turn. For example, X5-b0 is mapped to a certain position of cross array unit 1, then X5-b1 is mapped to the same position of cross array unit 2, X5-b2 is mapped to the same position of cross array unit 3, X5-b3 is mapped to the same position of cross array unit 4, X5-b4 is mapped to the same position of cross array unit 5, X5-b5 is mapped to the same position of cross array unit 6, X5-b6 is mapped to the same position of cross array unit 7, and X5-b7 is mapped to the same position of cross array unit 8.
[0062] In step S170, the shift operation of the output results of the plurality of cross array units is controlled according to the quantization precisions of the different quantization weight blocks.
[0063] In this embodiment, when the quantization accuracy of the quantization weight block is the first accuracy, the output results of multiple cross array units are directly output; when the quantization accuracy of the quantization weight block is the second accuracy, the output results of some cross array units are shifted and merged with the output results of other cross array units before output.
[0064] See also Figure 6 , if the quantization accuracy of the target weight block is INT4, that is, the weight data of the target weight block is mapped to the 4 cross array units in the first storage unit; or mapped to the 4 cross array units in the second storage unit, the output results of the 4 cross array units in the first storage unit or the output results of the 4 cross array units in the second storage unit can be directly output. If the quantization accuracy of the target weight block is INT8, that is, the weight data of the target weight block is respectively mapped to the 4 cross array units in the first storage unit and the 4 cross array units in the second storage unit, if the upper 4 bits are mapped to the first storage unit and the lower 4 bits are mapped to the second storage unit, then the output results of the 4 cross array units in the first storage unit need to be shifted, and the output results of the 4 cross array units in the second storage unit are not shifted and then merged into 8-bit data for output.
[0065] The model quantization method provided by the present invention obtains the weight matrix of the current network layer, divides the weight matrix into blocks to obtain multiple weight blocks; reorganizes the absolute values of the weights in the multiple weight blocks in column sorting to obtain multiple target weight blocks; determines the quantization accuracy of each target weight block according to the quantization deviation and quantization threshold of each target weight block; adjusts the initial quantization threshold according to the weight distribution deviation and distribution threshold of the multiple target weight blocks before and after quantization to obtain the target quantization accuracy of the multiple target weight blocks; quantizes the multiple target weight blocks according to the target quantization accuracy of the multiple target weight blocks to obtain multiple quantized weight blocks; and maps the multiple quantized weight blocks to multiple cross array units using a bit-interleaved mapping method. The present application performs quantization through weight blocks, and can flexibly select the bit width of the weight, and can reduce the loss of accuracy while ensuring compression efficiency.
[0066] Furthermore, the present application adopts an offline quantization method to quantize the model, which can avoid expensive retraining and has better performance in accuracy and energy consumption.
[0067] Furthermore, the absolute values of weights in multiple weight blocks are reorganized by sorting the columns to obtain multiple target weight blocks, so that the weight values in the target weight blocks can be made closer, thereby reducing the quantization error.
[0068] Furthermore, the quantization accuracy of each target weight block is determined according to the quantization deviation and quantization threshold of each target weight block, and the quantization threshold is adjusted through the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain the optimal quantization threshold, which can achieve a balance between the accuracy of model inference and hardware.
[0069] Furthermore, the quantization threshold is adjusted by using the binary method, and the quantization threshold is adjusted by the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain a suitable quantization threshold. This can quickly determine the optimal quantization threshold and improve the efficiency of the quantization strategy search.
[0070] Furthermore, a plurality of quantized weight blocks are mapped to a plurality of cross array units in a bit-interleaved mapping manner, that is, different bits of the same weight value are respectively mapped to the same position of a plurality of cross array units, which can reduce peripheral circuit overhead.
[0071] Figure 7 FIG. 2 is a schematic diagram showing the structure of a model quantization device provided by an embodiment of the present invention. Figure 7 As shown, the model quantization device includes a blocking module 71, a reorganization module 72, a quantization accuracy determination module 73, a quantization accuracy selection module 74, a quantization module 75 and a mapping module 76.
[0072] The block module 71 is used to obtain the weight matrix of the current network layer and divide the weight matrix into blocks to obtain a plurality of weight blocks.
[0073] In this embodiment, the weight values of the current network layer of the deep neural network are expanded into a weight matrix, and the size of the weight matrix is: (Cin×d×d)×Cout, Cin is the number of input channels, Cout is the number of output channels, and d is the width and height of the convolution kernel; when the size of the weight block is m*n, the number of weight blocks is (Cin×d×d / m)×(Cout / n).
[0074] See also Figure 2 , take Cin=2,d=2,Cout=4,m=2,n=2 as an example, the size of the convolution kernel of the current network layer is 2*2, the input channel is 2, and the output channel is 4, then the weight value of the current network layer is expanded into an 8*4 weight matrix; and the 8*4 weight matrix is divided into multiple 2*2 weight blocks (see Figure 2 (Figure on both sides of arrow 1).
[0075] The reorganization module 72 is used to reorganize the absolute values of weights in multiple weight blocks in column order to obtain multiple target weight blocks.
[0076] In this embodiment, each weight block is divided into multiple weight sub-blocks by rows, the weight values of multiple weight sub-blocks in the same column are sorted according to the size of their absolute values, and the weight sub-blocks with similar weight values are reorganized to obtain multiple target weight blocks, and the weight values of the target weight blocks are similar.
[0077] See also Figure 2 , divide the 2*2 weight block into 1*2 weight sub-blocks, and obtain the absolute value of the weight value of each column of the weight sub-block (see Figure 2 Then, the weight sub-blocks in each column are sorted according to their absolute values, and two similar 1*2 weight sub-blocks are reorganized into a 2*2 target weight block (see Figure 2 (see Figure 3 on both sides of arrow 3).
[0078] The quantization accuracy determination module 73 is used to determine the quantization accuracy of each target weight block according to the quantization deviation and the initial quantization threshold of each target weight block.
[0079] In this embodiment, the quantization accuracy determination module 73 obtains the quantization deviation of each target weight block, and the calculation formula of the quantization deviation is: Wherein, W is the original weight value in the weight block; Wq is the weight value of the weight block after quantization with the first precision. The quantization precision determination module 73 compares the quantization deviation of each target weight block with the quantization threshold. When the quantization deviation of the target weight block is less than the quantization threshold, the quantization precision of the target weight block is the first precision; when the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization precision of the target weight block is the second precision, wherein the second precision is greater than the first precision. In this embodiment, the first precision is INT4 and the second precision is INT8, but it is not limited to this.
[0080] The quantization precision selection module 74 is used to adjust the initial quantization threshold according to the weight distribution deviation of the multiple target weight blocks before and after quantization to obtain the target quantization precision of the multiple target weight blocks.
[0081] In this embodiment, the quantization precision selection module 74 includes a distribution deviation unit 741 , a judgment unit 742 , a quantization precision selection unit 743 and a threshold adjustment unit 744 .
[0082] Among them, the distribution deviation unit 741 is used to obtain the weight distribution deviation of multiple target weight blocks before and after quantization; the judgment unit 742 is used to judge whether the weight distribution deviation is less than the distribution threshold; the quantization accuracy selection unit 743 is used to use the current quantization accuracy of each target weight block as the target quantization accuracy of each target weight block when the weight distribution deviation is less than the distribution threshold; the threshold adjustment unit 744 is used to use the binary method to adjust the initial quantization threshold to obtain the quantization threshold until the weight distribution deviation is less than the distribution threshold when the weight distribution deviation is greater than or equal to the distribution threshold; the quantization accuracy determination module 73 is also used to redetermine the quantization accuracy of each target weight block according to the new quantization threshold.
[0083] The quantization module 75 is used to quantize the multiple target weight blocks according to the target quantization precision of the multiple target weight blocks to obtain multiple quantized weight blocks.
[0084] The mapping module 76 is used to map the multiple quantization weight blocks to the multiple cross array units by adopting a bit-interleaved mapping method.
[0085] In this embodiment, different bits of the same weight value are respectively mapped to the same position of a plurality of cross array units.
[0086] See also Figure 6The storage and calculation device includes a plurality of storage and calculation modules, and the weight value of each network layer is mapped to a corresponding storage and calculation module. Each storage and calculation module includes a first storage and calculation unit, a second storage and calculation unit, a shifter, an adder and a shift accumulator. The first storage and calculation unit and the second storage and calculation unit each include 4 cross array units, and the peripheral circuit of each cross array unit includes a digital-to-analog converter, an analog-to-digital converter and a shifter. Taking the quantization accuracy of the target weight block as INT4 as an example, the quantization weight block corresponding to the target weight block includes four weight values X1-X4, each weight value is 4 bits, and different bits of each weight value are sequentially mapped to the same position of different cross arrays. For example, X1-b0 is mapped to a certain position of cross array unit 1, then X1-b1 is mapped to the same position of cross array unit 2, X1-b2 is mapped to the same position of cross array unit 3, and X1-b3 is mapped to the same position of cross array unit 4. Taking the quantization accuracy of the target weight block as INT8 as an example, the target weight block includes four weight values X5-X8, each weight value is 8 bits, and different bits of each weight value are mapped to the same position of different cross arrays in turn. For example, X5-b0 is mapped to a certain position of cross array unit 1, then X5-b1 is mapped to the same position of cross array unit 2, X5-b2 is mapped to the same position of cross array unit 3, X5-b3 is mapped to the same position of cross array unit 4, X5-b4 is mapped to the same position of cross array unit 5, X5-b5 is mapped to the same position of cross array unit 6, X5-b6 is mapped to the same position of cross array unit 7, and X5-b7 is mapped to the same position of cross array unit 8.
[0087] The shift control module 77 is used to control the shift operation of the output results of multiple cross array units according to the quantization precision of different quantization weight blocks.
[0088] In this embodiment, when the quantization accuracy of the quantization weight block is the first accuracy, the output results of multiple cross array units are directly output; when the quantization accuracy of the quantization weight block is the second accuracy, the output results of some cross array units are shifted and merged with the output results of other cross array units before output.
[0089] See also Figure 6, if the quantization accuracy of the target weight block is INT4, that is, the weight data of the target weight block is mapped to the 4 cross array units in the first storage unit; or mapped to the 4 cross array units in the second storage unit, the output results of the 4 cross array units in the first storage unit or the output results of the 4 cross array units in the second storage unit can be directly output. If the quantization accuracy of the target weight block is INT8, that is, the weight data of the target weight block is respectively mapped to the 4 cross array units in the first storage unit and the 4 cross array units in the second storage unit, if the upper 4 bits are mapped to the first storage unit and the lower 4 bits are mapped to the second storage unit, then the output results of the 4 cross array units in the first storage unit need to be shifted, and the output results of the 4 cross array units in the second storage unit are not shifted and then merged into 8-bit data for output.
[0090] The model quantization device provided by the present invention performs quantization through weight blocks, and can flexibly select the bit width of the weights, thereby reducing the precision loss while ensuring the compression efficiency.
[0091] Furthermore, the present application adopts an offline quantization method to quantize the model, which can avoid expensive retraining and has good performance in terms of accuracy and energy consumption. Furthermore, the absolute values of weights in multiple weight blocks are reorganized by column sorting to obtain multiple target weight blocks, which can make the weight values in the target weight blocks closer and reduce the quantization error.
[0092] Furthermore, the quantization accuracy of each target weight block is determined according to the quantization deviation and quantization threshold of each target weight block, and the quantization threshold is adjusted through the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain the optimal quantization threshold, which can achieve a balance between the accuracy of model inference and hardware.
[0093] Furthermore, the quantization threshold is adjusted by using the binary method, and the quantization threshold is adjusted by the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization to obtain a suitable quantization threshold. This can quickly determine the optimal quantization threshold and improve the efficiency of the quantization strategy search.
[0094] Furthermore, a plurality of quantized weight blocks are mapped to a plurality of cross array units in a bit-interleaved mapping manner, that is, different bits of the same weight value are respectively mapped to the same position of a plurality of cross array units, which can reduce peripheral circuit overhead.
[0095] Figure 8 Schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Figure 8 As shown, the electronic device 100 of this embodiment includes: at least one processor 101 ( Figure 8Only one is shown in the figure) a processor, a memory 102, and a computer program 103 stored in the memory 102 and executable on the at least one processor 101, and when the processor 101 executes the computer program 103, the steps of the above-mentioned model quantization method are implemented.
[0096] The electronic device may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The electronic device may include, but is not limited to, a processor and a memory. Those skilled in the art will understand that Figure 8 This is merely an example of the electronic device 100 and does not constitute a limitation on the electronic device 100 . The electronic device 100 may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, it may also include input and output devices, network access devices, etc.
[0097] The processor 101 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0098] In some embodiments, the memory 102 may be an internal storage unit of the electronic device 100, such as a hard disk or memory of the electronic device 100. In other embodiments, the memory 102 may also be an external storage device of the electronic device 100, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the electronic device 100. Further, the memory 102 may also include both an internal storage unit of the electronic device 100 and an external storage device. The memory 102 is used to store an operating system, an application program, a boot loader (Boot Loader), data, and other programs, such as the program code of the computer program, etc. The memory 102 may also be used to temporarily store data that has been output or is to be output.
[0099] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.
[0100] An embodiment of the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device can implement the steps in the above-mentioned method embodiments when executing the computer program product.
[0101] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned various method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device that can carry the computer program code to the device / electronic device, a recording medium, a computer memory, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), an electric carrier signal, a telecommunication signal, and a software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.
[0102] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0103] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0104] In the embodiments provided in the present application, it should be understood that the disclosed devices / electronic devices and methods can be implemented in other ways. For example, the device / electronic device embodiments described above are merely schematic. For example, the block division of the modules or units is only a logical function block division. There may be other block division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0105] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0106] According to the embodiments of the present invention as described above, these embodiments do not describe all the details in detail, nor do they limit the invention to the specific embodiments described. Obviously, many modifications and changes can be made based on the above description. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present invention, so that those skilled in the art can make good use of the present invention and the modified use based on the present invention. The present invention is limited only by the claims and their full scope and equivalents.
Claims
1. A model quantization method, characterized in that: include: Get the weight matrix of the current network layer, and divide the weight matrix into multiple weight blocks; The absolute values of weights in multiple weight blocks are reorganized by sorting the columns to obtain multiple target weight blocks; Determine the quantization accuracy of each target weight block according to the quantization deviation and quantization threshold of each target weight block, wherein when the quantization deviation of the target weight block is less than the quantization threshold, the quantization accuracy of the target weight block is the first accuracy; when the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization accuracy of the target weight block is the second accuracy; The quantization threshold is adjusted according to the weight distribution deviation and distribution threshold of the multiple target weight blocks before and after quantization to obtain the target quantization accuracy of the multiple target weight blocks; Quantizing the multiple target weight blocks according to the target quantization precision of the multiple target weight blocks to obtain multiple quantized weight blocks; A plurality of quantized weight blocks are mapped to a plurality of cross array units by adopting a bit-interleaved mapping method, wherein the bit-interleaved mapping method is to map different bits of the same weight value to the same position of the plurality of cross array units respectively; Controlling the shifting operation of the output results of the plurality of cross array units according to the quantization precision of different quantization weight blocks; When the quantization accuracy of the quantization weight block is the first accuracy, directly outputting the output results of the plurality of cross array units; When the quantization accuracy of the quantization weight block is the second accuracy, the output results of some cross array units are shifted and combined with the output results of other cross array units before output.
2. The model quantization method according to claim 1, characterized in that: According to the weight distribution deviation and distribution threshold of multiple target weight blocks before and after quantization, the quantization threshold is adjusted to obtain the target quantization accuracy of multiple target weight blocks, including: Obtain weight distribution deviations of multiple target weight blocks before and after quantization; Determining whether the weight distribution deviation is less than a distribution threshold; When the weight distribution deviation is less than the distribution threshold, the current quantization accuracy of each target weight block is used as the target quantization accuracy of each target weight block; When the weight distribution deviation is greater than or equal to the distribution threshold, the quantization threshold is adjusted by using a dichotomy method to obtain a new quantization threshold; Re-determine the quantization accuracy of each target weight block according to the new quantization threshold; The above steps are repeated until the weight distribution deviation is less than the distribution threshold.
3. The model quantization method according to claim 1, characterized in that: The calculation formula of the quantization deviation is: , where W is the original weight value in the target weight block; Wq is the quantized weight value of the target weight block.
4. The model quantization method according to claim 1, characterized in that: The second precision is greater than the first precision.
5. A model quantization device, characterized in that: include: The block module is used to obtain the weight matrix of the current network layer and divide the weight matrix into multiple weight blocks; A reorganization module, used for reorganizing the absolute values of weights in multiple weight blocks in column order to obtain multiple target weight blocks; A quantization accuracy determination module, used to determine the quantization accuracy of each target weight block according to the quantization deviation of each target weight block and an initial quantization threshold, when the quantization deviation of the target weight block is less than the quantization threshold, the quantization accuracy of the target weight block is the first accuracy; When the quantization deviation of the target weight block is greater than or equal to the quantization threshold, the quantization accuracy of the target weight block is the second accuracy; A quantization accuracy selection module, used to adjust the initial quantization threshold according to the weight distribution deviation of multiple target weight blocks before and after quantization to obtain target quantization accuracy of multiple target weight blocks; A quantization module, used for quantizing the multiple target weight blocks according to the target quantization precisions of the multiple target weight blocks to obtain multiple quantized weight blocks; A mapping module, used for mapping a plurality of quantized weight blocks to a plurality of cross array units by adopting a bit-interleaved mapping method, wherein the bit-interleaved mapping method is to map different bits of the same weight value to the same position of the plurality of cross array units respectively; A shift control module is used to control the shift operation of the output results of multiple cross array units according to the quantization precision of different quantization weight blocks; when the quantization precision of the quantization weight block is the first precision, the output results of the multiple cross array units are directly output; when the quantization precision of the quantization weight block is the second precision, the output results of some cross array units are shifted and the output results of other cross array units are combined and output.
6. The model quantization device according to claim 5, characterized in that: The quantization accuracy selection module includes: A distribution deviation unit, used to obtain weight distribution deviations of multiple target weight blocks before and after quantization; A judging unit, used to judge whether the weight distribution deviation is less than a distribution threshold; A quantization precision selection unit, configured to use the current quantization precision of each target weight block as the target quantization precision of each target weight block when the weight distribution deviation is less than the distribution threshold; A threshold adjustment unit, used to adjust the initial quantization threshold by using a dichotomy method to obtain a quantization threshold when the weight distribution deviation is greater than or equal to the distribution threshold; The quantization accuracy determination module is also used to re-determine the quantization accuracy of each target weight block according to the new quantization threshold.
7. The model quantization device according to claim 5, characterized in that: The calculation formula of the quantization deviation is: , where W is the original weight value in the target weight block; Wq is the quantized weight value of the target weight block.
8. The model quantization device according to claim 5, characterized in that: The second precision is greater than the first precision.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the model quantization method according to any one of claims 1 to 4 is implemented.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the model quantization method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Neural network mapping method based on resistive random access memory and accelerator
CN112070204A
Neural network model quantification method, device and system, electronic equipment and storage medium
CN113902114A
Dynamic adaptation of deep neural networks
US20200134461A1
