DKM: D IFFERENTIABLE k -M EANS C LUSTERING L AYER FOR N EURAL N ETWORK C OMPRESSION
Mohammad Rastegari
A BSTRACT
Deep neural network (DNN) model compression for efﬁcient on-device inference becomes increasingly important to reduce memory requirements and keep user data on-device.
To this end, we propose a novel differentiable k -means clustering layer (DKM) and its application to train-time weight-clustering for DNN model compression.
DKM casts k -means clustering as an attention problem and enables joint optimization of the DNN parameters and clustering centroids.
Unlike prior works that rely on additional parameters and regularizers, DKM-based compression keeps the original loss function and model architecture ﬁxed.
We evaluated DKM-based compression on various DNN models for computer vision and natural language processing (NLP) tasks.
Our results demonstrate that DKM delivers superior compression and accuracy trade-off on ImageNet1k and GLUE benchmarks.
For example, DKM-based compression can offer 74.5% top-1 ImageNet1k accuracy on ResNet50 with 3.3MB model size ( 29.4 x model compression factor).
For MobileNet-v1, which is a challenging DNN to compress, DKM delivers 63.9% top-1 ImageNet1k accuracy with 0.72 MB model size ( 22.4x model compression factor).
This result is 6.8% higher top-1 accuracy and 33% relatively smaller model size than the current state-of-the-art DNN compression algorithms.
DKM also compressed a DistilBERT model by 11.8x with minimal (1.1%) accuracy loss on GLUE NLP benchmarks
I NTRODUCTION
Deep neural networks (DNN) have demonstrated super-human performance on many cognitive tasks (Silver et al., 2018).
While a fully-trained uncompressed DNN is commonly used for server-side inference, on-device inference is preferred to enhance user experience by reducing latency and keeping user data on-device.
Many such on-device platforms are battery-powered and resource-constrained, demanding a DNN to meet the stringent resource requirements such as powerconsumption, compute budget and storage-overhead (Wang et al., 2019b; Wu et al., 2018).
One solution is to design a more efﬁcient and compact DNN such as MobileNet (Howard et al., 2017) by innovating the network architecture or by leveraging Neural Architecture Search (NAS) methods (Liu et al., 2019; Tan et al., 2019).
Another solution is to compress a model with small accuracy degradation so that it takes less storage and reduces System-on-Chip (SoC) memory bandwidth utilization, which can minimize power-consumption and latency.
To this end, various DNN compression techniques have been proposed (Wang et al., 2019b;
Dong et al., 2020; Park et al., 2018; Rastegari et al., 2016; Fan et al., 2021; Stock et al., 2020;
Zhou et al., 2019; Park et al., 2019;
Yu et al., 2018; Polino et al., 2018).
Among them, weight-clustering/sharing (Han et al., 2016;
Wu et al., 2018; Ullrich et al., 2017; Stock et al., 2020) has shown a high DNN compression ratio where weights are clustered into a few shareable weight values (or centroids) based on k -means clustering.
Once weights are clustered, to shrink the model size, one can store indices (2bits, 4bits, etc.
depending on the number of clusters) with a lookup table rather than actual ﬂoating-point values.
Designing a compact DNN architecture and enabling weight-clustering together could provide the best solution in terms of efﬁcient on-device inference.
However, the existing model compression approaches do not usefully compress an already-compact DNN like MobileNet, presumably because the model itself does not have signiﬁcant redundancy.
We conjecture that such limitation comes from the fact that weight-clustering through k -means
algorithm (both weight-cluster assignment and weight update) has not been fully optimized with the target task.
The fundamental complexity in applying k -means clustering for weight-sharing comes from the following: a) both weights and corresponding k-means centroids are free to move (a general k -means clustering with ﬁxed observations is already NP-Hard), b) the weight-to-cluster assignment is a discrete process which makes k -means clustering non-differentiable, preventing effective optimization.
In this work, we propose a new layer without learnable parameters for differentiable k -means clustering, DKM, based on an attention mechanism (Bahdana et al., 2015) to capture the weight and cluster interactions seamlessly, and further apply it to enable train-time weight-clustering for model compression.
Our major contributions include the following:
• We propose a novel differentiable k -means clustering layer (DKM) for deep learning, which serves as a generic neural layer to develop clustering behavior on input and output.
• We demonstrate that DKM can perform multi-dimensional k -means clustering efﬁciently and can offer a high-quality model for a given compression ratio target.
• We apply DKM to compress a DNN model and demonstrate the state-of-the-art results on both computer vision and natural language models and tasks.
R ELATED W ORKS
Model compression using clustering : DeepCompression (Han et al., 2016) proposed to apply kmeans clustering for model compression.
DeepCompression initially clusters the weights using kmeans algorithm.
All the weights that belong to the same cluster share the same weight value which is initially the cluster centroid.
In the forward-pass, the shared weight is used for each weight.
In the backward-pass, the gradient for each shared weight is calculated and used to update the shared value.
This approach might degrade model quality because it cannot formulate weight-cluster assignment during gradient back propagation (Yin et al., 2019).
ESCQ (Choi et al., 2017; 2020) is optimizing the clusters to minimize the change in the loss by considering hessian.
Therefore, it is to preserve the current model state, instead of searching for a fundamentally better model state for compression.
HAQ (Wang et al., 2019b) uses reinforcement learning to search for the optimal quantization policy on different tasks.
For model compression, HAQ uses k -means clustering similar to DeepCompression yet with ﬂexible bit-width on different layers.
Our work is orthogonal to this work because the k -means clustering can be replaced with our DKM with a similar ﬂexible conﬁguration. "
And The
Bit Goes Down" (Stock et al., 2020) algorithm is based on Product Quantization and Knowledge
Distillation.
It evenly splits the weight vector of N elements into N/d contiguous d dimensional sub-vectors, and clusters the sub-vectors using weighted k -means clustering to minimize activation change from that of a teacher network.
GOBO
(Zadeh et al., 2020)
ﬁrst separates outlier weights far from the average of the weights of each layer and stores them uncompressed while clustering the other weights by an algorithm similar to k -means.
Model compression using regularization : Directly incorporating k -means clustering in the training process is not straightforward (Wu et al., 2018).
Hence, (Ullrich et al., 2017) models weightclustering as Gaussian Mixture Model (GMM) and ﬁts weight distribution into GMM with additional learning parameters using KL divergence (i.e., forcing weight distribution to follow k Gaussian distributions with a slight variance).
(Wu et al., 2018) proposed deep k -means to enable weightclustering during re-training.
By forcing the weights that have been already clustered to stay around the assigned center, the hard weight-clustering is approximated with additional parameters.
Both (Ullrich et al., 2017) and (Wu et al., 2018) leverage regularization to enforce weight-clustering with additional parameters, which will interfere with the original loss target and requires additional updates for the new variables (i.e., singular value decomposition (SVD) in (Wu et al., 2018)).
Also, relying on the modiﬁed loss cannot capture the dynamic interaction between weight distributions and cluster centroids within a batch, thus requiring an additional training ﬂow for re-training.
Enhance Model compression using dropout : Quant-Noise (Fan et al., 2021) is a structured dropout which only quantizes a random subset of weights (using any quantization technique) and thus can improve the predictive power of a compressed model.
For example, (Fan et al., 2021) showed good compression vs. accuracy trade-off on ResNet50 for ImageNet1k.
Model quantization : Besides clustering and regularization methods, model quantization can also reduce the model size, and training-time quantization techniques have been developed to improve the accuracy of quantized models (Li et al., 2019;
Zhao et al., 2019).
EWGS (J. Lee, 2021) adjusts gradients by scaling them up or down based on the Hessian approximation for each layer.
PROFIT (Park & Yoo, 2020) adopts an iterative process and freezes layers based on the activation instability.
Efﬁcient networks :
Memory-efﬁcient DNNs include MobileNet (Howard et al., 2017;
Sandler et al., 2018), EfﬁcientNet (Tan & Le, 2019; 2021) and ESPNet (Mehta et al., 2019).
MobileNet-v1 (Howard et al., 2017) on ImageNet1k dataset has top-1 accuracy of 70.3% with 16.
MB of memory in comparison to a ResNet18 which has 69.3% accuracy with 44.6 MB of model size.
Our method can be applied to these compact networks to reduce their model sizes further.
A LGORITHM 3. M OTIVATION
Popular weight-clustering techniques for DNN model compression (J. Lee, 2021; Han et al., 2016;
Dong et al., 2020; Stock et al., 2020) are based on k -means clustering along with enhancements such as gradient scaling/approximation.
Using k -means clustering, the weights are clustered and assigned to the nearest centroids which are used for forward/backward-propagation during training as illustrated in Fig.
1 (a).
Such conventional methods with clustering have two critical drawbacks:
• The weight-to-cluster assignment in conventional approaches is not optimized through back-propagation of training loss function.
• Gradients for the weights are computed in an ad-hoc fashion: the gradient of a centroid is re-purposed as the gradient of the weights assigned to the centroid.
These limitations are more pronounced for the weights on the boundary such as i and j in Fig.
1 (a).
In the conventional approaches, i and j are assigned to the centroids C 2 and C 1 respectively, simply because of their marginal difference in a distance metric.
However, assigning i to C 0 and j to Ccould be better for the training loss as their difference in distance is so small (Nagel et al., 2020). [Figure] [Equation] [Figure]
[Equation] [Figure] [Equation] [Figure]
[Equation]
[Figure]
Such lost opportunity cost is especially higher with a smaller number of centroids (or fewer bits for quantization), as each unfortunate hard assignment can degrade the training loss signiﬁcantly.
We overcome such limitations with DKM by interpreting weight-centroid assignment as distancebased attention optimization (Bahdana et al., 2015) as in Fig.
1 (b) and letting each weight interact with all the centroids.
Such attention mechanism naturally cast differentiable and iterative k -means clustering into a parameter-free layer as in Fig.
Therefore, during backward-propagation, attention allows a gradient of a weight to be a product of the attentions and the gradients of centroids, which in turn impact how the clustering and assignment will be done in the next batch.
overall our weight assignment will align with the loss function, and can be highly effective for DNN compression.
3. D IFFERENTIABLE K- MEANS CLUSTERING LAYER FOR W EIGHT - CLUSTERING
DKM can perform a differentiable train-time weight-clustering iteratively for k clusters as shown in Fig.
2 for the DNN model compression purpose.
Let C ∈ R
k be a vector of cluster centers and W ∈ R N be a vector of the weights, and then DKM performs as follows:
• In the ﬁrst iteration C can be initialized either by randomly selected k weights from W or k -means++.
For all subsequent iterations, the last known C from the previous batch is used to accelerate the clustering convergence.
• A distance matrix D is computed for every pair between a weight w i and a centroid c j using a differentiable metric f
(i.e., the Euclidean distance) using d ij = − f ( w i , c j ) .
• We apply softmax with a temperature τ on each row of D to obtain attention matrix A where a i,j = exp ( di,jτ ) P k exp ( di,kτ ) represents the attention from w i and
c j . •
Then, we obtain a centroid candidate, ˜C by gathering all the attentions from W for each centroid by computing ˜ c
j = P i a
i,j w i P i a i,j and update C with ˜C if the iteration continues.
• We repeat this process till | C − ˜C | ≤ ǫ at which point k -means has converged or the iteration limit reached, and we compute AC to get ˜W for forward-propagation (as in Fig. 2).
The iterative process will be dynamically executed imperatively in PyTorch (Paszke et al., 2019) and Tensorﬂow-Eager (Agrawal et al., 2019) and is differentiable for backward-propagation, as ˜W is based on the attention between weights and centroids.
DKM uses soft weight-cluster assignment which could be hardened in order to impose weight-clustering constraints.
The level of hardness can be controlled by the temperature τ in the softmax operation.
During inference we use the last attention matrix (i.e., A in Fig. 2) from a DKM layer to snap each weight to the closest centroid of the layer and ﬁnalize weight-clustering as in prior arts (i.e., no more attention), but such assignment is expected to be tightly aligned with the loss function, as the weights have been annealed by shuttling among centroids.
A theoretical interpretation of DKM is described in Appendix G.
Using DKM for model compression allows a weight to change its cluster assignment during training, but eventually encourages it to settle with the best one w.r.t the task loss.
Optimizing both weights and clusters simultaneously and channeling the loss directly to the weight-cluster assignment is by the attention mechanism.
Since DKM is without additional learnable parameters and transparent to a model and loss function, we can reuse the existing training ﬂow and hyper-parameters.
The key differences between DKM-based compression and the prior works can be summarized as follows:
• Instead of hard weight-cluster assignment and approximated gradient (Han et al., 2016; Wang et al., 2019b; J. Lee, 2021; Stock et al., 2020), DKM uses ﬂexible and differentiable attention-based weight-clustering and computes gradients w.r.t the task loss.
• Instead of modifying the loss function with regularizers to enforce clustering (Ullrich et al., 2017; Wu et al., 2018), DKM can be inserted into forward pass, making the optimization fully aligned with the task objective (i
e., no interference in loss).
• DKM requires no additional learnable parameters (Ullrich et al., 2017; J. Lee, 2021), thus making the training ﬂow simple.
For example, DKM-base approach does not need to substitute a convolution layer with a specialized version with additional learning parameters.
• DKM requires no additional computation such as Hessian trace (Dong et al., 2020; Choi et al., 2017) or SVD (J. Lee, 2021; Wu et al., 2018) for gradient approximation, because DKM uses a differentiable process.
• DKM-based compression does not require a complex training techniques such as freezing/progress (Park & Yoo, 2020) or distillation (Stock et al., 2020).
3. M ULTI -D IMENSIONAL DKM
DKM can be naturally extended into multi-dimensional weight-clustering (Stock et al., 2020) due to its simplicity, and is highly effective due to its differentiability.
We split N elements of weights into
Nd contiguous d dimensional sub-vectors and cluster the sub-vectors ( W ∈ R Nd ∗ d ).
For example, we simply ﬂatten all the convolutional kernels into a ( Nd , d ) matrix across both kernel and channel boundaries and apply multi-dimensional DKM to the matrix for clustering in our implementation as in Fig.
Accordingly, the cluster centroids will become d -dimensional as well ( C ∈ R k ∗ d ) and the metric calculation is done in the d -dimensional space.
With the multi-dimensional scheme, the effective bit-per-weight becomes bd for b -bit/ d -dim clustering.
The memory complexity of a DKM layer with N parameters is O ( r Nd 2 b ) where r is the number of iterations per Fig.
2 (i.e., all the intermediate results such as D and A at each iteration need to be kept for backward-propagation).
Such multi-dimensional clustering could be ineffective for conventional methods (i.e., DNN training not converging)
(Stock et al., 2020; Wang et al., 2019b; J. Lee, 2021), as now a weight might be on the boundary to multiple centroids, and the chance of making wrong decisions grows exponentially with the number of centroids.
For example, there are only two centroids for 1bit/1dim clustering, while there are 16 centroids in 4bit/4dim clustering, although both have the same effective bit-perweight .
Intuitively, however, DKM can work well with such multi-dimensional conﬁgurations as
DKM naturally optimizes the assignment w.r.t the task objective and can even recover from a wrong assignment decision over the training-time optimization process.
compression over 32bit
[Equation]
probability of using c
[Equation]
[Figure]
ResNet (
[Figure]
ResNet
Base
DC
HAQ
ATB ATB
DKM Model
Metrics 32bit 2bit ﬂex small large conﬁguration b/w ∗
ResNet
Top-1 (%) 69.
65.1 X cv † :5/8 § 0.717 Size (MB) 44.
fc ‡ : 8 /
ResNet
Top-1 (%) 76.
cv: 6 /1.077 74.3 △ 68.8 △
Size (MB) 97.
fc: 6 /
MobileNet
Top-1 (%) 70.
nc ◦ nc 63.
cv: 4 /1.427 v
Size (MB) 16.
fc: 4 /
MobileNet
Top-1 (%) 71.
nc nc 68.
cv: 2 /2.010 v
Size (MB) 13.
fc: 4 /∗ effective bit-per-weight (see Section 3.3); ◦ not converging † the convolution layers ; ‡ the last fully connected layer; § clustering with 6 bits and 8 dimensions; △ ATB with quantization-noise (Fan et al., 2021)
X also, 65.7% Top-1 accuracy and 1.09 MB with cv:6/8, fc:6/10 66.7% Top-1 accuracy and 1.49 MB with cv:4/4, fc:8/ The key beneﬁt of multi-dimensional DKM is captured in Fig. For a given N ≫ d in 32 bits, the compression ratio is d 32 b (i.e., a function of both d and b ).
Assuming the number of sub-vectors assigned to each centroid is same, the entropy of W is simply b .
Since higher entropy in the weight distribution indicates larger learning capacity and better model quality (Park et al., 2017), increasing b and d at the same ratio as much as possible may improve the model quality for a given target compression ratio (see Section 4.1 for results).
However, making b and d extremely large to maximize the entropy of W might be impractical, as the memory overhead grows exponentially with b .
E XPERIMENTAL R ESULTS
We compared our DKM-based compression with state-of-the-art quantization or compression schemes on various computer vision and natural language models.
To study the trade-off between model compression and accuracy, we disabled activation quantization in every experiment for all approaches, as our main goal is the model compression.
All our experiments with DKM were done on two x86 Linux machine with eight NVIDIA V100 GPUs each in a public cloud infrastructure.
We used a SGD optimizer with momentum 0.9, and ﬁxed the learning rate at 0.008 (without individual hyper-parameter tuning) for all the experiments for DKM.
Each compression scheme starts with publicly available pre-trained models.
The ǫ is set as 1 e– 4 and the iteration limit is 5.
4. I MAGE N ET 1 K
We compared our DKM-based compression with prior arts: DeepCompression (or DC ) (Han et al., 2016), HAQ (Wang et al., 2019b), and "And The Bit Goes Down" (or ATB ) (Stock et al., 2020) com-
[Table]
[Table]
[Table] [Table]
bined with Quantization-noise (Fan et al., 2021), ABC-Net (Lin et al., 2017), BWN (Rastegari et al., 2016), LR-Net (Shayer et al., 2018), and Meta-Quant (Chen et al., 2019). We also compared with
GBS which uses the same ﬂow as DKM except that Gumbel-softmax is used to generate stochastic soft-assignment as attention (Jang et al., 2017).
In the GBS implementation, we iteratively perform drawing to mitigate the large variance problem reported in (Shayer et al., 2018).
We set the mini-batch size 128 per GPU (i.e., global mini-batch size of 2048) and ran forepochs for all DKM cases.
Since the public ATB implementation does not include MobileNet-v1/v cases (Howard et al., 2017; Sandler et al., 2018), we added the support for these two by following the paper and the existing ResNet18/
(He et al., 2016) implementations.
Instead of using a complex
RL technique as in HAQ (Wang et al., 2019b), for DKM experiments, we ﬁxed conﬁgurations for all the convolution layers (noted as cv) and the last fully connected layer (noted as fc), except that we applied 8 bit clustering to a layer with fewer than 10,000 parameters.
Using DKM layer, our compression method offers a Pareto superiority to other schemes as visualized in Fig.
For ResNet50 and MobileNet-v1/v2, DKM delivered compression conﬁgurations that yielded both better accuracy and higher compression ratio than the prior arts.
For ResNet18, DKM was able to make a smooth trade-off on accuracy vs. compression, and ﬁnd Pareto superior conﬁgurations to all others: DKM can get 65.1% Top-1 accuracy with 1MB which is superior to ATB-large, 66.8% Top-1 accuracy with 1.49 MB which is superior to ATB-small, and 65.8% Top-1 accuracy with 1.09 MB as a superior balance point.
For MobileNet-v1/v2, ATB failed to converge, but DKM outperforms DC and HAQ in terms of both accuracy and size at the same time.
For ATB cases, adding quantization noise improves the model accuracy (Fan et al., 2021) only moderately.
GBS in fact shows better performance than ATB, but still worse than the proposed method, even after reducing variance through iteration, especially for the high compression conﬁgurations.
For ResNet18,
GBS with the same bit/dimension targets delivered the following Top-1 accuracies ranging from 61.7% to 65.7%.
For details, please refer to Appendix F.
We also compared DKM with a well-known regularization-based clustering method on GoolgeNet in Table 2, RPS (Wu et al., 2018) which has demonstrated superior performance to another regularization approach (Ullrich et al., 2017).
Note that only convolution layers are compressed, following
[Table] [Table]
the setup in RPS (Wu et al., 2018). Table 2 clearly indicates that DKM can allow both much better compression and higher accuracy than RPS even with 1 bit-per-weight.
We also compared our DKM-based algorithm with the latest scalar weight quantization approaches, PROFIT (Park & Yoo, 2020) and EWGS (J. Lee, 2021) (which have outperformed the prior arts in the low-precision regimes) by running their public codes on our environments with the recommended hyper-parameter sets.
Table 1 summarizes our comparison results on ResNet18, ResNet50, and MobileNet-v1/v2 for the ImageNet1k classiﬁcation task.
Following the experimental protocol in (Zhang et al., 2018; J. Lee, 2021; Rastegari et al., 2016), we did not compress the ﬁrst and last layers for all the experiments in Table 1.
It clearly shows that our approach with DKM can provide compression comparable to or better than other approaches, especially for the low-bit/highcompression regimes.
We denote clustering with b bits and d dimensions as b/d as it will assign bd bits in average to each weight, and the number of weight clusters is 2 b .
Especially with multi-dim clustering such as 4/4 or 8/8 bits, our DKM-based compression outperforms other schemes at 1 bit, while PROFIT cannot make training converge for MobileNet-v1/v2.
One notable result is 64.3% Top-1 accuracy of MobileNet-v1 with the 8/8 bit conﬁguration (which is 1 bit-equivalent).
DKM with 8/16 bits (effectively 0.5 bit per weight) shows degradation from the 8/8 bit conﬁguration, but still retains a good accuracy level.
We also tried PROFIT+EWGS as proposed in (J. Lee, 2021), which showed good results on MobileNet-v1/v2 for 3 bits but failed to converge for 2 and 1 bits.
With the overall compression ratio (or bit-per-weight) ﬁxed, our experiments with DKM conﬁrm that a higher d can yield a better quality training result.
For the example of MobileNet-v2, DKM 8/16 yielded 24% better top-1 accuracy than DKM 4/8 although both have the same 12 bit-per-weight, and the same trend is observed in other models.
However, DKM 8/8 failed to train ResNet50 due to the memory limitation, while DKM 8/16 successfully trained the same model, because the larger dimension (i.e., 8 vs 16) reduces the memory requirement of the attention matrix as discussed in
Section 3.3.
For additional discussion, please refer to the Appendix A.
4. GLUE NLP B ENCHMARKS
We compared our compression by DKM with GOBO (Zadeh et al., 2020) and EWGS (J. Lee, 2021) for BERT models on NLP tasks from the GLUE benchmarks (Wang et al., 2019a), QNLI (Questionanswering NLI) and MNLI (Multi NLI).
We ﬁxed the learning rate as 1e-4 for all the experiments which worked best for EWGS, and all experiments used mini-batch size 64 per GPU (i.e., global mini-batch size of 1024) with the maximum seq-length 128.
[Table]
[Figure]
We compared our DKM-based compression against EWGS (J. Lee, 2021) on the QNLI dataset, and
Table 3 demonstrates that DKM offers better predictability across all the tested models (Lan et al., 2019; Sanh et al., 2019;
Turc et al., 2019; Sun et al., 2020) than EWGS.
Note that the embedding layers were excluded from compression in QNLI experiments.
As in ImageNet1k experiments, the 4/4 bit conﬁguration delivers better qualities than the 1 bit conﬁguration on all four BERT models, and especially performs well for the hard-to-compress MobileBERT.
Table 3 also indicates that different transformer architectures will have different levels of accuracy degradation for a given compression target.
For the example of 1 bit, MobileBERT degraded most due to many hard-tocompress small layers, yet recovered back to a good accuracy with DKM 4/4.
When DKM compared against GOBO (Zadeh et al., 2020) (which has outperformed the prior arts on BERT compression) on DistilBERT with the MNLI dataset, our results in Table 4 clearly show that DKM offers a better accuracy-compression trade-off than GOBO, and also enables ﬁne-grained balance control between an embedding layer and others: using 2.5 bits for Transformer and 3 bits for embedding is better than 2 bits for Transformer and 4 bits for embedding for DistilBERT.
4. DKM- BASED C OMPRESSION A NALYSIS
Since DKM-based compression uses attention-driven clustering as in Fig.
2 during training but snaps the weights to the nearest centroids, there is a gap between train-time and inference-time weights which in turn leads to accuracy difference between train and validation/test accuracies.
Therefore, we measured the Top-1 accuracies with both weights for the DKM cases from Fig.
4 as in Table 5.
We observed the accuracy drop is about 0.2% - 1.7% and gets larger with hard-to-compress DNNs.
Fig.
5 shows that our compression approach can offer a smooth convergence and gradual weightclustering based on task loss back-propagated through DKM layers, without any custom training ﬂow and extra regularization.
For additional details and results, please refer to Appendix C.
C ONCLUSION
In this work, we proposed a differentiable k -means clustering layer, DKM and its application to model compression.
DNN compression powered by DKM yields the state-of-the-art compression quality on popular computer vision and natural language models, and especially highlights its strength in low-precision compression and quantization.
The differentiable nature of DKM allows natural expansion to multi-dimensional k -means clustering, offering more than 22x model size reduction at 63.9% top-1 accuracy for highly challenging MobileNet-v1.
R EPRODUCIBILITY S TATEMENT
Our universal setup for experiments is disclosed in the ﬁrst paragraph of Section 4 and per-datasetsetups are also stated in the ﬁrst paragraphs of Sections 4.1 and 4.2 along with key hyper-parameters in Appendix.
For Imagenet1k we used a standard data augmentation techniques: RandomResizedCrop(224), RandomHorizontalFlip, and Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) as in other papers (Park & Yoo, 2020; J. Lee, 2021).
During evaluation, we used the following augmentations: Resize(256), CentorCrop(224), and Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]).
For GLUE benchmark, we used the default setup from HuggingFace
A A BLATION S TUDY : DKM S YSTEM P ERFORMANCE AND O VERHEAD
Table 6 shows the GPU memory utilization and per-epoch runtime for the DKM cases from Table 1.
While DKM layers have negligible impacts on training speed, the GPU memory utilization increases with more bits (i.e., more clusters) and with smaller dimensions, as discussed in Section 3.3: the memory complexity of a DKM layer with N parameters and d dimension is O ( r Nd 2 b ) where r is the number of iterations.
[Table]
Therefore, one straightforward way to reduce GPU usage and avoid the out-of-memory exception is to limit r .
In order to overcome the out-of-memory error for ResNet50 with DKM 8/8, we applied a constraint r < 3 to all the DKM layers, and could reach the top-1 accuracy of 74.0 % with the 98.2% GPU memory utilization.
We believe the fundamental solution to this bottleneck is to use a sparse representation for A by keeping top- k centroids for each weight which is one of our future works.
B A BLATION S TUDY : H YPER -P ARAMETER τ SEARCH
In the current DKM implementation, we use a global τ to control the level of softness in the attention matrix.
The selection of τ affects the model predictive power as shown in Fig.
6 where there appears to be an optimal τ for a given DNN architecture.
For examples of ResNet18/80, τ = 2 e − 5 is the best value for the 2 bit clustering.
In our experiments, we used a binary search to ﬁnd out the best τ values w.r.t.
the top-1 accuracy, which are listed in Table 7.
In general, one can observe that a complex compression task (i.e., higher
compression targets, more compact networks) tends require a larger τ to provide enough ﬂexibility or softness.
• For MobileNet-v1/v2, it requires about 10x larger τ values then for ResNet18/50, because they are based on a more compact architecture and harder to compress.
• When the number of bits decreases, the compression gets harder because there are fewer centroids to utilize, hence requiring a larger τ value.
• When the centroid dimension increases, the larger τ value is required, as the compression complexity increases (i.e., need to utilize a longer sequence).
[Table]
It could be possible to cast τ as a learnable parameter for each layer or apply some scheduling to improve the model accuracy further (as a future work), but still both approaches need a good initial point which can be found using a binary search technique.
For the BERT experiments with GLUE benchmarks
[Table]
we used the following τ regardless of the compression level: 5.0e-5 for ALBERT, 8.0e-5 for DistilBERT, 1.5e-4 for BERT-tiny, and 4.0e-4 for
MobileBERT.
We found the BERT models are less sensitive to the τ than ImageNet classiﬁers.
C T RAIN - TIME VS . I NFERENCE - TIME W EIGHT D IFFERENCE /E RROR
As discussed in Section 4.3, DKM-based compression requires snap the train-time weights to the nearest centroids for accurate validation or inference, which creates minor accuracy degradation during inference.
To understand the behavior better, we measured the Frobenius norm of the weight difference (i.e., torch.norm ( train _ weight − inference _ weight ) ) as an error for each layer for every batch from ResNet18 with training with DKM cv:6/8 and fc:6/10 (from Fig. 4) on ImageNet1k.
The error changes of the ﬁve representative layers for over 120,000 batches or 200 epochs are plotted in Fig.
7 from which we can make the following observations:
• Although every layer starts with a different level of error, the errors get smaller over the training time, and eventually at the scale of 1e– 4 .
• Aggressive compression makes a layer to begin with a high level of error.
For example, the last fc layer starts with an error of 4 (because it targets 0.6 bit per weight).
• the later layers get stabilized better than the earlier layers after enough epochs have been passed.
Our observations are aligned with Fig.
5 (b) in the sense that DKM will encourage weights to be clustered tightly over time, decreasing the difference between train-time and inference-time weights, thus can be very effective in compressing the model and minimizing the model accuracy degradation.
D T HE NUMBER OF ITERATIONS IN DKM L AYER
In a DKM layer, we iteratively perform k -means clustering using the attention mechanism until the clustering process is converged or the maximum iteration number is reached.
In our experiments in
[Figure]
Section 4, we set the maximum number of iteration as 5 to avoid the out-of-memory error.
In order to understand how many iterations are needed before each forward pass, we collected the iteration count per batch from each layer for the ﬁrst eight epochs (the trend holds true for the remaining epochs) from ResNet18 with training with DKM cv:6/8 and fc:6/10 (from Fig. 4) on ImageNet1k, and plotted the graphs in Fig. 8.
Our observations include the following:
[Figure]
• (a) shows the number of iteration changes over the 8 epochs across all the compressed layers.
As one can see, the number of iterations hits the maximum limit initially, implying that the weights are being clustered aggressively.
• As the training continues however, the number of iterations decreases slowly with sporadic spikes, implying DKM-based compression helps learn good weight sharing.
• While the convolutional layers in (b), (c), and (d) get stabilized after only a few dozens of batches, the last fc layer in (e) requires much more iterations throughout the training.
We partially believe that this is because we used the DKM 6/10 conﬁguration which is more challenging than the DKM 6/8 for the convolution layer.
E ǫ IN DKM
The ǫ in Fig.
2 determines when to exit the iterative process.
Note that the default value for the experiments in Section 4 is 1e– 4 based on from sklearn.cluster.
KMeans (https://scikitlearn.org/stable/modules/generated/sklearn.cluster.KMeans.html).
Therefore, we performed the sen- sitivity analysis by varying the ǫ from 1e– 2 to 1e– 7 for ResNet18 with DKM cv:6/8 and fc:6/10 on
ImageNet1k and increase the maximum iteration limit to 15 not to make it as a bottleneck.
From the results, we found that the ﬁnal Top-1 accuracies for all the cases were in the range of 65.7 -65.9%.
To understand why and develop insights, we plotted the train-time vs. inference-time weight difference of the fc layer in Fig.
9 as similarly with Fig.
7 (d).
From the Fig.
9, we could make the following observations:
• With a large ǫ , there is a wider gap between training and inference weights, implying the clustering is not fully optimized.
For example, the initial error with ǫ = 1e– 2 is about 2x larger than one with ǫ = 1e– 6 .
However, the gap is closing over time, and after a sufﬁcient number of epochs, the errors are all in the similar range of 1e– 4 regardless of the ǫ value.
Larger ǫ values make DKM layers iterate fewer, decreasing the peak memory consumption.
For example, the iteration count was 1 throughout training when ǫ = 1e– 2 .
Therefore, apparently, as long as the training with DKM layers can run long enough, the selection of ǫ might not affect the ﬁnal result.
We believe this is because DKM ensures the clustering continuity by resuming from the last known centroid (i.e., from the previous batch).
In case that the planned training time is short, a smaller ǫ value would be preferred but at the cost of larger memory requirement.
F G UMBEL - SOFTMAX FOR SOFT ASSIGNMENT
Gumbel-Softmax distribution is a continuous distribution that approximates samples from a categorical distribution and also works with back-propagation.
Therefore, it is feasible to use the drawing from Gumbel-softmax as soft assignment to generate attention.
Therefore, we experimented Gumbel-Softmax-based DKM ( GBS ) along with the hard assignment scheme ( Hard ) with
ResNet18 on ImageNet1k.
We ran clustering iteratively for both GBS and Hard, and such iteration helps GBS reduce the variance (Shayer et al., 2018).
We applied the same compression conﬁgurations from Fig.
4 and kept all hyper-parameters and training ﬂow intact.
The comparison results are in Fig.
10 which shows that Hard is much worse than both GBS and DKM on all the cases with over 10% drop in Top-1 accuracy, proving that soft assignment is a superior way of clustering weights for model compression.
Although GBS outperforms Hard, GBS is still worse than DKM on all cases, and the degradation can be as signiﬁcant as 3.4% drop in Top-1 accuracy for the most aggressive compression target.
[Figure]
G R ELATION TO E XPECTATION -M AXIMIZATION (EM) AND THEORETICAL INTERPRETATION
Special sub-case of DKM where gradients are not propagated can be related to a standard EM formulation.
Here is the formulation-level correspondence: Suppose
[Equation]
is the Gaussian mixture model over cluster centers.
Referring to Fig.
• Maximizing log likelihood of weights
[Equation]
using the
EM algorithm is equivalent to DKM for the case of d
i
j = − w i − c j )
2 . • The attention matrix A where a i,j = exp ( di,jτ
[Equation]
P k exp ( di,kτ ) is equivalent to the responsibilities calculated in the E step: r i,j =
[Equation]
• Updating C using c j = P i a
i,j w i P i a i,j is equivalent to M step of EM algorithm.
Notice that variance is ﬁxed, therefore M step in EM is only updating cluster centers
as DKM does.
However, unlike EM where ﬁnding C every M step is the objective, DKM focuses on generating a representative ˜W for the train-time compression for DNN.
Even though there is formulation-level similarity between DKM and EM, the way both are optimized is signiﬁcantly different.
While EM iteratively optimizes a speciﬁc likelihood function for a set of ﬁxed observations, DKM needs to adjust (i.e., optimize) the observations (which are weights) without leading to a trivial solution such as all observations collapsing to a certain point.
Hence,
DKM can neither assume any statistical distribution nor optimize a speciﬁc likelihood function (i.e., the observations are dynamically changing).
Therefore, DKM uses a simple softmax and rides on the back-propagation to ﬁne-tune the observations w.r.t.
the task loss function after unrolling multiple attention updates.
When we propagate gradients, then this will turn into a stochastic nonconvex joint optimization where we simultaneously optimize observations and centroids for the task loss function, which is shown to offer better accuracy vs. compression trade-offs according to our experiments.