Matrix operation method, processor and computing device
By using matrix registers for matrix transposition, the problem of low efficiency of matrix transposition operation in high-performance computing is solved, and the efficiency of matrix transposition and hardware cost reduction are achieved.
Patent Information
- Application Number
- CN202411002112.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-05-31
- Filing Date
- 2023-08-16
- Publication Date
- 2024-12-03
- Estimated Expiration
- 2043-08-16
AI Technical Summary
In the field of high-performance computing, matrix transposition operation efficiency is low, and the prior art mainly relies on scalar registers or vector registers, resulting in a large number of instructions and low efficiency.
Matrix registers are used to perform matrix transposition operations. By storing matrix data to matrix registers and changing the reading method, the matrix transposition is realized, the number of instructions is reduced, and efficiency is improved.
Matrix transposition is implemented through matrix registers, which reduces the number of instructions, improves the efficiency of matrix transposition, and reduces the hardware cost of the processor.
Smart Images

Figure CN119066319A_ABST
Abstract
Description
[0001] This application is a divisional application. The application number of the original application is 202311036871.4, and the original application date is August 16, 2023. The entire contents of the original application are incorporated into this application by reference. Technical Field
[0002] The present application relates to the field of computing technology, and in particular to a matrix operation method, a processor and a computing device. Background Art
[0003] Matrix operations are frequently used in high performance computing (HPC) applications, including matrix transposition, addition, subtraction, and multiplication.
[0004] Currently, when matrices in HPC applications need to be transposed, the processor can use scalar registers or vector registers to complete the matrix transposition, which is inefficient. Summary of the invention
[0005] The embodiment of the present application provides a matrix operation method, a processor and a computing device, which can complete matrix transposition with the help of a matrix register, and can improve the efficiency of matrix transposition. The corresponding technical solution is as follows:
[0006] In a first aspect, a matrix operation method is provided, which is executed by a processor including a matrix register, and the matrix operation method includes:
[0007] The processor stores the first matrix data in a matrix register. The first matrix includes N rows and M columns, and the matrix register stores the first matrix data in a two-dimensional form of N rows and M columns. Then the processor reads the first matrix data from the matrix register row by row or column by column to obtain the second matrix data including M rows and N columns after transposing the first matrix data. M and N are positive integers, and the values of M and N may be equal or different.
[0008] A matrix register is a register that can store matrix data in a two-dimensional form. The processor may provide a read instruction for reading the matrix data stored in the matrix register by column or row. The processor may also provide a store instruction for storing vector data (such as a column in the matrix data) in the matrix register in the form of rows or columns. When the processor reads and stores data in the matrix register in the form of rows or columns, the corresponding differences in read performance and storage performance can be ignored.
[0009] In the scheme shown in the present application, for the first matrix data corresponding to the first matrix to be transposed, the processor can store the first matrix data in a matrix register. When each row of data stored in the matrix register is each row of data of the first matrix, the processor can read out the first matrix data stored in the matrix register in a column-by-column manner, and then the second matrix data of the second matrix after the first matrix is transposed can be obtained. When each row of data stored in the matrix register is each column of data of the first matrix, the processor can read out the first matrix data stored in the matrix register in a row-by-row manner, and then the second matrix data of the second matrix after the first matrix is transposed can be obtained.
[0010] It can be seen that the scheme shown in the present application only needs to store the matrix data to be transposed into the matrix register, and then change the reading mode and read out the matrix data stored in the matrix register, so as to realize the transposition of the matrix. In this way, for a matrix with N rows and M columns, only N storage instructions plus M read instructions (or M storage instructions plus N read instructions) are needed to realize the transposition operation of the matrix, which can reduce the number of instructions required for transposing the matrix and improve the efficiency of matrix transposition.
[0011] In one achievable manner, storing the first matrix data into a matrix register includes: storing the first matrix data stored in a memory into the matrix register row by row or column by column.
[0012] In the scheme shown in the present application, the first matrix data can be stored in the memory in the form of an array. Depending on the storage method, the processor can read a row of data or a column of data of the first matrix in the memory each time. For a row of data that can be read from the memory each time, the processor can store it in a matrix register in a row or column manner. That is, a row of data of the first matrix read out by the processor each time can be stored in the matrix register by row, or in the matrix register by column. For a column of data that can be read from the memory each time, the processor can store it in the matrix register in a row or column manner. That is, a column of data of the first matrix read out each time can be stored in the matrix register by row, or in the matrix register by column.
[0013] Wherein, after storing a row of data or a column of data of the first matrix read out each time into the matrix register in a row-by-row manner, the processor can read out the first matrix data stored in the matrix register in a column-by-column manner to obtain the second matrix data after transposition. After storing a row of data or a column of data of the first matrix read out each time into the matrix register in a column-by-column manner, the processor can read out the first matrix data stored in the matrix register in a row-by-row manner to obtain the second matrix data after transposition. It can be seen that in the scheme shown in the present application, the processor only needs to store the matrix data to be transposed into the matrix register, and then change the reading method and read out the matrix data stored in the matrix register, so as to realize the transposition of the matrix, thereby reducing the number of instructions required for transposing the matrix and improving the efficiency of transposing the matrix.
[0014] In one achievable manner, the processor further includes a vector register. Accordingly, storing the first matrix data in the matrix register includes: storing the first matrix data in the vector register in the form of vector data. Storing the vector data corresponding to the first matrix data stored in the vector register in the matrix register in a row or column manner.
[0015] In the scheme shown in the present application, the processor also includes a vector register, and the processor can store matrix data in the matrix register with the help of the vector register. Among them, for each row of data or column of data of the first matrix read from the memory, the processor can first store it in the vector register in the form of a vector, and then read the row of data or column of data of the first matrix stored in the vector register to the matrix register, thereby realizing the storage of the first matrix data in the matrix register. In this way, the memory stores the matrix data in the matrix register through the vector register, which can reduce the number of data channels established between the memory and the matrix register, and can reduce the hardware cost of the processor.
[0016] In one achievable manner, the first matrix data is read from the matrix register row by row or column by column to obtain the second matrix data after the first matrix data is transposed, including: reading the first matrix data from the matrix register row by row or column by column, and storing the vector data read each time into the vector register. The vector data stored in the vector register is read into a memory to obtain the second matrix data after the first matrix data is transposed.
[0017] In the scheme shown in the present application, the processor can store matrix data to the matrix register with the help of the vector register, and can also read the matrix data from the matrix register with the help of the vector register. Among them, for each row of data or column of data read from the matrix register, the processor can first store it in the form of a vector to the vector register, and then read the row of data or column of data stored in the vector register to the memory, thereby realizing the reading of the first matrix data from the matrix register in a row-by-row or column-by-column manner. In this way, the memory stores the matrix data to the matrix register through the vector register, and reads the matrix data from the matrix register through the vector register, which can avoid the establishment of a data channel between the memory and the matrix register, and can further reduce the hardware cost of the processor.
[0018] In one achievable manner, the processor further includes a vector register. Accordingly, reading the first matrix data from the matrix register row by row or column by column to obtain the second matrix data after transposing the first matrix data includes: reading the first matrix data from the matrix register row by row or column by column, and storing the vector data read each time into the vector register. Reading the vector data stored in the vector register into a memory to obtain the second matrix data after transposing the first matrix data.
[0019] In the scheme shown in the present application, the processor also includes a vector register, and the processor can read the matrix data stored in the matrix register into the memory with the help of the vector register. Among them, for each row of data or column of data read from the matrix register, it can be first stored in the vector register in the form of a vector, and then the row of data or column of data stored in the vector register is read into the memory, thereby realizing the first matrix data being read from the matrix register in a row-by-row or column-by-column manner. In this way, the memory reads the matrix data from the matrix register through the vector register, which can reduce the number of data channels established between the memory and the matrix register, thereby reducing the hardware cost of the processor.
[0020] In an achievable manner, the processor further comprises a calculation unit. Accordingly, after obtaining the second matrix data after transposing the first matrix data, the method further comprises: inputting the second matrix data into the calculation unit, and the calculation unit performs a specified calculation on the second data matrix.
[0021] In the solution shown in the present application, the processor also includes a computing unit, such as a matrix computing unit, a vector computing unit, etc. After the matrix data stored in the matrix register is read to obtain the transposed second matrix data, the read second matrix data can be directly input into the computing unit for subsequent calculations, without the need to store the read second matrix data into the memory, thereby improving the efficiency of matrix operations.
[0022] In a second aspect, a processor is provided, the processor comprising a matrix register, the processor being used to: store first matrix data in the matrix register, the first matrix comprising N rows and M columns, the matrix register storing the first matrix data in a two-dimensional form of N rows and M columns. Read the first matrix data from the matrix register row by row or column by column to obtain second matrix data after transposing the first matrix data, the second matrix comprising M rows and N columns.
[0023] In one achievable manner, the processor is configured to store the first matrix data stored in the memory into the matrix register in a row-by-row or column-by-column manner.
[0024] In one achievable manner, the processor further includes a vector register, and the processor is configured to: store the first matrix data in the vector register in the form of vector data, and store the vector data corresponding to the first matrix data stored in the vector register in the matrix register in the form of rows or columns.
[0025] In one achievable manner, the processor is used to: read the first matrix data from the matrix register row by row or column by column, and store the vector data read each time into the vector register. The vector data stored in the vector register is read into the memory to obtain the second matrix data after transposing the first matrix data.
[0026] In one achievable manner, the processor further includes a vector register, and the processor is configured to: read the first matrix data from the matrix register in a row-by-row or column-by-column manner, and store the vector data read each time in the vector register, and read the vector data stored in the vector register into a memory to obtain the second matrix data after transposing the first matrix data.
[0027] In one achievable manner, the processor further includes a calculation unit, and the processor is further configured to: input the second matrix data into the calculation unit, and the calculation unit performs a specified calculation on the second data matrix.
[0028] In a third aspect, a computing device is provided, which includes a memory and a processor, wherein the processor is used to execute instructions stored in the memory so that the processor performs the matrix operation method described in the first aspect above.
[0029] In a fourth aspect, a computer program product comprising instructions is provided. When the instructions are executed by the computing device described in the third aspect, the computing device executes the matrix operation method described in the first aspect.
[0030] In a fifth aspect, a computer-readable storage medium is provided, comprising computer program instructions. When the computer program instructions are executed by the computing device described in the third aspect, the computing device can perform the matrix operation method described in the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 It is a schematic diagram of a method for realizing matrix transposition by means of a scalar register in the related art;
[0032] Figure 2 It is a schematic diagram of a method for realizing matrix transposition by means of a vector register in the related art;
[0033] Figure 3 is a schematic diagram of a computing device structure provided in an embodiment of the present application;
[0034] Figure 4 is a flow chart of a matrix operation method provided by an embodiment of the present application;
[0035] Figure 5 is a schematic diagram of a matrix transposition provided in an embodiment of the present application;
[0036] Figure 6 is a schematic diagram of a computing device structure provided in an embodiment of the present application;
[0037] Figure 7 is a schematic diagram of a matrix transposition provided in an embodiment of the present application;
[0038] Figure 8 is a schematic diagram of a computing device structure provided in an embodiment of the present application;
[0039] Fig. 9 is a schematic diagram of a matrix transposition provided in an embodiment of the present application;
[0040] Fig.10 It is a schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0041] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0042] Matrix operations are currently frequently used in computers, especially in the fields of high performance computing (HPC) and artificial intelligence (AI). Matrix operations include transposition, addition, subtraction, and multiplication. In related technologies, matrix operations are generally implemented with the help of scalar processors or vector memories.
[0043] Figure 1The figure is a schematic diagram of a method for realizing matrix transposition by means of a scalar register in the related art. In internal storage such as memory and cache, matrices can be stored in the form of arrays. For example, the matrix If the matrix A is stored in memory by rows, the matrix actually stored in the memory is [1,2,3,4,5,6,7,8,9]. If the matrix A is stored in memory by columns, the matrix actually stored in the memory is [1,4,7,2,5,8,3,6,9]. Figure 1 As shown, in one example, if the matrix A is transposed with the help of a scalar register, and the matrix is stored in memory by rows, that is, the matrix A stored in the memory is [1,2,3,4,5,6,7,8,9], then each element "1", "4", "7", "2", "5", "8", "3", "6", "9" of the matrix A stored in the memory can be read in sequence. After each element is read, the read element can be stored in the scalar register, and then the element stored in the scalar register can be stored back to the memory, so that the elements "1", "4", "7", "2", "5", "8", "3", "6", "9" can be stored in the memory in sequence to obtain the matrix B after the matrix A is transposed. The matrix B is stored in memory by rows, that is, the matrix B[1,4,7,2,5,8,3,6,9], that is To transpose matrix A in this way, nine scalar Load instructions are needed to store the elements of matrix A in memory into scalar registers one by one, and nine scalar Store instructions are needed to store the elements in the scalar registers back to memory one by one. Similarly, if an n×n matrix is transposed with the help of scalar registers, n 2 times scalar Load instruction and n 2 Subscalar Store instruction.
[0044] Figure 2 FIG. 1 is a schematic diagram of a method for realizing matrix transposition by means of a vector register in the related art. Figure 2 As shown, in one example, if the matrix A is transposed with the help of a vector register, and the matrix is stored in memory by rows, that is, the matrix A stored in the memory is [1,2,3,4,5,6,7,8,9], then each column element "1,4,7", "2,5,8", "3,6,9" of the matrix A stored in the memory can be read in sequence. Each time a column of elements is read, it can be stored in the vector register, and then the elements stored in the vector register are stored back to the memory, so that the elements "1,4,7", "2,5,8", "3,6,9" can be stored in the memory in sequence to obtain the matrix B after the matrix A is transposed. The matrix B is stored in memory by rows, that is, the matrix B[1,4,7,2,5,8,3,6,9], that is To transpose matrix A in this way, three GatherLoad instructions are required to store each column of matrix A in memory into the vector register in turn, and three vector Store instructions are required to store the elements in the vector register back to the memory in turn. However, since matrix A is stored in memory by rows, that is, each column of matrix A is not stored continuously in memory, Gather Load is actually split into three scalar Load instructions to obtain a column of elements in matrix A. Therefore, with the help of vector registers, nine scalar Load instructions and three vector Store instructions are required. Similarly, if an n×n matrix is transposed with the help of vector registers, n scalar Load instructions and three vector Store instructions are required. 2 times scalar Load instructions and n times vector Store instructions.
[0045] The embodiment of the present application provides a method for matrix operation, which can complete the matrix transposition with the help of a matrix register in a processor, can reduce the number of instructions required for the matrix transposition, and improve the efficiency of the matrix transposition. Wherein, the matrix register is a register that can store matrix data in a two-dimensional form that distinguishes rows and columns. The processor can provide a corresponding read instruction, which can read the matrix data stored in the matrix register by column, and the processor also provides a corresponding storage instruction, which can store each column of matrix data in the form of rows or columns to the matrix register, or store each row of matrix data in the form of rows or columns to the matrix register. When the processor reads or stores the matrix data in the matrix register in the form of rows or columns, the difference in corresponding reading performance and storage performance can be ignored. In an example, the matrix register may be a matrix register corresponding to a scalable matrix unit (Scalable MatrixUnit, SME).
[0046] Figure 3 1 is a schematic diagram of a computing device structure provided by an embodiment of the present application, wherein the computing device includes at least a memory and a processor. The memory may be an external memory (such as a hard disk) or a memory memory (such as a memory, a cache). The processor includes at least a matrix register, and may also include a vector register and a scalar register, etc. ( Figure 3(not shown), if the processor only includes matrix registers, the matrix registers and the memory can access each other, such as data stored in the memory or cache can be directly written into the matrix registers, and data stored in the matrix registers can be directly written into the memory or cache. The processor can be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP) or a digital signal processor (DSP).
[0047] Figure 4 is a flowchart of a matrix operation method provided by an embodiment of the present application. The method can be performed by Figure 3 The processor of the computing device executes the matrix transposition with the help of the matrix register, which reduces the number of instructions required for matrix transposition and improves the efficiency of matrix transposition. Figure 4 , the method comprising:
[0048] Step 401: The processor stores first matrix data into the matrix register, wherein the first matrix includes N rows and M columns, and the matrix register stores the first matrix data in a two-dimensional form of N rows and M columns.
[0049] In implementation, applications involving matrix operations, such as HPC applications, applications involving artificial intelligence, etc., may be run in the computing device. When a matrix needs to be transposed during operation of the computer device, a matrix transposition request may be sent to the processor so that the processor transposes the corresponding matrix. The matrix operation method provided in the embodiment of the present application may form a function for implementing matrix transposition through assembly / intrinsic / high-level language, as a dynamic / static / high-level language acceleration library for matrix operations. When the processor needs to perform matrix transposition, the function for implementing matrix transposition may be called and executed in the function library to implement the matrix operation method provided in the embodiment of the present application, such as implementing the processing of steps 401-402.
[0050] In an embodiment of the present application, the matrix to be transposed may be referred to as a first matrix, and the matrix data corresponding to the first matrix may be referred to as first matrix data. The first matrix data may be stored in an external memory or an internal memory before being transposed. The first matrix may include N rows and M columns, where M and N are both positive integers, and the values of M and N may be equal or unequal. In the present application, the method of matrix operation is described in detail by taking the first matrix data stored in the memory and the first matrix data being a two-dimensional matrix as an example, and other situations are similar and will not be repeated.
[0051] The first matrix data can be stored in the memory by row or by column. When the first matrix data is stored in the memory by row, the processor can read the target matrix from the memory by row, and store the elements of a row read each time into the matrix register by row or by column. When the target matrix is stored in the memory by column, the processor can read the target matrix from the memory by column, and store the elements of a column read each time into the matrix register by row or by column.
[0052] like Figure 5 As shown, the first matrix is a matrix including 8 rows and 8 columns. Accordingly, the first matrix data can be stored in the memory as [00, 01, 02, 03, 04, 05, 06, 07, 10, 11, 12, ..., 75, 76, 77] by row. The processor can read a row of data of the first matrix data from the memory each time, such as [00, 01, 02, 03, 04, 05, 06, 07], [10, 11, 12, 13, 14, 15, 16, 17], etc., and can store a row of data of the first matrix data read each time in the form of rows into the matrix register.
[0053] In one example, the first matrix may also be matrices obtained by dividing a large-size matrix to be transposed. When reading any first matrix data, it may be read from the matrix data corresponding to the stored large-size matrix. The specific reading process belongs to the prior art and will not be described in detail in this application. Accordingly, after transposing each first matrix, a matrix after transposing the large-size matrix may be formed according to each transposed second matrix.
[0054] Step 402: read the first matrix data from the matrix register row by row or column by column to obtain the second matrix data after transposing the first matrix data, wherein the second matrix includes M rows and N columns.
[0055] After the first matrix data is stored in the matrix register, the first matrix data can be read from the matrix register in a row-by-row or column-by-column manner. If a row of data or a column of data of the first matrix data read each time is stored in the matrix register in a row manner, the first matrix data can be read out from the matrix register in sequence in a column-by-column manner, thereby completing the row-column conversion of the first matrix data. If a row of data or a column of data of the first matrix data read each time is stored in the matrix register in a column manner, the first matrix data can be read out from the matrix register in sequence in a row-by-row manner, thereby completing the row-column conversion of the first matrix data, and obtaining the second matrix data after the first matrix data is transposed.
[0056] like Figure 5As shown, for the first matrix data stored in the matrix register, the first matrix data can be read column by column, that is, [00, 10, 20, 30, 40, 50, 60, 70], [01, 11, 21, 31, 41, 51, 61, 71], ..., [07, 17, 27, 37, 47, 57, 67, 77] are read in sequence to obtain the second matrix data after the first matrix data is transposed.
[0057] In this way, for the first matrix of N rows and M columns, if the corresponding first matrix data is stored in memory by row, N storage instructions are required to store the first matrix data in the matrix register, and then M read instructions are required to read the first matrix data from the matrix register to obtain the second matrix data after transposition. If the corresponding first matrix data is stored in memory by column, M storage instructions are required to store the first matrix data in the matrix register, and then N read instructions are required to read the first matrix data from the matrix register to obtain the second matrix data after transposition. It can be seen that by adopting the matrix operation method provided by the present application, only M+N instructions are needed to complete the transposition of the matrix, which can improve the efficiency of matrix transposition.
[0058] Figure 5 Schematic diagram of a matrix transposition provided by an embodiment of the present application. Figure 5 As shown, the first matrix is an 8×8 matrix, such as The first matrix data can be stored in memory by row, that is, the first matrix data is stored in memory as [00,01,02,03,04,05,06,07,10,11,12,…,75,76,77]. When the first matrix data is transposed, each row element of the first matrix data in the memory can be stored in the matrix register through 8 storage instructions. Then, each column element of the first matrix data stored in the matrix register is read out through 8 read instructions. For example, each column element read can be stored in the memory in the form of a matrix. In this way, the second matrix data after the first matrix data is transposed can be obtained in the memory, and the second matrix data is stored in the memory as [00,10,20,30,40,50,60,70,01,11,21,…,57,67,77]. That is, the
[0059] In one example, for reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, the data read each time can be stored in the memory, and the second matrix data obtained in the memory is obtained by transposing the first matrix data. Then, the transposed second matrix data in the memory is input to the calculation unit in the processor for further calculation. In another example, for reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, the data read each time can be directly input to the corresponding calculation unit in the processor for further calculation to improve the calculation efficiency.
[0060] Figure 6 : is a schematic diagram of a computing device structure provided by an embodiment of the present application, and a vector register may also be included in the processor of the computing device. Among them, there is a bidirectional data path between the vector register and the memory, which can realize mutual access of data, and there is a bidirectional data path between the vector register and the matrix register, which can also realize mutual access of data. There is a unidirectional data path between the matrix register and the memory, that is, the data in the matrix register can be stored in the memory, and the data in the memory needs to be stored in the matrix register with the help of the vector register.
[0061] for Figure 6 For the computing device shown, the processing of the above step 401 can be replaced by: the processor stores the first matrix data in the form of vector data to the vector register, and then stores the vector data corresponding to the first matrix data stored in the vector register to the matrix register in the form of rows or columns.
[0062] When the first matrix data is stored in the memory by row, the processor can read the target matrix from the memory by row, and store each row of data read as the row vector corresponding to the first matrix into the vector register, so that the row vectors corresponding to the first matrix can be stored in multiple vector memories respectively. Then, the row vectors stored in each vector register can be stored in the matrix register by row or by column in turn.
[0063] Figure 7 Schematic diagram of a matrix transposition provided by an embodiment of the present application. Figure 7 As shown, the first matrix is an 8×8 matrix, such as The first matrix data can be stored in memory by row, that is, the first matrix data is stored in memory as [00,01,02,03,04,05,06,07,10,11,12,…,75,76,77]. When transposing the first matrix data, 8 vector Load instructions can be used to store each row element of the target matrix in the memory into the vector register. Then, 8 storage instructions are used to store the vectors stored in the vector register into the matrix register by row. Finally, 8 read instructions can be used to read out each column of the matrix stored in the matrix register. For example, each column of the read data can be stored in the memory in the form of a matrix. In this way, the second matrix data after the first matrix data is transposed can be obtained in the memory, and the second matrix data is stored in the memory as [00,10,20,30,40,50,60,70,01,11,21,…,57,67,77]. That is, the
[0064] In the embodiment of the present application, the memory can use the vector memory when storing data in the matrix register. This can reduce the number of data channels between the matrix register and the memory, can reduce the hardware cost of the processor, and will not increase the number of instructions required for matrix transposition too much, which can improve the efficiency of matrix transposition.
[0065] Figure 8 is a schematic diagram of a computing device structure provided in an embodiment of the present application, and Figure 6 The same as the computing device shown is that the processor of the computing device also includes a vector register, and a bidirectional data path between the vector register and the memory can realize mutual access to data. The vector register and the matrix register have a bidirectional data path, which can also realize mutual access to data. Figure 6 The difference between the computing devices shown is that there is a unidirectional data path between the matrix register and the memory, that is, the data in the matrix register can be stored in the memory, and the data in the memory needs to be stored in the matrix register with the help of the vector register.
[0066] for Figure 8 For the computing device shown, the processing of step 402 can be replaced by: reading the first matrix data from the matrix register row by row or column by column, and storing the vector data read each time into the vector register. Then, the vector data stored in the vector register is read into the memory to obtain the second matrix data after transposing the first matrix data.
[0067] If each row of data or each column of data in the first matrix data read is stored in the matrix register unit by row, the first matrix data stored in the matrix register can be read column by column, and each column of data (column vector) read each time can be stored in the vector register. Then the column vectors stored in each vector register can be stored in the memory in turn. If each row of data or each column of data in the first matrix data read is stored in the matrix register unit by column, the first matrix data stored in the matrix register can be read row by row, and each row of data (row vector) read each time can be stored in the vector register. Then the column vectors stored in each vector register can be stored in the memory in turn.
[0068] Fig. 9 Schematic diagram of a matrix transposition provided by an embodiment of the present application. Fig. 9 As shown, the first matrix is an 8×8 matrix, such as The first matrix data can be stored in memory by row, that is, the target matrix is stored in memory as [00,01,02,03,04,05,06,07,10,11,12,…,75,76,77]. When transposing the target matrix, each row of the first matrix data in the memory can be stored in the matrix register through 8 storage instructions. Then, the first matrix data stored in the matrix register is read column by column into the vector register through 8 read instructions. Finally, the vector stored in the vector register is read into the memory through 8 vector Load instructions. In this way, the second matrix data after the first matrix data is transposed can be obtained in the memory. The second matrix data is stored in the memory as [00,10,20,30,40,50,60,70,01,11,21,…,57,67,77]. That is,
[0069] In the embodiment of the present application, when the matrix register stores data in the memory, the vector memory can be used. This can reduce the number of data channels between the matrix register and the memory, can reduce the hardware cost of the processor, and will not increase the number of instructions required for matrix transposition too much, which can improve the efficiency of matrix transposition.
[0070] Fig.10 is a schematic diagram of a computing device structure provided in an embodiment of the present application, and Figure 6 and Figure 8 The computing device shown is similar in that the processor of the computing device also includes a vector register, the vector register and the memory can access each other, and the vector register and the matrix register can access each other. Figure 6The difference between the computing devices shown is that the matrix register and the memory cannot access each other, that is, if the data stored in the memory needs to be stored in the matrix register, the data in the memory can be first stored in the vector register, and then the data stored in the vector register can be stored in the matrix register. Similarly, if the data stored in the memory needs to be stored in the matrix register, the data in the memory can be first stored in the vector register, and then the data stored in the vector register can be stored in the matrix register.
[0071] for Fig.10 In the computing device shown in the figure, the processing of the above step 401 can be replaced by: the processor stores the first matrix data in the form of vector data to the vector register, and then stores the vector data corresponding to the first matrix data stored in the vector register to the matrix register in the form of rows or columns. Figure 6 The contents of the corresponding embodiments are the same and will not be described again here.
[0072] The processing of step 402 can be replaced by: reading the first matrix data from the matrix register row by row or column by column, and storing the vector data read each time into the vector register. Then read the vector data stored in the vector register into the memory to obtain the second matrix data after the first matrix data is transposed. The further processing can be the same as the above Figure 6 The contents of the corresponding embodiments are the same and will not be described again here.
[0073] In the embodiment of the present application, when storing data between the matrix register and the memory, the vector memory can be used. This can avoid establishing a data channel between the matrix register and the memory, can reduce the hardware cost of the processor, and will not increase the number of instructions required for transposing the matrix too much, which can improve the efficiency of matrix transposition.
[0074] The present application embodiment also provides a processor, in which a matrix register and a vector register are included. For example, the processor may be as described above. Figure 3 , 6 , 8, and 10. The processor may be used for:
[0075] The first matrix data is stored in a matrix register, the first matrix includes N rows and M columns, and the matrix register stores the first matrix data in a two-dimensional form of N rows and M columns. The first matrix data is read from the matrix register row by row or column by column to obtain second matrix data after transposing the first matrix data, the second matrix includes M rows and N columns.
[0076] In one achievable manner, the processor is configured to store the first matrix data stored in the memory into the matrix register in a row-by-row or column-by-column manner.
[0077] In one achievable manner, the processor further includes a vector register, and the processor is configured to: store the first matrix data in the vector register in the form of vector data, and store the vector data corresponding to the first matrix data stored in the vector register in the matrix register in the form of rows or columns.
[0078] In one achievable manner, the processor is used to: read the first matrix data from the matrix register row by row or column by column, and store the vector data read each time into the vector register. The vector data stored in the vector register is read into the memory to obtain the second matrix data after transposing the first matrix data.
[0079] In one achievable manner, the processor further includes a vector register, and the processor is configured to: read the first matrix data from the matrix register in a row-by-row or column-by-column manner, and store the vector data read each time in the vector register, and read the vector data stored in the vector register into a memory to obtain the second matrix data after transposing the first matrix data.
[0080] In one achievable manner, the processor further includes a calculation unit, and the processor is further configured to: input the second matrix data into the calculation unit, and the calculation unit performs a specified calculation on the second data matrix.
[0081] The processor provided in the embodiment of the present application can execute the matrix operation method described in the above embodiment to realize matrix transposition. The specific implementation method can refer to the content of the above embodiment, which will not be repeated here. The processor provided in the embodiment of the present application realizes the transposition of the matrix through the matrix register, which can reduce the number of instructions required to transpose the matrix and improve the efficiency of matrix transposition.
[0082] The present application also provides a computer program product including instructions. The computer program product may be a software or program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on the computing device provided in the above embodiment, the computing device executes the matrix operation method provided in the above embodiment.
[0083] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by a computing device or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions that instruct the computing device to perform the matrix operation method provided in the above embodiment.
[0084] In this application, the terms "first", "second", etc. are used to distinguish between identical or similar items with substantially the same effects and functions. It should be understood that there is no logical or temporal dependency between "first" and "second", nor is the quantity and execution order limited. It should also be understood that although the following description uses the terms first, second, etc. to describe various elements, these elements should not be limited by the terms. These terms are only used to distinguish one element from another element. In this application, the term "at least one" means one or more, and the term "multiple" means two or more.
[0085] The above description is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A matrix data processing method, characterized in that: The method comprises: The processor stores first matrix data in a matrix register, the first matrix data includes N rows and M columns, the matrix register includes N rows and M columns, the N rows and M columns of the first matrix data are stored in the N rows and M columns of the matrix register, and the matrix register is located in the processor; The processor reads the first matrix data from the matrix register to the memory in a row-by-row or column-by-column manner to obtain second matrix data after transposing the first matrix data, wherein the second matrix data includes M rows and N columns.
2. The method according to claim 1, characterized in that The processor stores the first matrix data in the matrix register, including: The first matrix data stored in the memory is stored in the matrix register in a row-by-row or column-by-column manner.
3. The method according to claim 1, characterized in that: The processor also includes a vector register; The processor stores the first matrix data in the matrix register, including: Storing the first matrix data in the form of vector data into the vector register; The vector data corresponding to the first matrix data stored in the vector register is stored in the matrix register in a row or column manner.
4. The method according to claim 3, characterized in that The processor reads the first matrix data from the matrix register in a row-by-row or column-by-column manner to obtain second matrix data after transposing the first matrix data, including: Reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, and storing the vector data read each time in the vector register; The vector data stored in the vector register is read into the memory to obtain the second matrix data obtained by transposing the first matrix data.
5. The method according to claim 1, characterized in that The processor also includes a vector register; The processor reads the first matrix data from the matrix register in a row-by-row or column-by-column manner to obtain second matrix data after transposing the first matrix data, including: Reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, and storing the vector data read each time in the vector register; The vector data stored in the vector register is read into the memory to obtain the second matrix data obtained by transposing the first matrix data.
6. The method according to any one of claims 1 to 5, characterized in that: The processor further includes a calculation unit, and after obtaining the second matrix data obtained by transposing the first matrix data, the method further includes: The second matrix data is input into the calculation unit, and the calculation unit performs a specified calculation on the second data matrix.
7. A processor, characterized in that: The processor comprises a matrix register, and the processor is used for: The first matrix data is stored in the matrix register, wherein the first matrix data includes N rows and M columns, the matrix register includes N rows and M columns, and the N rows and M columns of the first matrix data are stored in the N rows and M columns of the matrix register; The first matrix data is read from the matrix register to the memory in a row-by-row or column-by-column manner to obtain second matrix data after transposing the first matrix data, wherein the second matrix data includes M rows and N columns.
8. The processor according to claim 7, characterized in that The processor is used to: The first matrix data stored in the memory is stored in the matrix register in a row-by-row or column-by-column manner.
9. The processor according to claim 7, characterized in that: The processor further comprises a vector register, and the processor is configured to: Storing the first matrix data in the form of vector data into the vector register; The vector data corresponding to the first matrix data stored in the vector register is stored in the matrix register in a row or column manner.
10. The processor according to claim 9, characterized in that The processor is used to: Reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, and storing the vector data read each time in the vector register; The vector data stored in the vector register is read into the memory to obtain the second matrix data obtained by transposing the first matrix data.
11. The processor according to claim 7, characterized in that The processor further comprises a vector register, and the processor is configured to: Reading the first matrix data from the matrix register in a row-by-row or column-by-column manner, and storing the vector data read each time in the vector register; The vector data stored in the vector register is read into the memory to obtain the second matrix data obtained by transposing the first matrix data.
12. The processor according to any one of claims 7 to 11, characterized in that: The processor further comprises a computing unit, and the processor is further configured to: The second matrix data is input into the calculation unit, and the calculation unit performs a specified calculation on the second data matrix.
13. A computing device, characterized in that: The computing device comprises a memory and a processor, the processor comprises a matrix register, and the processor is configured to execute instructions stored in the memory, so that the processor executes the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Matrix transposition method
CN102253925A
Matrix LU decomposition vectorization calculation method of vector DSP core
CN114139108A
Register addressing information for data transfer instructions
CN115956233A
Method and apparatus for efficient matrix transpose
US20190042248A1
Data output apparatus capable of rotating data output therefrom relative to data input thereto
US4566002A
