code: def attend(x, source, hparams, name):
      """Self-attention layer with source as memory antecedent."""
      with tf.variable_scope(name):
          x = tf.squeeze(x, axis=2)
          if len(source.get_shape()) > 3:
              source = tf.squeeze(source, axis=2)
          source = common_attention.add_timing_signal_1d(source)
          y = common_attention.multihead_attention(
              common_layers.layer_preprocess(x, hparams),
              source,
              None,
              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,
          )
          res = common_layers.layer_postprocess(x, y, hparams)
          return tf.expand_dims(res, axis=2)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  import math
  import os
  from six.moves import range
  from tensor2tensor.layers import common_attention
  from tensor2tensor.layers import common_image_attention
  from tensor2tensor.layers import common_layers
  from tensor2tensor.layers import discretization
  from tensor2tensor.layers import latent_layers
  from tensor2tensor.layers import modalities
  from tensor2tensor.models import transformer
  from tensor2tensor.utils import beam_search
  from tensor2tensor.utils import contrib
  from tensor2tensor.utils import expert_utils
  from tensor2tensor.utils import registry
  from tensor2tensor.utils import t2t_model
  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: attend(x, source, hparams, name) and returns: tf