Large language model data management method and device, electronic equipment and storage medium

By caching and accumulating the target matrix in the large language model to calculate the key vector and value vector of the previous round, the problem of increased memory data bandwidth during the inference process of the large language model is solved, and storage space and efficiency are improved.

CN118709781APending Publication Date: 2024-09-27VERISILICON MICROELECTRONICS (SHANGHAI) CO LTD +5

Patent Information

Application Number
CN202410829844.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-25
Publication Date
2024-09-27

AI Technical Summary

Technical Problem

During the inference process of large language models, as the number of inferences increases, the amount of kv-cache data and data bandwidth in the memory increases, affecting the efficiency and performance of the device.

Method used

In non-first round of reasoning, the target matrix is ​​output to the attention layer, and the key vector and value vector of the previous round are calculated by caching the initial vector and accumulated vector, and the target matrix is ​​accumulated in the storage unit to reduce the storage of key vectors and values. The vector requirements are processed using the multiplication and accumulation operation function of the preset network layer. If the target matrix is ​​too large, it will be compressed and stored.

Benefits of technology

It effectively reduces the storage space and data bandwidth required for memory cache data, improves the inference efficiency and accuracy of large language models, and makes full use of hardware computing resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118709781A
    Figure CN118709781A
Patent Text Reader

Abstract

The invention aims to provide a big language model data management method and device, electronic equipment and a storage medium, and relates to the field of neural networks. The large language model data management method comprises the steps of outputting a target matrix to an attention layer under the condition that the attention layer performs non-first round of reasoning, so that the attention layer calculates a key vector and a value vector generated by the previous round of reasoning based on the target matrix; wherein the target matrix is stored in the storage unit, and the target matrix comprises an initial vector and N accumulation vectors; and after the attention layer completes the reasoning of the current round based on the key vector and the value vector, accumulating the target matrix in the storage unit based on the accumulation vector. In this way, the amount of data needing to be cached by the memory in the continuous reasoning process of the large language model can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of neural networks, and more specifically, to a large language model data management method, device, electronic device and storage medium. Background Art

[0002] During the reasoning process, the large language model (LLM) first converts the input text into multiple tokens (the basic unit of model input), and the model predicts the next token based on multiple tokens.

[0003] During the inference process, the large language model will cache the key (key vector) and value (value vector) calculated based on the attention mechanism to obtain kv-cache (cache of key vector and value vector). During the next inference process, the next token will be predicted based on the token output by the previous prediction, the key and value in the kv-cache, and the key and value in the kv-cache will be updated. When updating, new data is accumulated on the basis of the original key and value.

[0004] The kv-cache is stored in the memory. As the number of inferences increases, the amount of data accumulated in the key and value of the kv-cache increases during the continuous inference process, which increases the storage capacity and the data bandwidth of the kv-cache in the memory, affecting the efficiency of the device running large language models and the performance of the device. Summary of the invention

[0005] In view of this, the present application aims to provide a large language data management method, device, electronic device and storage medium to reduce the data bandwidth required for a memory to cache large language model cache data.

[0006] In a first aspect, an embodiment of the present application provides a large language model data management method, which is applied to a processor of an electronic device, the processor being configured with a large language model including an attention layer, and the electronic device also including a storage unit, the large language model data management method comprising: when the attention layer performs non-first-round reasoning, outputting a target matrix to the attention layer, so that the attention layer calculates a key vector and a value vector generated by a previous round of reasoning based on the target matrix; wherein: the target matrix is ​​stored in the storage unit, the target matrix includes an initial vector and N accumulated vectors, N=M-2, M is the current reasoning round of the attention layer, and M is greater than or equal to 2; the initial vector is intermediate data generated by the large language model during the first round of reasoning based on the input text and used to infer the key vector and the value vector; the accumulated vector is the fixed data required to be added to infer the key vector and the value vector of this round when the attention layer performs non-first-round reasoning; after the attention layer completes the current round of reasoning based on the key vector and the value vector, the target matrix in the storage unit is accumulated based on the accumulated vector.

[0007] In a large language model, the key vector and value vector of non-first-round reasoning are calculated by the same vector, that is, the intermediate data (initial vector) generated by the first-round reasoning, and the data used for accumulating the key vector and value vector in each round of reasoning (that is, the accumulated vector) are the same. Therefore, in an embodiment of the present application, the initial vector and accumulated vector required for calculating the key vector and value vector can be cached, that is, the target matrix is ​​cached, and the target matrix is ​​accumulated through the accumulated vector, so that the target matrix can infer the key vector and value vector used in the previous round of reasoning. Compared with separately caching the key vector and the value vector, caching the key vector and the value vector separately requires two storage spaces, while storing the target matrix only requires one storage space, that is, the storage space required to cache the target matrix can be halved compared to the prior art, thereby effectively reducing the data bandwidth required for the vector in the memory cache reasoning process.

[0008] In one embodiment, the large language model also includes a preset network layer located before the attention layer; the preset network layer is a network layer having a multiplication-accumulation operation function, but the multiplication-accumulation operation function is not enabled; outputting the target matrix to the attention layer includes: outputting the target matrix to the preset network layer, so as to process the target matrix through the multiplication-accumulation operation function in the preset network layer to obtain a calculation result; and outputting the calculation result to the attention layer.

[0009] The multiplication-accumulation operation function is executed by the hardware computing unit in the processor. The multiplication-accumulation operation function is not enabled in some network layers in the large language model, which makes insufficient use of the hardware computing unit resources by the network layer. In the embodiment of the present application, the target matrix is ​​output to a preset network layer that has a multiplication-accumulation operation function but the multiplication-accumulation operation function is not enabled. This allows the preset network layer to process the target matrix based on the multiplication-accumulation operation function to obtain a calculation result, thereby making full use of the hardware computing resources, so that caching the target matrix will not increase the amount of additional calculations and data reading.

[0010] In one embodiment, the preset network layer includes a fully connected layer before the attention layer.

[0011] In the large language model, the network layer before the attention layer is a fully connected layer, and the multiplication and accumulation function is not enabled in the fully connected layer. Therefore, in the embodiment of the present application, the target matrix that has not been calculated by the fully connected layer can be stored, and the target matrix can be output to the fully connected layer during reasoning, so as to calculate the data required to output to the attention layer through the fully connected layer. In this way, the hardware computing resources corresponding to the fully connected layer can be fully utilized, and the amount of cached data required can be reduced without additionally affecting the reasoning efficiency of the large language model.

[0012] In one embodiment, the electronic device also includes a data compression and decompression unit, and the large language model data management method also includes: if the size of the target matrix stored in the storage unit is greater than a preset threshold, compressing the target matrix based on the data compression and decompression unit to obtain compressed data; and before outputting the target matrix to the attention layer, the method also includes: decompressing the compressed data based on the data compression and decompression unit to obtain the target matrix.

[0013] In the embodiment of the present application, compressing the target matrix can further reduce the storage space required to store the target matrix.

[0014] In one embodiment, the method further includes: obtaining an input vector generated by the attention layer inferring the basic unit of the model input during the first round of reasoning; the basic unit of the model input is obtained by converting the text input into the large language model; and storing the input vector in the storage unit as the initial vector.

[0015] In the first round of reasoning of the large language model, the large language model will convert the text to obtain the basic units of the model input, and then infer the basic units of the model input to generate an input vector, which is used to calculate the key vector and the value vector. In the embodiment of the present application, the input vector is stored in the storage unit as the initial vector, which can effectively reduce the amount of data required to be stored compared to saving the value vector and key vector generated by the first round of reasoning.

[0016] In one embodiment, the storage unit includes a first memory and a second memory, the first memory is used to store the target matrix; the accumulation of the target matrix based on the accumulation vector includes: storing an accumulation vector into the second memory; after the attention layer completes the current round of reasoning, storing the accumulation vector in the second memory into the first memory to accumulate the target matrix.

[0017] In an embodiment of the present application, by storing the target matrix and the accumulated vectors required in the current round respectively in different memories, the accumulation of the target matrix can be carried out in an orderly manner, the possibility of errors in the accumulation of the target matrix can be reduced, and the accuracy of reasoning of the large language model can be improved.

[0018] In one embodiment, the capacity of the second memory is greater than a preset capacity; the electronic device also includes: a GEMM (General Matrix Multiplication) unit and a DMA (Direct Memory Access) unit; the attention layer is also used to perform general matrix matrix multiplication on the key vector and the value vector based on the GEMM unit; the outputting of the target matrix to the attention layer includes: reading data slices according to preset weights for the target matrix in the first memory, and outputting the data slices to the second memory; outputting the data slices from the second memory to the attention layer, so that the attention layer obtains the key vector and value vector based on the inference of the data slices, and calls the GEMM unit to perform matrix multiplication on the key vector and the value vector corresponding to the data slice; in the process of the GEMM unit performing matrix multiplication, based on the DMA unit, the next data slice is read out from the first memory according to the preset weight and stored in the second memory.

[0019] When the large language model is inferring, the GEMM unit is called to perform matrix multiplication on the key vector and the value vector, and data reading is implemented through the DMA unit. The two can run independently. Therefore, in the embodiment of the present application, when the GEMM unit is performing operations, the data in the first memory is read to the second memory through the DMA unit, and data loading and operation are performed simultaneously, which can effectively hide the delay caused by data reading and improve the operation efficiency of the large language model.

[0020] In a second aspect, an embodiment of the present application provides a processor configured with a large language model including an attention layer; the processor is used to execute the large language model data management method as described in any one of the first aspects.

[0021] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a processor, configured with a large language model including an attention layer; the processor is used to execute the large language model data management method as described in any one of the first aspects; a storage unit, connected to the processor; and used to store the target matrix required by the large language model during the reasoning process.

[0022] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium. When the computer program runs on a computer, the computer executes the large language model data management method as described in any one of the first aspects. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments of the present application will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without paying any creative work.

[0024] Figure 1 A flowchart of a large language model data management method provided in one embodiment of the present application;

[0025] Figure 2 A schematic diagram of a target matrix provided in an embodiment of the present application;

[0026] Figure 3 A schematic diagram of large language model reasoning provided in one embodiment of the present application;

[0027] Figure 4 A comparative schematic diagram provided for an embodiment of the present application;

[0028] Figure 5 A schematic diagram of a large language model data management device provided in one embodiment of the present application;

[0029] Figure 6 A schematic diagram of an electronic device provided according to an embodiment of the present application.

[0030] Icon: large language model data management device 400; output module 410; accumulation module 420; electronic device 500; processor 510; storage unit 520; first memory 521; second memory 522; data compression and decompression unit 530. DETAILED DESCRIPTION

[0031] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0032] First, to facilitate understanding, the reasoning process of the large language model is explained.

[0033] After receiving the input text, the large language model will perform the first round of reasoning, first converting the input text into multiple tokens (the basic unit of model input) at a time. The attention layer of the large language model will predict the next token based on the multiple tokens converted from the input text, and in the process of reasoning the next token, the Attention layer will calculate the Key (key vector) and Value (value vector). When calculating the value vector and the key vector, the information in the token is extracted to obtain the same input vector required to calculate the value vector and the key vector. For example, the input vector is vector A, and then the value vector and the key vector are calculated using different matrices and vector A. For example, the key vector is calculated by vector A and a fixed vector B, and the value vector is calculated by vector A and a fixed vector C. The prior art is to directly save the value vector and the key vector, and save the calculation results of vector A and vector B and the calculation results of vector A and vector C respectively.

[0034] After the token is obtained through reasoning, the large language model will perform the next round of predictive reasoning. For example, in the second round of reasoning, based on the originally saved key vector and value vector, the same vector D will be used to calculate the data required to accumulate the value vector and key vector of this round of reasoning, and after the data is accumulated to the originally saved key vector and value vector, the token of this round is inferred based on the accumulated value vector and key vector to complete the current round of speculation. Subsequent reasoning is analogous. That is, the prior art saves the value vector and key vector separately, and the value vector and key vector each require a storage space, requiring a total of two storage spaces.

[0035] An embodiment of the present application provides a large language model data management method, which can be applied to an electronic device, which is configured with a large language model, the large language model includes an attention layer, and the electronic device also includes a storage unit to store data generated by the large language model during the reasoning process.

[0036] In addition, the electronic device may also include peripheral circuits with other functions, the specific structure of which will be described later and will not be repeated here.

[0037] See also Figure 1 , Figure 1A flowchart of a large language model data management method is provided for an embodiment of the present application. The large language model data management includes:

[0038] S110, when the attention layer performs non-first-round reasoning, output the target matrix to the attention layer, so that the attention layer calculates the key vector and value vector generated by the previous round of reasoning based on the target matrix.

[0039] In an embodiment of the present application, a target matrix is ​​stored in a storage unit, and the target matrix includes an initial vector and N accumulated vectors, where N=M-2, M is the current reasoning round of the attention layer, and M is greater than or equal to 2.

[0040] The initial vector is the intermediate data generated by the large language model during the first round of reasoning based on the input text and used to infer the key vector and value vector. Taking the above content as an example, the initial vector is the input vector extracted from the token obtained by converting the input text, that is, vector A.

[0041] The accumulated vector is the fixed data required to add the key vector and value vector of the round when the attention layer is not the first round of reasoning. Taking the above content as an example, the accumulated vector can be the vector D in the above content.

[0042] It can be understood that in the embodiment of the present application, what is cached are the input vectors required by the calculated value vector and the key vector, and all the required input vectors constitute the target matrix cached in the storage unit of the present application.

[0043] See also Figure 2 , Figure 2 A schematic diagram of a target matrix provided in an embodiment of the present application. Figure 2 In the figure, the gray part represents the data that needs to be stored in the storage unit for this round of reasoning, and the white part represents the data that needs to be used for this round of calculation. Figure 2 All accumulation vectors shown have the same value.

[0044] In the large language model reasoning process, the initial vector used can only be obtained after the first round of reasoning is completed. Therefore, in the embodiment of the present application, there is no target matrix in the first round of reasoning, and the target matrix is ​​output to the attention layer for calculation only in the non-first round of reasoning. Figure 2As shown, the initial vector is obtained in the first round of reasoning, and the initial vector is stored in the storage unit. In the second round of reasoning, the initial vector is output to the attention layer to infer the value vector and key vector of the first round of reasoning, and is combined with the accumulated vector for calculation to obtain the value vector and key vector of the second round of reasoning. In the third round of reasoning, the initial vector and an accumulated vector are output to the attention layer again, and the key vector and value vector used in the second round are calculated. Finally, the key vector and value vector are accumulated using the calculation results of the accumulated vector to calculate the key vector and value vector used in the third round of reasoning, and so on. When the Mth round of operations is executed, the initial vector and (M-2) accumulated vectors are used to calculate the key vector and value vector used in the M-1th round.

[0045] Therefore, by outputting the target matrix to the attention layer, the attention layer can calculate the key vector and value vector of the previous round of reasoning, and thus perform the current round of reasoning based on the calculated value vector and key vector. The storage space required to save the target matrix is ​​similar to the space required to save only the value vector or only the key vector. The prior art requires saving the value vector and the key vector separately, which is equivalent to requiring two storage spaces. Compared with storing the value vector and the key vector separately, since the target matrix is ​​the data required for calculating both the value vector and the key vector, only one copy is required. Therefore, the method of storing the target matrix provided in the present application is equivalent to requiring only one storage space, and the required memory space is approximately half of the memory space required to store the value vector and the key vector separately.

[0046] Accordingly, in some embodiments, the initial vector can be obtained in the following manner: when obtaining the first round of reasoning, the attention layer infers the input vector generated by the basic unit of the model input; the basic unit of the model input is obtained by converting the text of the input large language model, and the input vector is stored in the storage unit as the initial vector.

[0047] The large language model includes multiple network layers. In addition to the attention layer, it can also include a fully connected layer, a convolutional layer, a pooling layer, etc. The specific structure can refer to the prior art and will not be expanded here. In the processor of the electronic device, different hardware units (referring to the circuit in the processor) are included, for example, GEMM unit, DMA unit, MAC (Multiply Accumulate) unit, etc., which are used to implement different calculations or other functions required by the large language model.

[0048] Different network layers will call different hardware units for calculations. For example, the attention layer will call the GEMM unit for calculations. In fact, some network layers do not fully utilize or saturate the hardware units during calculations, which may lead to bandwidth bottlenecks and fail to improve reasoning efficiency. For example, some network layers only calculate input vectors, and the reading and calculation efficiency of a single vector is low. MAC can calculate matrices including multiple vectors, thereby effectively improving data reading and calculation efficiency.

[0049] Therefore, in some embodiments of the present application, before outputting the target matrix to the attention layer, the target matrix can be output to the preset network layer first, so that the target matrix can be read and processed through the multiplication and accumulation operation function in the preset network layer to obtain the calculation result; and then the calculation result is output to the attention layer.

[0050] In this embodiment, since the matrix is ​​directly output to the preset network layer for calculation, compared with calculating a single vector, the efficiency of data reading and calculation can be effectively improved. At the same time, since the MAC function is not enabled in the preset network layer, the target matrix is ​​read and processed through the MAC operation function in the preset network layer, which fully utilizes the hardware resources and will not excessively affect the computing efficiency of the processor.

[0051] In the large language model, the network layer before the attention layer is a fully connected layer, and the fully connected layer does not use the MAC operation function. Therefore, in one embodiment of the present application, the preset network layer may include the fully connected layer before the attention layer.

[0052] In addition, if the preset network layer is not a fully connected layer before the attention layer, then accordingly, the target matrix is ​​input into the preset network layer, so as to obtain the calculation result after calculation through the preset network layer and the network layer between the preset network layer and the attention layer, and then the calculation result is output to the attention layer.

[0053] S120, after the attention layer completes the current round of reasoning based on the key vector and the value vector, the target matrix in the storage unit is accumulated based on the accumulation vector.

[0054] See also Figure 2 In this embodiment, each time a round of reasoning is completed, an accumulation vector is stored in the storage unit to accumulate the target matrix in the storage unit, so that the accumulated target matrix can calculate the key vector and value vector used in the previous round of reasoning when a new round of reasoning is performed.

[0055] In an embodiment of the present application, the storage unit may include a first memory and a second memory. The above-mentioned accumulation process may include: storing an accumulation vector in the second memory; after the attention layer completes the current round of reasoning, storing the accumulation vector in the second memory in the first memory to accumulate the target matrix.

[0056] In this embodiment, the first memory may be DDR (Double Data Rate, double data rate synchronous dynamic random access memory), and the second memory may be SRAM.

[0057] The first memory is used to store the target matrix. In the processor, the target matrix can be controlled to be output from the first memory to the second memory first, and then the target matrix is ​​read from the second memory to perform the reasoning of the attention layer. When performing the reasoning of the attention layer, an accumulation vector can be stored in the second memory, and after the attention layer completes the reasoning, the accumulation vector in the second memory is stored in the first memory. In this way, the accumulation vector can be stored during the reasoning process to reduce the time required for data storage. On the other hand, the accumulation error of the target matrix of the storage unit can be effectively reduced. The target matrix in the first memory is accumulated only after the reasoning is completed, so that the accumulation is carried out in an orderly manner, reducing the possibility of error in the accumulation of the target matrix.

[0058] See also Figure 3 , Figure 3 A schematic diagram of large language model reasoning provided for an embodiment of the present application. In an embodiment of the present application, after the target matrix is ​​output to the fully connected layer, the fully connected layer calculates the target matrix based on the MAC operation, and outputs the calculation result to the attention layer, and performs operations on the query vector, key vector, and value vector respectively. In the attention layer, the key vector will also perform a GEMM operation with the query vector to obtain the first operation result, and then the first operation result and the value vector will perform a GEMM operation to obtain the reasoning result of this round.

[0059] The processor may include a GEMM unit for performing GEMM operations and a DMA unit for obtaining a target matrix from a first memory. The GEMM unit and the DMA unit may operate independently. In some embodiments of the present application, data reading and reasoning in the attention layer may be performed simultaneously, so that subsequent data loading may be performed while the current data is performing GEMM operations, thereby reducing data reading and writing delays.

[0060] In one embodiment of the present application, outputting the target matrix to the attention layer includes: reading data slices for the target matrix in the first memory according to preset weights, and outputting the data slices to the second memory; outputting the data slices from the second memory to the attention layer, so that the attention layer obtains the key vector and value vector based on the inference of the data slices, and calls the GEMM unit to perform matrix multiplication on the key vector and value vector corresponding to the data slice; in the process of the GEMM unit performing matrix multiplication, the next data slice is read out from the first memory according to the preset weight based on the DMA unit and stored in the second memory.

[0061] In a large language model, data has multiple dimensions, and accordingly, the target matrix also includes multiple dimensions. Cutting on one of the dimensions can obtain multiple data slices, and the results of multiple data slices output to the attention layer for operation are the same. The operation results of multiple data slices are added together to obtain the operation result of the target matrix. Therefore, in an embodiment of the present application, the target matrix can be cut according to preset weights to obtain data slices, so that the DMA unit reads the data slices from the first memory.

[0062] Accordingly, the data slices can be output one by one to the second memory, and then output one by one from the second memory to the fully connected layer and the attention layer for inference to obtain the value vector and key vector corresponding to the data slice, and the GEMM unit is called to perform matrix multiplication on the key vector and value vector corresponding to the data slice.

[0063] Since the GEMM unit and the DMA unit can operate independently, when the GEMM unit performs matrix multiplication, the DMA unit can read the next data slice and store it in the second memory. After the GEMM unit completes the operation, the second memory is controlled to output the stored data slice to the fully connected layer and the attention layer for reasoning.

[0064] Therefore, performing the data reading operation during the GEMM unit operation can stack the data loading and subsequent GEMM operations in time. Performing data loading and GEMM operations at the same time can improve the reasoning efficiency of large language models.

[0065] In addition, in this embodiment, the storage space of the second memory is required to be large enough to store the slice data, the accumulated vector and other data.

[0066] In the embodiment of the present application, the initial vector and the accumulated vector required for calculating the key vector and the value vector can be cached, that is, the cache target matrix. Compared with separately caching the key vector and the value vector, which requires two storage spaces, storing the target matrix only requires one storage space, that is, the storage space required for caching the target matrix can be halved compared with the prior art, thereby effectively reducing the data bandwidth required for the vector in the memory cache reasoning process.

[0067] For easier understanding, see Figure 4 , Figure 4 This is a comparative schematic diagram provided in an embodiment of the present application. Figure 4 The left figure is a schematic diagram of storing key vectors and value vectors in the prior art. Figure 4 The right figure is a schematic diagram of storing the accumulated vector in this application. Among them, FC represents the fully connected layer, Q represents the query vector, K represents the key vector, V represents the value vector, TensorCore represents the tensor computing core, cached represents the cache, GEMM represents the GEMM operation, SOFTMAX represents the normalized exponential function, and heads represents the data slice.

[0068] like Figure 4 As shown, cached represents cached data. In the left figure, the prior art stores key vectors (K(S, dh) and K(1, dh)) and value vectors (V(S, dh) and V(1, dh)) separately, which requires two storage spaces. Ignoring the data format, the size of the required cache space can be expressed as Mbase=2*S*dh*Nheads, where Mbase is the storage space required for caching value vectors and key vectors, and S*dh*Nheads represents the cache space required for caching a single vector or the data size of a single vector.

[0069] like Figure 4 As shown in the right figure, in an embodiment of the present application, the target matrix (a matrix composed of 1, d_h*n_heads and 1, d_h*n_heads) composed of the initial vector and the accumulated vector is cached, that is, only one data needs to be cached, and one cache space is used. The required cache space can be expressed as: Madv = S*dh*Nheads, where Madv represents the storage space required for caching the target matrix. Among them, one storage space refers to the space required for caching the target matrix by the large language model data relationship method provided in the present application. The prior art requires two storage spaces to represent that the storage space required is twice that of the large language model data relationship method provided in the present application. One storage space does not refer to the specific size of the cache space. As the number of inference rounds increases, the size of the cache space corresponding to one storage space will also increase.

[0070] In addition, in the prior art, the data of the non-first-round reasoning input fully connected layer is an accumulated vector, that is, a single 1, d_h*n_heads, and in the embodiment of the present application, since the matrix of the initial vector and the accumulated vector is cached, the input of the non-first-round reasoning fully connected layer also becomes a matrix composed of 1, d_h*n_heads and 1, d_h*n_heads, wherein the number of 1, d_h*n_heads in the input matrix will also accumulate as the number of reasoning rounds increases.

[0071] In addition, the above embodiments of the present application can also be combined with other technologies to further reduce bandwidth overhead.

[0072] For example:

[0073] In one embodiment, the electronic device further includes a data compression and decompression unit. The large language data management method may further include: if the size of the target matrix stored in the storage unit is greater than a preset threshold, the target matrix is ​​compressed based on the data compression and decompression unit to obtain compressed data. Accordingly, before the target matrix is ​​output to the attention layer, the compressed data may be decompressed based on the data compression and decompression unit to obtain the target matrix.

[0074] The data compression and decompression unit is used to compress and decompress data. For details, please refer to the existing technology. The data compression and decompression unit can be implemented as a peripheral circuit in an electronic device or a configured program, which will not be expanded here.

[0075] In this embodiment, the size of the target matrix refers to the number of rows multiplied by the number of columns. For example, the number of rows is M, the number of columns is N, and the size of the target matrix is ​​M×N. Similarly, the size of the target matrix can also represent the amount of data in the target matrix. If the target matrix is ​​too large, such as greater than a preset threshold, it can be compressed to improve the reading efficiency of the target matrix and reduce the storage space required for the target matrix.

[0076] The preset threshold value can be configured according to the actual scenario. For example, the processor configures the preset threshold value according to the data reading efficiency of the storage unit to reduce the possibility of affecting the reading efficiency due to excessive data. The preset threshold value can also be configured according to the capacity of the storage unit to reduce the possibility of data loss due to the inability to store data. The above is only an example and should not be a limitation to the present application.

[0077] In some embodiments, a sparse analysis may be performed on the access to the target matrix. The sparse analysis may refer to the prior art and will not be expanded here. By combining other methods of reducing storage space occupancy and bandwidth access requests, the data bandwidth of the cached data of the large language model may be further reduced.

[0078] Based on the same inventive concept, an embodiment of the present application also provides a large language model data management device, which can be applied to a processor of an electronic device, the processor is configured with a large language model including an attention layer, and the electronic device also includes a storage unit.

[0079] See also Figure 5 , Figure 5 A schematic diagram of a large language model data management device provided in an embodiment of the present application. The large language model data management device 400 includes an output module 410 and an accumulation module 420 .

[0080] Output module 410 is used to output the target matrix to the attention layer when the attention layer performs non-first-round reasoning, so that the attention layer calculates the key vector and value vector generated by the previous round of reasoning based on the target matrix; wherein: the target matrix is ​​stored in a storage unit, the target matrix includes an initial vector and N accumulated vectors, N=M-2, M is the current reasoning round of the attention layer, and M is greater than or equal to 2; the initial vector is the intermediate data generated by the large language model based on the input text during the first round of reasoning and used to infer the key vector and value vector; the accumulated vector is the fixed data required to be added when the attention layer performs non-first-round reasoning, and the key vector and value vector of this round are inferred.

[0081] The accumulation module 420 is used to accumulate the target matrix in the storage unit based on the accumulation vector after the attention layer completes the current round of reasoning based on the key vector and the value vector.

[0082] In one embodiment, the large language model also includes a preset network layer located before the attention layer; the preset network layer is a network layer having a multiplication-accumulation operation function, but the multiplication-accumulation operation function is not enabled. The output module 410 is also used to output the target matrix to the preset network layer, so as to process the target matrix through the multiplication-accumulation operation function in the preset network layer to obtain a calculation result; and output the calculation result to the attention layer.

[0083] In one embodiment, the preset network layer is a fully connected layer before the attention layer.

[0084] In one embodiment, the electronic device also includes a data compression and decompression unit, and the output module 410 is also used to compress the target matrix based on the data compression and decompression unit to obtain compressed data if the size of the target matrix stored in the storage unit is greater than a preset threshold; and, before outputting the target matrix to the attention layer, the method also includes: decompressing the compressed data based on the data compression and decompression unit to obtain the target matrix.

[0085] In one embodiment, the accumulation module 420 is also used to obtain an input vector generated by the attention layer inferring the basic unit of the model input during the first round of reasoning; the basic unit of the model input is obtained by converting the text input to the large language model; and the input vector is stored in the storage unit as an initial vector.

[0086] In one embodiment, the storage unit includes a first memory and a second memory, the first memory is used to store the target matrix, and the accumulation module 420 is specifically used to store an accumulation vector into the second memory; after the attention layer completes the current round of reasoning, the accumulation vector in the second memory is stored in the first memory to accumulate the target matrix.

[0087] In one embodiment, the capacity of the second memory is greater than a preset capacity; the processor also includes a matrix multiplication GEMM unit and a direct memory access DMA unit for a general matrix; the attention layer is also used to perform matrix multiplication of the key vector and the value vector for a general matrix based on the GEMM unit. The output module 410 is also used to: read data slices according to preset weights for the target matrix in the first memory, and output the data slices to the second memory; output the data slices from the second memory to the attention layer, so that the attention layer can infer the key vector and value vector based on the data slice, and call the GEMM unit to perform matrix multiplication on the key vector and value vector corresponding to the data slice; in the process of the GEMM unit performing matrix multiplication, the next data slice is read out from the first memory according to the preset weight based on the DMA unit and stored in the second memory.

[0088] The functions implemented by the large language model data management device are similar to those of the aforementioned large language model data management method, which will not be described in detail here. For details, please refer to the content of the aforementioned large language model data management method.

[0089] Based on the same inventive concept, an embodiment of the present application further provides a processor, which is configured with a large language model including an attention layer. The processor can be used to execute the large language model data management method provided in any of the aforementioned embodiments.

[0090] In this embodiment, the processor may be an NPU (Neural Processing Unit). In other embodiments, it may be a CPU (Central Processing Unit), GPU (Graphics Processing Unit), etc. that can implement the above-mentioned large language model data management method, and there is no limitation here.

[0091] Based on the same inventive concept, the present application also provides an electronic device, see Figure 6 , Figure 6This is a schematic diagram of an electronic device provided in an embodiment of the present application, the electronic device includes: a processor and a storage unit. The processor is communicatively connected with the storage unit.

[0092] A processor is configured with a large language model including an attention layer, and the processor is used to execute the large language model data management method provided by any of the aforementioned embodiments.

[0093] The processor 510 may be an integrated circuit chip with signal processing capabilities. The processor 510 may be a general-purpose processor, including a CPU, a NP (Network Processor), etc.; it may also be a digital signal processor, an application-specific integrated circuit, a readily available programmable gate array or other programmable logic device or transistor logic device, a discrete hardware component, or a processor provided in the aforementioned embodiments. It may implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.

[0094] The processor 510 may be configured with a GEMM unit, a DMA unit, and other computing units required for a large language model.

[0095] The storage unit 520 includes a first memory 521 and a second memory 522 . The first memory 521 may be a DDR, and the second memory may be a SRAM.

[0096] In addition, the electronic device may further include a data compression and decompression unit 530 to compress and decompress the target matrix.

[0097] It can be understood that the electronic device 500 may also include more common modules required by itself, which are not introduced one by one in the embodiments of the present application.

[0098] Based on the same inventive concept, an embodiment of the present application further provides a computer-readable storage medium on which a computer program is stored. When the computer program runs on a computer, the computer executes the method provided in the above embodiment.

[0099] The computer-readable storage medium may be any available medium that can be accessed by a computer or a data storage device such as a server or a data center that includes one or more available media. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium (e.g., a DVD (digital video disc)), or a semiconductor medium (e.g., an SSD (Solid State Disk)).

[0100] If the large language data management method is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for 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 application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a ROM, a RAM, a magnetic disk, or an optical disk.

[0101] In the embodiments provided in the present application, it should be understood that the disclosed methods and devices can also be implemented in other ways. The device embodiments described above are merely illustrative. The functional modules in the various embodiments of the present application can be integrated together to form an independent part, or each module can exist separately, or two or more modules can be integrated to form an independent part.

[0102] The above embodiments can be freely combined without conflict, and the embodiments obtained by the combination are included in the protection scope of this application.

[0103] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

[0104] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.

Claims

1. A large language model data management method, characterized in that: A processor applied to an electronic device, the processor being configured with a large language model including an attention layer, the electronic device further comprising a storage unit, the large language model data management method comprising: When the attention layer performs non-first-round reasoning, the target matrix is ​​output to the attention layer, so that the attention layer calculates the key vector and value vector generated by the previous round of reasoning based on the target matrix; wherein: the target matrix is ​​stored in the storage unit, the target matrix includes an initial vector and N accumulated vectors, N=M-2, M is the current reasoning round of the attention layer, and M is greater than or equal to 2; the initial vector is the intermediate data generated by the large language model during the first round of reasoning based on the input text and used to infer the key vector and value vector; the accumulated vector is the fixed data required to be added for inferring the key vector and value vector of this round when the attention layer performs non-first-round reasoning; After the attention layer completes the current round of reasoning based on the key vector and the value vector, the target matrix in the storage unit is accumulated based on the accumulation vector.

2. The large language model data management method according to claim 1, characterized in that: The large language model also includes a preset network layer located before the attention layer; the preset network layer is a network layer having a multiplication-accumulation operation function, but the multiplication-accumulation operation function is not enabled; Outputting the target matrix to the attention layer includes: Outputting the target matrix to the preset network layer, so as to process the target matrix through the multiplication-accumulation operation function in the preset network layer to obtain a calculation result; Output the calculation result to the attention layer.

3. The large language model data management method according to claim 2, characterized in that: The preset network layer includes a fully connected layer before the attention layer.

4. The large language model data management method according to claim 1, characterized in that: The electronic device further includes a data compression and decompression unit, and the large language model data management method further includes: If the size of the target matrix stored in the storage unit is greater than a preset threshold, compressing the target matrix based on the data compression and decompression unit to obtain compressed data; Furthermore, before outputting the target matrix to the attention layer, the method further includes: decompressing the compressed data based on the data compression and decompression unit to obtain the target matrix.

5. The large language model data management method according to claim 1, characterized in that: The method further comprises: When obtaining the first round of reasoning, the attention layer infers the basic unit of the model input to generate an input vector; the basic unit of the model input is obtained by converting the text input to the large language model; The input vector is stored in the storage unit as the initial vector.

6. The large language model data management method according to any one of claims 1 to 5, characterized in that: The storage unit comprises a first memory and a second memory, wherein the first memory is used to store the target matrix; The accumulating the target matrix based on the accumulation vector includes: storing one of the accumulated vectors into the second memory; After the attention layer completes the current round of reasoning, the accumulated vector in the second memory is stored in the first memory to accumulate the target matrix.

7. The large language model data management method according to claim 6, characterized in that: The capacity of the second memory is greater than a preset capacity; and the processor further includes: General purpose matrix multiplication GEMM unit and direct memory access DMA unit; The attention layer is further used to perform a general matrix multiplication of the key vector and the value vector based on the GEMM unit; Outputting the target matrix to the attention layer includes: Reading data slices from the target matrix in the first memory according to preset weights, and outputting the data slices to the second memory; Outputting the data slice from the second memory to the attention layer, so that the attention layer infers the key vector and the value vector based on the data slice, and calling the GEMM unit to perform matrix multiplication on the key vector and the value vector corresponding to the data slice; During the process of the GEMM unit performing matrix multiplication, the next data slice is read out from the first memory according to the preset weight based on the DMA unit and stored in the second memory.

8. A processor, characterized in that: A large language model including an attention layer is configured; the processor is used to execute the large language model data management method as described in any one of claims 1-7.

9. An electronic device, characterized in that: include: A processor configured with a large language model including an attention layer; the processor is used to execute the large language model data management method according to any one of claims 1 to 7; A storage unit connected to the processor; Used to store the target matrix required by the large language model during the inference process.

10. A computer-readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is run on a computer, the computer is caused to execute the large language model data management method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Neural network model quantification method, system and device and computer medium

    CN114970822A

  • Multi-modal task processing and dialogue task processing method, system and equipment

    CN117033585A

  • Large language model reasoning optimization method and device, computer equipment and storage medium

    CN117194056A

  • Model operation method and device, electronic equipment and storage medium

    CN117786301A

  • Neural network interpretation method and device, electronic equipment and storage medium

    CN117910476A

Cited By

  • Method for reasoning by using large language model, control device and storage medium

    CN121303273A