code: def transformer_prepare_encoder(
      inputs,
      target_space,
      hparams,
      features=None,
      type_ids=None,
      num_types=None,
      reuse_target_embedding=tf.AUTO_REUSE,
  ):
      """Prepare one shard of the model for the encoder.
  
      Args:
        inputs: a Tensor.
        target_space: a Tensor.
        hparams: run hyperparameters
        features: optionally pass the entire features dictionary as well.
          This is needed now for "packed" datasets.
        type_ids: optional, an int64 Tensor of shape [batch, length] that allows
          for adding type embeddings, similar to positional embeddings.
        num_types: optional, an int that decides the number of types in type_ids.
        reuse_target_embedding: option to reuse variable name in the case that
          symbol modalities are reused between inputs/targets.
  
      Returns:
        encoder_input: a Tensor, bottom of encoder stack
        encoder_self_attention_bias: a bias tensor for use in encoder self-attention
        encoder_decoder_attention_bias: a bias tensor for use in encoder-decoder
          attention
      """
      ishape_static = inputs.shape.as_list()
      encoder_input = inputs
      if features and "inputs_segmentation" in features:
          # Packed dataset.  Keep the examples from seeing each other.
          inputs_segmentation = features["inputs_segmentation"]
          inputs_position = features["inputs_position"]
          targets_segmentation = features["targets_segmentation"]
          if (
              hasattr(hparams, "unidirectional_encoder")
              and hparams.unidirectional_encoder
          ):
              tf.logging.info("Using unidirectional encoder")
              encoder_self_attention_bias = (
                  common_attention.attention_bias_lower_triangle(
                      common_layers.shape_list(inputs)[1]
                  )
              )
          else:
              encoder_self_attention_bias = (
                  common_attention.attention_bias_same_segment(
                      inputs_segmentation, inputs_segmentation
                  )
              )
          encoder_decoder_attention_bias = (
              common_attention.attention_bias_same_segment(
                  targets_segmentation, inputs_segmentation
              )
          )
      else:
          encoder_padding = common_attention.embedding_to_padding(
              encoder_input
          )
          ignore_padding = (
              common_attention.attention_bias_ignore_padding(
                  encoder_padding
              )
          )
          if (
              hasattr(hparams, "unidirectional_encoder")
              and hparams.unidirectional_encoder
          ):
              tf.logging.info("Using unidirectional encoder")
              encoder_self_attention_bias = (
                  common_attention.attention_bias_lower_triangle(
                      common_layers.shape_list(inputs)[1]
                  )
              )
          else:
              # Usual case - not a packed dataset.
              encoder_self_attention_bias = ignore_padding
          encoder_decoder_attention_bias = ignore_padding
          inputs_position = None
      if hparams.proximity_bias:
          encoder_self_attention_bias += (
              common_attention.attention_bias_proximal(
                  common_layers.shape_list(inputs)[1]
              )
          )
      if target_space is not None and hparams.get(
          "use_target_space_embedding", True
      ):
          # Append target_space_id embedding to inputs.
          emb_target_space = common_layers.embedding(
              target_space,
              32,
              ishape_static[-1],
              name="target_space_embedding",
              dtype=hparams.get("activation_dtype", "float32"),
              reuse=reuse_target_embedding,
          )
          emb_target_space = tf.reshape(emb_target_space, [1, 1, -1])
          encoder_input += emb_target_space
      if hparams.pos == "timing":
          if inputs_position is not None:
              encoder_input = (
                  common_attention.add_timing_signal_1d_given_position(
                      encoder_input, inputs_position
                  )
              )
          else:
              encoder_input = common_attention.add_timing_signal_1d(
                  encoder_input
              )
      elif hparams.pos == "timing_from_features":
          encoder_input = (
              common_attention.add_timing_signals_from_features(
                  encoder_input, features, hparams.position_features
              )
          )
      elif hparams.pos == "emb":
          encoder_input = common_attention.add_positional_embedding(
              encoder_input,
              hparams.max_length,
              "inputs_positional_embedding",
              inputs_position,
          )
  
      # Add type embeddings
      if type_ids is not None:
          if not num_types:
              raise ValueError("Need to set num_types as well.")
          encoder_input = common_attention.add_positional_embedding(
              encoder_input,
              num_types,
              "inputs_type_embedding",
              type_ids,
          )
  
      encoder_self_attention_bias = common_layers.cast_like(
          encoder_self_attention_bias, encoder_input
      )
      encoder_decoder_attention_bias = common_layers.cast_like(
          encoder_decoder_attention_bias, encoder_input
      )
      return (
          encoder_input,
          encoder_self_attention_bias,
          encoder_decoder_attention_bias,
      )
  
file_path: tensor2tensor/tensor2tensor/layers/transformer_layers.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from tensor2tensor.layers import common_attention
  from tensor2tensor.layers import common_layers
  from tensor2tensor.utils import expert_utils
  from tensor2tensor.utils import mlperf_log
  import tensorflow.compat.v1
  from tensorflow.compat.v1 import estimator
label_desc: Use image node_id 2 for calling a global function with example usage: transformer_prepare_encoder(inputs, target_space, hparams, features, type_ids, num_types, reuse_target_embedding) and returns: encoder_input,  encoder_self_attention_bias,  encoder_decoder_attention_bias