BCSR Format for Block-Sparse Matrices

Block Compressed Sparse Row (BCSR), also called Block Sparse Row (BSR), stores a sparse matrix as a list of dense blocks together with the indices that locate them. It suits matrices whose non-zero values cluster into blocks, which is common for block-sparse attention, structured pruning and mixture-of-experts (MoE) layers. SciPy provides it as scipy.sparse.bsr_matrix, and GPU sparse libraries provide block-sparse kernels that use it.

CSR as the starting point

Compressed Sparse Row (CSR) stores a sparse matrix in three arrays:

  • values[] holds every non-zero value in row-major order.
  • col_indices[] holds the column of each non-zero value.
  • row_ptr[] holds, for each row, the offset of its first value in values. Row i occupies values[row_ptr[i]:row_ptr[i+1]].

CSR works well for very sparse matrices with scattered non-zeros. For moderately sparse matrices it has two drawbacks. It stores one index for every value. With BF16 values and 32-bit indices, the indices take twice as much space as the values, so at 50% density a CSR matrix is larger than the dense matrix. Its scattered access pattern also cannot feed the dense tile operations that matrix multiply units are built for.

The block layout

BCSR applies the same scheme to blocks instead of individual values. The matrix is divided into a grid of fixed-size blocks, for example 32×32. Every block that contains a non-zero value is stored in full, including any zeros inside it:

  • values[] holds every non-zero block as a dense BR × BK array.
  • col_indices[] holds the block column of each stored block.
  • row_ptr[] holds, for each block row, the offset of its first block.

Consider a matrix divided into a 4×4 grid of blocks, where four blocks, A to D, contain non-zero values:

block column:   0   1   2   3
block row 0   [ .   A   .   . ]
block row 1   [ .   .   .   . ]
block row 2   [ B   .   .   C ]
block row 3   [ .   .   D   . ]

Its BCSR representation is:

values      = [A, B, C, D]
col_indices = [1, 0, 3, 2]
row_ptr     = [0, 1, 1, 3, 4]

Block row 1 is empty, so row_ptr[1] and row_ptr[2] are equal. Block row 2 holds blocks B and C, at positions 1 and 2 of values.

BCSR stores one index per block, so the index overhead falls by a factor of the block area. A 32×32 block of BF16 values occupies 2,048 bytes and needs one 4-byte column index, an overhead of about 0.2%. At 50% block density, the BCSR matrix is close to half the size of the dense matrix.

Multiplying a BCSR matrix

A product of a BCSR matrix with a dense matrix walks the stored blocks one block row at a time:

# A is BCSR with shape [M, K] and block size [BR, BK].
# x is dense with shape [K, N], and out is dense with shape [M, N].
# M is a multiple of BR and K is a multiple of BK.

for i in range(M // BR):                      # block row of A
    for ptr in range(A.row_ptr[i], A.row_ptr[i + 1]):
        j = A.col_indices[ptr]                # block column of A
        block = A.values[ptr]                 # dense [BR, BK] block
        out[i*BR:(i+1)*BR, :] += block @ x[j*BK:(j+1)*BK, :]

Each step of the inner loop is a dense multiplication of one block with a slice of x. The work is proportional to the number of stored blocks, so empty blocks cost nothing. Zeros inside a stored block are still multiplied.

The block size is chosen to match the hardware. A block that matches the tile size of the matrix multiply unit keeps the unit fully used. A smaller block leaves part of the unit idle on each operation. A larger block wastes storage and computation on the zeros it contains.

Where block sparsity appears

Block-sparse attention. Attention patterns such as sliding windows and fixed global tokens leave whole blocks of the attention matrix empty, so the attention computation maps onto block-sparse products.

Structured pruning. Pruning that removes whole blocks of weights produces matrices that BCSR stores without waste.

Mixture-of-experts. MegaBlocks (Gale et al., 2022) expresses the expert computation in an MoE layer as block-sparse matrix products. Each expert multiplies only the tokens routed to it, and the number of tokens per expert can vary without padding or dropping tokens. MegaBlocks extends BCSR with explicit row indices for each block, which it calls blocked-CSR-COO, and with transpose indices that let a kernel walk the matrix in column order without copying it. The backward pass needs that column-order access for products with the transposed matrix.

Limits of the format

BCSR saves storage and computation in proportion to the fraction of empty blocks. It does not help when the non-zero values are scattered so that almost every block contains at least one of them. In that case BCSR stores nearly every block, and the work matches a dense multiplication.

In 2:4 sparsity, at most two values in every group of four consecutive values are non-zero. NVIDIA sparse tensor cores store the non-zero values together with a small amount of metadata that records their positions within each group. BCSR would store almost every block of such a matrix.


If you found this useful, please cite this post using

Senthilkumar Gopal. (Oct 2024). BCSR Format for Block-Sparse Matrices. sengopal.me. https://sengopal.me/posts/bcsr-format-for-block-sparse-matrices

or

@article{gopal2024bcsrformatblocksparse,
  title   = {BCSR Format for Block-Sparse Matrices},
  author  = {Senthilkumar Gopal},
  journal = {sengopal.me},
  year    = {2024},
  month   = {Oct},
  url     = {https://sengopal.me/posts/bcsr-format-for-block-sparse-matrices}
}