Method and device for calculating convolution in neural network
The accuracy of neural network calculations for convolution is improved through quantization and left shift operations, which solves the problem of insufficient accuracy under standard bit width and achieves high-precision calculations without increasing energy consumption.
Patent Information
- Application Number
- CN202310331543.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-24
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2043-03-24
AI Technical Summary
Existing neural network models are difficult to obtain high accuracy under standard bit width, and most hardware does not support fixed-point calculations of non-standard bits, resulting in insufficient accuracy and increased energy consumption when calculating convolution.
By quantizing the first tensor into a K-bit binary number, and the second tensor into a K+1-bit binary number, and performing multiplication and left shift operations, the addition result is generated to improve the convolution calculation accuracy, and only requires additional additions The 1-bit unit supports the calculation of the convolution of the highest bit of the first tensor and the lowest bit of the second tensor.
It improves the accuracy of convolution calculation results and meets business requirements for accuracy without increasing the energy consumption of computing equipment.
Smart Images

Figure CN118689448A
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of artificial intelligence technology, and more specifically to a method and device for calculating convolution in a neural network. Background Art
[0002] In recent years, deep learning models have been widely used in various fields such as computer vision, natural language processing, search recommendation and advertising. At present, most of the neural network models deployed on computing devices use quantized models to reduce data storage pressure, thereby reducing the energy consumption of computing devices. However, computing devices usually only provide standard computing units such as 8 bits and 16 bits to calculate convolution. However, many business models are difficult to obtain relatively high accuracy under standard bit widths, so some supplements to standard bits are needed to obtain sufficient accuracy. However, if the standard bits are supplemented, it will become a non-standard bit calculation. However, most hardware structures do not support fixed-point calculations of non-standard bits when calculating convolution.
[0003] Therefore, a method for calculating convolution in a neural network is needed that can meet the accuracy requirements of the business model without increasing the energy consumption of the computing device and improve the user's business experience. Summary of the invention
[0004] The present application provides a method for calculating convolution in a neural network, which can not only reduce the computing energy consumption and storage energy consumption of computing equipment, but also meet business needs, so that the calculation results can obtain relatively high accuracy.
[0005] In a first aspect, a method for calculating convolution in a neural network is provided. The method can be performed by a process, container, unit, module, or computing device capable of calculating convolution; or, it can also be performed by a component capable of calculating convolution (for example, a chip or circuit), without limitation.
[0006] The method includes: obtaining a first tensor and a second tensor, wherein the first tensor represents a weight parameter in a convolution operation of a neural network, and the second tensor represents an activation value parameter in a convolution operation of a neural network; based on the fixed-point computing capability of a processor, quantizing the first tensor into a K-bit binary number, and quantizing the second tensor into a K+1-bit binary number, wherein K is a standard bit width of quantization; performing a first processing on the first number and the third number to obtain a first value, wherein the first processing includes a multiplication operation, wherein the first number is the K-bit binary number, and the K-bit binary number is used to represent the integer part of the first tensor, and the second number is the highest bit of the K-bit binary number ; performing a second processing on the second number and the fourth number to obtain a second value, the second processing comprising first performing a multiplication operation and then shifting the result of the multiplication operation left by K-2 bits, wherein the third number is the value of the high K bits of the K+1-bit binary number, the high K bits of the K+1-bit binary number are used to represent the integer part of the second tensor, and the fourth number is the value of the lowest bit of the K+1-bit binary number, and the value of the lowest bit is used to represent the decimal part of the quantized second tensor; adding the first value to the second value to generate an addition operation result, wherein the addition operation result is a convolution operation result of the first tensor and the second tensor.
[0007] In the present application, the first tensor or the second tensor may be obtained based on input image information, or video information, or text information, or sound information, or a combination of the above training samples. For example, the image information may be processed to obtain the first tensor and the second tensor.
[0008] In the present application, for example, the value of K is specifically one of 2, 4, 8, and 16. Correspondingly, the value of K+1 can be specifically one of 3, 5, 9, and 17.
[0009] In this application, the range of K+1 binary numbers is: [0,2 K -1+2 -1 ], or the range of K+1 binary numbers is: [-2 K-1 ,2 K-1 -1+2 -1 ].
[0010] In the present application, K+1 is a binary number that can represent a 1-bit decimal part, thereby compensating for a portion of the precision.
[0011] Based on the above technical solution, by quantizing the first tensor to K standard bits and quantizing the second tensor to K+1 bits, where the value on the lowest bit of K+1 bits can represent the decimal part of the quantized second tensor, only an additional 1-bit unit is needed to calculate the convolution of the two 1-bit binary numbers of the highest bit of the first tensor and the lowest bit of the second tensor, so as to improve the accuracy of the entire convolution calculation result, which not only ensures the accuracy requirements of various business models in the neural network, but also does not increase the energy consumption of the computing device.
[0012] In a possible implementation, the operation result is determined according to the first value and the second value, and the method further includes: dequantizing the convolution operation result to obtain a dequantized addition operation result.
[0013] Based on the above technical solution, in the present application, when performing specific calculations, the first value and the third value can be dequantized respectively, and then the dequantized values can be added and the results can be output.
[0014] In a second aspect, a computing device is provided, the device being used to execute the method in any possible implementation of any aspect of the first aspect. Specifically, the device may include units and / or modules for executing the method in any possible implementation of the first aspect, for example, an acquisition unit, a quantization unit, a first computing unit, a second computing unit, and a processing unit.
[0015] In one implementation, the apparatus is a computing device for calculating convolution. When the apparatus is a computing device, the communication unit may be a transceiver, or an input / output interface; the processing unit may be at least one processor. Optionally, the transceiver may be a transceiver circuit. Optionally, the input / output interface may be an input / output circuit.
[0016] In another implementation, the device is a chip, a chip system or a circuit of a computing device capable of computing a convolution function. When the device is a chip, a chip system or a circuit for computing a convolution function, the communication unit may be an input / output interface, an interface circuit, an output circuit, an input circuit, a pin or a related circuit on the chip, the chip system or the circuit; the processing unit may be at least one processor, a processing circuit or a logic circuit.
[0017] In a third aspect, a computing device is provided, the device comprising: at least one processor, configured to execute a computer program or instruction stored in a memory, so as to execute the method in any possible implementation of the first aspect. Optionally, the device further comprises a memory, configured to store a computer program or instruction. Optionally, the device further comprises a communication interface, and the processor reads the computer program or instruction stored in the memory through the communication interface.
[0018] In one implementation, the apparatus is a computing device including a computational convolution module.
[0019] In another implementation, the device is a chip, a chip system or a circuit having a computational convolution function.
[0020] In a fourth aspect, the present application provides a processor, comprising: an input circuit, an output circuit, and a processing circuit. The processing circuit is used to receive a signal through the input circuit and transmit a signal through the output circuit, so that the processor executes the method in any possible implementation of the first aspect.
[0021] In the specific implementation process, the above-mentioned processor can be one or more chips, the input circuit can be an input port, the output circuit can be an output port, and the processing circuit can be a transistor, a gate circuit, a trigger, and various logic circuits. The input signal received by the input circuit can be received and input by, for example, but not limited to, a transceiver, and the signal output by the output circuit can be, for example, but not limited to, output to a transmitter and transmitted by the transmitter, and the input circuit and the output circuit can be the same circuit, which is used as an input circuit and an output circuit at different times. The embodiments of the present application do not limit the specific implementation methods of the processor and various circuits.
[0022] For the operations such as sending and acquiring / receiving involved in the processor, unless otherwise specified, or unless they conflict with their actual function or internal logic in the relevant description, they can be understood as operations such as processor output, reception, input, etc., or as sending and receiving operations performed by the radio frequency circuit and antenna, and this application does not limit this.
[0023] In a fifth aspect, a processing device is provided, comprising a processor and a memory. The processor is used to read instructions stored in the memory, and can receive signals through a transceiver and transmit signals through a transmitter to execute the method in any possible implementation of the first aspect.
[0024] Optionally, the number of the processors is one or more, and the number of the memories is one or more.
[0025] Optionally, the memory may be integrated with the processor, or the memory may be provided separately from the processor.
[0026] In the specific implementation process, the memory can be a non-transitory memory, such as a read-only memory (ROM), which can be integrated with the processor on the same chip or can be set on different chips respectively. The embodiments of the present application do not limit the type of memory and the setting method of the memory and the processor.
[0027] It should be understood that the related data interaction process, such as sending indication information, can be a process of outputting indication information from the processor, and receiving capability information can be a process of receiving input capability information by the processor. Specifically, the data output by the processor can be output to the transmitter, and the input data received by the processor can come from the transceiver. Among them, the transmitter and the transceiver can be collectively referred to as a transceiver.
[0028] The processing device in the fifth aspect may be one or more chips. The processor in the processing device may be implemented by hardware or by software. When implemented by hardware, the processor may be a logic circuit, an integrated circuit, etc.; when implemented by software, the processor may be a general-purpose processor implemented by reading software code stored in a memory, which may be integrated in the processor or located outside the processor and exist independently.
[0029] In a sixth aspect, a computer-readable storage medium is provided, which stores a program code for execution by a device, wherein the program code includes a method for executing any possible implementation of the first aspect described above.
[0030] In a seventh aspect, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute the method in any possible implementation of the first aspect.
[0031] In an eighth aspect, a chip system is provided, comprising a processor for calling and running a computer program from a memory, so that a device equipped with the chip system executes the methods in each implementation manner in the above-mentioned first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 It is a schematic diagram of a system framework applicable to this application.
[0033] Figure 2 It is a schematic flowchart of a method 200 for calculating convolution in a neural network provided by the present application.
[0034] Figure 3 This is a schematic diagram of a newly added 1-bit Cube computing unit provided in this application.
[0035] Figure 4 It is another schematic flowchart of a method 200 for calculating convolution in a neural network provided by the present application.
[0036] Figure 5 It is a schematic block diagram of a computing device 100 provided in the present application.
[0037] Figure 6It is a schematic block diagram of a computing device 200 provided in the present application. DETAILED DESCRIPTION
[0038] The technical solutions in the embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0039] In order to facilitate understanding of the technical solution of the present application, the professional terms involved in the present application are explained below.
[0040] 1. Tensor in Neural Networks
[0041] A "tensor" can be understood as a multidimensional array, which usually contains the following three key properties: dimension, shape, and data type. A "0-dimensional tensor" can be understood as a scalar, which is a real number that can be either positive or negative. For example, mass, volume, temperature, etc. can be understood as 0-dimensional tensors. A "1-dimensional tensor" can be understood as a vector. A vector is an array composed of numbers. It can contain multiple numbers but has only one dimension. A "2-dimensional tensor" can be understood as a matrix. A matrix has two dimensions, namely rows and columns. By combining multiple matrices into a new array, you can get a 3-dimensional tensor. By combining multiple 3-dimensional tensors into a new array, you can get a 4-dimensional tensor.
[0042] The “tensor” mentioned in this application is not limited in its dimension and can be a “0-dimensional tensor”, “1-dimensional tensor”, “2-dimensional tensor”, “4-dimensional tensor”, etc.
[0043] Exemplarily, the tensor in the present application can be understood as the "weight" of a neural network, or the "activation value" in a neural network.
[0044] 2. Fixed-point numbers (int) and floating-point numbers (float)
[0045] "Fixed-point numbers" and "floating-point numbers" refer to whether the position of the decimal point of a number in a computer is fixed or floating. Specifically, if the position of the decimal point in a number is fixed, it is a "fixed-point number"; if the position of the decimal point in a number is floating, it is a "floating-point number". Generally speaking, the range of values that can be represented by the fixed-point format is limited, but the required processing hardware is relatively simple, while the range of values that can be represented by the floating-point format is large, but the required processing hardware is relatively complex.
[0046] There are two commonly used representations of fixed-point numbers: if the decimal point is agreed to be after the lowest numerical digit, the number can only be a fixed-point integer; if the decimal point is agreed to be before the highest numerical digit, the number can only be a fixed-point decimal. The position of the decimal point of a "fixed-point number" is agreed upon and fixed in computer storage. The integer part and decimal part of a decimal are converted into binary representations respectively. For example, the decimal 25.125, the integer part "25" is represented by binary as: 11001, and the decimal part "0.125" is represented by binary as: .001, so 11001.001 is used together to represent the decimal 25.125. Therefore, in an 8-bit computer, the first 5 digits represent the integer part of a decimal, and the last 3 digits represent the decimal part. The decimal point defaults to the position after the fifth digit. In fact, the computer will not store the decimal point, which is agreed in advance.
[0047] "Floating point numbers" are used in computers to represent decimals, similar to the use of scientific notation in mathematics to represent larger numbers. For example: 352.47 = 3.5247 × 10 2 The decimal number 178.125 is converted to binary as "10110010.001", which can also be expressed as: 1.0110010001×2 111 (That is, 1.0110010001 multiplied by 2 to the seventh power can also be understood as the decimal point shifted right by 7.) The part "10110010001" is called the mantissa (M), the part "111" is called the exponent (P), and the positive and negative are called the sign (S): 0 for a positive number and 1 for a negative number. Therefore, a floating point number can be represented using three parts: the sign (S), the exponent (P), and the mantissa (M).
[0048] From this we can see that the data range that floating-point numbers can represent is much larger than the data range that fixed-point numbers can represent, and the accuracy is higher than fixed-point numbers. However, when calculating floating-point numbers, the calculation of the "exponent part" and the calculation of the "mantissa part" must be divided, and the calculation results require normalization. Therefore, floating-point calculation steps are more than fixed-point calculation steps, and the calculation speed is lower than fixed-point calculation speed.
[0049] 3. Quantization
[0050] Deep learning has been shown to work well on tasks including image classification, object detection, and natural language processing. A large number of applications are equipped with deep learning algorithms related to images (computer vision). As model predictions become more accurate and networks become deeper, the memory size consumed by neural networks becomes a problem, especially on mobile devices. Mobile phones in early 2019 are generally equipped with 4GB of memory to support the simultaneous operation of multiple applications, and three models usually take up 1GB of memory when running at once. Model size is not only a problem of memory capacity, but also a problem of memory bandwidth. The model uses the model's weights every time it makes a prediction, and image-related applications usually need to process data in real time. When the network is running, memory, central processing unit (CPU), and battery will be consumed rapidly, and it is necessary to consider the relative balance between the intelligence of mobile devices and energy consumption.
[0051] Therefore, the field of deep learning has invested a lot of research resources on these issues, mainly in two aspects: on the one hand, it is expected to design a more effective network architecture and achieve acceptable accuracy with a relatively small model size; on the other hand, it is expected to reduce the size of the network through compression, encoding, etc., and "quantization" is one of the most widely used compression methods. For example, the lightweight grid (mobilenetV1) quantized by the symbolic mathematics system (tensorflow) is only 4.8MB, which is smaller than most GIF animations, so it can be easily deployed on any mobile platform.
[0052] Through the above introduction, the "quantization" in this application can be understood as mapping the tensor stored in the floating-point number of the neural network to the fixed-point number domain, so as to realize the real-time operation of the network on the mobile device. It can also be understood that if some floating-point numbers can be converted to fixed-point representation, multiple can be read and run each time under the premise of accepting the loss of precision, which can significantly improve the operation efficiency.
[0053] For example, if a 4-bit fixed-point number is a signed number, its representation range is [-8, 7]. For example, the floating-point number 2.78 can be quantized as the fixed-point number "3", that is, "0011"; the floating-point number -7.8 can be quantized as the fixed-point number "-8", that is, "1000". For another example, if a 4-bit fixed-point number is an unsigned number, its representation range is [0, 15]. The floating-point number 6.12 can be quantized as the fixed-point number "6", that is, "0110"; the floating-point number 16.12 can be quantized as the fixed-point number "15", that is, "1111".
[0054] Correspondingly, "dequantization" means restoring the fixed-point results calculated by each computing node in the quantized neural network to the floating-point domain.
[0055] 4. Model Quantization
[0056] "Model quantization" means: converting the weights of the neural network and some or all of the intermediate calculation results (i.e., feature-map) in the reasoning process to the fixed-point domain for representation. For example, the weights in the original model are all float32. Through model quantization, the model is converted into a fixed-point model with weights (weights) all int8. It can also be understood that model quantization is to establish a mapping relationship between floating-point data and fixed-point data. Usually, the fixed-point numbers converted from floating-point numbers are 2bit (int2), 4bit (int4), 8bit (int8), and 16bit (int16). For example, the weights of operators in the network are quantized with 8bit, and the activation values are quantized with 8bit; or, the weights of operators in the network are quantized with 8bit, and the activation values are quantized with 16bit; or, the weights of operators in the network are quantized with 4bit, and the activation values are quantized with 4bit.
[0057] After the model is quantized, the energy consumption of model reasoning can be significantly reduced, and the memory and storage space occupied during neural network model reasoning and storage can be reduced. However, as mentioned above, the end side usually only provides standard fixed-point computing units such as 8-bit and 16-bit to calculate convolution, and many business models are difficult to obtain relatively high accuracy under the standard bit width, and some supplementation of the standard bit is required to obtain sufficient accuracy. However, if the standard bit is supplemented, it will become a non-standard bit calculation, and most hardware structures do not support non-standard bit fixed-point calculation when calculating convolution.
[0058] For edge-side neural network reasoning, when the model is deeply compressed to a non-standard number of bits, no further power consumption benefits can be obtained because there is usually no dedicated hardware computing unit to support it. In view of this, this application proposes a method for calculating convolution in a neural network, which can not only reduce the computing energy consumption and storage energy consumption of the computing device, but also meet the needs of the business, so that the calculation results can obtain relatively high accuracy.
[0059] Figure 1 A diagram showing an architecture applicable to convolution calculation in a neural network proposed in this application is shown. Figure 1(a) in it shows an end-side reasoning framework, which includes, from the outside to the inside, an interface module, an engine module, a framework module, a hardware-accelerated convolution module, a driver module, and an operating system module. Among them, the engine module may include, for example, a health engine; the framework module may include model management, for example, a model generator and a model executor; the hardware-accelerated convolution layer may include, for example, a neural network processor convolution layer, a vector convolution layer, and a central processing unit convolution layer; the driver module may include, for example, an interface, power supply, task management, memory, and a new design for non-functional products; the operating system module may include, for example, a Linux operating system, a Tee operating system, and other operating systems. The present application focuses on the neural network processor convolution module in the hardware-accelerated convolution module; as Figure 1 As shown in (b) in the figure, the memory tagging extension (MTE) unit can control the hardware unit Cube. The Cube unit is used to calculate the convolution in the neural network and send it to the buffer through the artificial intelligence (AI) chip calculation instruction pipeline (Fixpipe). The buffer can send the convolution operation result to MET, and MET can exchange information with the physical layer (layer1, L1).
[0060] Assume that, in the convolution calculation, A represents the activation value and B represents the weight, then the convolution calculation of A and B can be referred to the following formula 1:
[0061]
[0062] The calculation formula 1 can also be regarded as a deformation of the following formula (2):
[0063]
[0064] Among them, A can be understood as the integer part of the activation value, It can be understood as the decimal part of the activation value, and B can be understood as the integer part of the weight. It can be understood as the fractional part of the weight. It can be understood as the result of the convolution of the integer part of the weight and the integer part of the activation value. It can be understood as the accuracy corresponding to the weight. It can be understood as the precision corresponding to the activation value. For example, Figure 1 The Cube unit in (b) can be used to calculate It can also be understood that the existing calculation method only calculates the first term in Formula 1, so some accuracy is lost and it cannot meet business needs.
[0065] As mentioned above, existing hardware only supports standard bit calculations, so when performing convolution calculations, only The result of the calculation loses the precision of the weights and the precision of the activation values.
[0066] Figure 2 is a schematic flow chart of a method 100 for calculating convolution in a neural network provided by the present application, such as Figure 2 As shown, the method includes:
[0067] Step 201, obtaining a first tensor and a second tensor.
[0068] In the present application, for example, the first tensor represents a weight parameter in a convolution operation of a neural network, and the second tensor represents an activation value parameter in a convolution operation of a neural network.
[0069] In the present application, the first tensor or the second tensor may be obtained based on input image information, or video information, or text information, or sound information, or a combination of the above training samples. For example, the image information may be processed to obtain the first tensor and the second tensor.
[0070] Step 202 , based on the fixed-point computing capability of the processor, quantize the first tensor into a K-bit binary number, and quantize the second tensor into a K+1-bit binary number.
[0071] In the present application, K is a standard bit width of quantization, for example, the value of K is 2 bits, 4 bits, 8 bits or 16 bits.
[0072] As mentioned above, in the convolution calculation of the neural network, the AI Core on the end side only provides standard fixed-point computing units such as 2-bit, 4-bit, 8-bit, and 16-bit. Therefore, in order to facilitate subsequent fixed-point calculations, the first tensor can be quantized to a standard bit width based on the fixed-point calculation capability of the processor.
[0073] In the present application, K+1 can be a non-standard bit width of quantization. For example, it can be understood that the activation value is quantized to 3 bits; or, the activation value is quantized to 5 bits; or, the activation value is quantized to 9 bits; or, the activation value is quantized to 17 bits. It can also be understood that the precision of the second tensor is compensated in the present application. For example, the precision of the activation value is compensated.
[0074] Step 203: Perform a first process on the first number and the third number to obtain a first value, wherein the first process includes a multiplication operation.
[0075] In the present application, the first number is a K-bit binary number, which is used to represent the integer part of the first tensor, and the second number is the value of the highest bit of the K-bit binary number.
[0076] Specifically, the following formula (3) can be used for quantification:
[0077]
[0078] Among them, W float Represents the original floating-point domain weight of the neural network; s represents the scaling factor when the weight is quantized, which is predefined; min and max represent the maximum and minimum values after quantization, respectively; Indicates rounding. For example, for 4-bit quantization, [min, max] = [-8, 7]; Represents the weight represented by fixed-point numbers after quantization.
[0079] The following is an example of an unsigned number:
[0080] Example 1: Assume that the weight is a decimal number "9.0" and is quantized to 4 bits, that is, the value of K is 4. Assume that s is 1, then "9" is used as a 4-bit binary number, which can be expressed as "1001". That is, it can be determined that the first number is the binary number "1001", and because the value of the highest bit of the 4-bit binary number is "1", the second number determined is the binary number "1".
[0081] Example 2: Assume the weight is a decimal number "10.6" and is quantized to 4 bits, that is, the value of K is 4. Assume s is 0.5, then Since a 4-bit binary number can represent a maximum of 15 decimal numbers, it can only be quantized as the decimal number 15. "15" can be represented as "1111" as a 4-bit binary number. That is, it can be determined that the first number is the binary number "1111", and since the value of the highest bit of the 4-bit binary number is "1", the second number determined is the binary number "1".
[0082] It should be understood that the process is similar when quantization is performed to standard bit positions of 2 bits, 8 bits or 16 bits, and this application will not give any examples.
[0083] In the above example 1, assuming that the weight is the decimal number "9.0", the first number is the binary number "1001"; assuming that the activation value is the decimal number "11.5", the third number is the binary number "1011", and the convolution operation of the two binary numbers can be understood as the multiplication of two binary numbers, and the result is "1100011" (example of the first value), which represents the decimal number 99.
[0084] In the above example 2, assuming that the weight is the decimal number "10.6", the first number is the binary number "1111"; assuming that the activation value is the decimal number "8.3", the third number is the binary number "1000". The convolution operation of the two binary numbers can be understood as the multiplication of two binary numbers, and the result is "1111000" (example of the first value), which represents the decimal number 120.
[0085] Step 204, performing a second process on the second number and the fourth number to obtain a second value, the second process comprising first performing a multiplication operation and then left shifting the result of the multiplication operation by K-2 bits.
[0086] In the present application, the third number is the high K bits of the K+1-bit binary number, and the high K bits of the K+1-bit binary number are used to represent the integer part of the second tensor; the fourth number is the value of the lowest bit of the K+1-bit binary number, and the value of the lowest bit of the K+1-bit binary number is used to represent the decimal part of the quantized second tensor.
[0087] In one possible implementation, for an unsigned number, the range of K+1-bit binary numbers is: [0,2 K -1+2 -1 In another possible implementation, for a signed number, the range represented by the K+1-bit binary number is: [-2 K-1 ,2 K-1 -1+2 -1 ].
[0088] It should be understood that, for a binary number, the "high K bits" refer to the K bits of the binary number from left to right, the lowest bit refers to the rightmost binary number, and the highest bit refers to the leftmost binary number.
[0089] Example 1: Assume that the activation value is the decimal number "11.5" and is quantized to 5 bits, that is, the value of K+1 is 5. Assume that s is 1, then "11.5" is used as a 5-bit binary number, which can be expressed as "1011.1". That is, it can be determined that the third number is the binary number "1011". Since the value of the lowest bit of the 5-bit binary number is "1", the fourth number determined is the binary number "1". It can also be understood that the present application proposes that the activation value can be expressed as a K+1-bit binary number, in which the high K bits are used to represent the integer part and the lowest bit represents the decimal part.
[0090] Example 2: Assume that the activation value is the decimal number "8.3", quantized to 5 bits, that is, the value of K+1 is 5. Assume that s is 1, then "8.3" is a 5-bit binary number, which can be expressed as "1000.1". That is, it can be determined that the third number is the binary number "1000", and because the value of the lowest bit of the 5-bit binary number is "1", the fourth number is determined to be the binary number "1".
[0091] It can be seen from the above examples 1 and 2 that in the present application, since the second tensor is quantized to K+1 bits and the lowest bit represents the decimal part of the quantization, rounding is no longer required during the quantization process of the second tensor.
[0092] In the above example 1, assuming the weight is the decimal number "9.0", the second number is the binary number "1"; assuming the activation value is the decimal number "11.5", the fourth number is the binary number "1" (it should be noted that the "1" here represents a decimal, that is, 2 -1 ), the convolution operation of two binary numbers can be understood as the multiplication of two binary numbers, and the result is "1" (that is, 2 -1 )), after being shifted left by two places, is "10" (an example of the second value), which represents the decimal number 2.
[0093] In the above example 2, assuming the weight is the decimal number "10.6", the second number is the binary number "1"; assuming the activation value is the decimal number "8.3", the fourth number is the binary number "1". The convolution operation of the two binary numbers can be understood as the multiplication of two binary numbers, and the result is "1". After shifting left by two places, it is "10" (the example of the second value), which represents the decimal number 2.
[0094] Step 205: Add the first value and the second value to generate an addition result.
[0095] In the present application, the addition operation result is the convolution operation result of the first tensor and the second tensor.
[0096] Step 205 can be expressed by the following formula (4):
[0097]
[0098] in, It can be understood as the calculation result of the quantized standard bit in the original AI, for example Figure 3 Cube#1 in the calculation is used; It can be understood that a 1-bit calculation unit is added in this application, which is specifically used to calculate the accuracy of compensation. For example, Figure 3 Cube#2 shown is calculated. In addition, Refers to the lowest binary digit (i.e., the decimal part) after quantizing the activation value. Refers to the highest binary number after weight quantization.
[0099] For example, in Example 1, the sum of the first value and the second value is 99+2=101, that is, the convolution operation result of the weight and the activation value in this example is 101. For Example 1, according to an existing possible convolution calculation method, the weight is the decimal number "9.0", which is rounded to 9; the activation value is the decimal number "11.5", which is rounded to 11. After multiplying the two numbers, the value obtained is 99, and the inverse quantization is still 99. From this, it can be seen that when the weight and activation value are originally convolved, the actual value should be: 9×11.5=103.5. The convolution result obtained by an existing calculation method is 99, and the convolution calculation result obtained based on the method provided in the present application is 101. Obviously, the method provided in the present application is closer to the true value. In other words, the accuracy of the convolution calculation result of the method of the present application is improved.
[0100] For another example, in Example 2, the sum of the first value and the second value is 120+2=122, that is, the convolution operation result of the weight and the activation value in this example is 121. For Example 2, according to an existing possible convolution calculation method, the weight is the decimal number "10.6", 10.6 / 0.5=21.2, which is rounded to 21 or 22, which exceeds the representation range of 4-bit binary numbers, so it is represented by 15; the activation value is the decimal number "8.3", which is rounded to 8. After multiplying the two numbers, the value obtained is 15×8=120, and the result after dequantization is 120×1×0.5=60. It can be seen from this that when the weight and activation value are originally convolved, the actual value should be: 10.6×8.3=87.98. The convolution result obtained by an existing calculation method is 60, while the convolution calculation result obtained based on the method provided in the present application is 61. It is obvious that the method provided by the present application is closer to the true value. In other words, the accuracy of the convolution calculation results of the method of the present application is improved.
[0101] Alternatively, it can also be understood that in an existing convolution calculation method, only the first term in formula (1) is calculated, so the accuracy of the convolution result is low. The present application also compensates for the value of the third term (that is, the formula (4) proposed in the present application includes the second term), thereby improving the accuracy of the convolution calculation result.
[0102] Specifically, after the convolution calculation is completed, in order to facilitate subsequent calculations, the convolution calculation result needs to be "dequantized" to convert the fixed-point number into the floating-point domain. The calculation formula for dequantization is shown in the following formula (5):
[0103]
[0104] in, Represents the weight of analog quantization, which can also be understood as the result of the convolution operation after dequantization.
[0105] For example, in Example 1, s=1, and the convolution calculation result after dequantization is 101×1=101, and in Example 2, s=0.5, and the convolution calculation result after dequantization is 122×0.5=61.
[0106] Figure 4 Another schematic flow chart of the method 200 is shown in FIG. Figure 4 As shown, taking the quantized weight as 4 bits and the quantized activation value as 5 bits as an example, the original Cube unit is used for the calculation of the standard bit of quantization, that is, it can be used to calculate two 4-bit multiplications. The additional 1-bit Cube unit added in this application can be used for the two 1-bit multiplication calculations of the highest bit of the quantized weight and the lowest bit of the quantized activation value. It should be noted that the calculation result of the newly added 1-bit Cube calculation unit in this application can be accumulated with the calculation result of the original Cube calculation unit. Finally, the calculation results of the two Cube calculation units are added by an adder, and the addition result is output.
[0107] Optional, in Figure 4 A switch unit can also be added, and the switch unit can be used to determine whether to use the method provided by the present application to supplement the accuracy. For example, in some cases, there is no need to compensate for the accuracy, and it can be understood that the switch unit is "off". At this time, the existing method only performs convolution calculations on two quantized standard bits; for example, in other cases, it is necessary to supplement the accuracy, and it can be understood that the switch unit is "on". At this time, the method of the present application is used, and it is still necessary to use the newly added 1-bit Cube unit on the right to perform additional calculations.
[0108] Exemplarily, according to the method provided in this application, for a signed number, if the first tensor is quantized to 8 bits and the second tensor is quantized to 9 bits, the value of the final convolution calculation result may be between 256 and 512. If the calculation result is greater than 512, a new Cube calculation unit can be added.
[0109] Based on the method provided by this application, only an additional 1-bit computing unit is needed to calculate the multiplication of the second number and the fourth number, so as to improve the accuracy of the entire convolution calculation result, which not only ensures the accuracy requirements of each business model in the neural network, but also does not increase the energy consumption of the computing device. In other words, the method of this application can obtain low-bit energy efficiency benefits while also obtaining higher accuracy.
[0110] It can be understood that in this application, "if" and "if" both mean that the device will make corresponding processing under certain objective circumstances, and it does not limit the time, nor does it require that the device must have a judgment action when it is implemented, nor does it mean that there are other limitations.
[0111] It should be understood that the pre-defined in the present application can be understood as defined, pre-defined, stored, pre-stored, pre-negotiated, pre-configured, solidified, or pre-burned.
[0112] Those skilled in the art should be aware that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is performed in the form of hardware or computer software driving hardware 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.
[0113] The embodiment of the present application can divide the computing device into functional modules according to the above method example. For example, each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware or in the form of software functional modules. It should be noted that the division of modules in the embodiment of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. The following is an example of dividing each functional module corresponding to each function.
[0114] Figure 5 1 is a schematic block diagram of a computing device 100 provided in an embodiment of the present application. As shown in the figure, the device 100 may include: an acquisition unit 110, a quantization unit 120, a first processing unit 130, a second processing unit 140, and a third processing unit 150.
[0115] Among them, there are an acquisition unit, a quantization unit, a first processing unit, a second processing unit, and a third processing unit, wherein the acquisition unit is used to acquire a first tensor and a second tensor, wherein the first tensor represents a weight parameter in a neural network convolution operation, and the second tensor represents an activation value parameter in a neural network convolution operation; the quantization unit is used to quantize the first tensor into a K-bit binary number and the second tensor into a K+1-bit binary number based on the fixed-point calculation capability of the processor, wherein K is a standard bit width for quantization; the first processing unit is used to perform a first processing on the first number and the third number to obtain a first value, wherein the first processing includes a multiplication operation, wherein the first number is the K-bit binary number, and the K-bit binary number is used to represent the integer part of the first tensor, The second number is the value of the highest bit of the K-bit binary number; the second processing unit is used to perform a second processing on the second number and the fourth number to obtain a second value, the second processing includes first performing a multiplication operation and then shifting the result of the multiplication operation left by K-2 bits, wherein the third number is the value of the high K bits of the K+1-bit binary number, and the high K bits of the K+1-bit binary number are used to represent the integer part of the second tensor, and the fourth number is the value of the lowest bit of the K+1-bit binary number, and the value of the lowest bit is used to represent the decimal part of the quantized second tensor; the third processing unit is used to add the first value and the second value to generate an addition operation result, wherein the addition operation result is the convolution operation result of the first tensor and the second tensor.
[0116] In a possible implementation, the third processing unit is further used to dequantize the convolution operation result to obtain the dequantized convolution operation result.
[0117] It should also be understood that the apparatus 100 herein is embodied in the form of a functional unit. The term "module" herein may refer to an application specific integrated circuit (ASIC), an electronic circuit, a processor (e.g., a shared processor, a dedicated processor, or a group processor, etc.) and a memory for executing one or more software or firmware programs, a combined logic circuit, and / or other suitable components that support the described functionality.
[0118] The apparatus 100 of each of the above schemes has the function of implementing the corresponding steps of the above method 200. The functions can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above functions; for example, the determination module, the insertion module, etc. can be replaced by a processor to respectively perform the sending and receiving operations and related processing operations in each method embodiment. In addition, the processing unit can be a processing circuit.
[0119] It should be pointed out that Figure 5 The device in the above method embodiment may be a computing device, or a chip or chip system of a computing device, such as a system on chip (SoC). The processing unit is a processor or microprocessor or integrated circuit integrated on the chip. This is not limited here.
[0120] Figure 6 2 is a schematic block diagram of another computing device 200 provided in an embodiment of the present application. As shown in the figure, the device 200 includes: at least one processor 220. The processor 220 is coupled to the memory and is used to execute instructions stored in the memory to send signals and / or receive signals. Optionally, the device 200 also includes a memory 230 for storing instructions. Optionally, the device 200 also includes a transceiver 210, and the processor 220 controls the transceiver 210 to send signals and / or receive signals.
[0121] It should be understood that the processor 220 and the memory 230 can be combined into one processing device, and the processor 220 is used to execute the program code stored in the memory 230 to implement the above functions. In specific implementation, the memory 230 can also be integrated into the processor 220, or independent of the processor 220.
[0122] It should also be understood that the transceiver 210 may include a transceiver (or receiver) and a transmitter (or transmitter). The transceiver may further include an antenna, and the number of antennas may be one or more. The transceiver 210 may also be a communication interface or an interface circuit.
[0123] Specifically, the processor 220 in the computing device 200 may correspond to the acquisition unit 110 , the quantization unit 120 , the first processing unit 130 , the second processing unit 140 , and the third processing unit 150 in the computing device 100 .
[0124] As a solution, the device 200 is used to implement the steps in the embodiment of the above method 200. For example, the processor 220 is used to execute the computer program or instructions stored in the memory 230 to implement the various steps of the above method 200.
[0125] It should be understood that the specific process of each transceiver and processor executing the above corresponding steps has been described in detail in the above method embodiment, and for the sake of brevity, it will not be repeated here.
[0126] In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in a processor or an instruction in the form of software. The steps of the method disclosed in conjunction with the embodiment of the present application can be directly embodied as a hardware processor for execution, or a combination of hardware and software modules in a processor for execution. The software module can be located in a storage medium mature in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor reads the information in the memory and completes the steps of the above method in conjunction with its hardware. To avoid repetition, it is not described in detail here.
[0127] It should be noted that the processor in the embodiment of the present application can be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method embodiment can be completed by the hardware integrated logic circuit or software instructions in the processor. The above processor can be a general processor, a digital signal processor (digitalsignal processor, DSP), an application-specific integrated circuit (application-specific integrated circuit, ASIC), a field-programmable gate array (field-programmable gate array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to perform, or the hardware and software modules in the decoding processor can be combined and performed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.
[0128] It can be understood that the memory in the embodiments of the present application can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), and direct RAM bus RAM (DR RAM). It should be noted that the memory of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0129] According to the method provided in the embodiment of the present application, the present application also provides a computer program product, on which a computer program code is stored. When the computer program code is run on a computer, the computer executes the steps in the embodiment of method 200.
[0130] According to the method provided in the embodiment of the present application, the present application also provides a computer-readable medium, which stores program code. When the program code runs on a computer, the computer executes the steps in the above-mentioned method 200 embodiment.
[0131] The explanation of the relevant contents and beneficial effects of any of the above-mentioned devices can be referred to the corresponding method embodiments provided above, which will not be repeated here.
[0132] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a high-density digital video disc (DVD)), or a semiconductor medium (eg, a solid state disc (SSD)).
[0133] In the above-mentioned various device embodiments, the corresponding steps are performed by the corresponding modules or units. For example, the transceiver unit (transceiver) performs the steps of receiving or sending in the method embodiment, and other steps except sending and receiving can be performed by the processing unit (processor). The functions of the specific units can refer to the corresponding method embodiments. Among them, there can be one or more processors.
[0134] The terms "component", "module", "system", etc. used in this specification are used to represent computer-related entities, hardware, firmware, a combination of hardware and software, software, or software in execution. For example, a component can be, but is not limited to, a process running on a processor, a processor, an object, an executable file, an execution thread, a program and / or a computer. By way of illustration, both applications running on a computing device and a computing device can be components. One or more components may reside in a process and / or an execution thread, and a component may be located on a computer and / or distributed between two or more computers. In addition, these components may be executed from various computer-readable media having various data structures stored thereon. Components may, for example, communicate through local and / or remote processes according to signals having one or more data packets (e.g., data from two components interacting with another component between a local system, a distributed system and / or a network, such as the Internet interacting with other systems through signals).
[0135] 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.
[0136] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0137] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other 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.
[0138] 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.
[0139] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0140] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application. The aforementioned storage media include: various media that can store program codes, such as USB flash drives, mobile hard disks, read-only memories (ROM), random access memories (RAM), magnetic disks or optical disks.
[0141] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for computing convolution in a neural network, It is characterized in that The method comprises: Obtain a first tensor and a second tensor, wherein the first tensor represents a weight parameter in a neural network convolution operation, and the second tensor represents an activation value parameter in a neural network convolution operation; Based on the fixed-point computing capability of the processor, quantize the first tensor into a K-bit binary number, and quantize the second tensor into a K+1-bit binary number, where K is a standard bit width of quantization; Performing a first process on the first number and the third number to obtain a first value, wherein the first process includes a multiplication operation, wherein the first number is the K-bit binary number, the K-bit binary number is used to represent the integer part of the first tensor, and the second number is the value of the most significant bit of the K-bit binary number; Performing a second processing on the second number and the fourth number to obtain a second value, wherein the second processing includes first performing a multiplication operation and then shifting the result of the multiplication operation left by K-2 bits, wherein the third number is a value of a high K bit of the K+1 bit binary number, and the high K bit of the K+1 bit binary number is used to represent the integer part of the second tensor, and the fourth number is a value of a lowest bit of the K+1 bit binary number, and the value of the lowest bit is used to represent the decimal part of the quantized second tensor; The first value and the second value are added to generate an addition result, wherein the addition result is a convolution result of the first tensor and the second tensor.
2. The method according to claim 1, It is characterized in that The value of K is specifically one of 2, 4, 8, and 16.
3. The method according to claim 1 or 2, It is characterized in that The method further comprises: Dequantize the convolution operation result to obtain the dequantized convolution operation result.
4. The method according to any one of claims 1 to 3, It is characterized in that The range of the K+1-bit binary number is: [0,2 K -1+2 -1 ], or the range represented by the K+1-bit binary number is: [-2 K-1 ,2 K-1 -1+2 -1 ].
5. A computing device, Features include: an acquisition unit, a quantization unit, a first processing unit, a second processing unit, and a third processing unit, wherein: The acquisition unit is used to acquire a first tensor and a second tensor, wherein the first tensor represents a weight parameter in a convolution operation of a neural network, and the second tensor represents an activation value parameter in a convolution operation of a neural network; The quantization unit is used to quantize the first tensor into a K-bit binary number and quantize the second tensor into a K+1-bit binary number based on the fixed-point calculation capability of the processor, where K is a standard bit width of quantization; The first processing unit is used to perform a first processing on the first number and the third number to obtain a first value, wherein the first processing includes a multiplication operation, wherein the first number is the K-bit binary number, the K-bit binary number is used to represent the integer part of the first tensor, and the second number is the value of the highest bit of the K-bit binary number; The second processing unit is used to perform a second processing on the second number and the fourth number to obtain a second value, wherein the second processing includes first performing a multiplication operation and then shifting the result of the multiplication operation left by K-2 bits, wherein the third number is a value of a high K bit of the K+1 bit binary number, and the high K bit of the K+1 bit binary number is used to represent the integer part of the second tensor, and the fourth number is a value of a lowest bit of the K+1 bit binary number, and the value of the lowest bit is used to represent the decimal part of the quantized second tensor; The third processing unit is used to add the first value and the second value to generate an addition result, wherein the addition result is a convolution result of the first tensor and the second tensor.
6. The computing device according to claim 5, It is characterized in that The value of K is specifically one of 2, 4, 8, and 16.
7. A computing device according to claim 5 or 6, It is characterized in that The third processing unit is further used to dequantize the convolution operation result to obtain the dequantized convolution operation result.
8. A computing device according to any one of claims 5 to 7, It is characterized in that The range of the K+1-bit binary number is: [0,2 K -1+2 -1 ], or the range represented by the K+1-bit binary number is: [-2 K-1 ,2 K-1 -1+2 -1 ].
9. A computing device, It is characterized in that The method comprises a processor and a memory, wherein the processor runs instructions in the memory, so that the processor executes the method according to any one of claims 1 to 4.
10. A chip, It is characterized in that include: A processor and a data interface, wherein the processor reads instructions stored in the memory through the data interface, so that the chip executes the method according to any one of claims 1 to 4.
11. A computer-readable storage medium, It is characterized in that The method comprises computer program instructions which, when executed by a computing device, cause the device to perform the method as claimed in any one of claims 1 to 4.
12. A computer program product, It is characterized in that The method comprises computer program instructions, which, when executed by a computing device, cause the device to perform the method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Power weight quantification-based nerve network forward operation hardware structure
CN106066783A
Quick realization method for high-precision dynamic bit convolutional multiplication
CN108647184A
Neural network operation system based on digital-analog hybrid neurons
CN110991623A
Neural network operation circuit
CN112766477A
Image analysis system and method based on neural network input and output quantization, and equipment
CN113177634A
