A method for large-scale high-throughput sparse matrix vector integer multiplication based on FPGA
By compressing sparse matrix parallel reading and configuring accumulators on FPGA, the problem of discontinuous vector reading in sparse matrix multiplication is solved, realizing highly parallel sparse matrix vector multiplication and improving computation speed and performance.
Patent Information
- Application Number
- CN202310860930.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-13
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2043-07-13
AI Technical Summary
Existing technologies suffer from discontinuous vector reading in sparse matrix multiplication, resulting in low computational efficiency and failing to fully utilize the large-scale logic circuit resources of FPGAs for high-parallelism operations.
By compressing a sparse matrix into a two-dimensional storage matrix, multiple rows of information are read in parallel using the on-chip storage module of an FPGA. High parallelism computation is achieved through configurable parallel accumulators and data rearrangement structures, including multi-level bubble structures and variable input queues to handle invalid data.
It realizes high-throughput sparse matrix-vector multiplication on FPGA, solves the problem of discontinuous vector reading, improves the calculation speed, and significantly improves performance and reduces latency, especially in large-scale digital signal processing.
Smart Images

Figure CN116992203B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of FPGA acceleration for sparse matrix-vector integer multiplication and is applied to high-speed, low-latency digital signal processing. More specifically, it relates to a method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA. Background Technology
[0002] In scientific computing, numerous problems require the application of matrix multiplication, especially sparse matrix multiplication. Examples include machine learning, software simulation, and digital signal processing. A sparse matrix contains a large number of zero elements, which have no computational value. However, significant time is consumed in multiplying and adding these zero elements. Therefore, sparse matrix multiplication was developed to reduce computational waste and accelerate the process. Sparse matrix multiplication ignores the zero elements in the matrix, performing multiplication and addition only on the non-zero elements.
[0003] Modern CPUs employ numerous vectorized instructions to optimize the parallel performance of scientific computing, enabling operations on multiple operands with a single instruction. These instructions require operands to be contiguous in memory. However, sparse matrix multiplication suffers from discontinuous memory access, making its efficiency on CPUs relatively low. The programmability and parallelism of FPGAs (Field Programmable Gate Arrays) can mitigate memory access issues and significantly increase the speed of element-wise multiplication and addition. FPGAs contain numerous low-latency SRAM cells; by copying the vector of discontinuous accesses across multiple SRAM cells, elements at multiple random locations can be accessed simultaneously in a single cycle, multiplied with non-zero elements, and then added in parallel.
[0004] In some designs, the bandwidth for retrieving matrix information from off-chip memory becomes a bottleneck affecting computational speed. While sparse matrix-vector multiplication implemented using FPGAs can significantly accelerate computation, it does not fully utilize large-scale logic circuits to further improve computational parallelism. Summary of the Invention
[0005] To overcome the shortcomings of the prior art, this invention provides a method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA, which solves the problem of discontinuous vector reading in sparse matrix-vector multiplication and achieves high parallelism.
[0006] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:
[0007] A method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA, comprising the following steps:
[0008] S1. Sparse matrix row compression transformation: Compress the sparse matrix into a two-dimensional storage matrix, which can directly read the elements of the entire row of the storage matrix for calculation, and record the inter-row relationships of the storage matrix through a recording vector;
[0009] S2. Simultaneous computation of multiple rows of storage matrix: Input the information of multiple rows of storage matrix into multiple multiply-accumulate kernels in one cycle to obtain multiple multiply-accumulate results;
[0010] S3. Configurable parallel accumulator and data rearrangement and alignment: The outputs of multiple multiply-accumulate cores are arbitrarily configured into continuous subsequences based on the input, and then the subsequences are accumulated by a configurable adder; and the accumulated results are rearranged and aligned to obtain a consistent output bit width.
[0011] Further, in step S1, the number of columns in the storage matrix is defined according to the granularity of the multiply-accumulate kernel calculation, and the non-zero elements and their corresponding column numbers in the sparse matrix are extracted and stored. Each row of the storage matrix contains elements from the same row of the sparse matrix. Multiple consecutive rows of storage matrix information form a row in the sparse matrix. A vector is used to record whether consecutive rows of the storage matrix originate from the same row of the sparse matrix. The storage matrix is distributed across on-chip storage modules on the FPGA. For example, there are eight storage modules: the first row is stored in module 1, the second row in module 2, the eighth row in module 8, and the ninth row in module 1. This allows multiple rows of the storage matrix to be read within one cycle. The recording vector is stored using one bit. If two consecutive rows of the storage matrix belong to the same row, their corresponding bits are equal; otherwise, they are opposite.
[0012] Furthermore, in step S2, a single multiply-add kernel can input a row of stored matrix information. Each row contains N non-zero elements and their corresponding column numbers. The corresponding element of the vector is indexed by the column number information and multiplied by the non-zero element. Then, the N multiplication results are added together and output.
[0013] Further, in step S3, the configurable parallel accumulator performs parallel configurable accumulation of multiple multiply-accumulate kernel outputs through a record vector. Specifically, this includes parallel accumulation of one multiply-accumulate kernel output with the next multiply-accumulate kernel output. If the elements of the record vector are unequal, i.e., the two multiply-accumulate kernel outputs do not belong to the same row of the original matrix, the parallel accumulation is interrupted and restarted. During data rearrangement and alignment, since the number of accumulator outputs is fixed, invalid data may appear in the result. The number of valid output data is determined by the record vector. The output data needs to be rearranged and aligned. Therefore, this invention uses a bubble structure for parallel data rearrangement. Each level of the structure moves the valid data upward by 1 or 2 units, and through multi-level cascading, continuous valid data is finally output. After multiple levels of bubble structure, the number of valid data is determined by the record vector, and the results of multiple cycles need to be aligned. Specifically, different numbers of data enter a queue with variable input in parallel. When the number of valid data in the queue is greater than the number of multiply-accumulate kernels C, the first C data are output. This operation obtains a consistent output bit width, which facilitates subsequent FIFO and RAM operations.
[0014] Furthermore, step S1 specifically includes:
[0015] The matrix is compressed into two matrices, vals and cols, and a record vector, flags. All indices used in this invention start from 0. vals is an L x N matrix, where N is the computational granularity of each multiply-accumulate kernel. Row-sparse compression is performed on the ROW x COL matrix M, filling non-zero elements of each row into matrix vals. Once vals is full, the next row is filled. When a row in matrix M is compressed and about to wrap, vals also wraps. Unfilled elements in vals are defaulted to 0. Matrix cols is also an L x N matrix, with its elements being the column numbers of the elements in vals corresponding to the column numbers in matrix M. Matrix flags is an L x 1 matrix, with the 0th element being 0. The rule for other elements is: when the elements of the nth row and the (n-1th row) of matrix vals originate from different rows in matrix M, the nth row of matrix flags is the logical NOT of the (n-1th row).
[0016] Furthermore, step S2 specifically includes:
[0017] The values (vals) and cols are stored in the FPGA's BRAM (Block RAM or Ultra RAM). To achieve a larger read bandwidth, multiple fixed RAM circuit units are connected in parallel to form a RAM module, allowing all elements of each row of values (vals) and cols to be read from a single address. To enable simultaneous calculation of multiple rows of the matrix, C RAM modules are designed inside the FPGA to store values (vals) and cols. Each row of values (vals) and cols is stored sequentially in C RAM modules: the first row is stored in module 1, the second row in module 2, the Cth row in module C, and the (C+1)th row in module 1. This allows all elements of C consecutive rows in the values (vals) and cols matrix to be read from a single address in each clock cycle.
[0018] Furthermore, step S2 also includes:
[0019] The vector x is copied extensively into the Block RAM of each multiplication unit, allowing multiple multiplication operations to be processed independently in parallel. Each multiplication unit contains a Block RAM, a DSP48 unit, and corresponding lookup tables and flip-flops to handle the logic and timing. Each clock cycle, the multiplication unit inputs the element val from vals and the element col from cols in the same row and column. The multiplication unit uses the Block RAM to access the value x in the vector x by indexing col. col , will x col Multiply by val;
[0020] By connecting N multiplication units in parallel to form a multiply-add kernel, and inputting N elements from each row of `vals` and `cols` into the multiply-add kernel each clock cycle, a binary search addition tree is used to add the N multiplication results. Each input at each level of the addition tree is added pairwise, so only log₂N levels are needed to accumulate N numbers with a relatively low logic chain length. This operation yields the multiply-add result for the input rows corresponding to `vals` and `cols`.
[0021] This invention establishes multiple multiply-accumulate cores on the circuit and uses high-bandwidth matrix information reading to read elements from N*C values in `vals` and `cols` in each clock cycle. Then, the element indexed in `cols` is used to find the corresponding `x` in the row, which is multiplied by the corresponding `val`. Finally, an addition tree is used to sum the results of the multiplication units within each multiply-accumulate core. The next step is to accumulate the outputs of the multiple multiply-accumulate cores according to the record vector to obtain the output of the corresponding row.
[0022] Furthermore, this invention designs a structure that allows the outputs of multiple multiply-accumulate cores to be configurably accumulated in parallel according to the flags of their corresponding input rows, while simultaneously performing rearrangement and alignment. This structure consists of three parts: the first part is a configurable adder array, i.e., a configurable parallel accumulator; the second part is a bubbling structure circuit to eliminate invalid accumulation results; and the third part is a variable input queue, i.e., a register queue.
[0023] Furthermore, in step S3:
[0024] The configurable parallel accumulator has log2C stages, where C is the number of multiply-accumulate cores; each stage has C units, taking two numbers Loa and Roa as input, and adding them to obtain the result. Where L is the current layer number, n is the nth unit of the current level, and then the output is passed to the next layer;
[0025] Let Loa for each level be make For the output of the nth multiply-accumulate core, Roa is based on logic Configure if If it is 0, then Roa is 0; if If it is 1, then Roa is If n+2 L ≥C, let
[0026] in, The definition is: when L is 0, Where S n For the `vals` input line of the `n`th multiply-add kernel, specify the value of `flags`; when `L` is not 0...
[0027] After log2C level processing, this structure adds the n input values belonging to the same row of the original matrix to obtain the corresponding accumulated result; invalid results are eliminated and queued for output alignment; the configurable parallel accumulator output is... Recorded as The validity of the corresponding value is V n =S n XOR S n-1 .
[0028] Furthermore, in step S3, a bubble structure circuit is designed to manage the effective output of the adder array; the circuit structure of each stage is the same, with the 0th stage inputting the C outputs of the adder array. Recorded as and effectiveness V n , recorded as Output and effectiveness
[0029] The basic framework of each stage of the bubble sort circuit is to determine whether there is invalid data among the n-1 data points preceding the nth data point. If so, the output is shifted forward by 1 or 2 units. The shift must ensure that the order of valid data is not disrupted, and that all valid data is ultimately arranged from input 0 to VN-1, where VN is the number of valid input data points. If invalid data exists among the n-1 input points preceding the nth input, then that input must jump forward by at least 1 unit. If each stage can only shift forward by 1 unit, since the accumulator's 0th output... It must be effective, so in the worst case, a level C-2 is needed to eliminate all invalid data;
[0030] To accelerate the bubbling process, bypass points are set at certain locations, causing the data at these bypass points to be shifted forward by 2 units; for a bypass point... The rules for setting its position are: n = 2*u - 1, where u is a positive integer and 2*u ≤ C;
[0031] mark For the bypass point The input is valid and can move forward 2 units, where n>T; when 2*u-1≠n. It can only be 0; therefore, when n = 2*u-1, the bypass point... The logical representation is as follows formula The first item indicates whether the data at this point is valid; the second item determines whether there is valid data in the previous unit. If there is, it cannot move forward 2 units; the third item determines whether the data at the previous bypass point is invalid or whether the data can be moved forward to make room.
[0032] For the most basic operation of moving forward 1 unit M 1 This operation does not bypass the bypass point and is marked. To be able to move forward one unit at this point, its logical representation is:
[0033] Output data and validity criteria To move, for output The logic is: if n≠C-1, then... Then output If not satisfied, determine... If the value is 1, then the output is If it is not 1, then output... For output The logic is: if Output Otherwise, output 0; output validity. and Logical consistency;
[0034] The output of the adder array contains VN valid data points and C-VN invalid data points, which are then input into... A cascaded bubble structure circuit; after In the first stage of processing, all output data from output 0 to output VN-1 are valid data and their order remains unchanged. The next step is to send the outputs into a queue to wait for the final output. For U n .
[0035] Furthermore, step S3 also includes: designing a register queue of length 2C, denoted as Q for the data in the queue. n By notation p q The initial value is 0, the starting index for writing is determined, and the write length is VN;
[0036] When writing to the queue, it is necessary to check whether the S0 corresponding to the queue data is the same as the S0 corresponding to the queue input data in the previous cycle. C-1 If they are the same, then the queue will be... The data at point is incremented by the data at point U0, starting from p. q Start writing to U1, p q Increment VN by 1; if different, write the data of U0 to the value. p q Increment VN; if the notation p q If ≥2C, then overflow to p q -2C;
[0037] When p q Crossing either C or 0 and in the next cycle Q C-1 Q 2C-1 No need to be overwritten, Q0~Q C-1 Or Q C ~Q 2C-1 Output; if an overwrite occurs, output after a one-clock-cycle delay.
[0038] When the queue meets the output conditions, it can output the multiplication and addition results of consecutive C rows of matrices and vectors each time. In its fastest case, this design can output the multiplication results of C rows of matrices and vectors in one cycle. In sparse cases, it can fully utilize the BRAM resources on the FPGA chip, and it can also enable multiple multiply-accumulate cores to work together to handle dense cases, thus solving arbitrary matrix-vector multiplication. The structure proposed in this invention has multiple cascaded levels and a relatively long logic chain, but overall, the fan-out of each level is not large. Flip-flops can be inserted at appropriate locations, and pipelined design can be used to reduce latency and significantly improve the circuit throughput.
[0039] Compared with existing technologies, the advantages of this invention are as follows: This invention provides a method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA. It fully utilizes the logic resources of the FPGA to perform large-scale, high-throughput sparse matrix-vector integer multiplication, solving the problem of discontinuous vector reading in sparse matrix-vector multiplication and achieving high parallelism. Compared to existing sparse matrix multiplication methods that obtain matrices from off-chip memory, this invention leverages the low matrix update frequency and solves the bandwidth bottleneck of matrix reading through on-chip memory. In large-scale digital signal processing applications with high real-time requirements, such as optoelectronic signal processing, this method can achieve ultra-low data processing latency, with speeds several orders of magnitude higher than computer CPUs. This invention can also be applied to digital circuit chips to achieve extremely low power consumption and extremely high performance. Attached Figure Description
[0040] Figure 1 This is a schematic diagram of the method flow of the present invention.
[0041] Figure 2 This is a schematic diagram of the structure of the present invention.
[0042] Figure 3 This is a flowchart of the modified sparse matrix row compression algorithm in an embodiment of the present invention.
[0043] Figure 4 This is a schematic diagram of the multiply-add kernel structure proposed in an embodiment of the present invention.
[0044] Figure 5 This is a schematic diagram of the configurable parallel accumulator structure proposed in an embodiment of the present invention.
[0045] Figure 6 This is a schematic diagram of the bubble structure for data rearrangement and alignment proposed in an embodiment of the present invention.
[0046] Figure 7 This is the logical expression for bubbling in the data rearrangement and alignment bubbling structure proposed in this embodiment of the invention.
[0047] Figure 8 This is a schematic diagram of the queue structure for data rearrangement and alignment proposed in an embodiment of the present invention. Detailed Implementation
[0048] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. The present invention will be described in one embodiment below with reference to specific embodiments. The accompanying drawings are for illustrative purposes only and represent schematic diagrams, not actual pictures, and should not be construed as limiting the present patent. In order to better illustrate the embodiments of the present invention, some parts of the drawings may be omitted, enlarged, or reduced, and do not represent the actual product size. It is understandable to those skilled in the art that some well-known structures and their descriptions may be omitted in the drawings.
[0049] In the description of this invention, it should be understood that if terms such as "upper," "lower," "left," and "right" indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings, they are only for the convenience of describing the invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, the terms describing positional relationships in the accompanying drawings are only for illustrative purposes and should not be construed as limiting this patent. For those skilled in the art, the specific meaning of the above terms can be understood according to the specific circumstances. In addition, if the embodiments of this invention involve descriptions of "first," "second," etc., such descriptions are only for descriptive purposes and should not be construed as indicating or implying their relative importance or implicitly specifying the number of technical features indicated. Thus, features defined with "first" and "second" may explicitly or implicitly include at least one of those features. Furthermore, the meaning of "and / or" throughout the text is to include three parallel solutions. Taking "A and / or B" as an example, it includes solution A, or solution B, or a solution that simultaneously satisfies A and B.
[0050] Example 1:
[0051] like Figure 1 As shown, a method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA includes the following steps:
[0052] S1. Sparse matrix row compression transformation: Compress the sparse matrix into a two-dimensional storage matrix, which can directly read the elements of the entire row of the storage matrix for calculation, and record the inter-row relationships of the storage matrix through a recording vector;
[0053] S2. Simultaneous computation of multiple rows of storage matrix: Input the information of multiple rows of storage matrix into multiple multiply-accumulate kernels in one cycle to obtain multiple multiply-accumulate results;
[0054] S3. Configurable parallel accumulator and data rearrangement and alignment: The outputs of multiple multiply-accumulate cores are arbitrarily configured into continuous subsequences based on the input, and then the subsequences are accumulated by a configurable adder; and the accumulated results are rearranged and aligned to obtain a consistent output bit width.
[0055] In step S1, the number of columns in the storage matrix is defined according to the granularity of the multiply-accumulate kernel. Non-zero elements and their corresponding column numbers in the sparse matrix are extracted and stored. Each row of the storage matrix contains elements from the same row of the sparse matrix. Multiple consecutive rows of storage matrix information form a single row in the sparse matrix. A vector is used to record whether consecutive rows of the storage matrix originate from the same row of the sparse matrix. The storage matrix is distributed across on-chip storage modules on the FPGA. For example, with eight storage modules, the first row is stored in module 1, the second row in module 2, the eighth row in module 8, and the ninth row in module 1. This allows multiple rows of the storage matrix to be read within one cycle. The recording vector is stored using one bit. If two consecutive rows of the storage matrix belong to the same row, their corresponding bits are equal; otherwise, they are opposite.
[0056] In step S2, a single multiply-add kernel can input a row of stored matrix information. Each row contains N non-zero elements and their corresponding column numbers. The corresponding element of the vector is indexed by the column number information and multiplied by the non-zero element. Then, the N multiplication results are added together and output.
[0057] In step S3, the configurable parallel accumulator performs parallel configurable accumulation of multiple multiply-accumulate kernel outputs using a record vector. Specifically, it involves parallel accumulation of one multiply-accumulate kernel output with the next multiply-accumulate kernel output. If an element in the record vector is unequal (i.e., the two multiply-accumulate kernel outputs do not belong to the same row of the original matrix), the accumulation is interrupted and restarted. During data rearrangement and alignment, since the number of accumulator outputs is fixed, invalid data may appear in the result. The number of valid output data is determined by the record vector. The output data needs to be rearranged and aligned. Therefore, this invention uses a bubble structure for parallel data rearrangement. Each level of the structure moves valid data upwards by 1 or 2 units, and through multi-level cascading, continuous valid data is finally output. After multiple levels of the bubble structure, the number of valid data is determined by the record vector, and the results of multiple cycles need to be aligned. Specifically, different numbers of data enter a queue with variable input in parallel. When the number of valid data in the queue is greater than the number of multiply-accumulate kernels C, the first C data are output. This operation obtains a consistent output bit width, facilitating subsequent FIFO and RAM operations.
[0058] Example 2
[0059] A method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA, comprising the following steps:
[0060] Step 1: Compress the matrix into two matrices, vals and cols, and a record vector, flags. The indices used in this invention all start from 0.
[0061] vals is an L-row N-column matrix, where N is the computational granularity of each multiply-accumulate kernel. Row sparse compression is performed on the ROW-row COL-column matrix M, and the non-zero elements of each row are filled into the matrix vals one by one. When vals is full, the next row is filled. When the row of matrix M is compressed and about to wrap to a new row, vals wraps to a new row at the same time. The unfilled elements in vals are defaulted to 0.
[0062] The matrix cols is also an L-row N-column matrix, and the elements of vals correspond to the column numbers in matrix M.
[0063] The matrix flags is an L-row, 1-column matrix with the 0th element being 0. The rules for the other elements are as follows: when the elements of the nth row and the (n-1th row) of the matrix flags come from different rows of the matrix M, the nth row of the matrix flags is the logical NOT of the (n-1th row).
[0064] Step 2: Store vals and cols in the FPGA's BRAM, i.e., Block RAM or Ultra RAM; in order to obtain a larger read bandwidth, multiple fixed RAM circuit units are connected in parallel to form a RAM module, so that all elements of each row of vals and cols can be read from one address.
[0065] To enable simultaneous computation of multiple rows of the matrix, the FPGA internally uses C RAM modules to store vals and cols. Each row of vals and cols is stored sequentially in the C RAM modules: the first row is stored in module 1, the second row in module 2, the Cth row in module C, and the (C+1)th row in module 1. This allows all elements of C consecutive rows in the vals and cols matrix to be read from a single address in each clock cycle.
[0066] The vector x is copied extensively into the Block RAM of each multiplication unit, allowing multiple multiplication operations to be processed independently in parallel. Each multiplication unit contains a Block RAM, a DSP48 unit, and corresponding lookup tables and flip-flops to handle the logic and timing. Each clock cycle, the multiplication unit inputs the element val from vals and the element col from cols in the same row and column. The multiplication unit uses the Block RAM to access the value x in the vector x by indexing col. col , will x col Multiply by val;
[0067] By connecting N multiplication units in parallel to form a multiply-add kernel, and inputting N elements from each row of `vals` and `cols` into the multiply-add kernel each clock cycle, a binary search addition tree is used to add the N multiplication results. Each input at each level of the addition tree is added pairwise, so only log₂N levels are needed to accumulate N numbers with a relatively low logic chain length. This operation yields the multiply-add result for the input rows corresponding to `vals` and `cols`.
[0068] This invention establishes multiple multiply-accumulate cores on the circuit and uses high-bandwidth matrix information reading to read elements from N*C values in `vals` and `cols` in each clock cycle. Then, the element indexed in `cols` is used to find the corresponding `x` in the row, which is multiplied by the corresponding `val`. Finally, an addition tree is used to sum the results of the multiplication units within each multiply-accumulate core. The next step is to accumulate the outputs of the multiple multiply-accumulate cores according to the record vector to obtain the output of the corresponding row.
[0069] Step 3: This invention designs a structure that allows the outputs of multiple multiply-accumulate cores to be configurably accumulated in parallel according to the flags of their corresponding input rows, while simultaneously performing rearrangement and alignment. This structure consists of three parts: the first part is a configurable adder array, i.e., a configurable parallel accumulator; the second part is a bubbling structure circuit to eliminate invalid accumulation results; and the third part is a variable input queue, i.e., a register queue.
[0070] Part One, such as Figure 5 As shown, the configurable parallel accumulator has log2C stages, where C is the number of multiply-accumulate cores; each stage has C units, taking two input numbers Loa and Roa, and adding them to obtain the result. Where L is the current layer number, n is the nth unit of the current level, and then the output is passed to the next layer;
[0071] Let Loa for each level be make For the output of the nth multiply-accumulate core, Roa is based on logic Configure if If it is 0, then Roa is 0; if If it is 1, then Roa is If n+2 L ≥C, let
[0072] in, The definition is: when L is 0, Where S n For the `vals` input line of the `n`th multiply-add kernel, specify the value of `flags`; when `L` is not 0...
[0073] After log2C level processing, this structure adds the n input values belonging to the same row of the original matrix to obtain the corresponding accumulated result; invalid results are eliminated and queued for output alignment; the configurable parallel accumulator output is... Recorded as The validity of the corresponding value is V n =S n XOR S n-1 .
[0074] Part Two, such as Figure 6 and Figure 7 As shown, a bubble structure circuit is designed to manage the effective output of an adder array; the circuit structure of each stage is the same, with the 0th stage inputting the C outputs of the adder array. Recorded as and effectiveness V n , recorded as Output and effectiveness
[0075] The basic framework of each stage of the bubble sort circuit is to determine whether there is invalid data among the n-1 data points preceding the nth data point. If so, the output is shifted forward by 1 or 2 units. The shift must ensure that the order of valid data is not disrupted, and that all valid data is ultimately arranged from input 0 to VN-1, where VN is the number of valid input data points. If invalid data exists among the n-1 input points preceding the nth input, then that input must jump forward by at least 1 unit. If each stage can only shift forward by 1 unit, since the accumulator's 0th output... It must be effective, so in the worst case, a level C-2 is needed to eliminate all invalid data;
[0076] To accelerate the bubbling process, bypass points are set at certain locations, causing the data at these bypass points to be shifted forward by 2 units; for a bypass point... The rules for setting its position are: n = 2*u - 1, where u is a positive integer and 2*u ≤ C;
[0077] mark For the bypass point The input is valid and can move forward 2 units, where n>T; when 2*u-1≠n. It can only be 0; therefore, when n = 2*u-1, the bypass point... The logical representation is as follows formula The first item indicates whether the data at this point is valid; the second item determines whether there is valid data in the previous unit. If there is, it cannot move forward 2 units; the third item determines whether the data at the previous bypass point is invalid or whether the data can be moved forward to make room.
[0078] For the most basic operation of moving forward 1 unit M 1 This operation does not bypass the bypass point and is marked. To be able to move forward one unit at this point, its logical representation is:
[0079] Output data and validity criteria To move, for output The logic is: if n≠C-1, then... Then output If not satisfied, determine... If the value is 1, then the output is If it is not 1, then output... For output The logic is: if Output Otherwise, output 0; output validity. and Logical consistency;
[0080] The output of the adder array contains VN valid data points and C-VN invalid data points, which are then input into... A cascaded bubble structure circuit; after In the first stage of processing, all output data from output 0 to output VN-1 are valid data and their order remains unchanged. The next step is to send the outputs into a queue to wait for the final output. For U n .
[0081] Part Three, such as Figure 8 As shown, design a register queue of length 2C, and let Q be the data in the queue. n By notation p q The initial value is 0, the starting index for writing is determined, and the write length is VN;
[0082] When writing to the queue, it is necessary to check whether the S0 corresponding to the queue data is the same as the S0 corresponding to the queue input data in the previous cycle. C-1 If they are the same, then the queue will be... The data at point is incremented by the data at point U0, starting from p. q Start writing to U1, p q Increment VN by 1; if different, write the data of U0 to the value. p q Increment VN; if the notation p qIf ≥2C, then overflow to p q -2C;
[0083] When p q Crossing either C or 0 and in the next cycle Q C-1 Q 2C-1 No need to be overwritten, Q0~Q C-1 Or Q C ~Q 2C-1 Output; if an overwrite occurs, output after a one-clock-cycle delay.
[0084] When the queue meets the output conditions, it can output the multiplication and addition results of consecutive C rows of matrices and vectors each time. In its fastest case, this design can output the multiplication results of C rows of matrices and vectors in one cycle. In sparse cases, it can fully utilize the BRAM resources on the FPGA chip, and it can also enable multiple multiply-accumulate cores to work together to handle dense cases, thus solving arbitrary matrix-vector multiplication. The structure proposed in this invention has multiple cascaded levels and a relatively long logic chain, but overall, the fan-out of each level is not large. Flip-flops can be inserted at appropriate locations, and pipelined design can be used to reduce latency and significantly improve the circuit throughput.
[0085] Example 3
[0086] To make the technical means, creative features, objectives and effects of this invention easier to understand, the invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. Figures 5-8 The implementation is shown using C=8 (8 multiply-accumulate cores) as an example.
[0087] Figure 3 This is the flowchart of the modified sparse matrix row compression algorithm. ROW and COL are the number of rows and columns of the matrix, respectively, and N is the number of columns in the storage matrices vals and cols. In general, the algorithm contains two nested loops. The outer loop iterates through sr from 0 to ROW-1, and the inner loop iterates through sc from 0 to COL-1. The entire matrix is traversed through these two loops. Within each loop, it checks if the element at a given position is zero. If it is non-zero, the element is stored at the corresponding position (dr, dc) in matrix vals, and the column number sc is stored at the corresponding position (dr, dc) in cols. Simultaneously, dc is incremented by 1. If dc is greater than or equal to the number of columns N in the storage matrix vals, then dc is reset to zero, and dr is incremented by 1. At the beginning of each outer sr loop and after the dr increment, the element of row dr of the record vector flags is written to f. At the end of each outer sr loop, f is inverted.
[0088] This algorithm yields the storage matrices vals and cols. These matrices are then distributed across the FPGA's on-chip RAM module, allowing for the simultaneous reading of all elements from eight consecutive rows of vals and cols within a single cycle. Each row of elements is then input into... Figure 4 In the multiply-add kernel shown, the multiplication unit multiplies the corresponding x with val based on the col index, and then accumulates the multiplication results with a logical length of log2N through a binary tree adder.
[0089] Taking a matrix M whose non-zero elements in row 0 are between N and 2N as an example, the 0th and 1st rows of the storage matrices vals and cols store the information of the non-zero elements in row 0 of matrix M, and the 0th and 1st rows of the record vector flags are both 0, while the 2nd row is 1. Therefore, the outputs of the first two multiplication kernels need to be added together to obtain the result of the matrix-vector multiplication in row 0.
[0090] Each clock cycle, while reading 8 rows of the storage matrix vals and cols, it also reads 8 rows of the record vector flags, denoted as S0 to S7. The outputs of the 8 multiply-accumulate cores are then processed. Input to S0~S7 Figure 5 The configurable parallel accumulator shown yields the following results. Corresponding validity flags V0 to V7. Each stage of the accumulator is based on the bypass flag. Decide whether to interrupt the accumulation. By S n Decision. Output 8 data points. There is invalid accumulated data, interspersed among valid data. To facilitate subsequent operations, the valid data needs to be rearranged so that it is consecutively arranged starting from output 0.
[0091] Figure 6 This is a schematic diagram of a bubble sorting structure. The diagram illustrates the location of the bypass point. In each level of the sorting structure, each input is determined according to the control logic. and Determine whether to move up one unit, move up two units, or remain stationary. The control logic J for each level of the bubble structure... n It is determined by the validity of the input data. n The expression is by Figure 7 Given, K n To ensure the validity of the input, a 4-level bubble sorting structure can be used to sort the worst-case cases.
[0092] Figure 8This is a schematic diagram of a variable input queue structure. This structure is used to temporarily store the output of a bubble sort structure, aligning the data before outputting. The queue inputs VN consecutive valid data points U0 to U7, and the diagram illustrates the case where VN = 5. When S0 of the current cycle equals S7 of the previous cycle, it indicates that the outputs of the first few multiply-accumulate kernels of the current cycle and the last few multiply-accumulate kernels of the previous cycle need to be added to obtain the correct result for a row of the matrix-vector multiplication. In this case, U0 is accumulated to Q3, and U1 to U4 are overwritten to Q4 to Q7, P... q Add VN-1; conversely, overwrite U0~U4 to Q4~Q8, P q Increment VN. The queue's output conditions are as follows:
[0093] 1. When P q When the S0 of the next cycle is not equal to the S7 of the current cycle, output Q8~Q8. 15 Or Q0 to Q7.
[0094] 2. When P q When the value exceeds 0 but is not equal to 0, or exceeds 8 but is not equal to 8, output Q8~Q. 15 Or Q0 to Q7.
[0095] like Figure 2 As shown, cascading the outputs of the eight multiply-accumulate cores, a configurable parallel accumulator, a bubble sort structure, and a variable input queue enables a large-scale, high-throughput sparse matrix vector integer multiplication circuit capable of simultaneously computing up to eight rows of matrices and calculating arbitrary sparse matrices. By adjusting the computational granularity N (the number of columns in the storage matrix vals) and the number of multiply-accumulate cores C, the parallelism of the computation can be improved, fully utilizing the on-chip logic resources of a large-scale FPGA. Furthermore, flip-flops can be inserted into the control and data signals at each stage of the structure for pipelined design, significantly increasing the operating frequency.
[0096] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the present invention, and are not intended to limit the implementation of the present invention. Those skilled in the art can make other variations or modifications based on the above description. It is neither necessary nor possible to exhaustively describe all embodiments here. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the claims of the present invention.
Claims
1. A method for large-scale, high-throughput sparse matrix-vector integer multiplication based on FPGA, characterized in that, Includes the following steps: S1. Sparse matrix row compression transformation: Compress the sparse matrix into a two-dimensional storage matrix, which can directly read the elements of the entire row of the storage matrix for calculation, and record the inter-row relationships of the storage matrix through a record vector; S2. Simultaneous computation of multiple rows of the storage matrix: In one cycle, the information of multiple rows of the storage matrix is input into multiple multiply-accumulate kernels to obtain multiple multiply-accumulate results; S3. Configurable parallel accumulator and data rearrangement and alignment: The outputs of multiple multiply-accumulate cores are used as inputs, allowing the input data to be arbitrarily configured into continuous subsequences, and then the subsequences are accumulated through a configurable adder; The accumulated results are rearranged and aligned to obtain a consistent output bit width; In step S1, the number of columns in the storage matrix is defined according to the granularity of the multiply-accumulate kernel calculation. Non-zero elements and their corresponding column numbers in the sparse matrix are extracted and stored. The elements in each row of the storage matrix come from the same row of the sparse matrix. The storage matrix information of multiple consecutive rows forms a row in the sparse matrix. At the same time, a vector is used to record whether consecutive rows of the storage matrix come from the same row of the sparse matrix. The storage matrix is distributed and stored on the on-chip storage module on the FPGA, so that multiple rows of the storage matrix can be read in one cycle. The recording vector is stored using 1 bit. If two consecutive rows of the storage matrix belong to the same row, then their corresponding bits are equal, and vice versa. In step S3, the configurable parallel accumulator performs parallel configurable accumulation of multiple multiply-accumulate kernel outputs using a record vector. Specifically, it involves parallel accumulation of one multiply-accumulate kernel output with the next multiply-accumulate kernel output. If an element in the record vector is unequal (i.e., the two multiply-accumulate kernel outputs do not belong to the same row of the original matrix), the accumulation is interrupted and restarted. During data rearrangement and alignment, a bubble structure is used for parallel data rearrangement. Each level of the structure moves valid data upwards by 1 or 2 units, and through multi-level cascading, continuous valid data is ultimately output. After multiple levels of the bubble structure, the number of valid data is determined by the record vector, requiring alignment of results from multiple cycles. Specifically, different numbers of data enter a queue with variable input in parallel. When the number of valid data in the queue exceeds the number of multiply-accumulate kernels... At that time, before output To obtain a consistent output bit width, multiple data points are used.
2. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 1, characterized in that, In step S2, a single multiply-accumulate kernel can input a single line of stored matrix information, each line containing Each non-zero element and its corresponding column number are used to index the corresponding element in the vector, which is then multiplied by the non-zero element. The results of the multiplications are added together and then output. The computational granularity for each multiply-accumulate kernel.
3. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 1 or 2, characterized in that, Step S1 specifically includes: The matrix is compressed into two matrices. , and record vector ; It is OK A matrix of columns The computational granularity for each multiply-accumulate kernel; for OK Column matrix Perform row-sparse compression, filling the matrix with non-zero elements from each row one by one. middle, If a row is filled, fill the next row; when the matrix When the line is compressed and about to wrap to a newline... Simultaneous line break Unfilled elements default to 0; matrix It is also OK A matrix of columns, whose elements are The elements correspond to the matrix Column number in the matrix; It is A matrix with 1 row and 1 column, where the 0th element is 0, and the other elements follow the rule: when the matrix... The Line and number The elements of the row come from the matrix When the matrix has different rows, The Behavior No. The logic of the line is NOT.
4. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 3, characterized in that, Step S2 specifically includes: Bundle and The data is stored in the FPGA's BRAM, also known as Block RAM or Ultra RAM. To achieve a larger read bandwidth, multiple fixed RAM circuit units are connected in parallel to form a RAM module, allowing each row to be read from a single address. , All elements of the matrix; to enable simultaneous computation across multiple rows of the matrix, the FPGA internal design... A RAM module is used for storage. , each line , Sequentially stored In the RAM modules, the first row contains module 1, the second row contains module 2, and so on. Line Existence Module , No. Row Existence Module 1; enables reading from one address in each clock cycle. , Continuous in the matrix All elements of the row.
5. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 4, characterized in that, Step S2 further includes: vector The data is copied extensively into the Block RAM of each multiplication unit, allowing multiple multiplication operations to be processed independently in parallel. Each multiplication unit contains one Block RAM, one DSP48 unit, and corresponding lookup tables and flip-flops to complete the logic and timing. The input to the multiplication unit is... (The sentence is incomplete and requires further context to translate accurately.) elements in and the same row and column elements in The multiplication unit uses BRAM via Index to Vector The value in ,Will and Multiply; Will A multiplication unit is connected in parallel to form a multiply-accumulate core, and each clock cycle inputs to the multiply-accumulate core. , Each line 1 element, then use a binary addition tree to 1 element The results of each multiplication are added together, and each input at each level of the addition tree is added pairwise to obtain the result. , The result of multiplication and addition corresponding to the input row.
6. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 5, characterized in that, In step S3: Configurable parallel accumulators include Level, among which, This represents the number of multiply-add cores; each stage has... There are 1 unit, and you need to input 2 numbers. and Add them together to get the result. ,in, This is the current floor number. For the current level Each unit is then output to the next layer; Make each level for ,make For the first The output of each multiply-add kernel According to logic Configure if If it is 0, then =0; if If it is 1, then for ;like ,make ; in, The definition of is: when When it is 0, ,in For the first One multiply-add core The input line corresponding The value; when When it is not 0, ; go through The structure will use multiply-accumulate kernels for level processing. The input values belonging to the same row of the original matrix are summed to obtain the corresponding accumulated result; invalid results are eliminated and queued for output alignment; the parallel accumulator output is configurable. , recorded as The validity of the corresponding value is .
7. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 5, characterized in that, In step S3, a bubble structure circuit is designed to manage the effective output of the adder array; the circuit structure of each stage is the same, with the 0th stage inputting the adder array... Output , recorded as ; and effectiveness , recorded as Output and effectiveness ; The basic framework of each stage of a bubble structure circuit is determined by the first... Before the data If any data is invalid, the output should be shifted forward by 1 or 2 units. The shift should not disrupt the order of valid data, and the output should ultimately ensure that all valid data are sorted from input 0 to input 2. , The number of valid input data; if the first... The first input If an input contains invalid data, then that input must jump forward at least one unit. If each stage can only move forward one unit, then due to the accumulator's 0th output... It must be effective, so in the worst case, it needs to be effective. Only at level 1 can all invalid data be eliminated; To accelerate the bubbling process, bypass points are set at certain locations, causing the data at these bypass points to be shifted forward by 2 units; for a bypass point... The rules for setting its location are as follows: , are positive integers and ; mark For the bypass point The input is valid and can move forward 2 units, where ; hour It can only be 0; then Bypass point The logical representation is as follows ;formula The first item indicates whether the data at that point is valid, and the second item determines whether there is valid data in the previous unit. If there is, then it cannot move forward 2 units. The third step is to determine whether the data from the previous bypass point is invalid or whether the data can be moved forward to make room. For the most basic operation of moving forward 1 unit This operation does not bypass the bypass point and is marked. To be able to move forward one unit at this point, its logical expression is: ; Output data and validity criteria To move, for output , The logic is: if the following conditions are met... Then output If not satisfied, then judge. If the value is 1, then the output is If it is not 1, then output For output The logic is: if Output Otherwise, output 0; output validity. and Logical consistency; The output of the adder array includes One valid data and An invalid data entry was input. A cascaded bubble structure circuit; after Level processing, output data from output 0 to output All data is valid and in the correct order. The next step is to put the output into a queue to wait for the final output. for .
8. The method for large-scale high-throughput sparse matrix-vector integer multiplication based on FPGA according to claim 7, characterized in that, Step S3 further includes: designing a A register queue of length , denoted as . Through symbols The initial value is 0, the starting index for writing is determined, and the write length is... ; When writing to a queue, it is necessary to determine the corresponding queue data. Does it correspond to the queue input data of the previous cycle? If they are the same, then the queue will be... Data incremented at the location Data from Start writing , self-added If they are different, then... Data writing , self-added If the symbol Then it overflows to ; when cross Or either 0 or 0 and the next cycle 、 No need to be overwritten, will or Output; if an overwrite occurs, output after a one-clock-cycle delay.
Citation Information
Patent Citations
A circuit for realizing sparse matrix multiplication and an FPGA board
CN109740116A
Sparse matrix vector multiplication vectorization implementation method
CN111859277A
