Hardware acceleration method and device of ViT model, equipment and storage medium

By adjusting and accelerating the circuit design of the linear and nonlinear layers of the ViT model, the problem of difficulty in hardware acceleration of the ViT model on the edge hardware platform is solved, and the model size is reduced and resource utilization efficiency is improved.

CN118862986APending Publication Date: 2024-10-29SHANDONG YUNHAI GUOCHUANG CLOUD COMPUTING EQUIP IND INNOVATION CENT CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410977182.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-10-29

AI Technical Summary

Technical Problem

The existing technology is difficult to reduce the model size of the ViT model and achieve hardware acceleration while taking into account changes in the linear layer and nonlinear layer. It is especially difficult to meet real-time requirements on edge hardware platforms.

Method used

By using the batch normalization layer and the preset objective function to adjust the linear layer and nonlinear layer in the ViT model, design the acceleration circuit, and build a full pipeline architecture hardware accelerator, including replacement layer normalization, activation function and normalization The exponential function reduces the difficulty of hardware implementation and reduces resource consumption.

Benefits of technology

It is possible to reduce the size of the ViT model, reduce the consumption of hardware resources, improve the computing speed and energy efficiency ratio, and make full use of computing resources while taking into account the changes in the linear layer and the nonlinear layer.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118862986A_ABST
    Figure CN118862986A_ABST
Patent Text Reader

Abstract

The invention discloses a hardware acceleration method and device of a ViT model, equipment and a storage medium, and relates to the technical field of computer vision, and the method comprises the steps: employing a batch normalization layer and a preset objective function to adjust a linear layer and a nonlinear layer in an initial ViT model, and obtaining an adjusted ViT model; the preset target function comprises a linear rectification function and a target normalization function taking 2 as the bottom; performing model training on the adjusted ViT model based on a preset data set and a preset model training rule to obtain a target ViT model; respectively designing an acceleration circuit for the linear layer and the nonlinear layer of the target ViT model to obtain an acceleration circuit design result; and constructing a full-pipeline architecture hardware accelerator based on an acceleration circuit design result, and accelerating the target ViT model by using the obtained target full-pipeline hardware accelerator. Under the condition that changes of a linear layer and a nonlinear layer are considered, the scale of the ViT model is reduced, and hardware acceleration is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer vision technology, and in particular to a hardware acceleration method, device, equipment and storage medium for a ViT model. Background Art

[0002] Transformer, as a deep neural network architecture based on the self-attention mechanism, was introduced into the field of computer vision. Unlike Convolutional Neural Networks (CNN) that capture local information through convolution, the Visual Transformer model (also known as the ViT model) divides the input image data into several non-overlapping sub-blocks as tokens, and uses the Multi-Head Self-Attention (MHSA) mechanism to obtain global information. As a result, the model scale of the Visual Transformer is much larger than that of CNN, which leads to an explosive increase in the number of model parameters and computational complexity, making it difficult to deploy the Transformer on edge hardware platforms with limited hardware resources and power consumption, and making it difficult to meet real-time requirements.

[0003] At present, in order to reduce the model size of the visual Transformer, researchers have proposed some lightweight Transformer structures, such as the CNN-Transformer hybrid structure, using pruning methods to cut off unimportant tokens or layers in the Transformer, and using quantization technology to reduce the storage space required for storing parameters and reduce computational complexity. In other words, most of the existing work focuses on the compression and acceleration of the linear layer in the Transformer, while ignoring the nonlinear layer, such as the Softmax function (normalized exponential function), GELU activation function (Gaussian Error Linear Unit), and layer normalization layer (normalized network layer) that exist in large quantities in the Transformer. In addition, the nonlinear layer is complex to operate, has poor hardware friendliness, and is difficult to accelerate with dedicated hardware.

[0004] It can be seen that how to reduce the model scale of the ViT model and achieve hardware acceleration while taking into account the changes in the linear layer and the nonlinear layer is a problem that those skilled in the art need to solve. Summary of the invention

[0005] The purpose of the embodiments of the present invention is to provide a hardware acceleration method, device, equipment and storage medium of the ViT model, which can reduce the model scale of the ViT model and achieve hardware acceleration while taking into account the changes of the linear layer and the nonlinear layer, thereby reducing the difficulty of hardware implementation of the nonlinear layer, reducing hardware resource consumption, making full use of computing resources, and improving energy efficiency. The specific scheme is as follows:

[0006] In a first aspect, the present application discloses a hardware acceleration method of a ViT model, comprising:

[0007] Using a batch normalization layer and a preset objective function, respectively adjusting the linear layer and the nonlinear layer in the initial ViT model to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2;

[0008] Acquire a preset data set, and perform corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model;

[0009] Designing corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively to obtain corresponding acceleration circuit design results;

[0010] A full-pipeline architecture hardware accelerator is constructed based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and the target full-pipeline hardware accelerator is used to accelerate the target ViT model.

[0011] Optionally, the use of a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively to obtain an adjusted ViT model includes:

[0012] Replacing the layer normalization in the linear layer in the initial ViT model with a batch normalization layer to complete the corresponding first model adjustment operation and obtain the corresponding first ViT model;

[0013] Replacing the Gaussian error linear unit activation function in the nonlinear layer in the first ViT model with a linear rectification function to complete the corresponding second model adjustment operation and obtain the corresponding second ViT model;

[0014] The normalized exponential function with the natural exponent e as the base in the nonlinear layer in the second ViT model is replaced by the target normalization function with the base 2 to obtain the corresponding adjusted ViT model.

[0015] Optionally, replacing the normalized exponential function with a natural exponent e as the base in the nonlinear layer in the second ViT model with a target normalization function with a base of 2 to obtain a corresponding adjusted ViT model includes:

[0016] The corresponding initial expression is obtained by replacing the base of the exponential function in the normalized exponential function with the natural exponent e as the base with 2, and the numerator and the denominator in the initial expression are simultaneously divided by the same exponential term to obtain the corresponding first expression;

[0017] Transforming the first expression using a logarithmic identity to obtain a corresponding second expression; the second expression includes a corresponding exponential function with base 2 and a logarithmic function with base 2;

[0018] Performing corresponding function simplification on the exponential function using binary and shift operation properties to obtain a corresponding first exponential function, and completing an approximate fitting operation corresponding to the first exponential function based on a vertex formula of a quadratic function to obtain a corresponding target exponential function;

[0019] Performing a corresponding approximate fitting on the logarithmic function based on the vertex formula of the quadratic function to obtain a corresponding target logarithmic function;

[0020] The normalized exponential function with the natural exponent e as the base in the second ViT model is replaced by a target normalization function including the target exponential function and the target logarithmic function to obtain a corresponding adjusted ViT model.

[0021] Optionally, the step of respectively designing corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model includes:

[0022] For the target normalization function in the nonlinear layer in the target ViT model, the calculation logic of the target normalization function is analyzed, and a design is performed based on the obtained logic analysis result to obtain a target acceleration circuit design result corresponding to the target normalization function.

[0023] Optionally, performing corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model includes:

[0024] Dividing the preset data set to obtain corresponding training sets and validation sets;

[0025] By inputting the image data in the training set into the adjusted ViT model, an image classification result output by the adjusted ViT model is obtained;

[0026] Determine a corresponding loss value based on the image classification result and a preset cross entropy loss function;

[0027] The adjusted ViT model is iteratively trained based on the loss value and using a back propagation algorithm until a training termination condition is met, thereby obtaining a target ViT model.

[0028] Optionally, obtaining the target ViT model includes:

[0029] When the training termination condition is met, obtaining a verification result obtained by verifying the classification accuracy of the model in the iterative training process using the verification set;

[0030] Based on the verification results, the model with the highest classification accuracy is determined as the target ViT model.

[0031] Optionally, the construction of a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator includes:

[0032] In the process of developing a full-pipeline architecture hardware accelerator based on the acceleration circuit design results, a queue for input data caching is added before the computing units corresponding to each layer of the target ViT model, and the parallelism is set according to the computing amount of each computing unit to obtain the corresponding target full-pipeline hardware accelerator.

[0033] In a second aspect, the present application discloses a hardware acceleration device for a ViT model, comprising:

[0034] A model adjustment module, used to adjust the linear layer and the nonlinear layer in the initial ViT model respectively by using a batch normalization layer and a preset objective function to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2;

[0035] A model training module, used to obtain a preset data set, and perform corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model;

[0036] A circuit design module, used to design corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively, so as to obtain corresponding acceleration circuit design results;

[0037] The hardware acceleration module is used to construct a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and use the target full-pipeline hardware accelerator to accelerate the target ViT model.

[0038] In a third aspect, the present application discloses an electronic device, comprising:

[0039] Memory, used to store computer programs;

[0040] The processor is used to execute the computer program to implement the steps of the hardware acceleration method of the ViT model disclosed above.

[0041] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the steps of the hardware acceleration method of the ViT model disclosed above are implemented.

[0042] It can be seen that the present application uses a batch normalization layer and a preset objective function to make corresponding adjustments to the linear layer and the nonlinear layer in the initial ViT model respectively to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with base 2; obtains a preset data set, and performs corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rules to obtain a trained target ViT model; designs corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively to obtain corresponding acceleration circuit design results; constructs a full-pipeline architecture hardware accelerator based on the acceleration circuit design results to obtain a corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator to accelerate the target ViT model.

[0043] Beneficial effects: The present application firstly uses a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2; obtains a preset data set, and performs corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rules to obtain a trained target ViT model; respectively designs corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model to obtain corresponding acceleration circuit design results; constructs a full-pipeline architecture hardware accelerator based on the acceleration circuit design results to obtain a corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator to accelerate the target ViT model. That is, the present application provides a hardware acceleration method for the ViT model, and the present application can use a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively, and adjust the nonlinear layer while adjusting the linear layer, so as to reduce the difficulty of hardware implementation of the nonlinear layer, reduce hardware resource consumption, and improve the calculation speed. Furthermore, the present application performs model training and full-pipeline circuit design based on the obtained adjusted ViT model, and finally obtains the corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator for hardware acceleration. In this way, computing resources can be fully utilized, power consumption can be reduced, and energy efficiency can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0045] Figure 1 A flowchart of a hardware acceleration method of a ViT model provided by an embodiment of the present invention;

[0046] Figure 2 A schematic diagram for comparing layer normalization and batch normalization provided in an embodiment of the present invention;

[0047] Figure 3 A schematic diagram of a function image provided by an embodiment of the present invention;

[0048] Figure 4 A schematic diagram of an exponential function and logarithmic function processing module provided by an embodiment of the present invention;

[0049] Figure 5 A schematic diagram of a module of a target normalization function provided by an embodiment of the present invention;

[0050] Figure 6 A schematic diagram of the architecture of a target full-pipeline hardware accelerator and each layer of computing units provided by an embodiment of the present invention;

[0051] Figure 7 A flowchart of a specific hardware acceleration method of a ViT model provided by an embodiment of the present invention;

[0052] Figure 8 A schematic diagram of the structure of a hardware acceleration device of a ViT model disclosed in this application;

[0053] Fig. 9 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION

[0054] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0055] The terms "including" and "having" in the specification of the present invention and the above-mentioned drawings, as well as any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device including a series of steps or units is not limited to the listed steps or units, but may include steps or units that are not listed.

[0056] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0057] Next, a hardware acceleration method of a ViT model provided by an embodiment of the present invention is described in detail. Figure 1 A flowchart of a hardware acceleration method of a ViT model provided by an embodiment of the present invention includes:

[0058] Step S11: using a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively, so as to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with base 2.

[0059] Specifically, in this embodiment, the ViT model is first adjusted, that is, the linear layer and the nonlinear layer in the initial ViT model are adjusted accordingly using the batch normalization layer and the preset objective function to obtain the adjusted ViT model, including: replacing the layer normalization in the linear layer in the initial ViT model with the batch normalization (Batch normalization, BN) layer to complete the corresponding first model adjustment operation and obtain the corresponding first ViT model; replacing the Gaussian error linear unit activation function in the nonlinear layer in the first ViT model with the linear rectification function to complete the corresponding second model adjustment operation and obtain the corresponding second ViT model; replacing the normalized exponential function with the natural exponent e as the base in the nonlinear layer in the second ViT model with the target normalization function with the base 2 to obtain the corresponding adjusted ViT model. In this way, the linear layer and nonlinear layer of the model are adjusted based on the above operations. By replacing them with BN layers, the problem that the LN (Layer Normalization) and linear layers in the Transformer model cannot be combined for calculation is solved, and the problem that the mean and variance must be calculated for each inference is also solved; by replacing the target normalization function with base 2, the hardware implementation difficulty of the softmax function can be significantly reduced; by replacing it with a linear rectification function, the calculation speed of the hardware implementation is improved.

[0060] Regarding the batch normalization layer, by replacing the layer normalization in the ViT model with the batch normalization layer, it is convenient to merge the linear layer and the BN layer during inference to simplify the calculation. Figure 2 As shown in the figure, for images, layer normalization (1×C×HW) is to calculate the mean and standard deviation of all pixels within each sample, that is, the input is a feature map of C (number of channels of the feature map)×H (height)×W (width), N represents the batch, and the mean and standard deviation need to be recalculated for each sample at each inference, and then the mean, standard deviation, and learnable parameters γ and β are used for scaling and translation. Batch normalization (N×1×HW) normalizes each channel of each batch of data. During inference, the mean and variance of the validation sample are not used, but the sliding average result saved during training is used to calculate the corresponding learnable parameters γ and β obtained during training. Even for a single sample, it can support prediction, and during inference, the mean, standard deviation, and learnable parameters γ and β of BN are fixed values, which can be fused with the weight parameters of the linear layer to reduce the amount of calculation. The calculation formula of the normalization layer is shown below.

[0061]

[0062] Among them, μ and σ 2They represent the mean and variance of the data respectively, ε is a stable term to avoid the variance being zero, and γ and β are trainable parameters.

[0063] It is important to understand that both batch normalization and layer normalization are calculated using the above formula. The difference is that they calculate μ and σ 2 Batch normalization only updates μ and σ during the training phase. 2 , in the inference phase, μ and σ 2 are fixed parameters, while layer normalization requires dynamic calculation of μ and σ based on the eigenvalues ​​during both the training and inference phases. 2 , so only the former can perform the linear layer and batch normalization layer fusion operation described below.

[0064] Taking the fully connected layer as an example to introduce the specific fusion operation algorithm, the calculation formula for the fully connected layer is:

[0065] y (k) =∑ j w (j,k) ·x (j) +b (k) (2);

[0066] Among them, j and k are used to represent the index of the input dimension and the output dimension respectively, and the calculation results will be used as the input of the batch normalization layer. Substituting formula (2) into formula (1) and sorting it out, we can get formula (3), as shown below.

[0067]

[0068] In the formula, the second line is the new linear layer obtained by fusing the linear layer with the batch normalization layer, where and are the weight and bias of the new linear layer, respectively. Their calculation expressions are shown in equation (4) and equation (5).

[0069]

[0070] When the network model is finally deployed on hardware, only the weights and biases need to be deployed. and Since the final deployed weights and biases have changed, the object of the quantization operation has also become the fused network weights.

[0071] Regarding the linear rectification function, by replacing the Gaussian Error Linear Unit (GELU) activation function in the nonlinear layer with the Rectified Linear Unit (ReLU) function, the corresponding function image is as follows: Figure 3There are complex nonlinear operations in the GELU function image, and Sigmoid function or tanh function is usually used for approximate calculation. The function expression is shown below.

[0072]

[0073] There is no obvious difference between the ReLU function image and the GELU function image, and their function expressions are as follows.

[0074] ReLU(x)=max(0,x) (7);

[0075] In comparison, the ReLU function is friendlier than the GELU function in both fixed-point and hardware implementation.

[0076] In addition, regarding the target normalization function with base 2, by replacing the normalized exponential function (Softmax function) with natural exponent e as base in the nonlinear layer with the target normalization function, the hardware implementation difficulty of the softmax function is significantly reduced, the hardware resource consumption is reduced, and the calculation speed is accelerated.

[0077] The function of Softmax is to convert any vector Normalized to vector y∈(0,1) n And meet At the same time, the largest value in vector x is highlighted and other smaller values ​​are suppressed. Formula (8) is the specific expression of the Softmax function.

[0078]

[0079] In the Transformer model, the attention weight is generated mainly by using the Softmax function to highlight the maximum value and suppress the non-maximum value, that is, weighted operations are performed on different tokens so that the model can learn more information that needs attention. The main reason for using a natural constant as the base in the Softmax function is to utilize the property that its derivative is equal to itself, which is convenient for directly calculating the gradient value during the back propagation process of network model training. Compared with the training process, the design goal of the present invention pays more attention to the reasoning of the network model in the hardware part. Therefore, the Softmax function is replaced with a form that is easier to implement in hardware, and the base of the exponential function in the Softmax function is replaced by 2 from the natural constant to obtain formula (9).

[0080]

[0081] The function in formula (9) is named Softmax2. It is not difficult to find that Softmax2 also satisfies the properties that Softmax outputs a probability distribution vector and highlights the maximum value and suppresses the non-maximum value. However, in hardware implementation, the calculation of the exponential function of 2 in Softmax2 is simpler than the calculation of the exponential function of the natural constant in Softmax. Afterwards, Softmax2 can be transformed using logarithmic identities, binary and shift operation properties, and quadratic functions to obtain the final target normalization function.

[0082] Step S12: obtaining a preset data set, and performing corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model.

[0083] It should be understood that after the corresponding lightweight visual Transformer model, that is, the adjusted model, is obtained through step S11, the adjusted model needs to be trained. Obtain a pre-configured data set, that is, a preset data set, which includes a number of pictures, depending on the specific model application requirements. For example, for the application scenario of face recognition, the pictures in the data set can be several images containing faces, which are input into the adjusted model for classification and recognition to train its ability to recognize faces; for the application scenario of license plate recognition, the pictures in the data set can be several car images containing car license plates, which are input into the adjusted model for classification and recognition to train its ability to recognize license plates.

[0084] Among them, it needs to be further understood that an image data input interface can be provided to receive the image data input by the user, so as to configure the preset data set according to the user's needs, and then train a model that better meets the user's needs. In addition, a database can also be pre-built, which contains image templates corresponding to each typical application scenario. When the user does not provide image data (for example, not provided within the specified period), the developer defaults to extracting the corresponding image template from the database to construct the preset data set. It can be understood that the image data input by the user through the image data input interface has a higher training priority than the image template in the database. Among them, the image data input interface can be in the form of a selection button in the front-end interface so that the user can choose whether to input.

[0085] Specifically, regarding the training process, that is, the corresponding model training is performed on the adjusted ViT model based on the preset data set and the preset model training rules to obtain the trained target ViT model, including: dividing the preset data set to obtain a corresponding training set and a validation set; obtaining the image classification result output by the adjusted ViT model by inputting the image data in the training set into the adjusted ViT model; determining the corresponding loss value based on the image classification result and the preset cross entropy loss function; iteratively training the adjusted ViT model based on the loss value and using the back propagation algorithm until the training termination condition is met to obtain the target ViT model.

[0086] Furthermore, in this embodiment, obtaining the target ViT model includes: when the training termination condition is met, obtaining a verification result obtained by verifying the classification accuracy of the model in the iterative training process using the verification set; and determining the model with the highest classification accuracy as the target ViT model based on the verification result.

[0087] Step S13: respectively designing corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model to obtain corresponding acceleration circuit design results.

[0088] In this embodiment, after the trained target ViT model is obtained, the acceleration circuit design is started, specifically, the corresponding acceleration circuit is designed for the target normalization function, the batch normalization layer, the linear rectification function and other operation logics in the target ViT model.

[0089] Since Transformer is mainly composed of multi-head self-attention, the present invention has replaced the softmax function with the target normalization function, and the operation process of multi-head self-attention is shown in the following formula.

[0090]

[0091] Among them, it mainly involves matrix multiplication operations and sofmax2 function calculations. When calculating matrix multiplication, first use the method in step S11 to merge matrix multiplication with the BN layer to simplify the calculation. Since the three matrices Q (Query), K (Key), and V (Value) in Transformer are relatively large, they are limited by the storage space size and computing units of the hardware. It is impossible to complete the entire matrix multiplication directly at one time. It is necessary to divide the data into blocks, and then use a special block matrix multiplication to add the results of multiple blocks to obtain the final output result.

[0092] Since the Softmax2 function will be decomposed into an exponential function and a logarithmic function with base 2 in the process of being converted into the target normalization function through a series of transformations, the calculation process of the exponential function and the logarithmic function is similar. Both can be roughly divided into three parts, namely input data processing, fitting function and shift operation. The hardware design of the exponential function module and the logarithmic function module is as follows: Figure 4 As shown in the figure, both of them include fitting function, shift and addition modules. The only difference is the design of input data processing. In the exponential function module, the input x′ is directly split into i (integer part) and d′ (decimal part) according to the fixed-point decimal width, and the leading zero number calculation in the logarithmic function module is performed bit by bit through the combinational logic circuit.

[0093] Based on the above exponential function module and logarithmic function module, the calculation of Softmax2 can be divided into three steps, and the expression after the division is shown in formula (11).

[0094]

[0095] Among them, exp2 and log2 are exponential operations and logarithmic operations with base 2 respectively, and P represents the parallelism of the input data. The reused data includes x′ max and log2(s'), where x' max For all The maximum value of , repeated operations include subtraction and exp2 calculation. Based on the above characteristics, the calculation order and module framework of the Softmax2 function module can be planned. Figure 5 As shown in the figure, the solid arrows represent the first two steps of calculation, and the dotted arrows represent the third step of calculation. The input data first passes through the Findmax (maximum value search function) module to find the maximum value in the input. Then, the input minus the maximum value is written into the row buffer for temporary storage for subsequent reuse. Then, the exp2 of each item is calculated and summed using the addition tree structure. After the first two steps are completed, the third step completes the calculation of the output data by reusing the subtractor and exp2 modules in the first two steps, minimizing the computing resources required for this module.

[0096] That is to say, for the target normalization function in the nonlinear layer in the target ViT model, in this embodiment, the calculation logic of the target normalization function is analyzed, and the design is performed based on the obtained logic analysis result to obtain the target acceleration circuit design result corresponding to the target normalization function. By analogy, the target acceleration circuit design results of other operation logics such as the batch normalization layer and the linear rectification function in the target ViT model can be obtained, so as to finally obtain the acceleration circuit design result corresponding to the entire target ViT model.

[0097] Step S14: constructing a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and using the target full-pipeline hardware accelerator to accelerate the target ViT model.

[0098] In this embodiment, after obtaining the acceleration circuit design result, a full-pipeline architecture is designed using the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, such as Figure 6 As shown. In the target full-pipeline hardware accelerator, each layer has a dedicated computing unit. For example, the first layer computing unit includes corresponding matrix multiplication units, activation units, data rearrangement units, Softmax units, Add, FFN (Feedforward Neural Network), etc. Since it is difficult to achieve complete consistency in delay between computing layer modules, and computing layer modules usually have a certain startup cycle, it is necessary to add an additional queue (First In First Out, FIFO) before the computing layer for temporary storage of input data, thereby alleviating the problem of untimely processing of downstream modules and output blockage of upstream modules due to back pressure. In addition, since the output parallelism of the upper computing layer is not necessarily equal to the input parallelism of the lower computing layer, when the parallelism is inconsistent, it is necessary to add an additional data module to repackage the data stream and adjust the parallelism of the data stream.

[0099] It is further understood that, in this embodiment, for the connection of different branches in the residual connection structure, since the delay of the jump connection branch is lower than the delay of the main branch, it is necessary to add a queue with a larger depth in the jump connection branch for temporary storage of data, otherwise data loss will occur in the jump connection branch. Similarly, when the parallelism of the two branches is inconsistent, it is also necessary to add an additional repackaging module for processing.

[0100] In addition, regarding the delay balance between modules in the target full-pipeline hardware accelerator, since there is configurable parallelism in all modules, the parallelism can be set according to the computing amount of different modules, and the delay between modules can be balanced as much as possible. The computing delay of most computing layers is controlled within an approximate range, and the resource amount of the entire accelerator IP (Internet Protocol) is also controlled within a deployable range.

[0101] In summary, the present invention proposes a hardware acceleration method for a visual Transformer. First, the layer normalization in the Transformer is replaced with a batch normalization layer to simplify the calculation by merging it with a linear layer; then, the Softmax function with a natural exponential e as the base in the Transformer is replaced with a target normalization function with a base of 2, and the function with a base of 2 can be simplified in calculation in hardware; the above model is trained using a data set to obtain a hardware-friendly Transformer model; acceleration circuits are designed for the linear layer and nonlinear layer of the Transformer respectively, and finally a full-pipeline architecture hardware accelerator is constructed to accelerate the calculation of the Transformer to complete the corresponding hardware acceleration operation.

[0102] Further, such as Figure 7 As shown, before replacing the normalized exponential function with the natural exponent e as the base in the nonlinear layer of the second ViT model with the target normalized function with the base 2, the target normalized function is obtained by performing a series of function transformations on the normalized exponential function with the natural exponent e as the base, and then performing function replacement, specifically including the following steps:

[0103] Step S21: By replacing the base of the exponential function in the normalized exponential function with the natural exponent e as the base with 2, a corresponding initial expression is obtained, and the numerator and denominator in the initial expression are simultaneously divided by the same exponential term to obtain a corresponding first expression.

[0104] Specifically, in this embodiment, by utilizing the properties of fractions and exponential functions, the numerator and denominator of equation (9), that is, the initial expression, can be divided by the same exponential term at the same time. Here, the exponential term divided by the maximum value in the input sequence is selected to obtain the first expression. Equation (12) shows this transformation process.

[0105]

[0106] in In formula (12), the input value range of the exponential function calculation is limited to the interval (-∞,0]), and accordingly, the output value range is also limited to the interval (0,1].

[0107] Step S22: transform the first expression using a logarithmic identity to obtain a corresponding second expression; the second expression includes a corresponding exponential function with base 2 and a logarithmic function with base 2.

[0108] In this embodiment, in order to solve the problem caused by the division operation, the logarithmic identity is used to perform a transformation to obtain the second expression shown below.

[0109]

[0110] The operations involved only include exponential operations, logarithmic operations, and summation operations with a base of 2. The summation operation is easy to implement, and only the fixed-point design of exponential operations and logarithmic operations needs to be considered. So far, the fixed-point design of the Softmax function has been successfully transformed into the fixed-point design of exponential operations and logarithmic operations with a base of 2.

[0111] Step S23: Use the binary and shift operation properties to perform corresponding function simplification on the exponential function to obtain the corresponding first exponential function, and complete the approximate fitting operation corresponding to the first exponential function based on the vertex formula of the quadratic function to obtain the corresponding target exponential function.

[0112] In this embodiment, for the exponential function, if the input of the exponential function with base 2 is Then x can be split into the form of x=i+d, where i∈Z, d∈[0,1), that is, i and d are the integer part and the decimal part of x respectively. Using the properties of binary and shift operations, the exponential function operation can be simplified according to formula (14) to obtain the first exponential function.

[0113]

[0114] Here, “<<” and “>>” represent arithmetic left shift and arithmetic right shift operations respectively. Now the problem is transformed into 2 d Since d∈[0,1), the input value range of the exponential function calculation is a finite area, which can be achieved by a finite size lookup table or a local area fitting approximation. d The present invention chooses to use a quadratic function to complete 2 d Approximate fitting of . Formula (15) is the fixed-point decimal calculation expression after fitting approximation.

[0115]

[0116] Among them, A, B, and C are fitting coefficients respectively. The vertex form of quadratic function is selected for calculation to minimize the number of multiplication operations. The remaining values ​​in this expression except i are fixed-point representation values, which can be further optimized to full integer expressions calculated using stored values. Fixed-point numbers with the same decimal width can be selected for the fitting coefficients and the input values ​​to simplify the calculation process. If the storage value corresponding to the input value x is x', its overall width is N and the decimal width is D. At this time, the relationship between the fixed-point representation values ​​and the stored values ​​of each variable in formula (15) is formula (16).

[0117]

[0118] The symbols with superscripts represent the stored values. Since i and d are the integer part and decimal part of x respectively, i and d' are equal to the high ND digits and low D digits of x'. Keeping the decimal width of the output value unchanged, y = y' × 2 -D Under the condition of , substituting formula (16) into formula (15) and transforming it, we can get the full integer expression calculated using stored numerical values ​​as formula (17), which is the target exponential function.

[0119]

[0120] Step S24: performing a corresponding approximate fitting on the logarithmic function based on the vertex formula of the quadratic function to obtain a corresponding target logarithmic function.

[0121] In this embodiment, for the logarithmic function, if the input of the logarithmic function with base 2 is Then x can be split into x=a×2 l In the form of , where l∈Z, a∈[0.5,1). Using the properties of the logarithmic function, the logarithmic function operation of 2 can be simplified according to formula (18).

[0122] y=log2x=log2(a×2 l )=log2(a)+log2(2 l )=log2(a)+1 (18);

[0123] At this time, the problem is transformed into the calculation of log2(a), since a∈[0.5,1). Similarly, the quadratic function is used to complete the fitting approximation of log2(a). Formula (19) is the fixed-point decimal calculation expression after the fitting approximation:

[0124] y=A(a+B) 2 +C+l (19);

[0125] Among them, A, B, and C are fitting coefficients respectively. The vertex form of quadratic function is selected for calculation to minimize the number of multiplication operations. All values ​​in this expression are fixed-point representation values, which can also be further optimized to full integer expressions calculated using stored values. Fixed-point numbers with the same decimal width are selected for fitting coefficients and input values ​​to simplify the calculation process. If the storage value corresponding to the input value x is x', its overall bit width is N and the decimal bit width is D. At this time, the relationship between the fixed-point representation values ​​and the stored values ​​of each variable in formula (19) is formula (20).

[0126]

[0127] Unlike the exponential function, which directly splits the input value x' into i and d', a' and l' need to be calculated based on the number of leading zeros in the input value x', that is, the number of "0"s before the first "1" in the data.

[0128] If the number of leading zeros of x' is p, then the expressions of a' and l' are as shown in formula (21).

[0129]

[0130] Keep the output value decimal width unchanged, that is, u = y' × 2 -D Under the condition of , substitute equation (20) and equation (21) into equation (19) and simplify to obtain the full integer expression calculated using stored numerical values ​​as shown in equation (22).

[0131] y′=[A′(x′< <p+B″) 2 >>(2×N)]+C′+[(NDp)< <D (22);

[0132] In order to avoid additional shift calculation of the fitting coefficients, B" is further used in formula (22) to represent B' so as to obtain the final target logarithmic function. The expression of B" is shown in formula (23).

[0133] B″=B′×2 N-D =B×2 N (twenty three).

[0134] Step S25: using a target normalization function including the target exponential function and the target logarithmic function, to replace the normalized exponential function based on the natural exponent e in the second ViT model, so as to obtain a corresponding adjusted ViT model.

[0135] It can be seen that the present application uses a batch normalization layer and a preset objective function to make corresponding adjustments to the linear layer and the nonlinear layer in the initial ViT model respectively to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with base 2; obtains a preset data set, and performs corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rules to obtain a trained target ViT model; designs corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively to obtain corresponding acceleration circuit design results; constructs a full-pipeline architecture hardware accelerator based on the acceleration circuit design results to obtain a corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator to accelerate the target ViT model.

[0136] Beneficial effects: The present application firstly uses a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2; obtains a preset data set, and performs corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rules to obtain a trained target ViT model; respectively designs corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model to obtain corresponding acceleration circuit design results; constructs a full-pipeline architecture hardware accelerator based on the acceleration circuit design results to obtain a corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator to accelerate the target ViT model. That is, the present application provides a hardware acceleration method for the ViT model, and the present application can use a batch normalization layer and a preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively, and adjust the nonlinear layer while adjusting the linear layer, so as to reduce the difficulty of hardware implementation of the nonlinear layer, reduce hardware resource consumption, and improve the calculation speed. Furthermore, the present application performs model training and full-pipeline circuit design based on the obtained adjusted ViT model, and finally obtains the corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator for hardware acceleration. In this way, computing resources can be fully utilized, power consumption can be reduced, and energy efficiency can be improved.

[0137] Figure 8 A schematic diagram of the structure of a hardware acceleration device of a ViT model provided by an embodiment of the present invention, the device comprises:

[0138] A model adjustment module 11 is used to adjust the linear layer and the nonlinear layer in the initial ViT model respectively by using a batch normalization layer and a preset objective function to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2;

[0139] A model training module 12 is used to obtain a preset data set, and perform corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model;

[0140] A circuit design module 13, used to design corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively, so as to obtain corresponding acceleration circuit design results;

[0141] The hardware acceleration module 14 is used to construct a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and use the target full-pipeline hardware accelerator to accelerate the target ViT model.

[0142] It can be seen that the present application firstly uses the batch normalization layer and the preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively to obtain the adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2; obtains a preset data set, and performs corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rules to obtain the trained target ViT model; respectively designs corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model to obtain the corresponding acceleration circuit design results; constructs a full-pipeline architecture hardware accelerator based on the acceleration circuit design results to obtain the corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator to accelerate the target ViT model. That is, the present application provides a hardware acceleration method for the ViT model, and the present application can use the batch normalization layer and the preset objective function to adjust the linear layer and the nonlinear layer in the initial ViT model respectively, and adjust the nonlinear layer while adjusting the linear layer, so as to reduce the difficulty of hardware implementation of the nonlinear layer, reduce hardware resource consumption, and improve the calculation speed. Furthermore, the present application performs model training and full-pipeline circuit design based on the obtained adjusted ViT model, and finally obtains the corresponding target full-pipeline hardware accelerator, and uses the target full-pipeline hardware accelerator for hardware acceleration. In this way, computing resources can be fully utilized, power consumption can be reduced, and energy efficiency can be improved.

[0143] In some specific embodiments, the model adjustment module 11 may specifically include:

[0144] A first model adjustment unit, configured to replace a layer normalization in a linear layer in an initial ViT model with a batch normalization layer, so as to complete a corresponding first model adjustment operation and obtain a corresponding first ViT model;

[0145] A second model adjustment unit, used to replace the Gaussian error linear unit activation function in the nonlinear layer in the first ViT model with a linear rectification function to complete the corresponding second model adjustment operation and obtain the corresponding second ViT model;

[0146] The adjusted ViT model acquisition unit is used to replace the normalized exponential function with the natural exponent e as the base in the nonlinear layer of the second ViT model with the target normalization function with the base 2 to obtain the corresponding adjusted ViT model.

[0147] In some specific embodiments, the adjusted ViT model acquisition unit may specifically include:

[0148] A first expression acquisition subunit is used to obtain a corresponding initial expression by replacing the base of the exponential function in the normalized exponential function with the natural exponent e as the base with 2, and divide the numerator and denominator in the initial expression by the same exponential term at the same time to obtain a corresponding first expression;

[0149] A second expression acquisition subunit, used to transform the first expression by using a logarithmic identity to obtain a corresponding second expression; the second expression includes a corresponding exponential function with base 2 and a logarithmic function with base 2;

[0150] a target exponential function acquisition subunit, configured to perform corresponding function simplification on the exponential function by using the binary and shift operation properties to obtain a corresponding first exponential function, and to complete an approximate fitting operation corresponding to the first exponential function based on a vertex formula of a quadratic function to obtain a corresponding target exponential function;

[0151] A target logarithmic function acquisition subunit is used to perform a corresponding approximate fitting on the logarithmic function based on the vertex formula of the quadratic function to obtain a corresponding target logarithmic function;

[0152] The adjusted model acquisition subunit is used to replace the normalized exponential function with the natural exponent e as the base in the second ViT model with a target normalization function including the target exponential function and the target logarithmic function to obtain the corresponding adjusted ViT model.

[0153] In some specific embodiments, the circuit design module 13 may specifically include:

[0154] The acceleration circuit design unit is used to analyze the calculation logic of the target normalization function located in the nonlinear layer of the target ViT model, and design based on the obtained logic analysis result to obtain a target acceleration circuit design result corresponding to the target normalization function.

[0155] In some specific embodiments, the model training module 12 may specifically include:

[0156] A data set division unit, used to divide the preset data set to obtain corresponding training sets and verification sets;

[0157] An image classification result acquisition unit, configured to obtain an image classification result output by the adjusted ViT model by inputting the image data in the training set into the adjusted ViT model;

[0158] A loss value determining unit, used to determine a corresponding loss value based on the image classification result and a preset cross entropy loss function;

[0159] The target ViT model acquisition unit is used to iteratively train the adjusted ViT model based on the loss value and using a back propagation algorithm until a training termination condition is met to obtain a target ViT model.

[0160] In some specific embodiments, the target ViT model acquisition unit may specifically include:

[0161] A verification result acquisition subunit is used to obtain a verification result obtained by verifying the classification accuracy of the model in the iterative training process using the verification set when the training termination condition is met;

[0162] The target ViT model acquisition subunit is used to determine the model with the highest classification accuracy as the target ViT model based on the verification result.

[0163] In some specific embodiments, the hardware acceleration module 14 is specifically used to: in the process of performing a full-pipeline architecture hardware accelerator based on the acceleration circuit design result, add a queue for input data caching before the computing units corresponding to each layer of the target ViT model, and set the parallelism according to the computing amount of each computing unit to obtain the corresponding target full-pipeline hardware accelerator.

[0164] Fig. 9 A structural diagram of an electronic device provided by an embodiment of the present invention, such as Fig. 9 As shown, the electronic device includes: a memory 20 for storing a computer program;

[0165] The processor 21 is used to implement the steps of the hardware acceleration method of the ViT model in the above embodiment when executing a computer program.

[0166] The electronic device provided in this embodiment may include but is not limited to a smart phone, a tablet computer, a laptop computer or a desktop computer.

[0167] Among them, the processor 21 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 21 can be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a graphics processing unit (GPU), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 21 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.

[0168] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 20 is at least used to store the following computer program 201, wherein, after the computer program is loaded and executed by the processor 21, it can implement the relevant steps of the hardware acceleration method of the ViT model disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 202 may include Windows, Unix, Linux, etc. Data 203 may include but is not limited to program code, etc.

[0169] In some embodiments, the electronic device may further include a display screen 22 , an input / output interface 23 , a communication interface 24 , a power source 25 , and a communication bus 26 .

[0170] Those skilled in the art will understand that Fig. 9 The structure shown in the figure does not constitute a limitation on the electronic device that implements the hardware acceleration of the ViT model, and may include more or fewer components than those shown in the figure.

[0171] It is understandable that if the hardware acceleration method of the ViT model in the above embodiment 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 storage medium. Based on this understanding, the technical solution of the present invention is essentially or part of the contribution to the current technology or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, magnetic disk or optical disk and other media that can store program code.

[0172] Based on this, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the hardware acceleration method of the ViT model as described above are implemented.

[0173] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0174] The above is a detailed introduction to the hardware acceleration method, device, equipment and storage medium of a ViT model provided by the present invention. This article uses specific examples to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.

Claims

1. A hardware acceleration method for a ViT model, characterized in that: include: Using a batch normalization layer and a preset objective function, respectively adjusting the linear layer and the nonlinear layer in the initial ViT model to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2; Acquire a preset data set, and perform corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model; Designing corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively to obtain corresponding acceleration circuit design results; A full-pipeline architecture hardware accelerator is constructed based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and the target full-pipeline hardware accelerator is used to accelerate the target ViT model.

2. The hardware acceleration method of the ViT model according to claim 1, characterized in that: The batch normalization layer and the preset objective function are used to adjust the linear layer and the nonlinear layer in the initial ViT model respectively to obtain the adjusted ViT model, including: Replacing the layer normalization in the linear layer in the initial ViT model with a batch normalization layer to complete the corresponding first model adjustment operation and obtain the corresponding first ViT model; Replacing the Gaussian error linear unit activation function in the nonlinear layer in the first ViT model with a linear rectification function to complete the corresponding second model adjustment operation and obtain the corresponding second ViT model; The normalized exponential function with the natural exponent e as the base in the nonlinear layer in the second ViT model is replaced by the target normalization function with the base 2 to obtain the corresponding adjusted ViT model.

3. The hardware acceleration method of the ViT model according to claim 2, characterized in that: The step of replacing the normalized exponential function with the natural exponent e as the base in the nonlinear layer of the second ViT model with the target normalized function with the base 2 to obtain the corresponding adjusted ViT model includes: The corresponding initial expression is obtained by replacing the base of the exponential function in the normalized exponential function with the natural exponent e as the base with 2, and the numerator and the denominator in the initial expression are simultaneously divided by the same exponential term to obtain the corresponding first expression; Transforming the first expression using a logarithmic identity to obtain a corresponding second expression; the second expression includes a corresponding exponential function with base 2 and a logarithmic function with base 2; Performing corresponding function simplification on the exponential function using binary and shift operation properties to obtain a corresponding first exponential function, and completing an approximate fitting operation corresponding to the first exponential function based on a vertex formula of a quadratic function to obtain a corresponding target exponential function; Performing a corresponding approximate fitting on the logarithmic function based on the vertex formula of the quadratic function to obtain a corresponding target logarithmic function; The normalized exponential function with the natural exponent e as the base in the second ViT model is replaced by a target normalization function including the target exponential function and the target logarithmic function to obtain a corresponding adjusted ViT model.

4. The hardware acceleration method of the ViT model according to claim 2, characterized in that: The designing of corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively includes: For the target normalization function in the nonlinear layer in the target ViT model, the calculation logic of the target normalization function is analyzed, and a design is performed based on the obtained logic analysis result to obtain a target acceleration circuit design result corresponding to the target normalization function.

5. The hardware acceleration method of the ViT model according to claim 1, characterized in that: The performing corresponding model training on the adjusted ViT model based on the preset data set and the preset model training rule to obtain a trained target ViT model includes: Dividing the preset data set to obtain corresponding training sets and validation sets; By inputting the image data in the training set into the adjusted ViT model, an image classification result output by the adjusted ViT model is obtained; Determine a corresponding loss value based on the image classification result and a preset cross entropy loss function; The adjusted ViT model is iteratively trained based on the loss value and using a back propagation algorithm until a training termination condition is met, thereby obtaining a target ViT model.

6. The hardware acceleration method of the ViT model according to claim 5, characterized in that: The obtaining of the target ViT model comprises: When the training termination condition is met, obtaining a verification result obtained by verifying the classification accuracy of the model in the iterative training process using the verification set; Based on the verification results, the model with the highest classification accuracy is determined as the target ViT model.

7. The hardware acceleration method of the ViT model according to claim 1, characterized in that: The construction of a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator includes: In the process of developing a full-pipeline architecture hardware accelerator based on the acceleration circuit design results, a queue for input data caching is added before the computing units corresponding to each layer of the target ViT model, and the parallelism is set according to the computing amount of each computing unit to obtain the corresponding target full-pipeline hardware accelerator.

8. A hardware acceleration device for a ViT model, characterized in that: include: A model adjustment module, used to adjust the linear layer and the nonlinear layer in the initial ViT model respectively by using a batch normalization layer and a preset objective function to obtain an adjusted ViT model; wherein the preset objective function includes a linear rectification function and a target normalization function with a base of 2; A model training module, used to obtain a preset data set, and perform corresponding model training on the adjusted ViT model based on the preset data set and preset model training rules to obtain a trained target ViT model; A circuit design module, used to design corresponding acceleration circuits for the linear layer and the nonlinear layer of the target ViT model respectively, so as to obtain corresponding acceleration circuit design results; The hardware acceleration module is used to construct a full-pipeline architecture hardware accelerator based on the acceleration circuit design result to obtain a corresponding target full-pipeline hardware accelerator, and use the target full-pipeline hardware accelerator to accelerate the target ViT model.

9. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to execute the computer program to implement the steps of the hardware acceleration method of the ViT model as claimed in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the hardware acceleration method of the ViT model as claimed in any one of claims 1 to 7 are implemented.

Citation Information

Cited By

  • Hardware system model generation method and device, equipment, medium and program product

    CN119150935A