Method and apparatus for processing tensor data

By adjusting the shape and loading method of tensor data, we solve the problem of performance degradation of artificial intelligence chips when processing large-resolution images or ultra-long data, improve the computing efficiency and stability of the chip, adapt to hardware limitations, and improve processing performance.

CN118503205BActive Publication Date: 2024-10-25北京壁仞科技开发有限公司 +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410954161.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-17
Publication Date
2024-10-25
Estimated Expiration
2044-07-17

AI Technical Summary

Technical Problem

When artificial intelligence chips process large-resolution images or ultra-long text and voice data, due to memory layout limitations, the performance of operators decreases, and computing resources and memory bandwidth cannot be fully utilized, affecting overall performance.

Method used

By adjusting the shape of the tensor data so that the data length on the first axis is shortened and expanded on the second axis, it adapts to the memory layout and is loaded into the computing unit for calculation according to the granularity of the data block, avoiding the need to implement operator algorithms. Significant changes have been made to improve calculation efficiency.

Benefits of technology

It improves the performance of artificial intelligence chip processing operators, especially in the processing of ultra-long sequence data with flexibility and adaptability, improves task performance in the fields of computer vision and natural language processing, and enhances the stability and maintainability of the chip.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118503205B_ABST
    Figure CN118503205B_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure disclose a method for processing tensor data, a device for processing tensor data, an electronic device, and a computer-readable storage medium. The method includes: in response to the data length of the tensor data on the first axis being greater than a preset threshold, determining an adjustment multiple corresponding to the first axis; adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by an adjustment multiple and the data length on the second axis is enlarged by an adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than a preset threshold; arranging the tensor data after the shape adjustment in the memory; and based on the adjustment multiple, loading the tensor data arranged in the memory into the computing unit according to the granularity of the data block for calculation. The present disclosure enables the artificial intelligence chip to efficiently process the calculation of the operator when the length of the input data of the operator on a certain axis exceeds the hardware limit.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of artificial intelligence technology, and more specifically to a method for processing tensor data, a device for processing tensor data, an electronic device, and a storage medium. Background Art

[0002] At present, artificial intelligence chips are developing rapidly and have been widely used in various fields. For example, in the field of computer vision (CV), artificial intelligence can perform tasks such as image classification, target detection, and semantic segmentation. In the field of natural language processing (NLP), artificial intelligence can realize functions such as machine translation, text classification, and sentiment analysis. In particular, the introduction of large language models has greatly optimized the execution effect of these tasks.

[0003] Whether in the field of computer vision or natural language processing, AI chips equipped with large language models need to process high-resolution images or very long text and voice data. However, the attention mechanism in large language models usually relies on specific operators to implement. The operating efficiency of these operators is largely affected by the design characteristics of the GPU memory in AI chips. Generally speaking, no matter how the layout of the AI ​​chip is changed, it will eventually restrict the arrangement of tensors in memory. Any restrictions on the arrangement of tensors in memory may cause the performance of AI operators (especially operators that implement attention mechanisms) to degrade when processing large-resolution images or very long text and voice data.

[0004] Specifically, in computer vision scenarios, a large amount of computing resources and memory bandwidth are required to process large-resolution images. However, due to the limitations of memory layout, computing resources and memory bandwidth are often not fully met, resulting in reduced computing efficiency. Similarly, in natural language processing scenarios, processing very long text and voice data also requires a large amount of computing resources and memory. In this case, the memory layout limitations of the GPU will lead to reduced execution efficiency of operators, thus affecting overall performance.

[0005] To this end, it is necessary to improve the implementation of specific operators in the software stack of the AI ​​chip to better and more universally adapt to the memory layout of the AI ​​chip. Summary of the invention

[0006] Embodiments of the present disclosure provide a method for processing tensor data, an apparatus for processing tensor data, an electronic device, and a storage medium.

[0007] An embodiment of the present disclosure provides a method for processing tensor data, including: in response to a data length of the tensor data on a first axis being greater than a preset threshold, determining an adjustment multiple corresponding to the first axis; adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; arranging the tensor data after the shape adjustment in a memory; and based on the adjustment multiple, loading the tensor data arranged in the memory into a computing unit for calculation according to the granularity of data blocks.

[0008] For example, adjusting the shape of the tensor data includes: splitting the tensor data into multiple sub-tensors along the first axis based on the adjustment multiple, and the number of the multiple sub-tensors is equal to the adjustment multiple; and arranging the multiple sub-tensors along the second axis.

[0009] For example, based on the adjustment multiple, the tensor data arranged in the memory is loaded into the computing unit for calculation according to the granularity of the data block, including: determining the position of the data block to be calculated in the memory based on the adjustment multiple and the data length of the sub-tensor on the second axis; and based on the position of the data block to be calculated in the memory, loading the data block into the computing unit for calculation.

[0010] For example, the tensor data is a query tensor, and determining the position of the data block to be calculated in the memory based on the adjustment multiple and the data length of the sub-tensor on the second axis also includes: in a process of performing a first linear multiplication operation, determining a first offset value based on the adjustment multiple and the size of the data block to be calculated; and determining the position of the data block to be calculated in the memory based on the first offset value.

[0011] For example, based on the position of the data block to be calculated in the memory, loading the data block into the computing unit for calculation also includes: in the process of performing a first linear multiplication operation, based on the position of the data block to be calculated in the memory, loading the data block into the computing unit to perform the first linear multiplication operation and obtain a first intermediate result; based on the first intermediate result, performing a nonlinear operation and a second linear multiplication operation to obtain a calculation result; based on the adjustment multiple and the size of the data block to be calculated, determining a second offset value; and based on the second offset value, determining the storage location of the calculation result, and storing the calculation result in the storage location corresponding to the memory.

[0012] For example, the tensor data is a key tensor or a value tensor, and determining the position of the data block to be calculated in the memory based on the adjustment multiple and the data length of the sub-tensor on the second axis also includes: in the process of performing both the first linear multiplication operation and the second linear multiplication operation, determining a third offset value based on the adjustment multiple and the size of the data block to be calculated; and determining the position of the data block to be calculated in the memory based on the third offset value.

[0013] For example, the step of loading the data block to be calculated into the computing unit for calculation based on the position of the data block to be calculated in the memory also includes: in the process of performing a first linear multiplication operation, based on the position of the data block to be calculated in the memory, loading the data block into the computing unit to perform the first linear multiplication operation and a nonlinear operation to obtain a second intermediate result; in the process of performing a second linear multiplication operation, based on the position of the data block to be calculated in the memory, loading the data block into the computing unit again to perform the second linear multiplication operation to determine the calculation result based on the second intermediate result and the data in the data block to be calculated.

[0014] For example, based on the position of the data block to be calculated in the memory, loading the data block into the computing unit for calculation also includes: in response to the product of the data length of the sub-tensor on the first axis and the adjustment multiple being greater than the data length of the tensor data on the first axis before adjustment, determining whether the data block is the last data block to be calculated; in response to the data block being the last data block to be calculated, determining the valid data in the data block based on the difference between the product of the data length of the sub-tensor on the first axis and the adjustment multiple and the data length of the tensor data on the first axis before adjustment; and performing calculation based on the valid data in the data block.

[0015] For example, the tensor data is one of a query vector, a key vector, and a value vector, the first axis is a sequence axis, and the second axis is a batch axis.

[0016] An embodiment of the present disclosure provides an apparatus for processing tensor data, including: a first module, configured to: in response to a data length of tensor data on a first axis being greater than a preset threshold, determine an adjustment factor corresponding to the first axis; a second module, configured to: adjust the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment factor and the data length on the second axis is enlarged by the adjustment factor, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; a third module, configured to: arrange the tensor data after the shape adjustment in a memory; and a fourth module, configured to: based on the adjustment factor, load the tensor data arranged in the memory into a computing unit according to the granularity of data blocks for calculation.

[0017] An embodiment of the present disclosure provides an electronic device, comprising: a processor; and a memory, wherein a computer executable program is stored in the memory, and when the computer executable program is executed by the processor, the above-mentioned method for processing tensor data is executed.

[0018] An embodiment of the present disclosure provides a device, including: a processor; a memory, wherein the memory stores computer instructions, and when the computer instructions are executed by the processor, the method for accessing memory data and / or the method for storing data in the memory is implemented.

[0019] An embodiment of the present disclosure provides a computer-readable storage medium having computer instructions stored thereon, which implement the above-mentioned method for processing tensor data when executed by a processor.

[0020] According to another aspect of the present disclosure, a computer program product or a computer program is provided, the computer program product or the computer program including computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable medium, and the processor executes the computer instructions, so that the computer device executes the methods provided in the above-mentioned various aspects or various optional implementations of the above-mentioned various aspects.

[0021] Various embodiments of the present disclosure provide a method and apparatus for processing tensor data, which optimizes the implementation of operators in the software stack of an artificial intelligence chip so that when the length of the input data of the operator on a certain axis exceeds the hardware limitation, the artificial intelligence chip can still efficiently process the calculation of the operator, thereby improving the performance of the artificial intelligence chip in processing operators.

[0022] In one aspect of an embodiment of the present disclosure, the implementation of an operator for implementing an attention mechanism is optimized. The embodiments of the present disclosure have significant flexibility and adaptability when processing ultra-long sequence data. Even if the data length on a certain axis of the operator for implementing the attention mechanism exceeds the upper limit of the tensor expression specification of the hardware design of the artificial intelligence chip (e.g., an AI accelerator or a GPU), the artificial intelligence chip can support its implementation. As a result, the embodiments of the present disclosure exhibit excellent performance in processing super-resolution tasks in the fields of computer vision (CV) and natural language processing (NLP), as well as complex scenarios such as ultra-long speech texts.

[0023] In one aspect of the embodiments of the present disclosure, by splitting the loop layer to process the memory mapping logic, a substantial change to the algorithm implementation of the operator itself is avoided. This not only ensures the basic functions and performance of the operator, but also greatly improves its reusability, thereby improving the stability and maintainability of the artificial intelligence chip. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] In order to more clearly illustrate the technical solution of the embodiment of the present disclosure, the following briefly introduces the drawings required for describing the embodiment. The drawings described below are only exemplary embodiments of the present disclosure.

[0025] Figures 1A to 1C A schematic diagram of an operator implementing an attention mechanism on an artificial intelligence chip is shown.

[0026] Figure 2 is a flowchart illustrating a method for processing tensor data according to an embodiment of the present disclosure.

[0027] Figure 3 is a schematic diagram illustrating a method for processing tensor data according to an embodiment of the present disclosure.

[0028] Figure 4 is a schematic diagram showing an electronic device according to an embodiment of the present disclosure.

[0029] Figure 5 is an architectural diagram illustrating a computing device according to an embodiment of the present disclosure.

[0030] Figure 6 is a schematic diagram illustrating a computer-readable storage medium according to an embodiment of the present disclosure. DETAILED DESCRIPTION

[0031] In order to make the purpose, technical solutions and advantages of the present disclosure more obvious, the exemplary embodiments according to the present disclosure will be described in detail with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all the embodiments of the present disclosure, and it should be understood that the present disclosure is not limited to the exemplary embodiments described here.

[0032] In this specification and the accompanying drawings, substantially the same or similar steps and elements are represented by the same or similar reference numerals, and repeated descriptions of these steps and elements will be omitted. At the same time, in the description of the present disclosure, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance or ranking.

[0033] To facilitate description of the present disclosure, concepts related to the present disclosure are introduced below.

[0034] Optionally, various models involved in the embodiments of the present disclosure may be artificial intelligence models, especially neural network models based on artificial intelligence. Typically, neural network models based on artificial intelligence are implemented as acyclic graphs in which neurons are arranged in different layers. Typically, a neural network model includes an input layer and an output layer, which are separated by at least one hidden layer. The hidden layer transforms the input received by the input layer into a representation useful for generating outputs in the output layer. Network nodes (i.e., neurons) are fully connected to nodes in adjacent layers via edges, and there are no edges between nodes in each layer. Data received at the nodes of the input layer of the neural network are propagated to the nodes of the output layer via any one of a hidden layer, an activation layer, a pooling layer, a convolutional layer, etc. The input and output of the neural network model can take various forms, and the present disclosure does not limit this.

[0035] Artificial intelligence models usually use deep learning algorithms. Deep learning algorithms are composed of computing modules, which are called operators (Operators, Op for short). An operator is a mapping from a function space to a function space O: X→X'; in a broad sense, any operation on a function can be considered an operator. An operator is different from a physical operator. It is a mapping through which an element of a vector space produces another element in another vector space (or possibly the same vector space) through this mapping (or module). Specifically, in the field of deep learning and artificial intelligence, an operator refers to a function that performs a mathematical operation or transformation on a tensor.

[0036] Common operators include: Convolution, Pooling, Fully Connected, Activation Function, Normalization, Loss Function, etc. These operators form the basic computing units of deep neural network models through combination and stacking. Different deep learning frameworks (such as TensorFlow and PyTorch) usually provide optimized operator implementations to improve the efficiency of model training and reasoning.

[0037] Operators used to implement attention mechanisms are widely used in computer vision and natural language processing. Specifically, the attention mechanism is a technology widely used in large language models, which allows large language models to provide different attention / weights to different parts of the input data when processing the input data to improve the performance of the large language model. For example, in the field of computer vision, the attention mechanism can make the large language model pay more attention to the important areas in the image to improve the task performance. In the field of natural language processing, the attention mechanism enables the large language model to dynamically select the most relevant parts of the input text, thereby improving the performance of the large language model.

[0038] However, the operators used to implement the attention mechanism often perform poorly when applied to artificial intelligence chips. Specifically, computer vision and natural language processing tasks usually require processing high-resolution images or very long text data. The implementation of the attention mechanism relies on a large number of matrix operations and memory accesses. Artificial intelligence chips inevitably need to limit memory layout and bandwidth, so the performance of the operators used to implement the attention mechanism is often poor.

[0039] To this end, it is necessary to optimize the implementation of operators in the software stack of artificial intelligence chips, especially the operators used to implement the attention mechanism, so as to better adapt to the hardware characteristics and improve the overall computing efficiency.

[0040] Therefore, an embodiment of the present disclosure provides a method for processing tensor data, including: in response to the data length of the tensor data on a first axis being greater than a preset threshold, determining an adjustment multiple corresponding to the first axis; adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; arranging the tensor data after the shape adjustment in a memory; and based on the adjustment multiple, loading the tensor data arranged in the memory into a computing unit for calculation according to the granularity of data blocks.

[0041] In this embodiment of the present disclosure, the implementation of the operator is optimized in the software stack of the artificial intelligence chip so that when the length of the input data of the operator on a certain axis exceeds the hardware limitation, the artificial intelligence chip can still efficiently process the calculation of the operator, thereby improving the performance of the artificial intelligence chip in processing the operator.

[0042] In addition, in one aspect of an embodiment of the present disclosure, the implementation of an operator for implementing an attention mechanism is optimized. The embodiments of the present disclosure have significant flexibility and adaptability when processing ultra-long sequence data. Even if the data length on a certain axis of the operator for implementing the attention mechanism exceeds the upper limit of the tensor expression specification of the AI ​​accelerator or GPU hardware design, the artificial intelligence chip can support its implementation. As a result, the embodiments of the present disclosure exhibit excellent performance in processing super-resolution tasks in the fields of computer vision (CV) and natural language processing (NLP), as well as complex scenarios such as ultra-long speech texts.

[0043] In one aspect of the embodiments of the present disclosure, by splitting the loop layer to process the memory mapping logic, a substantial change to the algorithm implementation of the operator itself is avoided. This not only ensures the basic functions and performance of the operator, but also greatly improves its reusability, thereby improving the stability and maintainability of the artificial intelligence chip.

[0044] Various embodiments of the present disclosure are described in detail through the following drawings.

[0045] Figures 1A to 1C A schematic diagram of an operator implementing an attention mechanism on an artificial intelligence chip is shown.

[0046] Assume that due to the memory limitation of a certain artificial intelligence chip (or AI accelerator or GPU), in a single operation on the hardware, the height and width (H / W) dimensions of a single matrix-shaped tensor that can participate in the operation are limited to no more than 8192 floating point numbers. The input data of the operator used to implement the attention mechanism includes three tensor data, namely query tensor data Q, key tensor data K, and value tensor data V. These three tensor data have three axes (batch axis, sequence axis, head size axis), among which the sequence axis is the longest. In the scenario where the length of the sequence axis does not exceed 8192, the flash attention mechanism is proposed to optimize the computing efficiency.

[0047] The following takes the length of the sequence axis as 4096, the length of the head size axis as 768, and the length of the batch axis as 1 as an example to briefly describe the operation process of the operator based on the flash attention mechanism.

[0048] One of the operational purposes of the operator based on the flash attention mechanism is to solve the softmax function in formula (1).

[0049] (1)

[0050] in, , . In can be calculated as: . Represents the i-th element in the dot product of the input tensors Q and K.

[0051] If the flash attention mechanism is not used, to implement the calculation in formula (1), the following calculations need to be performed: (1) The dot product operation between the input tensors Q and K. The result of the dot product can be recorded as the QK tensor. Since the shapes of the Q tensor and the K tensor are both , the shape of the QK tensor is one ; (2) After the QK tensor is subjected to the softmax operation, the tensor QK'=softmax(QK) is obtained. This process corresponds to formula (1). QK' will be point-multiplied with the V tensor to obtain the result. Among them, the shape of the V tensor is a Tensor of , thus we get a Tensor of .

[0052] If the flash attention mechanism is used, to implement the calculation in formula (1), it is necessary to execute Figures 1A to 1C The calculation process shown in .

[0053] First, in Figure 1A In the first step (Q, K, V matrix partitioning) shown, the input tensors Q, K, and V are divided according to different or the same granularity. For example, each tensor Q is divided according to the batch axis, the sequence axis, and the head size axis. Specifically, along the direction of the sequence axis, the input tensor Q can be split into 4 blocks (Trunks), and the length of the sequence axis of each block is 4096 / 4=1024. In addition, along the direction of the head size axis, the input tensor Q can be split into 12 blocks, and the head size of each block is 768 / 12=64. Thus, a block Q' of size 1024x64 is obtained. Similar operations can also be applied to tensors K and V.

[0054] In such Figure 1B In the second step (local attention calculation) shown in the figure, each block Q' is further divided into multiple square sub-blocks (Chunks), and the calculations are performed separately according to the granularity of the sub-blocks. The size of each sub-block is 64x64. The sub-blocks of tensor Q can be denoted as chunk_Q, the sub-blocks of tensor K as chunk_K, and the sub-blocks of tensor V as chunk_V. The i-th sub-block in tensor Q is Figure 1B The i-th sub-block in the tensor K is shown as Qi in Figure 1B The ith sub-block in the tensor V is shown as Ki in Figure 1BIn one iteration, the dot product of chunk_Q and chunk_K is solved. Then, chunk_QK'=sorftmax(chunk_QK) is solved, and then the matrix multiplication of chunk_QK' and the corresponding chunk_V is performed to obtain the partial attention output Partial_QKV. The Partial_QKV obtained in this iteration is combined with the partial attention output in the previous iteration. Add and update the maximum value vector obtained in softmax and sum vector .

[0055] In such Figure 1C In the third step (final attention calculation) shown, the updated tensor With the summation vector Divide element-wise to get the final attention output.

[0056] Next, refer to the following pseudo code-0 description Figures 1A to 1C Some details in .

[0057] Pseudo code -0:

[0058] for (int batch_idx=0;batch_idx <bs_per_spc;batch_idx++)

[0059] for (int h_idx=0;h_idx <H';h_idx +=trunkQ)

[0060] for (int iter=0;iter <W';iter +=trunkKV)

[0061] trunk_mma0_process()

[0062] trunk_flash_softmax_process()

[0063] trunk_mmal_process()

[0064] Pseudocode - 0 shows a three - layer loop structure. The outermost loop "for (int batch_idx = 0; batch_idx < bs_per_spc; batch_idx++)" traverses along the batch dimension, where bs_per_spc represents the batch size of each row in the QK tensor. The middle - layer loop "for (int h_idx = 0; h_idx < H'; h_idx += trunkQ)" traverses the row dimension of the QK tensor with a step size of trunkQ. The value of H' is equal to the total number of rows of the QK tensor divided by trunkQ. The innermost loop "for(int iter = 0; iter < W'; iter += trunkKV)" traverses the column dimension of the QK tensor with a step size of trunkKV. The value of W' is equal to the total number of columns of the QK tensor divided by trunkKV. Through such a three - layer structure, the Figure 1A Q, K, V matrix partitioning operation in the first step in

[0065] Figure 1B The second step (local attention calculation) in

[0066] Figures 1A to 1C is implemented by three functions, namely "trunk_mma0_process()", "trunk_flash_softmax_process()", and "trunk_mmal_process()". "trunk_mma0_process()" performs the matrix multiplication of trunk_Q and trunk_K to calculate trunk_QK. "trunk_flash_softmax_process()" performs the Flash Softmax operation on trunk_QK to calculate QK' = softmax(QK). "trunk_mmal_process()" then performs the matrix multiplication of QK' and trunk_V.

[0067] Therefore, it is necessary to optimize the implementation of the operator in the software stack of the AI chip, especially the operator for implementing the attention mechanism, to better adapt to the hardware characteristics and improve the overall computing efficiency.

[0068] Therefore, an embodiment of the present disclosure provides a method for processing tensor data, including: in response to the data length of the tensor data on a first axis being greater than a preset threshold, determining an adjustment multiple corresponding to the first axis; adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; arranging the tensor data after the shape adjustment in a memory; and based on the adjustment multiple, loading the tensor data arranged in the memory into a computing unit for calculation according to the granularity of data blocks.

[0069] This embodiment optimizes the implementation of operators in the software stack of the artificial intelligence chip so that when the length of the operator's input data on a certain axis exceeds the hardware limitation, the artificial intelligence chip can still efficiently process the operator's calculation, thereby improving the performance of the artificial intelligence chip in processing operators.

[0070] Optionally, the embodiments of the present disclosure also optimize the implementation of the operator for implementing the attention mechanism. The embodiments of the present disclosure have significant flexibility and adaptability when processing ultra-long sequence data. Even if the data length on a certain axis of the operator for implementing the attention mechanism exceeds the upper limit of the tensor expression specification of the AI ​​accelerator or GPU hardware design, the artificial intelligence chip can support its implementation. As a result, the embodiments of the present disclosure show excellent performance in processing super-resolution tasks in the fields of computer vision (CV) and natural language processing (NLP), as well as complex scenarios such as ultra-long speech texts.

[0071] In one aspect of the embodiments of the present disclosure, by splitting the loop layer to process the memory mapping logic, a substantial change to the algorithm implementation of the operator itself is avoided. This not only ensures the basic functions and performance of the operator, but also greatly improves its reusability, thereby improving the stability and maintainability of the artificial intelligence chip.

[0072] The embodiments of the present disclosure also correspondingly provide a device for processing tensor data, an electronic device and a storage medium, a computer program product, and a computer program.

[0073] The following combination Figures 2 to 6 All or part of the embodiments according to the present disclosure are described in more detail. It should be noted that the same reference numerals in different drawings will be used to refer to the same elements described.

[0074] Figure 2 is a flow chart illustrating a method 20 for processing tensor data according to an embodiment of the present disclosure. Figure 3 is a schematic diagram showing a method 20 for processing tensor data according to an embodiment of the present disclosure.

[0075] For example, Figure 2 As shown, at least one embodiment of the present disclosure provides a method 20 for processing tensor data, which includes the following operations S210 to S240.

[0076] In operation S210 , in response to a data length of the tensor data on a first axis being greater than a preset threshold, an adjustment multiple corresponding to the first axis is determined.

[0077] In operation S220, the shape of the tensor data is adjusted, wherein the data length of the tensor data on the first axis after the adjustment is shortened by the adjustment factor and the data length on the second axis is enlarged by the adjustment factor, wherein the data length of the tensor data on the first axis after the adjustment is less than the preset threshold.

[0078] In operation S230 , the reshaped tensor data is arranged in a memory.

[0079] In operation S240, based on the adjustment multiple, the tensor data arranged in the memory is loaded into the computing unit according to the granularity of the data block for calculation.

[0080] Optionally, the input data of operators used to implement various functions in artificial intelligence chips can be represented by tensor data. The data structure of tensor data can be regarded as a multidimensional array, which is convenient for large-scale matrix operations. By processing tensor data, artificial intelligence chips can realize complex tasks such as image recognition, natural language processing, and speech recognition. For example, in image recognition, tensor data can be used to represent the pixel values ​​of an image, and after layers of operations by convolutional neural networks (CNNs), the target objects in the image are finally identified. In natural language processing (NLP), tensor data can be used to represent text data, which is processed by recursive neural networks (RNNs) or transformer models to realize functions such as text classification and machine translation.

[0081] Optionally, the tensor data optionally includes three axes, wherein the first axis is a sequence axis, the second axis is a batch axis, and the third axis is a head size axis. The sequence axis represents the temporal or spatial sequence of the input data. In natural language processing tasks, the data on the sequence axis may correspond to the word order in a sentence; in computer vision tasks, it may represent a dimension of an image. The batch axis indicates the batch of processed data to allow the neural network model to process a batch of data at a time, which is used to improve training efficiency and achieve parallel computing. The head size axis represents the number of representation subspaces from which the neural network model can learn information.

[0082] Assuming factors such as hardware design, memory capacity or computing power, the artificial intelligence chip has a limit on the data length of the tensor data it processes on a specific axis, which is also referred to as a preset threshold in this disclosure. It is worth noting that the artificial intelligence chip 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).

[0083] Specifically, it is assumed that there is a hardware restriction: the size of H (height) or W (width) of the tensor cannot exceed 8192. In the embodiment of the present disclosure, the restriction on H (height) or W (width) is expressed as H_Limitation and W_Limitation, respectively. In particular, the embodiment of the present disclosure takes H_Limitation=8192 as an example.

[0084] In actual application scenarios, the length of data on the first axis often triggers this limit. For example, when processing long text, high-resolution images, or complex time series data, the length of data to be processed on the first axis is likely to exceed the limit of 8192 floating-point numbers. Although the following is explained as an example of the first axis as the sequence axis, those skilled in the art should understand that in fact the length of data on each axis may exceed the limitation caused by hardware, and the present disclosure is not limited to this.

[0085] In response to the situation where the data length of the tensor data on the first axis is greater than a preset threshold, the embodiment of the present disclosure reshapes the tensor data in the software stack so that the reshaped tensor data meets the hardware limitations, and designs a corresponding processing solution adjustment for the reshaped tensor data.

[0086] Assume that Figure 3In the corresponding example, the data length of the tensor data X on the first axis is 9216, the data length on the second axis is 2, and the data length on the third axis is 128. At this time, the shape of the tensor data X can also be expressed as [2,9216,128]. In response to determining that the data length 9216 on the first axis is greater than the preset threshold 8192, it can be determined that the adjustment multiple corresponding to the first axis is 9. That is, 9216 can be split into 9×1024, thereby shortening the data length of the tensor data on the first axis by 9 times and expanding the data on the second axis by 9 times. As a result, the data length of the tensor data after the shape adjustment on the first axis is 1024, which is less than 8192. This process is also called folding and rearranging the data on the first axis.

[0087] like Figure 3 As shown, optionally, the process of adjusting the shape of the tensor data X is: along the first axis, splitting the tensor data into multiple sub-tensors, the number of the multiple sub-tensors is equal to the adjustment multiple; and arranging the multiple sub-tensors along the second axis. It is worth noting that the way of adjusting the tensor data X does not affect the actual value of the tensor data on the batch axis, that is, the value of its batch axis remains 2. Therefore, the shape of the tensor data after the adjustment can be recorded as [2, 9, 1, 1024, 128].

[0088] Then, the tensor data after the shape adjustment is arranged in the memory. Then, based on the adjustment multiple, the tensor data arranged in the memory is loaded into the computing unit according to the granularity of the data block for calculation.

[0089] Specifically, assume that the size of a data block (truck) is 2×1024×128. Assume that the position of the first element in the upper left corner of the data block in the tensor data before reshaping is (2,1025,1). Then according to the reshaping multiple (hereinafter referred to as sub_H, Figure 3 sub_H=9) and the data length of the sub-tensor on the second axis (hereinafter referred to as bs_per_spc, in Figure 3 bs_per_spc=2) The location of the data block in the memory can be schematically represented as (3,1,1). It can be seen that in actual calculations, the actual storage location of a data block in the memory is associated with the adjustment multiple. This process is also called a memory mapping process with data blocks as the granularity. Later, reference will be made to pseudocodes I-III to further illustrate how to implement the memory mapping process in multiple loop layers, and this disclosure will not be repeated here.

[0090] Therefore, when the length of the input data of an operator on a certain axis exceeds the hardware limitation, the embodiments of the present disclosure fold and rearrange the tensor data, rearrange the data in memory, and subsequently perform calculations at the granularity of data blocks, thereby ensuring that the artificial intelligence chip can still efficiently perform operator calculations, thereby improving the performance of the artificial intelligence chip in processing operators.

[0091] Next, some optional details of the above examples are described with reference to pseudocodes I-III. The following pseudocodes are used to describe some implementation details of method 20 when tensor data is input data of an operator for implementing the attention mechanism. It should be understood by those skilled in the art that these pseudocodes I-III are only examples and the actual implementation is not limited thereto.

[0092] In the examples of pseudocode I-III, the input data of the operator for implementing the attention mechanism includes three tensor data, namely, the query tensor Q, the key tensor K, and the value tensor V. These three tensor data all have three axes: the batch axis (the second axis), the sequence axis (the first axis), and the head size axis (the third axis). In the example of pseudocode I, the case where the data length on the first axis of the query tensor Q is greater than the preset threshold is processed. In the example of pseudocode II, the case where the data length on the first axis of the key tensor K or the value tensor V is greater than the preset threshold is processed. In the example of pseudocode III, the case where the data length on the first axis of the query tensor Q, the key tensor K, or the value tensor V is greater than the preset threshold is processed.

[0093] Pseudo code 1 is a pseudo code illustrating a method 20 for processing tensor data according to an embodiment of the present disclosure, which illustrates optional details when the tensor data is a query tensor. Pseudo code 1 is illustrated as follows.

[0094] Pseudocode I:

[0095] for (int batch_idx=0;batch_idx <bs_per_spc;batch_idx++)

[0096] for (int sub_h_idx=0;sub_h_idx <sub_H;sub_h_idx ++)

[0097] for (int h_idx=0;h_idx <H';h_idx +=trunkQ)

[0098] for (int iter=0;iter <W';iter +=trunkKV)

[0099] trunk_mma0_process()

[0100] trunk_flash_softmax_process()

[0101] trunk_mmal_process()

[0102] Optionally, in the operator that implements the attention mechanism, the query tensor Q only participates in the operation of the first linear multiplication operation (mma0). Subsequent non-linear operations (e.g., softmax) and the second linear multiplication operation (mma1) will not directly use the query tensor. In the operator that implements the attention mechanism, the traversal of the query tensor along the first axis can be set in a relatively outer loop. At the same time, there will be no axis reduction on the first axis of the query tensor Q. Specifically, axis reduction refers to the process of reducing one or more axes by performing a certain aggregation operation (such as summation, mean, etc.) on the tensor data. For example, for a three-dimensional tensor, by summing all the elements on one of the axes, a new two-dimensional tensor can be obtained. This process is called axis reduction for that axis.

[0103] In the implementation of the attention mechanism, the axis reduction operation usually occurs when processing the attention weights. For example, after calculating the dot product of the query tensor Q and the key tensor K, it is converted into attention weights through the Softmax operation, and these weights are weighted and summed on the value tensor V to generate the final output. At this time, the elements on the corresponding sequence axis of the key tensor and the value tensor will be aggregated, resulting in the phenomenon of axis reduction. Since there will be no axis reduction, there is no need to implement block-level accumulation for the query vector Q.

[0104] Next, take the example where the data length of the query tensor Q on the first axis is 9216, the data length on the second axis is 2, and the data length on the third axis is 128, and the preset threshold of the hardware limit is 8192. That is, assume the shape of the query tensor Q is [2, 9216, 128], and the adjustment multiple corresponding to the first axis is 9. The shape of the query tensor Q after adjusting the shape can be denoted as [2, 9, 1, 1024, 128].

[0105] Since there is no axis reduction on the first axis of the query tensor Q, in operation S240, the following method can be used to implement the memory mapping process at the block level based on the adjustment multiple by nesting a new loop in the traditional loop structure (e.g., pseudo-code - 0). Specifically, as shown in pseudo-code I, the newly added loop is "for (int sub_h_idx = 0; sub_h_idx < sub_H; sub_h_idx++)", where sub_h_idx is the loop variable in this loop, and sub_H is the adjustment multiple.

[0106] Then, in combination with the use of the loop variable sub_h_idx in the newly added loop layer, in the process of performing the first linear multiplication operation (mma0), based on the adjustment multiple (sub_H) and the size of the data block to be calculated (truck_Q), a first offset value can be determined, and based on the first offset value, the position of the data block to be calculated in the memory is determined. Then, in the process of performing the first linear multiplication operation, first, based on the position of the data block to be calculated in the memory, the data block is loaded into the calculation unit to perform the first linear multiplication operation (mma0) and obtain the first intermediate result (truck_QK); then, based on the first intermediate result (trunk_QK), a nonlinear operation (softmax) and a second linear multiplication operation (mma1) are performed to obtain a calculation result; based on the adjustment multiple and the size of the data block to be calculated, a second offset value is determined; and based on the second offset value, the storage location of the calculation result is determined, and the calculation result is stored in the storage location corresponding to the memory.

[0107] Each operation is further described in detail below with reference to pseudocode I. First, the outermost loop traverses each data on the batch axis. The loop variable of this layer is batch_idx, ranging from 0 to bs_per_spc (for example, the value is 2). Each iteration processes a batch of data. The second layer of loop from the outside to the inside (that is, the newly added loop) traverses the index of each sub-tensor, and the loop variable is sub_h_idx, ranging from 0 to sub_H'. Through this layer of loop, it can be ensured that all sub-tensors of the query vector Q are processed in the order of the tensor data before reshaping.

[0108] Next, the third loop from the outside to the inside processes the tensor Q in blocks according to the size of the data block trunkQ. h_idx is the loop variable of this layer, ranging from 0 to H' (H' is the length of the sub-tensor on the first axis = 1024), and trunkQ is increased each time, thereby realizing traversal in the column direction. The innermost loop processes the tensor in blocks according to the size of the data block trunkKV, and the variable is iter, ranging from 0 to W', and trunkKV is increased each time. Through this data block-by-data block processing method, the memory access pattern is further optimized.

[0109] In each innermost loop, three functions for implementing the attention mechanism are called in sequence.

[0110] The first is the first linear multiplication operation (mma0), which is recorded as trunk_mma0_process() in pseudocode I, and is responsible for performing the dot product of the data block in the query tensor Q and the data block in the key tensor K. The calculation result is the first intermediate result (QK).

[0111] Next is a nonlinear operation (softmax), which is recorded as trunk_flash_softmax_process() in pseudocode I. The Softmax function is applied to the first intermediate result, and scaling and normalization are performed to generate the attention weight (QK').

[0112] Then comes the second linear multiplication operation (mma1), which is recorded as trunk_mmal_process() in pseudocode I, which is responsible for multiplying the attention weight (QK') with the value tensor V to obtain the final attention output.

[0113] Therefore, through this multi-layer nested loop and function call, even if the data length on the first axis of the query vector Q exceeds the hardware limit, the memory mapping logic can be processed by splitting the loop layer, and the data at the data block granularity is processed at each step, avoiding major changes to the algorithm implementation of the operator itself used to implement the attention mechanism. This not only ensures the basic functions and performance of the operator, but also greatly improves its reusability, thereby improving the stability and maintainability of the artificial intelligence chip.

[0114] Pseudo code II is a pseudo code illustrating a method 20 for processing tensor data according to an embodiment of the present disclosure, which illustrates optional details when the tensor data is a key tensor or a value tensor. Pseudo code II is illustrated as follows.

[0115] Pseudocode II:

[0116] for (int batch_idx=0;batch_idx <bs_per_spc;batch_idx++)

[0117] for (int h_idx=0;h_idx <H';h_idx +=trunkQ)

[0118] for (int sub_iter_idx=0;sub_iter_idx <sub_H;sub_iter_idx ++)

[0119] for (int iter=0;iter <W';iter +=trunkKV)

[0120] trunk_mma0_process()

[0121] trunk_flash_softmax_process()

[0122] trunk_mmal_process()

[0123] Compared with the query tensor Q, the key tensor K or the value tensor V will directly participate in the operations of the first linear multiplication operation (mma0), subsequent non-linear operations (e.g., softmax), and the second linear multiplication operation (mma1). And in the operator implementing the attention mechanism, the traversal of the key tensor K and the value tensor V along the first axis is often set in the relatively inner loop. Therefore, axis disappearance may occur on the first axis of the key tensor K and the value tensor V. Due to the possible axis disappearance on the first axis of the key tensor K and the value tensor V, it is necessary to implement accumulation at the data block granularity for the key tensor K and the value tensor V.

[0124] To implement this process, it is necessary to correspondingly adjust the memory mapping method by setting a new loop layer in the loop structure. Taking the example where the data length of the key tensor K on the first axis is 9216, the data length on the second axis is 2, the data length on the third axis is 128, and the preset threshold of the hardware limit is 8192. That is, assuming the shape of the key tensor K is [2, 9216, 128], the adjustment multiple corresponding to the first axis is 9. The shape of the key tensor K after adjusting the shape can be denoted as [2, 9, 1, 1024, 128]. The situation where the data length of the value tensor V on the first axis exceeds the preset threshold is similar to that of the key tensor K, and the present disclosure will not elaborate here.

[0125] Since axis disappearance will occur on the first axis of the key tensor K, in operation S240, the following method can be used to newly nest a layer of loop in the loop structure in pseudo-code -0 based on the adjustment multiple, so as to implement the memory mapping process at the data block granularity. Specifically, as shown in pseudo-code II, the newly added loop is "for (int sub_iter_idx = 0; sub_iter_idx < sub_H; sub_iter_idx++)", where sub_iter_idx is the loop variable in this loop and sub_H is the adjustment multiple.

[0126] Next, in combination with the use of the loop variable sub_iter_idx in the newly added loop, in the process of executing both the first linear multiplication operation (mma0) and the second linear multiplication operation (mma1), based on the adjustment multiple (sub_H) and the size (truck_Q) of the data block to be calculated, a third offset value can be determined, and based on the third offset value, the position of the data block to be calculated in the memory is determined. In the process of executing the first linear multiplication operation, based on the position of the data block to be calculated in the memory, the data block is loaded into the calculation unit to perform the first linear multiplication operation (mma0) and the nonlinear operation (softmax) to obtain the second intermediate result (QK'); in the process of executing the second linear multiplication operation, based on the position of the data block to be calculated in the memory, the data block is loaded into the calculation unit again to perform the second linear multiplication operation (mma1) to determine the calculation result based on the second intermediate result and the data in the data block to be calculated.

[0127] Each operation is further described in detail below in conjunction with pseudocode II. First, the outermost loop in pseudocode II iterates over each data on the batch axis. The loop variable of this layer is batch_idx, which ranges from 0 to bs_per_spc (for example, the value is 2). Each iteration processes a batch of data.

[0128] The second loop from outside to inside processes the tensor Q in blocks according to the size of the data block trunkQ. h_idx is the loop variable of this layer, ranging from 0 to H' (H' is the length of the query tensor on the first axis, assuming that the data length of the query vector Q on the first axis does not exceed the limit at this time), and each time trunkQ is increased, thereby realizing traversal in the column direction.

[0129] The third loop from outside to inside (that is, the newly added loop) traverses the index of each sub-tensor, and the loop variable is intsub_iter_idx, ranging from 0 to sub_H' (sub_H' is the data length of the sub-tensor of the key tensor K on the first axis). Through this layer of loop, it can be ensured that all sub-tensors of the key tensor K are processed in the order of tensor data before shape adjustment.

[0130] Next, the innermost loop processes the tensor in chunks according to the size of the chunk trunkKV, with the variable iter ranging from 0 to W', increasing trunkKV each time. This chunk-by-chunk processing further optimizes the memory access pattern.

[0131] In each innermost loop, three functions for implementing the attention mechanism are called in sequence.

[0132] The first is the first linear multiplication operation (mma0), which is denoted as trunk_mma0_process() in pseudocode II, responsible for performing the dot product of the data block in the query tensor Q and the data block in the key tensor K, and its calculation result is the first intermediate result (QK).

[0133] Next is the nonlinear operation (softmax), which is recorded as trunk_flash_softmax_process() in pseudocode II. The Softmax function is applied to the first intermediate result, and it is scaled and normalized to generate the attention weight (QK').

[0134] Then comes the second linear multiplication operation (mma1), which is denoted as trunk_mmal_process() in pseudocode II, which is responsible for multiplying the attention weight (QK') with the value tensor V to obtain the final attention output.

[0135] Therefore, through this multi-layer nested loop and function call, even if the data length on the first axis of the key tensor (or value tensor) exceeds the hardware limit, the memory mapping logic can be processed by splitting the loop layer, and the data at the data block granularity is processed at each step, avoiding major changes to the algorithm implementation of the operator itself used to implement the attention mechanism. This not only ensures the basic functions and performance of the operator, but also greatly improves its reusability, thereby improving the stability and maintainability of the artificial intelligence chip.

[0136] In addition, since the axis disappears on the first axis of the key tensor K, it is necessary to determine whether the last sub-tensor may have padding data after the key tensor K is divided into multiple sub-tensors. If the last sub-tensor has padding data, the padding data may affect the accumulation result.

[0137] For example, if the shape of the key tensor K is [2,9216,128], and the adjustment factor corresponding to the first axis is 9, then the shape of the key tensor K after the adjustment can be recorded as [2,9,1,1024,128]. At this time, the data of each sub-tensor of the key tensor K is valid data, and there is no padding data. That is, if the product of the data length of the sub-tensor on the first axis and the adjustment factor is equal to the data length of the tensor data on the first axis before the adjustment, it is determined that when loading data according to data blocks, the data in each data block is valid data.

[0138] If the shape of the key tensor K is [2, 14400, 128], and the adjustment factor corresponding to the first axis is 2, then the shape of the key tensor K after the adjustment can be recorded as [2, 2, 1, 8192, 128]. At this time, if the granularity of the data block is 2048, then when sub_iter = 1, the calculations of the first three data blocks are normal (that is, the data in the first three data blocks are all valid data), and the fourth data block has padded data. For the fourth data block, only valid data can be calculated (involving softmax and mma1), otherwise it will affect the final result.

[0139] For example, if the product of the data length of the sub-tensor on the first axis and the adjustment multiple is greater than the data length of the tensor data on the first axis before adjustment, then it can be determined that there is padding data in the last data block. When determining that the current data block is the last data block to be calculated, based on the difference (8192×2-144400) between the product of the data length (8192) of the sub-tensor on the first axis and the adjustment multiple (2) and the data length (14400) of the tensor data on the first axis before adjustment, determine the valid data in the data block; and perform calculations based on the valid data in the data block. Thus, calculation errors caused by the reordering of the key tensor K in the memory are avoided. Of course, the present disclosure is not limited to this.

[0140] Pseudocode III is a pseudocode illustrating a method 20 for processing tensor data according to an embodiment of the present disclosure, which illustrates a situation where the data lengths of the query vector, the key tensor, and the value tensor on the first axis all exceed a preset threshold. Pseudocode III is illustrated as follows.

[0141] Pseudocode III:

[0142] for (int batch_idx=0;batch_idx <bs_per_spc;batch_idx++)

[0143] for (int sub_h_idx=0;sub_h_idx <sub_H;sub_h_idx ++)

[0144] for (int h_idx=0;h_idx <H';h_idx +=trunkQ)

[0145] for (int sub_iter_idx=0;sub_iter_idx <sub_H;sub_iter_idx ++)

[0146] for (int iter=0;iter <W';iter +=trunkKV)

[0147] trunk_mma0_process()

[0148] trunk_flash_softmax_process()

[0149] trunk_mmal_process()

[0150] As shown in pseudocode III, if the data lengths of the query vector, key tensor, and value tensor on the first axis all exceed the preset threshold, two additional loop layers are required compared to the traditional loop structure in pseudocode-0. These two loop layers have been described with reference to pseudocode I and pseudocode II, respectively, and will not be repeated in this disclosure.

[0151] According to another aspect of the present disclosure, a first module is provided, which is configured to: in response to the data length of the tensor data on the first axis being greater than a preset threshold, determine an adjustment multiple corresponding to the first axis; a second module is configured to: adjust the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; a third module is configured to: arrange the tensor data after the shape adjustment in a memory; and a fourth module is configured to: based on the adjustment multiple, load the tensor data arranged in the memory into a computing unit according to the granularity of data blocks for calculation.

[0152] According to yet another aspect of the present disclosure, an electronic device is provided for implementing the method 20 according to the embodiment of the present disclosure. Figure 4 A schematic diagram of an electronic device 2000 according to an embodiment of the present disclosure is shown.

[0153] like Figure 4 As shown, the electronic device 2000 may include one or more processors 2010 and one or more memories 2020. The memory 2020 stores a computer-readable code, and when the computer-readable code is run by the one or more processors 2010, the method described above may be executed.

[0154] For example, the processor of the electronic device 2000 can execute: in response to the data length of the tensor data on the first axis being greater than a preset threshold, determining the adjustment multiple corresponding to the first axis; adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is expanded by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; arranging the tensor data after the shape adjustment in a memory; and based on the adjustment multiple, loading the tensor data arranged in the memory into the computing unit for calculation according to the granularity of the data block.

[0155] The processor in the embodiment of the present disclosure may be an integrated circuit chip with signal processing capability. The above processor may be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component. The disclosed methods, operations, and logic block diagrams in the embodiment of the present disclosure may be implemented or executed. The general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc., and may be an X86 architecture or an ARM architecture.

[0156] In general, various example embodiments of the present disclosure may be implemented in hardware or dedicated circuits, software, firmware, logic, or any combination thereof. Certain aspects may be implemented in hardware, while other aspects may be implemented in firmware or software that may be executed by a controller, microprocessor, or other computing device. When various aspects of the disclosed embodiments are illustrated or described as block diagrams, flow charts, or using some other graphical representation, it will be understood that the blocks, devices, systems, techniques, or methods described herein may be implemented in hardware, software, firmware, dedicated circuits or logic, general purpose hardware or controllers or other computing devices, or some combination thereof as non-limiting examples.

[0157] For example, the method or device according to the embodiment of the present disclosure can also be used by Figure 5 The architecture of the computing device 3000 shown in FIG. Figure 5 As shown, the computing device 3000 may include a bus 3010, one or more 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 component 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 used for processing and / or communication of the method provided in the present disclosure and program instructions executed by the CPU. The computing device 3000 may also include a user interface 3080. Of course, Figure 5The architecture shown is only exemplary and can be omitted according to actual needs when implementing different devices. Figure 5 One or more components of a computing device are shown.

[0158] According to yet another aspect of the present disclosure, a computer-readable storage medium is provided. Figure 6 A schematic diagram of a storage medium 4000 according to the present disclosure is shown.

[0159] like Figure 6 As shown, the computer storage medium 4020 stores computer readable instructions 4010. When the computer readable instructions 4010 are executed by the processor, the method according to the embodiment of the present disclosure described with reference to the above figures can be executed. The computer readable storage medium in the embodiment of the present disclosure can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. The non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM) or a flash memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous connection dynamic random access memory (SLDRAM) and direct memory bus random access memory (DR RAM). It should be noted that memory of the methods described herein is intended to include, but is not limited to, these and any other suitable types of memory. It should be noted that memory of the methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.

[0160] The present disclosure also provides a computer program product or a computer program, which includes a computer instruction stored in a computer-readable storage medium. The processor of the computer device reads the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the computer device executes the method according to the present disclosure.

[0161] It should be noted that the flowcharts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment, or a part of a code, and the module, program segment, or a part of a code contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0162] In general, various example embodiments of the present disclosure may be implemented in hardware or dedicated circuits, software, firmware, logic, or any combination thereof. Certain aspects may be implemented in hardware, while other aspects may be implemented in firmware or software that may be executed by a controller, microprocessor, or other computing device. When various aspects of the disclosed embodiments are illustrated or described as block diagrams, flow charts, or using some other graphical representation, it will be understood that the blocks, devices, systems, techniques, or methods described herein may be implemented in hardware, software, firmware, dedicated circuits or logic, general purpose hardware or controllers or other computing devices, or some combination thereof as non-limiting examples.

[0163] The exemplary embodiments of the present disclosure described in detail above are merely illustrative and not restrictive. It should be understood by those skilled in the art that various modifications and combinations may be made to these embodiments or their features without departing from the principles and spirit of the present disclosure, and such modifications should fall within the scope of the present disclosure.

Claims

1. A method for processing tensor data, comprising: In response to the data length of the tensor data on the first axis being greater than a preset threshold, determining an adjustment multiple corresponding to the first axis; Adjusting the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; Arranging the reshaped tensor data in memory; and Based on the adjustment multiple, the tensor data arranged in the memory is loaded into the computing unit according to the granularity of the data block for calculation.

2. The method of claim 1, wherein: The adjusting the shape of the tensor data comprises: Along the first axis, based on the adjustment factor, split the tensor data into a plurality of sub-tensors, the number of the plurality of sub-tensors being equal to the adjustment factor; and The plurality of sub-tensors are arranged along the second axis.

3. The method of claim 2, wherein: The step of loading the tensor data arranged in the memory into the computing unit for computing according to the granularity of the data block based on the adjustment multiple includes: Determine, based on the adjustment factor and the data length of the sub-tensor on the second axis, a position of the data block to be calculated in the memory; and Based on the location of the data block to be calculated in the memory, the data block is loaded into the calculation unit for calculation.

4. The method of claim 3, wherein the tensor data is a query tensor, and the determining the location of the data block to be calculated in the memory based on the adjustment multiple and the data length of the sub-tensor on the second axis further comprises: In the process of performing the first linear multiplication operation, determining a first offset value based on the adjustment multiple and the size of the data block to be calculated; as well as Based on the first offset value, a position of the data block to be calculated in the memory is determined.

5. The method of claim 4, wherein based on the location of the data block to be calculated in the memory, loading the data block into the calculation unit for calculation further comprises: In the process of performing the first linear multiplication operation, based on the position of the data block to be calculated in the memory, the data block is loaded into the computing unit to perform the first linear multiplication operation and obtain a first intermediate result; Based on the first intermediate result, performing a nonlinear operation and a second linear multiplication operation to obtain a calculation result; Determining a second offset value based on the adjustment multiple and the size of the data block to be calculated; as well as Based on the second offset value, a storage location of the calculation result is determined, and the calculation result is stored in a storage location corresponding to the memory.

6. The method of claim 3, wherein the tensor data is a key tensor or a value tensor, and the determining the location of the data block to be calculated in the memory based on the adjustment multiple and the data length of the sub-tensor on the second axis further comprises: In the process of performing both the first linear multiplication operation and the second linear multiplication operation, determining a third offset value based on the adjustment multiple and the size of the data block to be calculated; Based on the third offset value, a position of the data block to be calculated in the memory is determined.

7. The method of claim 6, wherein: The step of loading the data block to be calculated into the calculation unit for calculation based on the position of the data block to be calculated in the memory further includes: In the process of performing the first linear multiplication operation, based on the position of the data block to be calculated in the memory, the data block is loaded into the computing unit to perform the first linear multiplication operation and the nonlinear operation to obtain a second intermediate result; During the process of performing the second linear multiplication operation, based on the position of the data block to be calculated in the memory, the data block is loaded into the computing unit again to perform the second linear multiplication operation, so as to determine the calculation result based on the second intermediate result and the data in the data block to be calculated.

8. The method of claim 6, wherein: The step of loading the data block to be calculated into the calculation unit for calculation based on the position of the data block to be calculated in the memory further includes: In response to the product of the data length of the sub-tensor on the first axis and the adjustment multiple being greater than the data length of the tensor data on the first axis before adjustment, determining whether the data block is the last data block to be calculated; In response to the data block being the last data block to be calculated, determining valid data in the data block based on a difference between a product of a data length of the sub-tensor on the first axis and the adjustment multiple and a data length of the tensor data on the first axis before the adjustment; and Based on the valid data in the data block, calculations are performed.

9. The method of claim 1, wherein: The tensor data is one of a query vector, a key vector, and a value vector. The first axis is a sequence axis, and the second axis is a batch axis.

10. An apparatus for processing tensor data, comprising: A first module is configured to: in response to a data length of the tensor data on a first axis being greater than a preset threshold, determine an adjustment multiple corresponding to the first axis; A second module is configured to: adjust the shape of the tensor data, wherein the data length of the tensor data on the first axis after the shape adjustment is shortened by the adjustment multiple and the data length on the second axis is enlarged by the adjustment multiple, wherein the data length of the tensor data on the first axis after the shape adjustment is less than the preset threshold; A third module is configured to: arrange the reshaped tensor data in a memory; and The fourth module is configured to: based on the adjustment multiple, load the tensor data arranged in the memory into the computing unit according to the granularity of the data block for calculation.

11. An electronic device, comprising: processor; as well as A memory, wherein a computer executable program is stored in the memory, and when the computer executable program is executed by the processor, the method for processing tensor data as described in any one of claims 1 to 9 is executed.

12. A computer-readable storage medium having computer instructions stored thereon, wherein when the computer instructions are executed by a processor, the method for processing tensor data as described in any one of claims 1 to 9 is implemented.