Model reasoning method and device
By position encoding the attention head of the Transformer model and averaging pooling compression of KV information, the problem of low model reasoning efficiency is solved, the reasoning efficiency and accuracy are improved, and memory and computing requirements are reduced.
Patent Information
- Application Number
- CN202410980431.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2044-07-19
AI Technical Summary
As the scale of the Transformer model increases, the inference efficiency decreases. Existing methods compress the KV information in the KV cache in the latent space or sequence dimension, but some information is usually lost, affecting the model inference effect.
By positionally encoding the key vector of at least one attention head, the loss of position information is avoided, and when the current input has the same semantics as the historical input, the KV information of the remaining attention heads is average pooled and compressed to reduce the storage of the KV cache. quantity.
It effectively avoids the loss of location information, reduces the storage capacity of KV cache, improves the efficiency and accuracy of model reasoning, and reduces the memory and computing requirements of artificial intelligence chips.
Smart Images

Figure CN118674050A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a model reasoning method and device. Background Art
[0002] In recent years, with the rapid development of deep learning, the Transformer model (a neural network architecture based on the self-attention mechanism) has achieved remarkable achievements in natural language processing and other fields. The model mainly consists of an attention mechanism and a feed-forward network (FFN). However, as the model size increases, inference efficiency becomes a key issue.
[0003] In the traditional Transformer model, the corresponding key vector (Key vector, also known as K vector) and value vector (Value vector, also known as V vector) need to be stored in the key-value cache (KV cache) for each inference. However, as the inference length increases, the key-value information (KV information) stored in the KV cache will also increase, resulting in a slower inference speed.
[0004] Currently, multiple pairs of KV information in the KV cache are compressed in the hidden space dimension or sequence (seq) dimension to reduce the amount of KV information stored in the KV cache, but this method usually loses some information, which in turn affects the model reasoning effect. Summary of the invention
[0005] The present invention provides a model reasoning method and device to solve the defects existing in the related technology.
[0006] The present invention provides a model reasoning method, comprising the following steps: 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; After position encoding the key vector in at least one first attention head, determining a 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; Determine second attention calculation results of the remaining attention heads based on the key vectors, value vectors, and position-encoded query vectors of the remaining attention heads; Performing model reasoning based on the first attention calculation result and the second attention calculation result to obtain a current reasoning result; Among them, 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.
[0007] According to a model inference method provided by the present invention, if it is determined that the current input corresponding word segmentation 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, including: If it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the key value information of the word segmentation corresponding to the current input and the key value information of the historical word segmentation are average pooled, and the key value information after average pooling 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: In the process of determining the second attention calculation result, if the word segmentation corresponding to the current input does not have the same historical word segmentation, the key value information of the word segmentation corresponding to the current input is saved in 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 corresponding key value information of the attention head corresponding to the second attention calculation result is compressed, and then the method further includes: Based on the size of the compressed key-value information, determine the specifications of the key-value cache required for the attention head corresponding to the second attention calculation result, and store the compressed key-value information in a key-value cache of corresponding specifications.
[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 query vector after the position encoding of the first attention head, the key vector after the position encoding, and the value vector, includes: determining an attention weight of the first attention head based on the position-encoded query vector and the position-encoded key vector of the first attention head; Based on the attention weight 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 inference method provided by the present invention, the inference 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 reasoning device, comprising the following modules: A vector determination unit, for determining a query vector, a key vector, and a value vector output by each attention head based on each attention head in the inference model and applying the current input; a first calculation unit, configured to, after position encoding a key vector in at least one first attention head, determine a first attention calculation result of the first attention head based on a query vector after the position encoding, the key vector after the position encoding, and a value vector of the first attention head; A second calculation unit, configured to determine second attention calculation results of the remaining attention heads based on the key vectors, the value vectors, and the position-encoded query vectors of the remaining attention heads; A model reasoning unit, configured to perform model reasoning based on the first attention calculation result and the second attention calculation result to obtain a current reasoning result; Among them, 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.
[0013] 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.
[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 any of the model reasoning methods described above.
[0015] 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.
[0016] The model reasoning method and device provided by the present invention positionally encodes the K vector corresponding to at least one first attention head, and then the position information is considered when at least one first attention head performs attention calculation, thereby avoiding the loss of position information affecting the reasoning effect. For the remaining attention heads, only the Q vector is positionally encoded, and the K vector and the V vector are not positionally encoded. Then, when it is determined that the KV information of the token corresponding to the current input is redundant information, 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 position information is not stored in the KVcache corresponding to the remaining attention heads, the important information in the KVcache is not discarded when the KV information of the remaining attention heads is compressed, and only the highly redundant information is compressed, which is close to lossless compression. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] 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.
[0018] Figure 1 It is a flow chart of 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 It is a structural schematic diagram of the model reasoning device provided by the present invention.
[0021] Figure 4 It is a structural schematic diagram 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] In the traditional Transformer model, the corresponding K vector and V vector need to be stored in the KV cache each time inference is performed. However, as the inference length increases, the KV information (i.e., KV matrix) stored in the KV cache will increase, resulting in a slower inference speed.
[0024] Currently, many pairs of KV information in the KV cache are compressed in the hidden dimension or seq dimension. Among them, compression in the hidden dimension can obtain lower-dimensional KV information storage, which can be expanded during calculation, so that less memory can be used to store KVcache information. Compression in the seq dimension is to compress multiple KV information into one or fewer KV information for storage, or to save important KV information and remove some irrelevant KV information to reduce the amount of KV information stored in the KV cache.
[0025] However, the above methods usually lose some information, which may be important in certain task scenarios and thus affect the model reasoning effect.
[0026] Figure 1 It is a flow chart of the model reasoning method provided by the present invention, such as Figure 1 As shown, the method includes step 110 , step 120 , step 130 and step 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 reasoning stage, or the output token corresponding to the previous reasoning 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 a 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; Step 130: Determine the second attention calculation results of the remaining attention heads based on the key vectors, value vectors, and position-encoded query vectors of the remaining attention heads; Step 140: Perform model reasoning based on the first attention calculation result and the second attention calculation result to obtain a current reasoning result; Among them, 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.
[0030] Specifically, KVcache is mainly used to store KV information corresponding to historical tokens, including not only the semantic information of the token itself, but also the token position information. Therefore, even tokens with the same semantics need to be saved separately in KVcache because they appear in different positions in the context.
[0031] A large part of the redundancy in KVcache is reflected in the repeated appearance of the same semantic token. For example, some common words that appear frequently appear in different positions in the context. Due to different position information, they need to be saved in KVcache multiple times. If the KV information corresponding to the tokens with the same semantic information but different position information in the context is directly compressed, the position information will be lost.
[0032] The same semantic token that appears repeatedly can be compressed. However, in order to avoid the loss of position information after compression, an embodiment of the present invention performs position encoding on the K vector corresponding to at least one first attention head, and determines the first attention calculation result of at least one first attention head based on the position-encoded Q vector, the position-encoded K vector, and the V vector. Furthermore, the position information is taken into account when performing attention calculation at least one first attention head, thereby avoiding the loss of position information affecting the reasoning effect.
[0033] In addition, for the remaining attention heads except the first attention head, only the Q vector is positionally encoded, while the K vector and the V vector are not positionally encoded. Since the K vectors and the V vectors corresponding to the remaining attention heads are not positionally encoded, the position information is not saved in the KVcache corresponding to the remaining attention heads. If the token corresponding to the current input is the same as the historical token during the attention calculation process of the remaining attention heads, that is, 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 information. At this time, the KV information corresponding to the remaining attention heads can be compressed to reduce the amount of KV information stored in the KVcache corresponding to the remaining attention heads.
[0034] It should be noted that since the Q vector does not need to be saved in KVcache, and thus does not need to be compressed, all attention heads in the embodiment of the present invention position-encode the Q vector when performing attention calculations, and then introduce corresponding position information when performing attention calculations to ensure the reasoning effect.
[0035] In addition, in the embodiment of the present invention, the K vector corresponding to at least one first attention head is positionally encoded, and the position information is taken into account when the at least one first attention head performs attention calculation, so as to avoid the loss of position information affecting the reasoning effect. For the remaining attention heads, only the Q vector is positionally encoded, and the K vector and the V vector are not positionally encoded. Then, when it is determined that the KV information of the token corresponding to the current input is redundant information, 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 position information is not stored in the KVcache corresponding to the remaining attention heads, the important information in the KVcache is not discarded when the KV information of the remaining attention heads is compressed, and only the highly redundant information is compressed, which is close to lossless compression.
[0036] In addition, the carrier of the above-mentioned reasoning model can be an artificial intelligence chip, such as a graphics processing unit (GPU), a general-purpose graphics processing unit (GPGPU), a tensor processing unit (TPU), etc. The model reasoning method of the above-mentioned embodiment is implemented on an artificial intelligence chip. Compared with the existing model reasoning method, the storage occupied by the model reasoning method of the above-mentioned embodiment is greatly reduced, and the number of calculations required is also less. Accordingly, it can effectively reduce the video memory of the artificial intelligence chip and the demand for computing power is also lower. In other words, the model reasoning method of the above-mentioned embodiment is implemented on an artificial intelligence chip, which has less memory and computing overhead and faster model reasoning speed.
[0037] The model reasoning method of the above embodiment is not limited to text models, but is also applicable to image and audio models. Taking the text model as an example, in the scenario of multiple rounds of dialogue, a lot of historical information needs to be stored in KVcache, and there are often many semantic repetitions in multiple rounds of dialogue. Based on the model reasoning method of the above embodiment, 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 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, including: If it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the key value information of the word segmentation corresponding to the current input of the remaining attention heads is averaged and pooled with the key value information of the historical word segmentation, and the key value information after average pooling 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 location information is not stored in the KVcache corresponding to the other attention heads, and then when the semantic information of the token corresponding to the current input is the same as that of the historical token, the KV information of the token corresponding to the current input is redundant information. 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 corresponding attention head.
[0040] When compressing the KV information of the attention head corresponding to the second attention calculation result, an embodiment of the present invention average-pools the KV information of the token corresponding to the current input and the KV information of the historical token (i.e., takes the average of the two), and saves the key value information after average pooling in the key value cache of the corresponding attention head.
[0041] It can be seen that for two tokens with the same semantics, the embodiment of the present invention only saves one copy of KV information, saving KVcache memory. The saved memory can support the reasoning of more batches of data and speed up the model reasoning efficiency.
[0042] Based on any of the above embodiments, the method further includes: In the process of determining the second attention calculation result, if the current input corresponding word segmentation does not have the same historical word segmentation, the key value information of the current input corresponding word segmentation is saved in 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 current input corresponding token does not have the same historical token, it means that the KV information of the current input corresponding token is not redundant information. In order to ensure the reasoning effect, the KV information of the current input corresponding token is saved in the KVcache of the attention head corresponding to the second attention calculation result.
[0044] Based on any of the above embodiments, compressing the corresponding key value information of the attention head corresponding to the second attention calculation result, and then further comprising: Based on the size of the compressed key-value information, determine the specifications of the key-value cache required by the attention head corresponding to the second attention calculation result, and store the compressed key-value information in the key-value cache of the corresponding specifications.
[0045] Specifically, the specification of the key-value cache required by the attention head corresponding to the second attention calculation result is used to characterize the size of the storage space of the key-value cache required by 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 by the attention head corresponding to the second attention calculation result becomes smaller, and then lower-cost hardware can be selected to store the compressed key-value information. It can be seen that the embodiment of the present invention can dynamically select matching and lower-cost hardware to store the compressed key-value information based on the size of the compressed key-value information.
[0046] Based on any of the foregoing embodiments, determining a 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: Determine an attention weight of the first attention head based on the position-encoded query vector and the position-encoded key vector of the first attention head; Based on the attention weight of the first attention head, the value vector of the first attention head is weighted averaged to determine the first attention calculation result.
[0047] Optionally, based on the Q vector and K vector of the position encoding of the first attention head, the attention score of the first attention head can be determined, and the attention score of the first attention head is normalized by the softmax function to obtain the attention weight of the first attention head. Then, based on the attention weight of the first attention head, the V vector of the first attention head is weighted averaged to determine the first attention calculation result.
[0048] Similarly, the second attention calculation results of the remaining attention heads are determined based on the following steps: Based on the Q vector and K vector after position encoding of the remaining attention heads, the attention scores of the remaining attention heads can be determined, and the attention scores of the remaining attention heads are normalized by the softmax function to obtain the attention weights of the remaining attention heads. Then, based on the attention weights of the remaining attention heads, the V vectors of the remaining attention heads are weighted averaged to determine the second attention calculation results of the remaining attention heads.
[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, and the model is inferred using the method of the above embodiment to obtain the output text corresponding to the input text.
[0050] Based on any of the above embodiments, the present invention further provides a model reasoning method, the method comprising: Assume that the inference model includes 16 attention heads in total, and use the last output token of the inference model as the current input token. After encoding the current input token into the corresponding vector, the Q vector, K vector, and V vector corresponding to each attention head are calculated based on the encoded vector. Each attention head has its own KV cache.
[0051] The Q and K vectors of the first attention head are position-encoded, while the V vector is not position-encoded. Then, attention calculation is performed based on the position-encoded Q and K vectors, as well as the V vector, to obtain the attention calculation result of the first attention head. For the remaining 15 attention heads, only the Q vector is position-encoded, while the K and V vectors are not position-encoded. Then, attention calculation is performed based on the position-encoded Q vector, as well as the K and V vectors, to obtain the attention calculation results of the remaining attention heads.
[0052] Among them, in the process of determining the attention calculation results of the remaining attention heads, determine whether the current input token is in the previous output token (that is, whether the current input token has the same historical token). If not, save the KV information corresponding to the current input token to the KV cache. If not, compress the KV information in the KV cache of the remaining attention heads. Optionally, when compressing the KV information, the KV information of the current input token and the KV information of the same historical token can be averaged and pooled, and the averaged pooled KV information is saved in the corresponding KV cache.
[0053] Figure 2 It is a schematic diagram of key value information compression provided by the present invention, such as Figure 2 As shown in the figure, head0 represents the first attention head, head1~head15 represent the remaining 15 attention heads, and the token obtained by the 5th step reasoning in head1 is the same as the token obtained by the n+1th step reasoning. Then, the KV information of the 5th step token and the KV information of the n+1th step token can be average pooled and saved in the KV cache of head1. That is, for the KV information of 2 identical tokens, only one KV information needs to be saved in the KV cache.
[0054] 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.
[0055] Based on any of the above embodiments, Figure 3 is a schematic diagram of the structure of the model reasoning device provided by the present invention, such as Figure 3 As shown, the device comprises: A vector determination unit 310, for determining a query vector, a key vector, and a value vector output by each attention head based on each attention head in the inference model and applying the current input; A first calculation unit 320 is configured to, after position encoding the key vector in at least one first attention head, determine a 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; A second calculation unit 330, configured to determine second attention calculation results of the remaining attention heads based on the key vectors, value vectors, and position-encoded query vectors of the remaining attention heads; A model reasoning unit 340, configured to perform model reasoning based on the first attention calculation result and the second attention calculation result to obtain a current reasoning result; Among them, 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.
[0056] Based on any of the above embodiments, 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, including: If it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the key value information of the word segmentation corresponding to the current input and the key value information of the historical word segmentation are averaged and pooled, and the key value information after average pooling 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 the process of determining the second attention calculation result, if the current input corresponding word segmentation does not have the same historical word segmentation, the key value information of the current input corresponding word segmentation is saved in the key value cache of the attention head corresponding to the second attention calculation result.
[0058] Based on any of the above embodiments, compressing the corresponding key value information of the attention head corresponding to the second attention calculation result, and then further comprising: Based on the size of the compressed key-value information, determine the specifications of the key-value cache required by the attention head corresponding to the second attention calculation result, and store the compressed key-value information in the key-value cache of the corresponding specifications.
[0059] Based on any of the foregoing embodiments, determining a 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: Determine an attention weight of the first attention head based on the position-encoded query vector and the position-encoded key vector of the first attention head; Based on the attention weight of the first attention head, the value vector of the first attention head is weighted averaged 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 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 (processor) 410 , a communication interface (Communications Interface) 420 , a memory (memory) 430 and a communication bus 440 , wherein the processor 410 , the communication interface 420 , and the memory 430 communicate with each other through the communication bus 440 . The processor 410 can call the logic instructions in the memory 430 to execute the model reasoning method, which includes: based on each attention head in the reasoning model, applying the current input, determining the query vector, key vector and value vector output by each attention head; 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, the position-encoded key vector and the value vector of the first attention head; based on the key vector, value vector and position-encoded query vector of the remaining attention heads, determining the second attention calculation results of the remaining attention heads; based on the first attention calculation result and the second attention calculation result, performing model reasoning to obtain the current reasoning 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] In addition, the logic instructions in the above-mentioned memory 430 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 this understanding, the technical solution of the present invention, in essence, 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.
[0063] On the other hand, the present invention also provides a computer program product, which includes a computer program, which 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, which includes: based on each attention head in the reasoning model, applying the current input, determining the query vector, key vector and value vector output by each attention head; 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, the position-encoded key vector and the value vector of the first attention head; determining the second attention calculation results of the remaining attention heads based on the key vectors, value vectors and position-encoded query vectors of the remaining attention heads; performing model reasoning based on the first attention calculation result and the second attention calculation result to obtain the current reasoning 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] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented by a processor to execute the model reasoning method provided by the above-mentioned methods when the computer program is executed, the method comprising: based on each attention head in the reasoning model, applying the current input, determining the query vector, key vector and value vector output by each attention head; 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, the position-encoded key vector and the value vector of the first attention head; determining the second attention calculation results of the remaining attention heads based on the key vectors, value vectors and the position-encoded query vectors of the remaining attention heads; performing model reasoning based on the first attention calculation result and the second attention calculation result to obtain the current reasoning 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, 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.
[0066] 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.
[0067] 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, characterized in that: include: 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; After position encoding the key vector in at least one first attention head, determining a 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; Determine second attention calculation results of the remaining attention heads based on the key vectors, value vectors, and position-encoded query vectors of the remaining attention heads; Based on the first attention calculation result and the second attention calculation result, perform model reasoning to obtain a current reasoning result; Among them, 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.
2. The model reasoning method according to claim 1, characterized in that: If it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, compressing the corresponding key value information of the attention head corresponding to the second attention calculation result includes: If it is determined that the word segmentation corresponding to the current input is the same as the historical word segmentation, the key value information of the word segmentation corresponding to the current input and the key value information of the historical word segmentation are average pooled, and the key value information after average pooling 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 comprises: In the process of determining the second attention calculation result, if the word segmentation corresponding to the current input does not have the same historical word segmentation, the key value information of the word segmentation corresponding to the current input is saved in 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 of the second attention calculation result further includes: Based on the size of the compressed key-value information, determine the specifications of the key-value cache required for the attention head corresponding to the second attention calculation result, and store the compressed key-value information in a key-value cache of corresponding specifications.
5. The model reasoning method according to claim 1, characterized in that: The determining, based on the position-encoded query vector, the position-encoded key vector, and the value vector of the first attention head, a first attention calculation result of the first attention head comprises: determining an attention weight of the first attention head based on the position-encoded query vector and the position-encoded key vector of the first attention head; Based on the attention weight 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 inference model is a text generation model, and the current input is determined based on input text.
7. A model reasoning device, characterized in that: include: A vector determination unit, for determining a query vector, a key vector, and a value vector output by each attention head based on each attention head in the inference model and applying the current input; a first calculation unit, configured to, after position encoding a key vector in at least one first attention head, determine a first attention calculation result of the first attention head based on a query vector after the position encoding, the key vector after the position encoding, and a value vector of the first attention head; A second calculation unit, configured to determine second attention calculation results of the remaining attention heads based on the key vectors, the value vectors, and the position-encoded query vectors of the remaining attention heads; A model reasoning unit, configured to perform model reasoning based on the first attention calculation result and the second attention calculation result to obtain a current reasoning result; Among them, 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.
8. 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 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, 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, 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
Patent Citations
Data query method and device, computer equipment and storage medium
CN114117242A
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
Method for temporal knowledge graph reasoning based on distributed attention
US20230401466A1
Welcoming robot conversation method
WO2023273170A1
Cited By
Large-scale language model KV Cache optimization method based on recent query attention information
CN119396995A
Data reasoning method, network device, medium, electronic device and program product
CN119783680A
Method and device for generating reasoning result, storage medium and electronic equipment
CN120930810A
Data processing method and server
CN121331107A
A data processing method and server
CN121331107B
