FlowSeq: Non-Autoregressive Conditional Sequence Generation with Generative Flow

Most sequence-to-sequence (seq2seq) models are autoregressive; they generate each token by conditioning on previously generated tokens. In contrast, non-autoregressive seq2seq models generate all tokens in one pass, which leads to increased efficiency through parallel processing on hardware such as GPUs. However, directly modeling the joint distribution of all tokens simultaneously is challenging, and even with increasingly complex model structures accuracy lags significantly behind autoregressive models. In this paper, we propose a simple, efficient, and effective model for non-autoregressive sequence generation using latent variable models. Specifically, we turn to generative flow, an elegant technique to model complex distributions using neural networks, and design several layers of flow tailored for modeling the conditional density of sequential latent variables. We evaluate this model on three neural machine translation (NMT) benchmark datasets, achieving comparable performance with state-of-the-art non-autoregressive NMT models and almost constant decoding time w.r.t the sequence length.

Most seq2seq models are autoregressive, meaning that they factorize the joint probability of the output sequence given the input sequence P ✓ (y|x) into the product of probabilities over the next to-⇤ Equal contribution, in alphabetical order. 1 https://github.com/XuezheMax/flowseq ken in the sequence given the input sequence and previously generated tokens: (1) Each factor, P ✓ (y t |y <t , x), can be implemented by function approximators such as RNNs (Bahdanau et al., 2015) and Transformers (Vaswani et al., 2017). This factorization takes the complicated problem of joint estimation over an exponentially large output space of outputs y, and turns it into a sequence of tractable multi-class classification problems predicting y t given the previous words, allowing for simple maximum loglikelihood training. However, this assumption of left-to-right factorization may be sub-optimal from a modeling perspective (Gu et al., 2019;Stern et al., 2019), and generation of outputs must be done through a linear left-to-right pass through the output tokens using beam search, which is not easily parallelizable on hardware such as GPUs.
Recently, there has been work on nonautoregressive sequence generation for neural machine translation (NMT; Gu et al. (2018); ; Ghazvininejad et al. (2019)) and language modeling (Ziegler and Rush, 2019). Nonautoregressive models attempt to model the joint distribution P ✓ (y|x) directly, decoupling the dependencies of decoding history during generation.
A naïve solution is to assume that each token of the target sequence is independent given the input: (2) Unfortunately, the performance of this simple model falls far behind autoregressive models, as seq2seq tasks usually do have strong conditional dependencies between output variables (Gu et al., 2018). This problem can be mitigated by introducing a latent variable z to model these conditional dependencies: where p ✓ (z|x) is the prior distribution over latent z and P ✓ (y|z, x) is the "generative" distribution (a.k.a decoder). Non-autoregressive generation can be achieved by the following independence assumption in the decoding process: Gu et al. (2018) proposed a z representing fertility scores specifying the number of output words each input word generates, significantly improving the performance over Eq.
(2). But the performance still falls behind state-of-the-art autoregressive models due to the limited expressiveness of fertility to model the interdependence between words in y.
In this paper, we propose a simple, effective, and efficient model, FlowSeq, which models expressive prior distribution p ✓ (z|x) using a powerful mathematical framework called generative flow (Rezende and Mohamed, 2015). This framework can elegantly model complex distributions, and has obtained remarkable success in modeling continuous data such as images and speech through efficient density estimation and sampling (Kingma and Dhariwal, 2018;Prenger et al., 2019;. Based on this, we posit that generative flow also has potential to introduce more meaningful latent variables z in the nonautoregressive generation in Eq. (3).
FlowSeq is a flow-based sequence-to-sequence model, which is (to our knowledge) the first non-autoregressive seq2seq model utilizing generative flows. It allows for efficient parallel decoding while modeling the joint distribution of the output sequence. Experimentally, on three benchmark datasets for machine translation -WMT2014, WMT2016 and IWSLT-2014, FlowSeq achieves comparable performance with state-of-the-art non-autoregressive models, and almost constant decoding time w.r.t. the sequence length compared to a typical left-to-right Transformer model, which is super-linear.

Background
As noted above, incorporating expressive latent variables z is essential to decouple the dependencies between tokens in the target sequence in non-autoregressive models. However, in order to model all of the complexities of sequence generation to the point that we can read off all of the words in the output in an independent fashion (as in Eq. (4)), the prior distribution p ✓ (z|x) will necessarily be quite complex. In this section, we describe generative flows (Rezende and Mohamed, 2015), an effective method for arbitrary modeling of complicated distributions, before describing how we apply them to sequence-to-sequence generation in §3.

Flow-based Generative Models
Put simply, flow-based generative models work by transforming a simple distribution (e.g. a simple Gaussian) into a complex one (e.g. the complex prior distribution over z that we want to model) through a chain of invertible transformations.
Formally, a set of latent variables 2 ⌥ are introduced with a simple prior distribution p ⌥ ( ). We then define a bijection function f : Z ! ⌥ (with g = f 1 ), whereby we can define a generative process over variables z: An important insight behind flow-based models is that given this bijection function, the change of variable formula defines the model distribution on z 2 Z by: Here @f ✓ (z) @z is the Jacobian matrix of f ✓ at z. Eq. (6) provides a way to calculate the (complex) density of z by calculating the (simple) density of and the Jacobian of the transformation from z to . For efficiency purposes, flowbased models generally use certain types of transformations f ✓ where both the inverse functions g ✓ and the Jacobian determinants are tractable to compute. A stacked sequence of such invertible transformations is also called a (normalizing) flow (Rezende and Mohamed, 2015): where f = f 1 f 2 · · · f K is a flow of K transformations (omitting ✓s for brevity).

Variational Inference and Training
In the context of maximal likelihood estimation (MLE), we wish to minimize the negative loglikelihood of the parameters: where is the set of training data. However, the likelihood P ✓ (y|x) after marginalizing out latent variables z (LHS in Eq. (3)) is intractable to compute or differentiate directly. Variational inference (Wainwright et al., 2008) provides a solution by introducing a parametric inference model q (z|y, x) (a.k.a posterior) which is then used to approximate this integral by sampling individual examples of z. These models then optimize the evidence lower bound (ELBO), which considers both the "reconstruction error" log P ✓ (y|z, x) and KL-divergence between the posterior and the prior: KL(q (z|y, x)||p ✓ (z|x)). (8) Both inference model and decoder ✓ parameters are optimized according to this objective.

FlowSeq
We first overview FlowSeq's architecture (shown in Figure 2) and training process here before detailing each component in following sections. Similarly to classic seq2seq models, at both training and test time FlowSeq first reads the whole input sequence x and calculates a vector for each word in the sequence, the source encoding.
At training time, FlowSeq's parameters are learned using a variational training paradigm overviewed in §2.2. First, we draw samples of latent codes z from the current posterior q (z|y, x). Next, we feed z together with source encodings into the decoder network and the prior flow to compute the probabilities of P ✓ (y|z, x) and p ✓ (z|x) for optimizing the ELBO (Eq. (8)).
At test time, generation is performed by first sampling a latent code z from the prior flow by executing the generative process defined in Eq. (5). In this step, the source encodings produced from the encoder are used as conditional inputs. Then the decoder receives both the sampled latent code z and the source encoder outputs to generate the target sequence y from P ✓ (y|z, x).

Source Encoder
The source encoder encodes the source sequences into hidden representations, which are used in computing attention when generating latent variables in the posterior network and prior network as well as the cross-attention with decoder. Any standard neural sequence model can be used as its encoder, including RNNs (Bahdanau et al., 2015) or Transformers (Vaswani et al., 2017).

Posterior
Generation of Latent Variables. The latent variables z are represented as a sequence of continuous random vectors z = {z 1 , . . . , z T } with the same length as the target sequence y. Each z t is a d z -dimensional vector, where d z is the dimension of the latent space. The posterior distribution q (z|y, x) models each z t as a diagonal Gaussian with learned mean and variance: where µ t (·) and t (·) are neural networks such as RNNs or Transformers.
Zero initialization. While we perform standard random initialization for most layers of the network, we initialize the last linear transforms that generate the µ and log 2 values with zeros. This ensures that the posterior distribution as a simple normal distribution, which we found helps train very deep generative flows more stably.
Token Dropout. The motivation of introducing the latent variable z into the model is to model the uncertainty in the generative process. Thus, it is preferable that z capture contextual interdependence between tokens in y. However, there is an obvious local optimum where the posterior network generates a latent vector z t that only encodes the information about the corresponding target token y t , and the decoder simply generates the "correct" token at each step t with z t as input. In this case, FlowSeq reduces to the baseline model in Eq.
(2). To escape this undesired local optimum, we apply token-level dropout to randomly drop an entire token when calculating the posterior, to ensure the model also has to learn how to use contextual information. This technique is similar to the "masked language model" in previous studies (Melamud et al., 2016;Devlin et al., 2018;Ma et al., 2018).

Decoder
As the decoder, we take the latent sequence z as input, run it through several layers of a neural sequence model such as a Transformer, then directly predict the output tokens in y individually and independently. Notably, unlike standard seq2seq decoders, we do not perform causal masking to prevent attending to future tokens, making the model fully non-autoregressive.

Flow Architecture for Prior
The flow architecture is based on Glow (Kingma and Dhariwal, 2018). It consists of a series of steps of flow, combined in a multi-scale architecture (see Figure 2.) Each step of flow consists three types of elementary flows -actnorm, invertible multi-head linear, and coupling. Note that all three functions are invertible and conducive to calculation of log determinants (details in Appendix A).
Actnorm. The activation normalization layer (actnorm; Kingma and Dhariwal (2018)) is an alternative for batch normalization (Ioffe and Szegedy, 2015), that has mainly been used in the context of image data to alleviate problems in model training. Actnorm performs an affine transformation of the activations using a scale and bias parameter per feature for sequences: Both z and z 0 are tensors of shape [T ⇥ d z ] with time dimension t and feature dimension d z . The parameters are initialized such that over each feature z 0 t has zero mean and unit variance given an initial mini-batch of data.
Invertible Multi-head Linear Layers. To incorporate general permutations of variables along the feature dimension to ensure that each dimension can affect every other ones after a sufficient number of steps of flow, Kingma and Dhariwal (2018) proposed a trainable invertible 1⇥1 convolution layer for 2D images. It is straightforward to apply similar transformations to sequential data: where W is the weight matrix of shape The log-determinant of this transformation is:  Unfortunately, d z in Seq2Seq generation is commonly large, e.g. 512, significantly slowing down the model for computing det(W). To apply this to sequence generation, we propose a multihead invertible linear layer, which first splits each d z -dimensional feature vector into h heads with dimension d h = d z /h. Then the linear transformation in (11) is applied to each head, with d h ⇥ d h weight matrix W, significantly reducing the dimension. For splitting of heads, one step of flow contains one linear layer with either rowmajor or column-major splitting format, and these steps with different linear layers are composed in an alternating pattern.
Affine Coupling Layers. To model interdependence across time steps, we use affine coupling layers (Dinh et al., 2016): x) and b(z a , x) are outputs of two neural networks with z a and x as input. These are shown in Figure 3 (c). In experiments, we implement s(·) and b(·) with one Transformer decoder layer (Vaswani et al., 2017): multi-head selfattention over z a , followed by multi-head interattention over x, followed by a position-wise feedforward network. The input z a is fed into this layer in one pass, without causal masking.
As in Dinh et al. (2016), the split() function splits z the input tensor into two halves, while the concat operation performs the corresponding reverse concatenation operation. In our architecture, three types of split functions are used, based on the split dimension and pattern. Figure 3 (b) illustrates the three splitting types. The first type of split groups z along the time dimension on alternate indices. In this case, FlowSeq mainly models the interactions between time-steps. The second and third types of splits perform on the feature dimension, with continuous and alternate patterns, respectively. For each type of split, we alternate z a and z b to increase the flexibility of the split function. Different types of affine coupling layers alternate in the flow, similar to the linear layers.
Multi-scale Architecture. We follow Dinh et al. (2016) in implementing a multi-scale architecture using the squeezing operation on the feature dimension, which has been demonstrated helpful for training deep flows. Formally, each scale is a combination of several steps of the flow (see Figure 3 (a)). After each scale, the model drops half of the dimensions with the third type of split in Figure 3 (b) to reduce computational and memory cost, outputting the tensor with shape [T ⇥ d 2 ]. Then the squeezing operation transforms the T ⇥ d 2 tensor into an T 2 ⇥d one as the input of the next scale. We pad each sentence with EOS tokens to ensure T is divisible by 2. The right component of Figure 2 illustrates the multi-scale architecture.

Predicting Target Sequence Length
In autoregressive seq2seq models, it is natural to determine the length of the sequence dynamically by simply predicting a special EOS token. However, for FlowSeq to predict the entire sequence in parallel, it needs to know its length in advance to generate the latent sequence z. Instead of predicting the absolute length of the target sequence, we predict the length difference between source and target sequences using a classifier with a range of [ 20,20]. Numbers in this range are predicted by max-pooling the source encodings into a single vector, 2 running this through a linear layer, and taking a softmax. This classifier is learned jointly with the rest of the model.

Decoding Process
At inference time, the model needs to identify the sequence with the highest conditional probability by marginalizing over all possible latent variables (see Eq. (3)), which is intractable in practice. We propose three approximating decoding algorithms to reduce the search space.
Argmax Decoding. Following Gu et al. (2018), one simple and effective method is to select the best sequence by choosing the highest-probability latent sequence z: where identifying y ⇤ only requires independently maximizing the local probability for each output position (see Eq. 4).
Noisy Parallel Decoding (NPD). A more accurate approximation of decoding, proposed in Gu et al. (2018), is to draw samples from the latent space and compute the best output for each latent sequence. Then, a pre-trained autoregressive model is adopted to rank these sequences. In FlowSeq, different candidates can be generated by sampling different target lengths or different samples from the prior, and both of the strategies can be batched via masks during decoding. In our experiments, we first select the top l length candidates from the length predictor in §3.5. Then, for each length candidate we use r random samples from the prior network to generate output sequences, yielding a total of l ⇥ r candidates.

Importance Weighted Decoding (IWD)
The third approximating method is based on the lower bound of importance weighted estimation (Burda et al., 2015). Similarly to NPD, IWD first draws samples from the latent space and computes the best output for each latent sequence. Then, IWD 2 We experimented with other methods such as meanpooling or taking the last hidden state and found no major difference in our experiments ranks these candidate sequences with K importance samples: IWD does not rely on a separate pre-trained model, though it significantly slows down the decoding speed. The detailed comparison of these three decoding methods is provided in §4.2.

Discussion
Different from the architecture proposed in Ziegler and Rush (2019), the architecture of FlowSeq is not using any autoregressive flow (Kingma et al., 2016;Papamakarios et al., 2017), yielding a truly non-autoregressive model with efficient generation. Note that the FlowSeq remains nonautoregressive even if we use an RNN in the architecture because RNN is only used to encode a complete sequence of codes and all the input tokens can be fed into the RNN in parallel. This makes it possible to use highly-optimized implementations of RNNs such as those provided by cuDNN. 3 Thus while RNNs do experience some drop in speed, it is less extreme than that experienced when using autoregressive models.

Experimental Setups
Translation Datasets We evaluate FlowSeq on three machine translation benchmark datasets: WMT2014 DE-EN (around 4.5M sentence pairs), WMT2016 RO-EN (around 610K sentence pairs) and a smaller dataset IWSLT2014 DE-EN (around 150K sentence pairs). We use scripts from fairseq  to preprocess WMT2014 and IWSLT2014, where the preprocessing steps follow Vaswani et al. (2017) for WMT2014. We use the data provided in  for WMT2016. For both WMT datasets, the source and target languages share the same set of BPE embeddings while for IWSLT2014 we use separate embeddings. During training, we filter out sentences longer than 80 for WMT dataset and 60 for IWSLT, respectively.

Modules and Hyperparameters
We implement the encoder, decoder and posterior networks with standard (unmasked) Transformer layers (Vaswani et al., 2017). For WMT datasets, the encoder consists of 6 layers, and the decoder and posterior are composed of 4 layers, and 8 attention heads. and for IWSLT, the encoder has 5 layers, and decoder and Optimization Parameter optimization is performed with the Adam optimizer (Kingma and Ba, 2014) with = (0.9, 0.999) and ✏ = 1e 6. Each mini-batch consist of 2048 sentences. The learning rate is initialized to 5e 4, and exponentially decays with rate 0.999995. The gradient clipping cutoff is 1.0. For all the FlowSeq models, we apply 0.1 label smoothing and averaged the 5 best checkpoints to create the final model. At the beginning of training, the posterior network is randomly initialized, producing noisy supervision to the prior. To mitigate this issue, we first set the weight of the KL term in ELBO to zero for 30,000 updates to train the encoder, decoder and posterior networks. Then the KL weight linearly increases to one for another 10,000 up-  Table 2: BLEU scores on two WMT datasets of models using advanced decoding methods. The first block are Transformer-base (Vaswani et al., 2017). The second and the third block are results of models trained w/w.o. knowledge distillation, respectively. n = l ⇥ r is the total number of candidates for rescoring.
dates, which we found essential to accelerate training and achieve stable performance.
Knowledge Distillation Previous work on nonautoregressive generation (Gu et al., 2018;Ghazvininejad et al., 2019) has used translations produced by a pre-trained autoregressive NMT model as the training data, noting that this can significantly improve the performance. We analyze the impact of distillation in § 4.2.

Main Results
We first conduct experiments to compare the performance of FlowSeq with strong baseline models, including NAT w/ Fertility (Gu et al., 2018), NAT-IR , NAT-REG (Wang et al., 2019), LV NAR (Shu et al., 2019), CTC Loss (Libovickỳ and Helcl, 2018), and CMLM (Ghazvininejad et al., 2019). Table 1 provides the BLEU scores of FlowSeq with argmax decoding, together with baselines with purely non-autoregressive decoding methods that generate output sequence in one parallel pass. The first block lists results of models trained on raw data, while the second block are results using knowledge distillation. Without using knowledge distillation, FlowSeq base model achieves significant improvement (more than 9 BLEU points) over CMLM-base and LV NAR. It demonstrates the effectiveness of FlowSeq on modeling the complex interdependence in target languages. Towards the effect of knowledge distillation, we can mainly obtain two observations: i) Similar to the findings in previous work, knowledge distillation still benefits the translation quality of FlowSeq. ii) Compared to previous models, the benefit of knowledge distillation on FlowSeq is less significant, yielding less than 3 BLEU improvement on WMT2014 DE-EN corpus, and even no improvement on WMT2016 RO-EN corpus. The reason might be that FlowSeq does not rely much on knowledge distillation to alleviate the multi-modality problem. Table 2 illustrates the BLEU scores of FlowSeq and baselines with advanced decoding methods such as iterative refinement, IWD and NPD rescoring. The first block in Table 2 includes the baseline results from autoregressive Transformer. For the sampling procedure in IWD and NPD, we sampled from a reduced-temperature model (Kingma and Dhariwal, 2018) to obtain high-quality samples. We vary the temperature within {0.1, 0.2, 0.3, 0.4, 0.5, 1.0} and select the best temperature based on the performance on development sets. The analysis of the impact of sampling temperature and other hyper-parameters on samples is in § 4.4. For FlowSeq, NPD obtains better results than IWD, showing that FlowSeq still falls behind auto-regressive Transformer on model data distributions. Comparing with CMLM (Ghazvininejad et al., 2019) with 10 iterations of refinement, which is a contemporaneous work that achieves state-of-the-art translation performance, FlowSeq obtains competitive performance on both WMT2014 and WMT2016 corpora, with only slight degradation in translation quality. Leveraging iterative refinement to further improve the performance of FlowSeq has been left to future work.

Analysis on Decoding Speed
In this section, we compare the decoding speed (measured in average time in seconds required to decode one sentence) of FlowSeq at test time with that of the autoregressive Transformer model. We use the test set of WMT14 EN-DE for evaluation and all experiments are conducted on a single NVIDIA TITAN X GPU.
How does batch size affect the decoding speed? First, we investigate how different decoding batch size can affect the decoding speed. We vary the decoding batch size within {1, 4, 8, 32, 64, 128}. Figure. 4a shows that for both FlowSeq and Transformer decoding is faster when using a larger batch size. However, FlowSeq has much larger gains in the decoding speed w.r.t. the increase in batch size, gaining a speed up of 594% of base model and 403% of large model when using a batch size of 128. We hypothesize that this is because the operations in FlowSeq are more friendly to batching while the Transformer model with beam search at test time is less efficient in benefiting from batching.
How does sentence length affect the decoding speed? Next, we examine if sentence length is a major factor affecting the decoding speed. We bucket the test data by the target sentence length. From Fig. 4b, we can see that as the sentence length increases, FlowSeq achieves almost constant decoding time while Transformer has a linearly increasing decoding time. The relative decoding speed up of FlowSeq versus Transformer linearly increases as the sequence length increases. The potential of decoding long sequences with constant time is an attractive property of FlowSeq.

Analysis of Rescoring Candidates
In Fig. 5, we analyze how different sampling hyperparameters affect the performance of rescoring. First, we observe that the number of samples r for each length is the most important factor. The performance is always improved with a larger sample size. Second, a larger number of length candidates does not necessarily increase the rescoring performance. Third, we find that a larger sampling temperature (0.3 -0.5) can increase the diversity of translations and leads to better rescoring BLEU. However, the latent samples become noisy when a large temperature (1.0) is used.

Analysis of Translation Diversity
Following (Shen et al., 2019), we analyze the output diversity of FlowSeq. Shen et al. (2019) proposed pairwise-BLEU and BLEU computed in a leave-one-out manner to calibrate the diversity and quality of translation hypotheses. A lower pairwise-BLEU score implies a more diverse hypothesis set. And a higher BLEU score implies a better translation quality. We experiment on a subset of test set of WMT14-ENDE with ten references each sentence (Ott et al., 2018). In Fig. 6, we compare FlowSeq with other multi- hypothesis generation methods (ten hypotheses each sentence) to analyze how well the generation outputs of FlowSeq are in terms of diversity and quality. The right corner area of the figure indicates the ideal generations: high diversity and high quality. While FlowSeq still lags behind the autoregressive generations, by increasing the sampling temperature it provides a way of generating more diverse outputs while keeping the translation quality almost unchanged. More analysis of translation outputs and detailed results are provided in the Appendix D and E.

Conclusion
We propose FlowSeq, an efficient and effective model for non-autoregressive sequence generation by using generative flows. One potential direction for future work is to leverage iterative refinement techniques such as masked language models to further improve translation quality. Another exciting direction is to, theoretically and empirically, investigate the latent space in FlowSeq, hence providing deep insights of the model, even enhancing controllable text generation.