A large model reasoning acceleration method and system combining machine learning and speculative sampling
By combining machine learning and speculative sampling, and using n-gram language models and large models, we solve the architectural limitations and memory waste problems of large model inference acceleration in knowledge base Q&A scenarios, and achieve more efficient text generation and concurrent processing. Significantly improves the model's inference performance.
Patent Information
- Application Number
- CN202411110911.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-14
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2044-08-14
AI Technical Summary
Existing technologies are difficult to further accelerate large model reasoning in knowledge base question and answer scenarios, especially when concurrency and performance are guaranteed. Existing methods have problems with architectural limitations and memory waste.
Combining machine learning and speculative sampling, by building an n-gram language model and combining it with a large model, using the KV Cache of the transformers library and the Paged Attention and dynamic batch processing of vLLM, the speculative sampling of different architecture models can be accelerated and the time of large model inference can be reduced. and memory requirements.
Significantly improves the reasoning performance of large models in knowledge base question and answer scenarios, achieving faster text generation and higher concurrent request processing capabilities, reducing memory access requirements, avoiding memory waste, and improving the overall reasoning efficiency of the model .
Smart Images

Figure CN118657220B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of electronic digital data processing, and in particular relates to a large model reasoning acceleration method combining machine learning and speculative sampling. Background Art
[0002] With the continuous development and improvement of the capabilities of large models, the application of large models in people's daily work, life and other scenarios has become more and more common. The increase in parameters of large models has enabled them to achieve excellent performance in many natural language, computer vision and reinforcement learning tasks. However, in this case, the reasoning process of large models is much slower than that of small models. Since its reasoning process is executed serially, it means that decoding and generating n tokens requires the large model to run n times. Therefore, if the time for the large model to generate each token can be reduced, the reasoning speed of the large model can be accelerated while ensuring the reasoning performance of the large model. Its importance is self-evident.
[0003] So far, methods for accelerating large model reasoning have been proposed from different perspectives, mainly the following:
[0004] From the perspective of optimization on the computing side, when decoding, the large model needs to calculate the attention of the current token and all previously generated tokens, and needs to calculate the KV vectors of all tokens. The KV values of the previous tokens are saved after calculation in each round of decoding. This method is the implementation idea of KV Cache, and this technology is used in the transformer library of hugging face.
[0005] From the perspective of memory IO optimization, the dynamic batching (Continuous Batching) strategy allows shorter requests to be terminated early when batching multiple requests for inference, and the new request occupies the KV Cache space of the terminated request. This strategy increases the throughput of large models by two to three times. Current mainstream inference frameworks such as Huggingface TGI, Ray serve, vLLM, TensorRT-LLM, etc. all support dynamic batching strategies.
[0006] From the perspective of memory allocation, there are three types of memory waste when inferencing large models: (1) Reservation: Since the output length of each request is uncertain, we need to reserve max_seq_len space for each request. (2) Internal Fragmentation: Under the Static Batching strategy, when a request ends, the remaining space is wasted. (3) External Fragmentation: Since the KV Cache is a huge matrix and must occupy continuous memory, if the operating system only allocates large continuous memory, a lot of small memory space will inevitably be wasted. Excessive memory waste affects the throughput of the model, thereby reducing the inference speed of the model. Paged Attention draws on the idea of managing virtual memory through pages in the operating system: KV Cache is divided into fixed-size blocks. These blocks do not need to be stored in continuous memory and are managed by a unified memory allocator. Requests are made on demand for memory, and there is no need to reserve memory of max_seq_len in advance, which solves the waste of reservation. After the request is completed, the blocks are released, which solves the problem of internal fragmentation. The system only needs to allocate small blocks, which solves the problem of external fragmentation. The current mainstream reasoning framework vLLM, NVIDIA's TensorRT-LLM library, and Microsoft's Deepspeed-MII library provide support for Paged Attention.
[0007] From the perspective of computational methods, speculative sampling has gained wide recognition. Speculative sampling uses a small and cheap scratch model to generate a candidate sequence of n tokens - [draft]. Then, all this information is batched into a large model that is isomorphic to the draft model. The large model generates the corresponding n+1 tokens and probabilities, and by comparing the probabilities generated by the large model and the draft model, it chooses to accept some or all as output. The core idea of this method is to generate some simple tokens through a small and fast scratch model, and difficult and complex tokens through a large model to achieve reasoning acceleration. Other frameworks that optimize computational methods to achieve reasoning acceleration have also been proposed, such as Medusa, Lookahead Decoding, etc.
[0008] The current mainstream methods have brought certain improvements in inference acceleration performance from different angles, and then further improving the model inference speed based on them is somewhat difficult. For example, vLLM guarantees concurrency and certain inference acceleration, but existing work does not support vLLM for speculative sampling algorithms; speculative sampling improves model performance, but requires the same architecture for large and small models and limited model concurrency. KV Cache of transformers accelerates the inference performance of the model, but cannot guarantee model concurrency...
[0009] Knowledge base question answering is widely used in real life. When a large model is reasoning, the knowledge base question answering system will search the local knowledge base in advance, and send the appropriate knowledge base search results as local knowledge or prompts to the large model for text answer generation. For the real-life scenario of knowledge base question answering, the mainstream reasoning acceleration methods have their own limitations, and there is no mature technical solution in the existing technology to achieve further large model reasoning acceleration. Summary of the invention
[0010] To solve the above problems, the present invention discloses a large model reasoning acceleration method and system combining machine learning and speculative sampling, aiming to give full play to the advantages of speculative sampling and vLLM in the real application scenario of knowledge base question and answer, and realize speculative sampling acceleration model reasoning performance of different architecture models by combining n-gram language model with large model, and utilize the KV Cache caching mechanism of transformers library and Paged Attention and dynamic batch processing of vLLM to achieve the ultimate goal, and accelerate the reasoning performance of large models in knowledge base question and answer scenarios.
[0011] To achieve the above object, the technical solution of the present invention is as follows:
[0012] A large model inference acceleration method combining machine learning and speculative sampling includes the following steps
[0013] Step 1: construct an n-gram language model based on the retrieved local knowledge;
[0014] Step 2: When inferring the n-gram language model, input the text token, output the probability distribution of the next token in the vocabulary, and sample and generate the next token based on the probability distribution of the next token, thereby continuously generating n tokens according to the user's question;
[0015] Step 3: concatenate the generated n tokens with the prefix and input them into the large model to predict n+1 tokens;
[0016] Step 4: Verify the n+1 tokens generated by the large model and the n tokens generated by the n-gram model in parallel;
[0017] Step 5: Concatenate the selected result with the current prompt word prefix token as a new token sequence.
[0018] Furthermore, the following steps are included between step 1 and step 2:
[0019] (1) Preprocess the user's question and call the genrerate function of the large model. The generate function method inherits the generate function in the GenerationMixin class of the transformers library.
[0020] (2) After the data is passed into the generate function in the GenerationMixin class, the input tensor is , which is the prefix of the prompt word;
[0021] (3) Select the sampling mode.
[0022] Furthermore, the following steps are included between step 1 and step 2:
[0023] After preprocessing, the user question is processed into a prompt word prefix and passed into the LLMEngine of the vLLM reasoning framework as a parameter;
[0024] After receiving the user request, LLM Engine processes the user's question into a request seq_group through the add_request method and adds it to the scheduler, which manages and dispatches the user request.
[0025] Execute reasoning through the step() method under the LLM Engine class; obtain the seq_groups to be executed and the token to be calculated from the scheduler;
[0026] The obtained data is processed and converted into an input tensor, i.e., the prompt word prefix.
[0027] Furthermore, the process of the scheduler managing and allocating user requests includes: arranging tasks that have not yet been completed to the large model for inference generation; and outputting tasks that have met the termination conditions to the user interface.
[0028] Furthermore, in step 1, the process of constructing the n-gram language model specifically includes the following steps:
[0029] Step 1: Based on the first n-1 tokens, calculate the probability distribution of the next token in the vocabulary. The size is 1*the size of the vocabulary. The calculation formula is as follows:
[0030] ;
[0031] in, Indicates that for consecutive tokens , the next token is predicted to be The probability of Indicates that the next token predicted is Under the premise that the previous n-1 tokens are The probability of Indicates that token appears The probability of Indicates that the token is , among the consecutive tokens that appear afterwards, the n-1th token is The probability of Indicates that the token is , among the consecutive tokens that appear afterwards, the n-2th token is The probability of Indicates that the token is , among the consecutive tokens that appear afterwards, the n-3th token is The probability of Indicates that the token is , the first token that appears afterwards is The probability of is a constant; , , and Respectively represent the number of occurrences of all tuples counted; Indicates that the token is The number of occurrences of The number of all tokens representing the local knowledge used to build the n-gram model;
[0032] Step 2: Based on the calculated probability distribution of the next token, sample and generate the next token. The sampling formula is: , where is the next token predicted based on the current existing token. is the probability distribution of the next word of the current token in the vocabulary, and argmax is the sampling function.
[0033] Furthermore, the step three specifically includes the following process:
[0034] The large model constructs input tokens based on the results generated by the current n-gram model, generating a probability distribution of n+1 tokens with a size of (n+1)*vocab size; vocab size is the size of the vocabulary.
[0035] Parallel sampling is performed on the probability distribution generated by the large model to generate n+1 tokens. The parallel sampling formula is as follows:
[0036] ;
[0037] in, The n+1 tokens obtained by parallel sampling, The probability distribution of the generated n+1 tokens is (n+1) * vocab size. It is the sampling function of PyTorch.
[0038] Furthermore, the parallel verification in step 4 includes the following process:
[0039] If the generated tokens are exactly the same, the large model accepts the n tokens generated by the n-gram language model and the n+1th token generated by itself and outputs them; if the kth token among the n tokens generated by the n-gram is different from the token generated by the large model, the first k-1 tokens generated by the large model are selected as output.
[0040] The present invention also provides a large model reasoning acceleration system combining machine learning and speculative sampling, including a memory, a processor and a computer program stored in the memory, and the processor executes the computer program to implement the above-mentioned large model reasoning acceleration method combining machine learning and speculative sampling.
[0041] The beneficial effects of the present invention are:
[0042] The present invention constructs an n-gram model using the retrieved local knowledge, uses it as a draft model, and then combines it with a large model to achieve speculative sampling. Compared with the current mainstream method, the constructed n-gram model generates more reliable content compared to the approximate small model used by the existing speculative sampling algorithm, with less computation and reduced memory access requirements, and faster speed.
[0043] The present invention further applies the improved speculative sampling algorithm to the transformers library and utilizes the kv cache mechanism of the transformers library to accelerate the inference effect.
[0044] The present invention further applies the improved speculative sampling algorithm to the reasoning framework vLLM to achieve reasoning acceleration.
[0045] In the case of user interface access, in the knowledge base question and answer system, the speed of generating texts of the present invention is significantly improved, and at the same time, it is ensured that the model can accept concurrent requests. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 A schematic diagram of the process of the large model reasoning acceleration method combining machine learning and speculative sampling provided by the present invention;
[0047] Figure 2 This is a schematic diagram of the idea flow of constructing the n-gram model of the present invention;
[0048] Figure 3 This is the principle flow chart of the autoregressive decoding of this model and the principle flow chart of the speculative sampling method;
[0049] Figure 4 This is a flow chart of the speculative sampling method implemented by the present invention in combination with a machine learning method and a large model;
[0050] Figure 5 A schematic diagram of the application process of the speculative sampling method implemented by the present invention in combination with the machine learning method and the large model in the transformers library;
[0051] Figure 6 The figure is a schematic diagram of the application process of the speculative sampling method implemented by the present invention in combination with the machine learning method and the large model in the inference framework vLLM. DETAILED DESCRIPTION
[0052] The technical solution provided by the present invention will be described in detail below in conjunction with specific embodiments. It should be understood that the following specific implementation methods are only used to illustrate the present invention and are not used to limit the scope of the present invention.
[0053] See also Figure 1, which is a flowchart of the step-by-step optimization steps of the speculative sampling method based on machine learning methods and large models provided by the present invention. The present invention queries the knowledge base to obtain local knowledge based on user input, and builds an n-gram language model on the local knowledge; in the n-gram language model reasoning stage, the probability distribution of the next token in the vocabulary is predicted according to the given text token, and the next token is sampled and predicted; based on the constructed n-gram model and large model, a speculative sampling algorithm is implemented to accelerate the reasoning of the large model; the speculative sampling method based on the n-gram model and the large model is applied to the transformers library to achieve reasoning acceleration; the speculative sampling method based on the n-gram model and the large model is applied to the reasoning framework vLLM, and the reasoning speed is further improved.
[0054] Figure 1 The optimization step 1 is: based on the retrieved local knowledge, an n-gram language model is constructed through the probability calculation method of statistical learning.
[0055] In the embodiment of the present invention, the n-gram language model is constructed in the following manner to calculate the probability distribution of all words in the vocabulary: Based on the first n-1 tokens, the probability distribution of the next word in the vocabulary is obtained, and the size is 1*vocab size.
[0056] When a simple bi-gram model (i.e., n is 2) is used for inference, if the current token is a punctuation mark, it is easy to predict the next token inaccurately. In the present invention, n is expanded to 4 to construct a 4-gram model. It is meaningless to have n too large or too small. In real life, n usually does not exceed 5.
[0057] The construction of the 4-gram model is implemented in the following steps:
[0058] See also Figure 2 , count all the quadruples that appear in the retrieved local knowledge ,in Indicates 4 consecutive tokens; continue to count all the bigrams And all triples and , and the number of times they occur;
[0059] Specifically, the statistical results are converted into probabilities, and the following calculation formula is used to calculate the n-gram probability based on the naive Bayes formula and using the statistical tuple data:
[0060]
[0061] in, Indicates that for consecutive tokens , the next token is predicted to be The probability of . Through the Naive Bayesian principle, this probability expression can be transformed into the second line expression. The Naive Bayesian assumption is that when the next token is In the case of The probability of these three tokens appearing independently, so the first line of expression is transformed into the second line of expression. Indicates that the next token predicted is Under the premise that the first three tokens are The probability of Indicates that token appears Furthermore, the second line of expression can be transformed into the third line of expression, where Indicates that the token is , the third token among the consecutive tokens that appear is The probability of Indicates that the token is , the second token among the consecutive tokens that appear is The probability of Indicates that the token is , the first token among the consecutive tokens that appear is probability. Indicates that token appears For predicting the next token, When the denominator is It is essentially a constant. For the convenience of calculation, it is converted into the fourth line expression which is proportional to the third line expression. The calculation of the fourth line expression can be realized by calculating the fifth line expression.
[0062] in , and Respectively represent the number of occurrences of all tuples counted. Indicates that the token is The number of occurrences of The number of all tokens representing the local knowledge used to build the n-gram model.
[0063] The above calculation scheme for a single token, when generalized to the calculation of the probability distribution of the entire vocabulary, still uses the method in formula (4) to first initialize 4 all-0 tensors of size 1 * vocab size, corresponding to the 4 probabilities calculated above, and then accumulate the probabilities of the four categories calculated above into 4 all-0 tensors. , , and After calculating the probability distribution, the next token can be generated by adopting a suitable sampling strategy.
[0064] Specifically, based on the calculated probability distribution of the next token, the implementation process of sampling and generating the next token is as follows:
[0065] In the above text, the probabilities corresponding to the four probability tensors are obtained through the first three tokens of the current token. The size is 1*vocab size. According to the above formula, they are multiplied to get the probability distribution of the corresponding next token. , we sample the index with the highest probability, i.e., the argmax method, and use it as the predicted next token. The sampling formula is: ;
[0066] In the formula is the next token predicted based on the current existing token. The probability distribution of the next word of the current token in the vocabulary.
[0067] After building the n-gram language model, speculative sampling is implemented based on the built n-gram language model and the large model to improve the inference speed of the large model.
[0068] See also Figure 3 , which are the implementation principles of autoregressive decoding and speculative sampling algorithms respectively. Autoregressive decoding is the text generation method used by the current open source large model; speculative sampling uses a draft model to generate several tokens, and the large model verifies the tokens generated by the draft model in parallel, and the tokens that pass the verification are output as the final generated token. The speculative sampling algorithm uses the method of draft model generation + large model parallel verification to achieve a shorter time than the large model autoregressive decoding when generating the same token, thereby accelerating reasoning.
[0069] The current existing methods use an approximate model with much smaller parameters than the large model as a draft model. For example, when the large model is Qwen-14B-Chat, the approximate Qwen-1.8B is used as the draft model. The speculative sampling based on the combination of the two can achieve a certain acceleration. The existing methods all use this strategy for reasoning.
[0070] The present invention constructs an n-gram model using the retrieved local knowledge, uses it as a draft model, and then combines it with the large model to achieve speculative sampling. Compared with the current mainstream method, the constructed n-gram model generates more reliable content compared to the approximate small model mentioned above, with less computation and reduced memory access requirements, that is, faster.
[0071] See also Figure 4 , using the constructed n-gram language model and large model, the specific steps to implement regression speculative sampling are as follows:
[0072] Step 1: construct an n-gram model based on the retrieved local knowledge. The process of constructing the n-gram model has been described in detail in the previous article.
[0073] Step 2: The constructed n-gram model continuously generates n tokens according to the user's question. The specific process of generating each token is as follows: n-gram model forward reasoning, output probability distribution 1*vocab size, n-gram sampling of probability distribution, and generating token.
[0074] Step 3: Concatenate the generated n tokens with the prefix and send them to the large model to predict n+1 tokens.
[0075] Specifically, the large model constructs the input token based on the results generated by the current n-gram model, generating a probability distribution of n+1 tokens with a size of (n+1)*vocab size;
[0076] Parallel sampling is performed on the probability distribution generated by the large model to generate n+1 tokens. The parallel sampling formula is as follows:
[0077] ;
[0078] In the formula, The n+1 tokens obtained by parallel sampling, The probability distribution of the generated n+1 tokens is (n+1) * vocab size. is the sampling function of PyTorch;
[0079] Step 4: Verify the n+1 tokens generated by the large model and the n tokens generated by the n-gram model in parallel. If the generated tokens are exactly the same, the large model accepts the n tokens generated by the n-gram language model and the n+1th token generated by itself, and outputs them. If the kth token among the n tokens generated by the n-gram is different from the token generated by the large model, the first k-1 tokens generated by the large model are selected as output.
[0080] Step 5: Concatenate the selected result and the current prompt word prefix token together as a new token sequence.
[0081] Repeat the above five steps in a loop until the text generation is completed. Each time the speculative sampling algorithm is performed, it is determined whether the end generation condition is met. If the generated token contains a stop symbol, or the total generated token length exceeds the specified length, the corresponding sequence state is set to Finished, the sequence is released, and the answer is output.
[0082] The optimization step 2 provided by the present invention is: applying the above-implemented speculative sampling algorithm to the transformers library, and utilizing the kv cache caching mechanism of the transformers library to accelerate the inference effect.
[0083] The current transformers library does not combine well with the speculative sampling algorithm. It is not possible to directly implement speculative sampling based on n-grams and large models with the transformers library to achieve better acceleration performance. Figure 5 , this article implements the transformers library's support for speculative sampling based on n-grams and large models. The specific implementation steps are as follows:
[0084] Step 1: User questions are searched in the local knowledge base, and an n-gram model is constructed based on the retrieved local knowledge.
[0085] Step 2: Preprocess the user's question and call the genrerate function of the large model. This generate function method inherits the generate function in the GenerationMixin class of the transformers library.
[0086] Step 3: After the data is passed to the generate function in the GenerationMixin class, the input tensor is , which is the prefix of the prompt word.
[0087] Step 3: Select sampling mode, assisted decoding.
[0088] Step 4: n-gram model forward inference, output probability distribution 1*vocab size.
[0089] Step 5: n-gram samples the probability distribution and generates tokens.
[0090] Step 6: Repeat steps 4 and 5 to get 4 tokens, "Yuzhang Gulu".
[0091] Step 7: Concatenate the generated token with the prompt word prefix to get a new input tensor .
[0092] Step 8: Forward inference of the large model, output probability distribution 5*vocab size.
[0093] Step 9: The large model samples the probability distribution and obtains 5 tokens "Yuzhang Gujun Hong".
[0094] Step 10: Verify the large model generation results and the n-gram model generation results in parallel and output "Yu Zhang Gu".
[0095] Compared with the inference acceleration effect of the transformer library without speculative sampling method, facing the same user problem of "reciting the preface to the Pavilion of Prince Teng", this optimization can speed up the model inference speed from 51s, an average of 16 tokens / s to 25s, an average of 33 tokens / s, with significant performance improvement without affecting the generation results.
[0096] The optimization step 3 provided by the present invention is: applying the above-mentioned speculative sampling algorithm based on the n-gram model and the large model to the reasoning framework vLLM to achieve reasoning acceleration.
[0097] The existing vLLM framework accelerates the large model to execute a step() and generate a token, and the existing technology does not implement speculative sampling in the vLLM framework. Figure 6 This paper combines the speculative sampling technology based on n-gram model and large model with the vLLM reasoning framework technology. The step() method is implemented to output multiple tokens at a time. After combining the two technologies, a strong combination effect is achieved to accelerate model reasoning. The specific implementation details are as follows:
[0098] Step 1: User questions are searched in the local knowledge base, and an n-gram model is constructed based on the retrieved local knowledge.
[0099] Step 2: After preprocessing, the user question is processed into a prompt word prefix and passed as a parameter to the LLM Engine of the vLLM reasoning framework. LLM is a class instantiated by the vLLM framework, which coordinates and controls user requests and assigns them to the large model to complete text generation.
[0100] Step 3: After receiving the user request, LLM Engine processes the user's question into a request seq_group through the add_request method and adds it to the scheduler. The scheduler manages and dispatches user requests, such as scheduling tasks that have not yet been completed to the large model for inference generation, and outputting tasks that have met the end conditions to the user interface.
[0101] Step 4: Execute reasoning through the step() method of the LLM Engine class. Obtain the seq_groups to be executed and the token to be calculated from the scheduler.
[0102] Step 5: Process the obtained data and convert it into an input tensor, i.e., the prompt word prefix.
[0103] Step 6: n-gram model forward inference, output probability distribution 1*vocab size.
[0104] Step 7: n-gram samples the probability distribution and generates tokens.
[0105] Step 8: Repeat steps 6 and 7 to get 4 tokens, "Yuzhang Gulu".
[0106] Step nine: concatenate the generated token with the prompt word prefix to obtain a new input tensor.
[0107] Step 10: Forward inference of the large model, output probability distribution 5*vocab size
[0108] Step 11: The large model samples the probability distribution and obtains 5 tokens "Yuzhang Gujun Hong".
[0109] Step 12: Verify the large model generation results and the n-gram model generation results in parallel and output "Yu Zhang Gu".
[0110] Step 13: Determine whether the output token meets the end generation condition. If yes, output all tokens generated so far by this seq_groups. If no, repeat steps 5 to 12.
[0111] Compared with the inference acceleration effect of the vLLM library that does not use the speculative sampling method, facing the same user problem of "reciting the Tengwang Pavilion Preface", this optimization can speed up the model inference speed from 32s, an average of 26 tokens / s to 7s, an average of 121 tokens / s, with significant performance improvement without affecting the generation results.
[0112] The present invention also provides a large model reasoning acceleration system combining machine learning and speculative sampling, including a memory, a processor and a computer program stored in the memory, and the processor executes the computer program to implement the above-mentioned large model reasoning acceleration method combining machine learning and speculative sampling.
[0113] It should be noted that the above content only illustrates the technical idea of the present invention and cannot be used to limit the protection scope of the present invention. For ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications all fall within the protection scope of the claims of the present invention.
Claims
1. A large model inference acceleration method combining machine learning and speculative sampling, characterized in that: The steps include: Step 1: construct an n-gram language model based on the retrieved local knowledge; specifically, the following steps are included: Step 1: Based on the first n-1 tokens, calculate the probability distribution of the next token in the vocabulary, with a size of 1*vocabulary size; the calculation formula is as follows: ; in, Indicates that for consecutive tokens , the next token is predicted to be probability; Indicates that the next token predicted is Under the premise that the previous n-1 tokens are probability; Indicates that token appears probability; Indicates that the token is , among the consecutive tokens that appear afterwards, the n-1th token is probability; Indicates that the token is , among the consecutive tokens that appear afterwards, the n-2th token is probability; Indicates that the token is , among the consecutive tokens that appear afterwards, the n-3th token is probability; Indicates that the token is , the first token that appears afterwards is probability; is a constant; , , and Respectively represent the number of occurrences of all tuples counted; Indicates that the token is The number of occurrences of The number of all tokens representing the local knowledge used to build the n-gram model; Step 2: Based on the calculated probability distribution of the next token, sample and generate the next token. The sampling formula is: , where is the next token predicted based on the current existing token. is the probability distribution of the next word of the current token in the vocabulary, and argmax is the sampling function; Step 2: When inferring the n-gram language model, input the text token, output the probability distribution of the next token in the vocabulary, and sample and generate the next token based on the probability distribution of the next token, thereby continuously generating n tokens according to the user's question; Step 3: concatenate the generated n tokens with the prefix and input them into the large model to predict n+1 tokens; Step 4: Verify the n+1 tokens generated by the large model and the n tokens generated by the n-gram model in parallel; Step 5: Concatenate the selected result with the current prompt word prefix token as a new token sequence.
2. The large model reasoning acceleration method combining machine learning and speculative sampling according to claim 1 is characterized in that: The following steps are also included between step 1 and step 2: (1) Preprocess the user's question and call the genrerate function of the large model. The generate function method inherits the generate function in the GenerationMixin class of the transformers library. (2) After the data is passed into the generate function in the GenerationMixin class, the input tensor is , which is the prefix of the prompt word; (3) Select the sampling mode.
3. The large model reasoning acceleration method combining machine learning and speculative sampling according to claim 1 is characterized in that: The following steps are also included between step 1 and step 2: (1) After preprocessing, the user question is processed into a prompt word prefix and passed into the LLMEngine of the vLLM reasoning framework as a parameter; (2) After receiving the user request, LLM Engine processes the user's question into a request seq_group through the add_request method and adds it to the scheduler. The scheduler manages and dispatches the user request; (3) Execute reasoning through the step() method under the LLM Engine class; obtain the seq_groups to be executed and the token to be calculated from the scheduler; (4) Process the obtained data and convert it into an input tensor, i.e., the prompt word prefix.
4. The large model reasoning acceleration method combining machine learning and speculative sampling according to claim 3 is characterized in that: The process of scheduler managing and allocating user requests includes: arranging tasks that have not yet been completed to the large model for inference generation; and outputting tasks that have met the termination conditions to the user interface.
5. The large model inference acceleration method combining machine learning and speculative sampling according to any one of claims 1 to 4, characterized in that: The step three specifically includes the following process: The large model constructs input tokens based on the results generated by the current n-gram model, generating a probability distribution of n+1 tokens with a size of (n+1)*vocab size; vocab size is the size of the vocabulary. Parallel sampling is performed on the probability distribution generated by the large model to generate n+1 tokens. The parallel sampling formula is as follows: ; in, The n+1 tokens obtained by parallel sampling, The probability distribution of the generated n+1 tokens is (n+1) * vocab size. It is the sampling function of PyTorch.
6. The large model inference acceleration method combining machine learning and speculative sampling according to any one of claims 1 to 4, characterized in that: The parallel verification in step 4 includes the following process: If the generated tokens are exactly the same, the large model accepts the n tokens generated by the n-gram language model and the n+1th token generated by itself and outputs them; if the kth token among the n tokens generated by the n-gram is different from the token generated by the large model, the first k-1 tokens generated by the large model are selected as output.
7. A large model inference acceleration system combining machine learning and speculative sampling, comprising a memory, a processor, and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the large model inference acceleration method combining machine learning and speculative sampling as described in any one of claims 1-6.
Citation Information
Patent Citations
Accelerator hardware and acceleration method based on large language model speculation sampling reasoning
CN117933401A
Method and apparatus for speculative decompression
US20160321076A1
