code: def __init__(
      self,
      dim,
      n_heads,
      d_head,
      dropout=0.0,
      context_dim=None,
      gated_ff=True,
      checkpoint=True,
      disable_self_attn=False,
      attn_mode="softmax",
      sdp_backend=None,
  ):
      super().__init__()
      assert attn_mode in self.ATTENTION_MODES
      if attn_mode != "softmax" and not XFORMERS_IS_AVAILABLE:
          logpy.warn(
              f"Attention mode '{attn_mode}' is not available. Falling "
              f"back to native attention. This is not a problem in "
              f"Pytorch >= 2.0. FYI, you are running with PyTorch "
              f"version {torch.__version__}."
          )
          attn_mode = "softmax"
      elif attn_mode == "softmax" and not SDP_IS_AVAILABLE:
          logpy.warn(
              "We do not support vanilla attention anymore, as it is too "
              "expensive. Sorry."
          )
          if not XFORMERS_IS_AVAILABLE:
              assert (
                  False
              ), "Please install xformers via e.g. 'pip install xformers==0.0.16'"
          else:
              logpy.info(
                  "Falling back to xformers efficient attention."
              )
              attn_mode = "softmax-xformers"
      attn_cls = self.ATTENTION_MODES[attn_mode]
      if version.parse(torch.__version__) >= version.parse("2.0.0"):
          assert sdp_backend is None or isinstance(
              sdp_backend, SDPBackend
          )
      else:
          assert sdp_backend is None
      self.disable_self_attn = disable_self_attn
      self.attn1 = attn_cls(
          query_dim=dim,
          heads=n_heads,
          dim_head=d_head,
          dropout=dropout,
          context_dim=context_dim if self.disable_self_attn else None,
          backend=sdp_backend,
      )  # is a self-attention if not self.disable_self_attn
      self.ff = FeedForward(dim, dropout=dropout, glu=gated_ff)
      self.attn2 = attn_cls(
          query_dim=dim,
          context_dim=context_dim,
          heads=n_heads,
          dim_head=d_head,
          dropout=dropout,
          backend=sdp_backend,
      )  # is self-attn if context is none
      self.norm1 = nn.LayerNorm(dim)
      self.norm2 = nn.LayerNorm(dim)
      self.norm3 = nn.LayerNorm(dim)
      self.checkpoint = checkpoint
      if self.checkpoint:
          logpy.debug(
              f"{self.__class__.__name__} is using checkpointing"
          )
  
imports: import logging
  import math
  from inspect import isfunction
  from typing import Any, Optional
  import torch
  import torch.nn.functional
  from einops import rearrange, repeat
  from packaging import version
  from torch import nn
  from torch.utils.checkpoint import checkpoint
label_desc: Use image node_id 1 to create a new BasicTransformerBlock object from inherited base classes: nn with example: obj = BasicTransformerBlock(dim, n_heads, d_head, dropout, context_dim, gated_ff, checkpoint, disable__attn, attn_mode, sdp_backend)