Sparse matrix vector multiplication space expression method
By unified expression of the sparse storage format of sparse matrices and kernel optimization, combined with reinforcement learning and Monte Carlo tree search algorithms, the sparse matrix vector multiplication program design is optimized, solving the problem of limited performance improvement of sparse matrix vector multiplication, and achieving remarkable performance. promote.
Patent Information
- Application Number
- CN202410876221.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-10-01
AI Technical Summary
In the existing sparse matrix vector multiplication program design, the sparse storage format of the sparse matrix and the kernel optimization are separated, resulting in limited performance improvement. The relationship between the sparse matrix mode and the kernel optimization design cannot be fully considered, and higher performance is ignored. opportunity.
By constructing the state space of the sparse matrix, using hierarchical mapping and the absolute and relative positions of non-zero elements, the sparse storage format and kernel optimization process of the sparse matrix are uniformly expressed, and the reinforcement learning module is used for automatic tuning, combined with the Monte Carlo tree search algorithm. Programming for optimized sparse matrix-vector multiplication.
The performance of sparse matrix vector multiplication is significantly improved, with an average GFLOPS performance improvement of 2.29 times. SpMV improves GFLOPS performance indicators by 3.66 times, achieving more efficient programming.
Smart Images

Figure CN118733945A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computing technology, in particular to the field of sparse matrix-vector multiplication, and more particularly to a method for spatially expressing sparse matrix-vector multiplication. Background Art
[0002] In a matrix, if the number of elements with a value of 0 (hereinafter referred to as zero elements) is far greater than the number of non-zero elements (hereinafter referred to as non-zero elements), and the distribution of non-zero elements is irregular, the matrix is called a sparse matrix. In some literature, a matrix with a majority of zero elements is called a sparse matrix; on the contrary, if the number of non-zero elements is the majority, the matrix is called a dense matrix.
[0003] Sparse Matrix-Vector Multiplication (SpMV) is a performance-critical operation in many traditional numerical simulation applications as well as in the emerging fields of big data and artificial intelligence. Specifically, it is the fundamental core across a variety of algorithms, including large-scale sparse linear solvers, graph analysis, and neural networks.
[0004] In recent years, there have been a lot of works aimed at advancing the design and implementation of sparse matrix-vector multiplication. The sparse nature of sparse matrices means that a wide range of sparse matrices and a huge combinatorial space of different optimization options can improve the performance of sparse matrix-vector multiplication.
[0005] Therefore, the key efforts of these works can be divided into two parts:
[0006] (1) It involves the storage format of sparse matrices to adapt to the pattern of non-zero elements or the underlying hardware (i.e., vector units). To date, more than a hundred formats have been proposed, ranging from basic formats (such as COO, CSR, DIA) to complex design formats (such as HYB, CSR5, yaSpMV).
[0007] (2) Involves kernel implementation and code scheduling, including parallel allocation between computational units and non-zero elements, and reduction algorithms in specific hardware (e.g., CSR-scalar, CSR-vector, ACSR, CSR-Adaptive).
[0008] The limitations of many current works in the expression of sparse matrix-vector multiplication space still come from two aspects: separate design expression and fixed design mapping.
[0009] In terms of the expression space of programming, for sparse matrix-vector multiplication programs, the spatial expression of current work is still limited to separately expressing the sparsity of sparse matrices and kernel optimization strategies. Current related work focuses on the storage of sparse formats (data layout in memory), kernel implementation (data calculation method) and configuration parameters (quantization details of format and kernel). However, almost all recent work formalizes sparsity patterns and kernel implementation independently. Therefore, it is difficult for existing expressions to capture the relationship between sparse matrix patterns and kernel optimization design, which hinders sparse matrix-vector multiplication program design from becoming more effective.
[0010] In terms of design mapping for programming, for sparse matrix-vector multiplication programming, the current mapping in the space is still fixed and limited by human experience. Spatial mapping requires building a mapping from each point expressed in the design space to an appropriate optimization strategy. Design mapping is essentially a mechanism for generating suitable design methods by integrating sparse storage formats with candidate kernel implementations. Notable successes in this area include CSR-Adaptive, CSR5, and yaSpMV. However, current efforts typically rely on representative samples or predefined templates to represent these mappings. This fact limits the mapping to human experience and may ignore opportunities for higher performance.
[0011] In general, in the existing design process of sparse matrix-vector multiplication, the sparse storage format of the sparse matrix and the kernel optimization are separated, and the decisions of all stages are analyzed only based on the original data form of the sparse matrix. Since the factors considered are not comprehensive enough, the performance of the designed program needs to be improved.
[0012] It should be noted that this background technology is only used to introduce the relevant information of the present invention to help understand the technical solution of the present invention, but it does not mean that the relevant information is necessarily the prior art. The relevant information is submitted and disclosed together with the present invention solution. If there is no evidence that the relevant information has been disclosed before the application date of the present invention, the relevant information shall not be regarded as the prior art. Summary of the invention
[0013] Therefore, the purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a method for expressing sparse matrix-vector multiplication space.
[0014] The objective of the present invention is achieved through the following technical solutions:
[0015] According to the first aspect of the present invention, a method for expressing a sparse matrix-vector multiplication space is provided, comprising: constructing a state space of a sparse matrix in a predetermined format, comprising: obtaining a sparse matrix required in a sparse matrix-vector multiplication to be calculated; in each of a plurality of decision stages, determining a stage expression corresponding to the sparse matrix, which comprises a hierarchical mapping, an absolute position and a relative position of non-zero elements, and the hierarchical mapping indicates the allocation information of the non-zero elements of the sparse matrix in the computing hardware. With the help of the stage expression proposed by the method of the present invention, the sparse storage format of the sparse matrix and the kernel optimization process are uniformly expressed, so that the influence of the previous kernel optimization process on the sparse storage format (such as: the absolute position of the non-zero elements, the relative position, and the allocation information of the non-zero elements in the computing hardware) is considered in the subsequent kernel optimization process, thereby helping to improve the performance of the designed sparse matrix-vector multiplication program.
[0016] Optionally, the hierarchical mapping includes: a memory mapping for indicating the memory index corresponding to each row of the sparse matrix non-zero element; a block mapping for indicating the memory index of the non-zero element allocated to each thread block; and a thread mapping for indicating the memory index of the non-zero element allocated to each thread. The hierarchical mapping can reflect the allocation of each non-zero element in the hierarchical computing resources of the computing hardware (such as GPU), and describes the hierarchical organization of the non-zero elements. When the hierarchical mapping is included in the stage expression, it can simultaneously express the relationship between the sparse matrix format and the program kernel optimization at different stages, thereby improving performance. Compared with the four most advanced formats (ACSR / CSR-Adaptive / CSR5 / cuSPARSE), the performance achieved by the stage expression proposed by the method of the present invention is 2.29 times faster in GFLOPS performance on average.
[0017] Optionally, the method further includes: constructing a strategy space for sparse matrix-vector multiplication, including: setting a strategy space corresponding to each stage to limit the set of actions that can be taken at that stage, and the actions are used to modify the stage expression of the previous stage. By modifying the stage expression through the actions taken at each stage, the decision of the subsequent stage can take into account the impact of the decision of the previous stage, avoiding analyzing the decision of all stages only based on the original data form of the sparse matrix, considering more comprehensive factors, and effectively improving the performance of the program designed accordingly.
[0018] Optionally, the method also includes: at each stage, using the stage expression of the previous stage and the strategy space of the current stage to determine the action taken in the current stage; according to the action taken in the current stage, modifying the stage expression of the previous stage to obtain the stage expression of the current stage.
[0019] Optionally, the deployed reinforcement learning module is used to determine the action taken in the current stage, including: using a neural network to extract stage features from the stage expression of the previous stage, and determining the strategy and value of the current stage based on the stage features; obtaining a search tree maintained for the sparse matrix based on the Monte Carlo tree search algorithm, and determining the action taken in the current stage from the strategy space based on the search tree, the strategy and value of the current stage. On the basis of a unified stage expression, combined with the application of reinforcement learning, the method of the present invention achieves the effect of automatic tuning; compared with the latest implementation of the current automatic tuning framework, SpMV improves the GFLOPS performance index by up to 3.66 times (1.27 times on average).
[0020] Optionally, the strategy space for sparse matrix-vector multiplication includes: a strategy space for the first stage, including whether to reorder the rows of the sparse matrix, and if so, reordering the rows of the sparse matrix from large to small according to the number of non-zero elements contained; a strategy space for the second stage, including compression and filling, to selectively compress or fill the sparse matrix obtained in the previous stage, compression means using the compression format to ignore the non-zero elements of the sparse matrix, and filling means treating all zero elements of the sparse matrix as non-zero elements; a strategy space for the third stage, including multiple allocation granularities set when allocating the non-zero elements of the thread blocks by row for the sparse matrix obtained in the previous stage; a strategy space for the fourth stage, including multiple allocation granularities set when allocating the non-zero elements allocated in each thread block by row for the threads and multiple allocation granularities set when allocating the threads by the number of non-zero elements; a strategy space for the fifth stage, including multiple preset reduction methods, and the reduction method is used to limit the way of summing the results of multiplying the non-zero elements in the sparse matrix and the elements in the vector completed by the relevant threads.
[0021] Optionally, the strategy space for sparse matrix-vector multiplication includes: a strategy space for the first stage, including compression and filling, to selectively compress or fill the sparse matrix, compression means using the compression format to ignore the non-zero elements of the sparse matrix, and filling means treating all zero elements of the sparse matrix as non-zero elements; a strategy space for the second stage, including multiple allocation granularities set when allocating non-zero elements by row to thread blocks for the sparse matrix obtained in the previous stage; a strategy space for the third stage, including multiple allocation granularities set when allocating non-zero elements allocated in each thread block by row to threads; a strategy space for the fourth stage, including multiple preset reduction methods, the reduction method is used to limit the method for summing the results of multiplying the non-zero elements in the sparse matrix and the elements in the vector completed by the relevant threads.
[0022] According to a second aspect of the present invention, a programming system for sparse matrix-vector multiplication based on reinforcement learning is provided, which is used to build programs on computing hardware with a hierarchical structure, and the system includes: a sparse matrix expression module, which is configured to: at each of multiple decision stages, determine the stage expression corresponding to the sparse matrix according to the method of the first aspect; a reinforcement learning module, which is configured to: at the i-th stage, use a neural network to extract stage features from the stage expression of the i-1th stage, and determine the strategy and prediction value of the i-th stage according to the stage features; obtain a search tree maintained for the sparse matrix based on a Monte Carlo tree search algorithm, and determine the action taken in the i-th stage based on the search tree, the strategy and the prediction value; and construct a program code that guides the sparse matrix-vector multiplication to complete the calculation on the computing hardware according to the actions taken in all stages, wherein i>0.
[0023] According to a third aspect of the present invention, there is provided an electronic device comprising: one or more processors; and a memory, wherein the memory is used to store executable instructions; the one or more processors are configured to implement the steps of the method described in the first aspect by executing the executable instructions. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:
[0025] Figure 1 A schematic diagram of system modules of a programming system for sparse matrix-vector multiplication based on reinforcement learning according to an embodiment of the present invention;
[0026] Figure 2 A schematic diagram of a reinforcement learning process of a system according to an embodiment of the present invention;
[0027] Figure 3 A simplified schematic diagram of a stage expression and changes in the stage expression with actions taken in each stage according to an embodiment of the present invention;
[0028] Figure 4 A schematic diagram of the relationship between the stage expression and the non-zero element allocation according to an embodiment of the present invention;
[0029] Figure 5 A schematic diagram of the structure of a neural network model according to an embodiment of the present invention;
[0030] Figure 6 is another structural schematic diagram of a neural network model according to an embodiment of the present invention;
[0031] Figure 7 A schematic diagram of the working principle of a perceptron according to an embodiment of the present invention;
[0032] Figure 8 A schematic diagram of the working principle of the pooling layer according to an embodiment of the present invention;
[0033] Fig. 9 A schematic diagram of performance feedback for training and testing of Go games according to the prior art and performance feedback for training and testing related to the programming of sparse matrix-vector multiplication according to an embodiment of the present invention;
[0034] Fig.10 FIG. 4 is a schematic diagram of storage areas with multiple priorities according to an embodiment of the present invention. DETAILED DESCRIPTION
[0035] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below through specific embodiments in conjunction with the accompanying drawings. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0036] As mentioned in the background technology section, in the existing design process of sparse matrix-vector multiplication, the sparse storage format of the sparse matrix and kernel optimization are separated. The strategies of all stages are analyzed only based on the original data format of the sparse matrix. Since the factors considered are not comprehensive enough, the performance of the designed program needs to be improved.
[0037] For the format, it is necessary to consider the storage of non-zero element hierarchical indexes and the division of non-zero elements in the matrix. For program optimization strategies, it is necessary to consider the distribution relationship between multiple levels of computing units (blocks / threads) in the GPU and non-zero elements. In addition, it is also necessary to consider runtime parameters during parallel computing and the means of non-zero element reduction.
[0038] For program optimization strategies, since sparse matrices may be allocated to different levels of computing units in the GPU at some stages, the decision basis of the next stage changes, and therefore, such changes should be considered. Moreover, in order to better express such decision basis in a unified manner, the present invention adopts a unified format for expression at different stages, that is, each stage expression includes a hierarchical mapping for indicating the allocation information of the sparse matrix in the hierarchical computing resources of the computing hardware at the current stage, and the absolute position and relative position of the non-zero element.
[0039] Therefore, with the help of the stage expression proposed by the method of the present invention, the sparse storage format of the sparse matrix and the kernel optimization process can be uniformly expressed, so that in the subsequent kernel optimization process, the impact of the previous kernel optimization process on the sparse storage format (such as: the absolute position of non-zero elements, relative position, and distribution information of non-zero elements in computing hardware) can be considered, thereby helping to improve the performance of the designed sparse matrix-vector multiplication program.
[0040] With the help of the stage expression in a unified format, the present invention can use the reinforcement learning module to extract the stage features from the stage expression of the i-1th stage, determine the strategy and prediction value of the i-th stage according to the stage features, and guide the Monte Carlo tree search algorithm to determine the action taken in the i-th stage through the strategy and prediction value; finally, according to the actions taken in all stages, a program code guiding the sparse matrix vector multiplication to complete the calculation on the computing hardware can be constructed. Thus, with the help of the stage expression in a unified format, the present invention can be based on the sparse matrix vector multiplication programming system of reinforcement learning (RL), and effectively combine and search for high-performance sparse matrix vector multiplication programs by using Monte Carlo tree search, so as to better optimize the calculation process of sparse matrix vector multiplication on computing hardware.
[0041] According to one embodiment of the present invention, see Figure 1 and Figure 2 , a program design system for sparse matrix vector multiplication based on reinforcement learning is provided, which is used to construct the program for computing hardware with a hierarchical structure, and the system includes: a sparse matrix expression module and a reinforcement learning module. The sparse matrix expression module is configured to: determine the stage expression corresponding to the sparse matrix in each of the multiple decision stages, and each stage expression includes a hierarchical mapping for indicating the allocation of information of the sparse matrix in the hierarchical computing resources of the computing hardware at the current stage, and the absolute position and relative position of the non-zero element. The reinforcement learning module is configured to: in the i-th stage, use a neural network to extract the stage feature from the stage expression of the i-1th stage, and determine the strategy and prediction value of the i-th stage according to the stage feature; obtain a search tree maintained for the sparse matrix based on the Monte Carlo tree search algorithm, and determine the action taken in the i-th stage based on the search tree, strategy and prediction value; according to the actions taken in all stages, construct a program code that guides the sparse matrix vector multiplication to complete the calculation on the computing hardware, wherein i>0. By generating the SpMV code, the SpMV program can be obtained.
[0042] To illustrate the purpose of decision-making, an illustrative decision-making stage is given below.
[0043] According to one embodiment of the present invention, the system of the present invention sets a plurality of decision-making stages to gradually determine what actions to take on a sparse matrix so as to efficiently complete the calculation on the computing device. The purpose of the decision at each stage is to determine the action to be taken at this stage. After obtaining the actions taken at each stage stage by stage, a program code can be constructed to guide the sparse matrix vector multiplication to complete the calculation on the computing hardware. Therefore, different implementation methods can be formed according to the number of stages set by the implementer and the strategy space of each stage (i.e., the possible action set of each stage). For ease of understanding, the relationship between the multiple decision stages in a method of expressing the sparse matrix vector multiplication space and the strategy space of the sparse matrix vector multiplication is first given below. The multiple decision stages include: stages 1-5. Among them:
[0044] The first stage is used to predict the strategy and value based on the initial expression. The strategy predicted in the first stage is the probability of reordering the rows of the sparse matrix. The strategy space of the first stage includes whether to reorder the rows of the sparse matrix. If so, the rows of the sparse matrix are reordered from large to small according to the number of non-zero elements they contain.
[0045] The second stage is used to predict the strategy and value according to the stage expression of the first stage. The strategy predicted in the second stage is the probability of compressing and filling the sparse matrix obtained in the first stage. The strategy space is compression and filling. The strategy space of the second stage includes compression and filling to selectively compress or fill the sparse matrix obtained in the previous stage. Compression means that the non-zero elements of the sparse matrix are ignored in the compression format, and filling means that all zero elements of the sparse matrix are regarded as non-zero elements.
[0046] The third stage is used to express prediction strategies and values based on the stage expression of the second stage. The strategy predicted in the third stage is the probability of each allocation granularity (to avoid confusion, it can be called the first allocation granularity) among the multiple allocation granularities set when the sparse matrix obtained in the second stage is allocated by row non-zero elements for thread blocks. The strategy space of the third stage includes multiple allocation granularities set when the sparse matrix obtained in the previous stage is allocated by row non-zero elements for thread blocks. For example: one allocation granularity is to divide one block into 32 rows, and the other allocation granularity is to divide one block into 64 rows.
[0047] The fourth stage is used to express prediction strategies and values based on the stage expressions of the third stage. The strategy predicted in the fourth stage is the probability of each allocation granularity (to avoid confusion, it can be called the second allocation granularity) set when the threads are allocated by row non-zero elements for the data in each thread block and the probability of each allocation granularity (to avoid confusion, it can be called the third allocation granularity) set when the threads are allocated by the number of non-zero elements. The strategy space of the fourth stage includes a set of multiple second allocation granularities and a set of multiple third allocation granularities.
[0048] The fifth stage is used to predict the strategy and value according to the stage expression of the fourth stage. The strategy predicted in the fifth stage is the probability of selecting each reduction method from the preset multiple reduction methods. The strategy space of the fifth stage includes multiple preset reduction methods. The reduction method is used to limit the way to sum the results of multiplying the non-zero elements in the sparse matrix and the elements in the vector completed by the relevant threads.
[0049] The decision items for each stage are shown in Table 1. If a stage involves multiple rows, the stage is to choose one item from the items in the two rows as an action.
[0050] Table 1
[0051]
[0052]
[0053]
[0054] The following will be explained with reference to the stages of the above exemplary implementation. However, as mentioned above, the implementer can actually design different stages (add, delete or change some stage decision matters, such as deleting the above stage 1, that is, not designing reordered nodes, or adding other existing reduction methods, such as thread offset reduction, to the stage 5) and the strategy space of each stage (for example, the multiple allocation granularities of the thread block allocation by row non-zero elements in the stage 3 of Table 1 can also be 16, 32, 64, 128 and 256), so as to form other implementations.
[0055] In order to better illustrate the operation process of the system, the sparse matrix expression module and the reinforcement learning module are explained separately below.
[0056] 1. Sparse Matrix Expression Module
[0057] Since sparse matrix-vector multiplication is a memory-intensive kernel, efficient access to non-zero elements is crucial to achieving high-performance programming. The location of non-zero elements in memory is crucial to designing efficient memory access scheduling. In order to encode all possible intermediate states in sparse matrix-vector multiplication programming on GPU, the present invention proposes a position-based hierarchical representation, namely, a stage expression corresponding to the sparse matrix. The stage expression is dynamically determined at each stage by the following sparse matrix expression module. In a method for expressing a sparse matrix-vector multiplication space, constructing a state space of a sparse matrix according to a predetermined format can be performed by a sparse matrix expression module.
[0058] According to one embodiment of the present invention, see again Figure 2The sparse matrix expression module is configured to: determine the stage expression corresponding to the sparse matrix in each stage of the plurality of decision stages. Figure 3 , each stage expression includes the hierarchical mapping (HM), the absolute position (AP) and the relative position (RP) of the non-zero element, which are used to indicate the allocation information of the sparse matrix in the hierarchical computing resources of the computing hardware at the current stage. The hierarchical mapping includes: memory mapping (ML), block mapping (BM) and thread mapping (TM).
[0059] The memory map ML, the block map BM, the thread map TM, the absolute position AP and the relative position RP are described below respectively.
[0060] The memory map ML is used to indicate the memory index corresponding to each row of the sparse matrix and reflects the memory layout of the non-zero elements.
[0061] The block map BM is used to indicate the memory index of the non-zero element allocated to each thread block;
[0062] The thread map TM is used to indicate the memory index of the non-zero element allocated to each thread.
[0063] The absolute position AP of the non-zero element represents the index of the non-zero metadata partition (eg, matrix row index or computation unit index).
[0064] The relative position RP of the non-zero element represents the local index (eg, column index) of the non-zero element within the specified partition. The local index indicates their corresponding positions in the continuous memory segment.
[0065] The above expressions of different stages may change with the actions taken at each decision stage. Due to the different decisions at each stage, there is a lot of content to be displayed, which is difficult to display efficiently with large-scale sparse examples. Therefore, in order to vividly explain the changes in stage expressions, the following is a Figure 3 The 4×4 sparse matrix in the figure is used to illustrate this. Due to the small overall size, Figure 3 The allocation granularity of stage 3 (corresponding to S3) and stage 4 (corresponding to S4) does not conform to the settings in Table 1 and is only simplified for illustration.
[0066] Schematic representation of stage 0 (ref. Figure 3 Below S1):
[0067] The first stage needs to use the stage expression of the 0th stage (that is, the initial expression of the sparse matrix). The schematic sparse matrix is the 4x4 matrix under the mark S1, where the white grid represents zero elements, the green grid represents non-zero elements, and the numbers in the green grid are the values of the non-zero elements.
[0068] The stage expression of stage 0 is shown below S1, where:
[0069] The value of the block map BM is -1. -1 is the preset default value because the mapping of the non-zero elements of the thread block allocation must be determined in stage 3.
[0070] The value of the thread mapping TM is -1. -1 is the preset default value because the mapping of the non-zero elements allocated by the thread must be determined in stage 4.
[0071] The value of the memory map ML is -1. -1 is the default value because the mapping of non-zero elements in memory (global memory) must be determined after stage 2.
[0072] The absolute positions of non-zero elements AP are 0, 1, 2, 3, 3. The values represent the absolute positions of the five non-zero elements, which are their row numbers in the sparse matrix. The range of row and column numbers is assumed to be 0 to 3.
[0073] The relative positions RP of the non-zero elements are 3, 3, 2, 1, and 3. The values in them represent the relative positions of the 5 non-zero elements, which are the column numbers at this time.
[0074] Illustratively, the first value of AP is 0 and the first value of RP is 3, indicating that the first non-zero element (1) is in the 3rd column of the 0th row of the sparse matrix, and the rest are similar and will not be described in detail.
[0075] Schematic representation of the first stage (refer to Figure 3 Below S2):
[0076] Assuming that the action taken in the first stage is to reorder, the result after reordering from large to small according to the number of non-zero elements in each row is shown in the sparse matrix below S2.
[0077] The stage expression of stage 1 is shown below S2, where:
[0078] The value of the block map BM is -1.
[0079] The thread map TM has a value of -1.
[0080] The value of the memory map ML is -1.
[0081] The absolute positions of non-zero elements AP are 0, 0, 1, 2, and 3. The values represent the absolute positions of the five non-zero elements, which are their row numbers in the sparse matrix. The range of row and column numbers is assumed to be 0 to 3.
[0082] The relative positions RP of the non-zero elements are 3, 3, 2, 1, and 3. The values in them represent the relative positions of the 5 non-zero elements, which are the column numbers at this time.
[0083] It can be seen that the values of AP and RP obtained after the first stage of sorting have changed. This is because the number of non-zero elements in the original third row is the largest (the number is 2), and after sorting it is in the 0th row. The number of non-zero elements in the remaining rows is the same, and they are arranged after it in the original order.
[0084] Indicatively, the first value of AP is 0 and the first value of RP is 1, indicating that the first non-zero element (4) of the reordered sparse matrix is in the first column of the 0th row of the sparse matrix, and the rest are similar and will not be described in detail.
[0085] In addition, if the action taken in stage 1 is not to reorder, the stage expression of stage 1 is consistent with the stage expression of stage 0.
[0086] Schematic representation of the second stage (refer to Figure 3 Below S3):
[0087] Assuming that the action taken in the second stage is compression, the result after compression is shown in the distribution of non-zero elements in the global memory below S3. ML[] represents a certain area of memory. Assume that ML[0] to ML[4] respectively record the four areas in the global memory that store four rows of non-zero elements.
[0088] In the stage expression of stage 2:
[0089] The value of the block map BM is -1.
[0090] The thread map TM has a value of -1.
[0091] The values of memory mapping ML are 0, 2, 3, 4, and 5. At the memory level, memory mapping ML is in the form of an ML array, which stores the memory offset of the first element in each row. Because there are 2 non-zero elements in row 0, 0 means that the memory offset of the non-zero element (4) in row 0 is 0, and 2 means that the memory offset of the first non-zero element (1) in row 2 is 2. This indicates the location of the non-zero elements (4, 5) in row 0 in global memory, and so on.
[0092] The absolute position AP of the non-zero element changes to 0, 0, 1, 2, and 3. The values represent the absolute positions of the five non-zero elements, which are the numbers of the four regions in the memory. The region numbers are assumed to be 0 to 3.
[0093] The relative position RP of the non-zero element changes to 0, 1, 0, 0, 0. The values therein represent the relative positions of the five non-zero elements, which are the offsets in the area where they are located.
[0094] It can be seen that the values of AP and RP obtained after compression in the second stage have changed, which is due to the compression of the sparse matrix.
[0095] Indicatively, the first value of AP is 0 and the first value of RP is 0, indicating that the first non-zero element (4) of the compressed sparse matrix is at the 0th position of the 0th area of the memory, and the rest are similar and will not be described in detail.
[0096] In addition, if the action taken in the second stage is padding instead of compression, all zero elements will be considered as non-zero elements, and the number of non-zero elements will be considered as 4×4=16. Then, in the stage expression of the second stage, ML, AP and RP will each have 16 values to represent the relevant information of 16 non-zero elements.
[0097] Schematic representation of the third stage (refer to Figure 3 S4 below):
[0098] The third stage is the allocation of non-zero elements in the thread blocks by row. The strategy space is originally 32, 64, 128, 256, for example. 32 means that the rows of the sparse matrix are allocated to the thread blocks according to the allocation granularity of 32 rows. The rest is similar and will not be described in detail. However, since the illustrated sparse matrix has only 4 rows, it is assumed here that the action taken is to allocate the rows of the sparse matrix to the thread blocks according to the allocation granularity of 2 rows for schematic illustration. The result after allocation is shown in the distribution of non-zero elements in thread block 0 and thread block 1 below S4.
[0099] In the stage expression of stage 3:
[0100] The values of the block map BM are 0, 3, and 5. BM shows how non-zero elements are allocated to thread blocks. The array BM[0] corresponding to thread block 0 is allocated to the non-zero elements in the first two rows (4, 5, 1), where the offset of the first non-zero element in global memory is 0, and the first value in BM is 0. The array BM[1] corresponding to thread block 1 is allocated to the non-zero elements in the last two rows (2, 3), where the offset of the first non-zero element in global memory is 3, and the first value in BM is 0. The third value in BM is 5, which indicates that the array BM[1] corresponding to thread block 1 has 2 non-zero elements allocated to it (5-3=2), and also indicates the end of BM (5 is greater than the number of non-zero elements 4).
[0101] The thread map TM has a value of -1.
[0102] The values of the memory map ML have not changed and are 0, 2, 3, 4, and 5.
[0103] The absolute position AP of the non-zero element changes to 0, 0, 0, 1, 1. The values represent the absolute positions of the five non-zero elements, which are the numbers of the corresponding thread blocks. The thread block numbers are assumed to be 0 to 1.
[0104] The relative positions RP of the non-zero elements are 0, 1, 2, 0, 0. The values therein represent the relative positions of the five non-zero elements, which are the offsets in the thread blocks in which they are located.
[0105] It can be seen that the values of AP and RP in the third stage have changed again. This is because after the non-zero elements are assigned to the thread blocks, the basis of the absolute and relative positions of the non-zero elements has changed.
[0106] Illustratively, the first value 0 of AP and the first value 0 of RP indicate that the first non-zero element (4) is allocated to thread block 0 and is located at position 0 therein, and the rest are similar and will not be described in detail.
[0107] Schematic representation of the fourth stage (refer to Figure 3 S5 below):
[0108] The strategy of the fourth stage is to allocate the data of the sparse matrix obtained in the third stage in each thread block with the probability of each allocation granularity when the threads are allocated by row non-zero elements and the probability of each allocation granularity when the threads are allocated by the number of non-zero elements. The strategy space is originally X16, X32, X64, X128, Y16, Y32, Y64, Y128, X refers to the allocation of threads by row non-zero elements, Y refers to the allocation of threads by the number of non-zero elements, and 16, 32, etc. represent the corresponding allocation granularity. However, since the illustrated sparse matrix has only 4 rows, the action taken is to allocate threads by row non-zero elements (each thread is allocated only one row) for schematic illustration. The result after allocation is shown in the distribution of non-zero elements in threads 0 to 3 below S5.
[0109] Correspondingly, in the stage expression of stage 4:
[0110] The values of the block map BM remain unchanged and are 0, 3, and 5.
[0111] The value of the thread map TM changes to 0, 2, 3, 4, and 5. It indicates the offset of the first non-zero element allocated by each thread in global memory.
[0112] The values of the memory map ML have not changed and are 0, 2, 3, 4, and 5.
[0113] The absolute position AP of the non-zero element changes to 0, 0, 1, 2, and 3. The values represent the absolute positions of the five non-zero elements, which are the numbers of the corresponding threads. The thread numbers are assumed to be 0 to 3.
[0114] The relative position RP of the non-zero element changes to 0, 1, 0, 0, 0. The values therein represent the relative positions of the five non-zero elements, which are now their offsets in the thread.
[0115] It can be seen that the values of AP and RP in the fourth stage have changed again. This is because after each non-zero element is assigned to a thread, the basis of the absolute position and relative position of the non-zero element has changed.
[0116] Illustratively, the first value of AP is 0 and the first value of RP is 0, indicating that the first non-zero element (4) is allocated to thread 0 and is located at position 0 therein, and the rest are similar and will not be described in detail.
[0117] Schematic representation of the fifth stage (refer to Figure 3 S6 below):
[0118] The strategy of the fifth stage is to select the probability of each reduction method from the preset multiple reduction methods, and its strategy space is multiple reduction methods. For example, the reduction strategy RS of the fifth stage selects the thread overall reduction, and the elements y[0] to y[4] in the final array y[] are used to record the reduction results of the 1st to 4th rows of the sparse matrix multiplied by the vector. The choice of reduction method will not affect the distribution of non-zero elements and other information. Therefore, the actual stage expression of the fifth stage will not change relative to the stage expression of the fourth stage.
[0119] In addition, S6 is only to show the complete process. In fact, the 5th stage has already determined the last action based on the stage expression of the 4th stage. Therefore, there is no need to determine the stage expression of the 5th stage.
[0120] In summary, the stage expression can be realized by a unified triple (HM, AP, RP), which is used as an intermediate representation to describe the state of the sparse matrix-vector multiplication design process, such as Figure 4 As shown in a and b in . For a given state, it can be represented as a tree structure with multiple levels, and the corresponding unified expression is shown. At the thread block level, the intermediate representation compresses the index of the thread block to which the non-zero elements are assigned, similar to the row index compression of non-zero elements in the CSR format. The BM array stores the memory offset of the first element in each block, similar to the row offset (row_offset) in the CSR format. BM[i+1]-BM[i] calculates the number of non-zero elements in the i-th block. The TM array stores the memory offset of the first element in each thread. At the memory level, the ML array stores the memory offset of the first element in each row. In order to express the absolute and relative positions between non-zero elements, AP represents the thread index of the non-zero element, indicating the number of threads. RP represents the local index of the non-zero elements within each thread, indicating their corresponding positions in the continuous memory segment.
[0121] 2. Reinforcement Learning Module
[0122] According to one embodiment of the present invention, the reinforcement learning module is configured to: in the i-th stage, extract the stage features from the stage expression of the i-1th stage using a neural network, determine the strategy and prediction value of the i-th stage according to the stage features; obtain a search tree maintained for the sparse matrix based on the Monte Carlo tree search algorithm, and determine the action taken in the i-th stage based on the search tree, the strategy and the prediction value; and construct a program code that guides the sparse matrix vector multiplication to complete the calculation on the computing hardware according to the actions taken in all stages, where i>0. Using reinforcement learning, the effect of automatic tuning is achieved on the basis of a unified stage expression.
[0123] The reinforcement learning module includes:
[0124] The neural network model includes a feature extractor, a policy network, and a value network, wherein the feature extractor extracts stage features from stage expressions, the policy network determines the strategy according to the stage features, and the value network determines the value according to the stage features.
[0125] Monte Carlo search algorithm is used to perform Monte Carlo search based on strategy and value to determine the actions to be taken at each stage.
[0126] The experience pool is used to collect experience samples to train the neural network model.
[0127] The following is a schematic introduction to the parts related to the reinforcement learning module.
[0128] (1) Neural network model
[0129] If only the simplicity of construction is considered, the neural network model of the present invention can use a preset multi-layer convolutional neural network to extract stage features from stage expressions, and then predict strategies and values based on the stage features.
[0130] Furthermore, in order to achieve better results, the present invention may also improve the feature extractor to better utilize hierarchical mapping to extract more diverse information from the stage expression.
[0131] Purpose of network structure design: The importance of the absolute position and relative position of each non-zero element needs to be automatically learned by the network and their impact on performance needs to be evaluated. In addition, the hierarchical mapping, as a hierarchical structure of non-zero elements, is multi-level and requires an iterative structure, which should be reflected in the network as prior knowledge. For this, see Figure 5, the absolute position and relative position of each non-zero element are input into N layers (the size of N depends on the number of levels in which the non-zero element is allocated in the hierarchical structure of the computing hardware. If the thread block level allocation and thread level allocation in NVIDIA's graphics card are taken as an example, N is 2) of basic modules. Each basic module is equipped with a multi-layer perceptron and a pooling layer for pooling the values extracted by the multi-layer perceptron. The pooling process of the pooling layer is guided by the hierarchical mapping of the non-zero element. The feature vector obtained after average pooling in each pooling layer is used as the input of the next basic module. The output of the last basic module is subjected to global average pooling through a global pooling layer to obtain stage features. The stage features are respectively input into the policy network and the value network to predict the policy and value.
[0132] Schematically, the perceptron can adopt a multi-layer perceptron. First, each non-zero thread id (absolute position) and the assigned processing position (relative position) are input into a multi-layer perceptron and aggregated based on the pooling layer to generate a high-dimensional feature vector (Embedding). The first basic module captures the non-zero number of processes of each thread and the dependencies related to the data location of each thread. Subsequently, the feature vector output by the first basic module is input into the multi-layer perceptron of the next basic module and aggregated based on the pooling layer of the second basic module, which produces multiple new embeddings representing the resource allocation and calculation sequence of each thread in each thread block. Finally, the output of the second basic module is used as the input of the global pooling layer, which extracts information related to all thread blocks. The entire network fully simulates the resource scheduling process on the GPU.
[0133] 1.1) Feature Extractor
[0134] Take N as 2 as an example, see Figure 6 According to one embodiment of the present invention, the feature extractor comprises:
[0135] A first feature extraction module 10 is used to extract a first intermediate feature from the absolute position and relative position of the non-zero element of the stage expression record using a perceptron;
[0136] A first pooling layer 20 is used to perform a first average pooling on the first intermediate feature according to the thread mapping recorded in the stage expression to obtain a second intermediate feature, wherein the size of each pooling block during the first average pooling is set to the area related to the data allocated to the corresponding thread in the first intermediate feature;
[0137] A second feature extraction module 30, configured to extract a third intermediate feature from the second intermediate feature using a perceptron;
[0138] A second pooling layer 40 is used to perform a second average pooling on the third intermediate feature according to the thread mapping and the block mapping recorded in the stage expression to obtain a fourth intermediate feature, wherein the size of each pooling block during the second average pooling is set to an area related to the data allocated to the corresponding thread block in the third intermediate feature;
[0139] The global pooling layer 50 is used to perform global average pooling on the fourth intermediate feature to obtain the stage feature.
[0140] When the perceptron extracts relevant features, it multiplies the input matrix (which can be a matrix consisting of the absolute and relative positions of non-zero elements or a feature vector output by the pooling layer) with the learnable weight matrix set in the perceptron to obtain the feature vector output by the perceptron. Figure 7 For example, assume that the input matrix is a 5×2 matrix consisting of the row and column numbers of the non-zero elements of the sparse matrix (such as the first row 01 represents a non-zero element at row 0 and column 1, and the rest are similar and will not be repeated). The weight matrix is assumed to be a 2×3 matrix at this time, and the two are multiplied to obtain a 5×3 eigenvector. Among them, the red box marks a position, which is the source of the value of the element sitting at the top corner in the eigenvector, that is, 0×1+1×1=1, and the rest are similar and will not be repeated. It should be understood that the matrix parameters of the multilayer perceptron are learnable, and can automatically learn which matrix features affect performance, not limited to the simple transformation examples given here. With the increase of channel feature embedding, more diverse information can be extracted.
[0141] Compared with the convolution method. Multilayer perceptrons provide two advantages. First, the multilayer perceptron layer can learn sparse features from high-dimensional space by mapping each non-zero element position to a high-dimensional vector. In contrast, the convolution layer can only capture the relative spatial distance of non-zeros in two-dimensional space. Second, although the convolution layer uses filters to aggregate the non-zero coordinate positions around each matrix element, it does not always reflect their proximity in the memory address. In contrast, the multilayer perceptron layer considers the position of each non-zero element individually, thereby avoiding learning false proximity relationships. Therefore, the embodiment of the present invention uses perceptrons to extract features to better capture information related to the performance of sparse matrix-vector multiplication in the hierarchical hardware of computing hardware, thereby improving the performance of subsequent designed programs.
[0142] When the first pooling layer 20 performs pooling, it performs pooling according to the pooling blocks determined by the thread mapping. Figure 8 As an example, the number of rows in the vector of the first intermediate feature is the same as the number of non-zero elements. The first pooling layer sets the pooling blocks corresponding to each thread according to the thread mapping. The size of the pooling block corresponding to each thread is the number of non-zero elements assigned to the thread. Assume that the allocation of some non-zero elements is as follows Figure 8As shown above, each line below is a non-zero element corresponding feature. The thread mapping is 0, 2, 4, 5, 6. Then, the size of the pooling block is 2-0=2, 4-2=2, 5-4=1, 6-5=1. After average pooling according to these pooling blocks, the result is Figure 8 The eigenvector shown in the lower right corner.
[0143] When the second pooling layer 40 performs pooling, it performs pooling according to the pooling blocks determined by the thread mapping and the block mapping. Because the previous thread mapping has been pooled once, the size of each pooling block cannot be determined only based on the block mapping. Preferably, the second pooling layer 40 determines the pooling blocks corresponding to each thread block according to the block mapping and the thread mapping, and the size of the pooling block corresponding to each thread block is the number of pooling rows corresponding to the thread block. Schematically, if a 4×2 feature vector is input into the second pooling layer, the block mapping is 0, 4, and 6. It can be known that the memory offset of the zero element allocated to the first thread block is 0-3, and the memory offset of the zero element allocated to the second thread block is 4-5; in addition, the thread mapping is 0, 2, 4, 5, and 6. It can be known that after the pooling of the previous first pooling layer, the area affected by the first thread block has been pooled into 2 rows. Therefore, the size of the first pooling block is 2, that is, the first two rows are pooled, and the remaining pooling blocks are similar and will not be repeated.
[0144] Referring to 3 again, it can be seen that in some stages, the thread mapping and / or block mapping may be default values, and a mechanism for the pooling layer to operate under the default value can be set. According to one embodiment of the present invention, the model is configured such that: when the thread mapping recorded in the stage expression is the default value, the second intermediate feature obtained by the first pooling layer is equal to the first intermediate feature; when the block mapping recorded in the stage expression is the default value, the fourth intermediate feature obtained by the second pooling layer is equal to the third intermediate feature.
[0145] When the global pooling layer 50 performs pooling, it may pool all rows of the input feature vector into one row to obtain a stage feature.
[0146] 1.2) Policy Network
[0147] The strategy network 60 is used to predict the strategy based on the stage characteristics, and the strategy represents the probability of taking each action under the state corresponding to the current input stage expression. The strategy is the probability distribution of the action to be selected.
[0148] According to an embodiment of the present invention, the policy network 60 can be implemented using a linear layer. In addition, in order to reduce the number of parameters of the model, all stages share a common neural network model. However, the policy spaces of different stages are different, so in the policy network 60, multiple policy heads can be set, each corresponding to a stage. When the prediction strategy is expressed according to the stage of a certain stage, the strategies output by the policy heads corresponding to other stages will be blocked.
[0149] 1.3) Value Network
[0150] The value network 70 is used to predict the value according to the stage characteristics, and the value represents the expected value of the state corresponding to the current input stage expression. In other words, the predicted value represents the estimation of the future performance potential of the current input stage expression.
[0151] According to one embodiment of the present invention, the value network 70 may be implemented using a linear layer.
[0152] 1.4) Monte Carlo search algorithm
[0153] Monte Carlo search algorithm is used to guide the efficient selection of actions at each stage.
[0154] According to one embodiment of the present invention, multiple searches are performed on the constructed search tree according to the Monte Carlo algorithm based on the strategy and value, and each search includes selection, expansion, simulation and back propagation. After the last search is completed, the action to be taken is determined based on the upper confidence bound (UCB) value of the action related to the strategy of the current stage on the search tree. The present invention is configured to take the action with the highest UCB value at each stage.
[0155] According to one embodiment of the present invention, the reward in each search tree is determined based on the actual performance obtained after the program code is executed and the historical reference performance corresponding to the search tree, wherein when the actual performance exceeds the historical reference performance, a positive reward is granted, otherwise a negative reward is granted. Optionally, the historical reference performance corresponding to each search tree is dynamically determined in the following manner: maintain an exclusive historical performance record for the search tree to collect the actual performance obtained after the program code constructed using the search tree is executed; from the historical performance record, determine the value closest to the preset α quantile as the historical reference performance. Preferably, the preset α quantile is the 75th percentile. See Fig. 9 a in the game of Go, in which skill is developed through adversarial play, in which two players continually challenge each other and strategically respond to the opponent's moves. However, see Fig. 9b in the figure, the procedure for tuning sparse matrix-vector multiplication does not involve direct opponents. The output is an absolute performance indicator (GFLOPS value), and further selection is required based on the complex characteristics of the sparse matrix. To this end, the absolute performance indicator needs to be stored. For example, a reward buffer record of historical performance can be maintained. Using a threshold r α , where the subscript αin(1,100) represents the αth quantile of the test performance in the history. For example, the threshold r 75 Corresponds to the GFLOPS value closest to the 75th percentile in the reward buffer record. Using the reward buffer record, when the test performance of the agent exceeds the reward buffer record threshold r of the previous historical performance 75 , a positive reward of +1 will be granted, otherwise a negative reward of -1 will be given. The technical solution of this embodiment can at least achieve the following beneficial technical effects: performance feedback is obtained by actually running the program, and the cumulative reward in the search path of the search tree is updated using the reward of the performance feedback; the search path can be further adjusted through performance feedback, which is different from the traditional greedy method, so that reinforcement learning can adjust and enhance decision-making by considering the quality of the optimization selection at the current stage and its actual feedback on the subsequent decision-making process.
[0156] According to one embodiment of the present invention, the reinforcement learning module of the present invention can maintain a search tree for each sparse matrix. However, the efficiency of such search and the efficiency of reinforcement learning module learning are not high. As an improvement, the reinforcement learning module is configured to maintain the same search tree for sparse matrices with the same non-zero element distribution form. Because sparse matrices with the same non-zero element distribution form (matrix size and non-zero element position are the same) are sparse matrices with the same computational properties, maintaining the same search tree for sparse matrices with the same computational properties can find high-performance programs corresponding to such sparse matrices more quickly.
[0157] 1.5) Experience Pool
[0158] The reinforcement learning module is further configured to: maintain an experience pool for collecting corresponding samples after the program code is executed, each sample includes: a sparse matrix and its corresponding search tree.
[0159] In addition, in the strategy selection process of Monte Carlo trees in sparse matrix-vector multiplication programming, some programming strategies are generally preferred because some programming strategies are relatively advanced. However, these specific strategies may not always be the best choice for each matrix. In particular, matrices with unique sparsity patterns (small sample cases) may not benefit as much from these traditional strategies and may require more customized programming to achieve high performance. Given that the training loss of small sample cases is often higher than the training loss of most sparse matrices, due to the limited available training data and the imbalanced search paths, the prediction performance of the trained neural network for small sample cases becomes poor if historical samples are randomly sampled indiscriminately.
[0160] In view of the above problems, according to an embodiment of the present invention, see Fig.10 , the reinforcement learning module is also configured to: associate each sample with its corresponding loss, set multiple priority storage areas in the experience pool, each storage area corresponds to a loss range, and the lower bound of the loss range corresponding to the higher priority storage area is larger; based on the multiple priority storage areas, differentially sample samples from each storage area for training. Preferably, the differential sampling of samples from each storage area for training includes: setting a larger sampling probability for the higher priority storage area, and sampling a batch of samples from the experience pool for training according to the sampling probability set for each storage area. The training process can adopt the general process of reinforcement learning. During the training process, samples can also be collected while training. The technical solution of this embodiment can at least achieve the following beneficial technical effects: a balanced solution path sampling strategy is developed based on priority storage, which modifies the solution path sampling probability in the accumulated training data buffer. In view of the fact that the training loss of small sample cases is often higher than the training loss of most sparse matrices, by intentionally increasing the sampling probability of these small samples, the system can design more matching programs for different sparse matrices, and develop solutions that adapt to a wider range of sparse matrices.
[0161] The method of training the neural network model based on samples sampled from the experience pool is as follows: the sparse matrix in the sample is input into the system for prediction, and the predicted value and strategy of each stage can be obtained. The search tree is used to calculate the average reward of the corresponding node in the current stage as the target value. The search tree is used to calculate the probability of each action under the corresponding node in the current stage as the target strategy. During training, the L2 loss function is used to guide the update of the parameters of the value network according to the predicted value and the target value (equivalent to the value label), and the cross entropy loss function is used to guide the update of the parameters of the strategy network according to the predicted strategy and the target strategy (equivalent to the strategy label); the total loss function is used to guide the update of the parameters of the feature extractor according to the predicted value, target value, predicted strategy and target strategy, where the total loss function is the weighted sum of the L2 loss function and the cross entropy loss function.
[0162] The following is a pseudo code example corresponding to a program to illustrate the generated program.
[0163] According to an example of the present invention, the schematic pseudo code is as follows:
[0164] read_matrix reads in sparse matrix data
[0165] sort_by_row
[0166] compressStorage data processing compression
[0167] for i in range(32) of block_id thread block allocation
[0168] for j in range(8)of thread_id thread allocation
[0169] for k in range(4) reduction strategy
[0170] accumulate and reduce the result
[0171] Atomically add to globalGlobal atomic writeback.
[0172] In order to verify the effect of the present invention, the inventor conducted a comparative experiment. The existing manual kernel design and automatic tuning system usually rely on expert knowledge or cost models, and the actual performance of the designed program is poor. For example: the existing expert knowledge sparse matrix vector multiplication program designs ACSR and CSR5 have performances of 55.18 and 35.47 GFLOPS respectively. The existing scheme that greedily maximizes the short-term benefits of the current stage does not consider the potential impact of the current choice on the subsequent decision-making process, and the performance is 60.39 GFLOPS. Both are lower than the performance of the sparse matrix vector multiplication program designed by the present invention: 78.50 GFLOPS.
[0173] In general, the solutions of the above embodiments of the present invention can achieve at least one or more of the following technical effects:
[0174] (1) The reinforcement learning module proposed in the embodiment of the present invention reduces the search time to less than ten minutes. The search process is accelerated by more than ten times. However, the current work is still limited to the process of selecting from predefined program templates, and lacks the process of efficient search in the search space. The latest AlphaSparse uses prior knowledge to prune the search space, but still requires several hours of search time.
[0175] (2) The embodiment of the present invention predicts strategies and values according to the stage characteristics corresponding to the stage expressions through reinforcement learning, and performs a Monte Carlo tree search algorithm based on the strategies and values to determine the actions to be taken in each stage, thereby efficiently generating a high-performance program. Compared with the manual design format and predefined templates of previous works, different optimization strategies can be specially selected for different characteristics of sparse matrices, thereby achieving performance improvement.
[0176] (3) By adjusting the stage expression at each stage according to the action of the previous stage, the present invention can consider the potential impact of the action selected in the previous stage on the subsequent decision stage, so as to better generate the action of the next stage, thereby achieving effective decision making and higher program performance.
[0177] (4) The present invention utilizes reinforcement learning (RL) to simulate the entire sparse matrix-vector multiplication (SpMV) programming process. Compared with the prior art which is unable to perform efficient heuristic search in the huge sparse matrix-vector multiplication program optimization search space, the present invention proposes a new automatic tuning system based on reinforcement learning (RL) to effectively combine and search for high-performance sparse matrix-vector multiplication programs using Monte Carlo tree search, thereby more effectively implementing program-related kernel design.
[0178] (5) The present invention proposes a hierarchical position-based unified intermediate expression (stage expression) that can simultaneously express the sparse matrix format and program kernel optimization related information, thereby solving the problem that the above-mentioned prior art cannot coordinately optimize the sparse matrix format and program kernel optimization strategy.
[0179] (6) In some embodiments of the present invention, a perceptron is used to extract features in a neural network model. The perceptron can learn sparse features from a high-dimensional space by mapping each non-zero element position to a high-dimensional vector. Block mapping and thread mapping are used to design different pooling blocks in different pooling layers for hierarchical pooling. Hierarchical pooling has the ability to simultaneously consider hardware resource levels and different non-zero positions, thereby improving the performance of the reinforcement learning module.
[0180] (7) Hierarchical pooling has the ability to simultaneously consider hardware resource hierarchies and different non-zero positions, improving the performance of sparse matrix-vector multiplication programs designed for hierarchical computing hardware in reinforcement learning modules.
[0181] It should be noted that although the above describes the various steps in a specific order, it does not mean that the various steps must be executed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.
[0182] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.
[0183] Computer readable storage medium can be a tangible device that holds and stores instructions used by an instruction execution device. Computer readable storage medium can include, for example, but is not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer readable storage medium include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, for example, a punch card or a protruding structure in a groove on which instructions are stored, and any suitable combination thereof.
[0184] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A method for expressing a sparse matrix-vector multiplication space, comprising: Construct the state space of the sparse matrix according to the predetermined format, including: Get the sparse matrix required for the sparse matrix-vector multiplication to be calculated; In each of the multiple decision stages, a stage expression corresponding to the sparse matrix is determined respectively, which includes a hierarchical mapping, an absolute position and a relative position of non-zero elements, wherein the hierarchical mapping indicates allocation information of the non-zero elements of the sparse matrix in computing hardware.
2. The method according to claim 1, characterized in that The hierarchical mapping includes: Memory mapping, used to indicate the memory index corresponding to each row of the sparse matrix with non-zero elements; Block map, used to indicate the memory index of the non-zero element assigned to each thread block; Thread map, used to indicate the memory index of the non-zero element allocated to each thread.
3. The method according to claim 1 or 2, characterized in that: The method further comprises: Constructing a strategy space for sparse matrix-vector multiplication includes: setting a strategy space corresponding to each stage to limit a set of actions that can be taken in the stage, wherein the actions are used to modify the stage expression of the previous stage. A set of behaviors for adjusting at least one of the hierarchical mapping, the absolute position, and the relative position of non-zero elements.
4. The method according to claim 3, characterized in that The method further comprises: At each stage, the action to be taken in the current stage is determined by using the stage expression of the previous stage and the strategy space of the current stage; According to the action taken in the current stage, the stage expression of the previous stage is modified to obtain the stage expression of the current stage.
5. The method according to claim 4, characterized in that Using the deployed reinforcement learning module, determine the actions taken at the current stage, including: A neural network is used to extract stage features from the stage expression of the previous stage, and the strategy and value of the current stage are determined according to the stage features; Get the search tree maintained for the sparse matrix based on the Monte Carlo tree search algorithm, and determine the action taken in the current stage from the policy space based on the search tree, the policy and value of the current stage.
6. The method according to claim 3, characterized in that: The strategy space for sparse matrix-vector multiplication includes: The strategy space of the first stage includes whether to reorder the rows of the sparse matrix. If so, the rows of the sparse matrix are reordered from large to small according to the number of non-zero elements they contain; The strategy space of the second stage includes compression and filling, which selectively compresses or fills the sparse matrix obtained in the previous stage. Compression means that the non-zero elements of the sparse matrix are ignored in the compression format, and filling means that all zero elements of the sparse matrix are regarded as non-zero elements. The strategy space of the third stage includes multiple allocation granularities set when allocating thread blocks by row non-zero elements of the sparse matrix obtained in the previous stage; The strategy space of the fourth stage includes multiple allocation granularities set when allocating non-zero elements allocated in each thread block by row and multiple allocation granularities set when allocating threads by number of non-zero elements; The strategy space of the fifth stage includes multiple preset reduction methods, and the reduction method is used to limit the method of summing the results of multiplying non-zero elements in the sparse matrix and elements in the vector completed by the relevant threads.
7. The method according to claim 3, characterized in that The strategy space for sparse matrix-vector multiplication includes: The strategy space of the first stage includes compression and filling to selectively compress or fill the sparse matrix. Compression means that the non-zero elements of the sparse matrix are ignored in the compression format, and filling means that all zero elements of the sparse matrix are regarded as non-zero elements. The strategy space of the second stage includes multiple allocation granularities set when allocating thread blocks by non-zero elements in rows of the sparse matrix obtained in the previous stage; The strategy space of the third stage includes multiple allocation granularities set when allocating non-zero elements allocated in each thread block by thread row; The strategy space of the fourth stage includes multiple preset reduction methods, and the reduction method is used to limit the method of summing the results of multiplying non-zero elements in the sparse matrix and elements in the vector completed by the relevant threads.
8. A sparse matrix-vector multiplication programming system based on reinforcement learning, for building programs on computing hardware with a hierarchical structure, the system comprising: A sparse matrix expression module, configured to: determine, at each of the plurality of decision stages, a stage expression corresponding to the sparse matrix according to a method according to any one of claims 1 to 7; The reinforcement learning module is configured to: in the i-th stage, use a neural network to extract stage features from the stage expression of the i-1th stage, and determine the strategy and prediction value of the i-th stage according to the stage features; obtain a search tree maintained for the sparse matrix based on a Monte Carlo tree search algorithm, and determine the action taken in the i-th stage based on the search tree, the strategy and the prediction value; according to the actions taken in all stages, construct a program code that guides the sparse matrix vector multiplication to complete the calculation on the computing hardware, where i>0.
9. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and the computer program can be executed by a processor to implement the steps of the method according to any one of claims 1 to 8.
10. An electronic device, characterized in that: include: one or more processors; as well as A memory, wherein the memory is used to store executable instructions; The one or more processors are configured to implement the steps of the method of any one of claims 1 to 8 by executing the executable instructions.
