class_docstr: Computes attention between hidden and encoder states.
  
  See arxiv.org/abs/1508.04025 for more info on each attention type.
docstr: Initialize attention layer.
docstr_tok: ["Initialize","attention","layer","."]
code: def __init__(
      self,
      attn_type,
      hiddensize,
      embeddingsize,
      bidirectional=False,
      attn_length=-1,
      attn_time="pre",
  ):
      """
      Initialize attention layer.
      """
      super().__init__()
      self.attention = attn_type
  
      if self.attention != "none":
          hsz = hiddensize
          hszXdirs = hsz * (2 if bidirectional else 1)
          if attn_time == "pre":
              # attention happens on the input embeddings
              input_dim = embeddingsize
          elif attn_time == "post":
              # attention happens on the output of the rnn
              input_dim = hsz
          else:
              raise RuntimeError("unsupported attention time")
  
          # linear layer for combining applied attention weights with input
          self.attn_combine = nn.Linear(
              hszXdirs + input_dim, input_dim, bias=False
          )
  
          if self.attention == "local":
              # local attention over fixed set of output states
              if attn_length < 0:
                  raise RuntimeError("Set attention length to > 0.")
              self.max_length = attn_length
              # combines input and previous hidden output layer
              self.attn = nn.Linear(
                  hsz + input_dim, attn_length, bias=False
              )
              # combines attention weights with encoder outputs
          elif self.attention == "concat":
              self.attn = nn.Linear(hsz + hszXdirs, hsz, bias=False)
              self.attn_v = nn.Linear(hsz, 1, bias=False)
          elif self.attention == "general":
              # equivalent to dot if attn is identity
              self.attn = nn.Linear(hsz, hszXdirs, bias=False)
  
imports: import math
  import torch
  import torch.nn
  from torch.nn.parameter import Parameter
  from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
  import torch.nn.functional
  from parlai.utils.torch import neginf
  from parlai.core.torch_generator_agent import TorchGeneratorModel
label_desc: Use image node_id 1 to create a new AttentionLayer object from inherited base classes: nn with example: obj = AttentionLayer(attn_type, hiddensize, embeddingsize, bidirectional, attn_length, attn_time)