Exp function optimization method based on table look-up interpolation method

By optimizing the exp function based on lookup table interpolation, and utilizing SIMD instructions to load data in parallel and simplify the calculation steps, the problem of low computational efficiency of the exp function in neural networks is solved, achieving faster computation speed and higher accuracy.

CN121858844APending Publication Date: 2026-04-14HEFEI JUNZHENG TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-12
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

In existing technologies, neural networks suffer from slow data loading and cumbersome calculation steps when calculating the exp function, especially in tasks such as image recognition and face recognition, resulting in low computational efficiency.

Method used

An optimization method for the exp function based on table lookup and interpolation is adopted. By pre-creating table entries and dividing fixed intervals, data is loaded in parallel using SIMD instructions. The exp function value is quickly calculated by combining table lookup and interpolation formulas, simplifying the calculation steps.

Benefits of technology

This improves the computational efficiency of the exp function, reduces data loading time and computation steps, and enhances the computational speed and accuracy of neural networks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121858844A_ABST
    Figure CN121858844A_ABST
Patent Text Reader

Abstract

The invention provides an exp function optimization method based on a look-up table interpolation method, and the method comprises the steps: S1, pre-creating table entries, firstly ensuring that input data is in a certain fixed interval, and dividing the interval into 32 equal parts; the 32 equal parts correspond to 32 exp values and 31 differential values, the exp values and the differential values do not exceed the expression range of UINT16, and then the exp values and the differential values are stored in an array dyt and an array y0t respectively; s2, reading input data, calculating a table item index, searching a corresponding value in the table by using the index, and then extracting a nearest exp function value and a difference value from dyt and y0t; the calculation method of the index index and the interpolation item coef is as follows: uint8t index = input gt; gt; 3; uint8t coef is equal to input tap, and the coef is equal to input tap; 0 * 7; s3, a function value and a differential value are obtained through table lookup and substituted into an interpolation formula for interpolation, and the interpolation formula is inter = y0 + dy * coef; s4, calculating an exp result, wherein the result is equal to (dy * coef + y0lt); lt; 3) gt; gt; 3; s5, judging whether all input calculations are completed or not; if yes, ending; if not, returning to S2 to continue calculation. The problem that exp calculation is tedious in image recognition is solved, calculation is simplified, and high precision is reserved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer computing and processing technology, and specifically relates to an optimization method for the exp function based on table lookup interpolation. Background Technology

[0002] In existing technologies, artificial neural networks (ANNs), or simply neural networks, are mathematical or computational models that mimic the structure and function of biological neural networks. Neural networks consist of a large number of interconnected artificial neurons performing calculations. In most cases, artificial neural networks can change their internal structure based on external information, making them adaptive systems. Modern neural networks are a nonlinear statistical data modeling tool, often used to model complex relationships between inputs and outputs, or to explore patterns in data.

[0003] The lookup table method is a common program acceleration technique that trades space (memory space) for time (running time). The basic process involves storing the results of frequently accessed databases or complex floating-point calculations in a data structure (arrays are most common). Later, the relevant data can be retrieved based on given conditions. For example, storing the tangent values ​​from 1 to 360 degrees in an array means that when 45 degrees is input, only the 45th element needs to be retrieved. This saves unnecessary computation time.

[0004] Interpolation, also known as "interpolation," is a method that involves inserting function values ​​at several points within a certain interval to construct an appropriate specific function. Known values ​​are then taken at these points, and the values ​​of this specific function are used as approximate values ​​of the function f(x) at other points in the interval. This method is called interpolation.

[0005] The exp function: In advanced mathematics, the "exp" function refers to an exponential function with the natural constant e (approximately 2.71828) as its base, also known as the exponential curve. This function can be represented as exp(x) or e^x, where e is a mathematical constant, also called the base of the natural logarithm.

[0006] Neural networks (NNs) are the core of deep learning. They consist of multiple layers of neurons, each performing a specific computational task and passing the results to the next layer. Neural networks can be trained to solve various tasks, such as image classification, speech recognition, and natural language processing. After training, neural networks can guess and infer things based on new data, and apply what they've learned to other situations; this behavior is called "reasoning" in the dictionary of artificial intelligence.

[0007] The MXU3.1 instruction set is an instruction set designed by Beijing Junzheng Integrated Circuit Co., Ltd. (hereinafter referred to as Junzheng) for applications with parallel computing characteristics such as audio and video, graphics, and image signal processing. It supports SIMD instructions to accelerate data operations.

[0008] Single Instruction Multiple Data (SIMD) is a digital parallel computing method that allows a single instruction to execute multiple data streams, thereby improving the program's processing speed.

[0009] In using neural network methods, various function calculations are often required, such as summing and multiplying the input vector Z. In some tasks, it may also be necessary to solve for vector Z. i The exp function value. However, compared to simple multiplication and addition, the exp function has a complex calculation process. When a vector has only one value to be calculated, the time consumption may be acceptable. But in neural network inference, the amount of input data is often very large, reaching tens of thousands of elements. Solving the exp function for so many elements simultaneously makes the time consumption unacceptable. Currently, the mainstream method for calculating the exp function is to use Taylor expansion to calculate it element by element:

[0010] exp(Z i Let ) represent the input vector, and use Taylor expansion to solve for exp(Z). i The common approach is as follows:

[0011] Let exponent = x / ln 2

[0012] That is, e x =2 exponent Further, the exponent is split into two parts: ui and vi. ui = int(expon t) / / ui is the basic part of the exponent.

[0013] vi = expoent-ui / / vi is the remainder of expoent

[0014] Calculate 2 respectively ui and 2 vi

[0015] The basic part utilizes floating-point number storage rules.

[0016] expbasic=2 ui expbasic=(int(x*1 / ln 2))+127)<<23

[0017] The remaining part is expanded using Taylor series.

[0018] expremain=2 vi r = x - (int(x * 1 / ln 2)))

[0019]

[0020] e x =expbaisc*expremain

[0021] The corresponding sixth-order Taylor expansion code implementation is shown in the table below:

[0022]

[0023]

[0024] However, as can be seen from the above implementation code, the Taylor implementation of the exp function is obtained by repeatedly multiplying and accumulating in a loop, and each value of the Zi vector is calculated one by one each time. Therefore, the existing technology has the following problems:

[0025] 1. Slow data loading speed: When the computer reads Zi data, it reads only one element from memory at a time, which takes too long to read data from memory.

[0026] 2. Calculating the exp value of a vector is computationally very expensive. The Taylor expansion method is used for calculation, which involves many steps. Summary of the Invention

[0027] To address the aforementioned issues, the purpose of this application is to simplify the computational steps involved in existing networks, particularly in image recognition and face recognition, while maintaining high accuracy.

[0028] Specifically, the present invention provides an optimization method for the exp function based on table lookup interpolation, the method comprising the following steps:

[0029] S1. Pre-create table entries. First, ensure that the input data is within a fixed range and divide the range into 32 equal parts. The 32 equal parts correspond to 32 exp values ​​and 31 difference values, all of which do not exceed the representation range of UINT16. Then, store the exp values ​​and difference values ​​in arrays dy_t and y0_t respectively.

[0030] S2. Read the input data, calculate the table entry index, use the index to find the corresponding value in the table, and then retrieve the most recent exp function value and difference value from dy_t and y0_t;

[0031] The calculation methods for the index and the interpolation term coef are as follows:

[0032] uint8_t index = input >> 3;

[0033] uint8_t coef = input & 0x7; The corresponding SIMD implementation is:

[0034] vindex=_mx512_srlib(vinputs,3);

[0035] vcoef0=_mx512_andv(vinputs,vparam1);

[0036] S3. Obtain the function value and difference value using a lookup table, then substitute them into the interpolation formula to perform interpolation. The interpolation formula is as follows:

[0037] inter = y0 + dy * coef, where inter represents the interpolation result, y0 represents the exp function value retrieved from y0_t when index is used as the index, and dy represents the difference value corresponding to index when index is used as the index of dy_t.

[0038] S4. Calculate the exp result:

[0039] result = (dy * coef + y0 << 3) >> 3; the corresponding SIMD implementation is:

[0040] vcoef0=_mx512_extubl(vcoef0);

[0041] v16u32 vcoef1=_mx512_extuhl(vcoef0);

[0042] v16u32 vcoef2=_mx512_extuhh(vcoef0);

[0043] v16u32 vtemp0=_mx512_wumulhl(vcoef1,vdy);

[0044] v16u32 vtemp1=_mx512_wumulhh(vcoef2,vdy);

[0045] vy0 = _mx512_sllih(vy0,3);

[0046] vtemp0=_mx512_wadduhl(vtemp0,vy0);

[0047] vtemp1=_mx512_wadduhh(vtemp1,vy0);

[0048] vtemp0 = _mx512_srliw(vtemp0, 3);

[0049] vtemp1 = _mx512_srliw(vtemp1, 3);

[0050] S5. Have all the inputs been calculated? Yes: End; No: Go back to S2 to continue the calculation.

[0051] The step S1 further includes:

[0052] Read 32 numbers each time from the input address input to calculate exp, and store the result in result; expressed as:

[0053] int i = 0; Determine whether i < size is satisfied; if satisfied, execute the loop body and perform i += 32; otherwise, jump out of the loop; expressed as: for(int i = 0; i < size; i += 32);

[0054] The loop body includes:

[0055] Use the _mx512_lao instruction to read 32 numbers at once from the input address; expressed as: vinputs = _mx512_lao(vinputs, 0, input, 0);

[0056] The _mx512_lao instruction reads data from the specified memory address to the specified register, and the usage method is:

[0057] Register 1 = _mx512_lao(Register 1, register offset, memory address, memory offset); The register offset and memory offset can be 0 or 1;

[0058] Use the _mx512_andv instruction, expressed as:

[0059] v64u8 vcoef0 = _mx512_andv(vinputs, vparam1);

[0060] Where vparam1: Each Byte stores 0x7 in advance;

[0061] The andv instruction receives two registers, performs a bitwise AND operation on the data in the registers, and returns the result of the operation;

[0062] Use the _mx512_srlib instruction to shift right by 3 bits to get index; expressed as:

[0063] v64u8 vindex = _mx512_srlib(vinputs, 3);

[0064] The srlib instruction takes a register and an immediate value. The register contains 64 8-bit numbers, each of which is shifted right by 3 bits.

[0065] In step S1, since the graph of the exp(x) function is exponential, when using linear interpolation, it is impossible to fit the graph with a linear function when x is too large. Therefore, in this method, the fixed interval is limited to x in the interval [-1, 0].

[0066] Step S2 further includes:

[0067] The index is reordered and used by gshufvb to look up dy,y0; it is represented as: vindex = _mx512_extubl(vindex);

[0068] The Extubl instruction: register2 = extubl(register1) is used to expand the lower 256 bits of data in register1 from 8 bits to 16 bits and put it into register2;

[0069] vindex=_mx512_ilveb(vindex,vindex);

[0070] The ilveb instruction: ilveb receives two registers. The data in each register is treated as an element. The even-numbered elements are taken from register 1 and register 2 respectively to form a new sequence; vindex = _mx512_sllih(vindex,1);

[0071] The data in register vindex is in 16-bit elements, with each element shifted left by 1 bit;

[0072] vindex=_mx512_addh(vindex,vparam256);

[0073] The elements in register index and register vparam256 are treated as one element per 16 bits, and the result of adding the corresponding positions is placed in register vindex.

[0074] vparam256: Each half word stores 0x100;

[0075] Given y0, it is represented as:

[0076] v32u16 vy0=_mx512_gshufvb(vy0,vindex,vr11);

[0077] The gshufv instruction, vrd = gshufvb(vrd,vrs,vrp), takes every 8-bit element from vrs, uses the lower 6 bits as an index, retrieves the element at that index position from vrp, and puts the retrieved element into vrd.

[0078] vr11 stores 32 uint16_t entries y0;

[0079] If we get dy, it means:

[0080] v32u16 vdy=_mx512_gshufvb(vdy,vindex,vr10);

[0081] vr10 stores 32 uint16_t entries dy.

[0082] Step S3 further includes:

[0083] Calculate coef*dy; expressed as:

[0084] vcoef0=_mx512_extubl(vcoef0);

[0085] The instruction `extubl(register1)` is used to expand the lower 256 bits of data in register1 (8 bits) into 16 bits and place them into register2.

[0086] v16u32 vcoef1=_mx512_extuhl(vcoef0);

[0087] The instruction `register2 = extuhl(register1)` expands the lower 256 bits of data in register1 (16 bits) into 32 bits and places it into register2.

[0088] v16u32 vcoef2=_mx512_extuhh(vcoef0);

[0089] The instruction `register2 = extuhh(register1)` expands the 16-bit data in the high 256 bits of register1 into 32-bit data and places it into register2.

[0090] v16u32 vtemp0=_mx512_wumulhl(vcoef1,vdy);

[0091] The elements of register vcoef1 and register vdy are multiplied one by one. However, each element of vcoef1 is 32 bits. The elements of vdy need to be expanded by taking the lower 16 bits of each 32 bits to form a 32-bit array. That is, the lower 16 bits of each 32-bit array are expanded back to 32 bits, and the higher 16 bits are discarded.

[0092] v16u32 vtemp1=_mx512_wumulhh(vcoef2,vdy);

[0093] The 16-bit element in the lower 256 bits of register vdy is expanded to 32 bits and then multiplied by the 32-bit element in vcoef2.

[0094] In step S4

[0095] y0<<3; means:

[0096] vy0 = _mx512_sllih(vy0,3);

[0097] In register vy0, each 16 bits is treated as an element, and each element is shifted left by 3 bits;

[0098] vtemp0=_mx512_wadduhl(vtemp0,vy0);

[0099] The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of low bits. That is, every 32 bits in vy0 is taken and extended back to 32 bits, while the high 16 bits are discarded.

[0100] vtemp1=_mx512_wadduhh(vtemp1,vy0);

[0101] The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of high bits. That is, every 32 bits, the high 16 bits are taken and then extended back to 32 bits, and the low 16 bits are discarded.

[0102] Step S4 further includes:

[0103] vtemp0=_mx512_srliw(vtemp0,3);

[0104] The 32-bit element in vtemp0 is shifted right by 3 bits;

[0105] vtemp1=_mx512_srliw(vtemp1,3);

[0106] Shift the 32-bit element in Vtemp1 right by 3 bits;

[0107] Once the calculation is complete, save the result to result;

[0108] _mx512_sao() is used to store data from a register into a specified array. Since a register has 512 bits, and sao stores 256 bits at a time, it needs to be stored twice.

[0109] _mx512_sao(vtemp0,0,result,0);

[0110] _mx512_sao(vtemp0,0,result,0) stores the lower 256 bits of register vtemp0 into the memory address pointed to by result or the array result.

[0111] _mx512_sao(vtemp0,1,result,1);

[0112] _mx512_sao(vtemp0,1,result,1) stores the high 256 bits of register vtemp0 into the memory address pointed to by result or the address of array result shifted 256 bits to the right, i.e., result+256.

[0113] _mx512_sao(vtemp1,0,result,2);

[0114] _mx512_sao(vtemp1,0,result,2) stores the lower 256 bits of register vtemp1 into a new address offset 512 bits from the memory address pointed to by result;

[0115] _mx512_sao(vtemp1,1,result,3);

[0116] _mx512_sao(vtemp1,1,result,3) stores the high 256 bits of register vtemp1 into a new address 768 bits forward from the memory address pointed to by result;

[0117] result += 32;

[0118] input += 32;

[0119] The loop ends.

[0120] Step S5 further includes:

[0121] exp_lut(z,len,result);

[0122] The function name exp_lut is defined in the exp function optimization method based on table lookup interpolation, i.e., this method. It calculates 32 elements in z each time and stores the result in result.

[0123] The method described is a lookup interpolation scheme based on the MXU3.1 instruction set.

[0124] The _mx512_lao instruction reads data from a specified memory address into a specified register. In the MXU3.1 instruction set, a register is 512 bits, and 0 represents 256 bits. One lao reads 256 bits from memory into the register, so two lao instructions are needed to fill a register. To prevent the register from being overwritten, the register offset specifies whether the data is placed in the first or second half of the register. Similarly, the memory offset specifies the address at which the actual reading of data from memory begins.

[0125] Therefore, the advantages of this application are: the optimized exp calculation is based on SIMD instructions, data reading and calculation are no longer carried out one by one from memory, but are completed in batches in registers, and the exp function value obtained by the lookup table interpolation method, through this method to improve the exp solution process, can greatly improve the calculation efficiency of the exp function. Attached Figure Description

[0126] The accompanying drawings, which are provided to further illustrate the invention and form part of this application, are not intended to limit the scope of the invention.

[0127] Figure 1 This is a flowchart illustrating the method described in this application.

[0128] Figure 2 This is a schematic diagram of the calculation process of the exp table lookup interpolation method in this application. Detailed Implementation

[0129] To better understand the technical content and advantages of the present invention, the present invention will now be described in further detail with reference to the accompanying drawings.

[0130] In existing target classification tasks, such as cat and dog recognition, it is necessary to predict the probabilities of cats and dogs respectively (the neural network will provide predictions for cats and dogs, but these need to be further converted into probability values). The method used is...

[0131] The probability of a cat = (e^predicted cat value) / (e^(predicted cat value) + e^(predicted dog value))

[0132] Probability of dog = (e ^ predicted value of dog) / (e ^ (predicted value of cat) + e ^ (predicted value of dog))

[0133] In which there is a calculation process, and the computer loads Z from the memory i For the process of elements, it takes too much time. The traditional calculation method can only load 1 floating-point number at a time. Through this method, 32 integers of UINT8 type can be loaded and calculated at a time, which can greatly reduce the time-consuming of loading data from the memory. In addition, even if this part of the loading time is ignored, the process of using Taylor expansion to solve exp in the traditional method is too cumbersome and time-consuming. Therefore, this method provides a look-up table interpolation scheme based on the MXU3.1 instruction set, which can quickly and batch solve the exp function. The specific scheme is as follows:

[0134] This method proposes a look-up table interpolation method for calculating the exp function value of integer data, which can batch evaluate the input vector Z i This method lies in:

[0135] 1. Based on the SIMD instructions of MXU3.1, parallel load data by using the SIMD read instruction.

[0136] 2. Simplify the exp solution process through the look-up table interpolation scheme and simplify the calculation steps.

[0137] As Figure 1 shown, the detailed calculation steps are as follows:

[0138] S1. Create table entries in advance. First, ensure that the input data is within a certain fixed interval, and divide the interval into 32 equal parts; since the graph of the exp(x) function is exponential, when using the linear interpolation method, it cannot be fitted by a linear function when x is too large. In this method, the effect is better when the certain fixed interval limits x to be in the [-1, 0] interval;

[0139] The 32 equal parts correspond to 32 exp values and 31 difference values (both not exceeding the representation range of UINT16). Then store the exp values and difference values into the arrays dy_t and y0_t respectively, where dy_t and y0_t are the numerical names.

[0140] The step S1 further includes:

[0141] Read 32 numbers each time from the input address input to calculate exp, and store the result in result; expressed as:

[0142] int i = 0; Determine whether i < size is satisfied; if satisfied, execute the loop body and perform i += 32; otherwise, jump out of the loop; expressed as: for(int i = 0; i < size; i += 32);

[0143] The loop body includes:

[0144] The _mx512_lao instruction is used to read 32 numbers from the input address at once; it is represented as: vinputs = _mx512_lao(vinputs, 0, input, 0);

[0145] The _mx512_lao reads data from a specified memory address into a specified register, and its usage is as follows:

[0146] Register1 = _mx512_lao(register1, register offset, memory address, memory offset). The register offset and memory offset can be either 0 or 1.

[0147] In the MXU 3.1 instruction set, a register is 512 bits, and 'o' represents 256 bits. One 'lao' instruction reads 256 bits from memory into the register; therefore, two 'lao' instructions are needed to fill a register. To prevent registers from being overwritten, the register offset specifies whether the data is placed in the first or second half of the register. Similarly, the memory offset specifies the address at which data actually begins to be read from memory; using the _mx512_andv instruction, this is represented as:

[0148] v64u8 vcoef0=_mx512_andv(vinputs,vparam1);

[0149] vparam1: Each Byte is pre-stored with 0x7;

[0150] The ANDV instruction receives two registers, performs a bitwise AND operation on the data in the registers, and returns the result of the operation.

[0151] Using the `_mx512_srlib` instruction, right-shift by 3 bits to obtain the index; represented as:

[0152] v64u8 vindex=_mx512_srlib(vinputs,3);

[0153] The srlib instruction takes a register and an immediate value. The register contains 64 8-bit numbers, each of which is shifted right by 3 bits.

[0154] S2. Read the input data, calculate the table entry index, use the index to find the corresponding value in the table, and then retrieve the most recent exp function value and difference value from dy_t and y0_t;

[0155] The formulas for calculating the index and the interpolation term coef are:

[0156] uint8_t index = input >> 3;

[0157] uint8_t coef = input & 0x7. Its corresponding SIMD code implementation is as follows:

[0158]

[0159] Step S2 further includes:

[0160] The index is reordered and used by gshufvb to look up dy,y0; it is represented as: vindex = _mx512_extubl(vindex);

[0161] The instruction `extubl(register1)` is used to expand the lower 256 bits of data in register1 (8 bits) into 16 bits and place them into register2.

[0162] vindex=_mx512_ilveb(vindex,vindex);

[0163] ilveb receives two registers, with each 8 bits of data in the registers treated as an element. It takes the even-numbered elements from register 1 and register 2 respectively to form a new sequence.

[0164] vindex=_mx512_sllih(vindex,1);

[0165] vindex=_mx512_addh(vindex,vparam256);

[0166] The 16-bit element of register index is shifted left by 1 bit and placed back into vindex. Then, it is added to the corresponding position of the 16-bit element in register vparam256, and the result is placed back into register vindex.

[0167] vparam256: Each half word stores 0x100;

[0168] Given y0, it is represented as:

[0169] v32u16 vy0=_mx512_gshufvb(vy0,vindex,vr11);

[0170] vrd = gshufvb(vrd,vrs,vrp): Take every 8-bit element in vrs, take the lower 6 bits as the index, take the element at that index position in vrp, and put the retrieved element into vrd.

[0171] vr11 stores 32 uint16_t entries y0;

[0172] If we get dy, it means:

[0173] v32u16 vdy=_mx512_gshufvb(vdy,vindex,vr10);

[0174] vr10 stores 32 uint16_t entries dy.

[0175] S3. Obtain the function value and difference value using a lookup table, then substitute them into the interpolation formula to perform interpolation. The interpolation formula is as follows:

[0176] inter = y0 + dy * coef

[0177] Where *inter* represents the interpolation result, *y0* represents the exp function value retrieved from *y0_t* when *index* is used as the index, and *dy* represents the difference value corresponding to *index* when *dy_t* is used as the index. Its SIMD code implementation is as follows:

[0178]

[0179] Step S3 further includes:

[0180] Calculate coef*dy; expressed as:

[0181] vcoef0=_mx512_extubl(vcoef0);

[0182] The instruction `extubl(register1)` is used to expand the lower 256 bits of data in register1 (8 bits) into 16 bits and place them into register2.

[0183] v16u32 vcoef1=_mx512_extuhl(vcoef0);

[0184] The instruction `register2 = extuhl(register1)` expands the lower 256 bits of data in register1 (16 bits) into 32 bits and places it into register2.

[0185] v16u32 vcoef2=_mx512_extuhh(vcoef0);

[0186] The instruction `register2 = extuhh(register1)` expands the 16-bit data in the high 256 bits of register1 into 32-bit data and places it into register2.

[0187] v16u32 vtemp0=_mx512_wumulhl(vcoef1,vdy);

[0188] The elements of register vcoef1 and register vdy are multiplied one by one. However, each element of vcoef1 is 32 bits. The elements of vdy need to be expanded to 32 bits by taking the lower 16 bits of each 32 bits (that is, take the lower 16 bits of each 32 bits and expand them back to 32 bits, while discarding the higher 16 bits).

[0189] v16u32 vtemp1=_mx512_wumulhh(vcoef2,vdy);

[0190] y0<<3; means:

[0191] vy0 = _mx512_sllih(vy0,3);

[0192] In register vy0, each 16 bits is treated as an element, and each element is shifted left by 3 bits;

[0193] vtemp0=_mx512_wadduhl(vtemp0,vy0);

[0194] The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of low bits (that is, every 32 bits in vy0 is taken as the low 16 bits and extended back to 32 bits, and the high 16 bits are discarded).

[0195] vtemp1=_mx512_wadduhh(vtemp1,vy0);

[0196] The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of high bits (that is, every 32 bits, the high 16 bits are taken and then extended back to 32 bits, and the low 16 bits are discarded).

[0197] S4. Calculate the exp result:

[0198] Step S4 further includes:

[0199] vtemp0=_mx512_srliw(vtemp0,3);

[0200] vtemp1=_mx512_srliw(vtemp1,3);

[0201] Once the calculation is complete, save the result to result;

[0202] _mx512_sao(vtemp0,0,result,0);

[0203] _mx512_sao(vtemp0,1,result,1);

[0204] _mx512_sao(vtemp1,0,result,2);

[0205] _mx512_sao(vtemp1,1,result,3);

[0206] result += 32;

[0207] input += 32;

[0208] The loop ends;

[0209] S5. Have all inputs been calculated? Yes: End; No: Return to S2 to continue calculation.

[0210] Step S5 further includes:

[0211] exp_lut(z,len,result); / / Calculates 32 elements of z each time and stores the result in result.

[0212] like Figure 2 The calculation process of exp using the table lookup interpolation method is shown below:

[0213] Start; Input vector Z; Set table entries; Calculate exp; Determine if calculation is complete, if yes, end; if no, return to exp calculation.

[0214] The specific code representation is shown in the table below:

[0215]

[0216]

[0217] In summary, this invention utilizes a lookup table interpolation method to solve for exp. By pre-setting table entries, the index is quickly calculated, the range of values ​​is found from the table entries, and then interpolation is performed. Compared with the original Taylor expansion method, the calculation steps of this new solution method are simpler and faster.

[0218] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations can be made to the embodiments of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for optimizing the exp function based on table lookup interpolation, characterized in that, The method includes the following steps: S1. Create table entries in advance. First, ensure that the input data is within a fixed range, and divide the range into 32 equal parts; the 32 equal parts correspond to 32 exp values and 31 difference values, all of which do not exceed the representation range of UINT16. Then store the exp values and difference values into arrays dy_t and y0_t respectively; S2. Read the input data, calculate the table entry index, and use the index to find the corresponding value in the table, and then take out the nearest exp function value and difference value from dy_t and y0_t; The calculation methods for the index index and the interpolation term coef are as follows: uint8_t index = input >> 3; uint8_t coef = input & 0x7; The corresponding SIMD implementation is: vindex = _mx512_srlib(vinputs, 3); vcoef0 = _mx512_andv(vinputs, vparam1); S3. Use the table lookup to obtain the function value and difference value, and perform interpolation after substituting them into the interpolation formula. The interpolation formula is as follows: inter = y0 + dy * coef, where inter represents the result of interpolation, y0 represents the exp function value retrieved from y0_t when index is used as the index; dy represents the difference value corresponding to when this index is used as the index of dy_t; S4. Calculate the exp result: result = (dy * coef + y0 << 3) >> 3; The corresponding SIMD implementation is: vcoef0 = _mx512_extubl(vcoef0); v16u32 vcoef1 = _mx512_extuhl(vcoef0); v16u32 vcoef2 = _mx512_extuhh(vcoef0); v16u32 vtemp0 = _mx512_wumulhl(vcoef1, vdy); v16u32 vtemp1 = _mx512_wumulhh(vcoef2, vdy); vy0 = _mx512_sllih(vy0, 3); vtemp0 = _mx512_wadduhl(vtemp0, vy0); vtemp1 = _mx512_wadduhh(vtemp1, vy0); vtemp0 = _mx512_srliw(vtemp0, 3); vtemp1 = _mx512_srliw(vtemp1, 3); S5. Have all the input calculations been completed? Yes: end; No: go back to S2 to continue the calculation.

2. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, Step S1 further includes: Read 32 numbers each time from the input address input to calculate exp, and store the results in result; expressed as: int i = 0; Determine whether i < size is satisfied; if satisfied, execute the loop body and perform i += 32; otherwise, jump out of the loop; expressed as: for(int i = 0; i < size; i += 32); The loop body includes: Using the `_mx512_lao` instruction, 32 numbers are read from the input address at once; this is represented as: vinputs=_mx512_lao(vinputs,0,input,0); The _mx512_lao instruction reads data from a specified memory address into a specified register. Its usage is as follows: Register1 = _mx512_lao(register1, register offset, memory address, memory offset); register offset and memory offset can be 0 or 1; Using the _mx512_andv directive, it can be represented as: v64u8 vcoef0=_mx512_andv(vinputs,vparam1); vparam1: Each Byte is pre-stored with 0x7; The ANDV instruction receives two registers, performs a bitwise AND operation on the data in the registers, and returns the result of the operation. Using the `_mx512_srlib` instruction, right-shift by 3 bits to obtain the index; represented as: v64u8 vindex=_mx512_srlib(vinputs,3); The srlib instruction takes a register and an immediate value. The register contains 64 8-bit numbers, each of which is shifted right by 3 bits.

3. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, In step S1, since the graph of the exp(x) function is exponential, when using linear interpolation, it is impossible to fit the graph with a linear function when x is too large. Therefore, in this method, the fixed interval is limited to x in the interval [-1, 0].

4. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, Step S2 further includes: The index is reordered for gshufvb to retrieve dy,y0 from the table; this is represented as: vindex=_mx512_extubl(vindex); The Extubl instruction: register2 = extubl(register1) is used to expand the lower 256 bits of data in register1 from 8 bits to 16 bits and put it into register2; vindex=_mx512_ilveb(vindex,vindex); The ilveb instruction: ilveb receives two registers. The data in each register is treated as an element. The even-numbered elements are taken from register 1 and register 2 respectively to form a new sequence; vindex = _mx512_sllih(vindex,1); The data in register vindex is in 16-bit elements, with each element shifted left by 1 bit; vindex=_mx512_addh(vindex,vparam256); The elements in register index and register vparam256 are treated as one element per 16 bits, and the result of adding the corresponding positions is placed in register vindex. vparam256: Each half word stores 0x100; Given y0, it is represented as: v32u16 vy0=_mx512_gshufvb(vy0,vindex,vr11); The gshufv instruction, vrd = gshufvb(vrd,vrs,vrp), takes every 8-bit element from vrs, uses the lower 6 bits as an index, retrieves the element at that index position from vrp, and puts the retrieved element into vrd. vr11 stores 32 uint16_t entries y0; If we get dy, it means: v32u16 vdy=_mx512_gshufvb(vdy,vindex,vr10); vr10 stores 32 uint16_t entries dy.

5. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, Step S3 further includes: Calculate coef*dy; expressed as: vcoef0=_mx512_extubl(vcoef0); The instruction `extubl(register1)` is used to expand the lower 256 bits of data in register1 (8 bits) into 16 bits and place them into register2. v16u32 vcoef1=_mx512_extuhl(vcoef0); The instruction `register2 = extuhl(register1)` expands the lower 256 bits of data in register1 (16 bits) into 32 bits and places it into register2. v16u32 vcoef2=_mx512_extuhh(vcoef0); The instruction `register2 = extuhh(register1)` expands the 16-bit data in the high 256 bits of register1 into 32-bit data and places it into register2. v16u32 vtemp0=_mx512_wumulhl(vcoef1,vdy); The elements of register vcoef1 and register vdy are multiplied one by one. However, each element of vcoef1 is 32 bits. The elements of vdy need to be expanded by taking the lower 16 bits of each 32 bits to form a 32-bit array. That is, the lower 16 bits of each 32-bit array are expanded back to 32 bits, and the higher 16 bits are discarded. v16u32 vtemp1=_mx512_wumulhh(vcoef2,vdy); The 16-bit element in the lower 256 bits of register vdy is expanded to 32 bits and then multiplied by the 32-bit element in vcoef2.

6. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, In step S4 y0<<3; means: vy0 = _mx512_sllih(vy0,3); In register vy0, each 16 bits is treated as an element, and each element is shifted left by 3 bits; vtemp0=_mx512_wadduhl(vtemp0,vy0); The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of low bits. That is, every 32 bits in vy0 is taken and extended back to 32 bits, while the high 16 bits are discarded. vtemp1=_mx512_wadduhh(vtemp1,vy0); The elements of vtemp0 and vy0 are added one by one, but the elements of vtemp0 are 32 bits, while the elements of vy0 are 16 bits of high bits. That is, every 32 bits, the high 16 bits are taken and then extended back to 32 bits, and the low 16 bits are discarded.

7. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, Step S4 further includes: vtemp0=_mx512_srliw(vtemp0,3); The 32-bit element in vtemp0 is shifted right by 3 bits; vtemp1=_mx512_srliw(vtemp1,3); Shift the 32-bit element in Vtemp1 right by 3 bits; Once the calculation is complete, save the result to result; _mx512_sao() is used to store data from a register into a specified array. Since a register has 512 bits, and sao stores 256 bits at a time, it needs to be stored twice. _mx512_sao(vtemp0,0,result,0); _mx512_sao(vtemp0,0,result,0) stores the lower 256 bits of register vtemp0 into the memory address pointed to by result; _mx512_sao(vtemp0,1,result,1); _mx512_sao(vtemp0,1,result,1) stores the high 256 bits of register vtemp0 into a new address offset 256 bits from the memory address pointed to by result; _mx512_sao(vtemp1,0,result,2); _mx512_sao(vtemp1,0,result,2) stores the lower 256 bits of register vtemp1 into a new address offset 512 bits from the memory address pointed to by result; _mx512_sao(vtemp1,1,result,3); _mx512_sao(vtemp1,1,result,3) stores the high 256 bits of register vtemp1 into a new address offset 768 bits from the memory address pointed to by result; result += 32; input += 32; The loop ends.

8. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, Step S5 further includes: exp_lut(z,len,result); The function name exp_lut is defined in the exp function optimization method based on table lookup interpolation, i.e., this method. It calculates 32 elements in z each time and stores the result in result.

9. The exp function optimization method based on table lookup interpolation according to claim 1, characterized in that, The method described is a lookup interpolation scheme based on the MXU3.1 instruction set.

10. The exp function optimization method based on table lookup interpolation according to claim 7, characterized in that, The _mx512_lao instruction reads data from a specified memory address into a specified register. In the MXU3.1 instruction set, a register is 512 bits, and 0 represents 256 bits. One lao instruction reads 256 bits from memory into the register, so two lao instructions are needed to fill a register. To prevent the register from being overwritten, the register offset specifies whether the data is placed in the first or second half of the register. Similarly, the memory offset specifies the address at which the actual reading of data from memory begins.