Large-scale language model reasoning optimization method based on token fusion

By compressing large-scale language models based on token fusion methods, the challenges of LLM in inference delay and computing cost are solved, and efficient deployment without retraining and small precision loss are achieved.

CN118761468AActive Publication Date: 2024-10-11NANKAI UNIV

Patent Information

Application Number
CN202410750336.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-12
Publication Date
2024-10-11
Estimated Expiration
2044-06-12

AI Technical Summary

Technical Problem

Existing large-scale language models (LLMs) have challenges in inference latency and computational cost, especially when deployed on resource-constrained devices and in latency-sensitive applications. Existing pruning methods require retraining or fine-tuning to restore accuracy, and relies on specific hardware support.

Method used

A method based on token fusion is used to compress large-scale language models. By grouping and fusing similar tokens, the amount of calculation and delay are reduced, and one-shot pruning without retraining is achieved, which can be deployed using conventional hardware.

Benefits of technology

It reduces the inference delay of LLM while maintaining a small accuracy loss, achieving efficient deployment and civilian application of the model, and avoiding the need for additional hardware support.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118761468A_ABST
    Figure CN118761468A_ABST
Patent Text Reader

Abstract

The invention discloses a token fusion-based large-scale language model reasoning optimization method, which comprises the following steps of: enabling a text sequence preprocessed by a word segmentation device to enter an embedded layer for encoding, generating a word vector and a position vector, and adding the word vector and the position vector to obtain a hidden state as an input matrix of a Transform module; for a large-scale language model, performing calculation of an MHA module, token grouping, token fusion, calculation of an MLP module and token recovery operation layer by layer; repeating the steps until the last layer of the large-scale language model; and the hidden state generated in the last layer enters a tail-line linear layer for calculation, and the probability of each vocabulary in the vocabulary list is output. According to the method, the LLM is compressed by using a pruning method, and the method mainly focuses on how to reduce the reasoning delay of the large language model. And the reasoning delay is reduced by using a model compression technology to compress a large language model, so that the calculation amount of model reasoning is reduced, the reasoning delay is reduced, the reasoning process is accelerated, and meanwhile, the precision loss can be ensured to be as small as possible.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the inference optimization of a large language model, and in particular to a large-scale language model (LLM) inference optimization method based on token fusion. Background Art

[0002] Current large-scale language models (LLMs) perform outstandingly on a variety of complex language tasks, but the number of parameters is usually as high as billions or even tens of billions, which brings expensive computing and memory costs. The video memory storage capacity and memory IO of existing GPUs are still relatively low, resulting in high inference latency for such models. Therefore, LLMs are very expensive for applications with high real-time requirements or latency sensitivity.

[0003] In the existing pruning methods for LLM, most of them prune the weight parameters of the model to reduce the number of model parameters and the amount of calculation. It is found that there is a general similarity between tokens in LLM, that is, in the input and output of each layer of transformer decoder, many tokens have a high degree of similarity, so the present invention uses a token fusion-based method to compress the model, and the compressed matrix after token fusion is involved in the operation, thereby reducing the amount of calculation and delay during model reasoning.

[0004] In recent years, LLM based on Transformer structure has performed well in many complex language processing tasks, especially GPT-style generative models, which have shown outstanding performance and excellent contextual learning ability. However, due to its huge size and computational cost, it is difficult to deploy such models on resource-constrained devices, and large language models are very expensive to reason about. It is still a big challenge to use such models in latency-sensitive applications. Generative large language models are mainly decoder-only models based on Transformer structure, which are composed of multiple decoder blocks stacked together, and the number of parameters is usually as high as billions. At present, the reasoning optimization for LLM is mainly in terms of throughput and latency. Throughput focuses on the amount of data processed per unit time, and latency focuses on user experience, that is, the time required for the model to generate the answer text after the user enters the question. Current research on LLM compression methods currently focuses on pruning (sparseness) and quantization. Quantization maps the parameters and activation values ​​of the model to lower-bit values, reducing the precision of the numerical representation in the model, thereby reducing the memory overhead and computational cost of the model. Although quantization can greatly reduce the storage requirements of the model, it requires specific hardware to support the calculation of low-precision values, such as 8-bit integer matrix calculations. In fact, pruning, as a model compression method that has long appeared, has been successful in many neural network models, but its application in LLM is very rare. In natural language understanding tasks, it is mainly applied to small language models such as the Bert model. Pruning removes redundant or unimportant components in the model, from single weights (unstructured pruning) to higher-granularity structures, such as rows or columns of weight matrices (structured pruning), thereby reducing scale and computational complexity. However, pruning methods with excellent performance usually require additional retraining or fine-tuning to restore accuracy, but they have not been widely used on LLM. On the one hand, pruning or sparsification methods often require retraining or iterative fine-tuning to restore the accuracy of the model, but this is not applicable to models with billions of parameters; on the other hand, LLM's unique contextual learning ability is difficult to maintain intact after pruning.

[0005] The current mainstream pruning applied on LLM is weight-based pruning. This method first defines a standard for evaluating the importance or redundancy of weights, and then prunes the weights that meet the standard in turn according to the standard. Then, the pruned model is fine-tuned to restore the accuracy loss caused by pruning. This solution belongs to static pruning, that is, the model is fixed after the implementation of the entire pruning solution, and the same parameters are used for model reasoning afterwards. At present, there are some static pruning solutions for LLM. Although they are one-shot methods that do not require retraining, some methods still require a small amount of weight update process and do not achieve true reasoning delay acceleration. Another type of weight-based pruning is dynamic pruning. The content of pruning is different according to the different inputs of the model each time, that is, the input determines which weight elements of the model will be pruned.

[0006] Existing LLM compression methods, including model sparsification and quantization, require additional hardware support. For example, quantization requires a GPU with fixed-point computing, and sparsification requires co-design with hardware to achieve acceleration. In terms of pruning, weight-based static pruning mainly focuses on the sparsification of linear layer weights, which also requires support such as semi-structured GPU hardware to achieve efficient inference deployment. Summary of the invention

[0007] In view of the above-mentioned prior art, the present invention provides a large-scale language model reasoning optimization method based on token fusion. The present invention uses a pruning method to compress LLM, and mainly focuses on how to reduce the reasoning delay of a large language model. To reduce the reasoning delay, it is necessary to use model compression technology to compress large language models, reduce the amount of calculation for model reasoning, thereby reducing the reasoning delay, speeding up the reasoning process, and making the application of LLM more popular. The present invention is a one-shot pruning method for LLM compression that does not require retraining or fine-tuning, and can reduce the reasoning delay while ensuring the smallest possible loss of accuracy.

[0008] In order to solve the above technical problems, the present invention proposes a large-scale language model reasoning optimization method based on token fusion, wherein the large-scale language model includes an embedding layer, a multi-layer Transformer module and a tail linear layer connected in sequence, and the Transformer module includes a multi-head attention mechanism module, a multi-layer perceptron module, a token grouping module, a token fusion module, a token difference predictor and a token restoration module; the multi-head attention mechanism module includes three linear layers; the method includes:

[0009] Step 1: The text sequence preprocessed by the word segmenter enters the embedding layer for encoding, generates a word vector and a position vector, and adds the word vector and the position vector to obtain a hidden state as the input matrix of the Transformer module;

[0010] Step 2: for the large-scale language model, perform calculation of the multi-head attention mechanism module, token grouping, token fusion, calculation of the multi-layer perceptron module and token recovery operation layer by layer; including the following steps:

[0011] Step 2-1) First, calculate the multi-head attention mechanism module, and use the attention score matrix generated by the multi-head attention mechanism module to obtain the preliminary token similarity matrix;

[0012] Step 2-2) using the token grouping module, based on the similarity obtained by the similarity matrix of the token and the set similarity threshold, the tokens are grouped, and the multi-head attention mechanism module is used to calculate the grouping index of each token;

[0013] Step 2-3) Using the token fusion module, the input of the multi-layer perceptron module is token-fused based on the obtained group index of each token, and the tokens in the same group are fused into a token vector, thereby obtaining a compressed input matrix;

[0014] Step 2-4) Input the obtained compressed input matrix to the multi-layer perceptron module for calculation, input the calculation result to the multi-head attention mechanism module of the next layer, perform three linear layer calculations, and generate the query matrix Q, key matrix K and value matrix V; at the same time, the token difference predictor generates the difference between each token in the group after token fusion and before fusion;

[0015] Step 2-5) The token restoration module copies the query matrix Q, key matrix K and value matrix V output by the three linear layers of the multi-head attention mechanism module to the token vector within the group, and restores the original number of tokens. Then, the query matrix Q, key matrix K and value matrix V are added to the output result of the token difference predictor to obtain the restored query matrix Q, key matrix K and value matrix V, which are used for calculations after the three linear layers in the multi-head attention mechanism module.

[0016] Step 3: Repeat step 2 until the last layer of the large-scale language model;

[0017] Step 4: The hidden state generated by the last layer of the Transformer module enters the tail linear layer for calculation, and the probability of each word in the vocabulary is output.

[0018] Furthermore, the large-scale language model reasoning optimization method of the present invention comprises:

[0019] The specific contents of step 2-1) include:

[0020] The multi-head attention mechanism module adopts a query-key-value mode. First, the input of the multi-head attention mechanism module is mapped to three different spaces through three linear layers to obtain a query matrix Q, a key matrix K and a value matrix V. The vector groups at corresponding positions in the key matrix K and the value matrix V are key-value pairs, representing the input information; for each query vector q in the query matrix Q, vector matrix multiplication is performed with the key matrix K to generate different attention score matrices, representing the attention scores of each token to other tokens; the attention scores are used to perform weighted average on different tokens, and finally linear mapping is performed to generate the output of the multi-head attention mechanism module;

[0021] In the multi-head attention mechanism module, the query matrix Q and the key matrix K are scaled dot product to generate an attention score matrix. In the attention score matrix, the value of each row represents the attention weight of the token to other tokens, indicating the similarity between the token and other tokens; the attention score matrix is ​​a preliminary token similarity matrix.

[0022] The specific contents of step 2-2) include:

[0023] The token grouping module adopts a threshold attenuation grouping method, defines a similarity threshold interval and attenuation step in advance, and groups tokens in descending order from the maximum threshold value according to the step length. The similarity matrix is ​​a matrix obtained by normalizing the scaled dot product results of the matrix Q and the matrix K. One row or one column represents one token. The similarity matrix is ​​a diagonal matrix, in which the elements represent the similarity between the token represented by the row and the token represented by the column.

[0024] According to the threshold interval and step size, multiple thresholds are obtained, and grouping is performed once according to each threshold, and each grouping is performed on the basis of the previous step;

[0025] In each grouping, matrix calculations such as matrix product are used. First, the similarity matrix is ​​set to 1 if it is greater than the threshold, and the other elements are set to 0 to obtain a preliminary grouping mask matrix. Then, the rows with more than 1 elements in the grouping mask are extracted as a group, and the masks of each group are concatenated to generate the grouping mask matrix for this time. At the same time, the similarity of the tokens of the determined group is set to -1.

[0026] The group mask matrix generated each time is spliced ​​together in the row direction, and all tokens that do not meet the threshold conditions are treated as separate groups to form a one-dimensional vector. The one-dimensional vector and the group mask matrix are spliced ​​together to form the final group mask matrix, which is used as the token fusion for the next step;

[0027] The grouping mask matrix is ​​in one-hot encoding format, each row represents a group, each column represents a token, and the element at position (i, j) indicates that the i-th group contains the j-th token; the fusion method is to perform weighted averaging on the tokens in the group based on similarity, and each group is finally fused into one token; the grouping mask matrix represents the grouping index of each token.

[0028] The specific contents of steps 2-3) include:

[0029] First, matrix multiplication is performed on the group mask matrix and the input matrix of the multilayer perceptron module;

[0030] Then, each row is divided by the number of tokens in the group to obtain a weighted average matrix;

[0031] Finally, the input size of the multi-layer perceptron module changes from (sequence length, hidden dimension) to (number of groups, hidden dimension), and the fused matrix is ​​then input into the multi-layer perceptron module for calculation.

[0032] The specific contents of steps 2-4) include:

[0033] The calculation results of the multi-layer perceptron module are input into the multi-head attention mechanism module of the next layer, and three linear layers are calculated to generate the query matrix Q, key matrix K and value matrix V;

[0034] At the same time, the compressed matrix after the token fusion in the previous step is restored to the shape and vector order of the original multi-layer perceptron module input matrix by copying the vectors of each group. Then, the input matrix after the restored shape is subtracted from the original input matrix to obtain the difference before and after the fusion, and the difference is input into the token difference predictor for difference prediction;

[0035] The structure of the token difference predictor is a two-layer multi-layer perceptron structure, which contains a Sigmoid activation function in the middle. The output and the input of the token difference predictor are both difference matrices, and the shape is the same as the input matrix of the multi-layer perceptron module in the Transformer. The output of the token difference predictor is the difference between the output of the compressed matrix after token fusion after the multi-layer perceptron operation and the three linear layers of the multi-head attention mechanism module and the output of the uncompressed matrix after the multi-layer perceptron module and the three linear layers of the multi-head attention mechanism module, so that the token is restored by the difference.

[0036] The specific contents of steps 2-5) include:

[0037] The token restoration module copies the query matrix Q, key matrix K and value matrix V output by the three linear layers of the multi-head attention mechanism module to the token vector within the group, and restores the original number of tokens. Then, the query matrix Q, key matrix K and value matrix V are added to the output result of the token difference predictor to obtain the restored query matrix Q, key matrix K and value matrix V, and the calculation after the three linear layers in the multi-head attention mechanism module is continued, that is, the scaled dot product of the Q matrix and the K matrix is ​​generated to generate the attention score matrix, and the attention score matrix is ​​multiplied by the V matrix and then enters the linear layer to generate the output of the multi-head attention mechanism module.

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

[0039] For generative large language models, grouping, fusing and compressing tokens based on token similarity can reduce the computational complexity of LLM reasoning, especially for FFN modules where parameters account for a large proportion of the model. Using compressed input for forward propagation can significantly reduce computational overhead, thereby reducing the reasoning delay of LLM and achieving efficient deployment of the model. The method of the present invention can be implemented using only conventional hardware. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 This is a schematic diagram of the overall steps of the large-scale language model inference optimization method based on token fusion.

[0041] Figure 2 This is the model structure diagram of the optimized large-scale language model. The figure shows the relationship between each module and the forward reasoning process. The dotted arrow actually points to the next layer. The structure of each layer is the same and is not shown separately in the figure.

[0042] Figure 3 This is a detailed structural diagram of the large-scale language model inference optimization method based on token fusion in the model. Figure 2 The detailed structure of some modules is shown in the figure.

[0043] Figure 4 It is a schematic diagram of the key-value pair attention mechanism.

[0044] Figure 5 It is a schematic diagram of the token grouping mask matrix, showing the generation process of the grouping mask matrix.

[0045] Figure 6 It is a schematic diagram of the token fusion algorithm, showing the calculation process of using the group mask matrix to input the matrix of the multi-layer perceptron module for token fusion.

[0046] Figure 7This is a schematic diagram of the input data of the token difference predictor, showing the generation process of the input data.

[0047] Figure 8 It is a schematic diagram of the structure of the token difference predictor.

[0048] Fig. 9 It is a token restoration diagram, showing the process of token restoration using the difference matrix generated by the token difference predictor. DETAILED DESCRIPTION

[0049] The design concept of a large-scale language model inference optimization method based on token fusion proposed in this invention mainly focuses on compressing the number of tokens to perform model pruning to reduce inference latency. In terms of the pruned content, LLM pruning methods are mainly divided into two categories: weight-based pruning and token-based pruning.

[0050] There are many fully connected layers in the Transformer structure. The value of the input data can indirectly determine the value of the output, which is related to the importance of the relevant weights. Therefore, the pruning weights are dynamically determined based on the input. This method does not require retraining and fine-tuning, and reduces memory bandwidth and inference latency by reducing some calculation processes.

[0051] In addition to weight-based pruning, there are word token-based pruning methods on language models. The input of a language model is usually one or more sentences, each sentence consists of multiple words, namely tokens, and then converted into word vectors of the same dimension through the Embedding layer. Token-based pruning is to prune part of the word vectors of a certain layer or each layer input to reduce the amount of calculation. Token-based pruning is currently mainly used in vision transformer models and small language models (such as Bert) in the visual field, and has not yet been applied to LLM. In addition, similar to token pruning is the token-based fusion solution, which is to fuse part of the tokens into one token. This method is also only applied to visual models.

[0052] In LLM, there are many tokens that are very similar. In the present invention, a pruning method applied to LLM is designed by combining token fusion and dynamic pruning scheme. This method does not require retraining and fine-tuning. By fusing similar tokens, the amount of input data for each layer is reduced, thereby reducing the amount of computational cost of model reasoning and accelerating the reasoning process.

[0053] The compression technology in the present invention is aimed at generative pre-trained large language models based on Transformer structures, such as OPT and Llama models. In such models, the basic structure is the Transformer block, and token fusion and restoration operations are performed layer by layer based on the Transformer block.

[0054] The present invention proposes a large-scale language model reasoning optimization method based on token fusion, wherein the large-scale language model comprises an embedding layer, a multi-layer Transformer module and a tail linear layer connected in sequence, wherein the Transformer module comprises a multi-head attention mechanism module, a multi-layer perceptron module, a token grouping module, a token fusion module, a token difference predictor and a token restoration module; the multi-head attention mechanism module comprises three linear layers; the input and output of the multi-head attention mechanism module (Multi-Head Attention, MHA) and the multi-layer perceptron module (Multi-Layer Perceptron, MLP) are matrices composed of embedding vectors of multiple tokens, and each row represents a token in a sentence sequence. The pruning scheme of the present invention first obtains the similarity matrix of the token according to the intermediate output of MHA, and then groups the tokens based on the similarity and a threshold set in advance to obtain the grouping index of each token. This process is actually carried out in parallel with the attention calculation process of MHA. After the MHA calculation is completed, the input of the MLP module is token-fused based on the grouping index obtained earlier, that is, the tokens in the same group are fused into a token vector to obtain the compressed input matrix, and then enter the calculation of the MLP module, as well as the first half of the calculation of the MHA module in the next layer. At the same time, the token difference predictor will generate the difference between the tokens in the group after and before the fusion of tokens, so before the Attention module starts the calculation of the second half, it will first perform a token recovery operation based on the generated difference to restore the original number of tokens, and then continue the same operation of the next layer.

[0055] like Figure 1 As shown, the method comprises the following steps:

[0056] Step 1: The text sequence preprocessed by the word segmenter enters the embedding layer for encoding to generate word vectors and position vectors. The word vectors and position vectors are added together to obtain a hidden state as the input matrix of the Transformer module.

[0057] Step 2: for the large-scale language model, perform calculation of the multi-head attention mechanism module, token grouping, token fusion, calculation of the multi-layer perceptron module and token recovery operation layer by layer; including the following steps:

[0058] Step 2-1) First, calculate the multi-head attention mechanism module, and use the attention scores generated by the multi-head attention mechanism module to obtain the preliminary token similarity matrix; the specific contents include:

[0059] The multi-head attention mechanism module adopts a query-key-value mode. First, the input of the multi-head attention mechanism module is mapped to three different spaces through three linear layers to obtain a query matrix Q, a key matrix K and a value matrix V. The vector groups at corresponding positions in the key matrix K and the value matrix V are key-value pairs representing the input information; for each query vector q in the query matrix Q, vector-matrix multiplication is performed with the key matrix K to generate different attention score matrices, representing the attention scores of each token to other tokens; the attention scores are used to perform weighted averaging on different tokens, and finally linear mapping is performed to generate the output of the multi-head attention mechanism module.

[0060] In the multi-head attention mechanism module, the query matrix Q and the key matrix K are scaled dot product to generate an attention score. In the attention score matrix, the value of each row represents the attention weight of the token to other tokens, indicating the similarity between the token and other tokens; the attention score matrix is ​​a preliminary token similarity matrix.

[0061] Step 2-2) Using the token grouping module, the tokens are grouped based on the similarity obtained by the token similarity matrix and the set similarity threshold, and the multi-head attention mechanism module is used to calculate the grouping index of each token; the specific contents include:

[0062] The token grouping module adopts a threshold attenuation grouping method, defines a similarity threshold interval and attenuation step in advance, and groups tokens in descending order from the maximum threshold value according to the step size. The similarity matrix is ​​a matrix obtained by normalizing the scaled dot product results of the matrix Q and the matrix K. One row or one column represents a token. The similarity matrix is ​​a diagonal matrix, and the elements therein represent the similarity between the token represented by the row and the token represented by the column.

[0063] Multiple thresholds are obtained according to the threshold interval and the step size, and grouping is performed according to each threshold, and each grouping is performed on the basis of the previous step.

[0064] In each grouping, matrix calculations such as matrix product are used. First, the similarity matrix elements greater than the threshold are set to 1, and the other elements are set to 0 to obtain a preliminary grouping mask matrix. Then, the rows with more than 1 elements in the grouping mask are extracted as a group, and the masks of each group are concatenated to generate the grouping mask matrix for this time. At the same time, the similarity of the tokens of the determined group is set to -1.

[0065] The grouping mask matrix generated each time is spliced ​​together in the row direction, and all tokens that do not meet the threshold conditions are treated as separate groups to form a one-dimensional vector. The one-dimensional vector is spliced ​​with the grouping mask matrix to form the final grouping mask matrix, which is used as the next step of token fusion.

[0066] The grouping mask matrix is ​​in one-hot encoding format, each row represents a group, each column represents a token, and the element at position (i, j) indicates that the i-th group contains the j-th token; the fusion method is to perform weighted averaging on the tokens in the group based on similarity, and each group is finally fused into one token; the grouping mask matrix represents the grouping index of each token.

[0067] Taking the Llama2 model as an example, in order to model long-distance dependencies and process variable-length input sequences, the Transformer model uses a self-attention mechanism, which "dynamically" generates weights for different connections. The self-attention mechanism uses a "query-key-value" model, such as Figure 3 As shown in , the input of MHA is first mapped to three different spaces to obtain the query matrix Q, key matrix K and value matrix V. The vector groups at corresponding positions in K and V are key-value pairs representing the input information. Figure 4 As shown in the figure, for each query vector q in Q, it is vector-matrix multiplied with K to generate different attention distributions, which represents the attention score of each token to other tokens, that is, the Attention Score matrix after matmul in the figure. Then, the matrix is ​​softmaxed and matrix-multiplied with the V matrix representing value information, that is, the attention scores are used to perform weighted average on different tokens, and finally a linear mapping is performed to generate the output of the MHA module.

[0068] In the MHA module, the attention score generated by the scaled dot product of Q and K actually also represents a similarity relationship. The value of each row represents the attention weight of the token to other tokens, which indirectly reflects the similarity between the token and other tokens. Therefore, the present invention groups tokens based on the matrix scaled dot product result of Q and K. The token grouping process runs in parallel with the product of the attention score matrix and V in MHA and the subsequent calculation process.

[0069] The grouping process adopts a threshold attenuation grouping method. The present invention defines a similarity threshold interval and an attenuation step, and groups tokens in descending order from the maximum threshold value according to the step length. The similarity matrix is ​​a matrix obtained by normalizing the scaled dot product result of QK. A row or a column represents a token, and the specific elements represent the similarity between the token represented by the row and the token represented by the column, so the similarity matrix is ​​a diagonal matrix. The present invention obtains multiple thresholds based on the threshold interval and the step length. The present invention performs a grouping based on each threshold, and each grouping is performed on the basis of the previous step.

[0070] Specifically, in order to reduce the extra time cost for grouping in each step, the present invention uses matrix operations such as matrix product to complete each grouping, thus avoiding a large amount of time consumption of for loops. Figure 5 As shown in the figure, firstly, the similarity matrix is ​​set to 1 if it is greater than the threshold value, and the other elements are set to 0 to obtain a preliminary grouping mask matrix; in order to ensure that the token does not appear repeatedly in different groups, the grouping mask is processed to ensure that there is only one 1 in each column; then the rows with more than 1 elements in the grouping mask are extracted as a group, and the masks of each group are concatenated to generate the grouping mask matrix of this step. At the same time, the similarities of the tokens of these determined groups are set to -1 to ensure that these tokens are not considered in the next grouping.

[0071] Finally, the group mask matrices generated each time are concatenated in the row direction. In addition, all tokens that do not meet the threshold conditions are treated as separate groups, that is, each group contains only one token, and are concatenated with the group mask matrix to form the final group mask matrix, which is used as the next step of token fusion.

[0072] Step 2-3) Token fusion is performed on the input of the multi-layer perceptron module based on the obtained group index of each token using the token fusion module, and the tokens in the same group are fused into a token vector, thereby obtaining a compressed input matrix; the specific contents include:

[0073] First, matrix multiplication is performed on the group mask matrix and the input matrix of the multilayer perceptron module;

[0074] Then, each row is divided by the number of tokens in the group to obtain a weighted average matrix;

[0075] Finally, the input size of the multi-layer perceptron module changes from (sequence length, hidden dimension) to (number of groups, hidden dimension), and the fused matrix is ​​then input into the multi-layer perceptron module for calculation.

[0076] Specifically, after obtaining the grouping information of the tokens, the tokens need to be fused. Because there is interaction between tokens in the calculation process of the attention mechanism in the MHA module, fusion cannot be performed in this process, and because the calculation of the MLP has no interaction with the tokens, the token fusion is selected for the input of the MLP module. Figure 3 As shown, the output of MHA is the input of MLP. In order to reduce the amount of calculation, the present invention applies the fusion algorithm before the residual connection branch.

[0077] The input of MLP is a matrix composed of the embedding vectors of all tokens. The purpose of the present invention is to compress the input matrix by reducing the number of tokens and reduce the subsequent calculation amount. The grouping mask matrix generated by the grouping algorithm is in one-hot encoding format, where each row represents a group, each column represents a token, and the position (i, j) where the element is 1 means that the i-th group contains the j-th token. The fusion method of the present invention is to perform weighted averaging on the tokens in the group based on similarity, and each group is finally fused into one token, such as Figure 6 As shown. The specific calculation method is to directly perform matrix multiplication on the group mask matrix and the MLP input matrix, and then divide each row by the number of tokens in the group to obtain the weighted average matrix. Finally, the input size of the MLP changes from (sequence length, hidden dimension) to (number of groups, hidden dimension), and the matrix dimension is greatly reduced. The fused matrix then enters the MLP module for subsequent calculations. Usually, the MLP module of the Transformer structure contains two Linear layers. Figure 3 The structure of Llama2 is shown in the figure, which includes three Linear layers. Therefore, the compressed matrix after fusion of tokens will reduce a lot of linear layer operations, thereby accelerating reasoning.

[0078] Step 2-4) Input the compressed input matrix obtained into the multi-layer perceptron module for calculation, input the calculation result into the multi-head attention mechanism module of the next layer, perform three linear layer calculations, generate the query matrix Q, key matrix K and value matrix V; at the same time, the token difference predictor generates the difference between each token in the group after token fusion and before fusion; the specific contents include:

[0079] The calculation process of the token difference predictor is as follows: the compressed matrix after token fusion is restored to the shape and vector order of the original multi-layer perceptron module input matrix by copying the vectors of each group, and then the input matrix after the restored shape is subtracted from the original input matrix to obtain the difference before and after fusion, and the difference is input into the token difference predictor for prediction, such as Figure 7 shown.

[0080] The structure of the token difference predictor is a two-layer multi-layer perceptron structure, which contains a Sigmoid activation function in the middle. The output and the input of the token difference predictor are both difference matrices, and the shape is the same as the input matrix of the multi-layer perceptron module in the Transformer, such as Figure 8 shown.

[0081] The output of the token difference predictor is the difference between the output of the compressed matrix after token fusion after the multi-layer perceptron operation and the three linear layers of the multi-head attention mechanism module and the output of the uncompressed matrix after the multi-layer perceptron module and the three linear layers of the multi-head attention mechanism module, so that the token is restored through the difference.

[0082] In addition, it is necessary to collect data in advance to train the token difference predictor. The entire model process should be run according to the positive operation steps and the steps including the token fusion operation. The input of the token difference predictor of each layer should be collected and labels should be made according to the output. In this way, the token difference predictor of each layer can be trained separately to enable it to learn the ability to restore tokens.

[0083] Step 2-5) The token restoration module copies the query matrix Q, key matrix K and value matrix V output by the three linear layers of the multi-head attention mechanism module to the token vector within the group, and restores the original number of tokens. Then, the query matrix Q, key matrix K and value matrix V are added to the output result of the token difference predictor to obtain the restored query matrix Q, key matrix K and value matrix V, which are used for calculations after the three linear layers in the multi-head attention mechanism module.

[0084] After token fusion, it needs to be restored to its original shape after several modules are calculated. As mentioned above, the number of tokens in the Q KV matrix after the three linear layers is restored in the MHA module. Specifically, the token difference predictor will generate the difference before and after the fusion of the three QKV matrices. The QKV matrices generated by the three linear layers are added to the output results of the token difference predictor. Fig. 9 As shown, the QKV matrix with restored shape is then used to continue the subsequent attention calculation, that is, Q and K are scaled dot product to generate attention scores, and the attention scores are then multiplied by the V matrix to summarize the token information, as shown in Figure 3 shown.

[0085] Step 3: Repeat step 2 until the last layer of the large-scale language model;

[0086] Step 4: The hidden state generated by the last layer of the Transformer module enters the tail linear layer for calculation, and the probability of each word in the vocabulary is output.

[0087] Although the present invention has been described above in conjunction with the accompanying drawings, the present invention is not limited to the above-mentioned specific embodiments, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can make many improvements and changes without departing from the purpose of the present invention, which are all within the protection of the present invention.

Claims

1. A large-scale language model reasoning optimization method based on token fusion, characterized in that: The large-scale language model includes an embedding layer, a multi-layer Transformer module and a tail linear layer connected in sequence, wherein the Transformer module includes a multi-head attention mechanism module, a multi-layer perceptron module, a token grouping module, a token fusion module, a token difference predictor and a token recovery module; The multi-head attention mechanism module includes three linear layers; the method includes: Step 1: The text sequence preprocessed by the word segmenter enters the embedding layer for encoding, generates a word vector and a position vector, and adds the word vector and the position vector to obtain a hidden state as the input matrix of the Transformer module; Step 2: for the large-scale language model, perform calculation of the multi-head attention mechanism module, token grouping, token fusion, calculation of the multi-layer perceptron module and token recovery operation layer by layer; including the following steps: Step 2-1) First, perform the calculation of the multi-head attention mechanism module, and use the attention score matrix generated by the multi-head attention mechanism module to obtain the preliminary token similarity matrix; Step 2-2) using the token grouping module, based on the similarity obtained by the similarity matrix of the token and the set similarity threshold, the tokens are grouped, and the multi-head attention mechanism module is used to calculate the grouping index of each token; Step 2-3) Using the token fusion module, the input of the multi-layer perceptron module is token-fused based on the obtained group index of each token, and the tokens in the same group are fused into a token vector, thereby obtaining a compressed input matrix; Step 2-4) Input the obtained compressed input matrix to the multi-layer perceptron module for calculation, input the calculation result to the multi-head attention mechanism module of the next layer, perform three linear layer calculations, and generate the query matrix Q, key matrix K and value matrix V; at the same time, the token difference predictor generates the difference between each token in the group after token fusion and before fusion; Step 2-5) The token restoration module copies the query matrix Q, key matrix K and value matrix V output by the three linear layers of the multi-head attention mechanism module to the token vector within the group, and restores the original number of tokens. Then, the query matrix Q, key matrix K and value matrix V are added to the output result of the token difference predictor to obtain the restored query matrix Q, key matrix K and value matrix V, which are used for calculations after the three linear layers in the multi-head attention mechanism module. Step 3: Repeat step 2 until the last layer of the large-scale language model; Step 4: The hidden state generated by the last layer of the Transformer module enters the tail linear layer for calculation, and the probability of each word in the vocabulary is output.

2. The large-scale language model reasoning optimization method according to claim 1, characterized in that: The specific contents of step 2-1) include: The multi-head attention mechanism module adopts a query-key-value mode. First, the input of the multi-head attention mechanism module is mapped to three different spaces through three linear layers to obtain a query matrix Q, a key matrix K and a value matrix V. The vector groups at corresponding positions in the key matrix K and the value matrix V are key-value pairs, representing the input information; for each query vector q in the query matrix Q, vector matrix multiplication is performed with the key matrix K to generate different attention score matrices, representing the attention scores of each token to other tokens; the attention scores are used to perform weighted average on different tokens, and finally linear mapping is performed to generate the output of the multi-head attention mechanism module; In the multi-head attention mechanism module, the query matrix Q and the key matrix K are scaled dot product to generate an attention score matrix. In the attention score matrix, the value of each row represents the attention weight of the token to other tokens, indicating the similarity between the token and other tokens; the attention score matrix is ​​a preliminary token similarity matrix.

3. The large-scale language model reasoning optimization method according to claim 2, characterized in that: The specific contents of step 2-2) include: The token grouping module adopts a threshold attenuation grouping method, defines a similarity threshold interval and attenuation step in advance, and groups tokens in descending order from the maximum threshold value according to the step length. The similarity matrix is ​​a matrix obtained by normalizing the scaled dot product results of the matrix Q and the matrix K. One row or one column represents one token. The similarity matrix is ​​a diagonal matrix, in which the elements represent the similarity between the token represented by the row and the token represented by the column. According to the threshold interval and step size, multiple thresholds are obtained, and grouping is performed once according to each threshold, and each grouping is performed on the basis of the previous step; In each grouping, matrix calculations such as matrix product are used. First, the similarity matrix is ​​set to 1 if it is greater than the threshold, and the other elements are set to 0 to obtain a preliminary grouping mask matrix. Then, the rows with more than 1 elements in the grouping mask are extracted as a group, and the masks of each group are concatenated to generate the grouping mask matrix for this time. At the same time, the similarity of the tokens of the determined group is set to -1. The group mask matrix generated each time is spliced ​​together in the row direction, and all tokens that do not meet the threshold conditions are treated as separate groups to form a one-dimensional vector. The one-dimensional vector and the group mask matrix are spliced ​​together to form the final group mask matrix, which is used as the token fusion for the next step; The grouping mask matrix is ​​in one-hot encoding format, where each row represents a group, each column represents a token, and the element at position (i, j) indicates that the i-th group contains the j-th token; the fusion method is to perform weighted averaging on the tokens in the group based on similarity, and each group is finally fused into one token; The grouping mask matrix represents the grouping index of each token.

4. The large-scale language model reasoning optimization method according to claim 3, characterized in that: The specific contents of steps 2-3) include: First, matrix multiplication is performed on the group mask matrix and the input matrix of the multilayer perceptron module; Then, each row is divided by the number of tokens in the group to obtain a weighted average matrix; Finally, the input size of the multi-layer perceptron module changes from (sequence length, hidden dimension) to (number of groups, hidden dimension), and the fused matrix is ​​then input into the multi-layer perceptron module for calculation.

5. The large-scale language model reasoning optimization method according to claim 4, characterized in that: The specific contents of steps 2-4) include: The compressed matrix after token fusion is restored to the shape and vector order of the original multi-layer perceptron module input matrix by copying the vectors of each group. Then, the input matrix after the restored shape is subtracted from the original input matrix to obtain the difference before and after fusion, and the difference is used as the input of the token difference predictor. Calculation of token difference predictor: The structure of the token difference predictor is a two-layer multi-layer perceptron structure with a Sigmoid activation function in the middle. The output and the input of the token difference predictor are both difference matrices, and the shape is the same as the input matrix of the multi-layer perceptron module in the Transformer. The output of the token difference predictor is the difference between the output of the compressed matrix after token fusion after the multi-layer perceptron operation and the three linear layers of the multi-head attention mechanism module and the output of the uncompressed matrix after the multi-layer perceptron module and the three linear layers of the multi-head attention mechanism module, so that the token is restored through the difference.

Citation Information

Patent Citations

  • Compression method and system used for neural network language model (NN LM)

    CN108415888A

  • Large language model reasoning optimization method and device, computer equipment and storage medium

    CN117194056A

  • Neural machine translation method fusing Bert pre-training language knowledge

    CN117313750A

  • Method for generating large model by reasoning text

    CN117391079A

  • Operation method and system of large language model, product, equipment and medium

    CN118113481A

Cited By

  • Dynamic reasoning method and system under large language model

    CN119150863A

  • Input vector processing method and system for large model

    CN119829736A

  • Large-scale language model reasoning acceleration method based on cache fusion, electronic equipment and storage medium

    CN119831032A

  • Structured pruning method for large language model and related equipment

    CN119849578A

  • Dynamic layer cutting system and method for large language model

    CN120181137A