An optimization method for batch matrix multiplication operators based on Ascend AI processors

By transporting and dividing data on the Ascend AI processor, and using the double buffer mechanism for parallel computing, the problem of failing to effectively utilize the double buffer mechanism in the existing technology is solved, and the computing efficiency is significantly improved.

CN114329325BActive Publication Date: 2024-09-24PEKING UNIV +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111374829.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-19
Publication Date
2024-09-24
Estimated Expiration
2041-11-19

AI Technical Summary

Technical Problem

The existing Ascend AI processor fails to effectively utilize the double buffering mechanism during the operation process, resulting in low operation efficiency.

Method used

By acquiring input data and transporting it to the AI ​​Core, the multi-core mechanism is used to allocate computing tasks, and the data is divided and buffered according to the number of loaded rows. The double buffer mechanism is used for parallel computing to avoid idle buffers.

Benefits of technology

The calculation time is shortened, the calculation efficiency is improved, and the efficiency of data processing is significantly improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114329325B
    Figure CN114329325B
Patent Text Reader

Abstract

The present invention discloses an optimization method for a batch matrix multiplication operator based on an Ascend AI processor, the method comprising: obtaining first input data and second input data, and transporting the first input data and the second input data to an AI Core; obtaining the number of loading rows of the second input data, and dividing the first input data and the second input data according to the number of loading rows and a double buffer mechanism of a preset buffer; loading the divided first input data and the second input data into the buffer for calculation to obtain output data; transporting the output data to an external storage for output. The present application can utilize a double buffer mechanism, can shorten the operation time of multiplying the first matrix and the second matrix, thereby improving the efficiency of data processing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data optimization, and in particular to an optimization method for a batch matrix multiplication operator based on an Ascend AI processor. Background Art

[0002] The Ascend processor is an AI processor based on the Da Vinci architecture. It is designed to provide chips with higher computing power and lower energy consumption for deep learning research, development, and deployment. It is currently the leading AI processor. The Da Vinci architecture is essentially a "Domain Specific Architecture" (DSA) chip that is designed to adapt to common applications and algorithms in a specific field. The existing Ascend processors use conventional batch matrix multiplication operators during calculations and do not utilize the double buffering mechanism of the Ascend processor, resulting in relatively low actual operating efficiency.

[0003] Therefore, the prior art still needs to be improved and developed. Summary of the invention

[0004] The technical problem to be solved by the present invention is that, in response to the above-mentioned defects of the prior art, an optimization method for a batch matrix multiplication operator based on the Ascend AI processor is provided, aiming to improve the efficiency of data processing by utilizing acceleration operation mechanisms such as double buffers by providing an optimization method for a batch matrix multiplication operator based on the Ascend AI processor.

[0005] The technical solution adopted by the present invention to solve the technical problem is as follows:

[0006] In a first aspect, the present invention provides a method for optimizing a batch matrix multiplication operator based on an Ascend AI processor, wherein the method comprises:

[0007] Obtaining first input data and second input data, and transferring the first input data and the second input data to the AI ​​Core;

[0008] Acquire the number of loaded lines of the second input data, and divide the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer;

[0009] Loading the divided first input data and second input data into the buffer for calculation to obtain output data;

[0010] The output data is transferred to external storage for output.

[0011] In one implementation, obtaining the first input data and the second input data, and transferring the first input data and the second input data to the AI ​​Core includes:

[0012] Acquire data types of the first input data and the second input data;

[0013] When the data type is float16, float32 or int32, transposing the first input data and the second input data;

[0014] Call the data movement instruction to move the transposed first input data and the second input data to the AICore.

[0015] In one implementation, when the data type is float16, float32, or int32, transposing the first input data and the second input data includes:

[0016] Acquire a first transposition flag corresponding to the first input data and a second transposition flag corresponding to the second input data;

[0017] Obtaining the value status of the first transposition flag and the second transposition flag;

[0018] The first input data and the second input data are transposed according to the value state.

[0019] In one implementation, obtaining the number of loaded lines of the second input data, and dividing the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer zone includes:

[0020] When the number of loaded rows is greater than 1;

[0021] The second input data is divided into two parts according to the double buffer mechanism.

[0022] In one implementation, loading the divided first input data and second input data into the buffer for calculation to obtain output data includes:

[0023] Load each portion of the second input data into a buffer;

[0024] Loading the first input data into a buffer, and multiplying the first input data with the portion of the second input data to obtain an intermediate result;

[0025] Add the intermediate results of the two buffers to get the output data.

[0026] In one implementation, loading the first input data into a buffer, and multiplying the first input data with the portion of second input data to obtain an intermediate result includes:

[0027] Loading a row of the first input data into one of the buffers;

[0028] Copying a row of the first input data according to the number of loaded rows to obtain copied data;

[0029] The copied data is multiplied with the one portion of second input data to obtain an intermediate result.

[0030] In one implementation, obtaining the first input data and the second input data, and before transferring the first input data and the second input data to the AI ​​Core includes:

[0031] Determine a multiple to be expanded according to the data types of the first input data and the second input data and a preset data movement instruction;

[0032] When the dimension of the last dimension of the first input data and the second input data is not equal to 1, expanding the last dimension of the first input data and the second input data according to the multiple;

[0033] When the dimension of the second dimension of the second input data is not equal to 1, the second dimension of the second input data is expanded upward according to the multiple.

[0034] In one implementation, the step of loading the divided first input data and the second input data into the buffer for calculation to obtain output data further comprises:

[0035] If the second dimension of the second input data is expanded;

[0036] Loading the divided first input data and second input data into the buffer for calculation to obtain intermediate data;

[0037] The intermediate data is sliced ​​and dimensionally reduced to obtain output data.

[0038] In one implementation, calling the data move instruction to move the transposed first input data and the second input data to the AI ​​Core includes:

[0039] Get the number of all AI Cores;

[0040] A batch of the first input data and the second input data is moved to an AI Core.

[0041] In one implementation, the transferring the output data to an external storage for output includes:

[0042] Get the output data from each AI Core;

[0043] The output data of each AI Core is transferred to external storage for output.

[0044] In a second aspect, an embodiment of the present invention further provides an optimization device for a batch matrix multiplication operator based on an Ascend AI processor, wherein the device comprises:

[0045] An acquisition module, used for acquiring first input data and second input data, and transferring the first input data and the second input data to the AI ​​Core;

[0046] a partitioning module, configured to obtain the number of loaded lines of the second input data, and partition the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer;

[0047] A calculation module, used for loading the divided first input data and second input data into the buffer for calculation to obtain output data;

[0048] The output module is used to transfer the output data to an external storage for output.

[0049] In a third aspect, an embodiment of the present invention further provides a terminal device, wherein the terminal device comprises: a processor, and a storage medium communicatively connected to the processor, the storage medium being suitable for storing multiple instructions; the processor being suitable for calling instructions in the storage medium to execute an optimization method for a batch matrix multiplication operator based on an Ascend AI processor according to any one of the above-mentioned schemes.

[0050] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement an optimization method for a batch matrix multiplication operator based on an Ascend AI processor as described in any one of the above solutions.

[0051] Beneficial effects of the present invention: Compared with the prior art, the present invention provides an optimization method for a batch matrix multiplication operator based on an Ascend AI processor, the method comprising obtaining first input data and second input data, and then moving the first input data and the second input data to an AI Core, so as to utilize the multi-core mechanism of the AI ​​processor to allocate the calculation process of each batch to an AI Core, thereby shortening the calculation time and improving the calculation efficiency, and then dividing the first input data and the second input data according to the number of loaded rows of the second input data, and utilizing the double buffering mechanism of the AI ​​processor to load the divided data into two buffers respectively, so that when one buffer is calculating data, the next copy of the data can be moved to another buffer for calculation, thereby avoiding the idleness of the buffer during the transfer process, and further improving the efficiency of data processing. BRIEF DESCRIPTION OF THE DRAWINGS

[0052] Figure 1 This is an architecture diagram of the Da Vinci AI Core in an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0053] Figure 2 It is a schematic diagram of batch matrix multiplication in an optimization method of a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0054] Figure 3 It is a flowchart of a specific implementation method of an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0055] Figure 4 This is a flowchart of transferring first input data and second input data to an AI Core in an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0056] Figure 5 It is a data storage method of input_y in batch matrix multiplication in an optimization method of a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0057] Figure 6 This is a schematic diagram of batch matrix multiplication after the second input data is transposed in an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0058] Figure 7 This is a flowchart of obtaining output data in an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0059] Figure 8It is a schematic diagram of batch matrix multiplication optimization in an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0060] Fig. 9 This is the test data result of an optimization method for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0061] Fig.10 It is a principle block diagram of an optimization device for a batch matrix multiplication operator based on an Ascend AI processor provided in an embodiment of the present invention.

[0062] Fig.11 It is a block diagram of the internal structure principle of the terminal device provided by an embodiment of the present invention. DETAILED DESCRIPTION

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

[0064] Ascend processor is an AI processor based on Da Vinci architecture. It is designed to provide chips with higher computing power and lower energy consumption for deep learning research, development and deployment. It is currently the leading AI processor. Ascend AI processor is mainly composed of AICore, AI CPU (ARM), L2Buffer / Cache (system cache / buffer), DDR / HBM (DDR controller / HBM storage particles), I / O controller, etc. Among them, AI Core is the computing core of Ascend AI processor, responsible for executing vector and tensor related computing-intensive operators. L2 / HBM / DDR These external storages relative to AI Core are collectively referred to as Global Memory, or GM for short.

[0065] AI Core uses the Da Vinci architecture, and its basic structure is as follows: Figure 1 As shown in the figure, from the control point of view, it can be seen as a relatively simplified basic architecture of a modern microprocessor, which includes three basic computing resources: matrix computing unit (CubeUnit), vector computing unit (Vector Unit) and scalar computing unit (Scalar Unit). These three computing units correspond to the three common computing modes of tensor, vector and scalar respectively. In actual computing, they perform their respective duties and form three independent execution pipelines. Under the unified scheduling of system software, they cooperate with each other to achieve optimized computing efficiency.

[0066] CANN is a heterogeneous computing architecture launched for existing AI scenarios. By providing multi-level programming interfaces, it supports users to quickly build AI applications and businesses based on the Ascend platform. Deep learning algorithms are composed of computing units, which are called operators. For example, matrix multiplication, convolution operation and pooling operation are all separate operators. CANN provides three operator development methods to achieve the best balance between efficiency and performance. The first is TBE-DSL (Tensor Boost Engine-Domain Specific Language), which is defined based on the grammatical rules of DSL to achieve data segmentation and scheduling. For developers, they only need to focus on the expression of calculations to improve development efficiency. The second is the TVM primitive development method. Developers need to understand the knowledge of neural networks, TVM primitives and Da Vinci architecture hardware. In this development method, developers can control the scheduling process of operators by themselves, but do not need to master the use of Da Vinci instructions. The third is TBE-TIK (Tensor Iterator Kernel), which requires developers to have instruction-level programming and tuning capabilities, including data arrangement and calculation expression. Compared with DSL and TVM development methods, TIK development methods can better exert the ultimate performance of the chip. CANN opens the encoding of the preset operator library and supports custom modification of operators. Operators designed by users can be quickly applied to neural network models.

[0067] Batch matrix multiplication (BatchMatMul) is an operator that implements batch processing of matrix multiplication. It is an important operator in various deep learning models. The calculation speed of the batch matrix multiplication operator will greatly affect the training and reasoning speed of various deep learning models. Therefore, the optimization of the batch matrix multiplication operator is of great significance. Figure 2 As shown in the figure, batch matrix multiplication is to multiply each row of the input input_x of each batch corresponding matrix with the corresponding element of each column of the input input_y, and sum the results as an element of the corresponding position of the output ouput. If there are two third-order tensors with dimensions [b,m,n] and [b,n,k], and the first dimension is the batch size, then the multiplication of these two tensors will result in a dimension [b,m,k].

[0068] According to research, there are usually multiple AI Cores in an Ascend AI processor. Therefore, performance optimization can be achieved by using a core-by-core parallel approach. When using core-by-core parallelism, the upper limit is 65535. Each AI Core performs data operations in parallel. At this time, Global Memory is visible to each AI Core. Another common performance optimization method is to use a double buffer mechanism. First, the output buffer (Unified Buffer, referred to as UB) is used to store the input and output of vector and scalar computing units. The instruction queues executed on the AI ​​Core mainly include the following categories, namely Vector instruction queue, Matrix instruction queue and storage move instruction queue (MTE2, MTE3). Different instruction queues are independent of each other and can be executed in parallel. The double buffer mechanism divides the output buffer into two. When Vector reads and calculates the data in one buffer, the storage move instruction can move the next copy of data into the other buffer. In this way, the data in and out transfer and Vector calculation are executed in parallel, avoiding the Vector being idle during data transfer and improving the utilization efficiency of the Vector unit. The existing batch matrix multiplication operator provided by CANN uses the TVM primitive development method. This development method does not schedule and optimize the Da Vinci instructions and architecture of the Ascend AI processor, and does not use the multi-core and double buffer mechanism and vector computing unit to improve data processing efficiency, resulting in low utilization efficiency of the Ascend AI processor.

[0069] In order to solve the problems of the prior art, this embodiment provides an optimization method for a batch matrix multiplication operator based on an Ascend AI processor, the method comprising obtaining first input data and second input data, and then moving the first input data and the second input data to an AI Core, so as to utilize the multi-core mechanism of the Ascend AI processor to allocate the calculation process of each batch to an AI Core, thereby shortening the calculation time and improving the calculation efficiency, and then dividing the first input data and the second input data according to the number of loaded rows of the second input data, and utilizing the double buffering mechanism of the Ascend AI processor to load the divided data into two buffers respectively, so that when one buffer is calculating data, the next copy of the data can be moved to another buffer for calculation, thereby avoiding the idleness of the buffer during the transfer process and further improving the efficiency of data processing.

[0070] Exemplary Methods

[0071] In this embodiment, an optimization method for batch matrix multiplication operators based on the Ascend AI processor can be applied to a terminal device, such as a computer, in which the Ascend AI processor is provided and the TIK development method is adopted. Figure 3 As shown in , an optimization method for batch matrix multiplication operators based on Ascend AI processors in this embodiment includes the following steps:

[0072] Step S100: Obtain first input data and second input data, and transfer the first input data and the second input data to the AI ​​Core.

[0073] Since this embodiment processes the first input data and the second input data, it is convenient to improve the efficiency of obtaining the output data, and since the storage inside the AI ​​Core is separated from the storage GM outside the AI ​​Core, the data needs to be moved to the AI ​​Core first, then calculated in the AI ​​Core, and finally the calculation result is moved back to the GM.

[0074] In one implementation, Figure 4 As shown, step S100 includes the following steps:

[0075] S101, obtaining data types of the first input data and the second input data;

[0076] S102: When the data type is float16, float32 or int32, transpose the first input data and the second input data;

[0077] S103, calling a data move instruction to move the transposed first input data and the second input data to the AI ​​Core.

[0078] In specific implementation, since the input data supported by the BatchMatMul operator in Mindspore 1.1 version when registering are three types of float16, float32, and int32, the data of other data types need to first call the data type conversion operator (Cast) when calling the BatchMatMul operator, so the first input data and the second input data are float16, float32, and int32 as an example for description in the embodiment of the present application. Since the first input data and the second input data need to be transferred to the AI ​​Core for calculation, and the data movement instruction is required in the data transfer process, the data_move instruction is used in the embodiment of the present application, and the offset address of the source operand and the destination operand of the instruction must be 32Byte aligned, and the space occupied by a float16, float32, and int32 in the memory is 2Byte, 4Byte, and 4Byte respectively, so the data_move instruction operation must transfer at least 16 float16, 8 float32, or 8 int32 at a time. The BatchMatMul operator multiplies a row of input_x with a column of input_y. When the number of columns in input_y is greater than 1, the data in a column of input_y is not connected in memory, such as Figure 5 As shown, if input_y is a three-dimensional matrix of type float32 [32,8,8], the input_y matrix is ​​stored in memory by row, as shown in the direction of the arrow in the figure, the end of each row is immediately followed by the beginning of the next row, and a column for matrix operation is not continuous in the memory, and the data_move instruction carries at least 32Byte of data at a time. Calling the data_move instruction once can carry 8 data, that is, a row of input_y, but only one data at the beginning of the row is actually used for matrix multiplication calculation. In this case, data transportation is very wasteful of limited input buffer space, which greatly affects the calculation efficiency of batch matrix multiplication operators. Therefore, the embodiment of the present application obtains a first transposition flag corresponding to the first input data and a second transposition flag corresponding to the second input data, and then transposes the first input data and the second input data according to the value states of the first transposition flag and the second transposition flag. Specifically, when the first input data is input_x, the second input data is input_y, the first transposition flag is transpose_a, and the second transposition flag is transpose_b, the embodiment of the present application further optimizes the following formula: First, transpose input_y. Then the batch matrix multiplication between input_x and input_y is equivalent to multiplying each row of input_x by the corresponding elements of each row of the transposed input_y. The results are summed and used as an element at the corresponding position of output, such as Figure 6 As shown. At this time, each row of data of each calculation of input_x and transposed input_y is continuous, and can be moved to the output buffer UB at one time using the data_move instruction, and the vector multiplication vmul is used to complete the multiplication at one time and then sum to get the final result. According to the value status of the first transposition flag and the second transposition flag, batch matrix multiplication can be divided into the following four categories:

[0079] 1. If the input input_x is not transposed, and the input_y is transposed, that is, input_x, input_y, transpose_a = False, transpose_b = True, then the corresponding elements of each row of input_x and input_y can be directly multiplied to get the final result. This case is recorded as CusBatchMatMul(input_x, input_y, False, True);

[0080] 2. If neither input_x nor input_y is transposed, that is, input_x, input_y, transpose_a = False, transpose_b = False, then you can first transpose input_y to get Transpose(input_y), and call CusBatchMatMul(input_x, Transpose(input_y), False, True) to get the final result. The calculation process is as follows:

[0081] input_y=mindspore.ops.Transpose()(input_y,(0,2,1))

[0082] CusBatchMatMul(input_x,input_y,False,True);

[0083] 3. If the input input_x is transposed, but input_y is not transposed, that is, input_x, input_y, transpose_a = True, transpose_b = False, then you can first transpose both input_x and input_y to get Transpose(input_x) and Transpose(input_y), and then call CusBatchMatMul(Transpose(input_x), Transpose(input_y), False, True) to get the final result. The calculation process is as follows:

[0084] input_x=mindspore.ops.Transpose()(input_x,(0,2,1))

[0085] input_y=mindspore.ops.Transpose()(input_y,(0,2,1))

[0086] CusBatchMatMul(input_x,input_y,False,True);

[0087] 4. If both input_x and input_y are transposed, that is, input_x, input_y, transpose_a = True, transpose_b = True, then transpose input_x to get Transpose(input_x), and then call CusBatchMatMul (Transpose(input_x), input_y, False, True) to get the final result. The calculation process is as follows:

[0088] input_x=mindspore.ops.Transpose()(input_x,(0,2,1))

[0089] CusBatchMatMul(input_x,input_y,False,True).

[0090] In summary, any situation can be converted into CusBatchMatMul (input_x, input_y, False, True) for calculation. Next, we only need to optimize the situation of CusBatchMatMul (input_x, input_y, False, True).

[0091] Preferably, since the data_move instruction is used in the process of moving the first input data and the second input data to the AI ​​Core, and the data_move instruction requires that the offset addresses of the source operand and the destination operand must be aligned to 32Byte, 32Byte can store 8 float32 and int32 or 16 float16. Therefore, the data_move instruction moves at least 8 float32 and int32 or 16 float16 at a time. If the source operand to be moved is less than 32Byte, it must be padded to 32Byte to ensure the correctness of the result of the data_move instruction. Therefore, in order to enable each row of the first input data, the second input data and the output data to be moved with one or several data_move instructions, the last dimension of the first input data and the second input data must be expanded first, and then moved to UB with the data_move instruction. The second dimension of the second input data also needs to be expanded so that the matrix calculation result, a one-dimensional array of the length of the second dimension number can also be directly moved back to GM through the data_move instruction. For example, the first input data input_x is a matrix of dimension [b,m,n], the second input data input_y is a matrix of dimension [b,k,n], and the data types of the first and second input data are float32 or int32. When n!=1, first expand the last dimension n of input_x and input_y upward to n / 8*8, which is a multiple of 8. Similarly, when k!=1, expand the second dimension k of input_y upward to k / 8*8, which is a multiple of 8. For float16 data type, expand n and k to multiples of 16. If the expansion of the last dimension of input_x and input_y does not affect the dimension of the final result, and the second dimension of input_y is expanded, then after CusBatchMatMul is calculated, the result slice needs to be reduced to the original dimension [b,m,k].

[0092] S200 , obtaining the number of loading lines of the second input data, and dividing the first input data and the second input data according to the number of loading lines and a double cache mechanism of a preset buffer.

[0093] Since the amount of data calculated by the Vector calculation unit in the embodiment of the present application at one time is limited, when performing matrix multiplication calculations, all the parts of input_y and input_x that need to be calculated need to be loaded into the output buffer UB. At this time, the first input data and the second input data can be divided to load a batch of input_y data into UB for calculation.

[0094] In a specific implementation, when the number of loaded rows of the second input data is greater than 1, the second input data is divided into two parts, so as to facilitate dividing the second input data into two parts according to the double buffer mechanism.

[0095] S300: Load the divided first input data and second input data into the buffer for calculation to obtain output data.

[0096] In the embodiment of the present application, after dividing the first input data and the second input data, the divided first input data and the second input data are loaded into a buffer for calculation, thereby obtaining output data.

[0097] In one implementation, Figure 7 As shown, step S300 includes the following steps:

[0098] S301, loading each portion of the second input data into a buffer respectively;

[0099] S302, loading the first input data into a buffer, and multiplying the first input data with the part of the second input data to obtain an intermediate result;

[0100] S303, adding the intermediate results of the two buffers to obtain output data.

[0101] In specific implementation, since a double buffer mechanism is used in UB, after the second input data is divided into two, a row of the first input data is loaded into a buffer, and then a row of the first input data is copied according to the number of loaded rows to obtain the copied data, and the copied data is multiplied with a part of the second input data, and then the vcadd instruction is called to accumulate the data of each row in the result to obtain the intermediate result, and finally the intermediate results of the two buffers are added to obtain the output data. The above steps are a calculation process in an AI Core, and a multi-core mechanism can be used to assign the calculation process of each batch to an AI Core. Specifically, when moving the first input data and the second input data, first obtain the number of all AI Cores, and then move a batch of the first input data and the second input data to an AI Core. Therefore, when outputting the data, it is also necessary to obtain the output data in each AI Core, and move the output data in each AICore to the GM for output. For example, Figure 8As shown in the figure, the size of input_x is [32,4,8], the size of input_y is [32,8,8], and the input data type is float32. Assuming that the input buffer size can only store 140 float32 data, the Vector vector calculation unit can only calculate 4*8=32 float32 data at a time. When performing matrix multiplication calculation, the double buffer mechanism is used to divide input_y into two, each buffer processes 4 rows of input_y data, and then load one row of input_x into UB, and copy one row into four rows to form a vector. In this way, one vmul vector multiplication instruction can complete the multiplication of all four rows of input_x and input_y in each buffer, without multiplying each row of input_x with each row of input_y. In this way, there is no need for a for loop, and three fewer vmul instructions are called. Then, the vcadd instruction is used to accumulate the 8 data in each row of the result to obtain a final result, and then data_move is used to move the final calculation result back to GM, thereby greatly improving the calculation efficiency.

[0102] S400: moving the output data to an external storage for output.

[0103] In specific implementation, since the present embodiment optimizes the data processing, after the output data is obtained, the output data needs to be moved to the external storage for output. Specifically, the present embodiment calls the data movement instruction to move the output data back to the GM. Preferably, the data movement instruction in the present embodiment is the data_move instruction.

[0104] In summary, since the present invention is based on the existing Mindspore framework for operator development, the Mindspore deep learning framework provides a Python API to conveniently and quickly expand the CusBatchMatMul operator of the AI ​​processor. The specific calculation process of the CusBatchMatMul operator is:

[0105] 1. Get the type of the first input data and the second input data. The number of bytes occupied by one data is bits. The minimum number of data moved by the data_move instruction at one time is move_mini_size. The number of data that the vector calculation unit can calculate at one time is vector_cal_size. When the input data is float32 and int32, bits = 4, move_mini_size = 8, vector_cal_size = 64. When the input data is float16, bits = 2, move_mini_size = 16, vector_cal_size = 128. The two variables move_mini_size and vector_cal_size require these two parameters in the subsequent data_move instruction, vector multiplication vmul instruction and vector addition vcadd instruction.

[0106] 2. Get the number of AI Cores of the current AI processor through the get_soc_spec("CORE_NUM") function, recorded as core_num, and get the output buffer size through the get_unified_buffer_size function. The unit of the obtained buffer size is Byte, recorded as ub_size. The total amount of data that can be loaded into the buffer is ub_load_data_size = ub_size / bits;

[0107] 3. According to the dimensions of the first input data input_x and the second input data input_y, multi-core parallel division is performed, which is mainly divided into the following situations:

[0108] (a) Multi-core partitioning is performed based on batches, and each batch is assigned to an AI Core for calculation;

[0109] (b) When n<=ub_load_data_size / 2.2, at least one row of n numbers of input_y can be put into UB. At this time, input_y_lines>=1. ​​In theory, the calculation data of input_x and input_y each occupy half in UB, and then multiplication is performed. However, in the actual calculation process, some variables are needed to assist the calculation, etc., which brings some additional storage overhead. Dividing by 2.2 is to take these overheads into account, leaving a certain amount of overhead space, and loading less calculation data in each calculation. The specific number of rows loaded for input_y is at most input_y_lines=ub_load_data_size / 2.2 / / n / / 2*2, / / 2*2 is to make the number of rows of input_y moved into UB an even number of rows, which can be evenly distributed to the double buffer for calculation. When input_y_lines>k is large, the first batch of input_y can be loaded into UB for matrix calculation. When input_y_lines<k, input_y can be processed in a loop k / input_y_lines times, calculating input_y_lines lines each time.

[0110] (c) When n <= ub_load_data_size / 2.2, input_x is divided into rows. If input_y_lines>2*k, two rows of input_x can be moved into UB for calculation at a time. In the double buffer, each buffer processes one row of input_x data. Otherwise, one row of input_x is moved into UB. The following operations are performed in parallel in the double buffer: first, one row of input_x is copied into input_y_lines / 2 rows, and input_y is moved into input_y_lines / 2 rows. Vector multiplication vmul is performed with the copied input_x to obtain the vector multiplication result res_vmul. Then, vcadd is called once or multiple times for each row of res_vmul to accumulate. The final accumulated value of each row is stored in the one-dimensional vector res. Then, res is directly moved back to the corresponding position of output in Global Memory using the data_move instruction.

[0111] (d) When n>ub_load_data_size / 2.2, input_y_lines <1, then each row of input_y can be divided into p segments, p = 1 / input_y_lines, each time 1 / p rows of input_y are processed, a row of input_x is also divided into p segments, each time a segment of the same length as input_y is sent into UB and multiplied by the vmul instruction, and then the vcadd instruction is used to sum the segment to obtain an intermediate calculation result of a value, which needs to be expanded to the size move_mini_size that can be carried by the data_move instruction. For example, when move_mini_size = 8, it is necessary to put a calculation result value of this segment at the first position of an 8-bit group and carry it out of UB. At this time, an intermediate matrix inner_output of size [b, k, 8*p] is needed to store the intermediate results calculated by the above segmentation method for the entire input_x and the entire input_y. The xp of each row in the matrix (x=0,1,2,3…) positions are all the results of multiplying and summing the 1 / p segments of each row of input_x and input_y. Finally, the inner_output matrix is ​​processed, each row of the inner_output matrix is ​​sent to UB, all matrix multiplication intermediate result values ​​are taken out, and finally transmitted to the output data. Preferably, the embodiment of the present application is tested on the Ascend 910 machine and the Mindspore 1.1.1 environment, and the results of multiple sets of data are as follows Fig. 9 As shown, the CusBatchMatMul operator time includes the time to transpose and slice the input data and results. According to the test results, it is obvious that CusBatchMatMul has a significant improvement over the BatchMatMul operator in the existing CANN library. For input_x is [32, 128, 128], input_y is [32, 128, 128], tranpose_a = True, transpose_b = True, CusBatchMatMul is about 3861 times better than the BatchMatMul operator. The BatchMatMul operator is a commonly used operator in deep learning neural networks. The CusBatchMatMul operator in the present invention has significantly improved performance relative to the BatchMatMul operator, which can increase the speed of network training and reasoning, thereby improving computing efficiency.

[0112] In summary, this embodiment first obtains the first input data and the second input data, and then moves the first input data and the second input data to the AI ​​Core, so as to utilize the multi-core mechanism of the AI ​​processor and allocate each batch of calculation process to an AI Core, thereby shortening the calculation time and improving the calculation efficiency. Then, the first input data and the second input data are divided according to the number of loaded rows of the second input data, and the double buffering mechanism of the AI ​​processor is used to load the divided data into two buffers respectively, so that when one buffer is calculating data, the next copy of the data can be moved to another buffer for calculation, thereby avoiding the idleness of the buffer during the transfer process and further improving the calculation efficiency.

[0113] Exemplary Devices

[0114] like Fig.10 As shown in , this embodiment also provides an optimization device for batch matrix multiplication operators based on the Ascend AI processor, and the device includes: an acquisition module 10, a division module 20, a calculation module 30, and an output module 40. Specifically, the acquisition module 10 is used to acquire the first input data and the second input data, and move the first input data and the second input data to the AI ​​Core. The division module 20 is used to obtain the number of loading rows of the second input data, and divide the first input data and the second input data according to the number of loading rows and the double cache mechanism of the preset buffer. The calculation module 30 is used to load the divided first input data and the second input data into the buffer for calculation to obtain output data. The output module 40 is used to move the output data to an external storage for output.

[0115] In one implementation, the acquisition module 10 includes:

[0116] An acquiring unit, configured to acquire data types of the first input data and the second input data;

[0117] a transposition unit, configured to transpose the first input data and the second input data when the data type is float16, float32 or int32;

[0118] The transport unit is used to call the data movement instruction to transport the transposed first input data and the second input data to the AI ​​Core.

[0119] In one implementation, the calculation module 30 includes:

[0120] A loading unit, used for loading each portion of the second input data into a buffer respectively;

[0121] an intermediate result obtaining unit, configured to load the first input data into a buffer, and multiply the first input data by the part of the second input data to obtain an intermediate result;

[0122] The output result acquisition unit is used to add the intermediate results of the two buffers to obtain output data.

[0123] In one implementation, the intermediate result obtaining unit includes:

[0124] A loading subunit, used for loading one line of the first input data into one of the buffers;

[0125] a copy subunit, configured to copy a row of the first input data according to the number of loaded rows to obtain copy data;

[0126] The intermediate result acquisition subunit is used to multiply the copied data with the one portion of the second input data to obtain an intermediate result.

[0127] Based on the above embodiments, the present invention further provides a terminal device, whose principle block diagram can be shown as follows: Fig.11 As shown. The terminal device includes a processor, a memory, a network interface, a display screen, and a temperature sensor connected through a system bus. Among them, the processor of the terminal device is used to provide computing and control capabilities. The memory of the terminal device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the terminal device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for optimizing a batch matrix multiplication operator based on the Ascend AI processor is implemented. The display screen of the terminal device can be a liquid crystal display screen or an electronic ink display screen, and the temperature sensor of the terminal device is pre-set inside the terminal device to detect the operating temperature of the internal device.

[0128] Those skilled in the art will understand that Fig.11 The principle block diagram shown in the figure is only a block diagram of a partial structure related to the scheme of the present invention, and does not constitute a limitation on the terminal device to which the scheme of the present invention is applied. The specific terminal device may include more or less components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0129] In one embodiment, a terminal device is provided, the terminal device including a memory, a processor, and an optimization program for a batch matrix multiplication operator based on an Ascend AI processor stored in the memory and executable on the processor. When the processor executes the optimization program for the batch matrix multiplication operator based on the Ascend AI processor, the following operation instructions are implemented:

[0130] Obtaining first input data and second input data, and transferring the first input data and the second input data to the AI ​​Core;

[0131] Acquire the number of loaded lines of the second input data, and divide the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer;

[0132] Loading the divided first input data and second input data into the buffer for calculation to obtain output data;

[0133] The output data is transferred to external storage for output.

[0134] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0135] In summary, the present invention provides an optimization method for a batch matrix multiplication operator based on an Ascend AI processor, the method comprising obtaining first input data and second input data, and then moving the first input data and the second input data to an AI Core, so as to utilize the multi-core mechanism of the Ascend AI processor to allocate the calculation process of each batch to an AI Core, thereby shortening the calculation time and improving the calculation efficiency, and then dividing the first input data and the second input data according to the number of loaded rows of the second input data, and utilizing the double buffering mechanism of the Ascend AI processor to load the divided data into two buffers respectively, so that when one buffer is calculating data, the next copy of the data can be moved to another buffer for calculation, thereby avoiding the idleness of the buffer during the transfer process and further improving the calculation efficiency.

[0136] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for optimizing batch matrix multiplication operators based on Ascend AI processors, characterized in that: The method comprises: Obtaining first input data and second input data, and transferring the first input data and the second input data to the AI ​​Core; Acquire the number of loaded lines of the second input data, and divide the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer; Loading the divided first input data and second input data into the buffer for calculation to obtain output data; Transferring the output data to external storage for output; The obtaining the number of loaded lines of the second input data, and dividing the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer zone, includes: When the number of loaded rows is greater than 1; dividing the second input data into two parts according to the double buffer mechanism; The step of loading the divided first input data and second input data into the buffer for calculation to obtain output data includes: Load each portion of the second input data into a buffer; Loading the first input data into a buffer, and multiplying the first input data with the portion of the second input data to obtain an intermediate result; Add the intermediate results of the two buffers to get the output data.

2. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 1, characterized in that: The obtaining of the first input data and the second input data, and transferring the first input data and the second input data to the AI ​​Core includes: Acquire data types of the first input data and the second input data; When the data type is float16, float32 or int32, transposing the first input data and the second input data; Call the data movement instruction to move the transposed first input data and the second input data to the AICore.

3. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 2, characterized in that: When the data type is float16, float32 or int32, transposing the first input data and the second input data comprises: Acquire a first transposition flag corresponding to the first input data and a second transposition flag corresponding to the second input data; Obtaining the value status of the first transposition flag and the second transposition flag; The first input data and the second input data are transposed according to the value state.

4. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 1, characterized in that: The step of loading the first input data into a buffer and multiplying the first input data with the portion of second input data to obtain an intermediate result comprises: Loading a row of the first input data into one of the buffers; Copying a row of the first input data according to the number of loaded rows to obtain copied data; The copied data is multiplied with the one portion of second input data to obtain an intermediate result.

5. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 1, characterized in that: The obtaining of the first input data and the second input data, and before transferring the first input data and the second input data to the AI ​​Core, includes: Determine a multiple to be expanded according to the data types of the first input data and the second input data and a preset data movement instruction; When the dimension of the last dimension of the first input data and the second input data is not equal to 1, expanding the last dimension of the first input data and the second input data according to the multiple; When the dimension of the second dimension of the second input data is not equal to 1, the second dimension of the second input data is expanded upward according to the multiple.

6. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 5, characterized in that: The step of loading the divided first input data and second input data into the buffer for calculation to obtain output data further includes: If the second dimension of the second input data is expanded; Loading the divided first input data and second input data into the buffer for calculation to obtain intermediate data; The intermediate data is sliced ​​and dimensionally reduced to obtain output data.

7. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 2, characterized in that: The calling data move instruction moves the transposed first input data and the second input data to the AI ​​Core, including: Get the number of all AI Cores; A batch of the first input data and the second input data is moved to an AI Core.

8. The optimization method for batch matrix multiplication operators based on Ascend AI processor according to claim 7, characterized in that: The step of transferring the output data to an external storage for output includes: Get the output data from each AI Core; The output data of each AI Core is transferred to external storage for output.

9. An optimization device for batch matrix multiplication operators based on Ascend AI processor, characterized in that: The device comprises: An acquisition module, used for acquiring first input data and second input data, and transferring the first input data and the second input data to the AI ​​Core; a partitioning module, configured to obtain the number of loaded lines of the second input data, and partition the first input data and the second input data according to the number of loaded lines and a double buffer mechanism of a preset buffer; A calculation module, used for loading the divided first input data and second input data into the buffer for calculation to obtain output data; An output module, used for transferring the output data to an external storage for output; The partitioning module is specifically used for: When the number of loaded rows is greater than 1; dividing the second input data into two parts according to the double buffer mechanism; The computing module comprises: A loading unit, used for loading each portion of the second input data into a buffer respectively; an intermediate result obtaining unit, configured to load the first input data into a buffer, and multiply the first input data by the part of the second input data to obtain an intermediate result; The output result acquisition unit is used to add the intermediate results of the two buffers to obtain output data.

10. A terminal device, characterized in that: The terminal device includes: a processor and a storage medium communicatively connected to the processor, wherein the storage medium is suitable for storing multiple instructions; the processor is suitable for calling the instructions in the storage medium to execute an optimization method for a batch matrix multiplication operator based on an Ascend AI processor as described in any one of claims 1 to 8.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement an optimization method for batch matrix multiplication operators based on the Ascend AI processor as described in any one of claims 1 to 8 above.