code: def transformer_interaction(
      h: torch.FloatTensor,
      r: torch.FloatTensor,
      t: torch.FloatTensor,
      transformer: nn.TransformerEncoder,
      position_embeddings: torch.FloatTensor,
      final: nn.Module,
  ) -> torch.FloatTensor:
      r"""Evaluate the Transformer interaction function, as described in [galkin2020]_..
  
      .. math ::
  
          \textit{score}(h, r, t) =
              \textit{Linear}(\textit{SumPooling}(\textit{Transformer}([h + pe[0]; r + pe[1]])))^T t
  
      :param h: shape: (`*batch_dims`, dim)
          The head representations.
      :param r: shape: (`*batch_dims`, dim)
          The relation representations.
      :param t: shape: (`*batch_dims`, dim)
          The tail representations.
      :param transformer:
          the transformer encoder
      :param position_embeddings: shape: (2, dim)
          the positional embeddings, one for head and one for relation
      :param final:
          the final (linear) transformation
  
      :return:
          The scores.
      """
      # stack h & r (+ broadcast) => shape: (2, *batch_dims, dim)
      x = torch.stack(broadcast_tensors(h, r), dim=0)
  
      # remember shape for output, but reshape for transformer
      hr_shape = x.shape
      x = x.view(2, -1, hr_shape[-1])
  
      # get position embeddings, shape: (seq_len, dim)
      # Now we are position-dependent w.r.t qualifier pairs.
      x = x + position_embeddings.unsqueeze(dim=1)
  
      # seq_length, batch_size, dim
      x = transformer(src=x)
  
      # Pool output
      x = x.sum(dim=0)
  
      # output shape: (batch_size, dim)
      x = final(x)
  
      # reshape
      x = x.view(*hr_shape[1:-1], x.shape[-1])
  
      return (x * t).sum(dim=-1)
  
imports: from __future__ import annotations
  import functools
  from typing import Optional, Tuple, Union
  import numpy
  import torch
  from torch import broadcast_tensors, nn
  from .compute_kernel import batched_dot
  from .sim import KG2E_SIMILARITIES
  from ..typing import GaussianDistribution
  from ..utils import clamp_norm, einsum, is_cudnn_error, make_ones_like, negative_norm, negative_norm_of_sum, project_entity, tensor_product, tensor_sum
label_desc: Use image node_id 31 for calling a global function with example usage: transformer_interaction(h, r, t, transformer, position_embeddings, final) and returns: unknown