Implementation method and equipment for activation function
Through the design of piecewise linear functions and parallel computing, the problems of high computational overhead and low efficiency in activation function implementation are solved, and efficient activation function calculation is achieved, which is suitable for resource-limited hardware.
Patent Information
- Application Number
- CN202410880349.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2044-07-02
AI Technical Summary
Existing activation function implementations are computationally expensive and inefficient, especially in hardware with limited storage and computing resources, resulting in high memory usage, accuracy limitations, poor scalability, and inefficient approximate model fitting speed and energy consumption.
Using the design of piecewise linear function, the target segment is determined by obtaining the input value, and the segment parameters are obtained for calculation. Simple addition, subtraction, multiplication, and division operations are used to avoid complex nonlinear operations, and the approximation of the activation function is achieved. Linear segmentation is used to Parallel computing of functions improves efficiency.
It significantly reduces memory usage, improves computing efficiency, avoids complex nonlinear operations, is suitable for resource-constrained hardware, and improves the execution efficiency of neural networks.
Smart Images

Figure CN118709728A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence, and in particular to a method and device for realizing an activation function. Background Art
[0002] With the development of society and technology, neural networks have been applied in more and more fields and have made good progress. The activation function plays an important role in neural network calculations.
[0003] The activation function is essentially a nonlinear transformation that maps the weighted input of a neuron to its output. In the development of information processing and artificial intelligence systems, the selection and fitting of nonlinear transformation functions has always been an important research topic. Early information processing models used simple threshold functions as nonlinear transformation functions. However, with the cross-integration of information theory and statistical physics, people have found that nonlinear transformation functions are closely related to probability distribution functions in statistical physics.
[0004] Currently, activation functions are generally implemented through activation function approximation methods, which are a type of technology that accelerates neural network inference calculations by approximating complex nonlinear functions such as sigmoid, tanh, and ReLU with simple, hardware-friendly functions. Common approximation methods include polynomial approximation, lookup table method, and quantization method. These methods significantly reduce computing and storage overhead while maintaining model accuracy, and can be used in conjunction with network pruning, weight quantization, and other technologies to improve model execution efficiency.
[0005] However, the inventors have found through research that the existing activation function implementation method still has the problems of high computational overhead and low efficiency. Summary of the invention
[0006] The technical problem solved by the present invention is how to solve the problems of high computational overhead and low efficiency in the current activation function implementation method.
[0007] To solve the above technical problems, an embodiment of the present invention provides a method for implementing an activation function, the method comprising: obtaining an input value; determining a target segment corresponding to the input value, the target segment being one or more segments in a piecewise linear function; obtaining parameters of the function corresponding to the target segment; calculating a calculation result based on the parameters and the input value; and obtaining an output value according to the calculation result.
[0008] Optionally, determining the target segment corresponding to the input value includes: obtaining the number of segments corresponding to the input value, selecting the target segment according to the number of segments, and the target segment is a segment in a piecewise linear function; obtaining the output value according to the calculation result includes: using the calculation result as the output value.
[0009] Optionally, the target segment is a plurality of segments in the piecewise linear function, and obtaining an output value according to the calculation result includes: selecting one from the obtained calculation results as the output value.
[0010] Optionally, the calculation result is obtained by executing the calculation based on the parameters and input values of different target segments through different thread processing.
[0011] Optionally, the parameters include one or more of the endpoint, slope and offset of this segment of the function.
[0012] Optionally, the slope and / or bias is expressed as 2 to the nth power, where n is represented by a one-hot code.
[0013] Optionally, the method also includes: if the piecewise linear function is an odd function, determining whether the input value is located on the negative half axis; in response to the input value being located on the negative half axis, obtaining the output value according to the calculation result, including: inverting the calculation result to obtain the output value.
[0014] Optionally, the piecewise linear function includes a first segment and a second segment, and the method further includes: obtaining the difference between the parameters of the first segment and the parameters of the second segment; and obtaining the parameters of the second segment based on the parameters of the first segment and the difference.
[0015] In the second aspect, the present application also provides an implementation device of an activation function, including an input end, a target segment determination module, an adder, a memory, a calculator and an output end, wherein: the input end is used to obtain an input value; the target segment determination module is used to determine the target segment corresponding to the input value, and the target segment is one or more segments in a piecewise linear function; the memory is used to store parameters of each segment function in the piecewise linear function; the calculator is used to calculate the calculation result based on the parameters and the input value; the output end is used to output the output value.
[0016] Optionally, the device also includes a first selector, which is used to obtain the number of segments corresponding to the input value, and select a target segment according to the number of segments, wherein the target segment is a segment in a piecewise linear function; the output value is the calculation result.
[0017] Optionally, the target segment is a plurality of segments in the piecewise linear function, and the device further includes a second selector, and the second selector is used to select at least one from the obtained calculation results as the output value.
[0018] Optionally, the calculator includes a shifter and an adder.
[0019] Optionally, the device further includes a negation module, which is used to negate the calculation result to obtain the output value when the piecewise linear function is an odd function and the input value is located on the negative semi-axis.
[0020] In a third aspect, the present application also provides a field programmable gate array chip, characterized in that the field programmable gate array chip is used to implement the activation function implementation method described in any one of the above items.
[0021] Compared with the prior art, the technical solution of the embodiment of the present invention has the following beneficial effects:
[0022] The implementation method of the activation function provided by the embodiment of the present invention realizes the maximum approximation of the activation function while reducing memory usage by designing the segmented intervals of the linear function and selecting specific parameters. The linear piecewise function is a linear function in each segment, and the calculation process only involves simple basic operations such as addition, subtraction, multiplication, and division, avoiding complex nonlinear operations such as exponential, logarithmic, trigonometric functions, etc., and the calculation efficiency is higher. The linear piecewise function can be represented by a small number of parameters such as segment endpoints and segment slopes, biases, etc., and the storage overhead is very small, and there is no need to store a large number of value pairs like a lookup table (LUT).
[0023] Furthermore, after obtaining the input value, the linear function of the target segment corresponding to the input value is first accurately determined, and at least one segment is selected from the piecewise linear function as the target segment. In the subsequent steps, only the mathematical expression of the selected target segment is calculated to obtain a calculation result, which can be directly used as the output value. Therefore, the linear function of the target segment corresponding to the input value is first accurately determined, and then the function-related calculation is performed to obtain the output value, which can effectively save the amount of calculation.
[0024] Furthermore, before performing function calculation, the precise target function is not selected, but the function calculation is performed on multiple target segments, and finally the output value is selected from the function calculation results of multiple target segments. The most suitable output result can be selected based on the input value and multiple calculation results. Furthermore, since each segment of the piecewise linear function is independent, the parallel calculation of the linear piecewise function is performed by different threads, the calculation is relatively simple, and the calculation efficiency can be significantly improved. In the calculation of neural networks and big data, multi-threaded work is also very necessary.
[0025] Furthermore, for odd functions, it is not necessary to calculate all partitions. After calculating the positive partition, the negative semi-axis can be directly inverted to obtain the corresponding function value. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 is a flow chart of a method for implementing a first activation function according to an embodiment of the present invention;
[0027] Figure 2 is a flow chart of a method for implementing a second activation function according to an embodiment of the present invention;
[0028] Figure 3 is a flow chart of a method for implementing a second activation function according to an embodiment of the present invention;
[0029] Figure 4 is a structural diagram of a device for implementing the first activation function according to an embodiment of the present invention;
[0030] Figure 5 is a structural diagram of a device for implementing the second activation function according to an embodiment of the present invention;
[0031] Figure 6 It is a structural diagram of a device for implementing the third activation function in an embodiment of the present invention. DETAILED DESCRIPTION
[0032] As mentioned in the background technology, the existing activation function implementation method still has the problems of high computational overhead and low efficiency.
[0033] Specifically, nonlinear function fitting aims to approximate complex nonlinear functions such as sigmoid, tanh, and ReLU with simple, hardware-friendly mathematical functions, while maintaining network accuracy and reducing computational and storage overhead. Polynomial approximation uses low-order polynomial approximation; the Look-Up Table (LUT) method pre-calculates and stores outputs under different inputs. These techniques can be combined with pruning, quantization and other methods to significantly improve the execution efficiency of neural networks.
[0034] Existing hardware still uses the traditional lookup table (LUT) method. In existing hardware implementations, the lookup table (LUT) is a commonly used technology for implementing nonlinear transformation functions (such as activation functions). LUT is essentially a method of pre-calculating and storing function output values. It divides the input range of the function into several intervals and stores the output value corresponding to each interval in a table, thereby achieving approximation of the function. In many hardware designs, especially in resource-constrained embedded systems and application-specific integrated circuits (ASICs), LUT technology is widely used to implement various nonlinear functions, such as sigmoid, hyperbolic tangent (tanh), inverse tangent (arctan), etc.
[0035] The inventors have found through research that the LUT method has the following disadvantages:
[0036] Disadvantage 1: Precision limitation. LUT is a discretized approximation of a function, and its precision is limited by the number of table entries and the interpolation method. Increasing the number of table entries can improve precision, but at the same time it will increase memory usage. Therefore, there is a trade-off between LUT precision and resource usage.
[0037] Disadvantage 2: Memory usage. Memory usage is a major limitation of LUT technology, especially when dealing with high-dimensional functions. For an n-dimensional function, if the input range of each dimension is divided into m intervals, the number of table entries that the LUT needs to store will be m^n. As the dimension n increases, the number of table entries increases exponentially. This means that even for a medium-sized LUT, the storage space required may become very large.
[0038] For example, suppose we want to implement a 5-dimensional function, and the input range of each dimension is divided into 100 intervals. Then the number of entries that the LUT needs to store will reach 100^5=10,000,000,000 (10 billion). Assuming that each entry needs to occupy 4 bytes (single-precision floating point number), the total storage space required for this LUT will reach 40GB. This is a huge overhead for many embedded systems and dedicated hardware, and it is even unacceptable.
[0039] In addition to the number of entries, the bit width of the LUT (the number of bits each entry occupies) also affects its memory usage. The bit width is usually related to the precision of the LUT. If higher precision is required, more bits are needed to represent each function value, which further increases the memory usage of the LUT.
[0040] In addition, large-capacity LUTs not only take up a lot of storage resources, but also affect the power consumption and cost of the system. Large-capacity memories usually have higher power consumption and require more hardware resources to address and read. This is an important consideration for battery-powered embedded devices or power-sensitive application scenarios.
[0041] Disadvantage 3: Poor scalability. LUT is designed for specific functions and specific input ranges. If you need to change the form of the function or expand the input range, you need to regenerate and store the LUT, which reduces the flexibility and reusability of the design.
[0042] In addition, the inventors have also found through research that the prior art has the following problems:
[0043] Problem 1: Inefficient approximate model fitting speed and energy consumption.
[0044] This is also because the traditional iterative algorithm based on series expansion has the following disadvantages:
[0045] Long latency. First, iterative algorithms usually require multiple iterations to achieve the required accuracy. The more iterations, the longer the calculation time, resulting in increased latency. Each iteration requires a series of arithmetic operations and storage access operations, which consume clock cycles and accumulate to significantly increase the total latency. In real-time systems or high-speed applications, excessively long iteration latency may not meet timing requirements, affecting the system's response speed and throughput.
[0046] Secondly, the series expansion in iterative algorithms usually involves a large number of multiplication and addition operations. The execution time of these arithmetic operations will directly affect the delay of each iteration. High-precision arithmetic operations (such as floating-point operations) usually take longer to execute because they require more complex circuit structures and more clock cycles to complete. If dedicated arithmetic units such as multipliers and adders are used in the iterative algorithm, their delay characteristics (such as the pipeline stage of the multiplier) will also affect the delay of the entire iteration process.
[0047] In addition, iterative algorithms need to frequently read and write intermediate results and status information, which involves access operations to on-chip memory (such as SRAM or register files). The access time (read and write latency) of the memory will affect the latency of each iteration, and a larger memory capacity usually means a longer access time. If there is data dependency in the algorithm (i.e., the current iteration depends on the result of the previous iteration), then the access latency of the memory will become a key factor affecting the entire iteration process.
[0048] On the other hand, iterative algorithms require additional control logic to manage the iteration process, including loop control, conditional judgment, state update, etc. Complex control logic circuits usually require more logic levels and longer combinational logic paths, which will increase the propagation delay of control signals. If the control logic delay is large, it will affect the start and completion time of each iteration, and thus affect the delay of the entire iteration process.
[0049] Finally, the hardware implementation of an iterative algorithm usually requires that each iteration be completed within a limited number of clock cycles. The choice of clock frequency directly affects the latency of the iteration process. A lower clock frequency means that each clock cycle lasts longer, allowing more time for arithmetic operations and memory accesses, but it also increases the overall iteration latency. If the critical path of the iterative algorithm is long (i.e., there is a long chain of combinational logic), then the clock frequency may need to be reduced to meet the timing constraints, which further increases the latency.
[0050] In summary, the iterative algorithm based on series expansion has a large delay problem when implemented in hardware due to the large number of iterations, complex arithmetic operations, frequent storage accesses, and control logic overhead. Long iteration delays will affect the real-time performance and response speed of the system, limiting the use of this algorithm in delay-sensitive applications.
[0051] Each iteration requires multiplication and addition operations, which requires additional multiplier and adder circuits, increasing the cost of hardware implementation. The iterative process requires additional control logic and storage units to manage states and intermediate results, increasing hardware complexity. Some functions may have multiple fixed points, and the iterative algorithm may converge to the wrong fixed point, affecting the approximate accuracy. Access requires three steps: encoding, storage, and decoding. The latency is usually large, which is not conducive to high-speed computing. SRAM or ROM storage is generally used, and the storage unit area and power consumption are large.
[0052] The second problem is that there is insufficient computing power for complex calculations with higher precision.
[0053] It is not conducive to the calculation of complex functions. As the complexity of the function increases, the required storage space will grow exponentially. It can only map discrete input values. For functions with continuous value ranges, it can only be implemented through interpolation or approximation, which will affect the accuracy.
[0054] To solve the above technical problems, first of all, please refer to Figure 1 , an embodiment of the present invention provides a first method for implementing an activation function, the method comprising the following steps S101 to S105, which are specifically described as follows.
[0055] Step S101, obtaining input value;
[0056] Step S102, determining a target segment corresponding to the input value, wherein the target segment is one or more segments in a piecewise linear function;
[0057] Step S103, obtaining parameters of the function corresponding to the target segment;
[0058] Step S104, calculating a calculation result based on the parameters and the input value;
[0059] Step S105, obtaining an output value according to the calculation result.
[0060] Determine the piecewise linear function of the activation function fitting, divide the positive semi-axis of the entire domain of the piecewise linear function into smaller sub-intervals, perform linear fitting operation on the function value of each sub-interval, and obtain the linear function of each section (or segment). The mathematical expression can be expressed as y=ax+b, where the specific values of related coefficients, such as slope a and bias (or intercept) b, can be determined by previous matlab fitting simulation, and the coefficients of each linear function are stored.
[0061] The input value can be used as the independent variable in a piecewise linear function, and the calculation result is the dependent variable obtained from the mathematical expression based on the piecewise linear function. For example, the mathematical expression of a piecewise linear function is y = 32x + 1024. Taking the input value as x, the calculated y value is the calculation result.
[0062] In Figure 1 the method, by designing the piecewise intervals of the linear function and selecting specific parameters, realizes the maximum approximation of the activation function while reducing memory occupancy. The piecewise linear function is a linear function within each section, and the calculation process only involves basic operations such as addition, subtraction, multiplication, and division, avoiding complex non-linear operations such as exponentiation, logarithm, and trigonometric functions, and has higher calculation efficiency. The piecewise linear function can be represented by a small number of parameters such as the section endpoints, section slopes, and biases, with very small storage overhead, and there is no need to store a large number of numerical pairs like a look-up table (LUT).
[0063] After fitting the activation function and implementing it on hardware, the piecewise linear function is easier to implement in a hardware circuit due to its simple calculation.
[0064] In a specific embodiment, the parameters include one or more of the endpoints, slopes, and biases of this section of the function. It should be noted that the parameters of the linear function can also include other parameters, which will not be elaborated here.
[0065] This application is mainly a hardware implementation method of a piecewise linear function with extremely low overhead. All operations on the input are implemented with 2^n. For example, for the first piecewise linear function, the mathematical expression is y = 64x, where the value range of x is 0 < x <= 32; for the second piecewise linear function, the mathematical expression is y = 32x + 1024, where the value range of x is 32 < x <= 64; for the third piecewise linear function, the mathematical expression is y = 8x + 2560, where the value range of x is 64 < x <= 196; for the fourth piecewise linear function, the mathematical expression is y = 4095, where the value range of x is x > 196. All operands are 2^n, so that only shifting and addition are involved in the circuit implementation. Compared with the traditional multiplier and adder / look-up table method, the hardware overhead of the dedicated integrated circuit designed by this method is very low. For example, in the first piecewise linear function, the slope is 64, which can be shifted left by 4 bits by a shifter. Another example is that in the second piecewise linear function, the slope is 32, which can be shifted left by 5 bits by a shifter, and adding 1024 means adding 1 at the 10th bit, and so on. For all activation functions, whether odd or even functions, this method can be used for fitting.
[0066] In a specific embodiment, the slope and / or bias is expressed as 2 to the power of n, and n is represented by a one-hot code. Among them, a one-hot code is a code system in which there are as many bits as there are states, and only one bit is 1, and the others are all 0. In the present application, the slope and / or bias of N-segment linear functions can be expressed as 2 to the power of n, where N is the number of one-hot codes, so 2^n states of the slope or bias can be represented by N bits of the one-hot code. As a result, the number of bits for representing the slope and bias can be greatly saved, further saving overhead.
[0067] In one embodiment, see Figure 2 , Figure 2 This is a flow chart of a method for implementing the second activation function of the present application. The method may include the following steps:
[0068] Step S101, obtaining input value;
[0069] Step S201, obtaining the number of segments corresponding to the input value, and selecting a target segment according to the number of segments, wherein the target segment is a segment in a piecewise linear function;
[0070] Step S103, obtaining parameters of the function corresponding to the target segment;
[0071] Step S104, calculating a calculation result based on the parameters and the input value;
[0072] Figure 1 Step S105 in the method may include: step S202, taking the calculation result as the output value.
[0073] In this embodiment, after obtaining the input value, the linear function of the target segment corresponding to the input value is first accurately determined, and at least one segment is selected from the piecewise linear function as the target segment. In the subsequent steps S103 and S104, only the mathematical expression of the selected target segment is calculated to obtain a calculation result, which can be directly used as the output value. Therefore, the linear function of the target segment corresponding to the input value is first accurately determined, and then the function-related calculation is performed to obtain the output value, which can effectively save the amount of calculation.
[0074] In one embodiment, see Figure 3 , Figure 3 This is a flow chart of a method for implementing the third activation function of the present application. This method may include the following steps:
[0075] Step S101, obtaining input value;
[0076] Step S301, determining a target segment corresponding to the input value, wherein the target segment is a plurality of segments in the piecewise linear function;
[0077] Step S103, obtaining parameters of the function corresponding to the target segment;
[0078] Step S104, calculating a calculation result based on the parameters and the input value;
[0079] Figure 1 Step S105 in the method may include: Step S302, selecting one of the obtained calculation results as the output value.
[0080] according to Figure 3 In a specific embodiment provided, after obtaining the input value, all linear function segments are first used as target segments, and the function value of each target segment is calculated to obtain the function value (i.e., calculation result) of each function segment, so that for the same input value, each function segment based on the piecewise linear function can obtain a calculation result, and the number of calculation results is the same as the number of function segments. After obtaining multiple calculation results, one of the multiple calculation results is selected as the output value according to the input value and each calculation result.
[0081] according to Figure 3 In another specific embodiment provided, after obtaining the input value, several similar segments are first selected from all linear function segments as target segments, and the function value of each target segment is calculated to obtain the function value (i.e., calculation result) of each function segment. Thus, for the same input value, multiple target segments based on the piecewise linear function can obtain a calculation result, and the number of calculation results is the same as the number of target segments. After obtaining multiple calculation results, one of the multiple calculation results is selected as the output value according to the input value and each calculation result.
[0082] In this embodiment, before performing function calculation, the precise target function is not selected, but the function calculation is performed on multiple target segments, and finally the output value is selected from the function calculation results of the multiple target segments. Finally, the most suitable output result can be selected based on the input value and multiple calculation results.
[0083] Furthermore, calculation results are obtained by executing the parameters and input values based on different target segments through different thread processing.
[0084] For example, after obtaining the input value, all 16 linear function segments are taken as target segments, and then 16 threads can be called, and each thread performs the calculation work of each linear function segment respectively.
[0085] Therefore, since each segment of the piecewise linear function is independent, the parallel calculation of the linear piecewise function is performed by different threads, which is relatively simple and can significantly improve the calculation efficiency. In the calculation of neural networks and big data, multi-threading is also very necessary.
[0086] In one embodiment, Figure 1 or Figure 2 or Figure 3 The method may further include: if the piecewise linear function is an odd function, determining whether the input value is located on the negative half axis; in response to the input value being located on the negative half axis, the step S105 obtains an output value according to the calculation result, which may include: inverting the calculation result to obtain the output value.
[0087] For example, the activation function tanh is an odd function. By symmetrically negating the positive interval, it is still valid when it is less than 0. For odd functions, there is no need to calculate all partitions. After calculating the positive partition, directly negate the negative semi-axis to get the corresponding function value. For hardware saving considerations, when the activation function is fitted as a piecewise linear function, only the positive semi-axis is calculated. When the activation function is an even function, there is no need to negate the calculation. When the activation function is an odd function, add the negation calculation after the function calculation module.
[0088] In one embodiment, the piecewise linear function includes a first segment and a second segment, and the method further includes: obtaining the difference between the parameters of the first segment and the parameters of the second segment; and obtaining the parameters of the second segment based on the parameters of the first segment and the difference.
[0089] Specifically, the parameters of one segment of a piecewise linear function (such as endpoints, slope, intercept, etc.) can be obtained by the difference between the parameters of another segment and its difference. Therefore, it is not necessary to store the parameters of each segment of the linear function, but only to store the parameters of several segments of the linear function and the corresponding differences, and then the parameters of each segment of the linear function can be obtained by simple addition or subtraction operations.
[0090] In a specific embodiment, only the parameters of one segment of the linear function are stored, and the parameters of the other segments of the linear function can be calculated based on the difference.
[0091] In another specific embodiment, for the parameter of the endpoints of each segment function in the piecewise linear function, only the endpoints of one or more segment functions are stored, and the endpoints of other segment functions are obtained by difference calculation.
[0092] It should be noted that other parameters can also be stored and calculated based on the above method, which will not be described in detail here.
[0093] Therefore, the parameters of one segment of the piecewise linear function (such as endpoints, slope, intercept, etc.) can be obtained by the difference between the parameters of another segment and its value, which can effectively save storage overhead. However, since the parameters of some functions need to be calculated, some computational efficiency may be sacrificed.
[0094] For the implementation method of the first activation function, the present application also provides an implementation device 40 of the first activation function, see Figure 4 The first activation function implementation device 40 includes an input terminal 401, a target segment determination module 402, a memory 403, a calculator 404 and an output terminal 405, wherein: the input terminal 401 is used to obtain an input value; the target segment determination module 402 is used to determine the target segment corresponding to the input value, and the target segment is one or more segments in a piecewise linear function; the memory 403 is used to store the parameters of each segment function in the piecewise linear function; the calculator 404 is used to calculate the calculation result based on the parameters and the input value, that is, to execute the calculation result of the linear function (referred to as function calculation); the output terminal 405 is used to output the output value.
[0095] Optionally, the calculator 404 includes a shifter, and the shifter is used to implement multiplication and division calculations of the linear function.
[0096] Optionally, the calculator 404 includes a shifter and an adder, and the calculation of the linear function is implemented through the shifter and the adder.
[0097] Optionally, the implementation device 40 of the first activation function may further include a negation module, wherein the negation module is used to negate the calculation result to obtain the output value when the piecewise linear function is an odd function and the input value is located on the negative semi-axis.
[0098] For more information about the working principle and working method of the first activation function implementation device 40, please refer to the above Figure 1 The description of the implementation method of the first activation function in will not be repeated here.
[0099] In one embodiment, the present application also provides a second activation function implementation device, in Figure 4 A first selector is added to the activation function implementation device 40, and the first selector is used to obtain the number of segments corresponding to the input value, and select a target segment according to the number of segments, and the target segment is a segment in a piecewise linear function; the output value is the calculation result.
[0100] Specifically, before the calculator performs function calculation, the first selector accurately determines the linear function of the target segment corresponding to the input value, and selects at least one segment from the piecewise linear function as the target segment.
[0101] See also Figure 5 , Figure 5A schematic diagram of an embodiment of a device for implementing a second activation function is provided. An input value is input into the device from an input terminal, and a segment of a linear function is selected as a target segment through a first selector. Then, a function calculation of the target segment function is implemented based on a shift register and an adder, and a calculation result is obtained as an output value, which is output at an output terminal.
[0102] Optionally, the parameters of the target segment function (including slope and bias) can be stored in a static random access memory (SRAM). After the target segment is determined, the parameters of the target segment function are obtained from the SRAM, and the function calculation of the target segment function is implemented by a shift register and an adder.
[0103] Optionally, if the piecewise linear function is an odd function and the input value is located on the negative semi-axis, the calculation result needs to be passed through a negation module to obtain the output value.
[0104] The implementation device of the second activation function can be used to execute the implementation method of the second activation function. For the relevant description of the implementation device of the second activation function, please refer to Figure 2 The description of the implementation method of the second activation function in will not be repeated here.
[0105] In one embodiment, the present application also provides a second activation function implementation device, wherein the target segment is a plurality of segments in the piecewise linear function. Figure 4 A second selector is added to the activation function implementation device 40, and the second selector is used to select at least one from the obtained calculation results as the output value.
[0106] Specifically, after obtaining the input value, all linear function segments are first taken as target segments, and the function value of each target segment is calculated to obtain the function value (ie, calculation result) of each function segment.
[0107] See also Figure 6 , Figure 6 A schematic diagram of the structure of an embodiment of a device for implementing the third activation function is provided. An input value is input into the device from the input end, and the function calculation of each piecewise linear function is implemented based on n segmented calculators (each segmented calculator includes a shift register of the segment and an adder of the segment, for example, the calculator of segment 1 includes a shift register of segment 1 and an adder of segment 1), and n calculation results are obtained. Then, the n calculation results are input into the second selector, and an output value is obtained at the output end.
[0108] Figure 6In the implementation device of the activation function, although all are combinatorial logic, there is actually a certain delay. In order to ensure the output speed, after the input value is input, the calculation results of all piecewise linear functions are calculated first, and then the second selector is used to select which piece of calculation results to output.
[0109] Optionally, if the piecewise linear function is an odd function and the input value is located on the negative half axis, the calculation result needs to be passed through a negation module and then sent to the second selector.
[0110] Optionally, the parameters of each linear function can be implemented by corresponding shift registers and registers related to the adder. When implementing a fixed activation function, such as the tanh function, the fitted piecewise linear function of the tanh function can be reused. Therefore, the corresponding n sets of calculators can be customized to implement the activation function.
[0111] Optionally, the parameters of the target segment function (including slope and bias) can be stored in a static random access memory (SRAM), and the parameters of each segment of the linear function are obtained from the SRAM, and the function calculation of the corresponding function is implemented by a shift register and an adder.
[0112] The implementation device of the third activation function can be used to execute the implementation method of the third activation function. For the relevant description of the implementation device of the third activation function, please refer to Figure 3 The description of the implementation method of the third activation function in will not be repeated here.
[0113] It should be noted that the activation function implementation devices provided in this application are all implemented through highly coupled hardware and circuits, and a computationally efficient activation function implementation device is provided through simple hardware circuits such as adders, shifters, negation modules, selectors, etc.
[0114] In addition, in the application, the inventors found through research that there is a lack of hardware fitting methods for probability calculation activation functions in the prior art. Specifically, probability calculation is different from the traditional von Neumann architecture in terms of model parameter scale and computational complexity, so it is necessary to design a dedicated activation function fitting method based on its characteristics. There is currently a lack of hardware accelerators designed specifically for probability calculations on the market, which limits the effective operation and deployment of the Ising model on hardware platforms.
[0115] To solve the above problems, in an application example, the activation function implemented in this application can be used in probability computing, which is an emerging computing paradigm that uses probability models and random processes to provide new ideas and methods for solving combinatorial optimization problems. By mapping the problem into a probabilistic framework and combining classical probabilistic graph models such as the Ising model, probabilistic computing can effectively jump out of the local optimum and find an approximate optimal solution within a reasonable time. With the development of new technologies such as quantum computing, the application of probabilistic computing in the field of combinatorial optimization has been further expanded, bringing new hope for solving large-scale combinatorial optimization problems. In-depth research and development of probabilistic computing theories and algorithms for combinatorial optimization have important theoretical and practical significance for promoting the development of fields such as operations optimization and artificial intelligence.
[0116] Statistical physics focuses on systems composed of a large number of particles, and describes the macroscopic properties of the system through probability distribution functions. Common probability distribution functions include Boltzmann distribution, Fermi-Dirac distribution, Bose-Einstein distribution, etc. These distribution functions describe the energy state distribution of particles in the system. The Sigmoid function commonly used in information processing systems has a similar mathematical form to the Fermi-Dirac distribution. The Fermi-Dirac distribution describes the probability of particles occupying energy levels in a fermion system, and its mathematical form is:
[0117] Where g(ε) is the particle energy, ε f is the chemical potential, k is the Boltzmann constant, and T is the absolute temperature.
[0118] Contrast this with the following mathematical form of the Sigmoid function:
[0119]
[0120] Among them, x is a variable and e is a natural constant.
[0121] It can be found that the Sigmoid function can be regarded as a form of Fermi-Dirac distribution under specific parameters. This similarity inspired people to introduce the concept of statistical physics into the study of information processing systems.
[0122] By linking nonlinear transformation functions with probability distribution functions, we can use the theoretical tools of statistical physics to analyze the behavior and properties of information processing systems. For example, we can analogize the state of elements in the system to the energy state of particles, and the evolution of the system to the thermodynamic evolution process. This interdisciplinary perspective provides new ideas for understanding and optimizing information processing systems.
[0123] Therefore, linking the nonlinear transformation function with the probability distribution function in statistical physics provides a new perspective for theoretical research and practical application of information processing systems. The implementation scheme of the activation function provided in this application can also realize the fitting of the sigmoid function and improve the computational efficiency.
[0124] In one embodiment, the present application also provides a field programmable gate array (FPGA) chip, which is used to implement the activation function implementation method described in any one of the present application. Since the activation function implementation method provided by the present application has a small amount of calculation and a simple structure, it can be deployed in an FPGA chip to implement lightweight operations of the activation function. The activation function implementation device of the present application can be used as part of the hardware structure of the FPGA.
[0125] In one embodiment, the present application also provides a product structure of a hardware accelerator, specifically including: FPGA chip: as the core processor of the accelerator, responsible for performing sparsely connected probability calculations. External memory: used to store model parameters and input data. Controller: used to manage data input and output, and schedule the operation process of the accelerator. PCIe interface: The device exchanges data with other components through the PCIe bus interface.
[0126] The embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to execute Figures 1 to 3 The computer readable storage medium may include a non-volatile memory or a non-transitory memory, and may also include an optical disk, a mechanical hard disk, a solid state hard disk, etc.
[0127] An embodiment of the present invention further provides a computing device, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and when the processor runs the computer program, Figures 1 to 3 The steps of implementing any activation function in .
[0128] In the embodiments of the present application, the processor may be a general-purpose processor, a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component, and may implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application may be directly embodied as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.
[0129] In the embodiments of the present application, the memory may be a non-volatile memory, such as a hard disk drive (HDD) or a solid-state drive (SSD), etc., or a volatile memory (volatile memory), such as a random-access memory (RAM). The memory is any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory in the embodiments of the present application may also be a circuit or any other device that can implement a storage function, for storing computer programs and / or data.
[0130] In the implementation method of the activation function provided in the embodiment of the present application, 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 program is 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, a network device, a user device or other programmable device. The computer program 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 instruction may be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, 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 a 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 digital video disc (DVD)), or a semiconductor medium (eg, an SSD).
[0131] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
[0132] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0133] In the above embodiments, the description of each embodiment has its own emphasis, and any multiple embodiments can be used in combination. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0134] 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 units in a processor for execution. The software unit 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 executes the instructions 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.
[0135] In the embodiment of the present application, the processor of the above-mentioned device may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0136] The present application also provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to cause a computer to execute some or all of the steps of any method described in the above method embodiment. The computer program product may be a software installation package.
[0137] In the several embodiments provided in the present application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of the above-mentioned units, which 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, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0138] The units described above 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 embodiments of the present application.
[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. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0140] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or all or part of the technical solution, can be embodied in the form of a software product, which is stored in a memory and includes several instructions for a computer device (which can be a personal computer, server or TRP, etc.) to execute all or part of the steps of the various embodiments of the present application. The aforementioned memory includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk and other media that can store program codes.
[0141] It should be understood that the term "and / or" in this article is only a description of the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article indicates that the associated objects before and after are in an "or" relationship.
[0142] The "plurality" appearing in the embodiments of the present application refers to two or more.
[0143] The first, second, etc. descriptions appearing in the embodiments of the present application are only used for illustration and distinction of the description objects. There is no order, nor do they indicate any special limitation on the number of devices in the embodiments of the present application, and cannot constitute any limitation on the embodiments of the present application.
[0144] The "connection" that appears in the embodiments of the present application refers to various connection methods such as direct connection or indirect connection to achieve communication between devices, and the embodiments of the present application do not impose any limitations on this.
[0145] The embodiments of the present application are introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A method for implementing an activation function, characterized in that: The method comprises: Get input value; Determine a target segment corresponding to the input value, wherein the target segment is one or more segments in a piecewise linear function; Obtaining parameters of the function corresponding to the target segment; Calculate the result based on the parameters and the input value; An output value is obtained according to the calculation result.
2. The method according to claim 1, characterized in that: The determining the target segment corresponding to the input value includes: Obtaining the number of segments corresponding to the input value, and selecting a target segment according to the number of segments, wherein the target segment is a segment in a piecewise linear function; The obtaining an output value according to the calculation result comprises: The calculation result is used as the output value.
3. The method according to claim 1, characterized in that The target segment is a plurality of segments in the piecewise linear function, and the output value is obtained according to the calculation result, including: One of the obtained calculation results is selected as the output value.
4. The method according to claim 3, characterized in that The calculation results are obtained by executing the calculations based on the parameters and input values of different target segments through different thread processing.
5. The method according to any one of claims 1 to 4, characterized in that: The parameters include one or more of the endpoint, slope and offset of this segment of the function.
6. The method according to claim 5, characterized in that The slope and / or bias is expressed as a power of 2 to the nth power, where n is represented by a one-hot code.
7. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: if the piecewise linear function is an odd function, determining whether the input value is located on a negative semi-axis; In response to the input value being located on the negative half axis, obtaining the output value according to the calculation result includes: The calculation result is inverted to obtain the output value.
8. The method according to any one of claims 1 to 4, characterized in that: The piecewise linear function includes a first segment and a second segment, and the method further includes: Obtaining a difference between a parameter of the first segment and a parameter of the second segment; Based on the parameters of the first segment and the difference, the parameters of the second segment are obtained.
9. A device for implementing an activation function, characterized in that: The method comprises an input terminal, a target segment determination module, an adder, a memory, a calculator and an output terminal, wherein: The input terminal is used to obtain an input value; A target segment determination module, used to determine a target segment corresponding to the input value, wherein the target segment is one or more segments in a piecewise linear function; A memory, used to store parameters of each segment function in the piecewise linear function; A calculator, used to calculate a calculation result based on the parameters and input values; Output terminal, used to output the output value.
10. The device according to claim 9, characterized in that The device also includes a first selector, which is used to obtain the number of segments corresponding to the input value, and select a target segment according to the number of segments, wherein the target segment is a segment in a piecewise linear function; the output value is the calculation result.
11. The implementation device according to claim 9, characterized in that: The target segment is a plurality of segments in the piecewise linear function, and the device further includes a second selector, and the second selector is used to select at least one from the obtained calculation results as the output value.
12. The device according to any one of claims 9 to 11, characterized in that The calculator includes a shifter and an adder.
13. The device according to any one of claims 9 to 11, characterized in that The device also includes a negation module, which is used to negate the calculation result to obtain the output value when the piecewise linear function is an odd function and the input value is located on the negative semi-axis.
14. A field programmable gate array chip, characterized in that: The field programmable gate array chip is used to implement the activation function implementation method as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Activation processing device applied to neural networks
CN108898216A
Universal computing device and method for a nonlinear function with symmetry
CN110058841A
Method and device for reducing parameter table storage space, equipment and computer readable storage medium
CN112307014A
Reconfigurable circuit applied to activation function of recurrent neural network
CN112734023A
Hardware implementation method of piecewise linear function with low hardware overhead
CN113065648A
