Language model reasoning optimization method, electronic device, storage medium and program product
By compressing the initial query vector and key vector of the large language model, the problem of storing a large amount of memory during the inference process is solved, and more efficient memory usage and computing efficiency are achieved.
Patent Information
- Application Number
- CN202410857744.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-06-28
AI Technical Summary
During the inference process of large language models, the storage of key vectors and value vectors takes up a lot of memory, resulting in low storage space efficiency.
By pre-populating the content to be analyzed, the initial query vector, key vector and value vector are generated and compressed to determine the target compression key vector, reduce the memory usage of storage, and output the analysis results based on the compression vector.
It effectively reduces the memory usage and calculation amount, and improves the output efficiency of analysis results.
Smart Images

Figure 00000000_0000
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a language model reasoning optimization method, an electronic device, a storage medium and a program product. Background Art
[0002] Large Language Model (LLM) is a hot topic in artificial intelligence technology today. LLM is a deep learning model trained based on massive text data. It has the ability to understand, generate and process natural language, and can play a role in various natural language processing tasks, such as text summarization, question-answering system, machine translation, etc.
[0003] The current LLM refers to a large model with more than 10 billion or even 100 billion parameters and mainly based on the transformer-decoder structure. The GPT4 / Sora that we are familiar with recently belongs to the category of LLM. Among them, the transformer-decoder model structure is composed of multiple transformer blocks. Each block generates a query vector, a key vector, and a value vector during the analysis of the input content to be analyzed. Then, based on the generated query vector, key vector, and value vector, more complex calculations are performed.
[0004] When performing complex calculations based on the generated query vectors, key vectors, and value vectors, it is usually necessary to store the key vectors and value vectors generated by each block. If the key vectors and value vectors are not cached, they need to be recalculated every time inference is performed. However, storing the calculated key vectors and value vectors in a storage device will take up a very large storage space.
[0005] Therefore, how to reduce the memory occupied by storing key vectors and value vectors when performing content analysis based on a large language model has become an urgent problem to be solved. Summary of the invention
[0006] In view of this, the present invention provides a language model reasoning optimization method, an electronic device, a storage medium and a program product to solve the problem of how to reduce the memory occupied by storing key vectors and value vectors.
[0007] In a first aspect, the present invention provides a language model reasoning optimization method, the method comprising:
[0008] Acquire at least one information unit to be analyzed corresponding to the content to be analyzed, and input each information unit to be analyzed into a preset language analysis model;
[0009] Based on the preset language analysis model, each information unit to be analyzed is pre-filled to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed;
[0010] Compressing each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector;
[0011] Determine at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector;
[0012] Storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors;
[0013] The target analysis result corresponding to the content to be analyzed is output based on each compressed query vector, each target initial key vector and each target initial value vector.
[0014] The language model reasoning optimization method provided in the embodiment of the present application obtains at least one information unit to be analyzed corresponding to the content to be analyzed, and inputs each information unit to be analyzed into a preset language analysis model; based on the preset language analysis model, each information unit to be analyzed is pre-filled to obtain the initial query vector, initial key vector and initial value vector corresponding to each information unit to be analyzed, and the accuracy of the obtained initial query vector, initial key vector and initial value vector is guaranteed. Each initial query vector and each initial key vector are compressed to obtain each compressed query vector and each compressed key vector, and the accuracy of each compressed query vector and each compressed key vector is guaranteed, so that each compressed query vector and each compressed key vector after compression occupies less memory. According to the relationship between each compressed query vector and each compressed key vector, at least one target compressed key vector is determined from the compressed key vector, and the accuracy of the determined at least one target compressed key vector is guaranteed. The target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector are stored, and the memory usage is reduced compared to storing the initial key vector and initial value vector before compression. The target analysis result corresponding to the content to be analyzed is output based on each compressed query vector, each target initial key vector and each target initial value vector, rather than outputting the target analysis result corresponding to the content to be analyzed based on the initial key vector and the initial value vector, thereby reducing the amount of calculation. Therefore, the above method not only reduces the memory usage, but also reduces the amount of calculation, thereby improving the efficiency of analyzing the content to be analyzed and outputting the target analysis result.
[0015] In an optional implementation, compressing each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector includes:
[0016] Each initial query vector is input into a preset query vector compression model, and each initial query vector is compressed based on a first target encoder in the preset query vector compression model to generate each compressed query vector.
[0017] Each initial key vector is input into a preset key vector compression model, and each initial key vector is compressed based on a second target encoder in the preset key vector compression model to generate each compressed key vector.
[0018] The language model reasoning optimization method provided in the embodiment of the present application inputs each initial query vector into a preset query vector compression model, compresses each initial query vector based on the first target encoder in the preset query vector compression model, generates each compressed query vector, ensures the accuracy of each generated compressed query vector, so that the compressed query vector occupies a small amount of memory. Input each initial key vector into a preset key vector compression model, compress each initial key vector based on the second target encoder in the preset key vector compression model, and generate each compressed key vector. The accuracy of each generated compressed key vector is ensured, so that the compressed key vector occupies a small amount of memory.
[0019] In an optional implementation, the training process of the preset query vector compression model includes:
[0020] Obtain multiple training query vectors;
[0021] Input each training query vector into the initial query vector compression network, encode and decode each training query vector, and obtain a virtual query vector corresponding to each training query vector;
[0022] The initial query vector compression network is trained based on each training query vector and each virtual query vector to generate a preset query vector compression model.
[0023] The language model inference optimization method provided in the embodiment of the present application obtains multiple training query vectors; each training query vector is input into the initial query vector compression network, and each training query vector is encoded and decoded to obtain a virtual query vector corresponding to each training query vector, thereby ensuring the accuracy of the virtual query vector corresponding to the obtained training query vector. The initial query vector compression network is trained based on each training query vector and each virtual query vector to generate a preset query vector compression model, thereby ensuring the accuracy of the generated preset query vector compression model.
[0024] In an optional implementation, the initial query vector compression network includes a first initial encoder and a first initial decoder, each training query vector is input into the initial query vector compression network, each training query vector is encoded and decoded, and a virtual query vector corresponding to each training query vector is obtained, including:
[0025] Inputting each training query vector into a first initial encoder in an initial query vector compression network, the first initial encoder encodes each training query vector to generate each initial encoded query vector; the dimension of the initial encoded query vector is smaller than the dimension of the training query vector;
[0026] Each initial coded query vector is input into the first initial decoder in the initial query vector compression network. The first initial decoder decodes each initial coded query vector to generate each virtual query vector. The dimension of the virtual query vector is consistent with the dimension of the training query vector.
[0027] The language model reasoning optimization method provided in the embodiment of the present application inputs each training query vector into the first initial encoder in the initial query vector compression network, and the first initial encoder encodes each training query vector to generate each initial encoded query vector, thereby ensuring the accuracy of each generated initial encoded query vector and reducing the dimension of the training query vector. Each initial encoded query vector is input into the first initial decoder in the initial query vector compression network, and the first initial decoder decodes each initial encoded query vector to generate each virtual query vector, thereby ensuring the accuracy of each generated virtual query vector. Therefore, it is possible to judge whether each encoded initial encoded query vector is accurate based on each virtual query vector.
[0028] In an optional implementation, training an initial query vector compression network based on a training query vector and a virtual query vector to generate a preset query vector compression model includes:
[0029] Based on a first preset loss function, calculating a first loss value between each training query vector and each virtual query vector;
[0030] Based on each first loss value, the initial query vector compression network is trained to generate a preset query vector compression model.
[0031] The language model inference optimization method provided in the embodiment of the present application calculates the first loss value between each training query vector and each virtual query vector based on the first preset loss function, thereby ensuring the accuracy of the calculated first loss value. Based on each first loss value, the initial query vector compression network is trained to generate a preset query vector compression model, thereby ensuring the accuracy of the generated preset query vector compression model.
[0032] In an optional implementation, the training process of the preset key vector compression model includes:
[0033] Get multiple training key vectors;
[0034] Input each training key vector into the initial key vector compression network, encode and decode each training key vector, and obtain a virtual key vector corresponding to each training key vector;
[0035] The initial key vector compression network is trained based on each training key vector and each virtual key vector to generate a preset key vector compression model.
[0036] The language model inference optimization method provided in the embodiment of the present application obtains multiple training key vectors; each training key vector is input into the initial key vector compression network, and each training key vector is encoded and decoded to obtain a virtual key vector corresponding to each training key vector, thereby ensuring the accuracy of the virtual key vector corresponding to each training key vector. The initial key vector compression network is trained based on each training key vector and each virtual key vector to generate a preset key vector compression model, thereby ensuring the accuracy of the generated preset key vector compression model.
[0037] In an optional implementation, the initial key vector compression network includes a second initial encoder and a second initial decoder, each training key vector is input into the initial key vector compression network, each training key vector is encoded and decoded, and a virtual key vector corresponding to each training key vector is obtained, including:
[0038] Inputting each training key vector into a second initial encoder in the initial key vector compression network, the second initial encoder encodes each training key vector to generate each initial encoded key vector; the dimension of the initial encoded key vector is smaller than the dimension of the training key vector;
[0039] Each initial encoded key vector is input into the second initial decoder in the initial key vector compression network, and the second initial decoder decodes each initial encoded key vector to generate each virtual key vector; the dimension of the virtual key vector is consistent with the dimension of the training key vector.
[0040] The language model reasoning optimization method provided in the embodiment of the present application inputs each training key vector into the second initial encoder in the initial key vector compression network, and the second initial encoder encodes each training key vector to generate each initial encoded key vector; the accuracy of each generated initial encoded key vector is ensured, and the dimension of the training key vector is reduced. Each initial encoded key vector is input into the second initial decoder in the initial key vector compression network, and the second initial decoder decodes each initial encoded key vector to generate each virtual key vector, which ensures the accuracy of each generated virtual key vector and enables the accuracy of the initial encoded key vector obtained after compressing the training key vector based on the virtual key vector detection.
[0041] In an optional implementation, the initial key vector compression network is trained based on each training key vector and each virtual key vector to generate a preset key vector compression model, including:
[0042] Based on a second preset loss function, calculating a second loss value between each training key vector and each virtual key vector;
[0043] Based on each second loss value, the initial key vector compression network is trained to generate a preset key vector compression model.
[0044] The language model inference optimization method provided in the embodiment of the present application calculates the second loss value between each training key vector and each virtual key vector based on the second preset loss function, thereby ensuring the accuracy of the obtained second loss value. Based on each second loss value, the initial key vector compression network is trained to generate a preset key vector compression model, thereby ensuring the accuracy of the generated preset key vector compression model.
[0045] In an optional implementation, the training key vector corresponds to the trained query vector; and the method further comprises:
[0046] Training an initial query vector compression network based on each training query vector and each virtual query vector to generate a backup query vector compression model;
[0047] Training an initial key vector compression network based on each training key vector and each virtual key vector to generate a backup key vector compression model;
[0048] Performing statistical calculations on each training query vector and the corresponding training key vector to obtain a first result;
[0049] Performing statistical calculations on each initial coding query vector and each corresponding initial coding key vector to obtain a second result;
[0050] Based on a third preset loss function between each first result and the corresponding second result, the spare query vector compression model is trained to generate a preset query vector compression model, and the spare key vector compression model is trained to generate a preset key vector compression model.
[0051] The language model reasoning optimization method provided in the embodiment of the present application trains the initial query vector compression network based on each training query vector and each virtual query vector to generate a spare query vector compression model, thereby ensuring that the accuracy of the spare query vector compression model is relatively high. The initial key vector compression network is trained based on each training key vector and each virtual key vector to generate a spare key vector compression model, thereby ensuring that the accuracy of the generated spare key vector compression model is relatively high. Statistical calculations are performed on each training query vector and the corresponding training key vector to obtain a first result, thereby ensuring the accuracy of the generated first result. Statistical calculations are performed on each initial encoding query vector and the corresponding initial encoding key vector to obtain a second result, thereby ensuring the accuracy of the generated second result. Based on the third preset loss function between each first result and the corresponding second result, the backup query vector compression model is trained to generate a preset query vector compression model, and the backup key vector compression model is trained to generate a preset key vector compression model, thereby ensuring the accuracy of the generated preset query vector compression model and the preset key vector compression model, and further ensuring the accuracy of each compressed query vector and each compressed key vector obtained based on the preset query vector compression model and the preset key vector compression model, and the accuracy of the results obtained by performing statistical calculations based on each compressed query vector and each compressed key vector.
[0052] In an optional implementation, determining at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector includes:
[0053] Obtaining a preset number of target compression key vectors corresponding to each compression query vector, where the preset number is used to represent the number of target compression key vectors associated with each compression query vector;
[0054] According to the relationship between each compressed query vector and each compressed key vector, a preset number of target compressed key vectors are determined from the compressed key vectors.
[0055] The language model reasoning optimization method provided in the embodiment of the present application obtains a preset number of target compressed key vectors corresponding to each compressed query vector, and the preset number is used to represent the number of target compressed key vectors that are associated with each compressed query vector; according to the relationship between each compressed query vector and each compressed key vector, a preset number of target compressed key vectors is determined from the compressed key vector, thereby ensuring the accuracy of the preset number of target compressed key vectors determined, and further ensuring the accuracy of the target analysis result corresponding to the output analysis content based on the target initial key vector corresponding to the target compressed key vector.
[0056] In an optional implementation, determining a target compressed key vector from the compressed key vectors according to a relationship between each compressed query vector and a corresponding preset number of compressed key vectors includes:
[0057] Calculate the attention score between each compressed query vector and each compressed key vector;
[0058] A preset number of compressed key vectors with top attention scores are determined as target compressed key vectors.
[0059] The language model reasoning optimization method provided in the embodiment of the present application calculates the attention score between each compressed query vector and each compressed key vector; a preset number of compressed key vectors with the highest attention scores are determined as target compressed key vectors, thereby ensuring the accuracy of the target analysis result corresponding to the output analysis content based on the target initial key vector corresponding to the target compressed key vector, and avoiding the discarding of relatively important target compressed key vectors, resulting in inaccurate output target analysis results. In addition, the above method outputs the target analysis result corresponding to the content to be analyzed based on each target initial key vector and each target initial value vector, rather than outputting the target analysis result based on all initial key vectors and initial value vectors, thereby reducing the amount of calculation.
[0060] In an optional implementation, outputting a target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector, and each target initial value vector includes:
[0061] Get the initial number of attention heads in the preset language analysis model;
[0062] For each attention mechanism head, obtain the position information of the target compressed key vector corresponding to the attention mechanism head in the compressed key vector;
[0063] Determine the target attention mechanism head from each attention mechanism head according to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector;
[0064] For each compressed query vector, processing the compressed query vector, the target initial key vector corresponding to the compressed query vector, and the target initial value vector corresponding to the compressed query vector based on the target attention mechanism head, and outputting a first output result;
[0065] Determine, based on the feedforward neural network layer, a second output result corresponding to the first output result;
[0066] Based on each second output result, a target analysis result is generated.
[0067] The language model reasoning optimization method provided in the embodiment of the present application obtains the initial number corresponding to the attention mechanism heads in the preset language analysis model; for each attention mechanism head, obtains the position information of the target compressed key vector corresponding to the attention mechanism head in the compressed key vector; according to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector, the target attention mechanism head is determined from each attention mechanism head, thereby ensuring the accuracy of the determined target attention mechanism head. For each compressed query vector, the compressed query vector, the target initial key vector corresponding to the compressed query vector, and the target initial value vector corresponding to the compressed query vector are processed based on the target attention mechanism head, and the first output result is output, thereby ensuring the accuracy of the output first output result, and reducing the number of target attention mechanism heads and the amount of calculation. Based on the feedforward neural network layer, the second output result corresponding to the first output result is determined, thereby ensuring the accuracy of the output second output result. Based on each second output result, a target analysis result is generated, thereby ensuring the accuracy of the generated target analysis result.
[0068] In an optional implementation, determining a target attention mechanism head from each attention mechanism head according to position information of a target compressed key vector corresponding to each attention mechanism head in the compressed key vector includes:
[0069] According to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector, the similarity between the target compressed key vectors corresponding to each attention mechanism head is calculated;
[0070] According to the similarity between the target compressed key vectors corresponding to each pair of attention heads, the target attention mechanism head is determined from each attention mechanism head.
[0071] The language model inference optimization method provided in the embodiment of the present application calculates the similarity between the target compressed key vectors corresponding to each attention mechanism head according to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector, thereby ensuring the accuracy of calculating the similarity between the target compressed key vectors corresponding to each attention mechanism head. According to the similarity between the target compressed key vectors corresponding to the two attention mechanism heads, the target attention mechanism head is determined from each attention mechanism head, thereby ensuring the accuracy of the target attention mechanism head determined from each attention mechanism head.
[0072] In an optional implementation, determining a target attention mechanism head from each attention mechanism head according to the similarity between target compressed key vectors corresponding to each of the attention mechanism heads includes:
[0073] Compare the similarity between the target compressed key vectors corresponding to the pairwise attention heads with the preset similarity threshold;
[0074] Delete any of the two attention mechanism heads whose similarity is greater than the preset similarity threshold;
[0075] Until the similarity between the target compressed key vectors corresponding to any two attention mechanism heads is less than the preset similarity threshold, the target attention mechanism head is obtained.
[0076] The language model inference optimization method provided in the embodiment of the present application compares the similarity between the target compressed key vectors corresponding to the pairwise attention mechanism heads with a preset similarity threshold; deletes any two attention mechanism heads whose similarity is greater than the preset similarity threshold, until the similarity between the target compressed key vectors corresponding to any two attention mechanism heads is less than the preset similarity threshold, and obtains the target attention mechanism head, thereby ensuring the accuracy of the obtained target attention mechanism head.
[0077] In an optional implementation, storing the target initial key vectors, the initial value vectors, and the compressed key vectors corresponding to the target compressed key vectors includes:
[0078] Storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors in a first storage device;
[0079] If a first current storage occupancy rate corresponding to the first storage device is greater than a first preset storage occupancy rate threshold, obtaining a first current compressed query vector;
[0080] Calculating a first relative score between a first current compressed query vector and each compressed key vector;
[0081] According to each first relative score, determining, from each compressed key vector, a first compressed key vector to be unloaded whose first relative score is less than a first preset threshold;
[0082] Each first compressed key vector to be unloaded and the initial value vector corresponding to each first compressed key vector to be unloaded are unloaded from the first storage device to the second storage device, wherein the storage speed corresponding to the second storage device is lower than that of the first storage device.
[0083] The language model reasoning optimization method provided in the embodiment of the present application stores the target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector to the first storage device, thereby ensuring that the target initial key vector, each initial value vector and each compressed key vector corresponding to the target compressed key vector can be quickly obtained from the first storage device, thereby improving the calculation speed. If the first current storage occupancy rate corresponding to the first storage device is greater than the first preset storage occupancy rate threshold, the first current compressed query vector is obtained; the first relative score between the first current compressed query vector and each compressed key vector is calculated; according to each first relative score, the first compressed key vector to be unloaded whose first relative score is less than the first preset threshold is determined from each compressed key vector; each first compressed key vector to be unloaded and the initial value vector corresponding to each first compressed key vector to be unloaded are unloaded from the first storage device to the second storage device, thereby ensuring that the first storage device can operate normally and avoiding affecting the normal operation of the first storage device due to the large amount of content stored in the first storage device. In addition, the first compressed key vector to be unloaded whose first relative score is less than the first preset threshold can ensure that the compressed key vector whose first relative score is greater than the first preset threshold is stored in the first storage device, ensuring that the compressed key vector whose first relative score is greater than the first preset threshold can be quickly calculated, thereby ensuring calculation efficiency.
[0084] In an optional embodiment, the method further includes:
[0085] If the second current storage occupancy rate corresponding to the second storage device is greater than the second preset storage occupancy rate threshold, obtaining the second current compressed query vector again;
[0086] Calculating a second relative score between the second current compressed query vector and each first to-be-offloaded compressed key vector;
[0087] According to each second relative score, determining, from each first compressed key vector to be uninstalled, a second compressed key vector to be uninstalled whose second relative score is less than a second preset threshold;
[0088] Each second compressed key vector to be unloaded and the initial value vector corresponding to each second compressed key vector to be unloaded are unloaded from the second storage device to the third storage device, wherein the storage speed corresponding to the third storage device is lower than that of the second storage device.
[0089] The language model reasoning optimization method provided by the embodiment of the present application is, if the second current storage occupancy rate corresponding to the second storage device is greater than the second preset storage occupancy rate threshold, then the second current compressed query vector is obtained again; the second relative score between the second current compressed query vector and each first compressed key vector to be unloaded is calculated, thereby ensuring the accuracy of the calculated second relative score. According to each second relative score, the second compressed key vector to be unloaded whose second relative score is less than the second preset threshold is determined from each first compressed key vector to be unloaded; each second compressed key vector to be unloaded and the initial value vector corresponding to each second compressed key vector to be unloaded are unloaded from the second storage device to the third storage device, thereby avoiding affecting the normal operation of the second storage device due to the large amount of content stored in the second storage device, and further ensuring the computing efficiency.
[0090] In a second aspect, the present invention provides a language model reasoning optimization device, the device comprising:
[0091] An acquisition module, used to acquire at least one information unit to be analyzed corresponding to the content to be analyzed, and input each of the information units to be analyzed into a preset language analysis model;
[0092] A filling module, used to pre-fill each of the information units to be analyzed based on the preset language analysis model, and obtain an initial query vector, an initial key vector and an initial value vector corresponding to each of the information units to be analyzed;
[0093] A compression module, used for compressing each of the initial query vectors and each of the initial key vectors to obtain each compressed query vector and each compressed key vector;
[0094] a determination module, configured to determine at least one target compressed key vector from the compressed key vectors according to the relationship between each of the compressed query vectors and each of the compressed key vectors;
[0095] A storage module, used for storing the target initial key vector corresponding to each of the target compressed key vectors, each of the initial value vectors and each of the compressed key vectors;
[0096] An output module is used to output a target analysis result corresponding to the content to be analyzed based on each of the compressed query vectors, each of the target initial key vectors and each of the target initial value vectors.
[0097] The language model reasoning optimization device provided in the embodiment of the present application obtains at least one information unit to be analyzed corresponding to the content to be analyzed, and inputs each information unit to be analyzed into a preset language analysis model; based on the preset language analysis model, each information unit to be analyzed is pre-filled to obtain the initial query vector, initial key vector and initial value vector corresponding to each information unit to be analyzed, thereby ensuring the accuracy of the obtained initial query vector, initial key vector and initial value vector. Each initial query vector and each initial key vector are compressed to obtain each compressed query vector and each compressed key vector, thereby ensuring the accuracy of each compressed query vector and each compressed key vector, so that each compressed query vector and each compressed key vector after compression occupies less memory. According to the relationship between each compressed query vector and each compressed key vector, at least one target compressed key vector is determined from the compressed key vector, thereby ensuring the accuracy of at least one determined target compressed key vector. The target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector are stored, thereby reducing the memory usage compared to storing the initial key vector and initial value vector before compression. The target analysis result corresponding to the content to be analyzed is output based on each compressed query vector, each target initial key vector and each target initial value vector, rather than outputting the target analysis result corresponding to the content to be analyzed based on the initial key vector and the initial value vector, thereby reducing the amount of calculation. Therefore, the above device not only reduces the memory usage, but also reduces the amount of calculation, thereby improving the efficiency of analyzing the content to be analyzed and outputting the target analysis result.
[0098] In a third aspect, the present invention provides an electronic device comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the language model reasoning optimization method of the first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0099] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the language model reasoning optimization method of the first aspect or any corresponding embodiment thereof.
[0100] In a fifth aspect, the present invention provides a computer program product, comprising computer instructions, which are used to enable a computer to execute the language model reasoning optimization method of the above-mentioned first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0101] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0102] Figure 1 is a schematic diagram of the results of each block in the transformer according to an embodiment of the present invention;
[0103] Figure 2 is a flowchart of a first language model reasoning optimization method according to an embodiment of the present invention;
[0104] Figure 3 is a schematic diagram of MHA in a decoding process according to an embodiment of the present invention;
[0105] Figure 4 is a schematic diagram of FFN during decoding according to an embodiment of the present invention;
[0106] Figure 5 is a flow chart of a second language model reasoning optimization method according to an embodiment of the present invention;
[0107] Figure 6 is a schematic diagram of encoding and decoding a training query vector according to an embodiment of the present invention;
[0108] Figure 7 is a flowchart of a third language model reasoning optimization method according to an embodiment of the present invention;
[0109] Figure 8 is a schematic diagram of determining a target compression key vector according to an embodiment of the present invention;
[0110] Fig. 9 is a flowchart of a fourth language model reasoning optimization method according to an embodiment of the present invention;
[0111] Fig.10 is a structural block diagram of a language model reasoning optimization device according to an embodiment of the present invention;
[0112] Fig.11 It is a schematic diagram of the hardware structure of the electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0113] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are 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 those skilled in the art without creative work are within the scope of protection of the present invention.
[0114] The Large Language Model (LLM) is a hot topic in artificial intelligence technology today. LLM is a deep learning model trained on massive text data. It has the ability to understand, generate and process natural language, and can play a role in various natural language processing tasks, such as text summarization, question-answering systems, machine translation, etc. The basic principle of the large language model is to use deep learning technology and multi-layer neural networks to model the statistical laws and potential semantic information of language. During the training process, the model will learn and abstract a large amount of text data to generate logical and coherent language output.
[0115] The current LLM refers to a large model with more than 10 billion or even 100 billion parameters and mainly based on the transformer-decoder structure. The GPT4 / Sora that we are familiar with recently belongs to the category of LLM. The transformer-decoder model structure is composed of multiple transformer blocks. The structure of each block is shown below. Figure 1 . Figure 1 A schematic diagram of a transformer structure provided by an embodiment of the present invention is shown in FIG. Figure 1 As shown in the figure, in the reasoning of the LLM of the transformer decoder structure, the text information is input into the encoder structure, and finally the embedding vector of each token (i.e., the information unit to be analyzed) is obtained. The text information is the initial embedding vector of a series of tokens. The encoder structure contains multiple layers of transformer blocks. Each transformer block contains a multi-head attention mechanism (Multi-Head Attention, MHA) and a feed-forward neural network (Feed Forward Network, FFN). The embedding vector obtained by the encoder structure will be passed to the decoder structure. The goal of the decoder is to generate the next token one by one based on the input embedding vector and the initial token. The decoder structure contains multiple layers of transformer blocks. Each transformer block contains a masked multi-head attention and feed-forward neural network and a cross-attention mechanism to integrate the embedding vector information. Specifically, as shown in the figure, Figure 1 As shown in the figure, the user input is embedded in the input, combined with the position encoding, and then passes through the masked multi-head self-attention, residual connection and layer normalization before reaching the feedforward layer, and then undergoes residual connection and layer normalization, and then passes through the linear layer and normalized activation layer respectively, and finally outputs the probability. Among them, in the masked multi-head self-attention, the value, key, and query are all passed through the linear layer, and then processed by the matrix multiplication-based attention mechanism, and then the splicing operation is performed to reach the linear layer. In the processing of the matrix multiplication-based attention mechanism, the key and query are input to the linear layer, and then processed by the scaling function, mask, and normalized activation, and then the value and the result after the normalized activation processing are matrix multiplied.
[0116] The calculation process of LLM reasoning is mainly to repeat the sub-unit operations in the block multiple times. For the convenience of description, some parameters of the transformer block layer are explained as follows: the hidden-size dimension is h, the number of attention heads is a, the sequence length is s, the number of transformer layers is l, the vocabulary length is V, and the batch size used for reasoning is b.
[0117] LLM reasoning consists of two steps: 1. Prefilling; 2. Decoding. Prefilling is the process of providing the user's input as a prompt (initial information) to the model. This prompt can be regarded as the initial information of the model, which is used to guide subsequent generation. Since the prefilling stage can be processed in parallel, it means that multiple inputs can be processed at the same time, thereby improving the efficiency of reasoning. Through parallel computing, the prefilling operation can be completed faster and the waiting time can be reduced.
[0118] Decoding is the process of generating a sequence of information units to be analyzed one by one. In this stage, the model gradually generates the next information unit to be analyzed based on the input prompt and historical data. Since the generation of each information unit to be analyzed depends on the previous information unit s to be analyzed, the decoding process usually needs to be calculated one by one, which limits the parallel execution capability on a single sequence. When only a single sample is inferred, the computational density of the decoding stage is low. Each parameter is only used after two floating-point calculations after being moved into the computing core, and then unloaded to the memory. This means that after each calculation, a large amount of computing resources are idle and not fully utilized, which affects the efficiency and speed of inference. In order to improve the efficiency of inference, you can consider using parallel computing technology. For example, by processing multiple samples in parallel or taking advantage of the parallelism of the model, the computing density can be improved to a certain extent. In addition, hardware acceleration can also be an option to optimize the computing process. Using dedicated hardware devices, such as GPU, TPU, etc., you can accelerate the calculation of the model and improve the inference speed. In addition, optimizing the model can also help improve the efficiency of inference. This includes using more efficient model architectures, compressing model parameters, and reducing the amount of calculation. Finally, reasonable adjustment of inference parameters and algorithms, such as batch size, beam size, etc., may also affect inference efficiency.
[0119] During the decoding process of LLM reasoning, the calculation amount of the current information unit to be analyzed is used as an example to illustrate: in MHA: the calculation amount of calculating QKV is 6×1×h2, where Q is the query vector, K is the key vector, and V is the value vector; the calculation amount of the transpose of Q multiplied by K is related to the current sequence length s, which is 2×1×s×h; the calculation amount of the weighted score obtained in the previous step and V is also related to the current sequence length s, which is 2×1×s×h; the calculation amount of linear mapping is 2×1×h2. In MLP: the calculation amount of calculating the current information unit to be analyzed is 16×1×h2. The calculation amount of the current information unit to be analyzed in each block is 24×1× h2+4×1×s×h; the total LLM calculation amount is l×(24×1× h2+4×1×s×h). In the transformer model, the total number of parameters is approximately l×(12+13h)+V×h. Ignoring the first-order term of h, the amount of computation required for forward reasoning of each information unit to be analyzed is approximately twice the number of parameters.
[0120] In the decoding process of the above LLM reasoning, we made a very important assumption when evaluating the computational complexity, that is, KV is cached (KV cache). The size of KV is 1×s×h in a sequence of length s, and the size of Q is 1×1×h. Each time we calculate the attention mechanism score matrix and the dot product with V, we need the value of KV in the s-1 sequence, but in the above calculation, we only calculated the KV computational complexity of the current information unit position to be analyzed. If we do not cache KV, we need to recalculate it every time we make inferences. If the total length of the sequence is s, the increased computational complexity relative to the KV cache solution is about 4×1×h2×(1+s-1)×(s-1) / 2=2× h2× s2 (1-1 / s). Therefore, the memory usage analysis for LLM reasoning is as follows: Assuming that the length of the input sequence is s1 and the length of the output sequence is n1, the KV cache peak memory usage is 1×(s1+n1)h×1×2×2=4×1×l×h×(s1+n1), where the first 2 represents the K / V cache and the second 2 represents that float16 occupies 2 bits. The number of LLM model parameters is approximately l×(12 +13h) +V×h. When the sequence length exceeds 100M, the KV cache will require more memory. For example, for a 13B model with h=5120 and l=40, the maximum KV cache occupies about 82TB. 2. The amount of computation and device memory usage during the self-attention mechanism calculation will increase exponentially. The aforementioned process mentioned that the amount of computation of Q×KT is related to the current sequence length s as 2×s×s×h, and the amount of computation of the score and V weighted obtained in the previous step is also related to the current sequence length s as 2×s×s×h. When s=100M, the amount of computation and intermediate storage usage will reach trillions.
[0121] Therefore, extending the sequence length of LLM will cause the following problems to be solved: 1. The increase in KV cache will exceed the memory of a single device or even a single system.
[0122] Based on this, the embodiment of the present application provides a method for optimizing language model reasoning. It should be noted that the execution subject of the method for optimizing language model reasoning provided by the embodiment of the present application can be a device for optimizing language model reasoning. The device for optimizing language model reasoning can be implemented as part or all of an electronic device through software, hardware, or a combination of software and hardware. The electronic device can be a server or a terminal. The server in the embodiment of the present application can be a single server or a server cluster composed of multiple servers. The terminal in the embodiment of the present application can be a smart phone, a personal computer, a tablet computer, a wearable device, and other intelligent hardware devices such as an intelligent robot. In the following method embodiments, the execution subject is an electronic device as an example for explanation.
[0123] According to an embodiment of the present invention, an embodiment of a language model reasoning optimization method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0124] In this embodiment, a language model inference optimization method is provided, which can be used in the above electronic device. Figure 2 is a flow chart of a language model reasoning optimization method according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:
[0125] Step S101: obtaining at least one information unit to be analyzed corresponding to the content to be analyzed, and inputting each information unit to be analyzed into a preset language analysis model.
[0126] Optionally, the electronic device may receive the content to be analyzed input by the user, or may receive the content to be analyzed sent by other devices. The embodiment of the present application does not specifically limit the manner in which the electronic device obtains the content to be analyzed.
[0127] Wherein, the content to be analyzed may be text content to be analyzed, or may be voice content to be analyzed. Wherein, when the content to be analyzed is text content to be analyzed, the text content to be analyzed may be Chinese, English, French, or other languages, and may also be code string content. The embodiment of the present application does not specifically limit the content to be analyzed to be text content to be analyzed. When the content to be analyzed is voice content to be analyzed, the electronic device may first convert the voice content to be analyzed into text content to be analyzed, and then the content to be analyzed may be text content to be analyzed for splitting. The embodiment of the present application does not specifically limit the content to be analyzed.
[0128] After receiving the content to be analyzed, the electronic device can segment the content to be analyzed using an appropriate word segmentation algorithm or tool to obtain at least one word unit. Then, the electronic device further processes each word unit obtained by segmentation and converts it into a specific information unit to be analyzed.
[0129] Optionally, the electronic device may also receive at least one information unit to be analyzed corresponding to the content to be analyzed input by the user, or receive at least one information unit to be analyzed corresponding to the content to be analyzed sent by other devices. The embodiment of the present application does not specifically limit the manner in which the electronic device obtains at least one information unit to be analyzed corresponding to the content to be analyzed.
[0130] Then, the electronic device inputs at least one to-be-analyzed information unit corresponding to the to-be-analyzed content into a preset language analysis model.
[0131] Step S102: pre-fill each information unit to be analyzed based on a preset language analysis model to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed.
[0132] Specifically, the electronic device can encode each information unit to be analyzed based on a preset language analysis model to obtain an initial encoding vector corresponding to each information unit to be analyzed. Then, according to the position of each information unit to be analyzed in the content to be analyzed, a position encoding is added to the initial encoding vector corresponding to each information unit to be analyzed to obtain a new position encoding vector. The new position encoding vector has the same dimension as the initial encoding vector.
[0133] Then, the electronic device performs matrix multiplication operation on the position coding vector corresponding to each information unit to be analyzed and the corresponding weight matrix to obtain the initial query vector, initial key vector and initial value vector corresponding to each information unit to be analyzed.
[0134] Exemplary: Assume that the input sequence corresponding to the content to be analyzed is x = [x1, x2, x3, ...xn], where each xi is a position coding vector of the information unit to be analyzed. Perform matrix multiplication operations on the position coding vector of the input sequence and three different weight matrices Wq, Wk and Wv respectively to obtain Q, K and V vectors. Among them: Q = x × Wq; K = x × Wk; V = x × Wv, where × represents matrix multiplication, Q is the initial query vector, K is the initial key vector, V is the initial value vector, Wq is the weight matrix corresponding to the initial query vector, Wk is the weight matrix corresponding to the initial key vector; Wv is the weight matrix corresponding to the initial value vector.
[0135] Step S103: compress each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector.
[0136] Specifically, the electronic device may compress each initial query vector and each initial key vector using a preset compression method to obtain each compressed query vector and each compressed key vector.
[0137] Among them, the preset compression method may include a preset quantization method, a preset dimensionality reduction method, a Huffman coding method and the like. The implementation of this application does not specifically limit the preset compression method.
[0138] Among them, the preset quantization method can be to convert the numerical values in each initial query vector and each initial key vector into a finite number of discrete values; the preset dimensionality reduction method can be to reduce the dimension of each initial query vector and each initial key vector through linear or nonlinear transformation; the Huffman coding method can be to encode the elements in each initial query vector and each initial key vector so that frequently appearing elements use shorter codes.
[0139] This step will be described in detail below.
[0140] Step S104: determining at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0141] Specifically, for each compressed query vector, the electronic device may calculate the relationship between the compressed query vector and each compressed key vector, and then determine at least one target compressed key vector from the compressed key vectors according to the relationship between the compressed query vector and each compressed key vector.
[0142] This step will be described in detail below.
[0143] Step S105, storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors.
[0144] Specifically, in order to facilitate subsequent calculations, the electronic device may store the compressed key vector and the initial value vector as well as the target initial key vector corresponding to each target compressed key vector in a preset storage device.
[0145] The preset storage device may be a GPU or a CPU, and the embodiment of the present application does not specifically limit the preset storage device.
[0146] Step S106: outputting a target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector and each target initial value vector.
[0147] Specifically, the electronic device can encode each information unit to be analyzed based on a preset language analysis model to obtain an initial coding vector corresponding to each information unit to be analyzed. Then, according to the position of each information unit to be analyzed in the content to be analyzed, a position coding is added to the initial coding vector corresponding to each information unit to be analyzed to obtain a new position coding vector. Among them, the new position coding vector has the same dimension as the initial coding vector. Then, the electronic device performs matrix multiplication operations on the position coding vector corresponding to each information unit to be analyzed with the corresponding weight matrix to obtain the initial query vector, initial key vector and initial value vector corresponding to each information unit to be analyzed.
[0148] For example, assuming that the number of information units to be analyzed is s, they are sent to the input layer of the preset language analysis model. The input layer converts the s information units to be analyzed into an embedded representation with a dimension of s×h, where h represents the vector dimension of an information unit to be analyzed. (2) Add position encoding information (sequence position) to each information unit to be analyzed to obtain a new embedded representation with unchanged dimension. (3) The embedded representation corresponding to each information unit to be analyzed will be converted into three QKV vectors.
[0149] Then, for each information unit to be analyzed, after the attention mechanism of the first transformer block, the number of attention mechanism heads is a. When processing the compressed query vector, target key vector and target value vector corresponding to the information unit to be analyzed, each attention mechanism will process the vector of (h / a) dimension of the information unit to be analyzed at the current position and the information unit to be analyzed at the historical position. The processing process is shown in the following formula 1. The final vector dimension is s×(h / a). The vectors obtained by all attention mechanism heads are concatenated and linearly mapped to obtain the output result of the attention mechanism module. The output dimension is s×h, see formula 2, where head is the output of the attention mechanism head.
[0150] (1)
[0151] (2)
[0152] in, To compress the query vector, is the target key vector, is the target value vector, is the transpose of the target key vector, is the sequence number corresponding to the information unit to be analyzed, is the weight.
[0153] Note that when performing softmax here, The result is masked to prevent the information unit to be analyzed at the current position from seeing the information unit to be analyzed at the future position.
[0154] Then, after the layer normalization and residual connection layer of the first transform block, a new output is obtained, and the dimension is still s×h. Among them, the layer normalization layer makes the input distribution of each neuron more stable by normalizing the mean and variance of each sample independently within the layer. The residual connection layer forms a "short-circuit connection" by adding the input directly to the transformed output, which facilitates the direct transmission of information and prevents the gradient from gradually disappearing in the deep network.
[0155] Then, after passing through the FFN layer of the first transformer block, the FFN layer first uses an MLP to transform the h dimension of each token to 4×h, and then uses another MLP to transform 4h back to h, and the final output dimension is still s×h. After passing through the L-layer transformer block layer by layer, an s×h output is obtained; and the first prediction information unit is predicted. Note that a 2(KV)×s×h cache vector will be generated in each layer of the block to avoid recalculating these KV values in the decoding stage of inference. Then, based on the output of each prediction information unit, the target analysis result is generated.
[0156] The computational complexity and computational flow of the decoding process of LLM reasoning (MHA and FFN) can be found in Figure 3 and Figure 4 . Figure 3 A schematic diagram of MHA in a decoding process provided by an embodiment of the present invention. Figure 3 In the above equation, d, l, and h all represent dimensions. Figure 3 As shown, the decoder input (d×1) is respectively operated with the query weight matrix (d×d), the key weight matrix (d×d) and the value weight matrix (d×d) to obtain the query matrix (d / h×1), the key matrix (d / h×1) and the value matrix (d / h×1). After combining the cache key tag value, feature joint processing, transposition processing, activation function processing, and feature joint processing, it is dispersed on a attention heads and combined with the output weight matrix for operation. After residual and normalization processing by the normalization layer, the attention model is output. In MHA, the amount of calculation of QKV of the current token is 6×1×h2, the amount of calculation of Q×KT is related to the current sequence length s as 2×1×s×h, the amount of calculation of the score obtained in the above step and the weighted V is also related to the current sequence length s as 2×1×s×h, and the amount of calculation of linear mapping is 2×1×h2.
[0157] Figure 4 A schematic diagram of FFN in a decoding process provided by an embodiment of the present invention. Figure 4 In , d and d FFN both represent dimensions. Figure 4As shown in the figure, the output of the attention model (d×1) is operated with the first weight matrix (dFFN×d) to obtain the matrix (d FFN×1) which is processed by the Gaussian error linear unit. The matrix (d FFN×1) obtained after the processing is operated with the second weight matrix (d×dFFN) to obtain the matrix (d×1). After that, the matrix is subjected to residual and normalization processing through the normalization layer, and then the encoder output is performed. The amount of calculation for calculating the current token in the MLP is 16×1×h2. Then the amount of calculation for the current token in each block is 24×1×h2 +4×1×s×h, and the total amount of LLM calculation is l×(24×1×h2 +4×1×s×h). The total number of parameters of the transformer model is about l×(12 h 2 +13h)+V×h. Ignoring the first-order term of h, the amount of calculation for forward reasoning of each token is about twice the parameter.
[0158] The language model inference optimization method provided in this embodiment obtains at least one information unit to be analyzed corresponding to the content to be analyzed, and inputs each information unit to be analyzed into a preset language analysis model; based on the preset language analysis model, each information unit to be analyzed is pre-filled to obtain the initial query vector, initial key vector and initial value vector corresponding to each information unit to be analyzed, thereby ensuring the accuracy of the obtained initial query vector, initial key vector and initial value vector. Each initial query vector and each initial key vector are compressed to obtain each compressed query vector and each compressed key vector, thereby ensuring the accuracy of each compressed query vector and each compressed key vector, so that each compressed query vector and each compressed key vector after compression occupies less memory. According to the relationship between each compressed query vector and each compressed key vector, at least one target compressed key vector is determined from the compressed key vector, thereby ensuring the accuracy of the determined at least one target compressed key vector. The target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector are stored, thereby reducing the memory usage compared to storing the initial key vector and initial value vector before compression. The target analysis result corresponding to the content to be analyzed is output based on each compressed query vector, each target initial key vector and each target initial value vector, rather than outputting the target analysis result corresponding to the content to be analyzed based on the initial key vector and the initial value vector, thereby reducing the amount of calculation. Therefore, the above method not only reduces the memory usage, but also reduces the amount of calculation, thereby improving the efficiency of analyzing the content to be analyzed and outputting the target analysis result.
[0159] In this embodiment, a language model inference optimization method is provided, which can be used in the above electronic device. Figure 5 is a flow chart of a language model reasoning optimization method according to an embodiment of the present invention. Figure 5 As shown, the process includes the following steps:
[0160] Step S201: obtaining at least one information unit to be analyzed corresponding to the content to be analyzed, and inputting each information unit to be analyzed into a preset language analysis model.
[0161] For more information about this step, see Figure 2 The introduction of step S101 is not repeated here.
[0162] Step S202: pre-fill each information unit to be analyzed based on a preset language analysis model to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed.
[0163] For more information about this step, see Figure 2 The introduction of step S102 is not repeated here.
[0164] Step S203: compress each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector.
[0165] Specifically, the above step S203 may include the following steps:
[0166] Step S2031: Input each initial query vector into a preset query vector compression model, and compress each initial query vector based on a first target encoder in the preset query vector compression model to generate each compressed query vector.
[0167] Specifically, the electronic device may input each generated initial query vector into a preset query vector compression model, and compress each initial query vector based on a first target encoder in the preset query vector compression model to generate each compressed query vector.
[0168] Step S2032: Input each initial key vector into a preset key vector compression model, and compress each initial key vector based on a second target encoder in the preset key vector compression model to generate each compressed key vector.
[0169] Specifically, the electronic device may input each generated initial key vector into a preset key vector compression model, compress each initial key vector based on a second target encoder in the preset key vector compression model, and generate each compressed key vector.
[0170] In an optional implementation manner of the present application, the training process of the preset query vector compression model includes:
[0171] Step a1, obtaining multiple training query vectors.
[0172] Specifically, the electronic device may receive multiple training query vectors input by a user, may also receive multiple training query vectors sent by other devices, and may also search for multiple training query vectors in a storage space.
[0173] The embodiment of the present application does not specifically limit the manner in which the electronic device obtains multiple training query vectors.
[0174] Step a2: input each training query vector into the initial query vector compression network, encode and decode each training query vector, and obtain a virtual query vector corresponding to each training query vector.
[0175] In an optional implementation of the present application, the initial query vector compression network includes a first initial encoder and a first initial decoder, and the above step a2 may include the following steps:
[0176] Step a21, input each training query vector into the first initial encoder in the initial query vector compression network, the first initial encoder encodes each training query vector to generate each initial encoded query vector.
[0177] Among them, the dimension of the initial encoding query vector is smaller than the dimension of the training query vector.
[0178] Specifically, the electronic device may input each training query vector into a first initial encoder in the initial query vector compression network, and the first initial encoder encodes each training query vector to generate each initial encoded query vector.
[0179] Step a22: input each initial coded query vector into a first initial decoder in the initial query vector compression network; the first initial decoder decodes each initial coded query vector to generate each virtual query vector.
[0180] The dimension of the virtual query vector is consistent with that of the training query vector.
[0181] Specifically, the electronic device inputs each initial coded query vector into a first initial decoder in the initial query vector compression network, and the first initial decoder decodes and reconstructs each initial coded query vector to generate each virtual query vector.
[0182] For example, Figure 6 As shown, the electronic device can input each training query vector into the first initial encoder in the initial query vector compression network, and the first initial encoder encodes each training query vector to generate each initial encoded query vector. Each initial encoded query vector is input into the first initial decoder in the initial query vector compression network, and the first initial decoder decodes each initial encoded query vector to generate each virtual query vector.
[0183] Step a3: training the initial query vector compression network based on each training query vector and each virtual query vector to generate a preset query vector compression model.
[0184] In an optional implementation manner of the present application, the above step a2 may include the following steps:
[0185] Step a31, based on a first preset loss function, calculating a first loss value between each training query vector and each virtual query vector.
[0186] Among them, the first preset loss function can be a mean square error (MSE) loss function or a cross entropy loss function. The embodiment of the present application does not specifically limit the first preset loss function.
[0187] The electronic device calculates a first loss value between each training query vector and each virtual query vector based on a first preset loss function.
[0188] Step a32: Based on each first loss value, the initial query vector compression network is trained to generate a preset query vector compression model.
[0189] Specifically, the electronic device calculates a first loss value between each training query vector and each virtual query vector based on a first preset loss function, and then trains a first initial encoder and a first initial decoder based on the first loss value to obtain a first target encoder and a first target decoder, thereby generating a preset query vector compression model.
[0190] In an optional implementation manner of the present application, the training process of the preset key vector compression model includes:
[0191] Step a4, obtaining multiple training key vectors.
[0192] Specifically, the electronic device may receive multiple training key vectors input by a user, may receive multiple training key vectors sent by other devices, and may search for multiple training key vectors in a storage space.
[0193] The embodiment of the present application does not specifically limit the manner in which the electronic device obtains multiple training key vectors.
[0194] Step a5, input each training key vector into the initial key vector compression network, encode and decode each training key vector, and obtain a virtual key vector corresponding to each training key vector.
[0195] In an optional implementation of the present application, the initial key vector compression network includes a second initial encoder and a second initial decoder, and the above step a5 may include the following steps:
[0196] Step a51, input each training key vector to the second initial encoder in the initial key vector compression network, the second initial encoder encodes each training key vector to generate each initial encoded key vector.
[0197] Among them, the dimension of the initial encoding key vector is smaller than the dimension of the training key vector.
[0198] Specifically, the electronic device may input each training key vector into a second initial encoder in the initial key vector compression network, and the second initial encoder encodes each training key vector to generate each initial encoded key vector.
[0199] Step a52, input each initial encoded key vector to the second initial decoder in the initial key vector compression network, and the second initial decoder decodes each initial encoded key vector to generate each virtual key vector.
[0200] Among them, the dimension of the virtual key vector is consistent with the dimension of the training key vector.
[0201] Specifically, the electronic device inputs each initial encoded key vector into the first initial decoder in the initial key vector compression network, and the first initial decoder decodes and reconstructs each initial encoded key vector to generate each virtual key vector.
[0202] Step a6: training the initial key vector compression network based on each training key vector and each virtual key vector to generate a preset key vector compression model.
[0203] In an optional implementation manner of the present application, the above step a6 may include the following steps:
[0204] Step a61, based on a second preset loss function, calculate a second loss value between each training key vector and each virtual key vector.
[0205] Among them, the second preset loss function can be a mean square error (MSE) loss function or a cross entropy loss function. The embodiment of the present application does not specifically limit the second preset loss function.
[0206] Specifically, the electronic device calculates a second loss value between each training key vector and each virtual key vector based on a second preset loss function, and then trains a second initial encoder and a second initial decoder based on the second loss value to obtain a second target encoder and a second target decoder, thereby generating a preset key vector compression model.
[0207] Step a62: Based on each second loss value, the initial key vector compression network is trained to generate a preset key vector compression model.
[0208] Specifically, the electronic device can train the initial key vector compression network based on each second loss value to generate a preset key vector compression model.
[0209] In an optional implementation of the present application, the training key vector corresponds to the trained query vector, and the above method may further include the following steps:
[0210] Step a7: training the initial query vector compression network based on each training query vector and each virtual query vector to generate a spare query vector compression model.
[0211] Specifically, the electronic device may train the initial query vector compression network based on the first loss values corresponding to each training query vector and each virtual query vector to generate a spare query vector compression model.
[0212] Step a8: training the initial key vector compression network based on each training key vector and each virtual key vector to generate a spare key vector compression model.
[0213] Specifically, the electronic device can train the initial key vector compression network based on the second loss values corresponding to each training key vector and each virtual key vector to generate a spare key vector compression model.
[0214] Step a9: Perform statistical calculation on each training query vector and the corresponding training key vector to obtain a first result.
[0215] Specifically, the electronic device may perform a dot product calculation on each training query vector and the corresponding training key vector to obtain a first result.
[0216] Step a10, performing statistical calculations on each initial coding query vector and each corresponding initial coding key vector to obtain a second result.
[0217] Specifically, the electronic device may perform a dot product calculation on each initial coding query vector and each corresponding initial coding key vector to obtain a second result.
[0218] Step a11: Based on a third preset loss function between each first result and the corresponding second result, the spare query vector compression model is trained to generate a preset query vector compression model, and the spare key vector compression model is trained to generate a preset key vector compression model.
[0219] Specifically, the electronic device can calculate the third loss value between each first result and the corresponding second result based on the third preset loss function, train the spare query vector compression model to generate a preset query vector compression model, and train the spare key vector compression model to generate a preset key vector compression model.
[0220] Step S204: determining at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0221] For more information about this step, see Figure 2 The introduction of step S104 is not repeated here.
[0222] Step S205, storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors.
[0223] For more information about this step, see Figure 2 The introduction of step S105 is not repeated here.
[0224] Step S206: outputting a target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector and each target initial value vector.
[0225] For more information about this step, see Figure 2 The introduction of step S106 is not repeated here.
[0226] The language model reasoning optimization method provided in the embodiment of the present application inputs each initial query vector into a preset query vector compression model, compresses each initial query vector based on the first target encoder in the preset query vector compression model, generates each compressed query vector, ensures the accuracy of each generated compressed query vector, so that the compressed query vector occupies a small amount of memory. Input each initial key vector into a preset key vector compression model, compress each initial key vector based on the second target encoder in the preset key vector compression model, and generate each compressed key vector. The accuracy of each generated compressed key vector is ensured, so that the compressed key vector occupies a small amount of memory.
[0227] In addition, multiple training query vectors are obtained; each training query vector is input into the first initial encoder in the initial query vector compression network, and the first initial encoder encodes each training query vector to generate each initial encoded query vector, thereby ensuring the accuracy of each generated initial encoded query vector and reducing the dimension of the training query vector. Each initial encoded query vector is input into the first initial decoder in the initial query vector compression network, and the first initial decoder decodes each initial encoded query vector to generate each virtual query vector, thereby ensuring the accuracy of each generated virtual query vector. Therefore, whether each encoded initial encoded query vector is accurate can be judged according to each virtual query vector.
[0228] Based on the first preset loss function, a first loss value between each training query vector and each virtual query vector is calculated, thereby ensuring the accuracy of the calculated first loss value. Based on each first loss value, the initial query vector compression network is trained to generate a spare query vector compression model, thereby ensuring the accuracy of the generated spare query vector compression model.
[0229] Acquire multiple training key vectors; input each training key vector into the second initial encoder in the initial key vector compression network, the second initial encoder encodes each training key vector to generate each initial encoded key vector; the accuracy of each generated initial encoded key vector is ensured, and the dimension of the training key vector is reduced. Input each initial encoded key vector into the second initial decoder in the initial key vector compression network, the second initial decoder decodes each initial encoded key vector to generate each virtual key vector, the accuracy of each generated virtual key vector is ensured, and the accuracy of the initial encoded key vector obtained after compressing the training key vector can be detected based on the virtual key vector.
[0230] Based on the second preset loss function, the second loss value between each training key vector and each virtual key vector is calculated to ensure the accuracy of the obtained second loss value. Based on each second loss value, the initial key vector compression network is trained to generate a spare key vector compression model to ensure the accuracy of the generated spare key vector compression model.
[0231] Then, statistical calculations are performed on each training query vector and the corresponding training key vector to obtain a first result, thereby ensuring the accuracy of the generated first result. Statistical calculations are performed on each initial coded query vector and the corresponding initial coded key vector to obtain a second result, thereby ensuring the accuracy of the generated second result. Based on the third preset loss function between each first result and the corresponding second result, the spare query vector compression model is trained to generate a preset query vector compression model, and the spare key vector compression model is trained to generate a preset key vector compression model, thereby ensuring the accuracy of the generated preset query vector compression model and the preset key vector compression model, and further ensuring the accuracy of each compressed query vector and each compressed key vector obtained based on the preset query vector compression model and the preset key vector compression model, and the accuracy of the result obtained by performing statistical calculations based on each compressed query vector and each compressed key vector.
[0232] In this embodiment, a language model inference optimization method is provided, which can be used in the above electronic device. Figure 7 is a flow chart of a language model reasoning optimization method according to an embodiment of the present invention. Figure 7 As shown, the process includes the following steps:
[0233] Step S301: obtaining at least one information unit to be analyzed corresponding to the content to be analyzed, and inputting each information unit to be analyzed into a preset language analysis model.
[0234] For more information about this step, see Figure 5 The introduction of step S201 is not repeated here.
[0235] Step S302: pre-fill each information unit to be analyzed based on a preset language analysis model to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed.
[0236] For more information about this step, see Figure 5 The introduction of step S202 is not repeated here.
[0237] Step S303: compress each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector.
[0238] For more information about this step, see Figure 5 The introduction of step S203 is not repeated here.
[0239] Step S304: Determine at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0240] Specifically, the above step S304 may include the following steps:
[0241] Step S3041, obtaining a preset number of target compression key vectors corresponding to each compression query vector.
[0242] The preset number is used to represent the number of target compression key vectors associated with each compression query vector.
[0243] Specifically, the electronic device can receive a preset number of target compressed key vectors corresponding to each compressed query vector input by a user, and can also receive a preset number of target compressed key vectors corresponding to each compressed query vector sent by other devices; the electronic device can also determine the preset number of target compressed key vectors corresponding to each compressed query vector based on the number of information units to be analyzed.
[0244] Exemplarily, the electronic device may multiply the number of information units to be analyzed by a preset value to obtain a preset number of target compressed key vectors corresponding to each compressed query vector. The preset value may be 0.01, 0.02, or any other value less than 1. The embodiment of the present application does not specifically limit the preset number of target compressed key vectors corresponding to each compressed query vector.
[0245] Step S3042: Determine a preset number of target compressed key vectors from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0246] Specifically, the above step S3042 may include the following steps:
[0247] Step b1, calculate the attention score between each compressed query vector and each compressed key vector.
[0248] Optionally, for each compressed query vector, the electronic device may calculate the dot product of the compressed query vector and each compressed key vector, and then divide it by the sequence length to calculate an attention score between the compressed query vector and each compressed key vector.
[0249] Optionally, for each compressed query vector, the electronic device can obtain the position of the information unit to be analyzed corresponding to the compressed query vector in the content to be analyzed. Then, according to the position of the information unit to be analyzed corresponding to the compressed query vector in the content to be analyzed, the compressed key vector corresponding to the compressed query vector and the compressed key vectors preset in number before it are determined as candidate compressed key vectors. The preset number can be 7, 8, or other values. The embodiment of the present application does not specifically limit the preset number.
[0250] Exemplarily, assuming that the preset number is 7, and the position of the to-be-analyzed information unit corresponding to the current compressed query vector in the to-be-analyzed content is 10, the compressed key vectors corresponding to the to-be-analyzed information units 3-10 are candidate compressed key vectors.
[0251] The electronic device may calculate the attention score between each compressed query vector and each candidate compressed key vector according to the candidate compressed key vector corresponding to each compressed query vector.
[0252] Exemplarily, for each compressed query vector, the electronic device may calculate the dot product between the compressed query vector and each candidate compressed key vector, and then divide it by the sequence length to calculate an attention score between the compressed query vector and each candidate compressed key vector.
[0253] Step b2: Determine a preset number of compressed key vectors with top attention scores as target compressed key vectors.
[0254] Specifically, for each compressed query vector, the electronic device sorts the attention scores of each candidate compressed key vector corresponding to the compressed query vector from large to small, and then determines a preset number of candidate compressed key vectors with top attention scores as target compressed key vectors.
[0255] For example, Figure 8 , the row represents Q, the column represents K, and the place marked with 1 in the grid represents the K value that the Q at the current position can be associated with, that is, the target compression key vector corresponding to the Q at the current position.
[0256] Step S305, storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors.
[0257] For more information about this step, see Figure 5 The introduction of step S205 is not repeated here.
[0258] Step S306: output the target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector and each target initial value vector.
[0259] Specifically, the above step S306 may include the following steps:
[0260] Step S3061, obtain the initial number of attention mechanism heads in the preset language analysis model.
[0261] Specifically, the electronic device can receive the initial number of attention mechanism heads in the preset language analysis model input by the user, and can also receive the initial number of attention mechanism heads in the preset language analysis model sent by other devices.
[0262] The embodiment of the present application does not specifically limit the initial number of attention mechanism heads in the preset language analysis model.
[0263] Step S3062: For each attention mechanism head, obtain the position information of the target compressed key vector corresponding to the attention mechanism head in the compressed key vector.
[0264] Specifically, for each attention mechanism head, the electronic device can obtain the position information of the target compressed key vector corresponding to the attention mechanism head in the compressed key vector.
[0265] Step S3063, determine the target attention mechanism head from each attention mechanism head according to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector.
[0266] Specifically, the above step S3063 may include the following steps:
[0267] Step c1, based on the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector, calculate the similarity between the target compressed key vectors corresponding to each attention mechanism head.
[0268] Specifically, according to the position information of the target compressed key vector corresponding to each attention mechanism head in the compressed key vector, the similarity between the target compressed key vectors corresponding to each attention mechanism head is calculated.
[0269] For example, assume that the number of target compressed key vectors corresponding to each compressed query vector is 5, the position information of the target compressed key vector corresponding to one attention mechanism head in the compressed key vector is (3, 5, 7, 8, 10), and the position information of the target compressed key vector corresponding to one attention mechanism head in the compressed key vector is (2, 5, 8, 9, 10). Then the electronic device calculates the number of (3, 5, 7, 8, 10) and (3, 5, 7, 8, 10).
[0270] Step c2, determining the target attention mechanism head from each attention mechanism head according to the similarity between the target compressed key vectors corresponding to the two attention mechanism heads.
[0271] Specifically, the above step c2 may include the following steps:
[0272] Step c21, comparing the similarity between the target compressed key vectors corresponding to the pairwise attention mechanism heads with a preset similarity threshold.
[0273] Specifically, the electronic device compares the similarity between the target compressed key vectors corresponding to the pairwise attention mechanism heads with a preset similarity threshold.
[0274] Step c22, delete any one of the two attention mechanism heads whose similarity is greater than the preset similarity threshold.
[0275] Specifically, the electronic device deletes any one of the two attention mechanism heads whose similarity is greater than a preset similarity threshold.
[0276] Step c23, until the similarity between the target compressed key vectors corresponding to any two attention mechanism heads is less than the preset similarity threshold, the target attention mechanism head is obtained.
[0277] Specifically, until the similarity between the target compressed key vectors corresponding to any two attention mechanism heads is less than a preset similarity threshold, the target attention mechanism head is obtained.
[0278] Step S3064: For each compressed query vector, based on the target attention mechanism head, the compressed query vector, the target initial key vector corresponding to the compressed query vector, and the target initial value vector corresponding to the compressed query vector are processed to output a first output result.
[0279] Specifically, after obtaining each target attention mechanism head, for each compressed query vector, the electronic device can use the target attention mechanism head to process the compressed query vector to the target initial key vector, and the target initial value vector corresponding to the target initial key vector. The processing process can be seen in Formula 3, and then the processed result is normalized by the first trasformerblock layer and the residual connection layer to obtain the first output result.
[0280] Step S3065: Determine a second output result corresponding to the first output result based on the feedforward neural network layer.
[0281] Then, the first output result is processed by the feedforward neural network layer based on the transformer block, and then the second output result corresponding to the first output result is obtained.
[0282] Step S3066: Generate a target analysis result based on each second output result.
[0283] Specifically, the electronic device splices the second output results to generate a target analysis result.
[0284] The language model inference optimization method provided in this embodiment obtains a preset number of target compressed key vectors corresponding to each compressed query vector, calculates the attention score between each compressed query vector and each compressed key vector; determines the preset number of compressed key vectors with the highest attention score as the target compressed key vector, thereby ensuring the accuracy of the target analysis result corresponding to the output analysis content based on the target initial key vector corresponding to the target compressed key vector, and avoiding the discarding of relatively important target compressed key vectors, resulting in inaccurate output target analysis results. In addition, the above method outputs the target analysis result corresponding to the content to be analyzed based on each target initial key vector and each target initial value vector, rather than outputting the target analysis result based on all initial key vectors and initial value vectors, thereby reducing the amount of calculation.
[0285] Then, the initial number of attention heads corresponding to the preset language analysis model is obtained; for each attention head, the position information of the target compressed key vector corresponding to the attention head in the compressed key vector is obtained; according to the position information of the target compressed key vector corresponding to each attention head in the compressed key vector, the similarity between the target compressed key vectors corresponding to the two attention heads is calculated, thereby ensuring the accuracy of calculating the similarity between the target compressed key vectors corresponding to the two attention heads. The similarity between the target compressed key vectors corresponding to the two attention heads is compared with the preset similarity threshold; any one of the two attention heads whose similarity is greater than the preset similarity threshold is deleted until the similarity between the target compressed key vectors corresponding to any two attention heads is less than the preset similarity threshold, thereby obtaining the target attention head, thereby ensuring the accuracy of the obtained target attention head.
[0286] For each compressed query vector, the compressed query vector, the target initial key vector corresponding to the compressed query vector, and the target initial value vector corresponding to the compressed query vector are processed based on the target attention mechanism head, and the first output result is output, thereby ensuring the accuracy of the output first output result, reducing the number of target attention mechanism heads, and reducing the amount of calculation. Based on the feedforward neural network layer, the second output result corresponding to the first output result is determined, thereby ensuring the accuracy of the output second output result. Based on each second output result, a target analysis result is generated, thereby ensuring the accuracy of the generated target analysis result.
[0287] In this embodiment, a language model inference optimization method is provided, which can be used in the above electronic device. Fig. 9is a flow chart of a language model reasoning optimization method according to an embodiment of the present invention. Fig. 9 As shown, the process includes the following steps:
[0288] Step S401: obtaining at least one information unit to be analyzed corresponding to the content to be analyzed, and inputting each information unit to be analyzed into a preset language analysis model.
[0289] For more information about this step, see Figure 7 The introduction of step S301 is not repeated here.
[0290] Step S402: pre-fill each information unit to be analyzed based on a preset language analysis model to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed.
[0291] For more information about this step, see Figure 7 The introduction of step S302 is not repeated here.
[0292] Step S403: compress each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector.
[0293] For more information about this step, see Figure 7 The introduction of step S303 is not repeated here.
[0294] Step S404: Determine at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0295] For more information about this step, see Figure 7 The introduction of step S304 is not repeated here.
[0296] Step S405, storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors.
[0297] Specifically, the above step S405 may include the following steps:
[0298] Step S4051, storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors in the first storage device.
[0299] Specifically, in the process of analyzing each information unit to be analyzed based on the preset language analysis model, the electronic device stores the target initial key vector, each initial value vector and each compressed key vector corresponding to the generated target compressed key vector in the first storage device.
[0300] Step S4052: If the first current storage occupancy rate corresponding to the first storage device is greater than a first preset storage occupancy rate threshold, a first current compressed query vector is obtained.
[0301] Specifically, the electronic device may monitor the first current storage occupancy rate corresponding to the first storage device in real time, and compare the first current storage occupancy rate corresponding to the first storage device with the first preset storage occupancy rate threshold. If the first current storage occupancy rate corresponding to the first storage device is greater than the first preset storage occupancy rate threshold, the first current compressed query vector is obtained.
[0302] Step S4053: Calculate a first relative score between the first current compressed query vector and each compressed key vector.
[0303] Optionally, the electronic device may calculate a dot product of the first current compressed query vector and each compressed key vector as the first relative score.
[0304] Optionally, the electronic device may further calculate a cosine similarity between the first current compressed query vector and each compressed key vector as a first relative score.
[0305] Optionally, the electronic device may further calculate the inverse of the Euclidean distance between the first current compressed query vector and each compressed key vector as the first relative score.
[0306] Step S4054: Determine, from among the compressed key vectors, according to the first relative scores, a first compressed key vector to be uninstalled whose first relative score is less than a first preset threshold.
[0307] Specifically, the electronic device may compare each first relative score with a first preset threshold, and determine, from each compressed key vector, a first compressed key vector to be uninstalled whose first relative score is less than the first preset threshold.
[0308] Step S4055: Unload each first compressed key vector to be unloaded and the initial value vector corresponding to each first compressed key vector to be unloaded from the first storage device to the second storage device.
[0309] The storage speed corresponding to the second storage device is lower than that of the first storage device.
[0310] Specifically, the electronic device unloads each first compressed key vector to be unloaded and the initial value vector corresponding to each first compressed key vector to be unloaded from the first storage device to the second storage device.
[0311] Step S4056: If the second current storage occupancy rate corresponding to the second storage device is greater than the second preset storage occupancy rate threshold, a second current compressed query vector is obtained again.
[0312] Specifically, the electronic device may monitor the second current storage occupancy rate corresponding to the second storage device in real time, and compare the second current storage occupancy rate corresponding to the second storage device with the second preset storage occupancy rate threshold. If the second current storage occupancy rate corresponding to the second storage device is greater than the second preset storage occupancy rate threshold, the second current compressed query vector is obtained.
[0313] Step S4057: Calculate a second relative score between the second current compressed query vector and each first to-be-offloaded compressed key vector.
[0314] Optionally, the electronic device may calculate a dot product of the second current compressed query vector and each compressed key vector as the second relative score.
[0315] Optionally, the electronic device may further calculate a cosine similarity between the second current compressed query vector and each compressed key vector as a second relative score.
[0316] Optionally, the electronic device may further calculate the inverse of the Euclidean distance between the second current compressed query vector and each compressed key vector as the second relative score.
[0317] Step S4058: Determine, according to each second relative score, from each first compressed key vector to be uninstalled, a second compressed key vector to be uninstalled whose second relative score is less than a second preset threshold.
[0318] Step S4059: Unload each second compressed key vector to be unloaded and the initial value vector corresponding to each second compressed key vector to be unloaded from the second storage device to the third storage device.
[0319] The storage speed corresponding to the third storage device is lower than that of the second storage device.
[0320] Specifically, the electronic device compares each second relative score with the second preset threshold, and then determines, from each first compressed key vector to be uninstalled, a second compressed key vector to be uninstalled whose second relative score is less than the second preset threshold.
[0321] Step S406: output the target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector and each target initial value vector.
[0322] For more information about this step, see Figure 7 The introduction of step S306 is not repeated here.
[0323] The language model reasoning optimization method provided in this embodiment stores the target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector to the first storage device, thereby ensuring that the target initial key vector, each initial value vector and each compressed key vector corresponding to the target compressed key vector can be quickly obtained from the first storage device, thereby improving the calculation speed. If the first current storage occupancy rate corresponding to the first storage device is greater than the first preset storage occupancy rate threshold, the first current compressed query vector is obtained; the first relative score between the first current compressed query vector and each compressed key vector is calculated; according to each first relative score, the first compressed key vector to be unloaded whose first relative score is less than the first preset threshold is determined from each compressed key vector; each first compressed key vector to be unloaded and the initial value vector corresponding to each first compressed key vector to be unloaded are unloaded from the first storage device to the second storage device, thereby ensuring that the first storage device can operate normally and avoiding affecting the normal operation of the first storage device due to the large amount of content stored in the first storage device. In addition, the first compressed key vector to be unloaded whose first relative score is less than the first preset threshold can ensure that the compressed key vector whose first relative score is greater than the first preset threshold is stored in the first storage device, ensuring that the compressed key vector whose first relative score is greater than the first preset threshold can be quickly calculated, thereby ensuring calculation efficiency.
[0324] If the second current storage occupancy rate corresponding to the second storage device is greater than the second preset storage occupancy rate threshold, the second current compressed query vector is obtained again; the second relative score between the second current compressed query vector and each first compressed key vector to be unloaded is calculated, thereby ensuring the accuracy of the calculated second relative score. According to each second relative score, the second compressed key vector to be unloaded whose second relative score is less than the second preset threshold is determined from each first compressed key vector to be unloaded; each second compressed key vector to be unloaded and the initial value vector corresponding to each second compressed key vector to be unloaded are unloaded from the second storage device to the third storage device, thereby avoiding affecting the normal operation of the second storage device due to the large amount of content stored in the second storage device, and further ensuring the computing efficiency.
[0325] In this embodiment, a language model reasoning optimization device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware for a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0326] This embodiment provides a language model reasoning optimization device, such as Fig.10 As shown, including:
[0327] The acquisition module 501 is used to acquire at least one information unit to be analyzed corresponding to the content to be analyzed, and input each information unit to be analyzed into a preset language analysis model.
[0328] The filling module 502 is used to pre-fill each information unit to be analyzed based on a preset language analysis model to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each information unit to be analyzed.
[0329] The compression module 503 is used to compress each initial query vector and each initial key vector to obtain each compressed query vector and each compressed key vector.
[0330] The determination module 504 is used to determine at least one target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and each compressed key vector.
[0331] A storage module 505, used to store the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors;
[0332] The output module 506 is used to output the target analysis result corresponding to the content to be analyzed based on each compressed query vector, each target initial key vector and each target initial value vector.
[0333] The further functional description of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0334] The language model reasoning optimization device in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.
[0335] The embodiment of the present invention also provides an electronic device having the above Fig.10 The language model inference optimization device shown.
[0336] See also Fig.11 , Fig.11 is a schematic diagram of the structure of an electronic device provided by an optional embodiment of the present invention, such as Fig.11As shown, the electronic device includes: one or more processors 10, a memory 20, and an interface for connecting components, including a high-speed interface and a low-speed interface. The components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process instructions executed in the electronic device, including instructions stored in or on the memory to display graphical information of a GUI on an external input / output device (such as a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple electronic devices can be connected, and the device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Fig.11 A processor 10 is taken as an example.
[0337] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0338] The memory 20 stores instructions executable by at least one processor 10, so that at least one processor 10 executes the method shown in the above embodiment.
[0339] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the electronic device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0340] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0341] The electronic device further includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Fig.11The example of connecting through bus is taken in the following.
[0342] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the electronic device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator rod, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device can be a touch screen.
[0343] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0344] A part of the present invention may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present invention through the operation of the computer. Those skilled in the art should understand that the existence of the computer program instruction in a computer-readable medium includes, but is not limited to, a source file, an executable file, an installation package file, etc., and accordingly, the way in which the computer program instruction is executed by the computer includes, but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium may be any available computer-readable storage medium or communication medium accessible to the computer.
[0345] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention. Such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A language model reasoning optimization method, It is characterized in that The method comprises: Acquire at least one information unit to be analyzed corresponding to the content to be analyzed, and input each of the information units to be analyzed into a preset language analysis model; Based on the preset language analysis model, pre-filling each of the information units to be analyzed to obtain an initial query vector, an initial key vector and an initial value vector corresponding to each of the information units to be analyzed; Compressing each of the initial query vectors and each of the initial key vectors to obtain each compressed query vector and each compressed key vector; Determining at least one target compressed key vector from the compressed key vectors according to the relationship between each of the compressed query vectors and each of the compressed key vectors; Storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors; Outputting a target analysis result corresponding to the content to be analyzed based on each of the compressed query vectors, each of the target initial key vectors, and each of the target initial value vectors; The storing of the target initial key vectors, the initial value vectors, and the compressed key vectors corresponding to the target compressed key vectors includes: Storing the target initial key vectors, the initial value vectors and the compressed key vectors corresponding to the target compressed key vectors in a first storage device; If a first current storage occupancy rate corresponding to the first storage device is greater than a first preset storage occupancy rate threshold, obtaining a first current compressed query vector; Calculating a first relative score between the first current compressed query vector and each of the compressed key vectors; According to each of the first relative scores, determining, from each of the compressed key vectors, a first compressed key vector to be uninstalled whose first relative score is less than a first preset threshold; Each of the first compressed key vectors to be unloaded and the initial value vector corresponding to each of the first compressed key vectors to be unloaded are unloaded from the first storage device to a second storage device, wherein a storage speed corresponding to the second storage device is lower than that of the first storage device.
2. The method according to claim 1, It is characterized in that The compressing each of the initial query vectors and each of the initial key vectors to obtain each compressed query vector and each compressed key vector comprises: Inputting each of the initial query vectors into a preset query vector compression model, and compressing each of the initial query vectors based on a first target encoder in the preset query vector compression model to generate each of the compressed query vectors; Each of the initial key vectors is input into a preset key vector compression model, and based on a second target encoder in the preset key vector compression model, each of the initial key vectors is compressed to generate each of the compressed key vectors.
3. The method according to claim 2, It is characterized in that The training process of the preset query vector compression model includes: Obtain multiple training query vectors; Inputting each of the training query vectors into an initial query vector compression network, encoding and decoding each of the training query vectors, and obtaining a virtual query vector corresponding to each of the training query vectors; The initial query vector compression network is trained based on each of the training query vectors and each of the virtual query vectors to generate the preset query vector compression model.
4. The method according to claim 3, It is characterized in that The initial query vector compression network includes a first initial encoder and a first initial decoder. The training query vectors are input into the initial query vector compression network, and each training query vector is encoded and decoded to obtain a virtual query vector corresponding to each training query vector, including: Inputting each of the training query vectors into the first initial encoder in the initial query vector compression network, wherein the first initial encoder encodes each of the training query vectors to generate each initial encoded query vector; the dimension of the initial encoded query vector is smaller than the dimension of the training query vector; Each of the initial encoded query vectors is input into the first initial decoder in the initial query vector compression network, and the first initial decoder decodes each of the initial encoded query vectors to generate each of the virtual query vectors; the dimension of the virtual query vector is consistent with the dimension of the training query vector.
5. The method according to claim 3, It is characterized in that The training of the initial query vector compression network based on the training query vector and the virtual query vector to generate the preset query vector compression model includes: Based on a first preset loss function, calculating a first loss value between each of the training query vectors and each of the virtual query vectors; Based on each of the first loss values, the initial query vector compression network is trained to generate the preset query vector compression model.
6. The method according to claim 4, It is characterized in that The training process of the preset key vector compression model includes: Get multiple training key vectors; Inputting each of the training key vectors into an initial key vector compression network, encoding and decoding each of the training key vectors, and obtaining a virtual key vector corresponding to each of the training key vectors; The initial key vector compression network is trained based on each of the training key vectors and each of the virtual key vectors to generate the preset key vector compression model.
7. The method according to claim 6, It is characterized in that The initial key vector compression network includes a second initial encoder and a second initial decoder, and each of the training key vectors is input into the initial key vector compression network, and each of the training key vectors is encoded and decoded to obtain a virtual key vector corresponding to each of the training key vectors, including: Inputting each of the training key vectors into the second initial encoder in the initial key vector compression network, the second initial encoder encodes each of the training key vectors to generate each initial encoded key vector; the dimension of the initial encoded key vector is smaller than the dimension of the training key vector; Each of the initial encoded key vectors is input into the second initial decoder in the initial key vector compression network, and the second initial decoder decodes each of the initial encoded key vectors to generate each of the virtual key vectors; the dimension of the virtual key vector is consistent with the dimension of the training key vector.
8. The method according to claim 6, It is characterized in that The training of the initial key vector compression network based on each of the training key vectors and each of the virtual key vectors to generate the preset key vector compression model includes: Based on a second preset loss function, calculating a second loss value between each of the training key vectors and each of the virtual key vectors; Based on each of the second loss values, the initial key vector compression network is trained to generate the preset key vector compression model.
9. The method according to claim 7, It is characterized in that The training key vector corresponds to the trained query vector; the method further comprising: Training the initial query vector compression network based on each of the training query vectors and each of the virtual query vectors to generate a spare query vector compression model; Training the initial key vector compression network based on each of the training key vectors and each of the virtual key vectors to generate a spare key vector compression model; Performing statistical calculations on each of the training query vectors and the corresponding training key vectors to obtain a first result; Performing statistical calculations on each of the initial coding query vectors and each of the corresponding initial coding key vectors to obtain a second result; Based on a third preset loss function between each first result and the corresponding second result, the backup query vector compression model is trained to generate the preset query vector compression model, and the backup key vector compression model is trained to generate the preset key vector compression model.
10. The method according to claim 1, It is characterized in that The determining at least one target compressed key vector from the compressed key vectors according to the relationship between the compressed query vectors and the compressed key vectors comprises: Acquire a preset number of the target compressed key vectors corresponding to each compressed query vector, where the preset number is used to represent the number of the target compressed key vectors associated with each compressed query vector; According to the relationship between each of the compressed query vectors and each of the compressed key vectors, a preset number of the target compressed key vectors are determined from the compressed key vectors.
11. The method according to claim 10, It is characterized in that The determining a target compressed key vector from the compressed key vectors according to the relationship between each compressed query vector and the corresponding preset number of compressed key vectors includes: Calculating an attention score between each of the compressed query vectors and each of the compressed key vectors; A preset number of the compressed key vectors with the highest attention scores are determined as the target compressed key vectors.
12. The method according to claim 11, It is characterized in that The outputting the target analysis result corresponding to the content to be analyzed based on each of the compressed query vectors, each of the target initial key vectors, and each of the target initial value vectors includes: Obtaining an initial number of attention mechanism heads in the preset language analysis model; For each of the attention mechanism heads, obtaining position information of the target compressed key vector corresponding to the attention mechanism head in the compressed key vector; Determine the target attention mechanism head from each of the attention mechanism heads according to the position information of the target compressed key vector corresponding to each of the attention mechanism heads in the compressed key vector; For each of the compressed query vectors, processing the compressed query vector, the target initial key vector corresponding to the compressed query vector, and the target initial value vector corresponding to the compressed query vector based on the target attention mechanism head, and outputting a first output result; Determine, based on the feedforward neural network layer, a second output result corresponding to the first output result; Based on each of the second output results, the target analysis result is generated.
13. The method according to claim 12, It is characterized in that The determining the target attention mechanism head from each of the attention mechanism heads according to the position information of the target compressed key vector corresponding to each of the attention mechanism heads in the compressed key vector comprises: Calculate the similarity between the target compressed key vectors corresponding to each of the attention mechanism heads according to the position information of the target compressed key vectors corresponding to each of the attention mechanism heads in the compressed key vector; According to the similarity between the target compressed key vectors corresponding to the attention mechanism heads pairwise, the target attention mechanism head is determined from each of the attention mechanism heads.
14. The method according to claim 13, It is characterized in that The determining the target attention mechanism head from each of the attention mechanism heads according to the similarity between the target compressed key vectors corresponding to the attention mechanism heads each other includes: Comparing the similarity between the target compressed key vectors corresponding to the attention mechanism heads with a preset similarity threshold; Deleting any one of the two attention mechanism heads whose similarity is greater than the preset similarity threshold; Until the similarity between the target compressed key vectors corresponding to any two of the attention mechanism heads is less than the preset similarity threshold, the target attention mechanism head is obtained.
15. The method according to claim 1, It is characterized in that The method further comprises: If the second current storage occupancy rate corresponding to the second storage device is greater than a second preset storage occupancy rate threshold, obtaining a second current compressed query vector again; calculating a second relative score between the second current compressed query vector and each of the first compressed key vectors to be offloaded; According to each of the second relative scores, determining, from each of the first compressed key vectors to be uninstalled, a second compressed key vector to be uninstalled whose second relative score is less than a second preset threshold; Each of the second compressed key vectors to be unloaded and the initial value vector corresponding to each of the second compressed key vectors to be unloaded are unloaded from the second storage device to a third storage device, wherein a storage speed corresponding to the third storage device is lower than that of the second storage device.
16. A language model reasoning optimization device, It is characterized in that The device comprises: An acquisition module, used to acquire at least one information unit to be analyzed corresponding to the content to be analyzed, and input each of the information units to be analyzed into a preset language analysis model; A filling module, used to pre-fill each of the information units to be analyzed based on the preset language analysis model, and obtain an initial query vector, an initial key vector and an initial value vector corresponding to each of the information units to be analyzed; A compression module, used for compressing each of the initial query vectors and each of the initial key vectors to obtain each compressed query vector and each compressed key vector; a determination module, configured to determine at least one target compressed key vector from the compressed key vectors according to the relationship between each of the compressed query vectors and each of the compressed key vectors; A storage module, used to store the target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector; wherein, the storing of the target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector includes: storing the target initial key vector, each initial value vector and each compressed key vector corresponding to each target compressed key vector to a first storage device; if the first current storage occupancy rate corresponding to the first storage device is greater than a first preset storage occupancy rate threshold, obtaining a first current compressed query vector; calculating a first relative score between the first current compressed query vector and each compressed key vector; according to each first relative score, determining a first to-be-unloaded compressed key vector whose first relative score is less than a first preset threshold from each compressed key vector; unloading each first to-be-unloaded compressed key vector and the initial value vector corresponding to each first to-be-unloaded compressed key vector from the first storage device to a second storage device, wherein the storage speed corresponding to the second storage device is less than that of the first storage device; An output module is used to output a target analysis result corresponding to the content to be analyzed based on each of the compressed query vectors, each of the target initial key vectors and each of the target initial value vectors.
17. An electronic device, It is characterized in that include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the language model reasoning optimization method according to any one of claims 1 to 15 by executing the computer instructions.
18. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the language model reasoning optimization method according to any one of claims 1 to 15.
19. A computer program product, It is characterized in that The method comprises computer instructions, wherein the computer instructions are used to cause a computer to execute the language model reasoning optimization method according to any one of claims 1 to 15.
