code: def embedding_postprocessor(
      input_tensor,
      input_context=None,
      use_token_type=False,
      token_type_ids=None,
      position_ids=None,
      token_type_vocab_size=16,
      token_type_embedding_name="token_type_embeddings",
      use_position_embeddings=True,
      position_embedding_name="position_embeddings",
      initializer_range=0.02,
      max_position_embeddings=512,
      dropout_prob=0.1,
      roberta=False,
      pos_injection=False,
      positional_channels=None,
      reading_order_decay_rate=None,
      anneal_reading_order=False,
      pos2d_embedding_fn=docrep_pos_embed,
  ):
      """Performs various post-processing on a word embedding tensor.
  
      Args:
          input_tensor: float Tensor of shape [batch_size, seq_length,
          embedding_size].
          use_token_type: bool. Whether to add embeddings for `token_type_ids`.
          token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length].
              Must be specified if `use_token_type` is True.
          token_type_vocab_size: int. The vocabulary size of `token_type_ids`.
          token_type_embedding_name: string. The name of the embedding table variable
              for token type ids.
          use_position_embeddings: bool. Whether to add position embeddings for the
              position of each token in the sequence.
          position_embedding_name: string. The name of the embedding table variable
              for positional embeddings.
          initializer_range: float. Range of the weight initialization.
          max_position_embeddings: int. Maximum sequence length that might ever be
              used with this model. This can be longer than the sequence length of
              input_tensor, but cannot be shorter.
          dropout_prob: float. Dropout probability applied to the final output tensor.
          roberta: Whether the model is the roberta variant.
      Returns:
          float tensor with same shape as `input_tensor`.
  
      Raises:
          ValueError: One of the tensor shapes or input values is invalid.
      """
      input_shape = get_shape_list(input_tensor, expected_rank=3)
      batch_size = input_shape[0]
      seq_length = input_shape[1]
      width = input_shape[2]
      output = input_tensor
  
      if use_token_type:
          if token_type_ids is None:
              raise ValueError(
                  "`token_type_ids` must be specified if"
                  "`use_token_type` is True."
              )
          token_type_table = tf.compat.v1.get_variable(
              name=token_type_embedding_name,
              shape=[token_type_vocab_size, width],
              initializer=create_initializer(initializer_range),
          )
          # This vocab will be small so we always do one-hot here, since it is always
          # faster for a small vocabulary.
          token_type_table = tf.convert_to_tensor(
              value=token_type_table
          )
          flat_token_type_ids = tf.reshape(token_type_ids, [-1])
          one_hot_ids = tf.one_hot(
              flat_token_type_ids,
              depth=token_type_vocab_size,
              dtype=token_type_table.dtype,
          )
          token_type_embeddings = tf.matmul(
              one_hot_ids, token_type_table
          )
          token_type_embeddings = tf.reshape(
              token_type_embeddings, [batch_size, seq_length, width]
          )
          output += token_type_embeddings
  
      if use_position_embeddings:
          assert_op = tf.compat.v1.assert_less_equal(
              seq_length, max_position_embeddings
          )
          with tf.control_dependencies([assert_op]):
              full_position_embeddings = tf.compat.v1.get_variable(
                  name=position_embedding_name,
                  shape=[max_position_embeddings, width],
                  initializer=create_initializer(initializer_range),
              )
  
              if anneal_reading_order:
                  full_position_embeddings *= tf.cast(
                      tf.random.uniform(
                          shape=tf.shape(input=full_position_embeddings)
                      )
                      > reading_order_decay_rate,
                      input_tensor.dtype,
                  )
  
              # Since the position embedding table is a learned variable, we create it
              # using a (long) sequence length `max_position_embeddings`. The actual
              # sequence length might be shorter than this, for faster training of
              # tasks that do not have long sequences.
              #
              # So `full_position_embeddings` is effectively an embedding table
              # for position [0, 1, 2, ..., max_position_embeddings-1], and the current
              # sequence has positions [0, 1, 2, ... seq_length-1], so we can just
              # perform a slice.
              if roberta:
                  position_embeddings = tf.slice(
                      full_position_embeddings, [2, 0], [seq_length, -1]
                  )
              else:
                  position_embeddings = tf.slice(
                      full_position_embeddings, [0, 0], [seq_length, -1]
                  )
  
              if position_ids is None:
                  num_dims = len(output.shape.as_list())
                  # Only the last two dimensions are relevant (`seq_length` and `width`), so
                  # we broadcast among the first dimensions, which is typically just
                  # the batch size.
                  position_broadcast_shape = []
                  for _ in range(num_dims - 2):
                      position_broadcast_shape.append(1)
                  position_broadcast_shape.extend([seq_length, width])
                  position_embeddings = tf.reshape(
                      position_embeddings, position_broadcast_shape
                  )
              else:
                  position_embeddings = tf.gather(
                      position_embeddings, position_ids
                  )
              output += position_embeddings
  
      if pos_injection and input_context is not None:
          output += pos2d_embedding_fn(
              input_context=input_context,
              positional_channels=positional_channels,
              batch_size=batch_size,
              seq_length=seq_length,
              width=width,
          )
  
      output = layer_norm_and_dropout(output, dropout_prob)
      return output
  
imports: import copy
  import json
  import math
  import numpy
  import tensorflow
  import functools
  from transformers.activations_tf import gelu
  from finetune.optimizers.recompute_grads import recompute_grad
  from finetune.nn.auxiliary import embed_position
label_desc: Use image node_id 11 for calling a global function with example usage: embedding_postprocessor(input_tensor, input_context, use_token_type, token_type_ids, position_ids, token_type_vocab_size, token_type_embedding_name, use_position_embeddings, position_embedding_name, initializer_range, max_position_embeddings, dropout_prob, roberta, pos_injection, positional_channels, reading_order_decay_rate, anneal_reading_order, pos2d_embedding_fn) and returns: output