R ETHINKING N EURAL N ETWORK Q UANTIZATION
Anonymous authors
Paper under double-blind review
A BSTRACT
Quantization reduces computation costs of neural networks but suffers from performance degeneration.
Is this accuracy drop due to the reduced capacity, or inefﬁcient training during the quantization procedure?
After looking into the gradient propagation process of neural networks by viewing the weights and intermediate activations as random variables, we discover two critical rules for efﬁcient training.
Recent quantization approaches violates the two rules and results in degenerated convergence.
To deal with this problem, we propose a simple yet effective technique, named scale-adjusted training (SAT), to comply with the discovered rules and facilitates efﬁcient training.
We also analyze the quantization error introduced in calculating the gradient in the popular parameterized clipping activation (PACT) technique.
Through SAT together with gradient-calibrated
PACT, quantized models obtain comparable or even better performance than their full-precision counterparts, achieving state-of-the-art accuracy with consistent improvement over previous quantization methods on a wide spectrum of models including MobileNet-V1/V2 and PreResNet-50.
I NTRODUCTION
Deep neural networks have gained rapid progress in tasks including computer vision, natural language processing and speech recognition [Voulodimos et al.
; Young et al.
; Nassif et al.
(2019)], which have been applied to real world systems such as robotics and self-driving cars [Lillicrap et al.
; Janai et al.
Recently, it becomes a signiﬁcant challenge to deploy the heavy deep models to resource-constrained platforms such as mobile phones and wearable devices.
To make deep neural networks more efﬁcient on model size, latency and energy, people have developed several approaches such as weight prunning [Han et al.
(2015)], model slimming [Liu et al.
(2017); Yu et al.
(2018)], and quantization [Courbariaux et al.
As one of the promising methods, quantization provides the opportunity to embed bulky and computation-intensive models onto platforms with limited resources.
By sacriﬁcing the precision of weights [Courbariaux et al.
; Han et al.
; Li et al.
; Zhu et al.
; Leng et al.
(2018)] and even features [Courbariaux et al.
; Rastegari et al.
; Zhou et al.
(2016; 2017); Park et al.
Mellempudi et al.
Mishra & Marr (2017);
Mishra et al.
; Xu et al.
; Choi et al.
; Jacob et al.
; Zhang et al.
; Zhuang et al.
; Gong et al.
(2019)], model sizes can be shrunk to a large extent, and full-precision multiplication is replaced by low-precision ﬁxed-point multiplication, addition or even bitwise operations, requiring much reduced latency and energy consumption during inference.
The quantized models are either directly derived from full-precision counterparts [Sheng et al.
(2018); Krishnamoorthi (2018)], or through regularized training [Rastegari et al.
; Choi et al.
; Bai et al.
Training-based methods better exploit the domain information and normally achieve better performance.
However, the quantized models still suffer from signiﬁcant accuracy reduction.
To alleviate this problem, instead of manually designing the number of bits in different layers, recent works resort to automated search methods [Elthakeb et al.
; Wu et al.
; Wang et al.
; Uhlich et al.
; Lou et al.
Despite all the effort to improve quantized models, we notice that a fundamental problem of network quantization remains untouched, that is, whether accuracy reduction is due to the reduced capacity of quantized models, or is due to the improper quantization procedure.
If the performance is indeed affected by the quantization procedure, is there a way to correct it and boost the performance?
[Figure]
In this paper, we ﬁrst investigate the condition of efﬁcient training of a generic deep neural network by analyzing the variance of the gradient with respect to the effective weights involved in convolution or fully-connected operations.
Here, effective weights denote the weights directly involved in convolution and other linear operations, which can be transformed weights in either quantized or unquantized training.
With some semi-quantitative analysis (together with empirical verﬁcation) of the scale of logit values and gradient ﬂow in the neural network during training, we discover that proper convergence of the network should follow two speciﬁc rules of efﬁcient training.
It is then demonstrated that deviation from these rules leads to improper training and accuracy reduction, regardless of the quantization level of weights or activations.
To deal with this problem, a simple yet effective technique named scale-adjusted training (SAT) is proposed, with which the rules of efﬁcient training are maintained so that performance can be boosted.
It is a general approach which can be integrated with most existing quantization-aware training algorithms.
We showcase the effectiveness of the SAT approach on a recent quantization approach PACT [Choi et al.
On the other hand, we also discover the quantization error introduced in calculating the gradient with respect to the trainable clipping level in the PACT technique, which also degenerates accuracy, especially for low precision models.
A schedule with calibrated gradient for PACT, namely CG-PACT, is proposed to correct the inaccurate gradient.
With the proposed SAT and CG-PACT, we achieve state-of-the-art performance of quantized neural networks on a wide range of models including MobileNet-V1/V2 and PreResNet-50 on ImageNet dataset, with consistent improvement over previous methods [Han et al.
; Choi et al.
; Zhang et al.
; Zhuang et al.
; Wang et al.
; Lou et al.
Esser et al.
under the same efﬁciency constraints.
As an example, Fig. 1 compares our method with some recent quantization techniques applied to ResNet-50 on ImageNet classiﬁcation task.
We believe our analysis and approaches will shed light on some existing issues of network quantization, and facilitate more advanced algorithms and applications.
R ELATED W ORK
Uniform-precision quantization.
Quantization of deep models has long been discussed since the early work of weight binarization [Courbariaux et al.
(2015; 2016)] and model compression [Han et al.
(2015)].The majority of previous methods enforce the same precision for weights/activations in different layers during quantization.
Early approaches focus on minimizing the difference in values [Rastegari et al.
or distributions [Han et al.
; Zhang et al.
(2018)] between quantized weights/activations and full-precision ones.
Recently, [Zhang et al.
(2018)] proposes a learning-based quantization method, where the quantizer is trained from data.
Regularizer for quantization is also proposed to implement binarized weights [Bai et al.
Ensemble of multiple models with low precision has also been studied [Zhu et al.
demonstrating improved performance than single model under the same computation budget.
[Esser et al.
(2019)] proposes a quantizer with trainable step size, and improves training convergence by balancing the magnitude of step size updates with weight updates, based on some heuristic analysis.
However, this method focuses on training the step size, and scales the gradients, instead of analyzing the impact of model weights themselves on the training dynamics.
Previous work have paid little attention to the impact of quantization procedure on training dynamics, and it remains unclear if previous algorithms are efﬁcient in training during quantization procedure.
Mixed-precision quantization.
Recent work attempts to use mixed-precision in one model, and weights and activations in different layers are assigned different bit-widths, resulting in better tradeoffs between efﬁciency and accuracy of neural networks.
Towards this end, automated algorithms are adopted to determine the most appropriate bit-width for each layer.
Reinforcement learning are adopted to search bit-width conﬁgurations with guidance from memory and computation cost [Elthakeb et al.
; Lou et al.
(2019)] or latency and energy produced by hardware simulators [Wang et al.
[Wu et al.
and [Uhlich et al.
(2019)] apply differentiable neural architecture search method to efﬁciently explore the searching space.
Although these methods result in more ﬂexible quantization architectures, the training procedure of quantized models still follows paradigms of uniform-precision quantization.
Efﬁcient Training.
Training dynamics of neural networks has been extensively studied, both on the impact of initialization schemes [He et al.
(2015)] and on the convergence condition with mean ﬁeld theory [Poole et al.
Early work mainly focuses on networks with simple structure, such as with only fully-connected layers [Schoenholz et al.
More advanced topics including residual networks and batch normalization are discussed with more powerful tools [Yang & Schoenholz (2017);
Yang et al.
facilitating efﬁcient training of CNN with 10k layers through orthogonal initialization of kernels [Xiao et al.
From a different angle of view, [Zhang et al.
(2019)] proposes a properly rescaled initialization scheme to replace normalization layers, which also enables training of residual network with a huge number of layers.
These approaches mainly focus on weight initialization methods to facilitate efﬁcient training.
We extend this analysis to the whole training process, and discovered some critical rules for efﬁcient training.
These rules guide our design of an improved algorithm for network quantization.
E FFICIENT T RAINING OF N ETWORK Q UANTIZATION 3. B ASIC Q UANTIZATION S TRATEGY
Historically, quantization of neural networks follows different conventions and settings [Krishnamoorthi (2018)].
Here we describe the convention adopted in this paper to avoid unnecessary ambiguity.
We ﬁrst train the full-precision model, which is used as a baseline for the ﬁnal comparison.
For quantized models, we use the pretrained full-precision model as initialization, and apply the same training hyperparameters and settings as full-precision model (including initial learning rate, learning rate scheduler, weight decay, the number of epochs, optimizer, batch size, etc.) to ﬁnetune the quantized model.
For the input image to the model, we use unsigned 8bit integer (uint8), which means we do not apply standardization (neither demeaning nor normalization).
Previous work sometimes avoid quantizing the ﬁrst and last layers due to accuracy drop [Zhou et al.
; Wu et al.
We follow a more practical setting to quantize weights in both layers with a minimum precision of 8bit [Choi et al.
(2018)] in our main results.
To investigate the effect of quantization level in these two layers, some additional results are shown in Appendix E.
The input to the last layer is quantized with the same precision as other layers.
As a widely adopted convention [Choi et al.
; Wang et al.
(2019)], bias in the last fully-connected layer(s) and the batch normalization (BN) layers (including weight, bias and the running statistics) are not quantized, but as shown in Appendix G, batch normalization layers of most networks can be eliminated except for a few cases.
Note that no bias term is used in convolution layers.
3. R ULES OF E FFICIENT T RAINING
Before detailed analysis, we ﬁrst formulate the problem we will investigate.
Following [He et al.
(2015); Schoenholz et al.
(2016)], we will discuss a simpliﬁed network composed of repeated blocks, each consisting of a linear layer (either convolution or fully-connected) without bias, a BN layer, an activation function and a pooling layer.
The last layer is a single fully-connected layer without bias, which produces the logit values for the loss function.
Suppose for the l -th block, the input is x ( l ) , and the effective weight of the fully-connected layer is Ξ ( l ) , which is called the effective weight because it is the de facto weight involved in forward and backward propagation of the network.
In other words, we have
[Equation]
where l = 1 , .
, L , L is the depth of the network, σ and µ stand for running statistics, and γ and β denote the trainable weights and bias in the BN layer, respectively.
h ( · ) is the activation function, which is assumed to be quasi-linear, such as the ReLU .
k pool l is the kernel size of the pooling layer, which can be set to 1 for the usual case with no pooling, and P is the set of pooling indices.
Note that for the last layer l = L , only Eq.
(1a) applies, and z ( L ) are the logits for calculating the cross entropy loss.
Optimization of the cross entropy loss is highly related to the scale of weights in the last fullyconnected layer.
Suppose the input to this layer is normalized with BN layer before pooling.
To avoid saturation issue for calculating gradient, we have the following rule of efﬁcient optimization for the cross entropy loss.
Efﬁcient Training Rule I
(ETR I)
To prevent the logits from entering the saturation region of the cross entropy loss, the scale (measured by the variance) of the effective weight Ξ ( L ) in the last fully-connected layer should be sufﬁciently small.
Speciﬁcally, we have
[Equation]
where n L is the length of input features of the fully-connected layer, and k pool L − 1 is the kernel size of the preceding pooling layer ( k pool L −
= 1 if the preceding layer is not pooling).
Empirically, we ﬁnd a ratio less than 0 .
1 is adequate.
Detailed derivation of this rule is given in Appendix A. Besides optimization of the cross entropy operation, training dynamics is also critical for efﬁcient training of deep neural network.
To analyze the training dynamics, we derive the following phenomenological law for gradient ﬂowing during training.
The Gradient Flowing Law For the aforementioned network, the variances of gradients of loss with respect to the effective weights in two adjacent layers are related by
[Equation]
On the other hand, if there is no batch normalization layer, we have
[Equation]
Here, both κ ( l ) 1 and κ ( l ) 2 are empirical parameters of order O (1) with respect to n l .
L is the loss function, and n l = c l · ( k conv l ) 2 and (cid:98) n l = c l +1 · ( k conv l ) 2 represent the number of input and output neurons of the l -th layer, respectively.
c l and c l +1 denote the number of input and output channels, and k conv l is the kernel size of this layer (which can be set to 1 for fully-connected layer).
Note that similar results have been studied for network initialization in [He et al.
and extensively studied with mean ﬁeld theory [Schoenholz et al.
; Yang & Schoenholz (2017); Xiao et al.
Here, we generalize the randomness assumption and empirically verify that the conclusion holds along the whole training procedure (see Appendix A).
Note that viewing weights and inputs as random variables along the whole training procedure is also adopted in previous literature of statistical mechanics approaches to analyze training dynamics of neural networks [Seung et al.
; Watkin et al.
; Martin & Mahoney (2017)].
To avoid gradient exploding/vanishing problems, the magnitudes of gradients should be on the same order when propagating from one layer to another.
Suppose k pool l = 1 , that is, there is no pooling operation (since the number of pooling layers is much smaller than the number of linear layers, their effect can be ignored for simplicity).
If we have BN after linear layer, then as long as the variance of weights in different layers are kept on the same order, the scaling factor in Eq.
(3) will be O (1) .
Note that we have implicitly assumed that n l and (cid:98) n l +1 do not differ in order.
On the other hand, if there is no BN layer directly following the linear layer, the scale of weights should be on the order of the reciprocal of the number of neurons to keep the scaling factor in Eq.
(4) on the order of O (1) .
Thus, we arrive at the basic rule for convergent training.
Efﬁcient Training Rule II (ETR II) To keep the gradient of weights in the same scale across the whole network, either BN layers should be used after linear layers such as convolution and fullyconnected layers, or the variance of the effective weights should be on the order of the reciprocal of the number of neurons of the linear layer ( n l or (cid:98) n l ).
Detailed derivation of the gradient ﬂowing law and ETR II is given in Appendix A.
With these rules, we are now ready to examine if training of quantized models is efﬁcient.
Following previous work PACT [Choi et al.
(2018)], we use the DoReFa scheme [Zhou et al.
(2016)] for weight quantization, and the PACT technique for activation quantization.
These methods are popular and typical for model quantization, and it is noteworthy that they are adopted only to showcase the effectiveness of our approach, instead of limiting our analysis.
Violations of training rules also exists in other quantization approaches such as BinaryNet [Courbariaux et al.
(2015)], XNORNet [Rastegari et al.
Ternary Quantization [Zhu et al.
(2016)], HWGQ [Cai et al.
(2017)] as well as a differentiable quantization approach Darts Quant [Uhlich et al.
(2019)], as weights distributions are changed during quantization/binarization procedure which results in potential violation of Efﬁcient
Training Rules.
Thus we believe our approach is general and can be used to boost the performance of such quantization algorithms as well.
3. W EIGHT Q UANTIZATION WITH SAT
The DoReFa scheme [Zhou et al.
(2016)] involves two steps, clamping and quantization.
Clamping transforms the weights to values between 0 and 1, while quantization rounds the weights to integers.
We here analyze the impact of both steps on training dynamics.
3.3. I MPACT OF C LAMPING
Before quantization, the weights are ﬁrst clamped to the interval between 0 and 1.
For a weight matrix W , we ﬁrst clamp it to
[Equation]
which is between 0 and 1.
This transformation generally contracts the scale of large weights, and enlarges the difference of small scale elements.
Thus, this clamping operation makes variables distributed more uniform in the interval [0 , 1] , which is beneﬁcial for reducing quantization error.
However, the variance of the clamped weights will be different from the original ones, potentially violating the efﬁcient training rules.
Speciﬁcally, clamping leads to violation of ETR I in the last linear layer, and voilation of ETR II in linear layers without BN layers followed.
For example, clamping on the full pre-activation ResNet [He et al.
(2016)] and VGGNet [Simonyan & Zisserman (2014)] will both violate ETR II since they have linear layers without BN followed.
In cases that convolution layers are followed by BN layers, the clamped weights are commensurate with each other and ETR II is preserved.
To understand the effect of clamping on ETR I, we ﬁrst analyze a model using clamped weights without quantization, following the DoReFa scheme
[Equation]
[Figure]
We have the effective weight Ξ
=
(cid:99) W in this case.
Fig.
2a gives the ratio between variances of the clamped and the original weights with respect to the number of neurons.
As a common practice [He et al.
(2015)], the original weights W are sampled from a Gaussian distribution of zero mean and variance proportional to the reciprocal of the number of neurons.
We ﬁnd that for large neuron numbers, the variance of weights can be enlarged to tens of their original values, potentially increasing the opportunity of violating ETR I.
Since the number of output neurons of the last linear layer is determined by the number of classes of data, we expect large dataset such as ImageNet [Deng et al.
(2009)] to be more vulnerable to this problem than small dataset such as CIFAR10 (see Appendix A for more details).
To verify our analysis, we train the MobileNet-V2 on ImageNet using traditional method as well as using clamped weights, and compare their learning curves.
As shown in Fig.
2b, clamping impairs the training procedure signiﬁcantly, reducing the ﬁnal accuracy by as much as 1%.
Also, we notice that clamping makes the model more prone to the over-ﬁtting issue, which is consistent with previous literature which claims that increasing weight variance in neural networks might worsen their generalization property [Seung et al.
; Watkin et al.
; Martin & Mahoney (2017)].
To deal with this problem, we propose a method named scale-adjusted training (SAT) to restore the variance of effective weights.
We directly multiplies the normalized weight with the square root of the reciprocal of the number of neurons in the linear layer as in Eq.
Here VAR [ (cid:99) W rs ] is the sample variance of elements in the weight matrix, calculated by averaging the square of elements in the weight matrix.
In back-propagation, VAR [ (cid:99) W rs ] is viewed as constant and receives no gradient.
This simple strategy is named constant rescaling and works well empirically across all of the experiments.
Note that here we have ignored the difference between weight variances across channels and just use variance of the weights in the whole layer for simplicity.
[Equation]
Fig.
2b compares the learning curves of vanilla method, and weight clamping with and without constant rescaling.
It shows that SAT recovers efﬁcient training and improves validation accuracy signiﬁcantly after weight clamping.
We also experiment with an alternative rescaling approach in
Appendix C and notice similar performance.
In the following experiments we will always use constant rescaling.
For MobileNet-V2, we only need to apply SAT to the last fully-connected layer.
For other models where convolution is not directly followed by BN such as full pre-activation
ResNet [He et al.
, we need to apply SAT to all such convolution layers (see Appendix E for more details).
Note that the proposed method does not mean to be exclusive, and there are other methods to preserve the rules of efﬁcient training.
For example, different learning rates can be used for the layers with no BN layer followed.
Before further discussion, we want to emphasize that the clamping is only a preprocessing step for quantization and there is no quantization operation involved up to now.
3.3. I MPACT OF W EIGHT Q UANTIZATION
With weights clamped to [0 , 1] , the DoReFa scheme [Zhou et al.
(2016)] further quantizes weights with the following function
[Equation]
Here, (cid:98)·(cid:101) indicates rounding to the nearest integer, and a equals 2 b − 1 where b is the number of quantization bits.
Quantized weights are given by
[Equation]
In this case, we have the effective weight given as Ξ = Q .
To see the impact of quantization on the training dynamics, we compare the variance of the quantized weight Q ij with the variance of the full-precision clamped weight (cid:99) W ij in Appendix B.
We ﬁnd that for precision higher than 3 bits, quantized weights have nearly the same variance as the full-precision weights, indicating quantization itself introduces little impact on training.
However, the discrepancy increases signiﬁcantly for low precision such as 1 or 2 bits, thus we should use the variance of quantized weights Q ij for standardization, rather than that of the clamped weight (cid:99) W ij .
For simplicity, we apply constant scaling to the quantized weights of linear layers without BN by
[Equation]
For typical models such as MobileNets and ResNets, only the last fully-connected layer needs to be rescaled, and such rescaling is only necessary during training for better convergence.
For inference, the scaling factor (which is positive) can be discarded, with the bias term being modiﬁed accordingly, introducing no additional operations.
For models with several fully-connected layers such as VGGNet [Simonyan & Zisserman (2014)], or with convolution layers not followed by BN layers, such as fully pre-activation ResNet, the scaling factors for these layers can be applied after computation-intensive convolutions or matrix multiplications, adding marginal computation cost.
3. A CTIVATION Q UANTIZATION WITH CG-PACT
For activation quantization, [Choi et al.
(2018)] proposes a method called parameterized clipping activation (PACT), where a trainable parameter α is introduced.
An activation value x is ﬁrst clipped to the interval [0 , α ] with the hard tanh function, then scaled, quantized and rescaled to produce the quantized value q as
[Equation]
Quantized value obtained this way is called dynamic ﬁxed-point number in [Mellempudi et al.
(2017)] because α is a ﬂoating number.
Since α is trainable, we need to calculate gradient with respect to it besides the activation x itself.
For this purpose, the straight through estimation (STE) method [Bengio et al.
(2013)] is usually adopted for backward propagation, which gives
[Equation] [Equation]
As q k ( x ) , the domain of deﬁnition for q (cid:48) k
( x ) is also restricted to [0 , 1] .
Chain rule gives (see Appendix D for more details)
[Equation]
[Figure]
[Table]
Note that we give different result for the case of x < α in Eq.
(13) compared to the original
PACT [Choi et al.
(2018)], where quantization error is ignored and the derivative of q is approximated to 0 , i.e., q k (cid:16) (cid:101) xα (cid:17)
≈ (cid:101) xα .
Such quantization error will not introduce signiﬁcant difference for high precisions, but the error introduced might be harmful for low precisions such as 4bit or lower.
Notably, a concurrent work [Jain et al.
(2019)] presents similar results.
To see the effect of this quantization error, we train MobileNet-V2 on ImageNet with low-precision of 2 and 4 bits, with the gradient calibrated as in Eq.
(13) or not as in original PACT
[Choi et al.
The learning curves are illustrated in Fig.
3, which show that calibration of PACT gradient facilitates efﬁcient training for low precision models.
This gradient calibration technique is named
CG-PACT for brevity.
E XPERIMENTS
Based on previous analysis, we apply the SAT and CG-PACT technqiues to popular models including MobileNet-V1, MobileNet-V2, PreResNet-50 on ImageNet.
For all experiments, we use cosine learing rate scheduler [Loshchilov & Hutter (2016)] without restart.
Learning rate is initially set to 0.05 and updated every iteration for totally 150 epochs.
We use SGD optimizer, Nesterov momentum with a momentum weight of 0.9 without damping, and weight decay of 4 × 10 − 5 .
The batch size
[Table] [Table]
is set to 2048 for MobileNet-V1/V2 and 1024 for PreResNet-50, and we adopt the warmup strategy suggested in [Goyal et al.
(2017)] by linearly increasing the learning rate every iteration to a larger value ( batch size / 256 × 0 . 05 )
for the ﬁrst ﬁve epochs before using the cosine annealing scheduler.
The input image is randomly cropped to 224 × 224 and randomly ﬂipped horizontally, and is kept as 8 bit unsigned integer with no standardization applied.
Note that we use full-precision models with clamped weight as initial points to ﬁnetune quantized models.
We compare our method with techniques in recent literature, including uniform-precision quantization algorithms such as DeepCompression [Han et al.
PACT [Choi et al.
LQNet [Zhang et al.
(2018)], LSQ
[Esser et al.
and mixed-precision approaches such as
HAQ [Wang et al.
and AutoQB [Lou et al.
Validation accuracy with respect to quantization levels for ResNet-50 is plotted in Fig.
It is obvious that our method gives signiﬁcant and consistent improvement over previous methods under the same resource constraint.
More thorough comparisons for quantization on MobileNets with or without quantized activation are given in Table 1 and 2, respectively.
Table 3 compares different quantization techniques on ResNet-50.
Surprisingly, the quantized models with our approach not only outperform all previous methods, including mixed-precision algorithms, but even outperform full-precision ones when the quantization is moderate ( ≥ 5 bits for both quantization and 4 bits for weight-only quantization on MobileNetV1/V2, ≥ 3 bits for either both and weight-only quantization on ResNet-50).
Our method reveals that previous quantization techniques indeed suffer from inefﬁcient training.
With proper adjustment to abide by the efﬁcient training rules, the quantized models achieve comparable or even better performance than their full-precision counterparts.
In this case, we have to rethink about the doctrine in the model quantization literature that quantization itself hampers the capacity of the model.
It seems with mild quantization, the generated models do not sacriﬁce in capacity, but beneﬁt from the quantization procedure.
The claimping and rescaling technique does not contribute to the gain in quantized models since they are already used in full-precision training.
One potential reason is that quantization acts as a favorable regularization during training and help the model to generalize better.
Note that quantizing both weight and activation gives better results than weight-only quantization in some cases (3 and 4 bits on ResNet-50), which also indicates that the quantization to the activations acts as a proper regularization and improves generalization capability of the network.
The underlying mechanism is not clear yet.
We left in-depth exploration as future work.
Ablation Study To see the different impacts of the two methods we proposed (SAT and CG-PACT), we experiment on MobileNet-V1 under two different quantization levels of 4bit and 8bit with combinations of the two approaches.
As shown in Table 4, both techniques contribute to the performance, and SAT is more critical than CG-PACT in both settings.
In mild quantization such as 8 bit, SAT itself is sufﬁcient to achieve high performance, which matches our previous analysis.
[Table]
C ONCLUSION
This paper studies efﬁcient training of quantized neural networks.
By analyzing the optimization of cross entropy loss and training dynamics in a neural network, we present a law describing the gradient ﬂow during training with semi-quantitative analysis and empirical veriﬁcation.
Based on this, we suggest two rules of efﬁcient training, and propose a scale-adjusted training technique to facilitate efﬁcient training of quantized neural network.
Our method yields state-of-the-art performance on quantized neural network.
The efﬁcient training rules not only applies to the quantization scenario, but can also be used as a inspection tool in other deep learning tasks.
A S EMI -Q UANTITATIVE A NALYSIS AND E MPIRICAL V ERIFICATION OF L OGIT S CALE AND G RADIENT F LOW
In this section, we ﬁrst make some semi-quantitative analysis of the scale of logit values and the gradient ﬂow to derive the laws and rules introduced in Section 3.2.
After that, we present some experimental results to verify the laws, together with some comments on our basic assumption.
We want to emphasize that our analysis is only semi-quantitative to give insights for practice, and is not guaranteed to be rigorous.
Complete and detailed analysis is far beyond the scope of this paper.
In contrast, we will focus on order analysis of quantities, instead of accurate values, so we assume all variables are random unless speciﬁcally mentioned, and variables in the same tensor (weight matrix or activation matrix in a layer) are distributed identically.
We will also widely adopt the assumption of independence between different random variables (with different names, layer indices or component indices), as claimed explicitly in the Axiom 3.2 of [Yang & Schoenholz (2017)].
This assumption is also adopted in other literature, such as [He et al.
; Schoenholz et al.
It is not rigorously correct, but it leads to useful conclusion in practice [Xiao et al.
In spite of this, we will give some comments on this assumption in the end of this section.
A. S EMI -Q UANTITATIVE A NALYSIS
As mentioned in Section 3.2, we discuss a simpliﬁed network composed of repeated blocks, each consisting of a linear (either convolution or fully-connected) layer without bias, a BN layer, the activation function and a pooling layer.
The ﬁnal stage is a single fully-connected layer without bias, which produces the logit values for the loss function.
The pooling operation can be either average pooling or max pooling, but we only analyze the case of average pooling, and leave the analysis of max pooling as future work.
We will ﬁrst analyze the scale of logit values produced by the last fully-connected layer, and then discuss the training dynamics of the network.
A.
A NALYSIS OF THE L OGIT S CALE
The logit values produced by the ﬁnal fully-connected layer is
[Equation]
where L is the depth of the network, Ξ ( L ) is the effective weight of this layer, and x ( L ) is its input given by the previous block.
The BN, nonlinear activation and pooling operations can be represented by
[Equation]
From this, we have
[Equation]
16a
[Equation]
where n L is the length of the input features of this last fully-connected layer.
For a wide range of models including ResNets and MobileNets, n L equals to the number of output channels of the last convolution layer.
Note that we have extensively used the assumption of independence, and rely on the fact that the activation function h ( · ) is quasi-linear.
Typically, the weight of the batch normalization layer γ is on the order of O (1) , and thus we have
[Equation]
To avoid output saturation, the softmax function for calculating the cross entropy loss has a preferred range of the logit values for efﬁcient optimization.
To get a more intuitive understanding of such preference, we plot the sigmoid function and its derivative in Fig.
4 as a simpliﬁcation of softmax function.
It is clear that for efﬁcient optimization, the scale of the input x should be well below 1 , from which we can derive that
[Equation]
which is the Efﬁcient Training Rule I.
For more intuitive understanding of the ETR I, here we analyze the impact of clamping operation for some typical models, based on the variance ratio given in Fig.
2a.
The results are illustrated in
Table 5, where the variance of the original weight VAR [ W ( L ) ij ] is given by
[Equation]
19) and we need to determine if the value of κ 0 deﬁned as
[Equation]
20) is sufﬁciently small in comparison with 1 .
Note that Ξ = W for vanilla models and Ξ = (cid:99) W for models with clamping.
From the results we can see that models with clamping have much larger κ 0 , which results in violation of the ETR I.
This explains the degeneration of the learning curve for clamping in Fig.
2b.
[Table]
4 x 0.
( x ) (a)    4 x 0.
0 ( x ) (b)
A.1. T RAINING D YNAMICS
Now we analyze the training dynamics of the network.
Suppose for the l -th block, the input is x ( l ) , and the effective weight of the linear layer is Ξ ( l ) .
In other words, we have
[Equation]
where l = 1 , .
, L − 1 .
Since the parameters γ ( l ) and β ( l ) are constants (although trainable), and the activation function is quasi-linear, we have
[Equation]
where n l is the number of input neurons in the l -th layer, i.e. n l =
c l · ( k conv l ) 2 , with c l and k conv l denoting the number of input channels and kernel size of the convolution layer, respectively.
For fully-connnected layer, we can set k conv l = 1 .
Here, we have omitted the indices of trainable parameters in the batch normalization layer by simply assuming that they are of the similar order.
The second equality holds because the activation function is quasi-linear, and in the third equality we have used the assumption of independence.
Meanwhile, in the last three equalities, we have used the deﬁnition of σ ( l ) and also the assumption that W ( l ) and x ( l ) are all independent with each other.
During training, both µ ( l ) i and σ ( l ) i are functions of z ( l ) i , and we have
[Equation]
therefore,
[Equation]
Here, m B is the batch size.
From the above results
[Equation]
we can derive the relationship between the gradients with respect to the weights and activations in two adjacent layers.
Actually, from Eq.
(21) and Eq.
(24), we have
[Equation]
The variances of the gradients are thus given by
[Equation]
where (cid:98) n l = c l +1 · ( k conv l ) 2 and c l +1 is the number of output channels.
We use the assumption of independence for different variables here again.
From this we can get
[Equation]
Note that the subscripts of variables inside VAR can be eliminated as variables in the same tensor are distributed identically by assumption.
If we have batch normalization layer after the linear layer, based on Eq. (22c) and Eq.
(22e), we have
[Equation]
Here, κ ( l ) 1 is some empirical parameter of order O (1) with respect to n l .
[Figure]
On the other hand, if there is no batch normalization layers, γ ( l ) and σ ( l ) i will be removed from Eq.
(27) and VAR [ ∂ Ξ ( l ) ij L ] can be formulated as
[Equation]
For typical case, κ ( l ) 2 is another empirical parameter of order O (1) with respect to n l .
Both Eq.
(28) and Eq.
(29) are referred to as the gradient ﬂowing law.
A. E MPIRICAL V ERIFICATION
We are now ready to verify Eq.
(18) and Eq.
(28) with experiments.
For this purpose, we train a vanilla ResNet18 with Kaiming-initialization [He et al.
and batch normalization to guarantee convergent training.
In this case, we have Ξ = W .
We calculate the two parameters κ 0 and κ 1 given by
[Equation]
Here κ 0 is calculated using statistics from the last fully-connected layer and κ 1 is calculated for all convolution layers along the whole training procedure.
We plot the values of κ 0 and κ 1 in Fig.
As we can see, κ 0 (cid:28) 1 during the whole training process as expected.
κ ( l ) 1 in different layers are all of order
O (1) during the training process, except for the ﬁrst layer, which involves max pooling and its analysis is out of our scope (we set k pool1 = 1 in the computation for this layer).
Nevertheless, we notice that the discrepancy is not large.
A. C OMMENTS ON THE I NDEPENDENCE A SSUMPTION
Here we want to present some further discussion on the independence assumption, which is extensively used in our analysis.
Our discussion is to facilitate more intuitive understanding of such assumption, but not in pursue of rigorous proof.
The ﬁrst point we want to emphasize is related to the central limit theorem (CLT).
Independence is one important condition for the CLT, but a sufﬁciently weak dependence does not harm [Van Kampen (1992)].
Typical examples where independence does not hold include the momentum of molecules in an ideal gas described by the micro-canonical ensemble, or random walk with persistence.
Even with independence assumption violated, the sum of random variables is able to converge to a Gaussian distribution as the number of random variables increases indeﬁnitely.
Thus, with sufﬁcient number of neurons, linear operation can result in normally distributed outputs, which further leads to the normal distribution of gradients.
Now since the weights are initialized with normal distribution, updating with normally distributed gradients will result in some new Gaussian random variables, and thus we can focus on the variance to characterize their statistical properties.
Experiment shows that the trained parameters still resemble Gaussian distribution (sometimes with some small spurs), especially for the last several layers.
Second, we investigate a simple example to show how the independence property can be derived with zeroth-order approximation.
For simplicity, we analyze the product of two random variables U and V , both of which are some function of another random variable X , that is
[Equation]
We can see that U and V are generally not independent.
The random variables can be viewed as a quantity with some true value given by the mean, but perturbed by some small random noise, that is
[Equation] [Equation]
32b)
and we have
[Equation]
33a)
[Equation] [Equation]
For zeroth-order approximation, we can ignore the second term, which is a second-order quantity, and get
[Equation]
For variance of UV , if E [ U ] = 0 , we have
[Equation]
35a
[Equation]
35b)
[Equation]
which is an approximated independence property widely used in our derivation.
Note that such zeroth-order approximation is broadly adopted in statistical mechanics and related areas for analyzing macroscopic law of noisy physical systems, where nonlinear mechanisms exist [Van Kampen (1992)].
More accurate results could be derived by expansion of the master equation, which is beyond the scope of this paper.
Such zeroth-order approximation is also similar to the annealed approximation method in condensed matter physics [Seung et al.
B I MPACT OF Q UANTIZATION ON W EIGHT V ARIANCE
In this section, we examine the impact of quantization on weight variance.
Fig.
6 shows the ratio between the standard deviations of them with respect to the number of bits for different channel numbers, which determines the variances of the original non-clamped weights W .
We can ﬁnd that for precision higher than 3 bits, quantized weights have nearly the same variance as the full-precision weights, indicating quantization itself introduces little impact on training.
However, the discrepancy increases signiﬁcantly for low precision such as 1 or 2 bits.
Also, different channel numbers give similar results.
[Figure]
[Figure]
C C OMPARISON OF R ESCALING M ETHOD
In this section, we will compare the constant rescaling methods proposed in 3.3 and another method named rescaling with standard deviation.
It standardizes the effective weights and then rescales them with the standard deviation of the original weights as in Eq.
(36), where VAR [ · ] is the sample variance of elements in the weight matrix, calculated by averaging the square of elements in the weight matrix.
[Equation]
We train MobileNet-V2 on ImageNet with weight clamping and rescale the last fully-connected layer using the two methods, and plot their learning curves as shown in Fig.
We ﬁnd that the two methods give similar results.
D G RADIENT C ALCULATION IN PACT
Here we derive the result in Eq.
The activation value x is clamped into the interval [0 , α ] and then quantized as follows
[Equation]
Based on the STE rule
[Equation]
we have
[Equation]
Taking derivative with respect to α in Eq.
[Equation]
we have
[Equation]
which is the result in Eq.
E B ITWIDTHS OF THE F IRST AND L AST L AYERS
Here we study the impact of quantization levels of the ﬁrst and the last layers.
Using MobileNetV1, we compare the two settings of quantizing these two layers to a ﬁxed 8 bits or to the same bit-width as other layers.
As shown in Table 6, we ﬁnd that the accuracy reduction is negligible for quantization levels higher than 4bits.
[Table]
F R ESCALING C ONVOLUTION L AYERS WITHOUT B ATCH N ORMALIZATION F OLLOWED
In this section, we study the impact of rescaling to convolution layers without BN layers followed.
As shown in Fig.
8, we plot the learning curves of Pre-ResNet50 with 2 bits weight-only quantization
[Figure]
on ImageNet, with constant rescaling applied to only the fully-connected layer or all layers.
We can see that applying rescaling to only the last fully-connected layer is not sufﬁcient for efﬁcient training, and rescaling on all convolution layers successfully achieves efﬁcient training.
G E LIMINATION OF B ATCH N ORMALIZATION L AYERS
As mentioned in Section 3.1, batch normalization layers are not quantized in our experiments, and full-precision multiplication involved in BN is a considerable amount of computation during inference.
Here, we want to discuss the possibility to eliminate such layers, especially for light-weight models such as MobileNet-V1/V2.
As shown in Eq.
(8), quantization of activation function already involves full-precision multiplication, so we want to examine if it is possible to absorb the batch normalization operations into this clipping function.
The quantized input to a linear layer can be written as
[Equation]
(40) where n is the integer output from the rounding function in quantizing the activation of previous layer, a = 2 b − 1 is determined by the bitwidth b of the previous layer, and α is the clipping level of the activation function in the previous layer.
The quantized output of the current layer is given by
[Equation]
where (cid:101) α and (cid:101) a are corresponding parameters for the current layer, clipping( · ) is the clipping function given by Eq.
(11a),
and we have simpliﬁed the batch normalization by absorbing the running statistics into the parameters.
Here, we have focused on one channel of the batch normalization layer, but the generalization is straightforward.
From this, we can see that we only need to use different biasing and clipping levels for different channels, which can be pre-computed.
The full-precision multiplications in BN layers are eliminated.
Meanwhile, since Q and n are both quantized, lower precision can be adopted for computing the clipping levels.
Note that the above discussion is only applicable to models without skip-connection, such as
MobileNet-V1, or models where convolution is not involved in skip-connection, such as MobileNetV2.
For ResNet and PreResNet, the above method is not applicable and more sophisticated methods might be necessary to eliminate BN layers.