Matrix storage method and device

By cutting and compressing the sparse matrix, and using displacement value pairs and index matrices/vectors to represent sub-block positions, the problem that existing technology cannot meet the high concurrency requirements of the GPU is solved, and efficient sparse matrix storage and parallel computing are achieved.

WO2024222762A1PCT designated stage expired Publication Date: 2024-10-31HUAWEI TECH CO LTD

Patent Information

Application Number
PCT/CN2024/089679
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-07-14
Filing Date
2024-04-24
Publication Date
2024-10-31

AI Technical Summary

Technical Problem

The existing sparse matrix compression format cannot meet the high concurrency requirements of new types of GPUs and vector processors, resulting in a gradually widening gap between sparse computing efficiency and dense computing, and cannot adapt to arbitrarily distributed sparse matrices.

Method used

By slicing and compressing the original matrix, multiple sub-blocks are obtained and stored in units of these sub-blocks. Displacement value pairs and index matrices/vectors are used to represent the positional relationship of the sub-blocks, optimizing the subscript data and reducing storage requirements. , and use sequential storage during storage to improve memory access efficiency.

Benefits of technology

It achieves efficient sparse matrix storage, improves compression rate and parallelism, adapts to arbitrarily distributed sparse matrices, reduces the number of memory accesses, and improves the performance of dedicated hardware accelerators.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024089679_31102024_PF_FP_ABST
    Figure CN2024089679_31102024_PF_FP_ABST
Patent Text Reader

Abstract

A matrix storage method and device, the method comprising: acquiring a plurality of first sub-blocks corresponding to an original matrix to be stored, the plurality of first sub-blocks being obtained by slicing the original matrix by using sub-blocks with a size of M1×N1 as a unit, and compressing, according to a specified direction, non-zero elements in each sub-matrix obtained after slicing, where M1 and N1 are positive integers; slicing each first sub-block by using sub-blocks with a size of M2×N2 as a unit to obtain a plurality of second sub-blocks, where M2 is a positive integer not greater than M1, and N2 is a positive integer not greater than N1; and storing data of the plurality of second sub-blocks in sequence by using a second sub-block as a storage unit. By providing a compressed storage mode capable of achieving the alignment of non-zero element rows and columns with block rows and columns, it is possible to improve the data locality of matrix data access, the data compression rate, and the degree of parallelism of matrix calculation.
Need to check novelty before this filing date? Find Prior Art

Description

A matrix storage method and device

[0001] CROSS-REFERENCE TO RELATED APPLICATIONS

[0002] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of the People's Republic of China on April 25, 2023, with application number 202310475926.5 and application name "A method for performing sparse matrix processing on a processor", the entire contents of which are incorporated by reference into this application; this application claims priority to the Chinese patent application filed with the State Intellectual Property Office of the People's Republic of China on July 14, 2023, with application number 202310875441.5 and invention name "A matrix storage method and device", the entire contents of which are incorporated by reference into this application. Technical Field

[0003] The present application relates to the field of data processing technology, and in particular to a matrix storage method and device. Background Art

[0004] Sparse matrix data formats are the most important data types for artificial intelligence (AI), high-performance computing (HPC), graph computing, and convolutional graph neural networks (GNNs). Currently, sparse matrix data formats include the coordinate format (COO), compressed sparse row format (CSR), compressed sparse column format (CSC), and ELLPACK format (ELL).

[0005] At present, new types of matrix accelerators such as CPU, GPU, NPU, PIM and other chips are booming. The efficiency gap between sparse computing and dense computing is gradually widening. The existing sparse compression format may no longer be able to meet the high concurrency requirements of GPU, vector and matrix units. There is an urgent need for a new compression storage method for sparse matrices.

[0006] Summary of the Invention

[0007] The present application provides a matrix storage method and device, which are used to provide a new compressed storage method for sparse matrices.

[0008] In a first aspect, the present application provides a matrix storage method, which can be executed by a device with a data processing function (such as a computing device) or by a component in the device (such as a processor). Taking a computing device as an example, in this method, the computing device can obtain multiple sub-blocks (referred to as first sub-blocks) corresponding to the original matrix. There are multiple ways to obtain it. For example, the computing device receives multiple first sub-blocks input by a user, or the computing device obtains multiple first sub-blocks stored in itself. The computing device first obtains the original matrix and then processes the original matrix to obtain the multiple first sub-blocks.

[0009] Among them, the multiple first sub-blocks are obtained after a computing device or other device performs processing such as slicing and compressing the original matrix. For example, the original matrix is ​​sliced ​​into sub-blocks of size M1×N1 (where M1 and N1 are positive integers) to obtain multiple sub-matrices of size M1×N1, and then the non-zero elements in each sub-matrix are compressed in a specified direction (such as row direction or column direction) to obtain multiple first sub-blocks.

[0010] The computing device then divides each first sub-block into sub-blocks of size M2×N2 to obtain multiple second sub-blocks, where M2 is a positive integer not greater than M1, N2 is a positive integer not greater than N1, and the size of the M2×N2 sub-block is smaller than the size of the M1×N1 sub-block. Data of the multiple second sub-blocks is sequentially stored using the second sub-blocks as storage units.

[0011] Through the above design, multiple first sub-blocks corresponding to the original matrix are obtained. The multiple first sub-blocks are obtained by compressing the non-zero elements in multiple sub-matrices of the original matrix according to a specified direction. Then, each first sub-block is cut into small blocks to obtain multiple second sub-blocks corresponding to one first sub-block.

[0012] Dividing the original matrix into multiple first sub-blocks helps improve the compression ratio, and dividing the first sub-block into multiple second sub-blocks for storage further improves the compression ratio. This allows for adapting to arbitrarily distributed sparse matrices and maintaining a high compression ratio for any sparse matrix. Storing multiple second sub-blocks sequentially using the second sub-block as the storage unit facilitates memory access. A single memory access can read at least one complete second sub-block, reducing the number of memory accesses during sparse matrix calculations. Furthermore, since all rows and columns of the second sub-blocks have the same length, this provides a data access method that is more user-friendly for vector processors or matrix processors. This allows for better locality in both row-order and column-order access, enabling efficient adaptation to dedicated hardware accelerators and improving the parallelism of sparse matrix calculations. On the one hand, a single memory access can read multiple complete second sub-blocks, which can be computed in parallel by multiple dedicated hardware accelerators, thereby improving multi-core parallelism. On the other hand, since a second sub-block can provide a dedicated hardware accelerator with sufficient data volume, it is more suitable for the dedicated hardware accelerator to accelerate the calculation, which is beneficial to improving the intra-core parallelism of a single dedicated accelerator, and the performance of the dedicated hardware accelerator can be greatly improved.

[0013] In a possible implementation, the method further includes:

[0014] The computing device obtains the subscript data for each first subblock in various ways, such as receiving the subscript data for each first subblock input by a user, obtaining the subscript data for each first subblock stored by the computing device, or determining the subscript data for each first subblock based on the obtained original matrix. In some cases, the subscript data for the first subblock may be obtained together with the first subblock, which is not particularly limited.

[0015] Taking a first sub-block as an example, the subscript data of the first sub-block is used to indicate the position (subscript, i.e., row and column number) of each element in the first sub-block in the original matrix; the subscript data of the first sub-block includes the displacement value pair, index matrix, and index vector corresponding to the first sub-block; they are introduced as follows:

[0016] In one example, when the specified direction is row direction:

[0017] (1) The displacement value pair includes a pair of displacement values, denoted as a first displacement value and a second displacement value, wherein the first displacement value is used to indicate a first offset of a corresponding row of the first row (i.e., the starting row) of the first sub-block in the original matrix relative to a starting row (e.g., row 0) of the original matrix, and the second displacement value is used to indicate a second offset of a corresponding column of the first column (i.e., the real column) of the first sub-block in the original matrix relative to a starting column (e.g., column 0) of the original matrix;

[0018] (2) The value of any element (e.g., the first element) in the index matrix is ​​equal to the difference between the column number of the element in the first sub-block corresponding to the first element in the original matrix and the second offset. The multiple elements in the index matrix correspond one-to-one to the multiple elements in the first sub-block.

[0019] (3) The value of any element (e.g., the second element) in the index vector is used to indicate the difference between the row number of the element in the first sub-block corresponding to the second element in the original matrix and the first offset. The multiple elements in the index vector correspond one-to-one to each row of the first sub-block.

[0020] In another example, when the direction is specified as column direction:

[0021] 1) The displacement value pair includes a pair of displacement values, recorded as a first displacement value and a second displacement value, wherein the first displacement value is used to indicate a first offset of a corresponding row of the first row (i.e., the starting row) of the first sub-block in the original matrix relative to the starting row (e.g., row 0) of the original matrix, and the second displacement value is used to indicate a second offset of a corresponding column of the first column (i.e., the first column) of the first sub-block in the original matrix relative to the starting column (e.g., column 0) of the original matrix;

[0022] 2) Multiple elements in the index matrix correspond one-to-one to multiple elements in the first sub-block, and a value of any element in the index matrix (e.g., the first element) is equal to the difference between the row number of the element in the first sub-block corresponding to the first element in the original matrix and the first offset. The multiple elements in the index matrix correspond one-to-one to the multiple elements in the first sub-block.

[0023] 3) The value of any element (e.g., the second element) in the index vector is used to indicate the difference between the column number of the element in the first sub-block corresponding to the second element in the original matrix and the second offset. Multiple elements in the index vector correspond one-to-one to each row of the first sub-block.

[0024] Through the above design, dicing the original matrix can mainly optimize the subscript data of the elements, and split the global subscript data of the original matrix into displacement value pairs, index arrays and index vectors of sub-matrices. Those skilled in the art know that when the scale of the original matrix is ​​large, the original global subscript data range is usually high-precision such as INT64 or INT32, and the data volume is also relatively large. After dicing by the method of the embodiment of the present application, the subscript data range of each submatrix will become smaller, and only a lower subscript precision is required, such as using INT16 or INT8 to fully include the subscript range. Each submatrix only needs to uniformly store one displacement value pair, and the amount of subscript data required to store the entire submatrix can be greatly reduced.

[0025] In a possible implementation, the method further includes: the aforementioned multiple second sub-blocks include multiple third sub-blocks, and the multiple third sub-blocks are obtained by slicing the index matrix corresponding to each first sub-block into sub-blocks of size M2×N2.

[0026] In one possible implementation, the computing device stores a mapping relationship between the second sub-block and the third sub-block, wherein the plurality of second sub-blocks corresponds to the plurality of third sub-blocks in the mapping relationship. Specifically, taking a first sub-block as an example, the plurality of second sub-blocks corresponding to the first sub-block and the plurality of third sub-blocks corresponding to the index matrix corresponding to the first sub-block in the mapping relationship correspond to each other in a one-to-one manner.

[0027] The second sub-block can be stored by selecting a first sub-block in row order or column order from the plurality of first sub-blocks, then using the second sub-block as a storage module and sequentially selecting second sub-blocks from the selected first sub-blocks in row order for storage. Elements within each second sub-block can be stored in row order or column order. Similarly, the third sub-block can be stored by sequentially selecting third sub-blocks from the index matrix corresponding to the selected first sub-block in row order, using the third sub-block as a unit, for storage. Alternatively, all second sub-blocks corresponding to the plurality of first sub-blocks can be arranged in row order or column order, and then second sub-blocks can be sequentially selected from all the arranged second sub-blocks in row order or column order for storage. Similarly, all third sub-blocks corresponding to the plurality of first sub-blocks can be arranged in row order or column order, and then third sub-blocks can be sequentially selected from all the arranged third sub-blocks in row order or column order for storage.

[0028] Among them, multiple second sub-blocks and multiple third sub-blocks can be stored separately or at intervals. Separate storage means that multiple second sub-blocks are stored in the first storage space, and the multiple second sub-blocks are stored sequentially in the first storage space. For example, one storage order in the first storage space is: second sub-block A, second sub-block B, second sub-block C, and so on. Similarly, multiple third sub-blocks are stored in the second storage space, and the multiple third sub-blocks are stored consecutively in the second storage space. For example, one storage order in the second storage space is: the third sub-block corresponding to the second sub-block A, the third sub-block corresponding to the second sub-block B, the third sub-block corresponding to the second sub-block C, and so on. In this way, when reading multiple second sub-blocks sequentially, the third sub-blocks corresponding to the multiple second sub-blocks can also be read sequentially.

[0029] Interval storage means storing the second and third sub-blocks adjacent to each other. This means that a second sub-block and its corresponding third sub-block form a storage module, and each second sub-block and its corresponding third sub-block are stored sequentially. For example, one storage order might be: second sub-block A, second sub-block A's corresponding third sub-block, second sub-block B, second sub-block B's corresponding third sub-block, second sub-block C, and so on. This allows for sequential access to the second and third sub-blocks through a single memory access.

[0030] Optionally, the addresses of the plurality of second sub-blocks and / or the plurality of third sub-blocks stored sequentially are continuous, which facilitates data reading and pre-fetching.

[0031] Through the above design, based on this storage method, data access of multiple consecutive second sub-blocks and / or third sub-blocks can be completed through one data access, which is convenient for memory access, helps to reduce the number of memory accesses during sparse matrix calculations, and can be adapted to dedicated hardware accelerators with high efficiency, which is helpful to improve the parallelism of sparse matrix calculations.

[0032] In one possible implementation, each second sub-block stored by the computing device includes at least one non-zero element; in some cases, before the computing device sequentially stores the values ​​of the elements in each second sub-block, it includes: eliminating the second sub-block including all zero elements.

[0033] Through the above design, the second sub-block of all zeros can be completely deleted and does not need to be stored. In this way, it can adapt to sparse matrices with arbitrary distribution and maintain a high compression rate under any sparse matrix.

[0034] In a second aspect, an embodiment of the present application provides a matrix computing device, which has the function of implementing the above-mentioned first aspect and any possible implementation method example of the first aspect. The beneficial effects can be found in the description of the first aspect and will not be repeated here. The functions can be implemented by hardware or by hardware executing corresponding software implementations. The hardware or software includes one or more modules corresponding to the above-mentioned functions. In one possible design, the structure of the device includes an acquisition module, a processing module, and a storage module. These modules can execute the functions of the above-mentioned first aspect and any possible implementation method example of the first aspect. For details, please refer to the detailed description in the method example and will not be repeated here.

[0035] In a third aspect, an embodiment of the present application provides a computing device having the function of implementing the computing device behavior in the first aspect and any possible implementation method example of the first aspect. The beneficial effects can be found in the description of the first aspect and will not be repeated here. The structure of the computing device includes a processor and a memory, and the processor is configured to support the device to perform the corresponding functions of the computing device in the method example of the first aspect. The memory is coupled to the processor, which stores the necessary program instructions and data for the communication device. The structure of the communication device also includes a communication interface for communicating with other devices.

[0036] In a fourth aspect, the present application also provides a computer-readable storage medium, which stores instructions. When the computer-readable storage medium is run on a computer, it enables the computer to execute the method in the above-mentioned first aspect and each possible implementation of the first aspect, or enables the computer to execute the method in the above-mentioned first aspect and each possible implementation of the first aspect.

[0037] In a fifth aspect, the present application also provides a computer program product comprising instructions, which, when run on a computer, enables the computer to execute the method in the above-mentioned first aspect and each possible implementation of the first aspect, or enables the computer to execute the method in the above-mentioned first aspect and each possible implementation of the first aspect.

[0038] In a sixth aspect, the present application also provides a computer chip, which is connected to a memory, and the chip is used to read and execute a software program stored in the memory, execute the above-mentioned first aspect and the methods in each possible design of the first aspect, or enable the computer to execute the above-mentioned first aspect and the methods in each possible implementation of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figures 1 to 7 are schematic diagrams of a sparse matrix data format;

[0040] FIG8 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application;

[0041] FIG9 is a schematic diagram of a flow chart corresponding to a matrix storage method provided in an embodiment of the present application;

[0042] FIG10 is a schematic diagram of a matrix blocking process provided in an embodiment of the present application;

[0043] FIG11 is a schematic diagram of a process for compressing a submatrix according to an embodiment of the present application;

[0044] FIG12 is a schematic diagram of a submatrix and a corresponding element value array provided in an embodiment of the present application;

[0045] FIG13 is a schematic diagram of another method for compressing a submatrix provided in an embodiment of the present application;

[0046] FIG14 is a schematic diagram of cutting into small pieces provided in an embodiment of the present application;

[0047] FIG15 is a schematic diagram of a small block storage method provided in an embodiment of the present application;

[0048] FIG16 is a schematic diagram of another small block storage method provided in an embodiment of the present application;

[0049] FIG17 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0050] In order to better explain the embodiments of this application, the relevant terms or technologies in this application are first explained:

[0051] 1. Row Vector

[0052] A row vector is a 1×m matrix, where m is any positive integer, for example: x=[x1 x2…x m ].

[0053] Column Vectors

[0054] A column vector is an n×1 matrix, where n is any positive integer, for example:

[0055] Matrix Size / Scale

[0056] An m×n matrix is ​​a rectangular array of elements arranged in m rows and n columns. For example:

[0057] Each number that makes up the matrix is ​​called an element of the matrix. 11 、A 12 ,…,A mn etc. are all elements of matrix A. The subscript (or coordinate) of the element is used to indicate the position of the element in the matrix, which can be the row number (or row coordinate) or column number (or column coordinate) of the element in the matrix. For example, A 11 The subscript "11" indicates that the element is located in the first row and first column of matrix A. 21 Indicates that the element is located in the second row and first column of matrix A. In addition, the subscript of the element can also have different forms, such as A 11 Can also be written as A 1,1 , A 21 Can also be written as A 2,1 Etc. Similar points will not be repeated below.

[0058] It should be noted that the row number of the starting row of a matrix is ​​not limited to 1, but can also be other values, such as 0. Similarly, the column number of the starting column of the matrix is ​​not limited to 1, but can also be other data such as 0. For example, if the row number of the starting row and the column number of the starting column of the above matrix are 0 and 0 respectively, then the subscript of the first element in the matrix is ​​"00", indicating that the element is in the 0th row and 0th column of the matrix.

[0059] 4. Matrix addition

[0060] Matrix addition refers to the addition of two matrices of the same size (i.e., the two matrices have the same number of rows and columns). For example, in the following example, both A and B are m×n matrices, and matrix C = A + B.

[0061] Similarly, matrix subtraction means subtracting the elements at the same position in two matrices of the same size.

[0062] 5. Matrix multiplication

[0063] For two matrices to be multiplied (such as matrices D and E), the number of columns of D is required to be the same as the number of rows of E. For example, if D is an m×n matrix and E is an n×p matrix, their product will be an m×p matrix. For example:

[0064] 6. Sparse matrix / dense matrix

[0065] The matrix is ​​divided into sparse matrix and dense matrix according to the ratio of non-zero elements in the matrix. Non-zero elements refer to elements with values ​​other than 0, while zero elements refer to elements with values ​​equal to 0.

[0066] If most (e.g. more than 50%) elements in a matrix are non-zero elements, then the matrix is ​​a dense matrix. Conversely, if most elements in a matrix are zero, then it is a sparse matrix. Sparsity is used to reflect the proportion of non-zero elements in a sparse matrix. The higher the sparsity, the lower the proportion of non-zero elements. The following is an example of a sparse matrix:

[0067] It is understandable that large matrices require a lot of memory to store, and most elements in a sparse matrix have a value of 0. Therefore, when a computer stores and operates on sparse matrices, the elements of the sparse matrix are usually encoded as sparse codes. Sparse codes usually only include information about non-zero elements (such as the value and subscript of the element). By storing its sparse code, the memory cost of operating the sparse matrix is ​​saved. In contrast, matrices in uncompressed format are generally called dense matrices. Dense matrices only include the values ​​of the matrix elements, and do not store the coordinates of the elements.

[0068] The following is an introduction to several common sparse coding formats (also known as sparse matrix data formats):

[0069] (1) Coordinate format (COO): A matrix is ​​represented by a triplet consisting of three values: row number, column number, and element value. The row number and column number are used to identify the position of the element value in the original matrix. Please refer to Figure 1, which shows the coordinate format corresponding to a simple sparse matrix as an example. Please note that for simplicity, only non-zero elements are shown in the sparse matrix in Figure 1. The remaining elements not shown are zero elements. Similarities will not be repeated below.

[0070] (2) Compressed sparse column format (CSC): sparse matrices are represented using three types of data: column offset, row number, and element value. CSC differs from COO in that it uses column offsets instead of column numbers. The column offset represents the starting offset position of the first non-zero element in a column within the numeric data.

[0071] Figure 2 shows an example of CSC compression. The following describes the three types of data:

[0072] As shown in Figure 2, based on index pointer (011467), we can get several intervals: [0,1), [1,1), [1,4), [4,6), [6,7), which correspond to the elements in columns 0, 1, 2, 3, and 4, respectively. That is, [0,1) corresponds to column 0, [1,1) corresponds to column 1, [1,4) corresponds to column 2, and so on.

[0073] Based on [0,1), we select the 0th row number in indices, which is 0. Since [1,1) does not exist in the valid range, the row number selected in indices based on [1,1) is None. Based on [1,4), we select the 1st to 3rd row numbers in indices, which are 0, 1, and 4. Based on [4,6), we select the 4th and 5th row numbers in indices, which are 4 and 6. Based on [6,7), we select the 6th row number in indices, which is 4.

[0074] Combined with data, we can determine that [0,1) corresponds to the element in the 0th column and 0th row, which is 8. [1,1) has no corresponding element. [1,4) corresponds to the element in the 2nd column and 0th row, which is 2, the element in the 2nd column and 1st row, which is 5, and the element in the 2nd column and 4th row, which is 5. [4,6) corresponds to the element in the 3rd column and 4th row, which is 1, and the element in the 3rd column and 6th row, which is 9. [6,7) corresponds to the element in the 4th column and 4th row, which is 9.

[0075] (3) Compressed sparse row format (CSR): Sparse matrices are represented by three types of data: row offset, column number, and element value. The difference between CSR and COO is that row offset is used instead of row number. The row offset represents the starting offset position of the first non-zero element of a row in the numerical data. Figure 3 shows an example of compression in the CSR format. The usage of the row offset value of CSR is similar to that of the column offset value of CSC. Please refer to the above introduction and will not be repeated here.

[0076] (4) ELLPACK format (ELL): Two matrices with the same rows as the original matrix are used to store values, column indices, and values, respectively. The matrix used to store column indices can be called a column matrix, and the matrix used to store values ​​can be called a value matrix. In these two matrices, if there are no elements, they are padded with zeros to the set row length.

[0077] Figure 4 illustrates an example of ELL compression. As can be seen, if a row contains too much attribute information, the column number matrix and the value matrix will become abnormally "fat," and other rows will have many zero-padded elements at the end, wasting storage space and affecting the compression ratio, which may be relatively low.

[0078] (5) Sliced ​​ELLPACK Format (SELL): SELL is an extension of the ELL format. It divides the input matrix into slices of C adjacent rows and sorts the rows within the slices according to the number of non-zero elements. Each slice is stored in the ELL format, and the number of non-zero elements stored in the ELL may vary from slice to slice. Each slice can have a different row length, which can reduce the number of zero-paddings in each slice, thereby improving the compression rate of the format. In addition, it introduces sorting to save storage overhead. Another variation is to limit the range of matrix sorting to the range of σ. In other words, this format does not sort the matrix rows globally but within σ consecutive rows, which can further reduce the additional computation introduced by sorting. Finally, each slice can be divided into arrays of various sizes for storage according to the distribution of the sorted elements, further reducing the compression rate.

[0079] Figure 5 shows an example of SELL compression. As shown in Figure 5, the original matrix is ​​divided into two stripes. Within each stripe, rows are sorted from largest to smallest by row length (measured by the number of nonzero elements), and the data in each stripe is stored in the ELL format. The first stripe includes an 8×6 array and a 3×6 array, while the second stripe includes a 9×6 array and a 3×6 array.

[0080] The above article introduces several sparse matrix data formats. Currently, sparse matrix data formats are important data types in technical fields such as artificial intelligence (AI), high performance computing (HPC), graph computing (Graph), and graph neural network convolution computing (GNN). These compressed data are widely used in many matrix operation scenarios such as sparse matrix-vector multiplication (SpMV), sparse matrix-sparse vector multiplication (SpMSpV), sparse matrix-matrix multiplication (SpMM), and sparse matrix-sparse matrix multiplication (SpGEMM or SpMSpM or SpSpMM).

[0081] Among them, Compressed Sparse Column (CSC), Compressed Sparse Row (CSR), and Coordinate Matrix (COO) are relatively mainstream data formats. The idea behind these data formats is to compress nonzero elements in the row or column direction to store information such as their values ​​and subscripts. Specifically, as mentioned above, CSC and CSR use two separate arrays to store the values ​​of all nonzero elements in the original sparse matrix, as well as the row or column indices, and use a third array to point to the starting position of each row or column in these arrays. In practice, the main drawback of CSC and CSR is poor locality in accessing values ​​or indices in the sparse matrix. Locality here can be understood as the tendency of a processor (e.g., a CPU) to access memory (e.g., RAM), whether accessing instructions or data, to cluster the accessed storage modules in a small, contiguous area. For example, as shown in Figure 6(a), CSC has good locality for data storage and access in the column direction, but poor locality for row-wise access. Similarly, as shown in Figure 6(b), CSR has good locality for row-wise data storage and access, but poor locality for column-wise access. Furthermore, the inconsistent row or column lengths of this format make it difficult to access multiple vectors simultaneously. This is because a vector instruction can only access row or column vectors of a fixed length. When the row or column lengths are inconsistent, multiple vectors cannot be accessed using a single vector instruction. This also makes it difficult to use processors with higher parallelism to accelerate matrix calculations.

[0082] In recent years, the elliptic equation format (ELLPACK) has also received considerable attention. The ELLPACK format is designed to improve the execution efficiency of vector processors. As shown in Figure 7, compared to the compressed sparse row format, it uses zero-padding to ensure that the length of the nonzero elements in each row is consistent. For a matrix A of size m×n and a maximum row length (the number of nonzero elements in that row) of k, it requires an array of size m×k to store matrix A. The main disadvantage of the ELLPACK format is that if the nonzero elements in the original sparse matrix are irregularly distributed, the compressed row lengths will vary significantly. Consequently, the ELLPACK array will contain more zero padding, resulting in a decrease in compression and, consequently, a reduction in the computational efficiency of the vector processor.

[0083] Based on the ELLPACK format, several improved data formats have been developed, such as the Sliced ​​Elliptic Equation format (SELL). Referring back to Figure 5, although the Sliced ​​Elliptic Equation format can further reduce the compression ratio compared to the ELLPACK format, it suffers from the same problem as the ELLPACK format: when the non-zero elements within a slice (such as a strip) are irregularly distributed, the compression ratio remains low.

[0084] In summary, existing sparse matrix compression formats all have shortcomings that prevent them from meeting the high concurrency requirements of newer vector processors (or matrix accelerators), thus limiting their development. A data format that maintains data locality and high compression rates is crucial and a key technology driving the development of sparse matrix computation accelerator chips.

[0085] In view of this, the present application provides a matrix storage method, which is implemented based on a new sparse matrix data format provided by the present application. The data format is applicable to sparse matrices of arbitrary sparsity and arbitrary non-zero element distribution, and can solve the determination of the above-mentioned existing data formats, including good locality and high compression rate. In short, in this method, taking the execution of a computing device as an example, the computing device processes the original matrix based on the data format to obtain a sparse code of the data format, and stores the sparse code of the data format with a fixed-size array as the granularity. This storage method can save storage overhead, and has good data locality, high compression rate and parallelism.

[0086] Next, the technical solution provided in the embodiments of the present application is described in detail with reference to the accompanying drawings.

[0087] FIG8 is a schematic diagram of a computing device provided by the present application, wherein the computing device 800 includes: a processor 801 and a memory 802. Optionally, the device 800 may further include a communication interface 804 and a matrix operator 806, wherein the processor 801, the memory 802, the matrix operator 806 and the communication interface 804 may be interconnected via a communication line 805; the communication line 805 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The communication line 805 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, FIG8 is represented by only one thick line, but this does not mean that there is only one bus or one type of bus.

[0088] The processor 801 can be a central processing unit (CPU), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA), an artificial intelligence (AI) chip, a system on chip (SoC) or a complex programmable logic device (CPLD), a graphics processing unit (GPU), a neural network processing unit (NPU), a microprocessor, or one or more integrated circuits for controlling the execution of the program of the present application.

[0089] Please note that FIG8 shows only one processor 801. In actual applications, there may be multiple processors 801. The multiple processors 801 may include multiple processors of the same type or multiple processors of different types. For example, the multiple processors 801 may include multiple CPUs. For another example, the multiple processors 801 may include at least one CPU and at least one GPU. A CPU may have one or more CPU cores. This embodiment does not limit the number of processors 801 or the number of CPU cores.

[0090] Specifically, the processor 801 is used to process data access requests from outside the computing device 800 (such as other computing devices), and is also used to process requests generated within the computing device 800. For example, the request is a write data request, and the write data request includes an original matrix, which may be a sparse matrix. After receiving the write data request, the processor 801 may execute the matrix storage method provided in the embodiment of the present application, process the original matrix, and save the processed data in the memory 802. For another example, the request may also be a read data request, and the read data request is used to request to read matrix data. The matrix data may include part or all of the original matrix. After receiving the read data request, the processor 801 reads the matrix data from the memory 802.

[0091] In addition, the processor 801 is also used to perform other data calculations or processing, such as matrix operations, specifically sparse matrix-vector multiplication (SpMV), sparse matrix sparse vector multiplication (SpMSpV), sparse matrix-matrix multiplication (SpMM), sparse matrix sparse matrix multiplication (SpGEMM or SpMSpM or SpSpMM), etc., without specific limitation. Optionally, the above-mentioned matrix operations can also be assigned to the matrix operator 806 for execution. In some cases, when the processor 801 has multiple cores, or the computing device 800 includes multiple processors 801, or the computing device 800 includes multiple matrix operators 806, multiple cores or multiple processors 801 or multiple matrix operators 806 can perform matrix operations in parallel, which will not be emphasized here.

[0092] Matrix operator 806 can be used to perform matrix operations. For details about matrix operations, see the above description and will not be repeated here. Matrix operator 806 may include, but is not limited to, a vector processor (VP), a vector processor system (VPS), a matrix processor, a matrix accelerator, and the like.

[0093] The communication interface 804 uses any transceiver or other device to communicate with other devices or communication networks, such as Ethernet, radio access network (RAN), wireless local area network (WLAN), wired access network, etc. For example, it can obtain read data requests, write data requests, matrix data, etc.

[0094] Memory 802 is used to store data and computer-executable program code. The data includes, but is not limited to, original matrix data, sparse matrix sparse coding, etc. The executable program code may include the program code of the matrix storage method provided in the embodiment of the present application. The processor 801 executes the executable program code to implement the matrix storage method provided in the embodiment. In other words, memory 802 stores computer-executable instructions for executing the solution of the present application, and the execution is controlled by processor 801. Processor 801 is used to execute the computer-executable instructions stored in memory 802, thereby implementing the matrix storage method provided in the above-mentioned embodiment of the present application.

[0095] For example, the memory 802 may also store a sparse matrix function library (spares blas), which may include but is not limited to one or more instructions provided in the present embodiment: a block instruction (the function of the instruction can be found in the description of step 902, which will not be repeated here), a compression processing instruction (the function of the instruction can be found in the description of step 903, which will not be repeated here), a zero filling instruction, a tiling instruction (the function of the instruction can be found in the description of step 904, which will not be repeated here). And the function library may also include other instructions, such as instructions for implementing existing sparse matrix data formats, such as compressed sparse column format instructions, elliptic equation format instructions, etc. The memory 802 may also store matrix operation instructions, such as read vector instructions, sparse matrix-vector multiplication (SpMV), sparse matrix sparse vector multiplication (SpMSpV), sparse matrix-matrix multiplication (SpMM), sparse matrix sparse matrix multiplication (SpGEMM or SpMSpM or SpSpMM), etc. Optionally, the memory 802 may also store source code for implementing any instruction or function in the function library.

[0096] The memory 802 can be independent and connected to the processor via a communication line 805. The memory 802 can also be integrated with the processor. Specifically, for example, the memory 802 can include a memory and can also include a hard disk. The memory refers to an internal memory that directly exchanges data with the processor 801. It can read and write data at any time and at a very high speed, and serves as a temporary data storage for the operating system or other running programs. Unlike the memory, the hard disk reads and writes data slower than the memory and is usually used to store data persistently. In some application scenarios, the processor 801 can temporarily store data (such as sparse coding of a sparse matrix) in the memory. When the total amount of data in the memory reaches a certain threshold, the processor 801 sends the data stored in the memory to the hard disk for persistent storage. These data can be obtained from an external device, or input by a user, or generated by the computing device 800, and are not specifically limited. Alternatively, the processor 801 reads data from the memory. When the memory misses, the processor 801 reads the data from the hard disk into the memory, and then reads the data from the memory.

[0097] Among them, the memory includes at least two types of memory. For example, the memory can be either random access memory or read-only memory (ROM). For example, the random access memory is dynamic random access memory (DRAM) or storage class memory (SCM). The memory can also include other random access memories, such as static random access memory (SRAM). As for the read-only memory, for example, it can be programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), etc. In addition, the memory can also be a dual in-line memory module or a dual in-line memory module (DIMM), that is, a module composed of DRAM. In actual applications, multiple memories and different types of memories can be configured in the computing device 800. This embodiment does not limit the number and type of memory.

[0098] The hard disk can specifically be a disk or other type of storage medium, such as a solid-state drive (SSD), a hard disk drive (HDD), a compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital versatile discs, Blu-ray discs, etc.), a disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.

[0099] It should be noted that the structure of the computing device 800 shown in Figure 8 is only an example. In actual applications, the computing device 800 may have more or fewer components. For example, the computing device 800 may also include input / output devices such as a keyboard, a mouse, and a display screen. This embodiment of the present application does not limit this.

[0100] Next, taking the computing device shown in FIG8 as an example, the data format and the corresponding matrix storage method provided in the embodiment of the present application are introduced in detail.

[0101] FIG9 is a flow chart of the compression storage method provided in an embodiment of the present application. The method can be executed by a computing device (such as computing device 800) having data processing capabilities, or by a component of the computing device, such as processor 801 or matrix operator 806. For ease of explanation, the following description will be made using computing device 800 as an example. As shown in FIG9 , the method includes:

[0102] Step 901: Obtain matrix data of the original matrix to be processed.

[0103] There are also multiple ways to obtain matrix data. One way is for the processor 801 to receive matrix data input from an external source. For example, the matrix data can be input by a user to the computing device 800 through a terminal device, or directly input by the user into the computing device 800. Accordingly, the computing device 800 (processor 801) receives the matrix data input directly or indirectly by the user. Another way is for the processor 801 to obtain the matrix data locally from the computing device 800, such as the processor 801 obtaining the matrix data from the memory 802.

[0104] Wherein, the matrix data can be the data of the original matrix, wherein the original matrix can be a dense matrix or a sparse matrix of arbitrary sparsity. Alternatively, the matrix data can be a sparse code of a sparse matrix, and the data format adopted by the sparse code can be any sparse matrix data format other than the data format provided in this application, such as but not limited to: CSC, CSR, COO, ELLPACK, etc. In short, sparse codes of any data format that can restore the original matrix are applicable to the embodiments of this application. Alternatively, the matrix data can also be data after further value-added processing of the sparse code, and the value-added processing such as deduplication, compression, etc. is not specifically limited.

[0105] The above is merely an example. In actual applications, any type of matrix data and any acquisition method are applicable to the embodiments of the present application.

[0106] In step 902 , the original matrix is ​​divided into blocks of size M1×N1 to obtain a plurality of sub-matrices.

[0107] Step 902 is described by taking the original matrix being a sparse matrix as an example.

[0108] FIG10 is a schematic diagram of a sparse matrix slicing method provided by this embodiment. Assuming that the size of the sparse matrix is ​​X×Y and the size of the sub-block is M1×N1, where M1 and N1 are positive integers, M1×N1 is less than X×Y, and in one case, M1 is a positive integer less than X, and N1 is a positive integer not greater than Y, or M1 is a positive integer not greater than X, and N1 is a positive integer less than Y. Generally, M1 is a positive integer less than X, and N1 is a positive integer less than Y. M1 can be equal to N1, and X can be equal to Y. For ease of explanation, assuming that M1=N1=3 in FIG10, the sparse matrix is ​​sliced ​​into sub-blocks of size 3×3, and at least one sub-matrix of size 3×3 can be obtained, that is, the sparse matrix is ​​divided into multiple sub-matrices of size 3×3. The multiple sub-matrices are adjacent to each other in sequence and do not overlap. In addition, since X may not be an integer multiple of M1 and / or Y may not be an integer multiple of N1, the scales of the multiple sub-matrices may be completely the same, completely different, or not completely the same.

[0109] As shown in Figure 10 (a), the sparse matrix A is cut into 4 sub-matrices with a sub-block size of 3×3. The scale of the 4 sub-matrices is the same, all 3×3. As shown in Figure 10 (b), the sparse matrix B is cut into 4 sub-matrices with a sub-block size of 3×3. The scales of the 4 sub-matrices are not exactly the same. The scales of the two upper sub-matrices are 3×3, and the scales of the two lower sub-matrices are 3×2. As shown in Figure 10 (c), the sparse matrix C is cut into 4 sub-matrices with a sub-block size of 3×3. The scales of the 4 sub-matrices are completely different. The scales of the 4 sub-matrices are: 3×3, 2×3, 3×2, and 2×2 respectively. Please note that the scale of the sparse matrix and the sub-block scale in Figure 10 are examples. The scale of the sparse matrix in actual applications is usually relatively large, and this application does not limit this.

[0110] Step 903: perform compression processing on each sub-matrix to obtain compressed data corresponding to each sub-matrix.

[0111] Taking a sub-matrix as an example, the compression processing flow provided in this application may include: the processor 801 compresses the non-zero elements in the sub-matrix in a specified direction (such as row direction or column direction) to obtain compressed data of the sub-matrix.

[0112] The following describes the compression processing method provided by this embodiment, combining the two cases of specifying the direction as row or column. Hereinafter, compression in the row direction may also be referred to as row compression, and the two have the same meaning. Similarly, compression in the column direction may also be referred to as column compression.

[0113] 1. Specify the direction as row direction.

[0114] Refer to Figure 11. In Figure 11, the sparse matrix is ​​cut into 4 sub-matrices with a sub-block size of M1×N1. The 4 sub-matrices are respectively recorded as sub-matrix 1, sub-matrix 2, sub-matrix 3, and sub-matrix 4 in order from left to right and from top to bottom.

[0115] First, taking the sub-matrix 1 shown in FIG11 as an example, the specific process of the processor 801 compressing the sub-matrix 1 may include:

[0116] The non-zero elements in submatrix 1 are compressed in a row direction to generate compressed data of submatrix 1. The compressed data of the submatrix includes: an element value array (such as block data) of submatrix 1 and subscript data.

[0117] (1) The element value array of submatrix 1 includes the non-zero elements in submatrix 1.

[0118] Wherein, the element value array includes the non-zero elements arranged in row order in submatrix 1, and the non-zero elements in the same row in the original matrix are still in the same row in the element value array, and there is no interval between the non-zero elements in the element value array. The compression process for obtaining the element value array can include: compressing the non-zero elements in submatrix 1 in the row direction (when there is a full element row in the submatrix, the entire row of all zero elements can be compressed), and performing zero padding according to the maximum row length after compression to obtain the element value array corresponding to submatrix 1 (also referred to as element value matrix). Optionally, the maximum row length after compression according to submatrix 1 can also be zero-padding to obtain the element value data corresponding to submatrix 1, in which all row lengths and all column lengths are consistent. The compression process of the element value arrays of the remaining submatrices is the same, and will not be repeated here.

[0119] (2) The subscript data of submatrix 1 (such as global index) is used to indicate the position (or subscript, hereinafter referred to as original position / original subscript) of each non-zero element in the element value array in the original matrix. The original subscript includes the original row number and the original column number. The original row number refers to the row number of the non-zero element in the element value array in the original matrix, and the original column number refers to the column number of the non-zero element in the element value array in the original matrix. For example, in Figure 11, the original subscript of the element in the 0th row and 0th column of the element value array in the original matrix is ​​(0,0). For another example, the original subscript of the element in the 0th row and 1st column of the element value array in the original matrix is ​​(0,2). For another example, the original subscript of the element in the 0th row and 2nd column of the element value array in the original matrix is ​​(0,6).

[0120] The subscript data of submatrix 1 includes a displacement value pair of submatrix 1, an index array (or index matrix), and an index vector.

[0121] 1) First, introduce the displacement value pair:

[0122] The subscript data of submatrix 1 includes a displacement value pair (such as block offset) of the submatrix. The so-called displacement value pair includes a row offset and a column offset. The row offset refers to the offset between the starting row of the submatrix and the starting row of the original matrix. The column offset refers to the offset between the starting column of the submatrix and the starting column of the original matrix.

[0123] For understanding purposes of Figure 11 , assume that the subblock size is M1×N1=16×16. The starting row and column numbers of the original matrix are 0. Submatrix 1's (original starting row number, original starting column number) is (0, 0); submatrix 2's (original starting row number, original starting column number) is (0, 16); submatrix 3's (original starting row number, original starting column number) is (16, 0); and submatrix 4's (original starting row number, original starting column number) is (16, 16).

[0124] Based on this, the row offset of submatrix 1 = the original row number of the starting row of submatrix 1 - the row number of the starting row of the original matrix = 0 - 0 = 0, and the column offset of submatrix 1 = the original column number of the starting column of submatrix 1 - the column number of the starting column of the original matrix = 0. That is, the displacement value pair of submatrix 1 is (0, 0).

[0125] The row offset of submatrix 2 = the original row number of the starting row of submatrix 2 - the row number of the starting row of the original matrix = 0 - 0 = 0. The column offset of submatrix 2 = the original column number of the starting column of submatrix 2 - the column number of the starting column of the original matrix = 16 - 0 = 16. That is, the displacement value pair of submatrix 2 is (0, 16).

[0126] The row offset of submatrix 3 = the original row number of the starting row of submatrix 3 - the row number of the starting row of the original matrix = 16 - 0 = 16. The column offset of submatrix 3 = the original column number of the starting column of submatrix 3 - the column number of the starting column of the original matrix = 0 - 0 = 0. That is, the displacement value pair of submatrix 3 is (16, 0).

[0127] The row offset of submatrix 4 = the original row number of the starting row of submatrix 4 - the row number of the starting row of the original matrix = 16 - 0 = 16. The column offset of submatrix 4 = the original column number of the starting column of submatrix 4 - the column number of the starting column of the original matrix = 16 - 0 = 16. That is, the displacement value pair of submatrix 4 is (16, 16).

[0128] 2) Index arrays and index vectors are introduced as follows:

[0129] When the specified direction is different, the elements in the index array and index vector have different meanings. The following continues to specify the direction as the row direction to introduce the index data and index vector of submatrix 1.

[0130] The values ​​of the elements in submatrix 1's index array and the column offset of submatrix 1 are used to indicate the original column number of the corresponding element in submatrix 1's element value array. Specifically, as shown in Formula 1: The original column number of any element in submatrix 1's element value array (denoted as the first element) = the column offset of submatrix 1 + the value of the element corresponding to the first element in submatrix 1's index array (denoted as the second element). Simply put, the original column number of a nonzero element = the row offset + the value of the corresponding element in the index array.

[0131] The elements in the index array of submatrix 1 correspond one-to-one with the elements in the element value array of submatrix 1. A one-to-one correspondence means that two elements at the same position in the index array and the element value array correspond one-to-one. In other words, the element at position (i, j) in the index array (e.g., the first element) corresponds to the element at position (i, j) in the element value array (e.g., the second element). For example, the element at position (0, 0) in the index array corresponds to the element at position (0, 0) in the element value array, the element at position (0, 1) in the index array corresponds to the element at position (0, 1) in the element value array, the element at position (16, 4) in the index array corresponds to the element at position (16, 4) in the element value array, and so on.

[0132] Based on Formula 1, the value of any element in the index array of submatrix 1 = the original column number of the element at the corresponding position in the element value array of submatrix 1 - the column offset of submatrix 1. For example, referring to Figure 11, the original column number of the element at position (0,1) in the element value array of submatrix 1 (such as the element marked with "a" in Figure 11) is 2. Since the column offset of submatrix 1 is 0, the value of the element at position (0,1) in the index array of submatrix 1 = 2 - 0 = 2. For another example, the original column number of the element at position (0,2) in the element value array of submatrix 1 (such as the element marked with "b" in Figure 11) is 6. Since the column offset of submatrix 1 is 0, the value of the element at position (0,2) in the index array of submatrix 1 = 6 - 0 = 6. For another example, the original column number of the element at position (1,0) in the element value array of submatrix 1 (such as the element marked with "c" in Figure 11) is 3. Since the column offset of submatrix 1 is 0, the value of the element at position (1,0) in the index array of submatrix 1 = 3-0 = 3.

[0133] The values ​​of the elements in the index vector of submatrix 1 and the row offset of submatrix 1 are used to indicate the original row number of the corresponding element in the element value array of submatrix 1. Specifically, as shown in Formula 2: The original row number of any element (denoted as the first element) in the element value array of submatrix 1 = the row offset of submatrix 1 + the value of the element corresponding to the first element (e.g., the second element) in the index vector of submatrix 1. Simply put, the original row number of a nonzero element = the row offset + the value of the corresponding element in the index vector.

[0134] The elements of the index vector correspond one-to-one to the rows of the value array. For example, the 0th element of the index vector corresponds to the 0th row of the value array, the 1st element of the index vector corresponds to the 1st row of the value array, the i-th element of the index vector corresponds to the i-th row of the value array, and so on. Alternatively, the element in the i-th row of the value array corresponds to the i-th element of the index vector.

[0135] Based on Formula 2, the value of any element in the index vector of submatrix 1 = the original row number of the corresponding row element in the element value array of submatrix 1 - the row offset of submatrix 1. The index vector can be a 1×n matrix, or an n×1 matrix. Assume that the index vector of submatrix 1 is x = [x0 x1…x n-1 ], continue to refer to Figure 11, for example, the original row number of the elements in the 0th row of the element value array of submatrix 1 is all 0. Since the row offset of submatrix 1 is 0, the value of the 0th element in the index vector of submatrix 1 (such as x0) = 0-0 = 0. For another example, the original row number of the elements in the 1st row of the element value array of submatrix 1 is all 1. Since the row offset of submatrix 1 is 0, the value of the 1st row element in the index vector of submatrix 1 (such as x1) = 1-0 = 1. For another example, the original row number of the elements in the 15th row of the element value array of submatrix 1 is all 15. Since the row offset of submatrix 1 is 0, the value of the 15th row element in the index vector of submatrix 1 (such as x1) 15 ) = 15 - 0 = 15. That is, the index vector of submatrix 1 is [0 1 2 3 ... 14 15]. It can be understood that when submatrix 1 does not include a row of all zero elements, the value of the element in the index vector of submatrix 1 is the original row number of the corresponding row element in the element value array.

[0136] The above describes the process of determining the compressed data of submatrix 1. The following briefly describes examples of determining the compressed data of other submatrices.

[0137] As mentioned above, the displacement value pair of submatrix 2 is (0, 16). Combined with Figures 11 and 12, for example, the original subscript of the element at position (0, 0) in the element value array of submatrix 2 (such as the element marked with "e" in Figure 11) is (3, 19), and the value of the element at position (0, 0) in the index array of submatrix 2 = 19-16 = 3.

[0138] For another example, the element at position (0,1) in the element value array of submatrix 2 (e.g., the element labeled "f" in FIG12 ) has an original subscript of (3,20). The value of the element at position (0,1) in the index array of submatrix 2 is 20-16 = 4. The value of the 0th element in the index vector of submatrix 2 (corresponding to the element in row 0 of the element value array, such as e and f) is 3-0 = 3.

[0139] For another example, the element at position (1,0) in the element value array of submatrix 2 (e.g., the element labeled "g" in Figure 12) has an original subscript of (7,17). The value of the element at position (1,0) in the index array of submatrix 2 is 17-16 = 1. The value of the first element in the index vector of submatrix 2 (corresponding to the element in the first row of the element value array, e.g., g) is 7-0 = 7.

[0140] Similarly, the index array and index vector of submatrix 2 are determined.

[0141] As previously mentioned, the displacement value pair of submatrix 3 is (16, 0). In conjunction with Figures 11 and 12 , for example, the original subscript of the element at position (0, 0) in the element value array of submatrix 3 (e.g., the element labeled "h" in Figure 12 ) is (16, 6). The value of the element at position (0, 0) in the index array of submatrix 3 is 6 - 0 = 6. The value of the 0th element in the index vector of submatrix 3 (corresponding to the element in row 0 of the element value array, e.g., h) is 16 - 16 = 0.

[0142] For another example, the element at position (2,0) in the element value array of submatrix 3 (e.g., the element labeled "k" in Figure 12) has an original subscript of (18,3). The value of the element at position (2,0) in the index array of submatrix 3 is 3-0 = 3. The value of the second element in the index vector of submatrix 3 (corresponding to the element in the second row of the element value array, e.g., k) is 18-16 = 2.

[0143] For example, the element at position (3,1) in the element value array of matrix 3 (e.g., the element marked with "o" in Figure 12) has an original subscript of (19,6). The value of the element at position (3,1) in the index array of submatrix 3 is 6-0 = 6. The value of the third element in the index vector of submatrix 3 (corresponding to the element in the second row of the element value array, such as o) is 19-16 = 3.

[0144] Similarly, the index array and index vector of submatrix 3 are determined.

[0145] As previously mentioned, the shift value pair of submatrix 4 is (16, 16). In conjunction with Figures 11 and 12 , for example, the original subscripts of the element at position (0, 1) in the element value array of submatrix 4 (e.g., the element labeled "y" in Figure 12 ) are (17, 18). The value of the element at position (0, 1) in the index array of submatrix 3 is 18 - 16 = 2. The value of the 0th element in the index vector of submatrix 4 (corresponding to the element in row 0 of the element value array, e.g., y) is 17 - 16 = 1.

[0146] For another example, the element at position (1,1) in the element value array of submatrix 4 (e.g., the element labeled "w" in FIG12 ) has an original subscript of (20,19). The value of the element at position (1,1) in the index array of submatrix 4 is 19-16 = 3. The value of the first element in the index vector of submatrix 4 (corresponding to the element in the first row of the element value array, e.g., w) is 20-16 = 4.

[0147] Similarly, the index array and index vector of submatrix 4 are determined.

[0148] It can be seen from the above design that dicing the original matrix can mainly optimize the subscript data of the elements, and split the global subscript data of the original matrix into displacement value pairs, index arrays and index vectors of sub-matrices. Those skilled in the art know that when the scale of the original matrix is ​​large, the original global subscript data range is usually high-precision such as INT64 or INT32, and the data volume is also relatively large. After dicing by the method of the embodiment of the present application, the subscript data range of each submatrix will become smaller, and only a lower subscript precision is required. For example, using INT16 or INT8 can completely include the subscript range. Each submatrix only needs to uniformly store one displacement value pair, and the amount of subscript data required to store the entire submatrix can be greatly reduced.

[0149] The above describes the process of obtaining compressed data of each sub-matrix by compressing in the row direction. The following describes the process of obtaining compressed data of each sub-matrix by compressing in the column direction.

[0150] 2. Specify the direction as column direction.

[0151] Please note that regardless of whether the compression direction is row or column, it does not affect the sub-matrix displacement value pairs, that is, the determined sub-matrix displacement value pairs are the same. Please refer to the above method for determining the sub-matrix displacement value pairs, which will not be repeated here. The following only explains the differences.

[0152] Figure 13 is a schematic diagram of compressing a submatrix in the column direction provided by this embodiment. Taking submatrix 1 shown in Figure 13 as an example, submatrix 1 is compressed in the column direction to obtain compressed data of submatrix 1. The compressed data of submatrix 1 includes the element value array and subscript data of submatrix 1.

[0153] Among them, the element value array of submatrix 1 includes the non-zero elements in submatrix 1 arranged in column order, the non-zero elements in the same column in the original matrix are still in the same column in the element value array, and there is no gap between the non-zero elements in the element value array. The compression process of obtaining the element value array can include: compressing the non-zero elements in submatrix 1 in the column direction (when there is a full element column in the submatrix, the entire zero element column can be compressed), and then zero-padding is performed according to the maximum column length after compression, thereby obtaining the element value array corresponding to the submatrix. The compression process of the element value arrays of the remaining submatrices is the same, and will not be repeated here.

[0154] The subscript data of submatrix 1 includes the displacement value pair, index array and index vector of submatrix 1.

[0155] The values ​​of the elements in submatrix 1's index array and the row offset of submatrix 1 are used to indicate the original row number of the corresponding element in submatrix 1's element value array. Specifically, as shown in Formula 3: The original row number of any element in submatrix 1's element value array (denoted as the first element) = the column offset of submatrix 1 + the value of the element corresponding to the first element in submatrix 1's index array (denoted as the second element). Simply put, the original row number of a nonzero element = the row offset + the value of the corresponding element in the index array.

[0156] Based on Formula 3, the value of any element in the index array of submatrix 1 is calculated as follows: the original row number of the element at the corresponding position in the element value array of submatrix 1 minus the row offset of submatrix 1. For example, referring to Figure 13, if the original row number of the element at position (0,1) in the element value array of submatrix 1 is 4 and the row offset of submatrix 1 is 0, then the value of the element at position (0,1) in the index array of submatrix 1 = 4 - 0 = 4. For another example, if the original row number of the element at position (3,2) in the element value array of submatrix 1 is 7 and the row offset of submatrix 1 is 0, then the value of the element at position (3,2) in the index array of submatrix 1 = 7 - 0 = 7. For another example, if the original row number of the element at position (5,5) in the element value array of submatrix 1 is 12 and the row offset of submatrix 1 is 0, then the value of the element at position (5.5) in the index array of submatrix 1 = 12 - 0 = 12. And so on.

[0157] The values ​​of the elements in the index vector of submatrix 1 and the column offset of submatrix 1 are used to indicate the original column number of the corresponding element in the element value array of submatrix 1. Specifically, as shown in Formula 4, the original column number of any element (denoted as the first element) in the element value array of submatrix 1 = the row offset of submatrix 1 + the value of the element corresponding to the first element (e.g., the second element) in the index vector of submatrix 1. Simply put, the original column number of a nonzero element = the column offset + the value of the corresponding element in the index vector.

[0158] Based on Formula 4, the value of any element in the index vector of submatrix 1 = the original column number of the corresponding row element in the element value array of submatrix 1 - the column offset of submatrix 1. The index vector can be a 1×m matrix, or an m×1 matrix. For example, referring to FIG13 , it is assumed that the index vector of submatrix 1 is a 1×m matrix, for example: y = [y0 y1…y m-1 ], m = the number of columns in the submatrix's element value array. For example, if the original column number of the elements in the 0th column of the submatrix's element value array is 0, and the column offset of submatrix 1 is 0, then the value of the element in the 0th column of the submatrix's index vector (e.g., y0) is 0-0=0. For another example, if the original column number of the elements in the 1st column of the submatrix's element value array is 1, then the value of the element in the 1st column of the submatrix's index vector (e.g., y1) is 1-0=1. And so on. When submatrix 1 does not include a column of all-zero elements, the value of the element in the submatrix's index vector is the original column number of the corresponding row element in the element value array.

[0159] The above describes two compression methods for compression directions. This application does not limit every matrix to using row compression or column compression. In practical applications, the use of row or column compression can be determined based on the needs of the matrix multiplication operation. For example, the left matrix of a sparse matrix multiplication (SpGEMM) (referring to one of the two matrices to be multiplied in the SpGEMM) can be stored using row compression, while the right matrix (referring to the other matrix to be multiplied in the SpGEMM) can be stored using column compression. This achieves better locality and is also suitable for use with GPU and vector processor architectures.

[0160] It should be noted that the present application may also compress different sub-matrices of an original matrix in different directions. However, generally, to improve processing efficiency, in step 903, the compression method of each sub-matrix may be the same. For example, if the compression method is in the row direction, each sub-matrix of the original matrix is ​​compressed in the row direction. If the compression method is in the column direction, each sub-matrix of the original matrix is ​​compressed in the column direction.

[0161] It should also be noted that steps 901 to 903 are optional steps and are not necessarily performed. For example, the computing device 800 may also obtain compressed data corresponding to each sub-matrix from other devices.

[0162] Step 904 : Tiling the arrays (eg, element value arrays, index arrays) in the compressed data of each sub-matrix into sub-blocks of size M 2 × N 2 to obtain multiple small blocks corresponding to each sub-matrix.

[0163] Taking a submatrix as an example, the process of slicing the compressed data of the submatrix into small blocks may include: slicing the element value array of the submatrix into small blocks of size M2×N2 to obtain multiple small blocks (referred to as the second subblock). Slicing the index array of the submatrix into small blocks of size M2×N2 to obtain multiple small blocks (referred to as the third subblock). Wherein, M2 is a positive integer not greater than M1, N2 is a positive integer not greater than N1, and M2×N2 is less than M1×N1. In one case, M2 is a positive integer less than M1 and N2 is a positive integer not greater than N1, or M2 is a positive integer not greater than M1 and N2 is a positive integer less than N1. That is, the size of the subblock is usually smaller than the size of the element value array or the index array.

[0164] Taking the element value array shown in Figure 12 as an example, Figure 14 is a schematic diagram of cutting the element value array of each sub-matrix into small blocks provided by an embodiment of the present application. In Figure 14, the sub-block scale is M2×N2=2×2. As shown in Figure 14, the processor 801 can cut each element value array into small blocks with a scale of 2×2 in the row direction. In this way, each element value array is cut into multiple small blocks, and each small block is the same size (as shown in Figure 14, the scale is 2×2) and does not overlap with each other. Optionally, when the element at the end of the row direction or column direction of the element value array is less than 2×2, zero padding is performed to make up the scale of 2×2. For example, referring to the bottom of the element value array of block2 in Figure 14, tile 22 is a small block obtained by zero padding, wherein the zero-filled elements in the small block are shown by the dotted box.

[0165] Similarly, processor 801 blocks the submatrix index array into M2×N2 sub-blocks to obtain multiple sub-blocks of the index array, each of which is of the same size and does not overlap. The effect of slicing the index array into small blocks can be seen in the effect of slicing the submatrix element value array into small blocks shown in FIG14 , and will not be further described here.

[0166] Please understand that the 2×2 sub-block size shown in Figure 14 is only an example. The embodiment of the present application does not limit the sub-block size used for cutting into small blocks. This embodiment also provides a method for determining the sub-block size, such as determining it based on the data bit width of a matrix operator such as a vector processor or a matrix accelerator. For example, if the length of the vector processor is 2, then it can be cut into small blocks based on 2×2 sub-blocks. For another example, if the length of the vector processor is 4, then it can be cut into small blocks based on 4*4 sub-blocks to adapt to the data bit width of the matrix operator. This is only an example of determining the sub-block size. This embodiment can also apply other methods to determine the sub-block size in this step, and there is no specific limitation.

[0167] It should be noted that the element value arrays and index arrays shown in the above Figures 11 to 14 are all shown as examples of compression processing including zero padding. The compression processing in the embodiments of the present application may also not include zero padding, such as the element value array and the index array do not include zero elements, such as removing the zero elements in the element value array and index array in Figures 11 to 15, and then cutting the element value array and index array without zero elements into small blocks and then filling the small blocks with zero.

[0168] Step 905 : Using M 2 ×N 2 sub-blocks as storage units, store the data of the small blocks corresponding to each sub-matrix in a specified order.

[0169] Specifically, step 905 includes storing the element value array and subscript data of each submatrix, wherein, when storing the element value array and the index array, the M2×N2 sub-block is used as the storage unit, and the small block data corresponding to each stored element value array and the index array is stored in a specified order.

[0170] This embodiment provides multiple ways to store small blocks:

[0171] Storage method 1:

[0172] Taking the element value array shown in FIG14 as an example, this embodiment can select small blocks in row order. For example, in one embodiment, for each small block included in multiple element value arrays belonging to the same sparse matrix, the sub-blocks are selected sequentially according to the row order between the small blocks. Optionally, small blocks with all zero elements can be eliminated, that is, small blocks with all zero elements are not stored. This will not be repeated below. As shown in FIG15, the order of selecting small blocks can be: tile00, tile01, tile02, tile10, tile11, tile12, tile13, tile20, tile21, tile22, tile30, tile31, tile40, ..., tile70, tile80, tile81, tile82, tile90, tile91, ..., tile121.

[0173] For each selected small block, the data of each small block is stored sequentially. For example, according to the selection order of the small blocks mentioned above, for tile00, the elements (0,0), (0,1), (1,0), and (1,1) of tile00 are stored sequentially. Then, the elements (0,0), (0,1), (1,0), and (1,1) of tile01 are stored sequentially. Then, the elements (0,0), (0,1), (1,0), and (1,1) of tile02 are stored sequentially. Then, the elements (0,0), (0,1), (1,0), and (1,1) of tile10 are stored sequentially, and so on.

[0174] In another embodiment, a submatrix is ​​used as an object, and the small blocks corresponding to each submatrix are stored sequentially in row order. For example, in conjunction with Figure 15, the small blocks corresponding to submatrix 1 are stored first, then the small blocks corresponding to submatrix 2, then the small blocks corresponding to submatrix 3, and finally the small blocks corresponding to submatrix 4. The small blocks within each submatrix can be stored sequentially or in parallel. In short, the small blocks within each submatrix are stored continuously, wherein the multiple small blocks corresponding to each submatrix can be selected in row order. Specifically, as shown in Figure 15, the order of selection of the small blocks can be: tile00, tile01, tile10, tile11, ..., tile70, tile02, tile12, tile13, tile22, tile80, tile81, tile90, ..., tile120, tile121, tile82, ..., tile91. Similarly, for each selected small block, the value of the element of each small block is stored sequentially. Please refer to the relevant introduction above and will not be repeated here.

[0175] The small blocks corresponding to the index array are stored in the above manner. For ease of explanation, the small blocks corresponding to the element value data are recorded as the second sub-block, and the small blocks corresponding to the index array are recorded as the third sub-block.

[0176] In the present application, the multiple second sub-blocks corresponding to a sub-matrix have a mapping relationship with the multiple third sub-blocks, and in the mapping relationship, the multiple second sub-blocks corresponding to the sub-matrix have a one-to-one correspondence with the multiple third sub-blocks. The one-to-one correspondence means that two sub-blocks at the same position correspond to each other. For example, based on the sub-block division, the second sub-block at the 0th row and 0th column in the element value array of sub-matrix 1 (such as tile00 in Figure 14) corresponds to the third sub-block at the 0th row and 0th column in the index array of sub-matrix 1 (not shown in Figure 14, such as tile00'), the second sub-block at the 0th row and 1st column in the element value array of sub-matrix 1 (such as tile01) corresponds to the third sub-block at the 0th row and 1st column in the block array of sub-matrix 1 (such as tile01'), the second sub-block at the 1st row and 0th column in the element value array (such as tile10) corresponds to the third sub-block at the 1st row and 0th column in the block array of sub-matrix 1 (such as tile10'), and so on.

[0177] Furthermore, the embodiment of the present application provides two methods. Method 1: The second sub-block and the third sub-block are stored independently. For example, multiple second sub-blocks are stored in a storage space A, and multiple third sub-blocks are stored in a storage space B. Based on the order of selecting the second sub-blocks above, the order of arrangement of the multiple second sub-blocks in storage space A can be: tile00, tile01, tile02, tile10, tile11, tile12, tile13, tile20, tile21, tile22, tile30, tile31, tile40, ..., tile70, tile80, tile81, tile82, tile90, tile91, ..., tile121. The selection order of the third sub-blocks can be the same as the selection order of the second sub-blocks. For example, in storage space B, the arrangement order of multiple third sub-blocks can be: tile00', tile01', tile02', tile10', tile11', tile12', tile13', tile20', tile21', tile22', tile30', tile31', tile40', ..., tile70', tile80', tile81', tile82', tile90', tile91', ..., tile121'.

[0178] Method 2: The second sub-block and the third sub-block are stored alternately. For example, a second sub-block and the third sub-block corresponding to the second sub-block are used as a storage object. The order of selecting the second sub-block mentioned above can be referred to, and the selected second sub-block and the third sub-block corresponding to the second sub-block can be stored in sequence. For example, one storage order can be: tile00, tile00', tile01, tile01', tile02, tile02', tile10, tile10', tile11, tile11', tile12, tile12', and so on.

[0179] Specifically, taking the computing device 800 as an example, the storage location of the above data can be the memory 802, or a storage device outside the computing device 800, or part of the data can be stored in the memory 802, and the rest of the data can be stored in a storage device outside the computing device 800, without any specific limitation.

[0180] In this embodiment of the present application, based on the second sub-block and the third sub-block corresponding to the second sub-block, the displacement value pair of the sub-matrix and the index vector of the sub-matrix, the value of the corresponding element in the original matrix and the original subscript of the element can be restored.

[0181] Storage method 2:

[0182] Still taking the element value array shown in FIG14 as an example, this embodiment can select the second sub-block according to the column order. For example, in one embodiment, for each small block included in multiple element value arrays belonging to the same sparse matrix, the sub-blocks are selected sequentially according to the column order between the small blocks. Optionally, small blocks with all zero elements can be eliminated, that is, small blocks with all zero elements are not stored. This will not be repeated below. As shown in FIG16, the order of selecting small blocks can be: tile00, tile10, tile20, tile30, tile40, tile50, tile60, tile70, tile80, tile90, tile100, tile110, tile120, tile01, tile11, tile21, tile31, ..., tile81, tile91, tile101, ..., tile121, tile02, tile12, tile22, tile82, tile91, tile13, tile83, tile84.

[0183] Similarly, in another embodiment, a submatrix is ​​used as an object, and the small blocks corresponding to each submatrix are sequentially stored in column order. Please refer to the previous description and will not be repeated here. Regardless of the selection method, for each selected small block, the data of each small block is sequentially stored. Please refer to the previous description and will not be repeated here.

[0184] The blocks corresponding to the index array are stored sequentially in the above manner. The blocks corresponding to the element value array and the blocks corresponding to the index array can be stored serially or in parallel.

[0185] Through the above design, the original matrix is ​​cut into blocks, and each sub-matrix obtained after the block cutting is then compressed to obtain compressed data of each sub-matrix. The element value array and index data in the compressed data are further cut into smaller blocks to obtain multiple second sub-blocks. The multiple second sub-blocks are stored in the second sub-block as a storage unit. In this application, the first sub-block is cut into multiple second sub-blocks, and the second sub-blocks with all zeros can be completely deleted. In this way, it can adapt to sparse matrices with arbitrary distribution and maintain a high compression rate under any sparse matrix.

[0186] In addition, the present application can be used to cut the data into small blocks in accordance with the data bit width of a dedicated hardware accelerator (such as a vector processor or a matrix processor). For example, if the data bit width of the hardware accelerator is n, the first sub-block can be cut into small blocks according to the size of n×n sub-blocks, and the cut small blocks (i.e., second sub-blocks) are used as storage units. The blocks are then stored in the row direction or the column direction to facilitate memory access. A single data access can read at least one complete second sub-block, reducing the number of memory accesses during sparse matrix calculations. Since all rows and columns of the second sub-block are consistent in length, it can provide a data access method that is more friendly to vector processors or matrix processors. It can achieve better locality for row-order or column-order access, and can be more efficiently adapted to dedicated hardware accelerators, which is conducive to improving the parallelism of sparse matrix calculations. On the one hand, a single memory access can read multiple complete second sub-blocks, and these multiple second sub-blocks can be calculated in parallel by multiple dedicated hardware accelerators, which can improve multi-core parallelism. On the other hand, since a second sub-block can provide a dedicated hardware accelerator with sufficient data volume, it is more suitable for a dedicated hardware accelerator to accelerate calculations, which is beneficial to improving the intra-core parallelism of a single dedicated accelerator. For example, taking an nxn small block as a unit, such as n=16, that is, the small block size is 16x16, for a sparse matrix sparse matrix multiplication operation, assuming a 16x16 second sub-block A×16x16 second sub-block B, the present application can realize the reading of 16 groups of vectors of the second sub-block A and 16 groups of vectors of the second sub-block B through a vector instruction, and the vectors are aligned after being read out. The dedicated hardware accelerator can complete 256 16×1 vector inner product calculations in one shot. In this way, the dedicated hardware accelerator can reach a parallelism of 256, and the performance can be greatly improved.

[0187] In summary, the storage method provided in the embodiments of the present application can improve data locality, increase data compression rate, and enhance the parallelism of matrix calculations.

[0188] It is worth noting that the above is introduced using sparse matrices as an example. The original matrix in the embodiment of the present application can also be a dense matrix, that is, the dense matrix is ​​also applicable to the data format and storage method provided in this embodiment. In this way, the format can be unified and the calculation between sparse matrices and dense matrices can be accelerated, such as the multiplication of sparse matrices and dense vectors, sparse matrices and sparse vectors, sparse matrices and dense matrices, and sparse matrices and sparse matrices. It can widely improve the matrix compression rate, matrix operation parallelism and data locality in various scenarios.

[0189] Based on the same inventive concept as the method embodiment, the present embodiment further provides a matrix storage device for executing the method of the method embodiment of FIG. 9 . As shown in FIG. 17 , device 1700 includes an acquisition module 1701, a processing module 1702, and a storage module 1703. Specifically, in device 1700, each module is connected via a communication path. In one application scenario, memory 802 stores executable program code, and processor 801 executes the executable program code to implement the functions of acquisition module 1701, processing module 1702, and storage module 1703, respectively, thereby implementing the matrix storage method provided in this embodiment.

[0190] An acquisition module 1701 is used to obtain multiple first sub-blocks corresponding to the original matrix. The multiple first sub-blocks are obtained by slicing the original matrix into sub-blocks of size M1×N1 and compressing the non-zero elements in each sub-matrix obtained after slicing in a specified direction, where M1 and N1 are positive integers. For details, please refer to the description of steps 901 to 903 in the method embodiment of Figure 9, which will not be repeated here.

[0191] Processing module 1702 is used to divide each first sub-block into sub-blocks of size M2×N2 to obtain multiple second sub-blocks, where M2 is a positive integer not greater than M1, and N2 is a positive integer not greater than N1; for details, please refer to the description of step 904 in the method embodiment of Figure 9, which will not be repeated here.

[0192] The storage module 1703 is configured to sequentially store the values ​​of the elements in each second sub-block. Detailed descriptions can be found in the description of step 905 in the method embodiment of FIG9 , which will not be repeated here.

[0193] In a possible implementation, the designated direction is a row direction;

[0194] The acquisition module 1701 is further used to obtain subscript data for each first sub-block, the subscript data being used to indicate the position of each element in the first sub-block in the original matrix; wherein the subscript data for each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block; wherein the displacement value pair includes a first displacement value and a second displacement value, the first displacement value being used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value being used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix being equal to the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset; and the value of any element in the index vector being used to indicate the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset.

[0195] In a possible implementation, the designated direction is a column direction;

[0196] Acquisition module 1701 is further configured to generate subscript data for each first sub-block, the subscript data being used to indicate the position of each element in the first sub-block in the original matrix; wherein the subscript data for each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block. The displacement value pair includes a first displacement value and a second displacement value, the first displacement value being used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value being used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix being equal to the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset; and the value of any element in the index vector being used to indicate the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset.

[0197] In one possible implementation, the processing module 1702 is further used to: cut the index matrix corresponding to each first sub-block into blocks of size M2×N2 to obtain multiple third sub-blocks; the storage module 1703 is further used to: sequentially store the values ​​of the elements in each third sub-block.

[0198] In one possible implementation, for each first sub-block, multiple second sub-blocks obtained based on the first sub-block correspond one-to-one to multiple third sub-blocks obtained based on the index matrix corresponding to the first sub-block; the order of storing the third sub-blocks is the same as the order of storing the second sub-blocks.

[0199] In one possible implementation, when sequentially storing the values ​​of elements within each second sub-block, the storage module 1703 is specifically used to: select each second sub-block by row or by column, and store the values ​​of the elements within each selected second sub-block by row, or store the values ​​of the elements within each selected second sub-block by column.

[0200] In a possible implementation, each stored second sub-block includes at least one non-zero element; before sequentially storing the values ​​of the elements in each second sub-block, the storage module 1703 is further configured to: eliminate second sub-blocks including all zero elements.

[0201] The present application also provides a computer program product including instructions. The computer program product may be software or a program product including instructions that can be run on a computing device or stored on any available medium. When the computer program product is run on at least one computing device, it causes the at least one computing device to perform the method of the embodiment of FIG. 9 , as described above, and will not be repeated here.

[0202] 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 containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive). The computer-readable storage medium includes instructions that instruct the computing device to execute the method executed in the embodiment of FIG. 9 above. See the relevant description above, which will not be repeated here.

[0203] Optionally, the computer-executable instructions in the embodiments of the present application may also be referred to as application code, which is not specifically limited in the embodiments of the present application.

[0204] Those skilled in the art will understand that the various numerical numbers such as the first and second involved in this application are only for the convenience of description and are not used to limit the scope of the embodiments of the present application, and also indicate the order of precedence. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one" refers to one or more. At least two refers to two or more. "At least one", "any one" or similar expressions refer to any combination of these items, including any combination of single items (individuals) or plural items (individuals). For example, at least one item (individual, kind) of a, b, or c can represent: a, b, c, ab, ac, bc or abc, where a, b, c can be single or multiple. "Multiple" refers to two or more, and other quantifiers are similar. Furthermore, when an element appears in the singular form “a,” “an,” and “the,” it does not mean “one or only one,” but rather “one or more than one,” unless the context clearly dictates otherwise. For example, “a device” means one or more of the devices.

[0205] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website, computer, server or data center to another website, computer, server or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. 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 drive (SSD)).

[0206] The various illustrative logic units and circuits described in the embodiments of the present application can be implemented or operated by a general-purpose processor, a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic, a discrete hardware component, or the design of any combination thereof. The general-purpose processor can be a microprocessor, alternatively, the general-purpose processor can also be any traditional processor, controller, microcontroller or state machine. The processor can also be implemented by a combination of computing devices, such as a digital signal processor and a microprocessor, a plurality of microprocessors, one or more microprocessors in conjunction with a digital signal processor core, or any other similar configuration to implement.

[0207] The steps of the methods or algorithms described in the embodiments of the present application can be directly embedded in hardware, software units executed by a processor, or a combination of the two. The software units can be stored in RAM memory, flash memory, ROM memory, EPROM memory, EEPROM memory, registers, hard disk, removable disk, CD-ROM, or other storage media in any form known in the art. Exemplarily, the storage medium can be connected to the processor so that the processor can read information from the storage medium and write information to the storage medium. Alternatively, the storage medium can also be integrated into the processor. The processor and storage medium can be arranged in an ASIC.

[0208] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.

[0209] Although the present application has been described with reference to specific features and embodiments thereof, it is apparent that various modifications and combinations may be made thereto without departing from the spirit and scope of the present application. Accordingly, this specification and the drawings are merely illustrative of the present application as defined by the appended claims and are deemed to cover any and all modifications, variations, combinations or equivalents within the scope of the present application. Obviously, those skilled in the art may make various modifications and variations to the present application without departing from the scope of the present application. Thus, the present application is intended to include such modifications and variations if they fall within the scope of the claims of the present application and their equivalents.

Claims

1. A matrix storage method, It is characterized in that include: Get a plurality of first sub-blocks corresponding to the original matrix, wherein the plurality of first sub-blocks are a sum of the original matrix with a scale of M 1 ×N 1 The non-zero elements in each sub-matrix obtained after the slicing are compressed in the specified direction, where M 1 、N 1 is a positive integer; For each first sub-block with a size of M 2 ×N 2 The blocks are cut into units of M to obtain multiple second sub-blocks, where M 2 Not greater than M 1 A positive integer, N 2 is not greater than N 1 A positive integer of ; The second sub-block is used as a storage unit to store the value of the element in each second sub-block.

2. The method according to claim 1, It is characterized in that The specified direction is a row direction; The method further comprises: Obtaining subscript data of each first sub-block, where the subscript data is used to indicate a position of each element in the first sub-block in the original matrix; The subscript data of each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block; the displacement value pair includes a first displacement value and a second displacement value, the first displacement value is used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value is used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix is ​​equal to the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset; the value of any element in the index vector is used to indicate the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset.

3. The method according to claim 1, It is characterized in that The specified direction is a column direction; The method further comprises: Obtaining subscript data of each first sub-block, where the subscript data is used to indicate a position of each element in the first sub-block in the original matrix; The subscript data of each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block; the displacement value pair includes a first displacement value and a second displacement value, the first displacement value is used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value is used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix is ​​equal to the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset; the value of any element in the index vector is used to indicate the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset.

4. The method according to claim 2 or 3, It is characterized in that The method further comprises: The index matrix corresponding to each first sub-block is M in size 2 ×N 2 The blocks are cut into units of sub-blocks to obtain multiple third sub-blocks; The values ​​of the elements within each third sub-block are stored sequentially.

5. The method according to claim 4, It is characterized in that For each first sub-block, a plurality of second sub-blocks obtained based on the first sub-block correspond one-to-one to a plurality of third sub-blocks obtained based on the index matrix corresponding to the first sub-block; The storage order of the third sub-block is the same as the storage order of the second sub-block.

6. The method according to any one of claims 1 to 5, It is characterized in that The values ​​of the elements in each second sub-block are stored, including: Each second sub-block is selected by row or by column, and the values ​​of the elements in each selected second sub-block are stored by row, or the values ​​of the elements in each selected second sub-block are stored by column.

7. The method according to any one of claims 1 to 6, It is characterized in that Each stored second sub-block includes at least one non-zero element.

8. A matrix storage device, It is characterized in that The matrix calculation device includes an acquisition module, a processing module, and a storage module: The acquisition module is used to acquire a plurality of first sub-blocks corresponding to the original matrix, wherein the plurality of first sub-blocks are obtained by converting the original matrix into a matrix with a scale of M. 1 ×N 1 The non-zero elements in each sub-matrix obtained after the slicing are compressed in the specified direction, where M 1 、N 1 is a positive integer; The processing module is used to process each first sub-block with a scale of M 2 ×N 2 The blocks are cut into units of M to obtain multiple second sub-blocks, where M 2 Not greater than M 1 A positive integer, N 2 is not greater than N 1 A positive integer of ; The storage module is used to sequentially store the values ​​of the elements in each second sub-block.

9. The device as claimed in claim 8, It is characterized in that The specified direction is a row direction; The acquisition module is further used to acquire subscript data of each first sub-block, where the subscript data is used to indicate the position of each element in the first sub-block in the original matrix; The subscript data of each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block; the displacement value pair includes a first displacement value and a second displacement value, the first displacement value is used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value is used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix is ​​equal to the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset; the value of any element in the index vector is used to indicate the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset.

10. The device according to claim 8, It is characterized in that The specified direction is a column direction; The acquisition module is further used to generate subscript data for each first sub-block, where the subscript data is used to indicate the position of each element in the first sub-block in the original matrix; The subscript data of each first sub-block includes a displacement value pair, an index matrix, and an index vector corresponding to the first sub-block; the displacement value pair includes a first displacement value and a second displacement value, the first displacement value is used to indicate a first offset of the first row of the first sub-block relative to the starting row of the original matrix, and the second displacement value is used to indicate a second offset of the first column of the first sub-block relative to the starting column of the original matrix; the value of any element in the index matrix is ​​equal to the difference between the row number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the first offset; the value of any element in the index vector is used to indicate the difference between the column number of the element corresponding to the element in the original matrix corresponding to the first sub-block and the second offset.

11. The device according to claim 8 or 9, It is characterized in that The processing module is further used to: process the index matrix corresponding to each first sub-block with a scale of M 2 ×N 2 The blocks are cut into units of sub-blocks to obtain multiple third sub-blocks; The storage module is further used for sequentially storing values ​​of elements in each third sub-block.

12. The device according to claim 11, It is characterized in that For each first sub-block, multiple second sub-blocks obtained based on the first sub-block correspond one-to-one to multiple third sub-blocks obtained based on the index matrix corresponding to the first sub-block; the order of storing the third sub-blocks is the same as the order of storing the second sub-blocks.

13. The device according to any one of claims 8 to 12, It is characterized in that When sequentially storing the values ​​of the elements in each second sub-block, the storage module is specifically used to: select each second sub-block by row or by column, and store the values ​​of the elements in each selected second sub-block by row, or store the values ​​of the elements in each selected second sub-block by column.

14. The device according to any one of claims 8 to 13, It is characterized in that Each stored second sub-block includes at least one non-zero element.

15. A computing device, It is characterized in that The device includes a processor and a memory; The memory is used to store computer program instructions; The processor executes and calls the computer program instructions in the memory to perform the method according to any one of claims 1 to 7.

16. A computer-readable storage medium, It is characterized in that When the computer-readable storage medium is executed by a storage device, the storage device executes the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Storage method of sparse matrix on SIMD multi-core processor with multi-level cache

    CN104636273A

  • Data processing method and device and hardware accelerator

    CN110929854A

  • Methods and systems for optimizing sparse matrix vector multiplication of a high-performance computing framework

    CN111428192A

  • Sparse matrix storage method, vector calculation method and electronic equipment

    CN113721982A

  • Performing matrix value indication

    US20220366007A1

Cited By

  • Array element processing method and device, storage medium and electronic equipment

    CN119760180A

  • Matrix storage device and method and matrix operation device and method

    CN120179976A