Model inference method and apparatus
By performing positional encoding and average pooling compression on the attention head of the Transformer model, the problem of slow inference speed caused by excessive KV cache storage is solved, achieving faster model inference speed and lower storage requirements.
Patent Information
- Application Number
- CN202410980431.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2044-07-19
AI Technical Summary
As the size of the Transformer model increases, the amount of key-value information stored in the KV cache increases, leading to slower inference speed. Existing compression methods may lose some information, affecting the model's inference performance.
By positionally encoding the key vector of at least one attention head, positional information loss is avoided, and average pooling compression is performed on tokens with the same semantics but different positions to reduce the KV information content of the remaining attention heads.
While ensuring inference performance, reduce the storage requirements of the KV cache, improve the model inference speed and efficiency, and reduce the memory and computing requirements of the artificial intelligence chip.
Smart Images

Figure CN118674050B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a model reasoning method and apparatus. Background Technology
[0002] In recent years, with the rapid development of deep learning, the Transformer model (a neural network architecture based on self-attention mechanism) has achieved remarkable success in natural language processing and other fields. This model mainly consists of an attention mechanism and a feed-forward network (ffn). However, as the model size increases, inference efficiency becomes a critical issue.
[0003] In the traditional Transformer model, the corresponding key vector (K vector) and value vector (V vector) need to be stored in the key-value cache (KV cache) during each inference. However, as the inference length increases, the key-value information (KV information) stored in the KV cache will also increase, which will slow down the inference speed.
[0004] Currently, many key-value information in key-value caches are compressed in the hidden space or sequence dimension to reduce the amount of key-value information stored in the key-value cache. However, this method usually loses some information, which in turn affects the model's inference performance. Summary of the Invention
[0005] This invention provides a model reasoning method and apparatus to address the deficiencies in related technologies.
[0006] This invention provides a model reasoning method, comprising the following steps: Based on each attention head in the inference model, the query vector, key vector, and value vector output by each attention head are determined using the current input. After position encoding the key vector in at least one first attention head, the first attention calculation result of the first attention head is determined based on the position-encoded query vector, position-encoded key vector, and value vector of the first attention head. Based on the key vector, value vector, and position-encoded query vector of the other attention heads, the second attention calculation result of the other attention heads is determined; Based on the first attention calculation result and the second attention calculation result, model inference is performed to obtain the current inference result; In the process of determining the second attention calculation result, if it is determined that the current input corresponding word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
[0007] According to a model inference method provided by the present invention, if it is determined that the current input word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, including: If it is determined that the current input word segment is the same as the historical word segment, then the key value information of the current input word segment and the key value information of the historical word segment are averaged and pooled, and the averaged key value information is saved in the key value cache of the attention head corresponding to the second attention calculation result.
[0008] According to a model reasoning method provided by the present invention, the method further includes: During the process of determining the second attention calculation result, if the current input corresponding word does not have the same historical word, the key value information of the current input corresponding word is saved to the key value cache of the attention head corresponding to the second attention calculation result.
[0009] According to a model inference method provided by the present invention, the step of compressing the corresponding key-value information of the attention head corresponding to the second attention calculation result further includes: Based on the size of the compressed key-value information, the specification of the key-value cache required for the attention head corresponding to the second attention calculation result is determined, and the compressed key-value information is stored in the key-value cache of the corresponding specification.
[0010] According to a model inference method provided by the present invention, determining the first attention calculation result of the first attention head based on the position-encoded query vector, the position-encoded key vector, and the value vector of the first attention head includes: The attention weight of the first attention head is determined based on the query vector and the key vector after position encoding of the first attention head. Based on the attention weights of the first attention head, a weighted average is performed on the value vector of the first attention head to determine the first attention calculation result.
[0011] According to a model reasoning method provided by the present invention, the reasoning model is a text generation model, and the current input is determined based on the input text.
[0012] The present invention also provides a model inference device, comprising the following modules: The vector determination unit is used to determine the query vector, key vector, and value vector output by each attention head in the inference model, based on the current input. The first calculation unit is used to perform position encoding on the key vector in at least one first attention head, and then determine the first attention calculation result of the first attention head based on the position-encoded query vector, the position-encoded key vector, and the value vector of the first attention head. The second calculation unit is used to determine the second attention calculation result of the other attention heads based on the key vector, value vector and position-encoded query vector of the other attention heads; The model inference unit is used to perform model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; In the process of determining the second attention calculation result, if it is determined that the current input corresponding word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
[0013] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the model inference method as described above.
[0014] 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 the model inference method as described above.
[0015] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the model inference method as described above.
[0016] The model inference method and apparatus provided by this invention perform positional encoding on the K vector corresponding to at least one first attention head. This ensures that positional information is considered during attention calculation by the first attention head, preventing the loss of positional information from affecting inference performance. For the remaining attention heads, only the Q vector is positionally encoded, not the K and V vectors. Therefore, when the KV information of the current input token is determined to be redundant, the KV information of the remaining attention heads can be compressed to reduce the amount of KV information stored in the KV cache corresponding to the remaining attention heads. Since the KV cache corresponding to the remaining attention heads does not store positional information, important information in the KV cache is not discarded during compression; only highly redundant information is compressed, achieving near-lossless compression. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in this invention or related technologies, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a flowchart illustrating the model reasoning method provided by the present invention.
[0019] Figure 2 This is a schematic diagram of key-value information compression provided by the present invention.
[0020] Figure 3 This is a schematic diagram of the model reasoning device provided by the present invention.
[0021] Figure 4 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0022] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0023] In the traditional Transformer model, the corresponding K vector and V vector need to be stored in the KV cache during each inference. However, as the inference length increases, the KV information (i.e., KV matrix) stored in the KV cache will also increase, which will slow down the inference speed.
[0024] Currently, key-value (KV) information in multiple key-value (KV) caches is compressed along either the hidden or sequence-based dimensions. Compression along the hidden dimension results in lower-dimensional KV information that can be stored and expanded during computation, requiring less memory to store the KV cache information. Compression along the sequence-based dimension reduces multiple KV entries to one or fewer, or preserves only the most important KV entries while removing irrelevant ones, thus reducing the amount of KV information stored in the cache.
[0025] However, the above methods often lose some information, which may be important in certain task scenarios and thus affect the model's inference performance.
[0026] Figure 1 This is a flowchart illustrating the model reasoning method provided by the present invention, as shown below. Figure 1 As shown, the method includes steps 110, 120, 130 and 140.
[0027] Step 110: Based on each attention head in the inference model, apply the current input to determine the query vector, key vector, and value vector output by each attention head.
[0028] Here, the current input can be understood as the input token corresponding to the current inference stage, or as the output token corresponding to the previous inference stage. After determining the current input, the query vector (Q vector), key vector (K vector), and value vector (V vector) output by each attention head can be determined.
[0029] Step 120: After position encoding the key vector in at least one first attention head, determine the first attention calculation result of the first attention head based on the position-encoded query vector, position-encoded key vector, and value vector of the first attention head. Step 130: Based on the key vector, value vector, and position-encoded query vector of the other attention heads, determine the second attention calculation result of the other attention heads; Step 140: Based on the calculation results of the first attention and the second attention, perform model inference to obtain the current inference result; In determining the result of the second attention calculation, if it is determined that the current input word segment is the same as the historical word segment, the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
[0030] Specifically, KVcache is mainly used to store KV information corresponding to historical tokens. It includes not only the semantic information of the token itself, but also the token location information. Therefore, even if the tokens have the same semantics, they need to be saved separately in KVcache because they appear in different positions in the context.
[0031] A significant portion of the redundancy in KVcache stems from the repeated occurrence of tokens with the same semantics. For example, some frequently occurring words may appear in different positions within the context. Because the positional information differs, these words need to be saved multiple times in the KVcache. Directly compressing the KV information corresponding to tokens with the same semantic information but different positional information within the context would result in the loss of positional information.
[0032] For repeated semantic tokens, compression can be performed. However, to avoid the loss of positional information after compression, this embodiment of the invention performs positional encoding on the K vector corresponding to at least one first attention head. Based on the positionally encoded Q vector, the positionally encoded K vector, and the V vector, the first attention calculation result of at least one first attention head is determined. Thus, positional information is considered when at least one first attention head performs attention calculation, avoiding the loss of positional information from affecting the inference effect.
[0033] Furthermore, for the attention heads other than the first attention head, only the Q vector is positionally encoded, not the K and V vectors. Since the K and V vectors corresponding to the other attention heads are not positionally encoded, their corresponding KV caches also do not store positional information. If, during the attention calculation process of the other attention heads, the token corresponding to the current input is the same as a historical token (i.e., the semantic information of the token corresponding to the current input is the same as that of the historical token), it indicates that the KV information of the token corresponding to the current input is redundant. In this case, the KV information corresponding to the other attention heads can be compressed to reduce the amount of KV information stored in the KV cache of the other attention heads.
[0034] It should be noted that since the Q vector does not need to be saved to the KVcache, and therefore does not need to be compressed, all attention heads in this embodiment of the invention perform positional encoding on the Q vector when performing attention calculation, thereby introducing the corresponding positional information during attention calculation to ensure inference performance.
[0035] Furthermore, in this embodiment of the invention, the K vector corresponding to at least one first attention head is positionally encoded. This ensures that positional information is considered during attention calculation by the first attention head, preventing the loss of positional information from affecting inference performance. For the remaining attention heads, only the Q vector is positionally encoded, not the K and V vectors. Therefore, if the KV information of the current input token is determined to be redundant, the KV information of the remaining attention heads can be compressed to reduce the amount of KV information stored in the KVcache corresponding to the remaining attention heads. Since the KVcache corresponding to the remaining attention heads does not store positional information, important information in the KVcache is not discarded during compression; only highly redundant information is compressed, resulting in near-lossless compression.
[0036] Furthermore, the aforementioned inference model can be implemented on an artificial intelligence chip, such as a graphics processing unit (GPU), a general-purpose graphics processing unit (GPGPU), or a tensor processing unit (TPU). Implementing the model inference method of the above embodiments on an artificial intelligence chip significantly reduces storage usage and the number of computations required compared to existing model inference methods. Consequently, it effectively reduces the video memory of the artificial intelligence chip and lowers the computational power requirement. In other words, implementing the model inference method of the above embodiments on an artificial intelligence chip results in lower memory and computational overhead, and faster model inference speed.
[0037] The model inference method described in the above embodiments is not limited to text-based models; it is also applicable to image-based and audio-based models. Taking text-based models as an example, in multi-turn dialogue scenarios, a lot of historical information needs to be stored in the KVcache, and there is often a lot of semantic repetition in multi-turn dialogues. Based on the model inference method described in the above embodiments, information with the same semantics can be compressed, greatly reducing KVcache storage.
[0038] Based on any of the above embodiments, if it is determined that the current input word segmentation is the same as the historical word segmentation, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, including: If it is determined that the current input word segment is the same as the historical word segment, then the key value information of the current input word segment of the other attention heads is averaged with the key value information of the historical word segment, and the averaged key value information is saved in the key value cache of the attention head corresponding to the second attention calculation result.
[0039] Specifically, if the token corresponding to the current input is the same as the historical token, that is, the semantic information of the token corresponding to the current input is the same as that of the historical token, since the position information is not stored in the KVcache corresponding to the other attention heads, the KV information of the token corresponding to the current input is redundant when the semantic information of the token corresponding to the current input is the same as that of the historical token. At this time, the KV information of the attention head corresponding to the second attention calculation result can be compressed to reduce the amount of KV information stored in the KVcache corresponding to the attention head.
[0040] When compressing the key-value information of the attention head corresponding to the second attention calculation result, this embodiment of the invention performs average pooling (i.e., takes the average value of the two) on the key-value information of the current input corresponding to the token and the key-value information of the historical token, and saves the average pooled key-value information in the key-value cache of the corresponding attention head.
[0041] Therefore, for two tokens with the same semantics, this embodiment of the invention only saves one set of KV information, saving KVcache memory. The saved memory can then support inference of more batches of data, thus accelerating model inference efficiency.
[0042] Based on any of the above embodiments, the method further includes: In determining the result of the second attention calculation, if the corresponding word of the current input does not have the same historical word, the key-value information of the corresponding word of the current input is saved to the key-value cache of the attention head corresponding to the second attention calculation result.
[0043] Specifically, in the process of determining the second attention calculation result, if the token corresponding to the current input does not have the same historical token, it indicates that the KV information of the token corresponding to the current input is not redundant. In order to ensure the reasoning effect, the KV information of the token corresponding to the current input is saved to the KVcache of the attention head corresponding to the second attention calculation result.
[0044] Based on any of the above embodiments, the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, and then the process further includes: Based on the size of the compressed key-value information, the specification of the key-value cache required for the attention head corresponding to the second attention calculation result is determined, and the compressed key-value information is stored in the key-value cache of the corresponding specification.
[0045] Specifically, the specification of the key-value cache required for the attention head corresponding to the second attention calculation result is used to characterize the storage space size of the key-value cache required for the attention head corresponding to the second attention calculation result. After compressing the key-value information corresponding to the attention head corresponding to the second attention calculation result, the storage space of the key-value cache required for the attention head corresponding to the second attention calculation result becomes smaller, thereby allowing for the selection of lower-cost hardware to store the compressed key-value information. Therefore, this embodiment of the invention can dynamically select matching and lower-cost hardware to store the compressed key-value information based on its size.
[0046] Based on any of the above embodiments, the first attention calculation result of the first attention head is determined based on the query vector after position encoding, the key vector after position encoding, and the value vector of the first attention head, including: The attention weights of the first attention head are determined based on the query vector and the key vector after position encoding of the first attention head. Based on the attention weights of the first attention head, a weighted average is performed on the value vector of the first attention head to determine the first attention calculation result.
[0047] Optionally, based on the Q-vector and K-vector encoded by the position of the first attention head, the attention score of the first attention head can be determined. The attention score is then normalized using a softmax function to obtain the attention weight of the first attention head. Next, based on the attention weight of the first attention head, a weighted average is performed on the V-vector of the first attention head to determine the first attention calculation result.
[0048] Similarly, the second attention calculation results for the remaining attention heads are determined based on the following steps: Based on the Q-vector and K-vector encoded from the positions of the other attention heads, their attention scores can be determined. These scores are then normalized using a softmax function to obtain their attention weights. Next, based on these weights, a weighted average of the V-vectors from the other attention heads is calculated to determine the second attention calculation result for each attention head.
[0049] Based on any of the above embodiments, the inference model is a text generation model. The current input is determined based on the input text. The model inference is performed using the method of the above embodiments to obtain the output text corresponding to the input text.
[0050] Based on any of the above embodiments, the present invention also provides a model reasoning method, the method comprising: Assume the inference model comprises 16 attention heads, using the model's previous output token as the current input token. After encoding the current input token into a corresponding vector, the Q-vector, K-vector, and V-vector for each attention head are calculated based on the encoded vectors. Each attention head has its own KV cache.
[0051] Positional encoding is performed on the Q and K vectors of the first attention head, while the V vector is not. Attention calculation is then performed based on the positionally encoded Q and K vectors, as well as the V vector, to obtain the attention calculation result for the first attention head. For the remaining 15 attention heads, positional encoding is performed only on the Q vectors, while the K and V vectors are not. Attention calculation is then performed based on the positionally encoded Q, K, and V vectors to obtain the attention calculation results for the remaining attention heads.
[0052] Specifically, during the process of determining the attention calculation results of the other attention heads, it is determined whether the current input token is among the previous output tokens (i.e., whether the current input token has the same historical token). If not, the key-value information corresponding to the current input token is saved to the key-value cache. Otherwise, the key-value information in the key-value caches of the other attention heads is compressed. Optionally, when compressing the key-value information, the key-value information of the current input token can be averaged with the key-value information of the same historical token, and the averaged key-value information is saved in the corresponding key-value cache.
[0053] Figure 2 This is a schematic diagram of key-value information compression provided by the present invention, such as... Figure 2 As shown, head0 represents the first attention head, and head1 to head15 represent the remaining 15 attention heads. The token obtained in the 5th inference step in head1 is the same as the token obtained in the (n+1)th inference step. Therefore, the KV information of the token in the 5th step and the KV information of the token in the (n+1)th step can be averaged and pooled, and the averaged KV information is saved in the KV cache of head1. That is, for the KV information of two identical tokens, only one copy of the KV information needs to be saved in the KV cache.
[0054] The model reasoning apparatus provided by the present invention is described below. The model reasoning apparatus described below and the model reasoning method described above can be referred to in correspondence.
[0055] Based on any of the above embodiments Figure 3 This is a schematic diagram of the structure of the model inference device provided by the present invention, as shown below. Figure 3 As shown, the device includes: The vector determination unit 310 is used to determine the query vector, key vector and value vector output by each attention head based on the current input in the inference model. The first calculation unit 320 is used to perform position encoding on the key vector in at least one first attention head, and then determine the first attention calculation result of the first attention head based on the position-encoded query vector, the position-encoded key vector, and the value vector of the first attention head. The second calculation unit 330 is used to determine the second attention calculation result of the other attention heads based on the key vector, value vector and position-encoded query vector of the other attention heads; The model inference unit 340 is used to perform model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; In determining the result of the second attention calculation, if it is determined that the current input word segment is the same as the historical word segment, the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
[0056] Based on any of the above embodiments, if it is determined that the current input word segmentation is the same as the historical word segmentation, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, including: If it is determined that the current input word segment is the same as the historical word segment, then the key value information of the current input word segment and the key value information of the historical word segment are averaged and pooled, and the averaged key value information is saved in the key value cache of the attention head corresponding to the second attention calculation result.
[0057] Based on any of the above embodiments, the method further includes: In determining the result of the second attention calculation, if the corresponding word of the current input does not have the same historical word, the key-value information of the corresponding word of the current input is saved to the key-value cache of the attention head corresponding to the second attention calculation result.
[0058] Based on any of the above embodiments, the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, and then the process further includes: Based on the size of the compressed key-value information, the specification of the key-value cache required for the attention head corresponding to the second attention calculation result is determined, and the compressed key-value information is stored in the key-value cache of the corresponding specification.
[0059] Based on any of the above embodiments, the first attention calculation result of the first attention head is determined based on the query vector after position encoding, the key vector after position encoding, and the value vector of the first attention head, including: The attention weights of the first attention head are determined based on the query vector and the key vector after position encoding of the first attention head. Based on the attention weights of the first attention head, a weighted average is performed on the value vector of the first attention head to determine the first attention calculation result.
[0060] Based on any of the above embodiments, the inference model is a text generation model, and the current input is determined based on the input text.
[0061] Figure 4 This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 4As shown, the electronic device may include: a processor 410, a communications interface 420, a memory 430, and a communications bus 440, wherein the processor 410, the communications interface 420, and the memory 430 communicate with each other through the communications bus 440. The processor 410 can call logical instructions in the memory 430 to execute a model inference method, which includes: determining the query vector, key vector, and value vector output by each attention head based on the current input, using each attention head in the inference model; after positionally encoding the key vector in at least one first attention head, determining the first attention calculation result of the first attention head based on the positionally encoded query vector, positionally encoded key vector, and value vector of the first attention head; determining the second attention calculation result of the remaining attention heads based on the key vector, value vector, and positionally encoded query vector of the remaining attention heads; performing model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; wherein, in the process of determining the second attention calculation result, if it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the corresponding key-value information of the attention head corresponding to the second attention calculation result is compressed.
[0062] Furthermore, the logical instructions in the aforementioned memory 430 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to related technologies, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0063] On the other hand, the present invention also provides a computer program product, which includes a computer program that 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 inference method provided by the above methods. The method includes: determining the query vector, key vector, and value vector output by each attention head based on the current input, using each attention head in the inference model; after position encoding the key vector in at least one first attention head, determining the first attention calculation result of the first attention head based on the position-encoded query vector, position-encoded key vector, and value vector of the first attention head; determining the second attention calculation result of the remaining attention heads based on the key vector, value vector, and position-encoded query vector of the remaining attention heads; performing model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; wherein, in the process of determining the second attention calculation result, if it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the corresponding key-value information of the attention head corresponding to the second attention calculation result is compressed.
[0064] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program implements the model inference method provided by the above methods. The method includes: determining the query vector, key vector, and value vector output by each attention head based on the current input, using each attention head in the inference model; after positionally encoding the key vector in at least one first attention head, determining the first attention calculation result of the first attention head based on the positionally encoded query vector, positionally encoded key vector, and value vector of the first attention head; determining the second attention calculation result of the remaining attention heads based on the key vector, value vector, and positionally encoded query vector of the remaining attention heads; performing model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; wherein, in the process of determining the second attention calculation result, if it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the corresponding key-value information of the attention head corresponding to the second attention calculation result is compressed.
[0065] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0066] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the parts that contribute to the related technology, can be embodied in the form of software products. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0067] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A model reasoning method, characterized in that, include: Based on each attention head in the inference model, the query vector, key vector, and value vector output by each attention head are determined using the current input; the current input is determined based on the input text. After position encoding the key vector in at least one first attention head, the first attention calculation result of the first attention head is determined based on the position-encoded query vector, position-encoded key vector, and value vector of the first attention head. Based on the key vector, value vector, and position-encoded query vector of the other attention heads, the second attention calculation result of the other attention heads is determined; Based on the first attention calculation result and the second attention calculation result, model inference is performed to obtain the current inference result; In the process of determining the second attention calculation result, if it is determined that the current input corresponding word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
2. The model reasoning method according to claim 1, characterized in that, If it is determined that the current input word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, including: If it is determined that the current input word segment is the same as the historical word segment, then the key value information of the current input word segment and the key value information of the historical word segment are averaged and pooled, and the averaged key value information is saved in the key value cache of the attention head corresponding to the second attention calculation result.
3. The model reasoning method according to claim 1, characterized in that, The method further includes: During the process of determining the second attention calculation result, if the current input corresponding word does not have the same historical word, the key value information of the current input corresponding word is saved to the key value cache of the attention head corresponding to the second attention calculation result.
4. The model reasoning method according to claim 1, characterized in that, The step of compressing the key-value information corresponding to the attention head corresponding to the second attention calculation result further includes: Based on the size of the compressed key-value information, the specification of the key-value cache required for the attention head corresponding to the second attention calculation result is determined, and the compressed key-value information is stored in the key-value cache of the corresponding specification.
5. The model reasoning method according to claim 1, characterized in that, The determination of the first attention calculation result of the first attention head based on the query vector, key vector, and value vector after position encoding of the first attention head includes: The attention weight of the first attention head is determined based on the query vector and the key vector after position encoding of the first attention head. Based on the attention weights of the first attention head, a weighted average is performed on the value vector of the first attention head to determine the first attention calculation result.
6. The model reasoning method according to claim 1, characterized in that, The reasoning model is a text generation model.
7. A model reasoning device, characterized in that, include: The vector determination unit is used to determine the query vector, key vector, and value vector output by each attention head in the inference model, based on the current input; the current input is determined based on the input text. The first calculation unit is used to perform position encoding on the key vector in at least one first attention head, and then determine the first attention calculation result of the first attention head based on the position-encoded query vector, the position-encoded key vector, and the value vector of the first attention head. The second calculation unit is used to determine the second attention calculation result of the other attention heads based on the key vector, value vector and position-encoded query vector of the other attention heads; The model inference unit is used to perform model inference based on the first attention calculation result and the second attention calculation result to obtain the current inference result; In the process of determining the second attention calculation result, if it is determined that the current input corresponding word segment is the same as the historical word segment, then the corresponding key value information of the attention head corresponding to the second attention calculation result is compressed.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the model reasoning method as described in any one of claims 1 to 6.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the model reasoning method as described in any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the model reasoning method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Compression method of visual self-attention model based on multi-granularity reasoning
CN115983322A
Model reasoning method and device based on key value matrix cache and medium
CN118036754A
