A high-precision approximation calculation device for the softmax function

By using a high-precision approximation calculation device, the zero-value calculation in the softmax function is predicted and skipped. Combined with mathematical transformations and penalty terms to optimize the calculation process, the problems of low accuracy and resource waste in the hardware implementation of the softmax function are solved, and efficient hardware calculation is achieved.

CN118733946BActive Publication Date: 2025-10-31PEKING UNIV SHENZHEN GRADUATE SCHOOL
View PDF 2 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing softmax functions have low hardware implementation accuracy, high hardware resource consumption, low energy efficiency, and cannot effectively predict and skip zero value calculations.

Method used

It employs a high-precision approximation calculation device, predicts and skips redundant calculations through a zero-value prediction unit, reduces the computational complexity of power functions and logarithmic functions by combining mathematical equivalent transformations and constant multiplication, and introduces a penalty term to improve accuracy. It includes a first module, a second module and a third module, which respectively perform power function and logarithmic function calculations and zero-value predictions.

Benefits of technology

It improves the calculation accuracy of the softmax function, reduces hardware resources and power consumption, and enhances energy efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118733946B_ABST
    Figure CN118733946B_ABST
Patent Text Reader

Abstract

This invention discloses a high-precision approximation calculation device for the softmax function, comprising 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. The third module includes a second zero-value prediction unit, a third softmax function calculation unit, and a second selector unit. The first and second zero-value prediction units are used to predict whether the output data of the device is zero based on their corresponding input data, so as to skip subsequent calculations. The first, second, and third softmax function calculation units are used to implement high-precision calculation of the softmax function, and the first and second selector units are used to select the final output data. This invention can greatly reduce the power consumption and area overhead 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] This invention relates to the field of very large-scale integrated circuit design, and in particular to a high-precision approximation calculation device for the softmax function. Background Technology

[0002] The softmax function is a crucial component of neural networks, used in normalization layers and the attention mechanism of Transformer networks. However, its mathematical form is complex, involving power functions and division operations, exhibiting non-linear characteristics that make fast and accurate hardware implementation difficult. Furthermore, in hardware acceleration, neural networks are often quantized, and the softmax function is typically followed by a quantization operation, introducing numerous zero values ​​into the output. While these zero values ​​do not affect network accuracy, predicting and eliminating them during hardware acceleration would significantly improve energy efficiency. Existing work has proposed many methods for softmax fitting, but these methods achieve low accuracy, cannot predict or eliminate zero values, require significant hardware resources, and suffer from low energy efficiency. Summary of the Invention

[0003] The purpose of this invention is to overcome the shortcomings of existing softmax function calculation methods, such as low accuracy, high hardware resource consumption, and low energy efficiency, and to provide a high-precision approximation calculation device for the softmax function.

[0004] To achieve the above objectives, the technical solution of the present invention is as follows:

[0005] A high-precision approximation calculation device for the softmax function is used to implement the softmax function after mathematical equivalence transformation, as shown in the following equation: The input data for the softmax function is a vector of length M. , i=0,1,…,M-1 Let X be the maximum value in 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. The third module includes a second zero-value prediction unit, a third softmax function calculation unit, and a second selector unit. and Input to the first zero-value prediction unit, output the first zero-value signal or ,Will 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 then input to a first selector unit. The first selector unit selects whether to use 0 as the output data of the device based on the first zero-value signal, or to input the first internal signal as the output data of the first module to a second softmax function calculation unit to obtain a second internal signal. , The second internal signal is input to the second zero-value prediction unit, and the second zero-value signal is output. ,Will The third softmax function calculation unit is input to obtain the result signal. The second zero value signal or the result signal is input to the second selector unit. The second selector unit is used to select 0 as the output data of the device based on the second zero value signal, or the result signal as the output data of the device.

[0006] Preferably, the first zero-value prediction unit includes a first subtractor and a first judge connected in sequence. The first subtractor is used to... and Perform the subtraction operation to obtain , ,Will In the input detector, the detector is used to determine... Does the zero-value prediction formula satisfy the condition? If it does, the first zero-value prediction unit outputs the first zero-value signal; if not, the first zero-value prediction unit outputs... .

[0007] Preferably, the second zero-value prediction unit includes a second subtractor and a second judge connected in sequence. The second subtractor is used to... , Subtract the second internal signal to obtain , , Indicates the second internal signal, The input is used in the second judgment unit to make a judgment. Does the zero-value prediction formula satisfy the condition? If it does, the second zero-value prediction unit outputs the second zero-value signal; otherwise, the second zero-value prediction unit outputs... .

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

[0009] ;

[0010] Where J is or N is the bit width after quantization by 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. The first Log2 constant multiplier is used to process the input... accomplish The constant multiplication is performed to obtain the first constant multiplication result, where n represents the integer part of the first constant multiplication result and f represents the fractional part of the first constant multiplication result. The fractional part of the first constant multiplication result is input into the first fitting calculator, and the fractional part of the first constant multiplication result is calculated using a power function with base 2 to obtain the 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, and the first fitting result a is shifted to the right n times to obtain the first right shift result R1. The first right shift result R1 obtained each time is input into the accumulator for accumulation to obtain the first internal signal S.

[0012] Preferably, the second softmax function calculation unit includes a highest-order bit detector, a second shifter, a second fitting calculator, an Ln constant multiplier, and an adder. The first internal signal S is input to the highest-order bit detector for highest-order bit detection, obtaining the highest-order bit result v. The first internal signal S and the highest-order bit result v are input to the second shifter, which performs v right shifts on the first internal signal to obtain a second right shift result R2. The second right shift result is input to the second fitting calculator, which performs a base-2 logarithmic function calculation on the second right shift result R2 to obtain a second fitting result b. Finally, the highest-order bit result v is input to the Ln constant multiplier to achieve the desired result. The constant multiplication is performed to obtain the second constant multiplication result c. The second fitting result b and the second constant multiplication result c are then input into an adder for addition to obtain the second internal signal. .

[0013] Preferably, 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 process the input... accomplish The constant multiplication is used to obtain the third constant multiplication result, where n' represents the integer part of the third constant multiplication result and f' represents the fractional part of the third constant multiplication result. The fractional part of the third constant multiplication result is input into the third fitting calculator, and the fractional part of the third constant multiplication result is calculated using a power function with base 2 to obtain the 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 shifted to the right n' times to obtain the result signal.

[0014] Preferably, in the first selector unit, if the input data of the first selector unit is a first zero value signal, the calculation process of the second and third modules is skipped, and 0 is directly used as the output data of the device; if the input data of the first selector 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 selector unit, if the input data of the second selector unit is a second zero value signal, then 0 is used as the output data of the device; if the input data of the second selector unit is a result signal, then 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] This invention proposes a high-precision approximation calculation device for the softmax function. The device includes a first zero-value prediction unit and a second zero-value prediction unit that, by combining with quantization principles, predict the output zero value, thereby eliminating redundant calculations in the softmax function and reducing device resource consumption, area requirements, and energy efficiency. To achieve high-precision softmax function calculation, the device includes a first, second, and third softmax function calculation unit that improves the format of the softmax function through mathematical equivalence transformations, thus reducing algorithm complexity; it also reduces the computational complexity of power and logarithmic functions by introducing constant multiplication; and it proposes a penalty term based on the error caused by hardware approximation calculations and improves the mathematical formula, thereby improving the accuracy of hardware calculations of the softmax function and ultimately achieving the goal of high-precision softmax function calculation. Attached Figure Description

[0018] The accompanying drawings are included to provide a further understanding of the embodiments, and these drawings are incorporated in and constitute a part of this specification. The drawings illustrate embodiments and, together with the description, serve to explain the principles of the invention. Other embodiments and many anticipated advantages of the embodiments will be readily recognized as they become better understood through reference to the following detailed description.

[0019] Figure 1 This is a schematic diagram illustrating the calculation process of a high-precision approximation calculation device for the softmax function according to an embodiment of this application.

[0020] Figure 2 This is a schematic diagram of the structure of a high-precision approximation calculation device for the softmax function according to an embodiment of this application;

[0021] Figure 3 This is a schematic diagram of the first zero-value prediction unit of a high-precision approximation calculation device for the softmax function according to an embodiment of this application;

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

[0023] Figure 5 This is a schematic diagram illustrating the principle of zero-value prediction for a high-precision approximation calculation device for the softmax function according to an embodiment of this application.

[0024] Figure 6 This is a schematic diagram of the first softmax function calculation unit of a high-precision approximation calculation device for the softmax function according to an embodiment of this application;

[0025] Figure 7 This is a schematic diagram of the second softmax function calculation unit of the high-precision approximation calculation device for the softmax function according to an embodiment of this application;

[0026] Figure 8 This is a schematic diagram of the third softmax function calculation unit of the high-precision approximation calculation device for the softmax function according to an embodiment of this application. Detailed Implementation

[0027] The present application will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and not intended to limit it. Furthermore, it should be noted that, for ease of description, only the parts relevant to the invention are shown in the accompanying drawings.

[0028] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.

[0029] The embodiments of this application propose a high-precision approximation calculation device for the softmax function, used to implement the softmax function after mathematical equivalence transformation, as shown in the following equation:

[0030] (1)

[0031] The input data for the softmax function is a vector of length M. , i=0,1,…,M-1 It is the maximum value in vector X.

[0032] refer to Figure 1The high-precision approximation calculation device for the softmax function proposed in the embodiments of this 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, the corresponding formula (1) The second module includes a second softmax function calculation unit, used to complete the logarithmic function calculation, which corresponds to the softmax function in formula (1). The third module includes a second zero-value prediction unit, a third softmax function calculation unit, and a second selector unit, used to complete the power function calculation, which corresponds to the formula (1) in the formula. The first zero-value prediction unit in the first module and the second zero-value prediction unit in the third module both adopt the zero-value prediction and calculation skipping method, saving calculation steps.

[0033] refer to Figure 2 In the embodiments of this application and As input data for a high-precision approximation calculation device for the softmax function, and Input to the first zero-value prediction unit, output the first zero-value signal or ,Will 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 then input to a first selector unit. The first selector unit is used to select whether to use 0 as the output data of the high-precision approximation calculation device based on the first zero-value signal, or to input the first internal signal as the output data of the first module into the second softmax function calculation unit to obtain a second internal signal. , The second internal signal is input to the second zero-value prediction unit, and the second zero-value signal is output. ,Will The third softmax function calculation unit is input to obtain the result signal. The second zero value signal or the result signal is input to the second selector unit. The second selector unit is used to select 0 as the output data of the high-precision approximation calculation device based on the second zero value signal, or the result signal as the output data of the high-precision approximation calculation device.

[0034] In a specific embodiment, refer to Figure 3 The first zero-value prediction unit includes a first subtractor and a first judge connected in sequence. The first subtractor is used to... and Perform the subtraction operation to obtain , ,Will In the input detector, the detector is used to determine... Does the zero-value prediction formula satisfy the condition? If it does, the first zero-value prediction unit outputs the first zero-value signal; if not, the first zero-value prediction unit outputs... , The input is processed in the first softmax function calculation unit to obtain the first internal signal S, which is then selected by the first selector unit and input to the second module. In other words, if 0 is taken as the output data of the high-precision approximation calculation device for the softmax function based on the first zero-value signal, the calculations of the first softmax function calculation unit, as well as the second and third modules, can be skipped.

[0035] In a specific embodiment, refer to Figure 4 The second zero-value prediction unit includes a second subtractor and a second judge connected in sequence. The second subtractor is used to... , Subtract the second internal signal to obtain , , Indicates the second internal signal, The input is used in the second judgment unit to make a judgment. Does the zero-value prediction formula satisfy the condition? If it does, the second zero-value prediction unit outputs the second zero-value signal; otherwise, the second zero-value prediction unit outputs... . The input is processed in the third softmax function calculation unit to obtain the result signal R. R is then selected by the first selector unit as the output data of the high-precision approximation calculation device for the softmax function. In other words, if 0 is used as the output data of the high-precision approximation calculation device for the softmax function based on the second zero-value signal, the calculation in the third softmax function calculation unit can be skipped.

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

[0037] (2)

[0038] Where J is or N is the bit width after quantization by the softmax function.

[0039] Specifically, both the first and second zero-value signals can be represented using Zero. If so, it indicates the existence of either the first zero-value signal or the second zero-value signal; if If the first zero-value signal or the second zero-value signal does not exist, then it means that the first zero-value signal or the second zero-value signal does not exist. Therefore, it can be... or The data is then 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] When the Softmax function is combined with N-bit quantization, the resulting value contains a large number of zero values. The first and second zero-value prediction units proposed in the embodiments of this application summarize a zero-value prediction formula by combining the softmax function and the N-bit quantization principle. This allows them to predict whether the output data of the device is zero before calculating the softmax function, thereby eliminating redundant calculations corresponding to zero values.

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

[0043] (3)

[0044] The output data of the Softmax function ranges from 0 to 1, therefore the maximum and minimum values ​​of the quantized data P are respectively... and Substituting this into formula (3) yields formula (4), which states that when the output of the softmax function is less than... At that time, the final quantization result will be zero.

[0045] (4)

[0046] By combining the formula (1) for the softmax function with the formula (4) for generating zero values ​​through quantization, we can deduce the zero-value prediction principle proposed in the embodiments of this application: In the softmax function, if the output of the power function is less than... As shown in formula (5), where If the input data is the power function and N is the number of quantized bits, then the quantized output value of the softmax function will also be zero.

[0047] (5)

[0048] By incorporating the zero-value prediction principle into the formula (1) of the actual softmax function, we can obtain: If in the first module And in the third module If formula (2) is satisfied, the quantized output data of the softmax function is zero. The computational operations corresponding to these zero values ​​can be skipped without affecting accuracy, and skipping them saves power. Simultaneously, zero-value prediction can limit the input to subsequent devices. or This reduces the data range, thereby lowering the bit width requirements for hardware computation, saving hardware area, and greatly improving hardware energy efficiency.

[0049] In a specific embodiment, if the input data of the first selector unit is a first zero-value signal, the calculation process of the second and third modules is skipped, and 0 is directly used as the output data of the high-precision approximation calculation device; if the input data of the first selector 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 selector unit, if the input data of the second selector unit is a second zero-value signal, then 0 is used as the output data of the high-precision approximation calculation device; if the input data of the second selector unit is a result signal, then the result signal is used as the output data of the high-precision approximation calculation device.

[0051] In a specific embodiment, refer to 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 process the input... accomplish The constant multiplication is performed to obtain the first constant multiplication result, where n represents the integer part of the first constant multiplication result and f represents the fractional part of the first constant multiplication result. The fractional part of the first constant multiplication result is input into the first fitting calculator, and the fractional part of the first constant multiplication result is calculated using a power function with base 2 to obtain the 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, and the first fitting result a is shifted to the right n times to obtain the first right shift result R1. The first right shift result R1 obtained each time is input into the accumulator for accumulation to obtain the first internal signal S.

[0052] In a specific embodiment, refer to Figure 7 The second softmax function calculation unit includes a highest-order bit detector, a second shifter, a second fitting calculator, an Ln constant multiplier, and an adder. The first internal signal is input to the highest-order bit detector for highest-order bit detection, obtaining the highest-order bit result v. The first internal signal and the highest-order bit result v are input to the second shifter, which performs v right shifts on the first internal signal to obtain a second right shift result R2. The second right shift result is input to the second fitting calculator, which performs a base-2 logarithmic function calculation on the second right shift result R2 to obtain a second fitting result b. The highest-order bit result v is then input to the Ln constant multiplier to achieve the desired result. The constant multiplication is performed to obtain the second constant multiplication result c. The second fitting result b and the second constant multiplication result c are then input into an adder for addition to obtain the second internal signal. .

[0053] In a specific embodiment, refer to 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 process the input... accomplish The constant multiplication is used to obtain the third constant multiplication result, where n' represents the integer part of the third constant multiplication result and f' represents the fractional part of the third constant multiplication result. The fractional part of the third constant multiplication result is input into the third fitting calculator, and the fractional part of the third constant multiplication result is calculated using a power function with base 2 to obtain the 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 shifted to the right n' times to obtain the 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 embodiments of this application is as follows: First, the power function and the logarithmic function are simplified through mathematical transformation, and the calculation error of the hardware approximation constant multiplication is introduced to generate an error correction term in the fitting calculation, thereby improving the accuracy. The general 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 calculation process of power functions, the embodiments of this application introduce... The constant multiplication replaces the power function with the power function with the power function with the power function with the power function with the power function with the power function of 2, as shown in formula (6).

[0056] (6)

[0057] Since hardware typically performs constant multiplication based on approximate calculations, errors are introduced. Embodiments of this application define the error of hardware approximation calculations as follows: As shown in formula (7), where This is a full-precision floating-point result. This is a hardware approximation calculation result, where h is the bit width of the constant multiplication in the hardware. The lower h is, the greater the error. The larger.

[0058] (7)

[0059] Will By introducing formula (6) and further transforming it, we can obtain formula (8):

[0060] (8)

[0061] make ,in The integer part For the decimal part, this step is implemented by the Log2 constant multiplier, so formula (8) can be further transformed into formula (9):

[0062] (9)

[0063] in This represents a right shift operation, which is implemented by a shifter.

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

[0065] (10)

[0066] In formula (10) Depend on Therefore, once the bit width h of constant multiplication in the hardware is determined, It is a constant value. In formula (10) Depend on and Sure, The range of input data can be determined in advance by statistical analysis, therefore It is also a constant value before hardware calculation. In formula (10) For input data with a small range of variation ( Furthermore, for nonlinear functions that already include penalty coefficients, the calculation can be completed using only low-complexity linear fitting or lookup table calculations. The high-precision calculation is performed by the fitting calculator.

[0067] In summary, when implementing the first and second modules for power function calculation, the first Log2 constant multiplier or the second Log2 constant multiplier is used to implement... or Where f or f' is passed to the first or third fitting calculator to implement or To obtain either a or a', n and a or n' and a' are simultaneously passed into the first or third shifter. or Obtain R1 or R in the first module And pass it to the accumulator to implement The first internal signal S is obtained; in the third module, This allows you to directly obtain the high-precision output data of the softmax function.

[0068] The embodiments of this application introduce the following for calculating the logarithmic function. Constant multiplication replaces the logarithmic function with a base of e to a base of 2. This step is implemented by the Ln constant multiplication unit, as shown in formula (11):

[0069] (11)

[0070] Where S is the cumulative term in formula (1). .make Where v is an integer, which can be obtained from the highest bit detector, and u takes values ​​in the range [1,2). Therefore, formula (11) can be further transformed into formula (12):

[0071] (12)

[0072] Since hardware typically performs constant multiplication based on approximate calculations, introducing errors that affect computational accuracy, embodiments of this application define this error as... As shown in formula (13), where This is a full-precision floating-point result. This is the hardware approximation result, where h is the bit width of the constant multiplication in the hardware.

[0073] (13)

[0074] In formula (13) By introducing formula (12) and further transforming it, we can obtain formula (14):

[0075] (14)

[0076] Define penalty items Thus, formula (14) is further simplified to formula (15):

[0077] (15)

[0078] In formula (15) Depend on Once the hardware bandwidth h is determined, it becomes a constant. Depend on And v is determined, where v can be determined in advance by statistically analyzing the range of the input data, therefore It is also a constant value before hardware calculation. In formula (15) For input data with a small range of variation ( The nonlinear function can be calculated with high precision using only low-complexity linear fitting calculation or lookup table calculation, which is implemented by the second fitting calculator; The result is achieved by an Ln constant multiplier; finally, the result of the second fitting calculator and the result of the Ln constant multiplier are added together by an adder to obtain the output Ln(S).

[0079] In summary, when implementing the second module for 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 the calculation. c is obtained; R² is then passed to the second fitting calculator for implementation. We obtain 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 specific embodiments of this application have been described above, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A high-precision approximation calculation device for the softmax function, characterized in that, The softmax function, after mathematical equivalence transformation, is shown in the following equation: The input data for the softmax function is a vector of length M. , i=0,1,…,M-1 Let X be the maximum value in 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. The third module includes a second zero-value prediction unit, a third softmax function calculation unit, and a second selector unit. and Input to the first zero-value prediction unit, output the first zero-value signal or The first zero-value prediction unit includes a first subtractor and a first judge connected in sequence. The first subtractor is used to... and Perform the subtraction operation to obtain , ,Will In the input detector, the detector is used to determine... Does the zero-value prediction formula satisfy the condition? If it does, the first zero-value prediction unit outputs the first zero-value signal; if not, the first zero-value prediction unit outputs... , The input is processed in the first softmax function calculation unit to obtain the first internal signal S, which is then selected by the first selector unit and input to the second module. The zero-value prediction formula is as follows: ; Where J is or N is the bit width after quantization by the softmax function; the first zero-value signal or the first internal signal is input to the first selector unit, which 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 as the output data of the first module to the second softmax function calculation unit to obtain the second internal signal. , The second internal signal is input to the second zero-value prediction unit, and the second zero-value signal is output or ,Will The third softmax function calculation unit is input to obtain a result signal. The second zero value signal or the result signal is input to the second selector unit. The second selector unit is used to select 0 as the output data of the device based on the second zero value signal, or the result signal as the output data of the device.

2. The high-precision approximation calculation device for the softmax function according to claim 1, characterized in that, The second zero-value prediction unit includes a second subtractor and a second judge connected in sequence. The second subtractor is used to... , Subtract the second internal signal to obtain , , Indicates the second internal signal, The input is given to the second judge, which is used to determine... Does the zero-value prediction formula satisfy the condition? If it does, the second zero-value prediction unit outputs a second zero-value signal; if not, the second zero-value prediction unit outputs... .

3. The high-precision approximation 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 process the input... accomplish The constant multiplication is performed to obtain the first constant multiplication result, where n represents the integer part of the first constant multiplication result and f represents the fractional part of the first constant multiplication result. The fractional part of the first constant multiplication result is input into the first fitting calculator, and the fractional part of the first constant multiplication result is calculated using a power function with base 2 to obtain the 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, and the first fitting result a is shifted right n times to obtain the first right shift result R1. The first right shift result R1 obtained each time is input into the accumulator for accumulation to obtain the first internal signal S.

4. The high-precision approximation calculation device for the softmax function according to claim 1, characterized in that, The second softmax function calculation unit includes a highest-order bit detector, a second shifter, a second fitting calculator, an Ln constant multiplier, and an adder. The first internal signal S is input to the highest-order bit detector for highest-order bit detection, obtaining the highest-order bit result v. The first internal signal and the highest-order bit result v are input to the second shifter, which performs v right shifts on the first internal signal S, obtaining a second right shift result R2. The second right shift result is input to the second fitting calculator, which performs a base-2 logarithmic function calculation on the second right shift result R2, obtaining a second fitting result b. The highest-order bit result v is then input to the Ln constant multiplier to achieve the desired result. The constant multiplication is performed to obtain the second constant multiplication result c. The second fitting result b and the second constant multiplication result c are then input into the adder for addition to obtain the second internal signal. .

5. The high-precision approximation 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 process the input... accomplish The constant multiplication is performed to obtain the third constant multiplication result, where n' represents the integer part of the third constant multiplication result and f' represents the fractional part of the third constant multiplication result. The fractional part of the third constant multiplication result is input into the third fitting calculator, and the fractional part of the third constant multiplication result is calculated using a power function with base 2 to obtain the 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 shifted right n' times to obtain the result signal.

6. The high-precision approximation calculation device for the softmax function according to claim 1, characterized in that, In the first selector unit, if the input data of the first selector unit is the first zero value signal, then 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 selector unit is the first internal signal, then the first internal signal is used as the output data of the first module.

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

Citation Information

Patent Citations

  • 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