Model reasoning method and device, equipment and medium

By training and compressing historical lexical sequences when the key-value pair cache of a large language model reaches a threshold, optimizing fast weights, and generating replacement lexical sequences, the problem of excessive resource consumption in long lexical tasks is solved, achieving efficient and accurate model inference.

CN118886508APending Publication Date: 2024-11-01SHANGHAI BIREN TECH CO LTD
View PDF 0 Cites 7 Cited by

Patent Information

Application Number
CN202411063960.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-02
Publication Date
2024-11-01

AI Technical Summary

Technical Problem

Existing large language models consume huge amounts of computational and storage resources in long lexical tasks due to the linear growth of key-value pair cache length, which may lead to memory shortage problems.

Method used

When the key-value pair cache length reaches the threshold, the model's fast weights are optimized by training and compressing the historical lexical sequence, generating a replacement lexical sequence, and then applying the trained model for inference.

Benefits of technology

It improves the efficiency and accuracy of the model in handling long sequence tasks, reduces the consumption of computing and storage resources, and maintains high-speed inference and output quality.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118886508A_ABST
    Figure CN118886508A_ABST
Patent Text Reader

Abstract

The invention provides a model reasoning method and device, equipment and a medium, and the method comprises the steps: carrying out the training of a model based on a historical lexical element sequence generated by the model reasoning under the condition that the length of a key value pair cache used by the model reasoning reaches a length threshold value; compressing the historical lexical element sequence to obtain a replacement lexical element sequence; and based on the replacement lexical element sequence, reasoning by applying a trained model. According to the method, the model is trained on the basis of the historical lexical element sequence generated by the model reasoning under the condition that the length of the key value pair cache used by the model reasoning reaches the length threshold value, so that the efficiency and the accuracy of processing the long sequence task by the model are improved. Meanwhile, by controlling the maximum length of the key value pair cache and compressing the historical lexical element sequence, the consumption of calculation and storage resources can be reduced. Based on the optimization, the model can keep efficient reasoning speed and output quality when processing complex and long-sequence tasks.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a model reasoning method, device, equipment and medium. Background Art

[0002] Large language models usually use autoregressive reasoning to infer the next token based on the existing tokens. During each step of reasoning, the self-attention modules at each layer of the model will save the key-value cache (KV cache) for subsequent reasoning.

[0003] However, as the number of inference steps increases, the length of the key-value cache increases linearly, which will lead to huge consumption of computing and storage resources in long word tasks. Especially when the video memory of existing AI accelerators (such as GPUs) is limited, the memory burden will gradually increase, which may eventually lead to insufficient memory. Summary of the invention

[0004] The present invention provides a model reasoning method, device, equipment and medium to solve the defect of huge consumption of computing and storage resources in related technologies.

[0005] The present invention provides a model reasoning method, comprising the following steps: When the length of the key-value pair cache used for model reasoning reaches a length threshold, training the model based on the historical word-meta sequence generated by the model reasoning; Compressing the historical word sequence to obtain a replacement word sequence; Based on the replacement word-gram sequence, the trained model is applied to perform inference.

[0006] According to the model inference method provided by the present invention, the model is trained based on the historical word-gram sequence generated by the model inference, including: Based on the historical word-gram sequence generated by the model reasoning, training the fast weights in the model; The quick weight is a weight in the model associated with the key-value pair cache.

[0007] According to the model inference method provided by the present invention, the fast weight includes the key weight and the value weight in the attention module of the model, the key weight is used to determine the key matrix, and the value weight is used to determine the value matrix.

[0008] According to the model inference method provided by the present invention, the training of the fast weights in the model based on the historical word-gram sequence generated by the model inference includes: From the historical word-gram sequence generated by the model inference, selecting the historical word-grams of the length threshold generated by the last inference as the first sample word-gram sequence; Based on the first sample word-gram sequence, the fast weights in the model are trained.

[0009] According to the model inference method provided by the present invention, the training of the fast weights in the model based on the first sample word-unit sequence includes: Selecting a second sample word-gram sequence other than the first sample word-gram sequence from the historical word-gram sequence; Based on the first sample word-gram sequence and the second sample word-gram sequence, fast weights in the model are trained.

[0010] According to the model inference method provided by the present invention, compressing the historical word sequence to obtain a replacement word sequence includes: Based on the importance of each historical word-gram in the historical word-gram sequence, historical word-grams are selected from the historical word-gram sequence to construct a replacement word-gram sequence.

[0011] The present invention provides a model reasoning device, comprising the following modules: A training unit, configured to train the model based on a historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches a length threshold; A compression unit, used for compressing the historical word-unit sequence to obtain a replacement word-unit sequence; An inference unit is used to perform inference based on the replacement word-unit sequence using the trained model.

[0012] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the model inference method described above is implemented.

[0013] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the model reasoning methods described above.

[0014] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned model reasoning methods.

[0015] The present invention provides a model reasoning method, device, equipment and medium, which improves the efficiency and accuracy of the model in processing long sequence tasks by training the model based on the historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches a length threshold. At the same time, controlling the maximum length of the key-value pair cache and compressing the historical word sequence can reduce the consumption of computing and storage resources. Based on these optimizations, the model can maintain efficient reasoning speed and output quality when processing complex and long sequence tasks. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0017] Figure 1 It is a flow chart of the working mechanism of autoregressive inference in related technologies.

[0018] Figure 2 It is a flow chart of the model reasoning method provided by the present invention.

[0019] Figure 3 It is a schematic diagram of the model reasoning process provided by the present invention.

[0020] Figure 4 This is a schematic diagram of the structure of the model reasoning device provided by the present invention.

[0021] Figure 5 It is a schematic diagram of the physical structure of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0022] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings 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 ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0023] Autoregressive inference is a generative modeling technique that generates new outputs incrementally by relying on previously generated outputs. Autoregressive inference is a very popular approach in natural language processing and other generative tasks such as image and audio generation. The core idea of ​​autoregressive inference is that the model predicts the next output based on the previous input or output. This process is done step by step, with each step generating a new output that is used as input for the next step. Specifically in language models, this means that the model predicts the n+1th word by parsing the first n words in a text.

[0024] Autoregressive inference can be divided into two main stages: contextual decoding and single-step decoding. In the contextual decoding stage, the model receives the initial input or prompt and generates the corresponding latent space representation. These representations are stored in the key-value cache for subsequent single-step decoding. In the single-step decoding stage, the model gradually generates new tokens, and the tokens generated at each step are used as input for the next step and continue to be processed. After each new token is generated, the self-attention module updates the key-value cache, and the new key (Key, K) matrix and value (Value, V) matrix are merged with the previously saved key-value cache to form a longer key-value cache. In this way, autoregressive inference can gradually generate sequences, generate a new token at each step, and continuously update the key-value cache to maintain memory and processing capabilities for long sequences.

[0025] Figure 1 This is a flowchart of the working mechanism of autoregressive inference in related technologies. Figure 1 As shown in the figure, the autoregressive inference model is implemented based on a hierarchical structure, which contains a multi-layer neural network (from layer 1 to layer L). Each layer contains a self-attention module and a fully connected module. The model receives user prompts or the output generated in the previous step as input. Here, the user prompts can be understood as a sequence of word units. Among them, the user prompts are converted into a series of word units by the encoder and embedded into the latent space representation. Specifically, the user prompts are embedded in a matrix of size s×d, where s is the sequence length and d is the latent space dimension. In each layer, the self-attention module is used to calculate the association between the input word units. The input x is passed through the weight matrix W Q , W K and W V The query (Query, Q) matrix, key matrix and value matrix are calculated respectively, and then the attention weight is calculated by the softmax function and combined with the value matrix and model weight matrix W O Multiply to get the output y. The specific calculation is as follows:

[0026] in, K , V The matrix will be cached as a new key-value pair, i.e. the new K cache and V cache The output size of the self-attention module is s×d, which is then transformed nonlinearly by the fully connected module to generate the final output of this layer.

[0027] like Figure 1 As shown in the figure, assume that the input sequence length is s and the latent space dimension is d. In the context decoding stage, the input is embedded as a matrix of size [s, d]. After the calculation of each layer of self-attention module, the size of the generated key matrix and value matrix is ​​also [s, d]. At this time, the size of the key-value cache is 2×L×[s, d]. In the single-step decoding stage, after the first step of generating a new key-value cache, a new word is added, and the size of the key-value cache becomes 2×L×[s+1, d]; after the first step of generating a new key-value cache, another word is added, and the size of the key-value cache becomes 2×L×[s+2, d]. And so on, for each new word, the length of the key-value cache increases by 1, and the final size of the key-value cache is 2×L×[s+n, d], where n is the number of generated words.

[0028] In this way, autoregressive reasoning implements the process of gradually generating sequences, generating a new token at each step, and continuously updating the key-value cache to maintain memory and processing capabilities for long sequences. However, as the number of reasoning steps increases, the length of the key-value cache grows linearly, which will lead to huge consumption of computing and storage resources in long sequence tasks.

[0029] In view of the above problems, an embodiment of the present invention provides a model reasoning method. Figure 2 Schematic diagram of the model inference method provided by the present invention. Figure 2 As shown, the method includes: Step 210: When the length of the key-value pair cache used for model reasoning reaches a length threshold, the model is trained based on the historical word sequence generated by the model reasoning.

[0030] The model here is a neural network model containing multiple attention layers, for example, it can be a Transformer model. The model can be specifically used to implement sequence reasoning, for example, it can be used to implement text sequence reasoning, image sequence reasoning, video sequence reasoning, and speech sequence reasoning, etc., and the embodiments of the present invention do not specifically limit this. Furthermore, the model here can be a model for tasks such as text classification and sentiment analysis, or a model for question-and-answer systems, dialogue generation, and long text generation. It can also be a model applied to computer vision tasks such as image classification, target detection, target tracking, high-resolution image generation, and long video generation, and the embodiments of the present invention do not specifically limit this.

[0031] Taking a single model inference as an example, the key-value pair cache generated by the model when inferring the historical word sequence before the current model inference can be obtained. The historical word sequence here is a sequence composed of multiple historical word elements, where the historical word elements can be understood as the word elements of the previous input model and the word elements output by the model inference. For example, in text sequence inference, the historical word element can be a character or a word, and the historical word element sequence can be the text input by the user, or a combination of the text input by the user and the text inferred by the model. The embodiment of the present invention does not make specific limitations on this.

[0032] In the process of reasoning historical words, each layer of attention modules in the model participates in the calculation, and each attention module calculates the key-value cache corresponding to the historical word. Therefore, as the model reasoning gradually runs, the length of the key-value cache generated by each attention module in the model gradually accumulates.

[0033] In this case, in order to avoid insufficient memory due to a large key-value cache size, a length threshold can be set in advance. The length threshold here is the maximum length of the key-value cache. The length threshold setting can be associated with the memory size of the running model.

[0034] Specifically, after the length of the key-value pair cache reaches the length threshold, the model will use the historical word-gram sequence generated by reasoning as training data, for example, the complete historical word-gram sequence can be used as training data, or the most recently generated partial sequence in the historical word-gram sequence can be used as training data. Using these training data, the model will fine-tune its internal model weight parameters (such as the weight matrix of the attention module) to better remember and understand the dependencies and contextual information in the long sequence. This process aims to optimize the weight parameters of the model through training, so that the historical word-gram sequence and the association of each historical word-gram in the historical word-gram sequence can be integrated into the weight parameters of the model, so that the model can maintain the memory of the historical word-gram sequence, so that it can more effectively process long word-gram data without retaining the key-value pair cache of the historical word-gram sequence. At the same time, this method can not only avoid the unlimited growth of the key-value pair cache by setting the length threshold, but also improve the performance and accuracy of the model in processing long word-gram tasks without increasing the storage burden.

[0035] Step 220, compress the historical word sequence to obtain a replacement word sequence.

[0036] Specifically, historical word sequences usually contain a large number of words. Directly storing and processing this data will lead to a sharp increase in storage space and computing resources. Therefore, compression technology can effectively reduce the amount of data and improve the processing efficiency of the model.

[0037] The embodiment of the present invention does not limit the compression method of the historical word sequence. For example, by removing redundant information, the repeated or unnecessary parts in the historical word sequence are identified and removed, and only the information that is critical to model reasoning is retained. For another example, by using the model's own attention mechanism, by calculating the importance weight of each word, the word that contributes most to reasoning is screened out to form a compressed replacement word sequence. Even random selection is one of the optional compression methods.

[0038] The compressed replacement word sequence is shorter than the historical word sequence, which reduces data redundancy and enables subsequent model reasoning and training to be performed more efficiently. This not only reduces the demand for storage and computing resources, but also speeds up the response of the model and improves the performance of the overall system.

[0039] Step 230, based on the replacement word sequence, use the trained model to perform reasoning.

[0040] Specifically, the amount of data in the replacement word-meta sequence is small, and the trained model can process these inputs more efficiently, thereby speeding up the reasoning process. The replacement word-meta sequence is input into the trained model as new context information. The trained model uses these compressed data for processing to generate a latent space representation. The model converts the replacement word-meta sequence into a latent space representation through the embedding layer and passes it layer by layer, processed by the self-attention module and the fully connected module of each layer. The self-attention module calculates new query, key, and value matrices to capture the dependencies and features in the replacement word-meta sequence. At each step of reasoning, the model generates the next word-meta based on the current latent space representation and the updated key-value pair cache. This process is repeated until the complete output sequence is generated.

[0041] By applying the trained model to reasoning based on the replacement word sequence, the efficiency and accuracy of reasoning can be significantly improved. The compressed replacement word sequence not only reduces the computational and storage burden of the model, but also retains certain contextual information, allowing the model to generate high-quality output in a shorter time.

[0042] The inference method provided by the embodiment of the present invention trains the model based on the historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches the length threshold, thereby improving the efficiency and accuracy of the model in processing long sequence tasks. At the same time, controlling the maximum length of the key-value pair cache and compressing the historical word sequence can reduce the consumption of computing and storage resources. Based on these optimizations, the model can maintain efficient reasoning speed and output quality when processing complex and long sequence tasks.

[0043] Based on the above embodiment, in step 210, the training of the model based on the historical word-gram sequence generated by the model inference includes: Based on the historical word-gram sequence generated by the model reasoning, training the fast weights in the model; The quick weight is a weight in the model associated with the key-value pair cache.

[0044] Specifically, when the model is inferring, it generates a series of historical word-gram sequences. These sequences contain all the input data and intermediate results encountered by the model during the inference process. By using these historical word-gram sequences, the model can identify and learn the important patterns in the input data and the dependencies between historical word-grams, thereby training and optimizing the fast weights in a targeted manner.

[0045] Fast weights are a type of weight between model weight parameters trained for a long time and short-term activation latent space variables. It is characterized by being able to be updated multiple times in a short time and having a high update frequency. Through the dynamic adjustment of fast weights, the model can adapt to the changes in the newly input data in a timely manner and capture the latest contextual information. Specifically, the fast weights are adjusted according to the current input and historical word sequences during each reasoning process, so that the model can more flexibly handle dependencies and important patterns in long sequence data. In an embodiment of the present invention, fast weights are weights related to the key-value pair cache in the model. The update of fast weights is a dynamic adjustment process, the purpose of which is to limit the length of the key-value pair cache while maintaining the efficient reasoning ability of the model. These fast weights directly affect the calculation and storage of the key matrix and value matrix in the self-attention module. Therefore, by training these weights, the model can better adapt to long sequence inputs and maintain the ability to remember long sequence historical information while reducing storage and computing resource consumption.

[0046] In this way, training the model based on historical word sequences can significantly optimize the performance of the model, making it more efficient and with better output quality when processing long word tasks such as long text generation and long video generation. This method not only solves the problem of unlimited growth of key-value cache, but also maintains the model's effective memory and understanding of long historical words, and is suitable for a variety of complex and demanding application scenarios.

[0047] Based on the above embodiment, the fast weight includes the key weight and the value weight in the attention module of the model, the key weight is used to determine the key matrix, and the value weight is used to determine the value matrix.

[0048] Specifically, during model inference, the key weights ( W K ) is used to calculate the importance and position relationship of each word in the input sequence and generate a key matrix. These key matrices play the role of matching the query matrix and the value matrix in the self-attention mechanism, thereby determining which input information is most important in the current step. Value weight ( W V ) is used to generate value matrices, which contain the actual value information and feature representation of each word in the input sequence, and are combined with the key matrix in the self-attention calculation to generate the final attention output.

[0049] During the training process, the model will dynamically adjust and optimize these key weights and value weights based on the information in the historical word sequence. This process can not only effectively integrate the key information in the long sequence, but also reduce the redundant data in the key-value pair cache through rapid weight updates, thereby improving the efficiency and accuracy of model reasoning. In this way, the model can better adapt to long sequence tasks and improve the performance and output quality of reasoning, especially in complex application scenarios such as processing long text generation and long video generation.

[0050] Based on the above embodiment, the training of the fast weights in the model based on the historical word sequence generated by the model reasoning includes: From the historical word-gram sequence generated by the model inference, selecting the historical word-grams of the length threshold generated by the last inference as the first sample word-gram sequence; Based on the first sample word-gram sequence, the fast weights in the model are trained.

[0051] Specifically, the model generates a large number of historical word-meta sequences during the reasoning process. These sequences contain the word-meta sequences generated by the model in each step of reasoning. When the length of the key-value pair cache used for model reasoning reaches the preset length threshold, the most recently generated historical word-meta sequences of the length threshold are extracted from these historical word-meta sequences as the first sample word-meta sequence. It can be understood that compared to the historical word-meta sequence as a whole, the first sample historical sequence can reflect the parameters that the model needs to learn most during this model training, thereby ensuring that the trained model can maintain memory of the historical word-meta sequence, especially the most recently generated first sample historical sequence in the historical word-meta sequence.

[0052] By selecting the historical word-grams of the length threshold generated by the last reasoning as the first sample word-gram sequence, the model can focus on these historical word-grams generated by the most recent reasoning during the training process of fast weights, thereby better adjusting and optimizing the key weights and value weights in the attention module.

[0053] Next, the fast weights in the model can be trained based on the first sample word-meta sequence. Specifically, the model will use the information in the first sample word-meta sequence to update and optimize its internal fast weights. In this process, the attention module of the model will calculate the key weights and value weights and adjust them so that the fast weights can better adapt to the new input data. This training process enables the model to update its internal parameters in a timely manner, improve its processing capabilities for long sequence data, and ensure that more accurate and relevant outputs are generated during the reasoning process. By training the fast weights, the model not only improves processing efficiency, but also maintains memory and understanding of important historical information.

[0054] Based on the above embodiment, the step of training the fast weights in the model based on the first sample word sequence includes: Selecting a second sample word-gram sequence other than the first sample word-gram sequence from the historical word-gram sequence; Based on the first sample word-gram sequence and the second sample word-gram sequence, fast weights in the model are trained.

[0055] Specifically, the second sample word-gram sequence is composed of historical word-grams that are not included in the first sample word-gram sequence. These word-grams cover contextual information over a longer time range and provide a wider range of data background and associated information.

[0056] After selecting the second sample word sequence, the fast weights in the model are trained based on the first sample word sequence and the second sample word sequence. In this process, the model will use the latest information in the first sample word sequence and the extensive context in the second sample word sequence to comprehensively update and optimize its internal fast weights. The attention module adjusts the key weights and value weights to ensure that the model can capture both recent key patterns and long-term dependencies.

[0057] This training method that combines two sample word sequences enables the model to not only effectively adapt to new input data, but also maintain memory and understanding of historical information over a longer period of time. By training fast weights, the model improves processing efficiency, reduces the consumption of storage and computing resources, and exhibits higher accuracy and stability when processing complex tasks such as long text generation and long video generation.

[0058] Based on the above embodiment, compressing the historical word sequence to obtain a replacement word sequence includes: Based on the importance of each historical word-gram in the historical word-gram sequence, historical word-grams are selected from the historical word-gram sequence to construct a replacement word-gram sequence.

[0059] First, based on the importance of each historical word in the historical word sequence, historical words are selected from the historical word sequence to construct a replacement word sequence. Specifically, the model generates a large number of historical word sequences during the reasoning process, and these sequences contain words generated at each step of reasoning. In order to effectively compress this data, the model evaluates the importance of each historical word.

[0060] Importance evaluation can be based on multiple factors, such as the position of each historical token in the context, its influence on the generation of the next token, and its frequency and relevance in the entire sequence. By combining these factors, the model can determine which historical tokens play a key role in the reasoning process.

[0061] After evaluating the importance, the model will select historical words with higher importance from the historical word sequence as the basis for constructing the replacement word sequence. These selected historical words will retain the main context information and dependencies, thereby compressing the data volume while ensuring that the replacement word sequence can effectively represent the key information of the original sequence.

[0062] Through this method, the model can effectively compress the historical word sequence, and the generated replacement word sequence can still maintain the expression of the main information and dependencies of the original sequence while greatly reducing the amount of data. This not only reduces the demand for storage and computing resources, but also improves the efficiency and accuracy of the model during reasoning.

[0063] Based on the above embodiments, Figure 3 It is a schematic diagram of the model reasoning process provided by the present invention. Figure 3 The operation flow of key-value cache in the autoregressive inference process of large models after the introduction of fast weight training mechanism is shown in detail. First, in the input stage, the user enters a prompt, which is converted into a sequence of tokens of length s by the tokenizer. These tokens are converted into latent space variables with size [s, d] through the embedding layer. These latent space variables are used as input and enter the first layer of the model for processing.

[0064] After the input sequence word enters the first layer of the model, context decoding is required. The input word sequence will enter each layer of the model in turn (a total of L layers), and each layer contains a self-attention module and a fully connected module. The main function of the self-attention module is to calculate the relationship and importance between the input words. Specifically, assuming that the input of each layer in the model is x , with shape [s, d]. In each layer, the query matrix, key matrix, and value matrix are first calculated:

[0065] Next, the output y is obtained by calculating the inner product of the query matrix and the key matrix and applying the softmax function and the output weight matrix:

[0066] in, W O is the output weight matrix. Each layer of the self-attention module will save the key matrix and value matrix of size [s, d] as a key-value pair cache for subsequent decoding steps. The total amount of key-value pair cache data saved in each layer is 2×L×s×d. The function of the fully connected module is to further linearly transform and nonlinearly activate the output of the self-attention module, thereby enhancing the model's expressiveness and ability to fit complex relationships.

[0067] In the single-step decoding stage, the next word is predicted and used as the input word for the next step of reasoning. Each layer of the model performs self-attention again, but this time the input word x The length of 1 is 1, and the size of the latent space is [1, d]. The calculation of the first step of word-unit reasoning is as follows:

[0068] Save the new key-value cache with a size of [s+1, d]. The total amount of data in the key-value cache is 2×L×(s+1)×d. After each step of reasoning, the key-value cache will increase by the length of a word. That is, after the i-th step of single-step decoding, the size of the key-value pair is [s+i, d].

[0069] As the inference progresses, the length of the key-value cache gradually increases. When the length reaches M, that is, the key-value cache size is [M, d], it stops increasing. At this time, the fast weight training phase begins, where m tokens are selected from the most recent M input tokens, and these m tokens are used to train the key model weight parameters of the model (such as the query weight matrix in the attention module). W Q , key weight matrix W K Sum weight matrix W V ) for fine-tuning training. The model weights are updated periodically through training to save the associated information of historical word units. The selection strategy of these m word units includes but is not limited to self-screening by the model (for example, based on the word unit with the highest prediction probability), or random selection. During the fast weight training process, the previous historical (k-1)×M input word units can also be added to the training data of the most recent M input word units, where k is the number of times the key-value pair cache length reaches M. In this way, the model can better remember historical information over a longer time range.

[0070] After the key weight parameters of the model are updated, the selected m words are used as new context inputs to restart the context decoding and single-step decoding steps. When the key-value cache reaches a length of M again, the fast weight training and restart are performed again. The length of the key-value cache after the restart is m+n, where n is the number of words that have been inferred since the restart. When the total number of inferences reaches the total number of predicted words N (at this time, the key-value cache length satisfies m+n≤M≪N, where k×M+n=N), the overall inference ends.

[0071] The model reasoning device provided by the present invention is described below. The model reasoning device described below and the model reasoning method described above can be referenced to each other.

[0072] Figure 4 is a schematic diagram of the structure of the model reasoning device provided by the present invention, such as Figure 4 As shown, the device comprises: A training unit 410 is used to train the model based on the historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches a length threshold; A compression unit 420, configured to compress the historical word-gram sequence to obtain a replacement word-gram sequence; The reasoning unit 430 is used to perform reasoning based on the replacement word sequence using the trained model.

[0073] The device provided by the embodiment of the present invention improves the efficiency and accuracy of the model in processing long sequence tasks by training the model based on the historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches the length threshold. At the same time, controlling the maximum length of the key-value pair cache and compressing the historical word sequence can reduce the consumption of computing and storage resources. Based on these optimizations, the model can maintain efficient reasoning speed and output quality when processing complex and long sequence tasks.

[0074] Based on any of the above embodiments, the training unit is specifically used for: Based on the historical word-gram sequence generated by the model reasoning, training the fast weights in the model; The quick weight is a weight in the model associated with the key-value pair cache.

[0075] Based on any of the above embodiments, the training unit is specifically used for: The fast weights include key weights and value weights in the attention module of the model, wherein the key weights are used to determine a key matrix and the value weights are used to determine a value matrix.

[0076] Based on any of the above embodiments, the training unit includes: A word unit selection subunit is used to select the historical word units with the length threshold generated by the last reasoning from the historical word unit sequence generated by the model reasoning as the first sample word unit sequence; The weight training subunit is used to train the fast weights in the model based on the first sample word sequence.

[0077] Based on any of the above embodiments, the word unit selection subunit is further used for: Selecting a second sample word-gram sequence other than the first sample word-gram sequence from the historical word-gram sequence; The weight training subunit is specifically used for: Based on the first sample word-gram sequence and the second sample word-gram sequence, fast weights in the model are trained.

[0078] Based on any of the above embodiments, the compression unit is specifically used for: Based on the importance of each historical word-gram in the historical word-gram sequence, historical word-grams are selected from the historical word-gram sequence to construct a replacement word-gram sequence.

[0079] Figure 5 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 5 As shown, the electronic device may include: a processor 510, a communication interface 520, a memory 530 and a communication bus 540, wherein the processor 510, the communication interface 520 and the memory 530 communicate with each other through the communication bus 540. The processor 510 may call the logic instructions in the memory 530 to execute the model reasoning method, which includes: When the length of the key-value pair cache used for model reasoning reaches a length threshold, training the model based on the historical word-meta sequence generated by the model reasoning; Compressing the historical word sequence to obtain a replacement word sequence; Based on the replacement word-gram sequence, the trained model is applied to perform inference.

[0080] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the relevant technology or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0081] On the other hand, the present invention further provides a computer program product, the computer program product comprising a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, when the computer program is executed by a processor, the computer can execute the model reasoning method provided by the above methods, the method comprising: When the length of the key-value pair cache used for model reasoning reaches a length threshold, training the model based on the historical word-meta sequence generated by the model reasoning; Compressing the historical word sequence to obtain a replacement word sequence; Based on the replacement word-gram sequence, the trained model is applied to perform inference.

[0082] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to perform the model reasoning method provided by the above methods, the method comprising: When the length of the key-value pair cache used for model reasoning reaches a length threshold, training the model based on the historical word-meta sequence generated by the model reasoning; Compressing the historical word sequence to obtain a replacement word sequence; Based on the replacement word-gram sequence, the trained model is applied to perform inference.

[0083] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0084] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment.

[0085] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A model reasoning method, It is characterized in that include: When the length of the key-value pair cache used for model reasoning reaches a length threshold, training the model based on the historical word-meta sequence generated by the model reasoning; Compressing the historical word sequence to obtain a replacement word sequence; Based on the replacement word-gram sequence, the trained model is applied to perform inference.

2. The model reasoning method according to claim 1, It is characterized in that The training of the model based on the historical word-gram sequence generated by the model reasoning includes: Based on the historical word-gram sequence generated by the model reasoning, the fast weights in the model are trained; the fast weights are the weights in the model related to the key-value pair cache.

3. The model reasoning method according to claim 2, It is characterized in that The fast weights include key weights and value weights in the attention module of the model, wherein the key weights are used to determine a key matrix and the value weights are used to determine a value matrix.

4. The model reasoning method according to claim 2, It is characterized in that The training of the fast weights in the model based on the historical word sequence generated by the model reasoning includes: From the historical word-gram sequence generated by the model inference, selecting the historical word-grams of the length threshold generated by the last inference as the first sample word-gram sequence; Based on the first sample word-gram sequence, the fast weights in the model are trained.

5. The model reasoning method according to claim 4, It is characterized in that The step of training the fast weights in the model based on the first sample word sequence includes: Selecting a second sample word-gram sequence other than the first sample word-gram sequence from the historical word-gram sequence; Based on the first sample word-gram sequence and the second sample word-gram sequence, fast weights in the model are trained.

6. The model inference method according to any one of claims 1 to 5, It is characterized in that The step of compressing the historical word sequence to obtain a replacement word sequence includes: Based on the importance of each historical word-gram in the historical word-gram sequence, historical word-grams are selected from the historical word-gram sequence to construct a replacement word-gram sequence.

7. A model reasoning device, It is characterized in that include: A training unit, configured to train the model based on a historical word sequence generated by the model reasoning when the length of the key-value pair cache used for model reasoning reaches a length threshold; A compression unit, used for compressing the historical word-unit sequence to obtain a replacement word-unit sequence; An inference unit is used to perform inference based on the replacement word-unit sequence using the trained model.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, the model reasoning method as described in any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the model reasoning method according to any one of claims 1 to 6 is implemented.

10. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor, the model reasoning method according to any one of claims 1 to 6 is implemented.

Citation Information

Cited By

  • Storage optimization method and device, electronic equipment and storage medium

    CN120371221A

  • Large model reasoning method and device, related equipment and computer program product

    CN120430418A

  • Question and answer reasoning method and device based on key value cache compression, equipment and medium

    CN120598057A

  • Question and answer reasoning method and device based on key-value cache compression, equipment and medium

    CN120598057B

  • End side model reasoning method and device based on RWKV architecture, electronic equipment and storage medium

    CN120725163A