January 01, 2021
Transformers have shown improved performance when compared to previous architectures for sequence processing such as RNNs. Despite their sizeable performance gains, as recently suggested, the model is computationally expensive to train and with a high parameter budget. In light of this, we explore parameter-sharing methods in Transformers with a specific focus on generative models. We perform an analysis of different parameter sharing/reduction methods and develop the Subformer. Our model combines sandwich-style parameter sharing, which overcomes naive cross-layer parameter sharing in generative models, and self-attentive embedding factorization (SAFE). Experiments on machine translation, abstractive summarization and language modeling show that the Subformer can outperform the Transformer even when using significantly fewer parameters.1
Recent improvements in NLP tasks can be attributed to the Transformer [1] model. The model has led to better deeply contextualized representations [2], better machine translation systems [1], and language models [3], [4]. Despite their success, one main drawback of training these models is their computational cost, being a greatly limiting factor for many, with training times and memory usage ballooning as model sizes increase to attain better performance.
With this in mind, there has been recent interest in making the Transformer more parameter-efficient to reap its performance benefits while making the model more computationally efficient and able to scale better. Many approaches have focused on automating model design with neural architecture search that aim at finding more efficient Transformer variations using gradient descent [5]–[7]. As such, these techniques are expensive, requiring a significant amount of GPU hours to find good designs. In contrast to these approaches, the model by [8] has directly tackled model parameter reduction in the context of deeply contextualized word representations, still attaining similar (or better) performance. In this paper, we take a similar approach and look to explore whether these ideas can be applied to sequence-to-sequence models in a simple manner by designing the Subformer.
The Subformer incorporates two novel techniques: (1) SAFE (Self-Attentive Factorized Embeddings), in which we use a small self-attention layer to reduce embedding parameter count, and (2) Sandwich-style Parameter Sharing, in which we develop a simple and intuitive technique for parameter sharing to be effective in Transformer models.
We evaluate the Subformer on machine translation, abstractive summarization, and language modeling, showing that our model can achieve similar or better performance compared with a base/big Transformer with a \(\sim\)40% parameter reduction and minimal modification to the original architecture, reinforcing the existing over-parameterization claims [7]–[9]. On WMT’14 EN-DE we achieve a BLEU score of 29.3, compared to Transformer-big’s 28.6 with 13M fewer parameters, and we also outperform the Transformer-XL model with a significant 3.6 lower perplexity and 37% fewer parameters.
Let us start by defining the notation to be used throughout the paper. We refer to the model dimension as \(d_m\), feed-forward projection dimension as \(\vec{d}_m\), the vocabulary size as \(V\), and the number of layers as \(L\). Note that, unlike standard Transformer models, in which the embedding dimension is kept the same as \(d_m\), we disentangle to embedding dimension to reduce parameter count. For this reason, we denote the embedding dimension to be \(d_e\).
We propose to reduce the number of parameters in our embedding layers, which can take up to 25% of the total parameter count (in the case of Transformer-base, [1]), using a small self-attention layer. Specifically, we look to reduce the embedding size by disentangling the model dimension from the embedding dimension, reducing the embedding dimension \(d_e\), and then projecting this to the model dimension \(d_m\) using a small self-attention sub-layer followed by a feed-forward module.
Given vocabulary size \(V\), the usage of a standard embedding layer would result in \(V \times d_m\) parameters. However, considering that the power of Transformers lies in their ability to learn contextual representations with attention, using a smaller \(d_e\) for non-contextual embeddings and then projecting to \(d_m\) is intuitively an effective method for parameter reduction [8]. This results in a significant parameter reduction for values of \(d_e \ll d_m\).
When using SAFE, the embedding layer is composed of a regular token \(\rightarrow\) vector embedding matrix \({\boldsymbol{E}}\in {\mathbb{R}}^{V \times d_e}\). This is followed by projecting the embeddings (summed with the positional encodings [1], denoted by \(PE\)) to the model dimension \(d_m\) using SAFE. Once we have our SAFE embeddings, we feed them through the first model layer —the base of the sandwich. The output of this first layer is then projected to the sandwich dimension \(d_s\). Once fed through the shared sandwich layers, we then project the output back to the model dimension using an MLP. The output of the projection is then fed through the final model layer to produce the output vectors.
Current models [3], [4], [8] often use a single linear projection, i.e. \(V \times d_e + d_e \times d_m\). In contrast, we empirically show that simply contextualizing this projection with a small self-attention layer results in stronger performance with a minimal addition of parameters —especially in the encoder-decoder case, where the input embedding layer and output projection are often tied [10] (Table 1).
Weight sharing techniques, despite being surprisingly effective, have been relatively unexplored in the context of generative Transformers. However, this has been shown to be powerful for leveraging models with large capacity and less memory usage/computation [8], [11].
| Model | Param. | BLEU |
|---|---|---|
| DeFINE [12] | 52M | 27.0 |
| \(d_e = 128\), Linear | 48M | 26.0 |
| \(d_e = 256\), Linear | 53M | 27.1 |
| \(d_e = 256\), 2-Layer Linear | 54M | 27.2 |
| \(d_e = 128\), SAFE | 48M | 26.6 |
| \(d_e = 256\), SAFE | 54M | 27.7 |
| [1] | 65M | 27.3 |
| Transformer-base (reimpl.) | 61M | 27.7 |
| Model | Param. | BLEU |
|---|---|---|
| All-Shared | 24M | 14.3 |
| All-Shared, \(d_m= 768\) | 41M | 22.0 |
| All-Shared (Independent FFN) | 27M | 22.4 |
| All-Shared (except last) | 31M | 23.2 |
| Every 2 layers shared | 38M | 27.2 |
| Sandwich | 38M | 27.3 |
| Sandwich, \(L=8\) | 38M | 27.7 |
| [1] | 65M | 27.3 |
| Transformer-base (Our reimpl.) | 61M | 27.7 |
Given that the output of each Transformer layer depends directly on its two sub-layers —multiheaded attention and the feedforward module— when discussing alternatives for parameter sharing across transformer layers there are several options. As we aim to leverage the aforementioned properties of weight sharing, we performed preliminary experiments, investigating the capabilities of weight sharing in the following five settings: (1) All-shared Naively sharing all encoder and all decoder layers —that is including both of their sub-layers, following [8], [13]. (2) All-Shared (Independent FFN) Naively sharing all encoder and all decoder layers but allowing each layer \(l \in [2, \cdot, L]\) to have an independent feed-forward sub-layer. (3) All-Shared (except last) Sharing weights across layers \(l \in [1,\ldots, L-1]\) such that layer \(L\) remains independent. (4) Every 2 layers shared Sharing every two layers, i.e. \([1,2],[3,4],[5,6]\) in the case of a 6-layer transformer. (5) Sandwich Finally, we only share the middle or central layers (i.e. \(2 \leq l \leq L-1\)), leaving layers \(1\) and \(L\) to have independent sets of parameters.
Table 2 summarizes the results of our exploratory study. As can be seen, naive parameter sharing/tying approaches do not offer any advantages, hurting performance significantly (\(\sim\)50%) when compared to the regular Transformer. However, our results also show that when combined properly, using Sandwich-style parameter sharing, we can attain a good balance of parameter reduction and performance. When compared to tasks such as pre-training deep contextualized word representations, generative tasks such as machine translation require informative token-level representations for each input token to be accurately translated. In this context, we surmise that the success of Sandwich-style parameter sharing on this sequence-to-sequence task is a result of it being able to have the input and output layers (arguably, the most important layers) be trained independently, allowing them to learn different operations than the sandwich layers.
Having introduced our proposed parameter reduction techniques, we will now explain the Subformer architecture. The Subformer is composed of four main components, for both the encoder and decoder: the embedding layer, the model layers, the sandwich module and the projection layers. We disentangle the sandwiched layer dimension from that of the model layer, allowing the sandwich layer width to be larger than the rest of the model. For this reason, we denote the dimension of the sandwiched layer to be \(d_s\) and its corresponding feed-forward dimension to be \(\vec{d}_s\).
We apply our method to a variety of sequence modeling tasks: neural machine translation, summarization, and language modeling. We compare Transformer-base and big [1] with the Subformer trained in the same setting. We also include simple sandwich-style parameter sharing (denoted as Sandwich-{base,big}) and the usage of only SAFE as an ablation of what these techniques do in their naive forms when decoupled. Additional implementation and training details with hyperparameter settings are in the appendix.
We evaluate our model on two standard translation benchmarks: WMT’14 English-German (EN-DE) and WMT’16 English-Romanian (EN-RO). Following previous work, we evaluate all models using tokenized BLEU [14]. In order to better contextualize our results, we consider parameter-efficient models DeLighT [7] (contemporaneous work), and the Evolved Transformer [6].
We test the model’s ability to process long documents on the CNN-DailyMail summarization benchmark [15], [16], comprising over 280K news articles paired with multi-sentence summaries. We also compare effects of BART [17] pretraining (details in Appendix 7). For this task we contexutalize our results with specialized architectures such as Pointer-Generator Networks [18], and methods leveraging pretraining: RobertaShare [19], BertExtAbs [20], and BART [17]. We evaluate using ROUGE 1,2,L [21].
We evaluate on the large-scale Wikitext-103 dataset [22]. Models are evaluated in terms of perplexity on the test portion. In order to better contextualize our results, we consider the QRNN [23], Transformer-XL [4] and Deep Equilibrium Model (DEQ) [24], which also employs parameter sharing.
| Base Models | WMT’14 EN-DE | WMT’16 EN-RO | ||
|---|---|---|---|---|
| 2-3 (r)4-5 | Param. | BLEU | Param. | BLEU |
| DeLighT | 37M | 27.6 | 22M | 34.3 |
| Evolved Transformer | 48M | 27.7 | — | — |
| DeLighT | 54M | 28.0 | 52M | 34.7 |
| Evolved Transformer | 64M | 28.2 | — | — |
| Transformer-base (orig) | 65M | 27.3 | 62M | 34.2\(^\dagger\) |
| Transformer-base (ours) | 61M | 27.7 | 62M | 34.1 |
| Sandwich-base | 38M | 27.3 | — | — |
| Only SAFE, \(d_e = 256\) | 54M | 27.7 | — | — |
| Subformer-small | 38M | 27.7 | 20M | 34.1 |
| Subformer-base | 52M | 28.1 | 48M | 34.7 |
| Subformer-mid | 63M | 28.5 | — | — |
| Big Models | Param. | BLEU |
|---|---|---|
| Evolved Transformer | 222M | 29.0 |
| Transformer-big (orig) | 213M | 28.4 |
| Transformer-big (ours) | 210M | 28.6 |
| Sandwich-big | 122M | 28.6 |
| Subformer-large | 197M | 29.3 |
Tables 3 and 42 summarize our results on machine translation. Firstly, we note that our Transformer baselines outperform [1] (base: 27.3 \(\rightarrow\) 27.7, big: 28.4 \(\rightarrow\) 28.6). We surmise that this is due to training for longer and with a larger batch size.
The Subformer outperforms our Transformer baselines when trained in the same setting, with similar or fewer parameters. Subformer-small reduces parameters by 40%, matching performance with our Transformer baselines. Subformer-base and mid, outperform our model significantly (0.4, 0.8 BLEU) when using a fewer/similar number of parameters. Furthermore, we note that Subformer-mid performs similarly to the Transformer-big model (210M params.) in Table 4, despite a 70% parameter reduction.
For our large set of models (Table 4), Sandwich-big achieves the same performance as our Transformer-big reimplementation, but with 40% fewer parameters. We believe this to be an indication of the capability of Sandwich-style parameter sharing as the encoder/decoder layers get wider, while also providing further evidence for the overparameterization of the Transformer. Subformer-large, with 197M parameters achieves a significant 0.7 BLEU score gain over Transformer-big, despite using 13M fewer parameters.
Results for language modeling can be seen in Table 5. The Subformer uses adaptive input embeddings [3] instead of SAFE embeddings, following common practice. We also train two Transformer baselines with the same setup —one with the same amount of parameters and another with a similar parameter count to Transformer-XL— to provide better context for comparison. Task-specific techniques that can be applied during training, such as caching [4] or other methods applied during inference time [26], [27] can further improve all models so we do not focus on these.
| Model | Param. | CL | PPL |
|---|---|---|---|
| QRNN | 151M | — | 33.00 |
| DeLighT | 99M | 480 | 24.14 |
| Transformer-XL | 151M | 640 | 24.03 |
| DEQ | 110M | — | 23.20 |
| Adaptive Inputs | 247M | 480 | 19.03 |
| Adaptive Inputs (4 Layer) | 96M | 480 | 26.42 |
| Adaptive Inputs (8 Layer) | 146M | 480 | 22.32 |
| Subformer | 83M | 480 | 20.88 |
| 96M | 480 | 20.39 | |
| 122M | 480 | 19.90 |
As seen in Table 5, the Subformer outperforms the baselines by a significant margin (between 1.4 and 6.5 perplexity), with a significant reduction in parameters.
| Model | Params. | R1 | R2 | RL |
|---|---|---|---|---|
| Ptr-Gen+Cov | — | 39.5 | 17.3 | 36.4 |
| RobertaShare | 152M | 40.3 | 18.9 | 37.6 |
| BertExtAbs | 220M | 41.7 | 19.4 | 38.8 |
| BART | 406M | 44.2 | 21.3 | 40.9 |
| Transformer (3 Layer) | 57M | 40.0 | 17.5 | 36.7 |
| Transformer | 77M | 40.1 | 17.6 | 36.8 |
| Transformer-BART | 77M | 41.2 | 18.7 | 37.6 |
| Subformer-base | 57M | 40.9 | 18.3 | 37.7 |
| Subformer-BART | 57M | 41.6 | 19.2 | 38.4 |
For the CNN/Daily Mail summarization task we use Subformer-base. We also pretrain a Transformer and Subformer model with the same architecture on Wikipedia (details in Appendix 7). As can be seen in Table 6, the Subformer outperforms two Transformer baselines with both the same parameter count and its respective Transformer-base configuration in both settings, demonstrating the Subformer’s performance on a variety of tasks and with longer sequences.
We found training time to consistently speed up by 10-30%, and inference speed to either be faster by 10-20% (keeping \(d_s=d_m\)) to be slightly slower by 10-30% (when \(d_s \gg d_m\)) (due to more operations, similar to [8]). The Subformer converges faster most likely due to fewer parameters to optimize. Given the fewer number of parameters, it can be expected for the models to converge with fewer iterations. We test this on the task of language modeling, where we found that the Subformer converged 65% faster than its Transformer counterpart, as shown in Table 7. We also measure inference speed on our machine translation models (Table 8).
| Model | Param. | Iterations | Dev. PPL |
|---|---|---|---|
| Adaptive Inputs | 146M | 272K | 22.31 |
| Subformer | 83M | 97K | 20.84 |
| Model | Param. | Speed \(\uparrow\) | BLEU |
|---|---|---|---|
| DeLighT | 37M | 0.30x | 27.6 |
| Transformer | 61M | 1.00x | 27.7 |
| SAFE, \(d_e=256\) | 54M | 1.17x | 27.7 |
| Sandwich-base | 38M | 1.26x | 27.3 |
| Subformer-base | 52M | 0.75x | 28.1 |
In this paper we have presented the Subformer, a parameter-efficient Transformer-based generative model primarily based on two simple parameter factorization/sharing techniques. Our empirical results on three sequence modeling tasks show that the Subformer can achieve similar or better performance compared with a base/big Transformer with a \(\sim\)40% parameter reduction. Furthermore, the simplicity of our approach allows the Subformer to be used in conjunction with other parameter reduction techniques in the literature, for even smaller but performant models. We hope our work incites interest in parameter sharing techniques for an even wider range of Transformer models, ultimately helping reduce their computational cost in general.
This work has impact in the field of natural language processing, and develops a more efficient approach for learning performant generative models. As with much of language technology has the potential to be both used for good and used maliciously. We also experiment with pretraining, learning representations in an unsupervised way, which is likely to capture and amplify biases found in the data. However, our approach has a potential positive impact given the lower cost and energy expenditure needed to train our proposed model.
We thank Jorge Balazs, Yusuke Iwasawa, Jungo Kasai, Cristian Rodriguez-Opazo, Alfredo Solano, Yutaro Yamada, and Victor Zhong for their helpful feedback and discussions over this work. MR is grateful to the Masason Foundation for their support.
The Subformer is a play on words, referencing its small size - i.e. sub-, as well as the Sandwich-style parameter sharing technique, referencing the type of sandwich.
When using SAFE, our parameter count would result in \(V \times d_e + \ 5d_e^2 + d_e \times d_m\) parameters, where the first term represents the embedding layer, the value \(5d_e^2\) groups the query, key, and value projections and 2 output feed-forward layers, and \(d_e \times d_m\) represents the linear projection from the embedding dimension to the model dimension. As mentioned in the paper, this results in a significant parameter reduction for values of \(d_e \ll d_m\).
As we tie the decoder’s output projection layer (returning a distribution over the vocabulary) with the decoder’s input embedding matrix, we project the decoder’s last hidden state (with dimension \(d_m\)) to \(d_e\) using a two layer MLP. Also, when we perform encoder attention in the decoder’s Sandwich Module, we simply linearly project the query from the decoder from \(d_s\) to \(d_m\) and then project it back to \(d_s\) once the attention operation is complete.
| Method | Embedding Memory Usage | Model Memory Usage |
|---|---|---|
| Transformer | \(d_m \times V\) | \(L(4d_m^2 + 2\big(\vec{d}_m \times d_m)\big)\) |
| Sandwich (naive) | — | $3(4d_m^2 + 2(4_m d_m)) $ |
| Subformer | \(d_e \times V+ 5d_e^2 + d_e \times d_m\) | \(2\big(4d_m^2 + 2(\vec{d}_m \times d_m)\big) + \big(4d_s^2 + 2\big(\vec{d}_s \times d_s + 2(d_s\times d_m)\big)\big)\) |
Table [tb:mem] summarizes the memory footprint of our proposed techniques. In this table, the benefits of Sandwich-style parameter sharing can be seen as the number of independent layers is controlled to be \(L \leq 3\), however, Transformers generally need to be deeper (with a standard of \(L = 6\)) to learn more meaningful representations with the parameter count scaling linearly with respect to the layer count. Similarly, the benefits of disentangling the model dimension from the embedding dimension can be seen as well. Due to the parameter reduction attained by these techniques, the models can be trained in memory-constrained scenarios with a larger batch size.
Training was done on 8 GPUs on a single DGX-1 Machine, while training on 16 GPUs was done using multiple compute nodes of a compute cluster. We train all base/small models on 8 NVIDIA Tesla V100 GPUs. For all big/large models, we train on 16 NVIDIA
Tesla V100 GPUs. All models were trained with mixed precision [28] and are implemented in PyTorch [29] using our modification of fairseq [30].
We train using 8192 tokens per GPU an update frequency of 2, for small, base models. For large models, we train on 16 GPUs with 4096 tokens per GPU with an update frequency of 2. We follow the training setup of [31]: we use the same weight initialization scheme as BERT [2], sampling weights from \(\mathcal{N}(0,0.02)\), initializing biases to zero and setting layer normalization parameters \(\beta\) and \(\gamma\) to be \(0\) and \(1\), respectively. For regularization we use the best of \([0.1,0.2,0.3]\) dropout, weight decay of \(0.01\), while using label-smoothed cross entropy loss with \(\epsilon = 0.1\). We train using an effective batch size of 128K tokens. The models are trained using Adam [32], with hyper-parameters \(\beta = (0.9,0.999)\) and \(\epsilon = 10^{-6}\). We warm up the learning rate to a peak of \(5 \times 10^{-4}\) within 10K iterations and then decay the learning rate with the inverse square root schedule. When creating the final model, we use the checkpoint with the lowest loss on the development set and generate using a beam size of 5 [1], tuning the length penalty of \(\alpha \in [0.0,0.2,\)…\(,2.0]\) in the validation set. We perform early stopping, training for a maximum of 250K iterations.
We use the following settings for our models: (1) Subformer-small has \(d_m = 512\), \(d_s = 768\), \(d_{e} = 256\) and \(L=8\), (2) Subformer-base has \(d_m = 512\), \(d_s = 1024\), \(\vec{d}_s = 3072\), \(d_e = 320\), (3) Subformer-mid has \(d_m = 768\), \(d_s = 768\), \(d_e = 350\) and (4) Subformer-large has \(d_m = 1024\), \(d_s = 2048\) and \(d_e = 512\). For WMT’16 EN-RO, our small model has \(d_m = 320\), \(d_s = 512\) and \(d_{e} = 192\) and our base model has \(d_m = 512\), \(d_s = 640\), and \(d_e = 384\).
In terms of datasets, we make use of the same pre-processed data used by [31] for WMT’14 EN-DE with a 32K BPE [33] vocabulary and during evaluation we perform de-hyphenation [1]. We use the same data as [34] for WMT’16 EN-RO with a 35K BPE vocabulary.
We follow [35] and use the official ROUGE-1.5.5.pl script with parameters -m -a -n 2. As mentioned in the paper, our
model configuration is the same as Subformer-base, but we set \(d_e = 256\). Articles are truncated to 400 tokens [18]
and we use a BPE vocabulary of 32K types [35]. We follow the training schedule of [35]. During inference, we tune generation length in the range of {40, 50, 60} and use tri-gram blocking, following standard practice. When pretraining, we pretrain on Wikipedia (14GB) for 100K
iterations, using a batch size of 512K tokens. We use a learning rate of 7e-4, warmed up over 10K iterations.
When training our language models, we use 8 GPUs with 3072 tokens per GPU and an update frequency of 3, following [3]. Models are trained using Nesterov’s accelerated gradient optimizer [36], warming up the learning rate to 1.0 for 16K iterations, and then annealing for 270K iterations using a cosine annealing schedule. We use three configurations: (1) \(d_m = 768, \vec{d}_m = 3072, d_s = 1536, \vec{d}_s = 6144\), (2) \(d_m = 768, \vec{d}_m = 4096\) and \(d_s = 2048, \vec{d}_s = 6144\) and (3) \(d_m = 1024, \vec{d}_m = 4096\) and \(d_s = 2048, \vec{d}_s = 6144\). All models use \(L = 12\). Our considered dataset, Wikitext-103, contains 103M tokens and has a vocabulary of nearly 270K.
Given the effectiveness of the Transformer, improving the architecture has been of much interest to the NLP community. Within this domain, one branch of research concerns the reduction of the quadratic complexity (w.r.t. sequence length) of the Transformer’s core self-attention mechanism [11], [37], pushing it down to linear or log-linear complexity. The second branch of research regards improving the expressiveness of Transformer models, by using more layers [38], or by improving the architecture [6], [11]. The third branch of research regards improving the parameter efficiency of Transformers. Approaches towards this goal include neural architecture search approaches [5], [6], where new Transformer-based architectures are learned using gradient descent, more manually crafted approaches [7], [13], as well as weight-sharing approaches [8], [11]. The work most similar to ours is ALBERT [8] in which complete weight sharing is used to pre-train deep contextualized word representations [2], [39]. Different from this work, we focus on common NLP generative/sequence-to-sequence tasks versus large-scale pre-training and develop an approach to increase model capacity while reducing parameter footprint tailored to this setting.
We find prior work on pruning and quantizing Transformer models to reduce their size with a focus on sequence-to-sequence settings like machine translation [40], on encoder-based methods like BERT [41], [42] or with a more generic scope in mind [43], [44]. Our approach is orthogonal to these since we directly aim at reducing the number of parameters of Transformer models by proposing architecture modifications and weight sharing techniques.
As embeddings can substantially increase the parameter count as the vocabulary size increases, especially in sequence modeling scenarios, embedding reduction techniques have been proposed, including using a linear projection to project to a lower dimension [3], [4] or using combinations of block sparse transformations [7], [12]. We propose a self-attention based projection layer, SAFE, which we empirically show to outperform the aforementioned linear projection methods with a similar parameter count.
In all tables, results from other work used to contextualize our results are placed above the double bar.↩︎