Encoder: 6 layers

xnx_n [B, S, 512], MM [B, 1, 1, S]to xn+1x_{n+1} [B, S, 512]

Parameters3,152,384 × 6 layers

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

ComponentCalculationParameters
Multi-head self-attention1,050,624
LayerNorm512 + 5121,024
Position-wise feed-forward network2,099,712
LayerNorm512 + 5121,024
Per-instance total3,152,384

One of 6 independent layers.

No trained weights were published to check against.

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).

Sources

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

Open pinned code ↗

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.

transformer.py 129–167 ↗ · tensorflow/tensor2tensor @ 0c66117d4297

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

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

transformer.py 59–61 ↗ · tensorflow/tensor2tensor @ 0c66117d4297

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