L EARNING TO G ROW P RETRAINED M ODELS FOR E FFICIENT T RANSFORMER T RAINING
Anonymous authors
Paper under double-blind review
A BSTRACT
Scaling transformers has led to significant breakthroughs in many domains, leading to a paradigm in which larger versions of existing models are trained and released on a periodic basis.
Curiously, new instances of such models are typically trained completely from scratch, despite the fact that they are often just scaled-up versions of their smaller counterparts.
How can we use the implicit knowledge in the parameters of smaller, extant models to enable faster training of newer, larger models?
This paper describes an approach for accelerating transformer training by learning to grow pretrained transformers, where we learn to linearly map the parameters of the smaller model to initialize the larger model.
For tractable learning, we factorize the linear transformation as a composition of (linear)
width- and depth-growth operators, and further employ a Kronecker factorization of these growth operators to encode architectural knowledge.
Experiments across both language and vision transformers demonstrate that our LEarning to GrOw (LEGO) approach can save around 50% computational cost of training from scratch, while also consistently outperforming strong baselines that also reuse smaller pretrained models to initialize larger models.
Our code will be made publicly available.
I NTRODUCTION
The transformer architecture (Vaswani et al., 2017) has emerged as a general purpose architecture for modeling many structured domains (Devlin et al., 2019; Brown et al., 2020; Rives et al., 2021; Dosovitskiy et al., 2021; Touvron et al., 2021a).
Perhaps more so than other architectures, the transformer empirically seems to have inductive biases that make it especially amenable to scaling (Rosenfeld et al., 2019; Kaplan et al., 2020), which has led to a paradigm in which larger versions of smaller, existing models are trained and released on a periodic basis (e.g., the GPT lineage of models (Radford et al., 2018; 2019; Brown et al., 2020)).
New instances of such models are typically trained completely from scratch, despite the fact that they are often scaled-up versions of their smaller counterparts.
Given the compute required to train even the smaller models, we argue that training each model from scratch is wasteful, and that prior knowledge implicit in the parameters of smaller, extant models should be leveraged to enable faster training of larger models.
One approach to this problem is through the lens of model growth , wherein a smaller model’s pretrained parameters are used to initialize a subset of the larger model’s parameters.
While earlier works generally froze the parameters initialized from the pretrained model and only trained the new (randomly initialized) parameters (Fahlman & Lebiere, 1989; Fahlman, 1990; Gutstein et al., 2008), subsequent work has shown that copying a subset of the pretrained parameters to initialize the new parameters and then finetuning the entire network significantly accelerates training and sometimes even leads to better performance (Chen et al., 2015).
When applied to modern transformers, these mechanisms roughly translate to a depth-expansion operator in which pretrained models are stacked (or combined with identity layers) to initialize deeper transformers (Gong et al., 2019;
Yang et al., 2020), and a width-expansion operator in which the smaller model’s
matrices are copied to initialize the larger model’s matrices (e.g., in block-diagonal fashion)
(Chen et al., 2021b;
Gu et al., 2020).
Noting the empirical effectiveness of such recipes, we observe that existing mechanisms generally do not have a learning component (e.g., randomly copying over neurons for width-expansion or stacking consecutive layers for depth-expansion).
This paper instead proposes an efficient, data-driven approach for learning to grow transformers.
In particular, our LEarning to GrOw (LEGO) approach frames the problem of initializing the larger model’s parameters as learning a linear mapping from the smaller model’s parameters, i.e., θ ( large ) =
Mθ ( small ) where θ ( small ) and θ ( large ) are the vectorized parameters of the small/large models.
Of course, this mapping is completely intractable to learn without any restrictions on M .
We thus factorize the linear mapping to be a composition of sparse width- and depth-expansion operators, M = L depth R width , where both width and depth matrices are further factorized to be a Kronecker product of smaller matrices that express architectural knowledge (e.g., through grouping parameters by layers and neurons).
We show that our growth operators can represent existing approaches such as layer-stacking and neuron-copying as special cases.
We find that with a small amount of learning on M (e.g., 100 gradient steps) to initialize the larger model, we can significantly accelerate training and outperform existing approaches for model growth with both vision and language transformers.
Figure 1 illustrates our approach.
Targeting model families where there already exists a rich set of smaller pretrained models, extensive experiments on BERT (Devlin et al., 2019), RoBERTa (Liu et al., 2019), GPT2 (Radford et al., 2019), and ViT (Dosovitskiy et al., 2021; Touvron et al., 2021a;b) show that LEGO can consistently improve transformer training efficiency over the traditional way of training from scratch across domains and model sizes.
For instance, LEGO saves 44 .
7% and 22 .
5% FLOPs for training BERT-Base and GPT2-Large from scratch by reusing pretrained smaller models that are half as big.
Similarly, for vision transformers, when using DeiT-S (Touvron et al., 2021a) for initialization,
LEGO yields 55% savings in FLOPs with no performance drop on ImageNet (Deng et al., 2009).
These FLOPs savings directly translate to similar savings in wall clock.
We also find that models trained using our proposed approach achieves very similar performance compared to the trainedfrom-scratch baselines when transferred to other downstream tasks (e.g., GLUE (Wang et al., 2018),
SQuAD (Rajpurkar et al., 2016) for language models, and CIFAR (Krizhevsky et al., 2009), Flowers102 (Nilsback & Zisserman, 2008), etc., for vision models.).
R ELATED W ORK
Efficient Training.
Efficient training of transformers has been studied from multiple perspectives.
Some methods that are orthogonal to our work include mixed precision training (Shoeybi et al., 2019), large batch optimization (You et al., 2019), distributed training (Huang et al., 2019), and dropping layers (Zhang & He, 2020) or tokens (Hou et al., 2022).
Knowledge inheritance (Qin et al., 2021) explores knowledge distillation during pre-training to efficiently learn larger transformers.
Progressive training, which first trains a small transformer with few layers and then gradually expands by stacking layers, has also been applied to transformer training (Gong et al., 2019;
Yang et al., 2020; Li et al., 2022;
Shen et al., 2022).
Net2Net Chen et al.
(2015) uses function preserving transformations to grow width by copying neurons and depth by using identity layers.
Recently, bert2BERT (Chen et al., 2021b) extends Net2Net to transformers.
In contrast these approaches, our approach learns to (linearly) transform the parameters of a smaller model to initialize a larger model.
While there is a line of work on learning to grow neural networks in a data-driven way, these methods are in general difficult to apply to modern-scale transformers since they (for example) involve growing a single neuron at a time or employ expensive optimization/search procedures (Wei et al., 2016;
Cai et al., 2018; Wu et al., 2019; 2021; Evci et al., 2022).
Network Initialization.
Our work is also related to work on neural network initialization.
Existing work in this line include controlling the norm of the parameters (Mishkin & Matas, 2015; Kilcher et al., 2018; Dai et al., 2019; Wu et al., 2019; Glorot & Bengio, 2010) or replacing the normalization layers (Brock et al., 2021;
Zhang et al., 2019; Huang et al., 2020).
MetaInit (Dauphin &
Schoenholz, 2019) proposes an automatic network initialization method that optimizes the norms of weight tensors to minimize the gradient quotient on minibatches of random Gaussian samples.
GradInit (Zhu et al., 2021) learns to initialize networks by adjusting norm of each network layer so that a single step optimization with prescribed hyperparameters results in the smallest possible loss value.
We instead focus on taking advantage of smaller pretrained transformers to better initialize larger transformers, which remains an understudied problem.
Structured Matrices.
Finally, our work is also related to structured matrices which are typically used to replace dense weight matrices for reducing training and inference computation cost.
Representative works use sparse and low rank matrices (Chiu et al., 2021; Han et al., 2015), Chebyshev (Tang et al., 2019), Toeplitz (Sindhwani et al., 2015), Kronecker-product matrices (Zhang et al., 2015), and butterfly matrices (Dao et al., 2019) and its variants (Chen et al., 2021a).
A unified framework to learn a broad family of structured matrices for small-footprint deep learning is presented in Sindhwani et al.
Dao et al.
(2022) propose the family of Monarch matrices, which inherit the expressiveness of butterfly matrices and achieves reasonable accuracy-efficiency tradeoffs in many sparse training applications.
While our approach is inspired by these works, we propose to grow pretrained models by learning structured sparse linear operators with Kronecker factorization, which to our best knowledge has not been explored in the literature.
P ROPOSED A PPROACH
Notation.
We denote the parameters of a neural network with L layers and D dimensions as Θ L,D
=
[ W· · · W L ] ⊤ ∈ R LD × D , where W l ∈ R D
× D denotes the weights for the l -th layer.
1 With slight abuse of notation, we denote the vectorization of Θ L,D as vec( Θ L,D ) ⊤ = (cid:2) vec( W 1 ) ⊤ · · · vec( W L ) ⊤ (cid:3) .
Our goal is to re-use the parameters
Θ =
Θ L 1 ,D 1 from a pretrained smaller model to initialize a large model Θ ( new ) =
Θ L 2 ,D 2 through a model growth operator
M : R L 1 D 1 × D 1 → R L 2 D 2 × D 2 that maps the weights of the smaller network to the weights of the larger one: Θ ( new ) =
M ( Θ ) , where L 1 <
L 2 and D 1 < D 2 .
After model growth, we adopt Θ ( new ) as the initialization of the large model and train it following standard recipes.
3. E XISTING G ROWTH O PERATORS
Existing works have separately established model growth operators M for depth ( L 1 < L 2 , D 1 =
D 2 ) and width ( L 1 =
L 2 , D 1 < D 2 ).
We summarize these methods below.
Depth Expansion.
StackBERT (Gong et al., 2019) proposes to duplicate the smaller model to double the depth, based on the observation that upper layers share similar functionality with the lower layers.
In contrast, interpolation-based depth expansion methods (Chang et al., 2017; Dong et al., 2020) interleave every layer to form a deeper model, which can be roughly interpreted as simulating a finer grained solution to the original dynamical system from a neural ODE perspective (Chen et al., 2018).
Letting L 2 = kL 1 , the two methods’ growth operators can be formulated as:
[Equation]
Width Expansion.
Net2Net (Chen et al., 2015) expands width of neural networks by randomly copying neurons while preserving output values via normalization.
This can be seen as growing a matrix associated with a particular layer by duplicating the columns and rows of its weight matrix.
Suppose a layer has weight matrix W l ∈ R D 1 × D 1 .
3 To expand it to a matrix W ( new ) l ∈ R D 2 × D1 For
but straightforwardly dimensions (e.g., FFN layers of transformers).
We therefore have vec( Θ L,D )
⊤ ∈ R LD 2 .
Our approach is also agnostic with regard to vectorization order.
We define a single layer as f l ( x ) =
W l x + b l , where the row number of W l corresponds to the output dimension, and the column number of W l corresponds to the input dimension.
( D 2 > D 1 ), Net2Net copies W l to its upper-left corner of W ( new ) l , fills the new columns via a random selection matrix S l , and finally duplicates and normalizes rows according to the selection matrix from the previous layer.
Formally, the growth operator of Net2Net can be written as:
[Equation]
where S l ∈ { 0 , 1 } D 1 × ( D 2 − D 1 ) is a random selection matrix.
The diagonal of D l is a D 1dimensional histogram, whose i -th entry indicates number of times i -th column of W l was copied.
3. L EARNING TO G ROW
While existing operators have been empirically successful in accelerating transformer-based models such as BERT (Gong et al., 2019; Chen et al., 2021b), we observe that generally do not have a learning component and perform the depth- and width-expansions separately.
In this section we introduce a general framework for LEarning to GrOw (LEGO) pretrained models, which generalizes existing operators by combining the width- and depth-growth operators in a data-driven way.
We can formulate the problem of initializing the weights of the larger model Θ ( new ) from the smaller model Θ through the following optimization problem,
[Equation]
where D is the data distribution.
It is of course intractable to optimize over the entire operator space, and thus we further simplify the function M to be a linear transformation, which results in the following formulation,
[Equation]
This simplified objective is still completely infeasible to apply to contemporary neural networks where L 1 D 1 can easily be in the hundreds of millions.
We therefore propose an efficient parameterization of M for tractable learning.
3.2. D ECOMPOSITION ALONG D EPTH AND W IDTH
Our first step is to decompose the LEGO operator as M = L depth R width , where L depth and
R width expand the depth and width of model separately.
More concretely, we decompose M as
[Equation]
where R l ∈ R D 22 × D 21 and ℓ i,j ∈ R D 22 .
L depth is an array of diagonal matrices and R width is a block-diagonal matrix, i.e., both matrices are highly structured and sparse.
When applying
R width to weights vec( Θ ) , the parameters of each layer will be transformed independently via vec( W ( new ) l ) =
R l vec( W l ) and lifted to a higher dimension.
The l -th row block of L depth corresponds to growth operator of l -th layer, which amounts to linearly combining all layers of the smaller model via vec( W ( new ) l )
[Equation]
ℓ l,
[Equation]
) k vec( W l ) k .
By this factorization, we can effectively reduce complexity of LEGO operator from O ( D 21 L 1 D 22 L 2 ) to O
( D 21 D 22 L 1 ) and encode architectural knowledge by grouping parameters by layers.
We show in Sec.
3.4 that this representation still enjoys high representation power due to its connection with Monarch matrices (Dao et al., 2022).
3.2. P ARAMETER S HARING VIA K RONECKER F ACTORIZATION
The above LEGO operator still requires O ( D 2 1 D 2 2 L 1 ) parameters for R width and O ( L 1 L 2 D 2 2 ) for
L depth .
The width operator R width in this case still remains prohibitively expensive given that
D 1 (and D 2 ) can easily be in the hundreds or thousands.
In this section, we propose a Kronecker factorization to further reduce the number of learnable parameters for each growth operator.
Depth.
For depth, we treat an entire layer as a single group and construct a new layer by combining existing layers, effectively tying parameters for all neurons in same layer.
Formally, each block in
L depth is simplified to be diag( ℓ i,j )
=
w i,j I .
Then the entire matrix can be written as a Kronecker factorization, L depth = w ⊗ I , where w ∈ R L 2 × L 1 is a matrix whose entry w i,j indicates blending weights of j -th layer of the small model to form i -th layer of the large model.
This strategy reduces the number of parameters in L depth to O
( L 1 L 2 ) , and is shown on left hand side of Figure 1.
Width.
For width, we decompose each diagonal block of width expansion operator R width using the Kronecker factorization R l =
A l ⊗ B l , where A l , B l ∈ R D 2 × D 1 .
Since vec( CAB )
=
( B ⊤ ⊗ C ) vec( A ) (Schacke, 2004), we then have,
[Equation]
Here we observe that B l W l A ⊤ l performs in- and out-dimension expansion by A l and B l , respectively.
Each new column/row is a linear combination of columns/rows of small model’s weight matrix.
This factorization, which can be seen as grouping parameters by neurons , reduces number of parameters to O
( L 1 D 1 D 2 ) .
Figure 1 (right) illustrates LEGO’s width-expansion operator.
Altogether, we obtain the final parameterization of LEGO operator M :
[Equation]
expansion
[Equation]
We can exploit the factorization to implement LEGO operator (Eq. 8) efficiently.
Overall Training. Concretely, LEGO expands a model in three steps: (1) for each layer, inserting new rows by linearly combining existing rows by B l , (2) for each layer, inserting new columns by linearly combining existing columns by A l , and then finally (3) reconstructing each layer by linearly combining the weight matrices with w along the depth.
We then run a few steps (e.g.,iterations) of SGD to optimize M , which has negligible compute cost relative to regular training.
After obtaining M , we initialize large model with M vec( Θ ) , and train parameters Θ ( new ) through SGD as usual.
Algorithm 1 summarizes a forward pass of LEGO with transformer. Finally, as shown in Appendix A we note that StackBERT (Eq. 1), Interpolation (Eq. 1), and Net2Net (Eq. 2) are all special cases of LEGO (Eq.
8) with a particular setting of L depth and R width .
3. G ROWING T RANSFORMERS WITH LEGO
While LEGO can be applied to any multi-layer neural network architecture, in this paper we focus on using LEGO to grow transformers which have been shown to be particularly amenable to scaling.
Below we briefly describe how LEGO is applied to the main transformer embedding/attention layers and defer further details (e.g., growing bias vectors, layer norm parameters) to the Appendix B.
Embedding Layer.
Embedding layer can be regarded as a linear layer whose inputs are one-hot embeddings.
We draw a learnable matrix B ( emb ) to extend its output dimension.
This embedding layer is also used as the final output layer for our transformer language modeling experiments.
Attention and Feedforward Layers.
An attention layer consists of multi-head attention weights ( W Q , W K , W V ) and a linear projection ( W O ).
Let A kl and B kl where k ∈ { Q, K, V, O } be the in- and out-dimension expansion matrices (Eq. 6) for query, key, value, and projection in the l -th layer, respectively.
To make sure the new input and output channels are aligned across modules, we tie LEGO operator as follows: (1) A k l = B ( emb ) ⊤ for ∀ k ∈ { Q, K, V } , (2) A O l =
( B V ⊤ l ) , (3) B
Ol = B ( emb ) for ∀ l ∈
[ L 1 ] , among which the last constraint is added to take into account residual connections (Chen et al., 2021b).
We similarly tie parameters for the feed-forward networks.
Specifically, we ensure A ( fc 1) l
[Equation]
Since transformers make heavy use of residual layers with skip connections, we found that simply using the same B ( emb ) to parameterize A k l and B k l for many layers/modules worked well in prac- tice.
This reduces the number of learnable parameters even further and enables fast learning of M on a small amount of data (100 gradient steps).
3. C ONNECTION TO M ONARCH M ATRICES
As shown in Sec.
3.2.1, our depth-width decomposition factorizes M into a multiplication of two structured sparse matrices.
We examine the expressiveness of this factorized representation by relating it to Monarch matrices (Dao et al., 2022), defined below.
Definition 1.
Let the space of Monarch matrices be M ⊆ R mn 1 ×
mn 2 .
Then matrix M ∈ M
if
M = P 1 LP ⊤ 2 R = P 1 diag( L 1 , · · · , L n 1 ) P
⊤ 2 diag( R 1 , · · · , R n 2 ) where L i ∈ R b 1 × b 2 , R i ∈ R b 3 ×
b 4 are dense rectangular matrices, and n 1 b 2 = n 2 b 3 .
P 1 is the permutation π ( i ) =
( i − b
⌊ ib 1 ⌋ − 1) n 1 + ⌊ ib 1 ⌋ + 1 and P 2 is the permutation π ( j ) =
( j − b 2 ⌊ jb 2 ⌋ − 1) n 1 + ⌊ jb 2 ⌋ + 1 .
It is clear that the block-diagonal matrix R has the identical form to our width growing operator
R width .
By applying the permutation matrices P 1 and P 2 to L , L is transformed into the exactly same form with our depth-growth operator L depth in Eq.
This implies that our depth-width decomposition coincides with Monarch sparsification of dense matrices, which generalize butterfly matrices (Dao et al., 2019) and enjoy rich expressivity properties (Dao et al., 2020; 2022).
E XPERIMENTS
We conduct experiments to answer three key research questions.
Q1:
To what extent LEGO can improve the training efficiency (FLOPs and wall time) of transformers compared to training from scratch and other growth operators?
Q2:
Can LEGO be universally effective across transformers from different domains (e.g., language and vision) and sizes?
Q3:
Can models trained using LEGO achieve similar performance compared to the baselines when transferred to other downstream task?
4. E XPERIMENTAL S ETUP
Datasets.
We follow (Tan & Bansal, 2020) to use the English Wikipedia corpus 4 for training
BERT (Devlin et al., 2019) and RoBERTa (Liu et al., 2019).
We use the the public C4 (Raffel et al., 2020) dataset for training GPT2 (Radford et al., 2019).
We use ImageNet (Deng et al., 2009) for training vision transformers.
We use GLUE (Wang et al., 2018), SQuADv1.1 (Rajpurkar et al., 2016), and SQuADv2.0 (Rajpurkar et al., 2018) for evaluating pretrained BERT models.
We test downstream performance of vision transformers (DeiT (Touvron et al., 2021a)) by performing transfer learning on 5 downstream image classification tasks, including CIFAR10 (Krizhevsky et al., 2009), CIFAR100 (Krizhevsky et al., 2009), Flowers102 (Nilsback & Zisserman, 2008), StanfordCars (Krause et al., 2013), and ChestXRay8 (Wang et al., 2017).
Models.
We experiment with following transformers.
(1) BERT-Small → BERT-Base, BERTBase → BERT-Large, BERT-Small → BERT-Large for BERT, (2) RoBERTa-Small → RoBERTa-Base for RoBERTa, (3) GPT2-Base → GPT2-Large for GPT2, (4) DeiT-S → DeiT-B, and (5) CaiTXS → CaiT-S for vision transformer.
5 More details are included in the Appendix C.
Baselines.
We compare our approach with the following baselines.
(1) training from scratch baseline where we train the larger transformer without using any smaller pretrained models, (2) progressive training methods designed for growing depth in transformers (StackBERT (Gong et al., 2019) and
MSLT (Yang et al., 2020)), (3) bert2BERT (Chen et al., 2021b) that extends Net2Net (Chen et al., 2015) for width expansion and stacking for depth expansion, (4) KI (Qin et al., 2021) which uses distillation for transferring knowledge from the smaller model to the larger model.
Implementation Details.
We always use 100 gradient steps to learn the LEGO operator for all models, which is negligible in terms of FLOPs/wall time compared to full training after initialization.
We train both BERT and RoBERTa models for 400 K steps with a warmup of 10 K steps.
We remove the next-sentence prediction task (Liu et al., 2019) and use a fixed sequence length offor pretraining both models.
For BERT, we use a batch size of 256 and a learning rate of 2 e − 4 , while we use a batch size of 1024 and a learning rate of 8 e − 4 for training RoBERTa models.
Following (Shen et al., 2022), we train GPT2 models with a batch size of 384 and sequence length
[Figure]
either BERT-Small or BERT-Base for accelerating BERT-Large training.
[Table]
of 1024 . For vision transformer, we build our models based on DeiT (Touvron et al., 2021a) and CaiT (Touvron et al., 2021b), and apply their default hyper-parameters for training on ImageNet dataset.
We train all our vision transformers for 300 epochs with a batch size of 1024 .
For transfer learning with BERT/RoBERTa, we follow (Tan & Bansal, 2020), we train for 3 epochs with a learning rate of 1 e − 4 and a batch-size of 32 for all tasks in GLUE.
On SQuAD v1.1 and SQuAD 2.0, we fine-tune for 2 epochs with a learning rate of 5 e − 5 and a batch size of 12 .
We run both GLUE and SQuAD evaluations three times with different random seeds and report the mean numbers.
For transfer learning experiments on DeiT, we finetune the pretrained models with 1000 epochs, batch size 768, learning rate 0 .
01 , and use the same data augmentation in training on ImageNet.
We use the same pretraining data and experimental settings for all the baselines (including our approach) for a fair comparison.
Note that we include the additional compute required for training LEGO in all our tables and figures.
However, since our LEGO operator is parameter efficient and we only train it for 100 steps, the influence on visualization and quantitative saving percentages is negligible.
4. R ESULTS AND A NALYSIS
BERT.
Figure 2 shows comparison between the different baselines for training BERT models.
As seen from Figure 2(a), LEGO saves 44 .
7% computational cost (FLOPs) of training BERT-Base (12 layers, 768 dimensions) from scratch by reusing BERT-Small which is almost half of the size.
On the same setting, LEGO offers 40 .
7% savings in wall time compared to training from scratch (Figure 2(b)).
Among the compared methods, StackBERT is the most competitive in terms of both
FLOPs and wall time.
However, our data-driven approach to growth obtains +10 .
and +7 .
2% improvements in FLOPs and wall time on top of StackBERT.
Similarly, LEGO significantly outperforms the recent bert2BERT method which saves about 30% computational costs.
We observe that
KI does not provide any real savings in training as it requires additional computation for knowledge distillation.
Furthermore, Figure 2(c) shows that our LEGO approach is flexible in growing either
BERT-Small or BERT-Base for accelerating BERT-Large training.
As expected, reusing BERT-Base instead of BERT-Small leads to 14 .
9% more savings in FLOPs ( 45 . 2% vs 30 . 3% ) as BERT-Base contains more implicit knowledge in its parameters.
Finally, Table 3 shows the per-task performance of different BERT-Base models on both GLUE and SQuAD benchmarks.
Despite being trained significantly faster than scratch, BERT trained with our LEGO approach achieve very similar performance comparing to the baselines on both benchmarks. [Figure]
RoBERTa and GPT2.
Figure 3(a-b) shows the results on RoBERTa, whose training recipe uses larger batch size and learning rate than BERT.
LEGO similarly accelerates RoBERTa training, which indicates that our method is robust to optimization hyperparameters.
On GPT2, LEGO saves 22 . 5% computation cost of training GPT2-Large (345M parameters) by reusing GPT2-Base (117M parameters)
(Figure 3(c)).
These consistent improvements clearly show that LEGO is universally effective for accelerating training of transformers across different model architectures and sizes.
[Figure]
Vision Transformer.
Figure 4 shows that by using DeiT-S, LEGO can save 55 .
4 % FLOPs and 52 % GPU wall time to reach the same performance of 81% on ImageNet.
Interestingly, the model initialized by our datadriven growth operator (w/ only 100 gradient steps of tuning) can already achieve 72 % accuracy at the beginning of training and leads to the final accuracy of 81 .
7% at the end of the training.
Compared to the next best method, bert2BERT, LEGO obtains more than 15% savings, which once again demonstrates the effectiveness of our approach in growing vision transformers as well.
Tab.
2 shows that fine-tuning results on downstream tasks are also on-par performance with model trained from scratch, showing that LEGO does not harm the model generalization capability when transferred to downstream datasets.
We also find similar savings in CaiT-XS → CaiT-S where LEGO saves FLOPs by 52 . 6% and wall time by 46 . 1% over training CaiT-S from scratch on ImageNet (see Appendix H for more details).
[Table]
Combining with Other Training Strategies.
We also validate that our LEGO approach can be effectively combined with other orthogonal strategies such as layer dropping (Zhang & He, 2020), token dropping (Hou et al., 2022), and staged training (Chen et al., 2021b).
More details are included in Appendix D. Figure 5 shows that LEGO can be combined with other training techniques to improve the computational savings by 4 .
7% , 7 . 4% , and 8 . 2% with layer dropping, token dropping and staged training, respectively.
Following (Chen et al., 2021b), we also apply staged training strategy to bert2BERT and observe that LEGO still outperforms bert2BERT with staged training by 16 .
7% (see Figure 5(c)).
[Figure]
4. A BLATION S TUDIES
[Figure]
Depth Only Expansion.
In this experiment, we examine the effectiveness of our proposed depth expansion operator ( L depth ) by only growing depth of a BERT from 6 layers to 12 layers, i.e, (BERT(6, 768) → BERT(12, 768).
We compare with stacking (StackBERT, Gong et al., 2019), Interpolation (InterBERT,
Chang et al., 2017; Dong et al., 2020) (see
Eq. 1), and MSLT (Yang et al., 2020).
For
LEGO, we only apply its L depth component to the pre-trained model weights.
Results in Figure 6(a) shows that a datadriven approach works well even when just growing across the depth dimension.
[Table]
Width Only Expansion.
We also verify the effectiveness of
R width by only extending BERT width from 512 to 768 , i.e., (BERT(12, 512) → BERT(12, 768).
We compare LEGO based initialization with direct copy (Wei et al., 2016), function preserving initialization (FPI, Chen et al., 2015), and advanced knowledge initialization (AKI, Chen et al., 2021b).
LEGO’s width expansion component outperforms all other methods, as shown in Figure 6(b).
Learning Steps.
Our main experiments just use 100 gradient steps to grow.
We tune our LEGO operator on the pretraining set for 100 , 500 , 1000 , and 10000 steps and compute the additional FLOPs for BERT-Small → BERT-Base training.
Table 3 shows that training
LEGO operator within 1000 steps result in the identical model convergence (reaching 1 . 8 PPL at 215 K steps).
This suggests tuning model weights under the linear constraints of LEGO can achieve a faster convergence.
Training LEGO operator for more than 10000 steps can provide a model with slightly faster convergence ( 214 K steps), but results in less savings overall.
C ONCLUSION
We introduced an efficient data-driven approach for accelerating training by learning to grow transformers.
We initialize the larger transformer’s parameters as a linear mapping from the smaller pretrained model’s parameters and factorize the mapping to be a composition of sparse width- and depth-expansion operators with a Kronecker factorization that groups parameters into layers and neurons.
We demonstrate the effectiveness of our proposed approach on both language and vision transformers of different sizes, outperforming several competing methods.
While our compute resources prevented us from applying LEGO to even larger transformers, it would be interesting to see if this can be applied on top of extremely large models such as GPT3 (Brown et al., 2020).
A U NIVERSALITY OF LEGO O PERATOR
Proposition 1.
StackBERT (Eq. 1), Interpolation (Eq. 1), and Net2Net (Eq. 2) are all the special cases of the LEGO operator (Eq. 8).
Proof.
We prove Proposition 1 by constructing parameters in L depth and R width .
Stacking.
Stacking based methods (Gong et al., 2019; Yang et al., 2020) duplicate the entire lower blocks on top of the small model to the form new layers (Eq. 1).
Formally, we show this operation can be done by the following operator:
[Equation]
Interpolation.
Interpolation based methods (Chang et al., 2017; Dong et al., 2020) interleave each layer for twice.
We can construct the following matrix to achieve layer interpolation (Eq. 1).
[Equation]
We remark that any rearrangement of layers to construct new layers (mathematically a permutation of existing layers with replacement) can be constructed in a similar way.
Thus, our LEGO operator is a super set of them all.
Net2Net.
Since we show in Eq. 6, the Kronecker factorization on R l amounts to decomposing the general growth operator into in-dimension and out-dimension expansion.
We can construct
Net2Net (Chen et al., 2015) based growth by simply letting:
L depth =
I
[Equation]
R width =  
A
[Equation]
A
[Equation]


A l = (cid:20)
[Equation]
(cid:101) S l
[Equation]
(cid:21) ,
B l = (cid:20) IS l , (cid:21) (11) (12) where S l ∈ { 0 , 1 } ( D 2 − D 1) × D 1 is a selection matrix to enlarge the out dimension, and (cid:101) S l − 1 =
S l − 1 diag( 1 ⊤ S l − 1 ) − 1 copies the selection from S l − 1 with normalization to guarantee functionality preserving in expansion.
B G ROWING T RANSFORMERS WITH LEGO
Transformer consists of embedding layer, multi-block attention layer, and an output layer.
The core ingredient attention block consists of a Multi-Head Attention (MHA) module followed by a
FeedForward Network (FFN), with a skip connection across the both blocks.
Applying LEGO requires the following considerations:
Embedding Layers.
For both language and vision transformers, embedding layer can be regarded as a linear layer, whose inputs are one-hot embeddings in language models.
We draw a learnable matrix B ( emb ) to extend its output dimension.
Multi-Head Attention Block.
An attention layer in transformer consists of multi-head attention weights ( W Q , W K , W V ) and a linear projection ( W O ).
Let A kl and B kl with k ∈ { Q, K, V, O } be the in- and out-dimension expansion matrices (Eq. 6) for query, key, value, and projection in the l -th layer, respectively.
Applying B kl to W k ( k ∈ Q, K, V ) constructs new heads by weighted summing rows of all existing heads.
To make sure the new input and output channels are aligned across modules, we tie our LEGO operator with the following scheme:
(1) A kl =
B ( emb ) ⊤ for ∀ k ∈ { Q, K, V } , (2) A Ol = B ( V ) ⊤ l , (3) B Ol = B
( emb )
for ∀ l ∈
[ L 1 ] , among which the last constraint is added for residual connection (Chen et al., 2021b).
Both Bias terms and layer normalization inherit the associated linear transformations’ out-dimension expansion matrices to grow the width.
For depth expansion, each module independently combines the same module from other layers (Eq.
8) with learnable coefficients w .
Feed-Forward Networks.
Each attention block is followed by a two-layer FFN.
Let A k l and B k l with k ∈ { fc 1 , fc 2 } be the in- and out-dimension expansion matrices (Eq. 6) for the first and second FFN layer in the l -th layer, respectively.
Similarly, we tie the parameters for feed-forward networks: A ( fc 1) l = B ( emb ) ⊤ , A ( fc 2) l
= B ( fc 1) ⊤ l and B ( fc 2) l
= B ( emb ) .
Output Layer.
For output head, we ensure A ( out ) =
B ( emb ) ⊤ , since the output dimension of attention layers are always aligned with the B ( emb ) by our construction.
The output layer does not need out-dimension expansion.
Algorithm 1 summarizes a forward pass of LEGO with transformer.
C M ODEL C ONFIGURATIONS
We summarize the settings of different transformer models used for our experiments in Table 4.
For
BERT and RoBERTa, we re-use the code base provided by (Tan & Bansal, 2020).
For GTP2, we follow the model configuration of OpenAI and use the pre-training code provided by (Shen et al., 2022).
For DeiT, we use their official codebase (Touvron et al., 2021a).
[Table]
D O RTHOGONAL T RAINING S TRATEGIES
For layer dropping, we follow the same progressive dropping rate schedule with (Zhang & He, 2020), and set the maximum dropping rate to 0.1 to recover the performance.
For token dropping, we randomly set 15% tokens aside in the middle layers.
In the first 50 k steps of staged training, only a sub-network is activated and trained, and afterwards, we do full-model training for 350k steps.
Our LEGO is mainly proposed for improving efficiency of the pre-training stage and hence is compatible with various fine-tuning schemes like full model finetuning, adapters (Houlsby et al., 2019; Pfeiffer et al., 2020) or prompt tuning (Lester et al., 2021; Jia et al., 2022) for adaptation to downstream tasks. For full model finetuning, Table 1 and Table 2 show that despite being trained significantly faster than scratch, BERT and DeiT trained with our LEGO approach respectively, achieve
3: Ω ( emb ) ← B ( emb ) W ( emb ) do ▷ Width Expansion 5: Ω Ql ← B ( Q ) W Ql B ( emb ) ⊤ 6: Ω Kl ← B ( K ) W Kl B ( emb ) ⊤ 7: Ω Vl ← B ( V ) W Vl B ( emb ) ⊤ 8: Ω Ol ← B ( emb ) W Vl B ( V ) ⊤ 9: Ω ( ln 1) l ← B ( emb ) W ( ln 1) l 10: Ω ( fc 1) l ← B ( fc 1) W Vl B ( emb ) ⊤ 11: Ω ( fc 2) l ← B ( emb ) W V l B ( fc 1) ⊤ 12: Ω ( ln 2)
l ← B ( emb ) W ( ln 2) l 13: end
14: for l = 1 , · · · , L 2 do ▷ Depth Expansion 15: Ω Ql ← (cid:80) L 1 j =1 w Ql,j Ω Qj 16:
Ω Kl ← (cid:80) L 1 j =1 w Kl,j Ω Kj
: Ω Vl ← (cid:80) L 1 j =1 w Vl,j Ω Vj 18: Ω Ol ← (cid:80) L 1 j =1 w Ol,j Ω Oj 19: Ω ( ln 1) l ← (cid:80)
L 1 j =1 w ( ln 1) l,j Ω
( ln 1) j 20: Ω ( fc 1) l ← (cid:80)
L 1 j =1 w ( fc 1) l,j Ω ( fc 1) j 21: Ω ( fc 2) l ← (cid:80)
L 1 j =1 w ( fc 2) l,j Ω ( fc 2)
j 22: Ω ( ln 2) l ← (cid:80)
L 1 j =1 w ( ln 2) l,j Ω ( ln 2) j 23: end for 24: Ω ( out ) ← W ( out ) B ( emb ) ⊤ 25: Train transformer with parameters Ω . [Table]
very similar performance comparing to the baselines on downstream tasks.
We also test BERT-Base models trained using different baselines by using adapterfusion (Pfeiffer et al., 2020) instead of full finetuning on GLUE benchmark.
Table 5 shows that LEGO also achieves on-par performance with model trained from scratch under adapter-based tuning with 44.7% savings in FLOPs abd 40.5% savings in wall time.
This shows that LEGO does not harm the model generalization capability when adapters are used as a parameter-efficient finetuning strategy for transferring a trained model to downstream datasets.
[Figure]
Method
SSTMNLI
MRPC
CoLA
QNLI
QQP
STS-B
Average (
[Table]
StackBERT 88.
[Table]
[Table]
[Table]
[Table]
LEGO 88.
F R EUSING SMALLER MODELS TRAINED FOR ONLY FEW STEPS
LEGO focuses on utilizing the knowledge of smaller models that have already been pretrained and available.
In this section, we investigate how LEGO can leverage smaller existing models that are only trained for few steps to accelerate training of a larger model.
We perform an experiment on
BERT-Base by reusing a BERT-Small trained for only 50k steps
instead full training for 220k steps as used in our experiments.
Figure 7 shows that LEGO can still save 35.2% savings in FLOPs and 30.2% savings in wall time over the BERT-Base training from scratch.
G S TACK BERT VS LEGO AT THE END OF BERT-B ASE TRAINING
Table 1 shows that BERT trained with our LEGO approach achieve very similar performance compared to StackBERT on GLUE benchmark at the point where it reaches the log perplexity of the model trained from scratch.
In this section, we analyze the performance of StackBERT and LEGO inn detail for training a BERT-Base by reusing BERT-Small.
Figure 2 shows that StackBERT achieves lower log perplexity when FLOPs > 4e 18 .
However, both LEGO and StackBERT leads to very similar log perplexity ( 1 . 71 vs 1 . 72 ) at the end of training.
We also check the downstream
GLUE performance of both models at the end of training and find that they obtain very similar performance across all the tasks, as shown in Table 6.
H R ESULTS ON C AI T
In addition to DeiT (Touvron et al., 2021a), we perform additional experiments with CaiT (Touvron et al., 2021b) on ImageNet and find that while reusing CaiT-XS, LEGO offers about 52.6% savings in FLOPs and 46.1% savings in wall time over the CaiT-S training from scratch (see Figure 8).
[Figure]
[Table]
This once again corroborates that our proposed LEGO is universally effective for different vision transformers irrespective of the architecture and sizes.
I GLUE PERFORMANCE WITH LEGO INITIALIZATION
Table 1 shows the downstream fine-tuning results on GLUE and SQuAD for pretrained BERT models using different baselines.
The numerical results reflect that despite being trained significantly faster than scratch, BERT trained with our LEGO approach achieve very similar performance comparing to the baselines on both benchmarks.
In this section, we perform an additional experiment by directly finetuning a BERT-Base model initialized by LEGO (from BERT-Small) without any pre-training and observe that LEGO initialized model can benefit downstream tasks compared to a
BERT-Small model trained from scratch (1st row vs 2nd row).
Table 7 summarizes the results.
J S CALABILITY TO LARGE MODELS WITH BILLION + PARAMETERS
Our extensive experiments on BERT (Devlin et al., 2019), RoBERTa (Liu et al., 2019), GPT2 (Radford et al., 2019), DeiT (Touvron et al., 2021a) and CaiT (Touvron et al., 2021b) show that LEGO can consistently improve transformer training efficiency over the traditional way of training from scratch across domains and model sizes.
One interesting future direction of our work is scaling
LEGO to very large models with parameters more than 100B, such as GPT3 (Brown et al., 2020).
While we currently do not possess the compute resources for this extreme large-scale study, we perform a preliminary experiment on GPT2-1.5B (Radford et al., 2019) by using GPT2-Large as the initialization.
We train for 15k steps on C4 dataset (Raffel et al., 2020) and find that our proposed
LEGO saves about 39% computation cost (FLOPs) of training GPT2-1.5B from scratch to reach the same log perplexity (which is 3.3).
We believe that it is imperative to study the extent to which the benefits of LEGO remain at the scale on which the modern large language models are trained.
We hope to cover this in our future work.