code: def __init__(self, config, is_cross_attention=False):
      super().__init__()
  
      max_positions = config.max_position_embeddings
      self.register_buffer(
          "bias",
          torch.tril(
              torch.ones(
                  (max_positions, max_positions), dtype=torch.uint8
              )
          ).view(1, 1, max_positions, max_positions),
      )
      self.register_buffer("masked_bias", torch.tensor(-1e4))
  
      self.embed_dim = config.hidden_size
      self.num_heads = config.num_attention_heads
      self.head_dim = self.embed_dim // self.num_heads
      self.split_size = self.embed_dim
      if self.head_dim * self.num_heads != self.embed_dim:
          raise ValueError(
              f"`embed_dim` must be divisible by num_heads (got `embed_dim`: {self.embed_dim} and `num_heads`: {self.num_heads})."
          )
  
      self.scale_attn_weights = config.scale_attn_weights
      self.is_cross_attention = is_cross_attention
  
      if self.is_cross_attention:
          self.c_attn = Conv1D(2 * self.embed_dim, self.embed_dim)
          self.q_attn = Conv1D(self.embed_dim, self.embed_dim)
      else:
          self.c_attn = Conv1D(3 * self.embed_dim, self.embed_dim)
      self.c_proj = Conv1D(self.embed_dim, self.embed_dim)
  
      self.attn_dropout = nn.Dropout(config.attn_pdrop)
      self.resid_dropout = nn.Dropout(config.resid_pdrop)
  
      self.pruned_heads = set()
  
imports: from dataclasses import dataclass
  from typing import Optional, Tuple
  from .configuration_gpt2 import GPT2Config
  from fastNLP.transformers.torch.activations import ACT2FN
  from fastNLP.transformers.torch.file_utils import ModelOutput, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
  from fastNLP.transformers.torch.modeling_outputs import BaseModelOutputWithPastAndCrossAttentions, CausalLMOutputWithCrossAttentions, SequenceClassifierOutputWithPast, TokenClassifierOutput
  from fastNLP.transformers.torch.modeling_utils import Conv1D, PreTrainedModel, SequenceSummary, find_pruneable_heads_and_indices, prune_conv1d_layer
  from fastNLP.transformers.torch.utils.model_parallel_utils import assert_device_map, get_device_map
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.core.log import logger
label_desc: Use image node_id 1 to create a new GPT2Attention object from inherited base classes: Module with example: obj = GPT2Attention(config, is_cross_attention)