Transformer

Published
Vaswani et al. · Jun 2017
Line
Encoder–decoder
View
Architecture
Source

tensorflow/tensor2tensor v1.0.14 @ 0c66117 · arXiv:1706.03762v7 · WMT 2014 English–German · vocabulary of about 37,000 · no trained weights were publishedHow to read

Encoder: 6 layers

Open inside →×

learnedtransformer_encoder(encoder_input, residual_fn, encoder_self_attention_bias, hparams) · transformer.py 129–167

6 identical layers with independent weights over the source positions: self-attention, then a feed-forward network, each added to its input and normalized.

w=xn′+DPdrop(Δffn)w=x'_n+D_{\mathrm{P_{\mathrm{drop}}}}(\Delta_{\mathrm{ffn}})
xn+1=LayerNorm⁡(w)x_{n+1}=\operatorname{LayerNorm}(w)
All 23 steps
Multi-head self-attention:\text{Multi-head self-attention:}
pQKV=xWQKV+bQKVp^{QKV}=xW^{QKV}+b^{QKV}
q=p1:512QKV{\htmlClass{vl-stream-q}{q}}=p^{QKV}_{1:512}
k=p513:1024QKV{\htmlClass{vl-stream-k}{k}}=p^{QKV}_{513:1024}
v=p1025:1536QKV{\htmlClass{vl-stream-v}{v}}=p^{QKV}_{1025:1536}
Q=reshape⁡(q){\htmlClass{vl-stream-q}{Q}}=\operatorname{reshape}({\htmlClass{vl-stream-q}{q}})
Q~=Q/64\tilde {\htmlClass{vl-stream-q}{Q}}={\htmlClass{vl-stream-q}{Q}}/\sqrt{64}
K=reshape⁡(k){\htmlClass{vl-stream-k}{K}}=\operatorname{reshape}({\htmlClass{vl-stream-k}{k}})
V=reshape⁡(v){\htmlClass{vl-stream-v}{V}}=\operatorname{reshape}({\htmlClass{vl-stream-v}{v}})
S=Q~KTS=\tilde QK^{\mathsf T}
S~=S+M\tilde S=S+{\htmlClass{vl-stream-context}{M}}
A[b,i,t,s]=exp⁡(S~[b,i,t,s])∑u=0S−1exp⁡(S~[b,i,t,u])A[b,i,t,s]=\frac{\exp(\tilde S[b,i,t,s])}{\sum_{u=0}^{S-1}\exp(\tilde S[b,i,t,u])}
O=AVO=AV
o=reshape⁡(O)o=\operatorname{reshape}(O)
Δ=oWO+bO\Delta=oW^{O}+b^{O}
u=xn+DPdrop(Δattn)u=x_n+D_{\mathrm{P_{\mathrm{drop}}}}(\Delta_{\mathrm{attn}})
xn′=LayerNorm⁡(u)x'_n=\operatorname{LayerNorm}(u)
Position-wise feed-forward network:\text{Position-wise feed-forward network:}
h=xW1+b1h=xW_1+b_1
a=max⁡(0, h)a=\max(0,\,h)
Δ=aW2+b2\Delta=aW_2+b_2
w=xn′+DPdrop(Δffn)w=x'_n+D_{\mathrm{P_{\mathrm{drop}}}}(\Delta_{\mathrm{ffn}})
xn+1=LayerNorm⁡(w)x_{n+1}=\operatorname{LayerNorm}(w)
in
DPdrop(x0)D_{\mathrm{P_{\mathrm{drop}}}}(x_0) [B batch, S source positions, 512 d]MM [B batch, 1, 1, S keys]
out
xn+1x_{n+1} [B batch, S source positions, 512 d]
parameters
3,152,384 · × 6 layers = 18,914,304
Parameters

3,152,384 parameters per layer, across its learned components

Exact per-instance accounting
ComponentCalculationParameters
Multi-head self-attention1,050,624 across its components1,050,624
LayerNorm512+512512 + 5121,024
Position-wise feed-forward network2,099,712 across its components2,099,712
LayerNorm512+512512 + 5121,024
Per-instance total3,152,384

Across 6 independent layers: 3,152,384×6=18,914,3043{,}152{,}384 \times 6 = 18{,}914{,}304 parameters.

Derived from the modules the pinned tensor2tensor v1.0.14 code creates, with a vocabulary of about 37,000 (the paper’s words); no trained weights were published to count.

Explanation

The output of each sublayer is LayerNorm(x+Sublayer(x))(x+\text{Sublayer}(x)); all sublayers and embeddings produce outputs of dmodel=512d_{\text{model}}=512 (§3.1).

Codetransformer.py 59–61, 129–167
transformer.py 129–167
def transformer_encoder(encoder_input,
                        residual_fn,
                        encoder_self_attention_bias,
                        hparams,
                        name="encoder"):
  """A stack of transformer layers.

  Args:
    encoder_input: a Tensor
    residual_fn: a function from (layer_input, layer_output) -> combined_output
    encoder_self_attention_bias: bias Tensor for self-attention
       (see common_attention.attention_bias())
    hparams: hyperparameters for model
    name: a string

  Returns:
    y: a Tensors
  """
  x = encoder_input
  # Summaries don't work in multi-problem setting yet.
  summaries = "problems" not in hparams.values() or len(hparams.problems) == 1
  with tf.variable_scope(name):
    for layer in xrange(hparams.num_hidden_layers):
      with tf.variable_scope("layer_%d" % layer):
        x = residual_fn(
            x,
            common_attention.multihead_attention(
                x,
                None,
                encoder_self_attention_bias,
                hparams.attention_key_channels or hparams.hidden_size,
                hparams.attention_value_channels or hparams.hidden_size,
                hparams.hidden_size,
                hparams.num_heads,
                hparams.attention_dropout,
                summaries=summaries,
                name="encoder_self_attention"))
        x = residual_fn(x, transformer_ffn_layer(x, hparams))
  return x
transformer.py 59–61
    def residual_fn(x, y):
      return common_layers.layer_norm(x + tf.nn.dropout(
          y, 1.0 - hparams.residual_dropout))
Sources2

Direct evidence

Each of num_hidden_layers encoder layers: self-attention over the encoder input with the padding bias, then the feed-forward network, each through residual_fn.

encoder · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 129–167

Read code excerpt
def transformer_encoder(encoder_input,
                        residual_fn,
                        encoder_self_attention_bias,
                        hparams,
                        name="encoder"):
  """A stack of transformer layers.

  Args:
    encoder_input: a Tensor
    residual_fn: a function from (layer_input, layer_output) -> combined_output
    encoder_self_attention_bias: bias Tensor for self-attention
       (see common_attention.attention_bias())
    hparams: hyperparameters for model
    name: a string

  Returns:
    y: a Tensors
  """
  x = encoder_input
  # Summaries don't work in multi-problem setting yet.
  summaries = "problems" not in hparams.values() or len(hparams.problems) == 1
  with tf.variable_scope(name):
    for layer in xrange(hparams.num_hidden_layers):
      with tf.variable_scope("layer_%d" % layer):
        x = residual_fn(
            x,
            common_attention.multihead_attention(
                x,
                None,
                encoder_self_attention_bias,
                hparams.attention_key_channels or hparams.hidden_size,
                hparams.attention_value_channels or hparams.hidden_size,
                hparams.hidden_size,
                hparams.num_heads,
                hparams.attention_dropout,
                summaries=summaries,
                name="encoder_self_attention"))
        x = residual_fn(x, transformer_ffn_layer(x, hparams))
  return x

Direct evidence

Post-norm: each sublayer output is dropped out, added to its input and layer-normalized.

residual-fn · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 59–61

Read code excerpt
    def residual_fn(x, y):
      return common_layers.layer_norm(x + tf.nn.dropout(
          y, 1.0 - hparams.residual_dropout))