Parallel computing method, processor, electronic device and medium for attention operator
By determining the parallel allocation strategy based on the number of processor operating units and tensor shape, the problem of low computational efficiency of the attention operator on long sequence inputs is solved, and more efficient parallel computing performance and resource utilization are achieved.
Patent Information
- Application Number
- CN202410962056.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-18
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-07-18
AI Technical Summary
In the field of artificial intelligence, the running time and memory complexity of the attention operator have a quadratic relationship with the sequence length, resulting in long execution time and large memory overhead of the neural network model for long sequence inputs. How to make full use of the computing resources of the computing unit? Parallel computing has become an important research direction.
By determining the parallel allocation strategy of the tensor based on the number of computing units of the processor and the shape of the tensor, for example, allocate the first and second axis data of the first tensor into M1 and N1 shares, so that the product of M1 and N1 is equal to The number of computing units of the processor is T, and the T computing units of the processor are used to perform parallel calculations.
It achieves a comprehensive determination of parallel allocation strategies based on the number of processor computing units and tensor shapes, making full use of processor computing resources, improving parallel computing performance, optimizing parallel computing effects, and avoiding resource waste.
Smart Images

Figure CN118503606B
Abstract
Description
Technical Field
[0001] Embodiments of the present disclosure relate to the field of artificial intelligence technology, and more specifically, to a parallel computing method, processor, electronic device, and medium for an attention operator. Background Art
[0002] In the field of artificial intelligence, the attention mechanism is widely used in neural network models. Specifically, in the attention mechanism, the attention operator is used to find the attention weight of the input value and obtain the final output result. However, since the running time and memory complexity of the attention operator are quadratically related to the sequence length, the neural network model has defects such as long execution time and high memory overhead for long sequence inputs. How to distribute the data to be processed in parallel to make full use of the computing resources of the computing unit has become an important research direction. Summary of the invention
[0003] The embodiments of the present disclosure provide a parallel computing method, processor, electronic device and medium of an attention operator, which are used to implement a parallel allocation strategy for tensors based on the number of computing units of the processor and the shape of the tensor, so as to fully utilize the computing resources of the computing units in the processor and improve the parallel computing performance.
[0004] According to one aspect of the present disclosure, a parallel computing method for an attention operator in a neural network is provided, which is applicable to a processor including T computing units. The method according to an embodiment of the present disclosure includes: obtaining a first tensor to be parallelized, wherein the shape of the first tensor is [B1, S1, H1], wherein B1, S1, and H1 represent the data dimensions of the first tensor on the first axis, the second axis, and the third axis, respectively; according to the number of computing units T of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determining to allocate the data of the first axis of the first tensor into M1 parts and the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T; and according to the determination, using the T computing units of the processor to parallelize the first tensor, wherein T is an integer greater than 1, and B1, S1, and H1 are integers greater than or equal to 1.
[0005] According to some embodiments of the present disclosure, the first tensor is a query tensor in an attention operator, and the attention operator includes one of the following: a fast attention operator and a grouped attention operator.
[0006] According to some embodiments of the present disclosure, based on the number T of computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determining to allocate the data of the first axis of the first tensor into M1 parts and to allocate the data of the second axis of the first tensor into N1 parts so that the product of M1 and N1 is equal to T includes: when B1 is greater than or equal to T, M1 is equal to T, and N1 is equal to 1; and when B1 is less than T, M1 is equal to B1, and N1 is equal to T / B1.
[0007] According to some embodiments of the present disclosure, the attention operator is a fast attention operator, and the first tensor is a query tensor in the fast attention operator. The method according to the embodiment of the present disclosure also includes: obtaining a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B1, S1, H1]; determining to allocate the data of the first axis of the key tensor into M1 copies and not allocate the data of the second axis of the key tensor, and to allocate the data of the first axis of the value tensor into M1 copies and not allocate the data of the second axis of the value tensor; and using the T computing units of the processor to perform parallel calculations on the allocated key tensor and value tensor.
[0008] According to some embodiments of the present disclosure, the attention operator is a grouped attention operator, and the first tensor is a query tensor in the grouped attention operator. The method according to the embodiment of the present disclosure also includes: obtaining a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B2, S1, H1], wherein B2 is an integer greater than or equal to 1; determining to allocate the data of the first axis of the key tensor into M2 copies and not allocate the data of the second axis of the key tensor, and to allocate the data of the first axis of the value tensor into M2 copies and not allocate the data of the second axis of the value tensor; and using the T computing units of the processor to perform parallel calculations on the allocated key tensor and value tensor.
[0009] According to some embodiments of the present disclosure, determining to allocate data of the first axis of the key tensor into M2 copies and not allocate data of the second axis of the key tensor, and to allocate data of the first axis of the value tensor into M2 copies and not allocate data of the second axis of the value tensor includes: when B2 is equal to B1, M2=M1; and when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T.
[0010] According to some embodiments of the present disclosure, when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T includes: when B2 is greater than or equal to T, M2 is equal to T; and when B2 is less than T, M2 is equal to B2.
[0011] According to another aspect of the present disclosure, a processor is also provided, which includes T computing units, and the processor is configured to: obtain a first tensor to be calculated in parallel, wherein the shape of the first tensor is [B1, S1, H1], wherein B1, S1, H1 represent the data dimensions of the first tensor on the first axis, the second axis, and the third axis, respectively; determine to distribute the data of the first axis of the first tensor into M1 parts and distribute the data of the second axis of the first tensor into N1 parts according to the number T of computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, so that the product of M1 and N1 is equal to T; and according to the determination, use the T computing units of the processor to perform parallel calculations on the first tensor, wherein T is an integer greater than 1, and B1, S1, H1 are integers greater than or equal to 1.
[0012] According to some embodiments of the present disclosure, the first tensor is a query tensor in an attention operator, and the attention operator includes one of the following: a fast attention operator and a grouped attention operator.
[0013] According to some embodiments of the present disclosure, based on the number T of computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determining to allocate the data of the first axis of the first tensor into M1 parts and to allocate the data of the second axis of the first tensor into N1 parts so that the product of M1 and N1 is equal to T includes: when B1 is greater than or equal to T, M1 is equal to T, and N1 is equal to 1; and when B1 is less than T, M1 is equal to B1, and N1 is equal to T / B1.
[0014] According to some embodiments of the present disclosure, the attention operator is a fast attention operator, the first tensor is a query tensor in the fast attention operator, and the processor is further configured to: obtain a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B1, S1, H1]; determine to allocate the data of the first axis of the key tensor into M1 copies and not allocate the data of the second axis of the key tensor, and allocate the data of the first axis of the value tensor into M1 copies and not allocate the data of the second axis of the value tensor; and use the T computing units of the processor to perform parallel calculations on the allocated key tensor and value tensors.
[0015] According to some embodiments of the present disclosure, the attention operator is a grouped attention operator, the first tensor is a query tensor in the grouped attention operator, and the processor is further configured to: obtain a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B2, S1, H1], wherein B2 is an integer greater than or equal to 1; determine to distribute the data of the first axis of the key tensor into M2 parts and not distribute the data of the second axis of the key tensor, and distribute the data of the first axis of the value tensor into M2 parts and not distribute the data of the second axis of the value tensor; and use the T computing units of the processor to perform parallel calculations on the distributed key tensor and value tensors.
[0016] According to some embodiments of the present disclosure, determining to allocate data of the first axis of the key tensor into M2 copies and not allocate data of the second axis of the key tensor, and to allocate data of the first axis of the value tensor into M2 copies and not allocate data of the second axis of the value tensor includes: when B2 is equal to B1, M2=M1; and when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T.
[0017] According to some embodiments of the present disclosure, when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T includes: when B2 is greater than or equal to T, M2 is equal to T; and when B2 is less than T, M2 is equal to B2.
[0018] According to another aspect of the present disclosure, an electronic device is also provided. The electronic device according to an embodiment of the present disclosure includes a processor and a non-transitory memory, wherein a computer-readable code is stored in the non-transitory memory, and when the computer-readable code is executed by the processor, the processor executes the steps of the parallel computing method for attention operators in a neural network as described above.
[0019] According to another aspect of the present disclosure, a non-temporary computer-readable storage medium is provided, on which computer-readable instructions are stored. When the computer-readable instructions are executed by a processor, the processor executes the steps of the parallel computing method for attention operators in a neural network as described above.
[0020] By using the parallel computing method, processor, electronic device and medium of the attention operator provided by the embodiment of the present disclosure, according to the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor [B1, S1, H1] and the data dimension S1 of the second axis, it is determined that the data of the first axis of the first tensor is allocated to M1 parts and the data of the second axis of the first tensor is allocated to N1 parts, so that the product of M1 and N1 is equal to T, so that according to the above-determined allocation strategy, the first tensor is calculated in parallel using the T computing units of the processor, thereby being able to comprehensively determine the parallel allocation strategy of the tensor according to the number of computing units of the processor and the shape of the tensor, so as to make full use of the computing resources of the computing units in the processor and improve the parallel computing performance. By using the parallel computing method of the attention operator provided according to the embodiment of the present disclosure, it can be applicable to scenarios of various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0022] Figure 1 A schematic flow chart of a parallel computing method of an attention operator according to an embodiment of the present disclosure is shown;
[0023] Figure 2 A schematic diagram of a query tensor, a key tensor, and a value tensor according to some embodiments of the present disclosure is shown;
[0024] Figure 3 According to some embodiments of the present disclosure, Figure 2 Schematic diagram of parallel resource allocation for query tensors shown;
[0025] Figure 4 Another schematic diagram of a query tensor, a key tensor, and a value tensor according to some embodiments of the present disclosure is shown;
[0026] Figure 5 According to some embodiments of the present disclosure, Figure 4 Schematic diagram of parallel resource allocation for query tensors shown;
[0027] Figure 6 A schematic block diagram of a processor according to some embodiments of the present disclosure is shown;
[0028] Figure 7A schematic block diagram of an electronic device according to some embodiments of the present disclosure is shown;
[0029] Figure 8 A block diagram of an example computing device implementing some embodiments of the present disclosure is shown;
[0030] Fig. 9 A schematic block diagram of a computer-readable storage medium according to some embodiments of the present disclosure is shown. DETAILED DESCRIPTION
[0031] The technical solutions in the embodiments of the present disclosure will be described clearly and completely below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0032] In addition, as shown in the present disclosure and claims, unless the context clearly indicates an exception, the words "a", "an", "a kind" and / or "the" do not specifically refer to the singular, but may also include the plural. The words "first", "second" and similar words used in the present disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components. Similarly, words such as "include" or "comprise" mean that the elements or objects appearing before the word include the elements or objects listed after the word and their equivalents, without excluding other elements or objects. Words such as "connect" or "connected" and similar words are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect.
[0033] Flowcharts are used in this disclosure to illustrate the steps of the method according to the embodiments of the present disclosure. It should be understood that the preceding or following steps are not necessarily performed precisely in order. On the contrary, various steps may be processed in reverse order or simultaneously. At the same time, other operations may also be added to these processes.
[0034] With the development of broader general computing scenarios such as artificial intelligence training, reasoning, and scientific computing, higher and higher requirements are placed on the data processing efficiency of processor chips. The processing method of parallel computing of multiple computing units can provide faster data processing rates.
[0035] In the field of artificial intelligence, attention mechanisms are widely used in neural network models. As an example, various attention operators are widely used in large language neural network models (e.g., Transformer). As another example, attention mechanisms can also be applied to neural networks in fields such as speech processing, image processing, text processing, video processing, etc., without limitation here.
[0036] However, since the running time and memory complexity of the attention operator are quadratically related to the sequence length, the neural network model that includes the attention operator has defects such as long running time and high memory overhead when processing long sequences.
[0037] It is understood that the professional terms, nouns, etc. described herein have meanings known to those skilled in the art. To facilitate understanding of the disclosed solution, the terms that may be involved in at least some embodiments of the disclosed solution are explained as follows.
[0038] Tensor is a multi-dimensional array data structure. In the field of deep learning, the dimensions of input data, intermediate data processed during deep learning, and output data are flexible and not exact, so a flexible data form is needed to describe various types of data, which gives rise to the concept of tensor. In the field of deep learning, all data to be calculated are stored and exist in the form of tensors. If the data is not in the form of tensors, it needs to be converted into the data structure of tensors first.
[0039] The calculation process of the Attention operator involves three tensors, namely the query tensor (QTensor), the key tensor (KTensor), and the value tensor (VTensor). The shapes of the Q Tensor, K Tensor, and V Tensor of the Attention operator can be expressed as three axes. The first axis corresponds to the batch dimension (Batch), the second axis corresponds to the sequence dimension (Sequence), and the third axis corresponds to the head size dimension (Headsize), which can be expressed as [Batch, Sequence, Headsize]. During the calculation process, the dot product of Q Tensor and K Tensor represents the similarity between Q Tensor and K Tensor. Then, the calculation results of Q Tensor and K Tensor are normalized by using a normalization function (for example, softmax). Therefore, the normalized result is a mask matrix (Mask Matrix) with a value range of [0-1], which can also be understood as an attention score (AttentionScore) matrix. V Tensor represents the features after linear transformation of the input, and then the normalized mask matrix is calculated with VTensor to obtain the filtered V features to act on the output results. It can be understood that the introduction of Q Tensor and KTensor is to obtain a mask matrix with a value range of [0-1], and the introduction of V Tensor is to retain the features of the input.
[0040] Flash Attention operator, in order to overcome the defects of long execution time and large memory overhead in the Attention operator of the neural network model for processing long sequences, the related technology proposes a fast attention operator (FlashAttention), as an accurate attention algorithm for optimizing the input / output (I / O) memory access overhead in the calculation process of the attention operator. It reduces the number of memory read / writes between the high bandwidth memory (HBM) of the processor (for example, the graphics processing unit (GPU)) and the on-chip static random access memory (SRAM) of the processor through tiling, thereby reducing execution time and memory overhead. Flash Attention has a significant performance improvement in terms of model training time and model quality. For Flash Attention, it can use multiple computing units of the processor to perform parallel calculations on Q Tensor, K Tensor, and V Tensor to improve computing efficiency. In addition, for the Flash Attention operator, the data shapes of QTensor, K Tensor, and V Tensor are the same, that is, the values of [Batch, Sequence, Headsize] are the same.
[0041] The Group Attention operator is based on Flash Attention. The main idea is to enhance the diversity of features input to the attention head. Unlike Self-Attention, it provides different input splits for each attention head and cascades output features across heads to reduce computational redundancy in multi-head attention. In addition, it increases model capacity by increasing network depth. Compared with Flash Attention, for Group Attention, the data shapes of Q Tensor, K Tensor, and V Tensor can be different, that is, the values of [Batch, Sequence, Headsize] are different. Specifically, the Batch dimension of K Tensor and V Tensor can be different from the Batch dimension of Q Tensor, while for the Sequence and Headsize dimensions, the values of Q Tensor, K Tensor, and V Tensor are the same.
[0042] For the Attention operator, the parallel strategy is to distribute the computation of the Attention operator to multiple computing units such as a GPU for parallel computing. Specifically, in the related art, each tensor of the Attention operator is usually split by the Batch dimension, so that the computation of the Q Tensor, K Tensor, and V Tensor is distributed to multiple computing units according to the Batch data dimension.
[0043] However, due to the characteristics of the Batch data dimension itself, one computing unit needs to process a complete Batch of data. This means that in some cases, splitting the parallel computing according to the Batch dimension cannot fully utilize the GPU's computing resources, and there are inefficient computing scenarios such as some computing units waiting to idle or repeating calculations. For example, when the number of GPU computing units is 16 and the Batch dimension of the first axis of the Q Tensor is 8, the computing amount of the Q Tensor, K Tensor, and V Tensor can only be allocated to 8 computing units according to 8 batches, and the remaining 8 computing units do not participate in parallel computing, which will not fully utilize the parallel computing resources of the processor. That is, when the number of batches is less than the number of computing units, splitting the Batch dimension cannot fully utilize the resources of the computing units, wasting computing power.
[0044] In addition, the above method of splitting data according to the Batch dimension for parallel computing cannot be adapted to the GroupAttention operator. This is because in the calculation process of the GroupAttention operator, the Batch dimension of K Tensor and V Tensor may be different from the Batch dimension of Q Tensor, so it is impossible to uniformly split according to the Batch dimension.
[0045] In view of the technical problems existing in the parallel computing of the above Attention operator, the present disclosure provides a parallel computing method, processor, electronic device and medium of the attention operator. According to the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor [B1, S1, H1] and the data dimension S1 of the second axis, it is determined to allocate the data of the first axis of the first tensor into M1 parts and the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T, so that according to the above-determined allocation strategy, the T computing units of the processor are used to perform parallel computing on the first tensor, thereby comprehensively determining the parallel allocation strategy of the tensor according to the number of computing units of the processor and the shape of the tensor, so as to make full use of the computing resources of the computing units in the processor and improve the parallel computing performance. The parallel computing method provided according to the embodiment of the present disclosure can be applied to scenes of various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing.
[0046] Next, the implementation scheme of the parallel computing method for the attention operator in the neural network according to the embodiment of the present disclosure will be described in detail with reference to the accompanying drawings.
[0047] Figure 1 A schematic flow chart of a parallel computing method of an attention operator in a neural network according to an embodiment of the present disclosure is shown. As some implementations, the neural network is a natural language processing neural network, or a neural network model for natural language processing. As an example, it can be a Transformer network model, such as a ChatGenerative Pre-trained Transformer (Ghat-GPT) network model, etc. It can be any neural network model including an attention operator, which is not limited here. The attention operator involved in the embodiment of the present disclosure can be a conventional attention operator, such as a multi-head attention operator (Multi-head Attention, MHA), which adopts a standard multi-head attention mechanism. In addition, the attention operator involved in the embodiment of the present disclosure can also be the Flash Attention operator, Group Attention operator, etc. described above, or other types of Attention operators or Attention operators that may be developed in the future, which are not limited here. It can be understood that the Attention operator corresponding to the embodiment of the present disclosure can be any Attention form involving Q Tensor, K Tensor, and V Tensor in the calculation process.
[0048] Further, the parallel computing method according to the embodiment of the present disclosure is applicable to a processor including T computing units, where T is an integer greater than 1. As an example, T may be an integer equal to 2, 4, 8 or 16, etc., without limitation. In some implementations, the processor may be a graphics processor including 16 computing units (T=16) or a dedicated artificial intelligence computing chip or other structure. By using these computing units, parallel computing of a large amount of data can be realized, and computing efficiency can be improved. The computing power of the processor is particularly important in the field of artificial intelligence.
[0049] like Figure 1 As shown, the parallel computing method according to the embodiment of the present disclosure includes steps S101-S103. First, in step S101, a first tensor to be parallelized is obtained, wherein the shape of the first tensor is [B1, S1, H1], wherein B1, S1, H1 represent the data dimensions of the first tensor on the first axis, the second axis, and the third axis, respectively, wherein B1, S1, H1 are integers greater than or equal to 1.
[0050] According to some embodiments of the present disclosure, the first tensor [B1, S1, H1] is a query tensor (QTensor) in the attention operator, B1 indicates that the data dimension of the Q Tensor in the Batch axis is B1, S1 indicates that the data dimension of the Q Tensor in the Sequence axis is S1, and H1 indicates that the data dimension of the Q Tensor in the Headsize axis is H1. Furthermore, the attention operator includes one of the following: a fast attention operator (Flash Attention) and a grouped attention operator (GroupAttention). That is, according to an embodiment of the present disclosure, the first tensor may be a QTensor in Flash Attention or a Q Tensor in Group Attention. In other implementations, according to an embodiment of the present disclosure, the first tensor may also be a Q Tensor in other attention operators.
[0051] It can be understood that obtaining the first tensor can be understood as the processor obtaining the storage location of the first tensor in the memory. Prior to this, the first tensor can be a tensor obtained by mapping (Embedding) the input data of the neural network model. Usually, in the natural language network model, the amount of data of the first tensor is large. As an example, the shape of the first tensor can be [32, 2048, 128]. In order to increase the data processing speed, parallel computing can be used, that is, the data processing process of the first tensor is distributed to multiple computing units, and the calculations are performed simultaneously to improve the computing efficiency. In order to perform parallel computing, the processor needs to first distribute the data of the first tensor according to the computing units so that each computing unit can find the corresponding data content or data storage location to be processed.
[0052] Next, in step S102, according to the number T of the computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, it is determined to allocate the data of the first axis of the first tensor into M1 parts and to allocate the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T. In step S103, according to the above determination, the first tensor is calculated in parallel using the T computing units of the processor.
[0053] It can be seen that in the allocation strategy of the first tensor according to the embodiment of the present disclosure, the number T of the processor's operating units, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor are comprehensively considered, and the final allocation result satisfies that the product of M1 and N1 is equal to T, that is, the Batch and Sequence calculation processing of the first tensor is allocated to all the operating units of the processor (that is, T operating units) to make full use of the computing resources of each operating unit in the processor and improve the parallel computing performance. By reasonably allocating the computing resources of the processor, the parallel computing effect is optimized and resource waste in parallel computing is avoided.
[0054] Next, the specific allocation strategy of the first tensor will be described.
[0055] According to some embodiments of the present disclosure, in the above step S102, based on the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, it is determined to allocate the data of the first axis of the first tensor into M1 parts and to allocate the data of the second axis of the first tensor into N1 parts so that the product of M1 and N1 is equal to T, including: when B1 is greater than or equal to T, M1 is equal to T, and N1 is equal to 1; and when B1 is less than T, M1 is equal to B1, and N1 is equal to T / B1.
[0056] As a case, when B1 is greater than or equal to T, for example, B1=32, T=16, the allocation strategy of the first tensor is: divide the data of the Batch axis into M1=T=16 parts, and divide the data of the Sequence axis into 1 part. In this case, since the amount of data in the Batch axis is large, only the data of the Batch axis is divided, and the data of the Sequence axis is not divided (because N1=1 part), that is, each operation unit processes 2 Batch data and the complete Sequence data.
[0057] In addition, it should be noted that in practical applications, since the amount of data in the Headsize axis is generally small, splitting the data in the Headsize axis is usually not considered.
[0058] As another case, when B1 is less than T, for example, B1=8, T=16, the allocation strategy of the first tensor is: divide the data of the Batch axis into M1=B1=8 parts, and divide the data of the Sequence axis into N1T / B1=2 parts. In this case, since the amount of data on the Batch axis is small, only splitting the data of the Batch axis is not enough to fully utilize the computing resources of the 16 operation units. Therefore, the data of the Sequence axis is further split, and the number of splits is N1=2. In this case, both operation units process 1 Batch data. For example, operation unit 0 and operation unit 1 both process the complete Batch-0 data. Further, operation unit 0 processes the first half of the data on the Sequence axis, and operation unit 1 processes the second half of the data on the Sequence axis. For specific parallel strategies, please refer to the following text in conjunction with Figure 2-Figure 5 Description.
[0059] The parallel processing allocation strategy of the first tensor obtained according to the parallel computing method of the attention operator provided in accordance with the embodiment of the present disclosure can be applicable to scenarios of various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing.
[0060] According to some implementations of the present disclosure, the attention operator is a fast attention operator, the first tensor is a query tensor in the fast attention operator (i.e., the Q Tensor in Flash Attention), and the parallel computing method according to the present disclosure further includes: obtaining a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B1, S1, H1]. In this part of the implementation, the parallel allocation scheme for the data of K Tensor and VTensor in Flash Attention is further defined. In Flash Attention, the data shapes of Q Tensor, K Tensor, and V Tensor are the same, that is, the values of [Batch, Sequence, Headsize] are the same, and thus, the shapes of the key tensor and the value tensor obtained are both [B1, S1, H1].
[0061] Next, the parallel computing method according to the present disclosure also includes: determining to allocate the data of the first axis of the key tensor into M1 copies and not allocate the data of the second axis of the key tensor, and to allocate the data of the first axis of the value tensor into M1 copies and not allocate the data of the second axis of the value tensor; and using the T computing units of the processor to perform parallel computing on the allocated key tensor and value tensor.
[0062] Specifically, for the case of Flash Attention, since the data shapes of Q Tensor, K Tensor, and V Tensor are the same, the Batch axes of K Tensor and V Tensor can be split according to the allocation scheme determined for Q Tensor, that is, the data of the first axis of the key tensor is allocated to M1 parts and the data of the first axis of the value tensor is allocated to M1 parts.
[0063] Further, in the Attention operator, since the data of the Sequence axis of K Tensor and V Tensor is only used as an intermediate calculation process, the data of the Sequence axis of K Tensor and V Tensor cannot be split according to the operation unit, that is, the Sequence data of K Tensor and V Tensor cannot be split and processed by different operation units, so each operation unit processes the entire Sequence data of K Tensor and V Tensor. Therefore, in the method according to the embodiment of the present disclosure, the data of the second axis of the key tensor is not allocated and the data of the second axis of the value tensor is not allocated.
[0064] According to some other implementations of the present disclosure, the attention operator is a group attention operator, the first tensor is a query tensor in the group attention operator (i.e., the Q Tensor in Group Attention), and the parallel computing method according to the present disclosure further includes: obtaining a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B2, S1, H1], wherein B2 is an integer greater than or equal to 1. In this partial implementation, the parallel allocation scheme for the data of K Tensor and V Tensor in Group Attention is further defined. In Group Attention, the data shapes of Q Tensor, K Tensor, and V Tensor can be different, that is, the values of [Batch, Sequence, Headsize] can be different, and thus, the shapes of the key tensor and the value tensor obtained are [B2, S1, H1]. Specifically, for Group Attention, the batch dimension of K Tensor and V Tensor can be different from the batch dimension of Q Tensor, while for the sequence and headsize dimensions, the values of Q Tensor, K Tensor, and V Tensor are the same.
[0065] Next, the parallel computing method according to the present disclosure also includes: determining to distribute the data of the first axis of the key tensor into M2 parts and not distribute the data of the second axis of the key tensor, and to distribute the data of the first axis of the value tensor into M2 parts and not distribute the data of the second axis of the value tensor; and using the T computing units of the processor to perform parallel computing on the distributed key tensor and value tensor.
[0066] Specifically, for the case of Group Attention, since the data shapes of Q Tensor, K Tensor, and V Tensor may be different, the above determination to allocate the data of the first axis of the key tensor as M2 parts and not allocate the data of the second axis of the key tensor, and to allocate the data of the first axis of the value tensor as M2 parts and not allocate the data of the second axis of the value tensor includes: when B2 is equal to B1, M2=M1; and when B2 is not equal to B1, the value of M2 is determined based on the comparison result of B2 and T. That is, when B2 is equal to B1, this is similar to the case of Flash Attention, and thus, the Batch axis of KTensor and V Tensor can be split according to the allocation scheme determined for Q Tensor, that is, the data of the first axis of the key tensor is allocated as M1 parts and the data of the first axis of the value tensor is allocated as M1 parts. When B2 is not equal to B1, the value of M2 is determined based on the comparison result of B2 and T.
[0067] According to an embodiment of the present disclosure, when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T includes: when B2 is greater than or equal to T, M2 is equal to T; and when B2 is less than T, M2 is equal to B2. This part of the allocation process is similar to the process of determining the value of M1 based on the comparison result of B1 and T, and will not be repeated here.
[0068] The parallel computing method of the attention operator provided according to the embodiment of the present disclosure can be applied to scenarios with various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing. Take the number of computing units in the processor as T=16 as an example. In case 1, the shape of the Q Tensor of the Attention operator is [8, 1024, 128]. According to the parallel computing method provided according to the present disclosure, the Batch dimension can be divided into 8 parts and the Sequence dimension can be divided into 2 parts, for a total of 8 2 = 16, so that all computing units can be fully utilized for parallel computing; in case 2, the shape of the Q Tensor of the Attention operator is [4, 1024, 128]. According to the parallel computing method provided in the present disclosure, the Batch dimension can be divided into 4 parts, and the Sequence dimension can be divided into 4 parts, for a total of 4 4 = 16, so that all the computing units can be fully utilized for parallel computing; in case 3, the shape of the Q Tensor of the Attention operator is [2,1024, 128]. According to the parallel computing method provided in the present disclosure, the Batch dimension can be split into 2 parts, and the Sequence dimension can be split into 8 parts, for a total of 2 8 = 16, so that all computing units can be fully utilized for parallel computing; in case 4, the shape of the Q Tensor of the Attention operator is [32, 1024, 128]. According to the parallel computing method provided in the present disclosure, the Batch dimension can be split into 16 parts, and the Sequence dimension can be split into 1 part, for a total of 16 1 = 16, so that all computing units can be fully utilized for parallel computing.
[0069] Figure 2 Schematic diagrams of Q Tensor, K Tensor, and V Tensor according to some embodiments of the present disclosure are shown. Figure 3 According to some embodiments of the present disclosure, Figure 2 The schematic diagram of Q Tensor parallel resource allocation shown below will be combined with Figure 2 and Figure 3Describe the parallel processing strategy for Q Tensor, K Tensor, and V Tensor in Flash Attention. Specifically, Figure 2 and Figure 3 In the example, T=16, and the shapes of Q Tensor, K Tensor, and V Tensor are all [4,2048, 128].
[0070] like Figure 2 As shown in Figure 1, for Flash Attention, the data shapes of Q Tensor, K Tensor, and V Tensor are the same, that is, the values of [Batch, Sequence, Headsize] are the same, for example, they are all [B1, S1, H1]. Figure 2 4 shows the case when B1 is equal to 4, that is, Q Tensor, K Tensor, and V Tensor all include 4 batches, which are shown as Batch-0, Batch-1, Batch-2, and Batch-3, respectively. In order to distinguish different batches, they are displayed as different filling shapes. In addition, although Q Tensor, K Tensor, and V Tensor, such as Batch-0, are shown as the same filling shape, it can be understood that the specific values therein are different.
[0071] Next, refer to Figure 3 , which shows a processor scenario of T=16, that is, the processor includes 16 computing units (shown as C-0 to C-15) for parallel computing. Figure 2 In Q Tensor, since the Batch value is equal to 4, the data of QTensor on the Batch axis is divided into 4 parts. Further, the Sequence axis is divided into 4 parts, that is, the 2048-dimensional data is divided into 4 parts and processed by different computing units. Figure 3 , for the computing units C-0 to C-3, all process the entire block of Batch-0 data, and further, C-0 to C-3 process 1 / 4 of the data of the Sequence axis respectively (shown as 512). Figure 3 For operation units C-4 to C-7, all of them process the entire Batch-1 data. Furthermore, C-4 to C-7 process 1 / 4 of the Sequence axis data respectively. The parallel computing scheme for operations C-8 to C-15 is analogous to this.
[0072] According to Figure 3The parallel computing solution shown can use the 16 computing units of the processor to complete the calculation processing of the Q Tensor in parallel, and can fully utilize the computing resources, avoiding the waste of computing resources caused by only splitting the Batch axis data.
[0073] In addition, references such as Figure 3 The parallel computing scheme shown can also similarly realize parallel processing of KTensor and VTensor data of Flash Attention. Since the Sequence axis data of KTensor and VTensor are not split, the operation units C-0 to C-3 process the complete Batch-0 data and the complete Sequence data of KTensor and VTensor, and the operation units C-4 to C-7 process the complete Batch-1 data and the complete Sequence data of KTensor and VTensor, and so on.
[0074] Figure 4 Another schematic diagram of Q Tensor, K Tensor, and V Tensor according to some embodiments of the present disclosure is shown. Figure 5 According to some embodiments of the present disclosure, Figure 4 The schematic diagram of Q Tensor parallel resource allocation shown below will be combined with Figure 4 and Figure 5 Describe the parallel processing strategy for the Q Tensor, K Tensor, and V Tensor in Group Attention. Specifically, Figure 4 and Figure 5 In the example, T=16, the shape of Q Tensor is [8, 2048, 128], and the shapes of KTensor and V Tensor are both [4, 2048, 128], that is, B1 is not equal to B2.
[0075] refer to Figure 4For Group Attention, the data shapes of Q Tensor, K Tensor, and V Tensor are different. For Q Tensor, Batch-0, Batch-1, Batch-2, Batch-3, Batch-4, Batch-5, Batch-6, and Batch-7 are shown, with a total of 8 batches. In order to distinguish different batches, different filling shapes are displayed. For KTensor and V Tensor, Batch-0, Batch-1, Batch-2, and Batch-3 are shown, with a total of 4 batches. In addition, although Batch-0 of QTensor, K Tensor, and V Tensor is shown as the same filling shape, it can be understood that the specific values therein are different.
[0076] Next, refer to Figure 5 , which shows a processor scenario of T=16, that is, the processor includes 16 computing units (shown as C-0 to C-15) for parallel computing. Figure 4 In Q Tensor, since the Batch value is equal to 8, the data of QTensor on the Batch axis is divided into 8 parts. Furthermore, the Sequence axis is split into 2 parts, that is, the 2048-dimensional data is divided into 2 parts and processed by different computing units. Figure 5 , for the computing units C-0 to C-1, all process the entire block of Batch-0 data, and further, C-0 to C-1 process 1 / 2 of the data of the Sequence axis respectively (shown as 1024). Figure 5 For operation units C-2 to C-3, all of them process the entire Batch-1 data. Furthermore, C-2 to C-3 process 1 / 2 of the Sequence axis data respectively. The parallel computing scheme for operations C-4 to C-15 is similar.
[0077] According to Figure 5 The parallel computing solution shown can use the 16 computing units of the processor to complete the calculation processing of the Q Tensor in parallel, and can fully utilize the computing resources, avoiding the waste of computing resources caused by only splitting the Batch axis data.
[0078] In addition, references such as Figure 5The parallel computing scheme shown can also similarly realize the parallel processing of the KTensor and VTensor data of Group Attention. Since the Sequence axis data of KTensor and VTensor are not split, and the number of batches of KTensor and VTensor is 4, it can be understood that the operation units C-0 to C-3 all process the complete Batch-0 data and the complete Sequence data of KTensor and VTensor, and the operation units C-4 to C-7 all process the complete Batch-1 data and the complete Sequence data of KTensor and VTensor, and so on.
[0079] In the parallel computing method of the attention operator provided according to the embodiment of the present disclosure, according to the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor [B1, S1, H1], and the data dimension S1 of the second axis, it is determined to allocate the data of the first axis of the first tensor into M1 parts and the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T, so that according to the above-determined allocation strategy, the first tensor is parallelly calculated using the T computing units of the processor, thereby being able to comprehensively determine the parallel allocation strategy of the tensor according to the number of computing units of the processor and the shape of the tensor, so as to make full use of the computing resources of the computing units in the processor and improve the parallel computing performance. The parallel computing method of the attention operator provided according to the embodiment of the present disclosure can be applied to scenarios of various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing.
[0080] According to another aspect of the present disclosure, a processor is also provided. Figure 6 A schematic block diagram of a processor according to some embodiments of the present disclosure is shown.
[0081] like Figure 6 As shown, the processor 1000 according to an embodiment of the present disclosure may include a plurality of operation units 1010 , for example, T operation units, where T is an integer greater than 1.
[0082] As an example, the processor 1000 may be a high-performance processing chip such as a graphics processing unit for processing big data such as a natural language model, etc. It is understandable that the processor 1000 may also include other components not shown to implement computing functions, which are not limited here. As an example, the operation unit 1010 may include components such as a vector computing core, a tensor computing core, and a main cache of the tensor computing core, which are not described in detail here.
[0083] Specifically, according to the embodiment of the present disclosure, the processor 1000 is configured to: obtain a first tensor to be calculated in parallel, wherein the shape of the first tensor is [B1, S1, H1], wherein B1, S1, H1 respectively represent the data dimensions of the first tensor on the first axis, the second axis and the third axis, and B1, S1, H1 are integers greater than or equal to 1; according to the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determine to distribute the data of the first axis of the first tensor into M1 parts and distribute the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T; and according to the determination, use the T computing units of the processor to perform parallel calculations on the first tensor.
[0084] According to some embodiments of the present disclosure, the first tensor is a query tensor in an attention operator, and the attention operator includes one of the following: a fast attention operator and a grouped attention operator.
[0085] According to some embodiments of the present disclosure, based on the number T of computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determining to allocate the data of the first axis of the first tensor into M1 parts and to allocate the data of the second axis of the first tensor into N1 parts so that the product of M1 and N1 is equal to T includes: when B1 is greater than or equal to T, M1 is equal to T, and N1 is equal to 1; and when B1 is less than T, M1 is equal to B1, and N1 is equal to T / B1.
[0086] According to some embodiments of the present disclosure, the attention operator is a fast attention operator, the first tensor is a query tensor in the fast attention operator, and the processor is further configured to: obtain a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B1, S1, H1]; determine to allocate the data of the first axis of the key tensor into M1 copies and not allocate the data of the second axis of the key tensor, and allocate the data of the first axis of the value tensor into M1 copies and not allocate the data of the second axis of the value tensor; and use the T computing units of the processor to perform parallel calculations on the allocated key tensor and value tensors.
[0087] According to some embodiments of the present disclosure, the attention operator is a grouped attention operator, the first tensor is a query tensor in the grouped attention operator, and the processor is further configured to: obtain a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B2, S1, H1], wherein B2 is an integer greater than or equal to 1; determine to distribute the data of the first axis of the key tensor into M2 parts and not distribute the data of the second axis of the key tensor, and distribute the data of the first axis of the value tensor into M2 parts and not distribute the data of the second axis of the value tensor; and use the T computing units of the processor to perform parallel calculations on the distributed key tensor and value tensors.
[0088] According to some embodiments of the present disclosure, determining to allocate data of the first axis of the key tensor into M2 copies and not allocate data of the second axis of the key tensor, and to allocate data of the first axis of the value tensor into M2 copies and not allocate data of the second axis of the value tensor includes: when B2 is equal to B1, M2=M1; and when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T.
[0089] According to some embodiments of the present disclosure, when B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T includes: when B2 is greater than or equal to T, M2 is equal to T; and when B2 is less than T, M2 is equal to B2.
[0090] According to some embodiments of the present disclosure, the neural network is a natural language processing neural network, and the processor is a graphics processor.
[0091] Specifically, the processor 1000 according to the embodiment of the present disclosure can implement the steps of the parallel computing method for the attention operator in the neural network according to the embodiment of the present disclosure described above in combination with the accompanying drawings, and achieve similar technical effects, which will not be repeated here.
[0092] According to yet another aspect of the present disclosure, an electronic device is provided. Figure 7 A schematic block diagram of an electronic device according to some embodiments of the present disclosure is shown.
[0093] like Figure 7 As shown, the electronic device 2000 may include a processor 2010 and a non-transitory memory 2020. According to some embodiments of the present disclosure, the non-transitory memory 2020 stores a computer-readable code, which, when executed by the processor 2010, may execute the steps of the parallel computing method for the attention operator in the neural network provided according to the embodiment of the present disclosure.
[0094] The processor 2010 can perform various actions and processes according to the program stored in the non-temporary memory 2020. Specifically, the processor 2010 can be any one of a GPU (Graphics Processing Unit), a TPU (Tensor Processing Unit), an NPU (Neural Network Processing Unit), a DPU (Deep Learning Processing Unit), an APU (Accelerated Processing Unit), and a GPGPU (General-Purpose computing on Graphics Processing Unit). In addition, it can also be any conventional processor. For example, the processor here can refer to a processor chip capable of parallel computing.
[0095] The non-transitory memory 2020 stores a computer-readable code, which, when executed by the processor 2010, can cause the processor to implement the steps of the parallel computing method for the attention operator in the neural network according to some embodiments of the present disclosure. The non-transitory memory 2020 can also be any suitable type of memory. As an example, by executing the computer-readable code in the non-transitory memory 2020, the processor can implement the steps of the parallel computing method for the attention operator in the neural network described above in conjunction with the accompanying drawings.
[0096] As an example, the parallel computing method or electronic device for attention operators in a neural network according to an embodiment of the present disclosure can also be used as follows: Figure 8 The exemplary computing device 3000 shown in FIG. Figure 8 As shown, the computing device 3000 may include a bus 3010, one or more central processing units (CPUs) 3020, a read-only memory (ROM) 3030, a random access memory (RAM) 3040, a communication port 3050 connected to a network, an input / output 3060, a hard disk 3070, etc. The storage device in the computing device 3000, such as the ROM 3030 or the hard disk 3070, may store various data or files required for the processing and / or communication of the method provided by the present disclosure and the program instructions executed by the CPU. The computing device 3000 may also include a user interface 3080. Of course, Figure 8 The architecture shown is only exemplary and can be omitted according to actual needs when implementing different devices. Figure 8 One or more components of a computing device are shown.
[0097] According to yet another aspect of the present disclosure, a non-transitory computer-readable storage medium is also provided. Fig. 9 A schematic diagram of a computer-readable storage medium according to some embodiments of the present disclosure is shown.
[0098] like Fig. 9 As shown, a computer-readable storage medium 4000 stores computer-readable instructions 4010. When the computer-readable instructions 4010 are executed by the processor, the parallel computing method for the attention operator in the neural network described with reference to the above figures can be executed. The computer-readable storage medium includes, but is not limited to, a volatile memory and / or a non-volatile memory. Volatile memory may include, for example, a random access memory (RAM) and / or a cache memory (cache), etc. Non-volatile memory may include, for example, a read-only memory (ROM), a hard disk, a flash memory, etc. For example, the computer-readable storage medium 4000 may be connected to a computing device such as a computer, and then, when the computing device runs the computer-readable instructions 4010 stored on the computer-readable storage medium 4000, the parallel computing method for the attention operator in the neural network provided according to the embodiment of the present disclosure as described above may be performed.
[0099] Some embodiments of the present disclosure provide a parallel computing method, processor, electronic device and medium of an attention operator, which are used to implement a parallel allocation strategy for tensors based on the number of computing units of the processor and the shape of the tensor, so as to fully utilize the computing resources of the computing units in the processor and improve the parallel computing performance.
[0100] By using the parallel computing method, processor, electronic device and medium of the attention operator provided by the embodiment of the present disclosure, according to the number T of the processor's computing units, the data dimension B1 of the first axis of the first tensor [B1, S1, H1] and the data dimension S1 of the second axis, it is determined that the data of the first axis of the first tensor is allocated to M1 parts and the data of the second axis of the first tensor is allocated to N1 parts, so that the product of M1 and N1 is equal to T, so that according to the above-determined allocation strategy, the first tensor is calculated in parallel using the T computing units of the processor, thereby being able to comprehensively determine the parallel allocation strategy of the tensor according to the number of computing units of the processor and the shape of the tensor, so as to make full use of the computing resources of the computing units in the processor and improve the parallel computing performance. By using the parallel computing method of the attention operator provided according to the embodiment of the present disclosure, it can be applicable to scenarios of various tensor shapes, reasonably allocate the computing resources of the processor, optimize the parallel computing effect, and avoid resource waste in parallel computing.
[0101] Those skilled in the art will appreciate that the contents disclosed in this disclosure may be subject to various variations and improvements. For example, the various devices or components described above may be implemented by hardware, or by software, firmware, or a combination of some or all of the three.
[0102] In addition, although the present disclosure makes various references to certain units in a computing device or computing apparatus according to an embodiment of the present disclosure, any number of different units may be used and run on a client and / or server. The units are merely illustrative, and different aspects of a parallel computing method, processor, electronic device, or computing device may use different units.
[0103] Those skilled in the art will appreciate that all or part of the steps in the above method can be completed by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk. Optionally, all or part of the steps in the above embodiment can also be implemented using one or more integrated circuits. Accordingly, each module / unit in the above embodiment can be implemented in the form of hardware or in the form of a software functional module. The present disclosure is not limited to any particular form of combination of hardware and software.
[0104] Unless otherwise defined, all terms used herein have the same meaning as commonly understood by those of ordinary skill in the art to which the present disclosure belongs. It should also be understood that terms such as those defined in common dictionaries should be interpreted as having a meaning consistent with their meaning in the context of the relevant technology, and should not be interpreted in an idealized or highly formal sense, unless explicitly defined as such herein.
[0105] The above is an explanation of the present disclosure and should not be considered as a limitation thereof. Although several exemplary embodiments of the present disclosure are described, it will be readily understood by those skilled in the art that many modifications may be made to the exemplary embodiments without departing from the novel teachings and advantages of the present disclosure. Therefore, all such modifications are intended to be included within the scope of the present disclosure as defined in the claims. It should be understood that the above is an explanation of the present disclosure and should not be considered to be limited to the specific embodiments disclosed, and modifications to the disclosed embodiments and other embodiments are intended to be included within the scope of the appended claims. The present disclosure is defined by the claims and their equivalents.
Claims
1. A parallel computing method for attention operators in neural networks, characterized in that: The method is applicable to a processor including T computing units, and the method includes: Get a first tensor to be parallelized, where the shape of the first tensor is [B1, S1, H1], where B1, S1, and H1 represent the data dimensions of the first tensor on the first axis, the second axis, and the third axis, respectively; According to the number T of the computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determine to allocate the data of the first axis of the first tensor into M1 parts and allocate the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T; and According to the determination, the first tensor is calculated in parallel using the T computing units of the processor, where T is an integer greater than 1, and B1, S1, and H1 are integers greater than or equal to 1.
2. The method according to claim 1, characterized in that The first tensor is a query tensor in the attention operator, and the attention operator includes one of the following: a fast attention operator and a grouped attention operator.
3. The method according to claim 1, characterized in that The determining, according to the number T of the computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, to distribute the data of the first axis of the first tensor into M1 parts and to distribute the data of the second axis of the first tensor into N1 parts so that the product of M1 and N1 is equal to T includes: When B1 is greater than or equal to T, M1 is equal to T, and N1 is equal to 1; and When B1 is less than T, M1 is equal to B1, and N1 is equal to T / B1.
4. The method according to claim 1, characterized in that The attention operator is a fast attention operator, the first tensor is a query tensor in the fast attention operator, and the method further includes: Get a key tensor and a value tensor associated with the query tensor, wherein the shapes of the key tensor and the value tensor are both [B1, S1, H1]; Determine to allocate M1 copies of data on the first axis of the key tensor and not allocate data on the second axis of the key tensor, and allocate M1 copies of data on the first axis of the value tensor and not allocate data on the second axis of the value tensor; and The T computing units of the processor are used to perform parallel calculations on the allocated key tensor and the value tensor.
5. The method according to claim 1, characterized in that The attention operator is a grouped attention operator, the first tensor is a query tensor in the grouped attention operator, and the method further includes: Get a key tensor and a value tensor associated with the query tensor, wherein the key tensor and the value tensor are both of shape [B2, S1, H1], where B2 is an integer greater than or equal to 1; Determine to allocate data of the first axis of the key tensor into M2 copies and not allocate data of the second axis of the key tensor, and allocate data of the first axis of the value tensor into M2 copies and not allocate data of the second axis of the value tensor; and The T computing units of the processor are used to perform parallel calculations on the allocated key tensor and the value tensor.
6. The method according to claim 5, characterized in that The determining to allocate the data of the first axis of the key tensor into M2 copies and not allocate the data of the second axis of the key tensor, and to allocate the data of the first axis of the value tensor into M2 copies and not allocate the data of the second axis of the value tensor comprises: In the case where B2 is equal to B1, M2=M1; and When B2 is not equal to B1, the value of M2 is determined based on the comparison result of B2 and T.
7. The method according to claim 6, characterized in that In the case that B2 is not equal to B1, determining the value of M2 based on the comparison result of B2 and T includes: When B2 is greater than or equal to T, M2 is equal to T; and When B2 is less than T, M2 is equal to B2.
8. A processor, characterized in that: The processor includes T computing units, and the processor is configured to: Get a first tensor to be parallelized, where the shape of the first tensor is [B1, S1, H1], where B1, S1, and H1 represent the data dimensions of the first tensor on the first axis, the second axis, and the third axis, respectively; According to the number T of the computing units of the processor, the data dimension B1 of the first axis of the first tensor, and the data dimension S1 of the second axis of the first tensor, determine to allocate the data of the first axis of the first tensor into M1 parts and allocate the data of the second axis of the first tensor into N1 parts, so that the product of M1 and N1 is equal to T; and According to the determination, the first tensor is calculated in parallel using the T computing units, where T is an integer greater than 1, and B1, S1, and H1 are integers greater than or equal to 1.
9. An electronic device, characterized in that: The electronic device comprises a processor and a non-transitory memory having computer readable code stored therein, wherein the computer readable code, when executed by the processor, causes the processor to perform the steps of the method as claimed in any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium having computer-readable instructions stored thereon, wherein when the computer-readable instructions are executed by a processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 7.
