class_docstr: Transformer decoder that applies multi-head self-attention for
  sequence decoding.
  
  It is a stack of :class:`~texar.tf.modules.encoders.MultiheadAttentionEncoder`,
  :class:`~texar.tf.modules.FeedForwardNetwork` and residual connections.
  
  Args:
      vocab_size (int, optional): Vocabulary size. Required if
          :attr:`output_layer` is `None`.
      output_layer (optional): An output layer that transforms cell output
          to logits. This can be:
  
          - A callable layer, e.g., an instance             of :tf_main:`tf.layers.Layer <layers/Layer>`.
          - A tensor. A dense layer will be created using the tensor             as the kernel weights. The bias of the dense layer is determined by            `hparams.output_layer_bias`. This can be used to tie the output             layer with the input embedding matrix, as proposed in             https://arxiv.org/pdf/1608.05859.pdf
          - `None`. A dense layer will be created based on attr:`vocab_size`            and `hparams.output_layer_bias`.
          - If no output layer in the end is needed, set             `(vocab_size=None, output_layer=tf.identity)`.
  
  .. document private functions
  .. automethod:: _build
class_docstr_tok: ["Transformer","decoder","that","applies","multi-head","self-attention","for","sequence","decoding",".","It","is","a","stack","of",":","class",":","`","~texar.tf.modules.encoders.MultiheadAttentionEncoder","`",",",":","class",":","`","~texar.tf.modules.FeedForwardNetwork","`","and","residual","connections",".","Args",":","vocab_size","(","int",",","optional",")",":","Vocabulary","size",".","Required","if",":","attr",":","`","output_layer","`","is","`","None","`",".","output_layer","(","optional",")",":","An","output","layer","that","transforms","cell","output","to","logits",".","This","can","be",":","-","A","callable","layer",",","e.g.",",","an","instance","of",":","tf_main",":","`","tf.layers.Layer","<","layers\/Layer",">","`",".","-","A","tensor",".","A","dense","layer","will","be","created","using","the","tensor","as","the","kernel","weights",".","The","bias","of","the","dense","layer","is","determined","by","`","hparams.output_layer_bias","`",".","This","can","be","used","to","tie","the","output","layer","with","the","input","embedding","matrix",",","as","proposed","in","https",":","\/\/arxiv.org\/pdf\/1608.05859.pdf","-","`","None","`",".","A","dense","layer","will","be","created","based","on","attr",":","`","vocab_size","`","and","`","hparams.output_layer_bias","`",".","-","If","no","output","layer","in","the","end","is","needed",",","set","`","(","vocab_size=None",",","output_layer=tf.identity",")","`",".","..","document","private","functions","..","automethod",":",":","_build"]
docstr: Stacked multihead attention module.
          
docstr_tok: ["Stacked","multihead","attention","module","."]
code: def _self_attention_stack(
      self,
      inputs,
      memory,
      decoder_self_attention_bias=None,
      memory_attention_bias=None,
      cache=None,
      mode=None,
  ):
      """Stacked multihead attention module."""
  
      def _layer_norm(x, scope):
          return layers.layer_normalize(
              x, reuse=tf.AUTO_REUSE, scope=scope
          )
  
      inputs = tf.layers.dropout(
          inputs,
          rate=self._hparams.embedding_dropout,
          training=is_train_mode(mode),
      )
      if cache is not None:
          if memory is not None:
              memory_attention_bias = cache["memory_attention_bias"]
      else:
          assert decoder_self_attention_bias is not None
  
      x = inputs
      for i in range(self._hparams.num_blocks):
          layer_name = "layer_{}".format(i)
          layer_cache = cache[layer_name] if cache is not None else None
          with tf.variable_scope(layer_name) as layer_scope:
              with tf.variable_scope("self_attention"):
                  multihead_attention = self.multihead_attentions[
                      "self_att"
                  ][i]
                  selfatt_output = multihead_attention(
                      queries=_layer_norm(x, layer_scope),
                      memory=None,
                      memory_attention_bias=decoder_self_attention_bias,
                      cache=layer_cache,
                      mode=mode,
                  )
                  x = x + tf.layers.dropout(
                      selfatt_output,
                      rate=self._hparams.residual_dropout,
                      training=is_train_mode(mode),
                  )
              if memory is not None:
                  with tf.variable_scope(
                      "encdec_attention"
                  ) as encdec_attention_scope:
                      multihead_attention = self.multihead_attentions[
                          "encdec_att"
                      ][i]
                      encdec_output = multihead_attention(
                          queries=_layer_norm(
                              x, encdec_attention_scope
                          ),
                          memory=memory,
                          memory_attention_bias=memory_attention_bias,
                          mode=mode,
                      )
                      x = x + tf.layers.dropout(
                          encdec_output,
                          rate=self._hparams.residual_dropout,
                          training=is_train_mode(mode),
                      )
              poswise_network = self.poswise_networks[i]
              with tf.variable_scope(
                  "past_poswise_ln"
              ) as past_poswise_ln_scope:
                  sub_output = tf.layers.dropout(
                      poswise_network(
                          _layer_norm(x, past_poswise_ln_scope)
                      ),
                      rate=self._hparams.residual_dropout,
                      training=is_train_mode(mode),
                  )
                  x = x + sub_output
  
      return _layer_norm(x, scope=self.variable_scope)
  
imports: import collections
  import tensorflow
  from tensorflow.contrib.seq2seq import Decoder
  from texar.tf.core import layers
  from texar.tf.module_base import ModuleBase
  from texar.tf.modules.networks.networks import FeedForwardNetwork
  from texar.tf.modules.encoders.transformer_encoders import default_transformer_poswise_net_hparams
  from texar.tf.modules.encoders.multihead_attention import MultiheadAttentionEncoder
  from texar.tf.modules.decoders.rnn_decoder_base import _make_output_layer
  from texar.tf.modules.decoders import tf_helpers
  from texar.tf.utils import beam_search, transformer_attentions
  from texar.tf.utils.shapes import shape_list
  from texar.tf.utils.mode import is_train_mode
  from texar.tf.modules.decoders.dynamic_decode import dynamic_decode
label_desc: Use image node_id 6 for calling the TransformerDecoder obj's underlying member method code with example usage: obj._self_attention_stack(inputs, memory, decoder__attention_bias, memory_attention_bias, cache, mode) and returns: _layer_norm, layers