Hybrid Compression for Memory-Efficient Matrix-Vector Multiplication
Authors: Felipe A. Louza, Giovanni Manzini, Guilherme P. Telles
Organizations: Faculdade de Engenharia Elétrica, Universidade Federal de Uberlândia, Uberlândia, MG, Brazil · Instituto de Computação, Universidade Estadual de Campinas, Campinas, SP, Brazil · Department of Computer Science, University of Pisa, Pisa, Italy
Iterative machine-learning algorithms repeatedly access large real-valued matrices through matrix-vector products, and their performance is limited by memory bandwidth rather than arithmetic throughput. Lossless compression schemes that support computation directly over the compressed data reduce both the data moved and the memory footprint of these workloads. The mm-RePair framework [11] follows this approach by applying the RePair grammar compressor to the Compressed Sparse Row/Value (CSRV) representation of a matrix and performing matrix-vector multiplications directly on the compressed data. Its effectiveness, however, degrades on matrices that are dense or contain many distinct values. In this paper we present a hybrid strategy for the mm-RePair framework that combines grammar and entropy compression. The CSRV sequence is partitioned into a grammar-compressible component, formed by the entries that mm-RePair can compress effectively, and an entropy-compressible component holding the remaining entries, which is encoded with the ans-fold coder [24]. Compression improves further by sorting and delta encoding the entries of each row in the CSRV representation, and by remapping the alphabet to a contiguous range. Experiments on real-valued machine-learning matrices, including word embeddings and transformer hidden states, show that our hybrid strategy is more compact than the original \mmRepair on every input and reduces the peak memory of multiplication, with the largest gains on dense matrices with many distinct values, at competitive multiplication times. It also outperforms the column-reordering variant of \mmRepair on most of the matrices, and combining the two is even more compact on the sparse ones, at the cost of a much longer compression time. Finally, our strategy scales with the number of threads as well as the original \mmRepair does.
Figures & tables
Figure 1: A matrix M (left) and its CSRV representation (V,S) (right). The array V holds the distinct nonzero values of M , and S is a single sequence of t+n symbols, obtained by scanning M row by row: each nonzero M[i,j] becomes the pair ⟨ℓ,j⟩ with V[ℓ]=M[i,j] , and the delimiter \$$ closes each row. In Sthepair\langle 2,5\rangleofthesecondrowdenotesthevalueV[2]=6.0incolumn5,whilethesamevalueincolumn4ofthefirstrowisthedifferentpair\langle 2,4\rangle$ , since a pair identifies a value in a given column.
Figure 2: The mm-RePair representation of the matrix of Figure 1 . The top part shows the integer sequence Sint with m=6 , obtained by encoding \$$ as 0andeachpair\langle\ell,j\rangleas\ell m+j+1,andthenumbersabovethecellsarepositions.Thebottompartshowstheruleset\mathcal{R}andthefinalsequence\mathcal{C}producedbyRePair.Sincenorulemaycontain0,everydelimiterremainsin\mathcal{C},whichalsocontainsterminals,suchas10inthefirstrowand1$ in the second.
Figure 3: Compression pipeline of mm-RePair in its re-ans variant. The matrix is converted into its CSRV representation (V,S) (step 1), the sequence S is encoded as the integer sequence Sint (step 2), Sint is compressed with RePair , modified so that no rule contains the delimiter 0 , into the rule set R and the final sequence C (step 3), and R is stored as a packed array while C is entropy coded with ans-fold (step 4). The compressed representation is the triple (R,C,V) , with V stored uncompressed.
Figure 4: Overview of the partitioning scheme. The integer sequence Sint is partitioned into two components: SA is compressed with RePair as in mm-RePair , and SB is encoded with ans-fold , while V is stored unchanged.
Figure 5: Compression pipeline of hybrid mm-RePair . The matrix is converted into its CSRV representation (V,S) (step 1), and S is encoded as the integer sequence Sint (step 2). When criterion ( 1 ) holds, the identifiers of Sint are replaced by their ranks and the reverse mapping wcode is produced (step 3). The remapping is computed once on the whole sequence, so a single wcode serves both components, and it does not affect the partition, which depends only on digram frequencies. The sequence is partitioned into SA and SB (step 4), and SA is compressed with RePair into RA and CA (step 5). The rows of CA and of SB are sorted and delta encoded ( ole ) and entropy coded with ans-fold , as is wcode , while RA is stored as a packed array (step 6). The five blocks on the right form the compressed representation (RA,CA,SB,V) , plus wcode .
Figure 6: The compression pipeline of hybrid mm-RePair on the matrix of Figure 1 . To keep the example small, an incident digram occurring twice is enough here to keep an entry in SA . The CSRV sequence is encoded as Sint (steps 1–2). Criterion ( 1 ) holds, since ∣Sint∣=36 , ∣Σ∣=12 and b=4 give 36⋅28>32⋅12 , producing wcode (step 3). The remapped sequence is partitioned into SA and SB (step 4), SA is compressed with RePair (step 5), and the rows of SB are sorted and delta encoded (step 6).
Matrix
Dimensions
Density
CSRV
Partition
Rows
Cols
Nonzeros
#distinct
maxval
∣Σ∣
SB
airline78
14,462,943
29
72.66%
7,794
226,007
29,967
4.5%
census
2,458,285
68
43.03%
45
3,042
331
3.2%
covtype
581,012
54
22.00%
6,682
360,779
15,952
13.7%
imagenet
1,262,102
900
30.99%
824
741,448
48,772
0.5%
mnist2m
2,000,000
784
25.25%
255
199,915
181,260
2.2%
Table 1: Matrices used in our experiments. The column nonzeros reports the percentage of nonzero entries and #distinct the number of distinct nonzero values. The CSRV columns report maxval , the largest identifier in Σ , and the alphabet size ∣Σ∣ . The last column reports the fraction of S assigned to SB by re-hybd .
Matrix
Baseline
mm-RePair
gzip
xz
re-ans
re-pc
re-hybd
re-hybd-pc
airline78
13.27%
7.14%
9.27%
8.34%
8.75%
7.25%
census
5.54%
2.74%
1.53%
1.30%
1.50%
1.27%
covtype
6.25%
3.39%
3.87%
3.26%
3.38%
2.88%
imagenet
5.52%
3.57%
4.08%
3.81%
3.39%
3.18%
mnist2m
6.46%
4.12%
5.33%
5.31%
4.48%
4.48%
Table 2: Compression ratios, expressed as percentages of the size of the original dense matrix. Best value per row in bold.
Figure 7: Relative improvement in compression ratio of re-pc , re-hybd and re-hybd-pc over re-ans , computed as (co−cr)/co , where co and cr are the compression ratios of re-ans and of the compared method.
Matrix
Baseline
mm-RePair
gzip
xz
re-ans
re-pc
re-hybd
re-hybd-pc
airline78
92.10
1,852.06
375.81
4,340.11
1,062.10
4,874.00
census
25.36
290.50
40.30
3,403.37
86.00
3,496.71
covtype
3.07
58.71
5.93
455.31
52.47
473.89
imagenet
180.83
2,062.24
562.73
283,583.64
1,267.62
280,717.79
mnist2m
156.60
3,154.99
401.40
341,737.83
2,601.48
339,112.74
Table 3: Compression times in seconds.
Matrix
re-ans
One technique
Two techniques
re-hybd
split
ole
map
split+ole
split+map
ole+map
airline78
9.27%
9.27%
8.97%
8.94%
8.97%
8.94%
8.75%
8.75%
census
1.53%
1.53%
1.50%
1.53%
1.50%
1.53%
1.49%
1.50%
covtype
3.87%
3.85%
3.71%
3.56%
3.75%
3.49%
3.41%
3.38%
imagenet
4.08%
4.06%
3.40%
4.06%
3.40%
4.06%
3.39%
3.39%
mnist2m
5.33%
5.31%
4.49%
5.32%
4.49%
5.30%
4.48%
4.48%
Table 4: Compression ratio (%) of each technique. split denotes the hybrid partitioning into SA and SB , ole the row ordering and delta encoding, and map the alphabet remapping. re-hybd is the combination of all three. Values marked † were obtained by forcing the remapping on glove and susy , where criterion ( 1 ) rejects it, so they are not configurations that re-hybd uses. Best value per row in bold.
Figure 8: Relative improvement in compression ratio over re-ans of each configuration of Table 4 , computed as in Figure 7 . Hatched bars are the configurations in which the remapping is forced against criterion ( 1 ).
Matrix
Peak memory
Reduction w.r.t. re-ans
re-ans
re-pc
re-hybd
re-hybd-pc
re-pc
re-hybd
re-hybd-pc
airline78
15.39%
13.59%
14.83%
12.03%
11.7%
3.6%
21.8%
census
4.12%
3.76%
3.96%
3.61%
8.6%
3.9%
12.4%
covtype
10.03%
8.98%
8.56%
7.94%
10.4%
14.7%
20.8%
imagenet
5.20%
4.88%
4.49%
4.22%
6.3%
13.7%
18.8%
mnist2m
6.85%
6.83%
5.96%
5.96%
0.2%
13.0%
13.0%
Table 5: Peak memory during matrix–vector multiplication, as a percentage of the size of the original dense matrix, and relative reduction with respect to re-ans , computed as (po−pr)/po where po and pr are the peak memory of re-ans and of the compared method. Best value per row in bold.
Matrix
Running time (s)
Speedup w.r.t. re-ans
re-ans
re-pc
re-hybd
re-hybd-pc
re-pc
re-hybd
re-hybd-pc
airline78
2.84
2.54
3.56
2.46
1.12 ×
0.80 ×
1.15 ×
census
0.17
0.15
0.24
0.20
1.13 ×
0.71 ×
0.85 ×
covtype
0.06
0.05
0.09
0.08
1.20 ×
0.67 ×
0.75 ×
imagenet
3.18
2.86
3.29
2.60
1.11 ×
0.97 ×
1.22 ×
mnist2m
6.40
6.00
6.28
5.71
1.07 ×
1.02 ×
1.12 ×
Table 6: Average running time per iteration of matrix–vector multiplication, in seconds, and speedup with respect to re-ans , where speedups below 1× indicate a slowdown. Best value per row in bold.
Matrix
Levels
Dimensions
Density
CSRV
Partition
Rows
Cols
Nonzeros
#distinct
maxval
∣Σ∣
SB
glove.q64
64
400,000
300
100.00%
64
18,613
12,590
0.4%
glove.q256
256
400,000
300
100.00%
239
71,413
45,853
0.6%
glove.q1k
1,024
400,000
300
100.00%
892
267,420
164,789
4.9%
glove.q4k
4,096
400,000
300
100.00%
3,294
988,031
583,634
74.1%
glove.q16k
16,384
400,000
300
100.00%
11,840
3,551,977
2,025,863
99.5%
Table 7: glove and its quantized variants, sorted by number of distinct nonzero values, with the columns of Table 1 . “Levels” is the number of quantization levels. The last column reports the fraction of S assigned to SB by re-hybd , with a single block.
Figure 9: Compression ratio, peak memory and running time of re-ans , re-pc , re-hybd and re-hybd-pc on glove as a function of the number of distinct nonzero values (logarithmic scale), from the 64 values of glove.q64 to the 717,119 values of the original matrix. The column-reordering variants are drawn with dashed lines and cross markers, so that they remain visible where they coincide with re-ans and re-hybd .
Figure 10: Compressed size of the matrices of Table 1 when the input is divided into p row blocks, relative to the single-threaded representation, for re-ans , re-pc , re-hybd and re-hybd-pc .
Figure 11: Peak memory during matrix–vector multiplication with p threads, relative to the single-threaded execution, for re-ans , re-pc , re-hybd and re-hybd-pc .
Figure 12: Running time of matrix–vector multiplication with p threads, relative to the single-threaded execution, for re-ans , re-pc , re-hybd and re-hybd-pc .
Matrix
1 thread
2 threads
4 threads
8 threads
16 threads
SA
SB
SA
SB
SA
SB
SA
SB
SA
SB
airline78
95.54%
4.46%
95.40%
4.60%
95.07%
4.93%
94.37%
5.63%
93.25%
6.75%
census
96.80%
3.20%
96.80%
3.20%
96.80%
3.20%
96.80%
3.20%
96.80%
3.20%
covtype
86.29%
13.71%
82.86%
17.14%
79.24%
20.76%
75.91%
24.09%
71.78%
28.22%
imagenet
99.55%
0.45%
99.49%
0.51%
99.39%
0.61%
99.21%
0.79%
98.92%
1.08%
mnist2m
97.81%
2.19%
95.43%
4.57%
90.30%
9.70%
82.19%
17.81%
72.97%
27.03%
Table 8: Fraction of the non-delimiter symbols assigned to SA and SB by the partitioning step for 1, 2, 4, 8 and 16 threads. Fractions of SB above 50% in bold.