Method, device and equipment for automatically distributing heterogeneous extension rules of sparse attention
By evaluating and dynamically assigning extended rules for sparse attention masks, the performance and efficiency issues of large language models under long sequences and different input lengths are solved, achieving higher flexibility and adaptability.
Patent Information
- Application Number
- CN202410777843.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-17
- Publication Date
- 2024-10-18
AI Technical Summary
Existing sparse attention methods have limitations when dealing with long sequences and different input lengths, and are difficult to adapt to the dynamic changes of different attention heads, resulting in a decrease in the performance and efficiency of large language models under various sequence lengths.
Dynamically allocate extended rules for sparse attention heads, including hyperparameters, to adapt to different inputs by evaluating the impact of each attention mask on a large language model, determining accuracy loss, and searching for a Pareto front compression scheme based on the objective function Sequence length.
It improves the overall performance and efficiency of large language models under various input sequence lengths, solves the limitations of sparse attention methods under long sequences and different input lengths, and enhances the flexibility and adaptability of the model.
Smart Images

Figure CN118798262A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of large language models, and in particular to a method, device and equipment for automatically allocating heterogeneous expansion rules of sparse attention. Background Art
[0002] In autoregressive inference of large language models, two phases are involved: prefill and decode. In the prefill phase, the model processes the entire sequence of input tokens (chunk tokens) to generate the initial response token. In the subsequent decode phase, the model uses the newly generated token and the previously cached K (key, key) and V (value, value) matrices to generate subsequent tokens until the generation is completed. Although this iterative process is effective, it increases memory and computational requirements due to the expansion of the KV (key-value) cache.
[0003] Existing techniques use manually designed uniform sparse attention masks to speed up the attention calculation of large models. Assuming the length of the input sequence is N, the attention matrix of the large model is an N*N matrix, where each position (i, j) represents the attention of the i-th word to the j-th word. In order to reduce the cost of attention calculation, existing works use sparse attention to skip some attention calculations. This design limits the scope of most attention to only cover local contexts, thereby maintaining controlled attention calculation and key-value cache overhead when processing long sequences.
[0004] However, through the above method, although the large language model can maintain computational efficiency when processing long inputs, it has certain limitations when processing long sequences and different input lengths, lacks flexibility, and is difficult to adapt to the dynamic changes of different attention heads. Summary of the invention
[0005] The present application provides a method, device and equipment for automatically allocating heterogeneous expansion rules of sparse attention to solve the problem that the sparse attention method in the related art has certain limitations when processing long sequences and different input lengths, thereby reducing the overall performance and efficiency of large language models under various sequence lengths.
[0006] In a first aspect, an embodiment of the present application provides a method for automatically allocating heterogeneous extension rules for sparse attention, comprising the following steps: evaluating the impact of each attention mask on the final prediction result of a large language model; determining the accuracy loss at different input sequence lengths of the large language model based on the evaluation result; searching for a Pareto frontier compression scheme at different input sequence lengths of the large language model based on an objective function, wherein the input sequence length, accuracy loss and density constraint of the large language model are inputs to the objective function, and the objective function outputs a set of extension rules for each sparse attention allocation, wherein each extension rule in the set of extension rules includes hyperparameters of the corresponding sparse attention head.
[0007] Optionally, the objective function is:
[0008]
[0009] Among them, N i Indicates the length of the i-th sequence, i indicates The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates that the input sequence length is N i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
[0010] Optionally, the change in the loss function value It is represented by the sum of the changes in the loss function value brought by each attention head, where The calculation formula is:
[0011]
[0012] in, is the hth attention head in N i Use compression rule r for input sequence length h The change in the loss function value brought by the corresponding attention mask to the entire model.
[0013] Optionally, the function that extends the rule is:
[0014] S h =F p (N|h);
[0015] Among them, S h =α h +β h ×N, h represents the hth attention, N represents the length of the input sequence, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length, F p Represents an extension function.
[0016] Optionally, the expansion function is any one of a polynomial function, an exponential function and a logarithmic function.
[0017] Optionally, the method further includes: selecting an expansion rule in the expansion rule set that produces the minimum loss on a predetermined input length as the heterogeneous expansion rule, wherein the predetermined input length is greater than all lengths of the different input sequence lengths.
[0018] The second aspect of the present application provides an automatic allocation device for heterogeneous extension rules of sparse attention, including: an evaluation module, used to evaluate the impact of each attention mask on the final prediction result of a large language model; a determination module, used to determine the accuracy loss of different input sequence lengths of the large language model according to the evaluation result; a search module, used to search for a Pareto front compression scheme on different input sequence lengths of the large language model based on an objective function, wherein the input sequence length, accuracy loss and density constraints of the large language model are used as inputs of the objective function, and the objective function outputs a set of extension rules for each sparse attention allocation, and each extension rule in the set of extension rules includes hyperparameters of the corresponding sparse attention head.
[0019] Optionally, the objective function is:
[0020]
[0021] Among them, N i Indicates the length of the i-th sequence, i indicates The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates the length of the input sequence N i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
[0022] Optionally, the change in the loss function value It is represented by the sum of the changes in the loss function value brought by each attention head, where The calculation formula is:
[0023]
[0024] in, is the hth attention head in N i Use compression rule r for input sequence length h The change in the loss function value brought by the corresponding attention mask to the entire model.
[0025] Optionally, the function that extends the rule is:
[0026] S h =F p (N|h);
[0027] Among them, S h =α h +β h ×N, h represents the hth attention, N represents the length of the input sequence, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length, F p Represents an extension function.
[0028] Optionally, the expansion function is any one of a polynomial function, an exponential function and a logarithmic function.
[0029] Optionally, it further includes: a selection module, used to select an expansion rule in the expansion rule set that produces the minimum loss on a predetermined input length as a heterogeneous expansion rule, wherein the predetermined input length is greater than all lengths of different input sequence lengths.
[0030] The third aspect of the present application provides an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement a method for automatically allocating heterogeneous expansion rules for sparse attention as in the above-mentioned embodiment.
[0031] The fourth aspect of the present application provides a computer-readable storage medium having a computer program or instruction stored thereon. When the computer program or instruction is executed by a processor, it is used to implement the method for automatically allocating heterogeneous expansion rules for sparse attention as in the above-mentioned embodiment.
[0032] The fifth aspect of the present application provides a computer program product, including a computer program or instructions, which, when executed, are used to implement the method for automatically allocating heterogeneous expansion rules for sparse attention as in the above-mentioned embodiment.
[0033] Therefore, this application includes the following beneficial effects:
[0034] The embodiment of the present application can determine the accuracy loss of a large language model at different input sequence lengths, further search for a Pareto frontier compression scheme at different input sequence lengths of the large language model based on the objective function, and use the objective function to assign different expansion rules to each sparse attention head while following the density constraint, each expansion rule includes a hyperparameter corresponding to the sparse attention head, thereby improving the overall performance and efficiency of the large language model at various input sequence lengths. Thus, the technical problem that the sparse attention method in the related art has certain limitations when processing long sequences and different input lengths, thereby reducing the overall performance and efficiency of the large language model at various sequence lengths, etc.
[0035] Additional aspects and advantages of the present application will be given in part in the description below, and in part will become apparent from the description below, or will be learned through the practice of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:
[0037] Figure 1 A flowchart of a method for automatically allocating heterogeneous expansion rules for sparse attention provided according to an embodiment of the present application;
[0038] Figure 2 A schematic diagram of a sparse attention search space provided according to an embodiment of the present application;
[0039] Figure 3 An exemplary diagram of a device for automatically allocating heterogeneous expansion rules for sparse attention provided according to an embodiment of the present application;
[0040] Figure 4 It is a schematic diagram of the structure of an electronic device provided according to an embodiment of the present application. DETAILED DESCRIPTION
[0041] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.
[0042] The following describes the method, device and equipment for automatically allocating heterogeneous extension rules of sparse attention in the embodiment of the present application with reference to the accompanying drawings. In view of the problem mentioned in the above background technology that although the large language model can maintain computational efficiency when processing long inputs, it has certain limitations when processing long sequences and different input lengths, lacks flexibility, and is difficult to adapt to the dynamic changes of different attention heads, the present application provides a method for automatically allocating heterogeneous extension rules of sparse attention, in which the accuracy loss of the large language model on different input sequence lengths can be determined, and the Pareto front compression scheme on different input sequence lengths of the large language model can be further searched based on the objective function. While following the density constraint, the objective function is used to assign different extension rules to each sparse attention, and each extension rule includes the hyperparameters of the corresponding sparse attention head. As a result, the sparse attention method in the related art has certain limitations when processing long sequences and different input lengths, thereby reducing the overall performance and efficiency of the large language model under various sequence lengths.
[0043] Current large models are usually based on the Transformer architecture. Transformer is a model architecture widely used in natural language processing (NLP) tasks (e.g., translation, question answering, etc.), which captures the correlation of different positions in the input sequence (e.g., input text sequence) through a multi-head self-attention mechanism. Transformer's MHA (Multi-Head Self Attention) is the key to the function of large language models (LLMs). The MHA mechanism first transforms the input sequence into query (Query, Q), key (Key, K), and value (Value, V) matrices through linear projection. These matrices are combined with the cached K and V (KV-Cache) in the previous sequence to calculate the attention matrix (A). This calculation process is modified by a causal mask (M) to ensure the autoregressive property, and finally the output (O) is obtained, as shown in the following formula:
[0044] S=QK T ,A=softmax(S+M),O=AV;
[0045] In the autoregressive inference of a large language model, two phases are involved: prefill and decode. In the prefill phase, the model processes the entire input token (chunk token) sequence to generate the initial response token. In the subsequent decode phase, the model uses the newly generated token and the previously cached K and V matrices to generate subsequent tokens until the generation is completed.
[0046] Existing technologies use manually designed unified sparse attention masks to speed up the attention calculation of large models. Assuming the length of the input sequence is N, the attention matrix of the large model is an N*N matrix, where each position (i, j) represents the attention of the i-th word to the j-th word. In order to reduce the cost of attention calculation, existing works use sparse attention to skip some attention calculations.
[0047] For example, existing studies such as StreamingLLM and LM-Infinite have designed sliding window attention masks with global attention for all attention heads. This design allows all attention heads to use a unified fixed-span sliding window mask on all input lengths, and form a global attention connection with the rest of the sequence at the beginning. In this way, the model can maintain computational efficiency when processing long inputs, but lacks flexibility under different input lengths and is difficult to adapt to the dynamic changes of different attention heads.
[0048] Existing static sparse attention methods in generative language models, such as fixed-span sliding window masks, have the following problems:
[0049] 1. Limited effective context length: These methods restrict the attention mechanism to the local context, limiting the model's ability to aggregate global information in long inputs. As a result, the effective context length (defined as the maximum input length at which the content retrieval accuracy exceeds 90%) is kept short and usually does not exceed a fixed attention span. This can lead to significant accuracy loss when processing long sequences.
[0050] 2. Inefficient resource allocation: The unified attention mask cannot distinguish the needs of local context attention heads and global context attention heads. This uniformity leads to inefficient allocation of computational and memory resources, where the global context head is overly restricted and the local context head is allocated too many resources.
[0051] 3. Insufficient input length expansion: As the input length increases, some attention heads need to expand their attention span faster to maintain performance. However, static, unified methods do not adapt to the heterogeneous expansion rules of different heads, resulting in significant performance degradation under longer inputs.
[0052] 4. Lack of automatic optimization mechanism: Current methods do not have an automatic optimization mechanism to efficiently find the optimal mask parameters in a large-scale search space. This limits the ability of the attention mask design to adapt to different input lengths, thereby reducing the overall performance and efficiency of the model under various sequence lengths.
[0053] Therefore, an embodiment of the present application proposes a method for automatically allocating heterogeneous expansion rules for sparse attention.
[0054] Specifically, Figure 1 A flowchart of a method for automatically allocating heterogeneous expansion rules for sparse attention provided in an embodiment of the present application.
[0055] like Figure 1 As shown, the method for automatically allocating heterogeneous expansion rules of sparse attention includes the following steps:
[0056] In step S101, the impact of each attention mask on the final prediction result of the large language model is evaluated.
[0057] It is understandable that the embodiments of the present application can use different methods to evaluate the impact of each attention mask on the final prediction result of the large language model, such as Taylor first-order approximation, black box model, etc.
[0058] In step S102, the accuracy loss of the large language model at different input sequence lengths is determined according to the evaluation result.
[0059] It should be understood that the present application can obtain the accuracy loss through simulation and calculation based on any existing loss function, and can also obtain the accuracy loss based on experimental data, without any specific limitation.
[0060] In step S103, a Pareto front compression scheme is searched for different input sequence lengths of the large language model based on the objective function, wherein the input sequence length, accuracy loss and density constraint of the large language model are inputs of the objective function, and the output of the objective function is a set of extension rules assigned to each sparse attention head, and each extension rule in the set of extension rules includes hyperparameters of the corresponding sparse attention head.
[0061] Among them, the Pareto front compression scheme refers to a set of schemes in which, in a multi-objective optimization problem (i.e., an optimization problem for different input lengths), there is no other scheme that can improve another objective without deteriorating at least one objective. Specifically in this application, the Pareto front compression scheme means that under different input sequence lengths, each compression scheme achieves the best balance between accuracy loss and density constraint.
[0062] It can be understood that the embodiments of the present application can search for Pareto front compression schemes at different input sequence lengths of a large language model based on the objective function, thereby improving the flexibility of the large language model and being able to adapt to the dynamic changes of different attention heads.
[0063] In the embodiment of the present application, the objective function is:
[0064]
[0065] Among them, N i Indicates the length of the i-th input sequence, i represents The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth input sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates that the input sequence length is B i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
[0066] It should be noted that The entire model is compressed using a certain compression scheme. i The change in the loss function value of the model on the data. is 1 ,r 2 …r H}, that is, any change in the compression rule of the attention head will affect the loss function value of the model.
[0067] In the embodiment of the present application, the change in the loss function value is It is represented by the sum of the changes in the loss function value brought by each attention head, where The calculation formula is:
[0068]
[0069] in, is the hth attention head in N i Use compression rule r for input sequence length hThe change in the loss function value brought by the corresponding attention mask to the entire model.
[0070] It can be understood that the embodiments of the present application can use the input sequence length, accuracy loss and density constraint of the large language model as the input of the objective function. The objective function outputs different expansion rules assigned to each sparse attention head. The expansion rules include hyperparameters of the corresponding sparse attention head, that is, different expansion rules are assigned to different sparse attention heads, which can flexibly adapt to changes in different input sequence lengths. The hyperparameters of the corresponding sparse attention head are determined based on the expansion rules, and the hyperparameters of each sparse attention head can be efficiently determined. While complying with the density constraint, the accuracy loss caused by attention sparsification is minimized.
[0071] In the embodiment of the present application, the function of the extension rule is:
[0072] S h =F p (N|h);
[0073] Among them, S h =α h +β h ×B, h represents the hth attention, N represents the length of the input sequence, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length, F p Represents an extension function.
[0074] In the embodiment of the present application, the expansion function is any one of a polynomial function, an exponential function and a logarithmic function.
[0075] In an embodiment of the present application, the method for automatically allocating heterogeneous extension rules for sparse attention may also include: selecting an extension rule in the extension rule set that produces the smallest loss on a predetermined input length as a heterogeneous extension rule, wherein the predetermined input length is greater than all lengths of different input sequence lengths.
[0076] It can be understood that the embodiment of the present application can also select the expansion rules that meet the conditions in the expansion rule set as the heterogeneous expansion rules, so that the expansion rules can better adapt to longer inputs.
[0077] It should be understood that the present application may also adopt other methods to select the final expansion rule, for example, it may randomly select from the expansion rule set, or select the expansion rule that produces the minimum loss on other predefined input lengths.
[0078] The following is a specific example to illustrate the method for automatically allocating heterogeneous expansion rules for sparse attention.
[0079] When designing the search space for the mask in this application, the inherent heterogeneity and scalability of the attention patterns of large language models are taken into account. Therefore, a hardware-friendly sliding window mask is used as the basic sparse attention mask, such as Figure 2 As shown in Figure 2. Similar to previous work, the first few tokens (e.g., 64 tokens) are not masked. The attention span is equal to the sliding window span plus the number of initial unmasked tokens. The density describes the density of the attention mask, which is defined as the ratio of the attention span of the mask to the input length. It should be understood that the definition of density is not limited to this, and can also be the ratio of area, etc.
[0080] A simple linear function is used to define the attention span (S) of the attention head (h) for the input length (N). h ):
[0081] S h =α h +β h ×N;
[0082] Among them, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length. To ensure hardware friendliness, the number of different rules per model layer can be limited, for example, to a maximum of two rules per layer.
[0083] The α and β hyperparameters for each attention head can be selected from multiple discrete options. For large language models with many layers, the search space for this application can become very large. For example, for a 7B model with 32 attention heads and 32 layers per layer, with 6 and 9 different α and β options per head, the potential search space expands to 54 1024 Therefore, this application designs an automatic expansion rule to efficiently find the best α and β for any large language model.
[0084] The core of this application is to assign different expansion rules to each attention head, rather than the linear expansion rule itself. The form of the expansion rule can include various functions of the following forms:
[0085] Sh=F p (N|h);
[0086] Among them, p is a set of hyperparameters of the function, and the F function can take various forms, such as polynomial function, exponential function, logarithmic function, etc.
[0087] This application automatically selects the optimal expansion rule for each attention head to minimize the accuracy loss under the density budget under different input sequence lengths.
[0088] This application will first evaluate the impact of each attention mask on the final prediction result of the model through various existing methods, such as Taylor first-order approximation, black box model, etc.
[0089] Based on the results of the analysis and evaluation, the present application first determines a set of Pareto front compression plans at different input lengths. The Pareto front compression scheme refers to a set of schemes in which there is no other scheme that can improve another objective without deteriorating at least one objective in a multi-objective optimization problem (i.e., an optimization problem for different input lengths). Specifically for the present application, the Pareto front compression scheme means that each compression scheme achieves the best balance between accuracy loss and density constraints under different input sequence lengths.
[0090] Multi-objective optimization is used to search for a set of Pareto-optimal compression solutions over the analytically evaluated lengths. The objective for each length is to minimize the total accuracy loss while complying with any user-defined density constraints. The objective is formulated as follows:
[0091]
[0092] In the above formula, N i Indicates the length of the i-th input sequence, i represents The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth input sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates that the input sequence length is N i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
[0093] It should be noted that in order to ensure that the solution selected in this application can be best generalized to the length of the unanalyzed evaluation, this application selects the solution that can produce the smallest loss on the unanalyzed evaluation length (such as a larger length) as the final solution among all solutions on the Pareto front, that is, among the multiple solutions generated by the objective function.
[0094] Based on the above automatic expansion rules, the expansion rules are efficiently customized for each attention head, and the accuracy loss caused by attention sparsification is minimized while following the user-defined density constraints.
[0095] In addition, the present application may try other forms of sparse attention masks, such as random sparse masks, global random masks, etc., and may use other optimization algorithms, such as combining different objective functions of multi-objective optimization problems to transform them into single-objective optimization problems.
[0096] According to the method for automatically assigning heterogeneous expansion rules for sparse attention proposed in the embodiment of the present application, the accuracy loss of a large language model at different input sequence lengths can be determined, and the Pareto front compression scheme at different input sequence lengths of the large language model can be further searched based on the objective function. Different expansion rules are assigned to each sparse attention head using the objective function while following the density constraint. Each expansion rule includes hyperparameters of the corresponding sparse attention head, thereby improving the overall performance and efficiency of the large language model at various input sequence lengths.
[0097] Next, the device for automatically allocating heterogeneous expansion rules for sparse attention proposed in accordance with an embodiment of the present application will be described with reference to the accompanying drawings.
[0098] Figure 3 It is a block diagram of a device for automatically allocating heterogeneous expansion rules for sparse attention according to an embodiment of the present application.
[0099] like Figure 3 As shown, the sparse attention heterogeneous expansion rule automatic allocation device 10 includes: an evaluation module 100, a determination module 200 and a search module 300.
[0100] Among them, the evaluation module 100 is used to evaluate the impact of each attention mask on the final prediction result of the large language model; the determination module 200 is used to determine the accuracy loss of the large language model at different input sequence lengths according to the evaluation results; the search module 300 is used to search for the Pareto frontier compression scheme at different input sequence lengths of the large language model based on the objective function, wherein the input sequence length, accuracy loss and density constraint of the large language model are inputs of the objective function, and the objective function outputs an extended rule set for each sparse attention allocation, and each extended rule in the extended rule set includes the hyperparameters of the corresponding sparse attention head.
[0101] In the embodiment of the present application, the objective function is:
[0102]
[0103] Among them, N i Indicates the length of the i-th sequence, i indicates The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates the length of the input sequence N i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
[0104] In the embodiment of the present application, the change in the loss function value is It is represented by the sum of the changes in the loss function value brought by each attention head, where The calculation formula is:
[0105]
[0106] in, is the hth attention head in N i Use compression rule r for input sequence length h The change in the loss function value brought by the corresponding attention mask to the entire model
[0107] In the embodiment of the present application, the function of the extension rule is:
[0108] S h =F p (N|h);
[0109] Among them, S h =α h +β h ×N, h represents the hth attention, N represents the length of the input sequence, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length, F p Represents an extension function.
[0110] In the embodiment of the present application, the expansion function is any one of a polynomial function, an exponential function and a logarithmic function.
[0111] In the embodiment of the present application, the device 10 of the embodiment of the present application further includes: a selection module.
[0112] The selection module is used to select an expansion rule in the expansion rule set that produces the minimum loss on a predetermined input length as a heterogeneous expansion rule, wherein the predetermined input length is greater than all lengths of different input sequence lengths.
[0113] It should be noted that the aforementioned explanation of the embodiment of the method for automatic allocation of heterogeneous extension rules for sparse attention is also applicable to the device for automatic allocation of heterogeneous extension rules for sparse attention of this embodiment, and will not be repeated here.
[0114] According to the device for automatically allocating heterogeneous expansion rules for sparse attention proposed in the embodiment of the present application, the accuracy loss on different input sequence lengths of the large language model can be further searched for the Pareto front compression scheme on different input sequence lengths of the large language model based on the objective function. Different expansion rules are assigned to each sparse attention by using the objective function while following the density constraint. Each expansion rule includes hyperparameters of the corresponding sparse attention head, thereby improving the overall performance and efficiency of the large language model under various input sequence lengths.
[0115] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device may include:
[0116] Memory 401 , processor 402 , and a computer program stored in the memory 401 and executable on the processor 402 .
[0117] When the processor 402 executes the program, the method for automatically allocating heterogeneous expansion rules for sparse attention provided in the above-mentioned embodiment is implemented.
[0118] Furthermore, the electronic device further comprises:
[0119] The communication interface 403 is used for communication between the memory 401 and the processor 402 .
[0120] The memory 401 is used to store computer programs that can be executed on the processor 402 .
[0121] The memory 401 may include a high-speed RAM (Random Access Memory) memory, and may also include a non-volatile memory, such as at least one disk memory.
[0122] If the memory 401, the processor 402 and the communication interface 403 are implemented independently, the communication interface 403, the memory 401 and the processor 402 can be connected to each other through a bus and communicate with each other. The bus can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0123] Optionally, in a specific implementation, if the memory 401, the processor 402 and the communication interface 403 are integrated on a chip, the memory 401, the processor 402 and the communication interface 403 can communicate with each other through an internal interface.
[0124] The processor 402 may be a CPU (Central Processing Unit), or an ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement the embodiments of the present application.
[0125] An embodiment of the present application also provides a computer-readable storage medium having a computer program or instruction stored thereon. When the computer program or instruction is executed by a processor, the above-mentioned method for automatically allocating heterogeneous expansion rules of sparse attention is implemented.
[0126] An embodiment of the present application also provides a computer program product, including a computer program or instructions, which, when executed, is used to implement the above-mentioned method for automatically allocating heterogeneous expansion rules for sparse attention.
[0127] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms are not necessarily directed to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or N embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.
[0128] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include at least one of the features. In the description of this application, "N" means at least two, such as two, three, etc., unless otherwise clearly and specifically defined.
[0129] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, fragment or portion of code comprising one or N executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.
[0130] It should be understood that the various parts of the present application can be implemented in hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, the steps or methods can be implemented in software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function on a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array, a field programmable gate array, etc.
[0131] A person of ordinary skill in the art may understand that all or part of the steps carried by the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the above-mentioned program may be stored in a computer-readable storage medium, which, when executed, includes one of the steps of the method embodiment or a combination thereof.
[0132] Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limitations on the present application. Ordinary technicians in this field can change, modify, replace and modify the above embodiments within the scope of the present application.
Claims
1. A method for automatic allocation of heterogeneous expansion rules for sparse attention, characterized in that: The following steps are involved: Evaluate the impact of each attention mask on the final prediction of the large language model; Determine the accuracy loss at different input sequence lengths for the large language model based on the evaluation results; A Pareto front compression scheme is searched for at different input sequence lengths of a large language model based on an objective function, wherein the input sequence length, accuracy loss and density constraint of the large language model are inputs of the objective function, and the objective function outputs a set of expansion rules assigned to each sparse attention head, and each expansion rule in the set of expansion rules includes hyperparameters of the corresponding sparse attention head.
2. The method for automatically allocating heterogeneous expansion rules for sparse attention according to claim 1, characterized in that: The objective function is: Among them, N i Indicates the length of the i-th input sequence, i represents The i-th element in the set, and They represent the set of input sequence lengths to be analyzed and the set of input sequence lengths subject to density constraints, respectively. j Indicates the length of the jth input sequence, j indicates The jth element in represents the candidate rule set, and Represent the accuracy loss and density constraint of different lengths, r h represents the compression rule used for the h-th attention head, represents the minimization loss function, Indicates that the input sequence length is N i The change in the loss function value of the large language model on the data, H represents the total number of attention heads of the large language model, Represents the average density threshold.
3. The method for automatically allocating heterogeneous expansion rules for sparse attention according to claim 2, characterized in that: The change in the loss function value It is represented by the sum of the changes in the loss function value brought by each attention head, where The calculation formula is: in, is the hth attention head in N i Use compression rule r for input sequence length h The change in the loss function value brought by the corresponding attention mask to the entire model.
4. The method for automatically allocating heterogeneous expansion rules for sparse attention according to claim 1, characterized in that: The function of the extended rule is: S h =F p (N|h); Among them, S h =α h +β h ×N, h represents the hth attention head, N represents the length of the input sequence, α h and β h is a hyperparameter that controls the baseline span and its rate of expansion with input length, F p Represents an extension function.
5. The method for automatically allocating heterogeneous expansion rules for sparse attention according to claim 4, characterized in that: The expansion function is any one of a polynomial function, an exponential function and a logarithmic function.
6. The method for automatically allocating heterogeneous expansion rules for sparse attention according to any one of claims 1 to 5, characterized in that: Also includes: An expansion rule in the expansion rule set that produces the minimum loss on a predetermined input length is selected as the heterogeneous expansion rule, wherein the predetermined input length is greater than all lengths of the different input sequence lengths.
7. A device for automatically allocating heterogeneous expansion rules for sparse attention, characterized in that: include: An evaluation module is used to evaluate the impact of each attention mask on the final prediction result of the large language model; A determination module for determining the accuracy loss at different input sequence lengths of the large language model based on the evaluation results; A search module is used to search for Pareto front compression schemes on different input sequence lengths of a large language model based on an objective function, wherein the input sequence length, accuracy loss and density constraint of the large language model are inputs of the objective function, and the objective function outputs an expansion rule set assigned to each sparse attention head, and each expansion rule in the expansion rule set includes a hyperparameter of the corresponding sparse attention head.
8. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method for automatically allocating heterogeneous expansion rules for sparse attention as described in any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program or instruction stored thereon, characterized in that: When the computer program or instruction is executed, it implements the method for automatically allocating heterogeneous expansion rules for sparse attention as described in any one of claims 1-6.
10. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed, it implements the method for automatically allocating heterogeneous expansion rules for sparse attention as described in any one of claims 1-6.
Citation Information
Patent Citations
Automatic attention rarefaction method and device, electronic equipment and storage medium
CN118036678A
Cited By
Event-driven sparse attention optimization method and system based on brain-like computing chip
CN121349702A
Event-driven sparse attention optimization method and system based on brain-like computing chip
CN121349702B
