High-precision approximate calculation device for softmax function

By designing a high-precision approximate calculation device, using mathematical equivalent transformations and zero-value prediction units, combined with quantization principles and constant multiplication, the problem of low softmax function calculation accuracy and high resource overhead has been solved, and efficient softmax function calculation has been achieved, improving the efficiency of softmax function calculations. Hardware energy efficiency.

CN118733946AActive Publication Date: 2024-10-01PEKING UNIV SHENZHEN GRADUATE SCHOOL
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202410295350.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-15
Publication Date
2024-10-01
Estimated Expiration
2044-03-15

AI Technical Summary

Technical Problem

The existing softmax function calculation method has low accuracy, high hardware resource overhead, low energy efficiency, and cannot effectively predict and skip zero value calculations, resulting in low efficiency in hardware acceleration.

Method used

A high-precision approximate calculation device is designed. Through mathematical equivalent transformation and zero-value prediction unit, combined with the quantization principle, it predicts and skips the zero-value calculation in the softmax function, reducing algorithm complexity and resource overhead, and introducing constant multiplication to reduce power. The computational complexity of functions and logarithmic functions, and the mathematical formulas are improved through penalty terms to improve computational accuracy.

Benefits of technology

It realizes the calculation of high-precision softmax function, reduces the power consumption and area overhead of hardware resources, improves energy efficiency, and greatly improves the computing efficiency of the hardware through prediction and skip zero value calculation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118733946A_ABST
    Figure CN118733946A_ABST
Patent Text Reader

Abstract

The invention discloses a high-precision approximate calculation device for a softmax function, which comprises a first module, a second module and a third module, the first module comprises a first zero value prediction unit, a first softmax function calculation unit and a first selector unit, the second module comprises a second softmax function calculation unit, and the third module comprises a second zero value prediction unit, a second softmax function calculation unit and a third selector unit. And the third module comprises a second zero value prediction unit, a third softmax function calculation unit and a second selector unit, and the first zero value prediction unit and the second zero value prediction unit are used for predicting whether the output data of the device is a zero value or not according to the corresponding input data so as to perform skipping subsequent calculation. The first softmax function calculation unit, the second softmax function calculation unit and the third softmax function calculation unit are used for realizing high-precision calculation of a softmax function, and the first selector unit and the second selector unit are used for selecting final output data. The invention can greatly reduce the overhead such as power consumption and area of the computing device and improve the energy efficiency of the device.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of very large scale integrated circuit design, in particular to a high-precision approximate calculation device for a softmax function. Background Art

[0002] The Softmax function is an important component of neural networks, such as the normalization layer in neural networks and the attention mechanism (Attention) of Transformer networks. However, the mathematical form of the softmax function is complex, which includes operations such as power functions and divisions, and has nonlinear characteristics, making it difficult to implement quickly and accurately on hardware. Secondly, neural networks are often quantized in hardware acceleration, and the softmax function in the network is usually followed by a quantization operation, which will introduce a large number of zero values ​​in the output data of the softmax function. These zero values ​​have no effect on the accuracy of the network. If the calculation of these zero values ​​can be predicted and skipped during hardware acceleration, the hardware energy efficiency will be greatly improved. Existing work has proposed many softmax fitting calculation methods, but the softmax function implemented by these methods has low accuracy, and cannot predict and skip zero value operations, requiring more hardware resource overhead and low hardware energy efficiency. Summary of the invention

[0003] The purpose of the present invention is to overcome the shortcomings of the existing softmax function calculation method, such as low precision, large hardware resource overhead, low energy efficiency, etc., and to provide a high-precision approximate calculation device for the softmax function.

[0004] In order to achieve the above purpose, the technical solution of the present invention is:

[0005] A high-precision approximate calculation device for a softmax function is used to realize a softmax function after mathematical equivalent transformation, as shown in the following formula: The input data of the softmax function is a vector X=[x0,x1,…,x M-1 ],x′ i =x i -x max ,i=0,1,…,M-1,x max is the maximum value in the vector X; the device includes a first module, a second module and a third module, the first module includes a first zero value prediction unit, a first softmax function calculation unit and a first selector unit, the second module includes a second softmax function calculation unit, and the third module includes a second zero value prediction unit, a third softmax function calculation unit and a second selector unit, wherein x i With x max Input the first zero value prediction unit, output the first zero value signal or x'i , x′ i Input the first softmax function calculation unit to obtain the first internal signal, input the first zero value signal or the first internal signal to the first selector unit, the first selector unit is used to select 0 as the output data of the device based on the first zero value signal, or input the first internal signal as the output data of the first module into the second softmax function calculation unit to obtain the second internal signal, and x i 、x max The second internal signal is input into the second zero value prediction unit, and the second zero value signal or x" is output. i , x″ i Input the third softmax function calculation unit to obtain the result signal, input the second zero value signal or the result signal into the second selector unit, and the second selector unit is used to select 0 as the output data of the device based on the second zero value signal, or to use the result signal as the output data of the device.

[0006] Preferably, the first zero value prediction unit comprises a first subtractor and a first judger connected in sequence, wherein the first subtractor is used to i With x max Perform subtraction to get x′ i , x′ i =xx max , x′ i In the input judgement, the judgement is used to judge x′ i Whether the zero value prediction formula is satisfied; if so, the first zero value prediction unit outputs the first zero value signal; if not, the first zero value prediction unit outputs x′ i .

[0007] Preferably, the second zero value prediction unit comprises a second subtractor and a second judger connected in sequence, the second subtractor being used for i 、x max Subtract the second internal signal to get x″ i ,x″ i =xx max -Ln(S), Ln(S) represents the second internal signal, x″ i Input into the second judgement device, the second judgement device is used to judge x″ i Whether the zero value prediction formula is satisfied; if so, the second zero value prediction unit outputs a second zero value signal; if not, the second zero value prediction unit outputs x″ i .

[0008] As a preferred embodiment, the zero value prediction formula is:

[0009] J < -N·ln(2);

[0010] Where J is x′ i or x″ i , N is the bit width after quantization of the softmax function.

[0011] Preferably, the first softmax function calculation unit includes a first Log2 constant multiplier, a first fitting calculator, a first shifter and an accumulator, wherein the first Log2 constant multiplier is used to calculate the input x′ i Realize x′ i ×Log2(e) is multiplied by a constant to obtain a first constant multiplication result, wherein n represents the integer part of the first constant multiplication result, and f represents the decimal part of the first constant multiplication result; the decimal part of the first constant multiplication result is input into a first fitting calculator, a power function calculation with base 2 is performed on the decimal part of the first constant multiplication result to obtain a first fitting result a, the integer part n of the first constant multiplication result and the first fitting result a are input into a first shifter, the first fitting result a is right-shifted n times to obtain a first right-shifted result R1, and the first right-shifted result R1 obtained by each calculation is input into an accumulator for accumulation operation to obtain a first internal signal S.

[0012] Preferably, the second softmax function calculation unit includes a highest bit detector, a second shifter, a second fitting calculator, a Ln constant multiplier and an adder. The first internal signal S is input into the highest bit detector for highest bit detection to obtain the highest bit result v. The first internal signal S and the highest bit result v are input into the second shifter. The first internal signal is right-shifted v times to obtain a second right-shifted result R2. The second right-shifted result is input into the second fitting calculator. The second right-shifted result R2 is calculated by a logarithmic function with base 2 to obtain a second fitting result b. The highest bit result v is input into the Ln constant multiplier to implement constant multiplication of v×Log2(e) to obtain a second constant multiplication result c. The second fitting result b and the second constant multiplication result c are input into the adder for addition operation to obtain the second internal signal ln(S).

[0013] Preferably, the third softmax function calculation unit includes a second Log2 constant multiplier, a third fitting calculator and a third shifter, wherein the second Log2 constant multiplier is used to adjust the input x″ i Implement x″ i×Log2(e) to obtain a third constant multiplication result, wherein n' represents the integer part of the third constant multiplication result, and f' represents the decimal part of the third constant multiplication result; the decimal part of the third constant multiplication result is input into a third fitting calculator, a power function calculation with base 2 is performed on the decimal part of the third constant multiplication result to obtain a third fitting result a', the integer part n' of the third constant multiplication result and the third fitting result a' are input into a third shifter, and the third fitting result a' is right-shifted n' times to obtain a result signal.

[0014] Preferably, in the first selection unit, if the input data of the first selection unit is a first zero-value signal, the calculation process of the second module and the third module is skipped, and 0 is directly used as the output data of the device; if the input data of the first selection unit is a first internal signal, the first internal signal is used as the output data of the first module.

[0015] Preferably, in the second selection unit, if the input data of the second selection unit is a second zero-value signal, 0 is used as the output data of the device; if the input data of the second selection unit is a result signal, the result signal is used as the output data of the device.

[0016] Compared with the prior art, the present invention has the following beneficial effects:

[0017] The present invention proposes a high-precision approximate calculation device for a softmax function. The first zero-value prediction unit and the second zero-value prediction unit included in the device can predict and output zero values ​​by combining with the quantization principle, thereby skipping redundant calculations in the softmax function, thereby reducing the device resource power consumption, area and other overheads and improving energy efficiency. In order to achieve high-precision softmax function calculation, the first softmax function calculation unit, the second softmax function calculation unit and the third softmax function calculation unit included in the device improve the format of the softmax function by mathematical equivalent transformation, thereby reducing the algorithm complexity; reduce the calculation complexity of the power function and the logarithmic function by introducing a constant multiplication method; propose a penalty term based on the error caused by the hardware approximate calculation and improve the mathematical formula, thereby improving the accuracy of the hardware calculation of the softmax function, and finally achieving the high-precision calculation goal of the softmax function. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The accompanying drawings are included to provide a further understanding of the embodiments and are incorporated into and constitute a part of this specification. The accompanying drawings illustrate the embodiments and together with the description are used to explain the principles of the present invention. Other embodiments and many expected advantages of the embodiments will be readily appreciated as they become better understood by reference to the following detailed description.

[0019] Figure 1 A schematic diagram of a calculation process of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0020] Figure 2 A schematic diagram of the structure of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0021] Figure 3 Schematic diagram of a first zero value prediction unit of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0022] Figure 4 A schematic diagram of a second zero-value prediction unit of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0023] Figure 5 A schematic diagram of the principle of zero value prediction of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0024] Figure 6 A schematic diagram of a first softmax function calculation unit of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0025] Figure 7 A schematic diagram of a second softmax function calculation unit of a high-precision approximate calculation device for a softmax function according to an embodiment of the present application;

[0026] Figure 8 This is a schematic diagram of a third softmax function calculation unit of the high-precision approximate calculation device for the softmax function according to an embodiment of the present application. DETAILED DESCRIPTION

[0027] The present application will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the relevant invention, rather than to limit the invention. It should also be noted that, for ease of description, only the parts related to the relevant invention are shown in the accompanying drawings.

[0028] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0029] In the embodiment of the present application, a high-precision approximate calculation device for a softmax function is proposed, which is used to realize the softmax function after mathematical equivalent transformation, as shown in the following formula:

[0030]

[0031] The input data of the softmax function is a vector X=[x0,x1,…,x M-1 ],x′ i =x i -x max ,i=0,1,…,M-1,x max is the maximum value in vector X.

[0032] refer to Figure 1 The high-precision approximate calculation device for the softmax function proposed in the embodiment of the present application includes a first module, a second module and a third module. The first module includes a first zero value prediction unit, a first softmax function calculation unit and a first selector unit, which are used to complete the power function calculation and accumulation operation, that is, corresponding to the formula (1) The second module includes a second softmax function calculation unit, which is used to complete the logarithmic function calculation, that is, the ln() calculation corresponding to formula (1); the third module includes a second zero value prediction unit, a third softmax function calculation unit and a second selector unit, which is used to complete the power function calculation, that is, the exp(x′) in formula (1) i -ln()). The first zero value prediction unit in the first module and the second zero value prediction unit in the third module both adopt zero value prediction and calculation skipping mode, saving calculation steps.

[0033] refer to Figure 2 In the embodiments of the present application, x i With x max As the input data of the high-precision approximate calculation device for the softmax function, x i With x max Input the first zero value prediction unit, output the first zero value signal or x' i , x′ i Input the first softmax function calculation unit to obtain the first internal signal, input the first zero value signal or the first internal signal to the first selector unit, the first selector unit is used to select 0 as the output data of the high-precision approximate calculation device based on the first zero value signal, or input the first internal signal as the output data of the first module into the second softmax function calculation unit to obtain the second internal signal, and x i 、x max The second internal signal is input into the second zero value prediction unit, and the second zero value signal or x" is output. i , x″ iInput the third softmax function calculation unit to obtain the result signal, and input the second zero value signal or the result signal into the second selector unit, the second selector unit is used to select 0 as the output data of the high-precision approximate calculation device based on the second zero value signal, or to use the result signal as the output data of the high-precision approximate calculation device.

[0034] In a specific embodiment, reference Figure 3 The first zero value prediction unit includes a first subtractor and a first judger connected in sequence, and the first subtractor is used to i With x max Perform subtraction to get x′ i , x′ i =xx max , x′ i In the input judgement, the judgement is used to judge x′ i Whether the zero value prediction formula is satisfied; if so, the first zero value prediction unit outputs the first zero value signal; if not, the first zero value prediction unit outputs x′ i , x′ i The first softmax function calculation unit is input for calculation to obtain the first internal signal S, and the first selector unit selects and inputs it into the second module. That is to say, if 0 is used as the output data of the high-precision approximate calculation device for the softmax function according to the first zero-value signal, the calculation of the first softmax function calculation unit and the second module and the third module can be skipped.

[0035] In a specific embodiment, reference Figure 4 The second zero value prediction unit includes a second subtractor and a second judger connected in sequence, and the second subtractor is used to i 、x max Subtract the second internal signal to get x″ i ,x″ i =xx max -Ln(S), Ln(S) represents the second internal signal, x″ i Input into the second judgement device, the second judgement device is used to judge x″ i Whether the zero value prediction formula is satisfied; if so, the second zero value prediction unit outputs a second zero value signal; if not, the second zero value prediction unit outputs x″ i . x″ iThe signal is input into the third softmax function calculation unit for calculation to obtain a result signal R, and R is selected by the first selector unit as the output data of the high-precision approximate calculation device for the softmax function. In other words, if 0 is used as the output data of the high-precision approximate calculation device for the softmax function according to the second zero-value signal, the calculation of the third softmax function calculation unit can be skipped.

[0036] In a specific embodiment, the zero value prediction formula is:

[0037] J<-N·ln(2) (2)

[0038] Where J is x′ i or x″ i , N is the bit width after quantization of the softmax function.

[0039] Specifically, the first zero value signal and the second zero value signal can both be represented by Zero. If Zero=1, it means that the first zero value signal or the second zero value signal exists; if Zero=0, it means that the first zero value signal or the second zero value signal does not exist. i or x″ i Passed to subsequent devices for calculation.

[0040] refer to Figure 5 , the principle and process of the zero value prediction formula are as follows:

[0041] After the Softmax function is combined with N-bit quantization, the generated value will contain a large number of zero values. The first zero value prediction unit and the second zero value prediction unit proposed in the embodiment of the present application summarize the zero value prediction formula by combining the softmax function and the N-bit quantization principle, so that before calculating the softmax function, it is possible to predict whether the output data of the device is a zero value, thereby skipping the redundant calculation corresponding to the zero value.

[0042] Taking linear quantization as an example, the quantization method first performs a range detection on the data to be quantized to obtain the maximum value max and the minimum value min of the data. When quantized to N bits, when the data to be quantized P meets formula (3), the quantization result will be zero.

[0043]

[0044] The output data of the Softmax function ranges from 0 to 1, so the maximum and minimum values ​​of the quantized data P are max = 1 and min = 0 respectively. Substituting them into formula (3) yields formula (4), that is, when the output of the softmax function is less than 2 -N , the final quantization result will be zero.

[0045] softmax(x i )<2 -N (4)

[0046] Combining the formula (1) of the softmax function with the formula (4) for generating zero values ​​by quantization, we can derive the zero value prediction principle proposed in the embodiment of the present application: In the softmax function, if the output result of the power function is less than 2 -N , as shown in formula (5), where in is the input data of the power function and N is the quantized bit, then the quantized output value corresponding to the softmax function will also be zero.

[0047] exp(in)<2 -N (5)

[0048] Introducing the zero-value prediction principle into the actual softmax function formula (1) yields: If x′ is i =x i -x max And in the third module x″ i =x i -x max -Ln(S) satisfies formula (2), then the quantized output data of the softmax function is zero. The calculation operations corresponding to these zero values ​​can be skipped without affecting the accuracy, which can save power consumption. At the same time, the zero value prediction can limit the x′ transmitted to the subsequent device. i or x″ i data range, thereby reducing the bit width requirements of the hardware calculation, thereby saving hardware area and greatly improving hardware energy efficiency.

[0049] In a specific embodiment, in the first selection unit, if the input data of the first selection unit is a first zero-value signal, the calculation process of the second module and the third module is skipped, and 0 is directly used as the output data of the high-precision approximate calculation device; if the input data of the first selection unit is a first internal signal, the first internal signal is used as the output data of the first module.

[0050] In a specific embodiment, in the second selection unit, if the input data of the second selection unit is a second zero-value signal, 0 is used as the output data of the high-precision approximate computing device; if the input data of the second selection unit is a result signal, the result signal is used as the output data of the high-precision approximate computing device.

[0051] In a specific embodiment, reference Figure 6 The first softmax function calculation unit includes a first Log2 constant multiplier, a first fitting calculator, a first shifter and an accumulator. The first Log2 constant multiplier is used to input x′ i Realize x′i ×Log2(e) is multiplied by a constant to obtain a first constant multiplication result, wherein n represents the integer part of the first constant multiplication result, and f represents the decimal part of the first constant multiplication result; the decimal part of the first constant multiplication result is input into a first fitting calculator, a power function calculation with base 2 is performed on the decimal part of the first constant multiplication result to obtain a first fitting result a, the integer part n of the first constant multiplication result and the first fitting result a are input into a first shifter, the first fitting result a is right-shifted n times to obtain a first right-shifted result R1, and the first right-shifted result R1 obtained by each calculation is input into an accumulator for accumulation operation to obtain a first internal signal S.

[0052] In a specific embodiment, reference Figure 7 The second softmax function calculation unit includes a highest bit detector, a second shifter, a second fitting calculator, a Ln constant multiplier and an adder. The first internal signal is input into the highest bit detector for highest bit detection to obtain the highest bit result v. The first internal signal and the highest bit result v are input into the second shifter. The first internal signal is right-shifted v times to obtain a second right-shifted result R2. The second right-shifted result is input into the second fitting calculator. The second right-shifted result R2 is calculated by a logarithmic function with a base of 2 to obtain a second fitting result b. The highest bit result v is input into the Ln constant multiplier to implement the constant multiplication of v×Log2(e) to obtain the second constant multiplication result c. The second fitting result b and the second constant multiplication result c are input into the adder for addition operation to obtain the second internal signal ln(S).

[0053] In a specific embodiment, reference Figure 8 The third softmax function calculation unit includes a second Log2 constant multiplier, a third fitting calculator and a third shifter. The second Log2 constant multiplier is used to input x″ i Implement x″ i ×Log2(e) to obtain a third constant multiplication result, wherein n' represents the integer part of the third constant multiplication result, and f' represents the decimal part of the third constant multiplication result; the decimal part of the third constant multiplication result is input into a third fitting calculator, a power function calculation with base 2 is performed on the decimal part of the third constant multiplication result to obtain a third fitting result a', the integer part n' of the third constant multiplication result and the third fitting result a' are input into a third shifter, and the third fitting result a' is right-shifted n' times to obtain a result signal.

[0054] The main idea of ​​the first softmax function calculation unit, the second softmax function calculation unit and the third softmax function calculation unit proposed in the embodiment of the present application is: first simplify the power function and the logarithmic function through mathematical form conversion, and introduce the calculation error of the hardware approximate constant multiplication to generate an error correction term in the fitting calculation to improve the accuracy. The overall formula of the Softmax function is shown in (1), so the calculation process is divided into power function calculation and logarithmic function calculation.

[0055] For the power function calculation process, the embodiment of the present application replaces the power function with base e with the power function with base 2 by introducing the log2(e) constant multiplication, as shown in formula (6).

[0056]

[0057] Since hardware usually performs constant multiplication based on approximate calculations, errors are introduced. In the embodiment of the present application, the error of hardware approximate calculation is defined as err0, as shown in formula (7), where log2(e) is the full precision result of the floating point bit, log2(e) h is the result of hardware approximate calculation, h is the bit width of constant multiplication in hardware, the lower h is, the larger the error err0 is.

[0058] err0=log2(e)-log2(e) h (7)

[0059] Introducing err0 into formula (6) and further transforming it, we can get formula (8):

[0060]

[0061] Let x′ i log2(e) h =-(n i +f i ), where n i is the integer part, f i is the fractional part. This step is implemented by the Log2 constant multiplier, so formula (8) can be further converted to formula (9):

[0062]

[0063] Here >> represents a right shift operation, which is implemented by a shifter.

[0064] Internal punishment and overall penalty Therefore, formula (9) can be further simplified to formula (10):

[0065]

[0066] In formula (10), P in By log2(e) h Determine, so after the bit width h of the constant multiplication in the hardware is determined, P in In formula (10), P ov By P in and n i OK, i It can be determined in advance by statistically analyzing the range of input data, so P ov It is also a fixed value before hardware calculation. The input data has a small variation range (f i ∈[0,1)) and a nonlinear function that already contains a penalty coefficient can be completed based on low-complexity linear fitting calculations or lookup table calculations This step is achieved by the fitting calculator.

[0067] In summary, when implementing the first module and the second module of power function calculation, the first Log2 constant multiplier or the second Log2 constant multiplier is used to implement x′·log2(e) h =-(n i +f i ) or x″·log2(e) h =-(n′ i +f′ i ), where f or f' is passed to the first fitting calculator or the third fitting calculator to implement or Get a or a', n and a or n' and a' are simultaneously passed into the first shifter or the third shifter to realize a>>n or a'>>n' to get R1 or R, in the first module R1=exp(x'), and passed into the accumulator to realize The first internal signal S is obtained; in the third module, R=exp(x″)=softmax(x), and the output data of the high-precision softmax function is directly obtained.

[0068] For the calculation process of the logarithmic function, the embodiment of the present application introduces the ln(2) constant multiplication to replace the logarithmic function from base e to base 2. This step is implemented by the Ln constant multiplication unit, as shown in formula (11):

[0069] ln(S)=ln(2)·log2(S) (11)

[0070] Where S is the cumulative term in formula (1) Let S = u 2 v, where v is an integer that can be obtained by the highest bit detector, and u has a value range of [1,2). Therefore, formula (11) can be further transformed into formula (12):

[0071]

[0072] Since hardware usually performs constant multiplication based on approximate calculations, thereby introducing errors that affect calculation accuracy, the embodiment of the present application defines the error as err1, as shown in formula (13), where ln(2) is the full-precision result of the floating point bit, ln(2) h is the result of the hardware approximate calculation, and h is the bit width of the constant multiplication in the hardware.

[0073] err1=ln(2)-ln(2) h (13)

[0074] Introducing err1 in formula (13) into formula (12) and further transforming it, we can get formula (14):

[0075] ln(S)=(ln(2) h +err1)·v+ln(2)·log2(u),u∈[1,2) (14)

[0076] Define the penalty term P ln =err1·v, thus formula (14) is further simplified to formula (15):

[0077] ln(S)=ln(2) h ·v+ln(2)·log2(u)+P ln ,u∈[1,2) (15)

[0078] In formula (15), err1 is given by ln(2) h Determine, it is a fixed value after the hardware bandwidth h is determined; P ln Determined by err1 and v, where v can be determined in advance by counting the range of the input data, so P ln It is also a fixed value before hardware calculation. In formula (15), ln(2)·log2(u)+P ln For nonlinear functions with a small input data variation range (u∈[1,2)), high-precision calculations can be completed based on low-complexity linear fitting calculations or lookup table calculations. This step is implemented by the second fitting calculator; ln(2) h v is realized by the Ln constant multiplier; finally, the result of the second fitting calculator and the result of the Ln constant multiplier are added by an adder to obtain the output Ln(S).

[0079] In summary, when implementing the second module of logarithmic function calculation, the accumulator passes S to the highest bit detector to obtain v. At this time, S and v are simultaneously passed to the second shifter to obtain R2 (and u in formula (12)); v is simultaneously passed to the Ln constant multiplier to complete ln(2) h ·v gets c; R2 is passed to the second fitting calculator to achieve ln(2)·log2(u)+P ln Get b; finally b and c are passed into the adder to obtain Ln(S) and complete the high-precision calculation of the logarithmic function.

[0080] The above describes the specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the 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 high-precision approximate calculation device for a softmax function, characterized in that: It is used to implement the softmax function after mathematical equivalent transformation, as shown in the following formula: The input data of the softmax function is a vector X=[x0, x1, ..., x M-1 ],x′ i =x i -x max , i = 0, 1, ..., M-1, x max is the maximum value in the vector X; the device comprises a first module, a second module and a third module, the first module comprises a first zero value prediction unit, a first softmax function calculation unit and a first selector unit, the second module comprises a second softmax function calculation unit, the third module comprises a second zero value prediction unit, a third softmax function calculation unit and a second selector unit, wherein x is i With x max Input the first zero value prediction unit, output the first zero value signal or x' i , x′ i The first softmax function calculation unit is input to obtain a first internal signal, the first zero-value signal or the first internal signal is input to the first selector unit, the first selector unit is used to select 0 as the output data of the device based on the first zero-value signal, or the first internal signal is input to the second softmax function calculation unit as the output data of the first module to obtain a second internal signal, and x is i 、x max and the second internal signal is input into the second zero value prediction unit, and the second zero value signal or x" is output. i , x″ i The third sofimax function calculation unit is input to obtain a result signal, and the second zero-value signal or the result signal is input to the second selector unit, and the second selector unit is used to select 0 as the output data of the device based on the second zero-value signal, or to use the result signal as the output data of the device.

2. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: The first zero value prediction unit comprises a first subtractor and a first judger connected in sequence, wherein the first subtractor is used to i With x max Perform subtraction to get x′ i , x′ i =xx max , x′ i Input into the judgement device, the judgement device is used to judge x' i Whether the zero value prediction formula is satisfied; if satisfied, the first zero value prediction unit outputs a first zero value signal; if not satisfied, the first zero value prediction unit outputs x′ i .

3. The high-precision approximate calculation device for the sofimax function according to claim 2, characterized in that: The second zero value prediction unit comprises a second subtractor and a second judger connected in sequence, wherein the second subtractor is used to i 、x max Subtract the second internal signal to get x″ i ,x″ i =xx max -Ln(S), Ln(S) represents the second internal signal, x″ i The second judgement device is used to judge x″ i Whether the zero value prediction formula is satisfied; if so, the second zero value prediction unit outputs a second zero value signal; if not, the second zero value prediction unit outputs x″ i .

4. The high-precision approximate calculation device for the softmax function according to claim 3, characterized in that: The zero value prediction formula is: J<-N·ln (2); Where J is x′ i or x″ i , N is the bit width after quantization of the softmax function.

5. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: The first softmax function calculation unit includes a first Log2 constant multiplier, a first fitting calculator, a first shifter and an accumulator. The first Log2 constant multiplier is used to calculate the input x′ i Realize x′ i ×Log2(e) is multiplied by a constant to obtain a first constant multiplication result, wherein n represents the integer part of the first constant multiplication result, and f represents the decimal part of the first constant multiplication result; the decimal part of the first constant multiplication result is input into the first fitting calculator, a power function calculation with base 2 is performed on the decimal part of the first constant multiplication result to obtain a first fitting result a, the integer part n of the first constant multiplication result and the first fitting result a are input into the first shifter, the first fitting result a is right-shifted n times to obtain a first right-shifted result R1, and the first right-shifted result R1 obtained by each calculation is input into the accumulator for accumulation operation to obtain a first internal signal S.

6. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: The second softmax function calculation unit includes a highest bit detector, a second shifter, a second fitting calculator, a Ln constant multiplier and an adder. The first internal signal S is input into the highest bit detector for highest bit detection to obtain the highest bit result v. The first internal signal and the highest bit result v are input into the second shifter. The first internal signal S is right-shifted v times to obtain a second right-shifted result R2. The second right-shifted result is input into the second fitting calculator. The second right-shifted result R2 is calculated by a logarithmic function with base 2 to obtain a second fitting result b. The highest bit result v is input into the Ln constant multiplier to implement constant multiplication of v×Log2(e) to obtain a second constant multiplication result c. The second fitting result b and the second constant multiplication result c are input into the adder for addition operation to obtain the second internal signal ln(S).

7. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: The third softmax function calculation unit includes a second Log2 constant multiplier, a third fitting calculator and a third shifter. The second Log2 constant multiplier is used to input x″ i Implement x″ i ×Log2(e) to obtain a third constant multiplication result, wherein n' represents the integer part of the third constant multiplication result, and f' represents the decimal part of the third constant multiplication result; the decimal part of the third constant multiplication result is input into the third fitting calculator, and a power function calculation with base 2 is performed on the decimal part of the third constant multiplication result to obtain a third fitting result a'; the integer part n' of the third constant multiplication result and the third fitting result a' are input into the third shifter, and the third fitting result a' is right-shifted n' times to obtain a result signal.

8. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: In the first selection unit, if the input data of the first selection unit is the first zero-value signal, the calculation process of the second module and the third module is skipped, and 0 is directly used as the output data of the device; If the input data of the first selection unit is the first internal signal, the first internal signal is used as the output data of the first module.

9. The high-precision approximate calculation device for the softmax function according to claim 1, characterized in that: In the second selection unit, if the input data of the second selection unit is the second zero-value signal, 0 is used as the output data of the device; if the input data of the second selection unit is the result signal, the result signal is used as the output data of the device.

Citation Information

Patent Citations

  • Isolated digit speech recognition classification system and method combining principal component analysis (PCA) with restricted Boltzmann machine (RBM)

    CN105206270A

  • Target detecting and identifying method and target detecting and identifying system based on sound vibration signal

    CN106646587A

  • Sine, cosine and arc tangent function operation device and method based on CORDIC algorithm

    CN112783471A

  • Small floating point arithmetic unit based on logarithm arithmetic system and application of small floating point arithmetic unit in FPGA (field programmable gate array)

    CN115840555A

  • Hardware system and method for realizing softmax function calculation

    CN116562347A