code: def attention_CrossAttention_forward(
      self, x, context=None, mask=None, **kwargs
  ):
      h = self.heads
  
      q = self.to_q(x)
      context = default(context, x)
  
      context_k, context_v = apply_hypernetworks(
          shared.loaded_hypernetworks, context, self
      )
      k = self.to_k(context_k)
      v = self.to_v(context_v)
  
      q, k, v = (
          rearrange(t, "b n (h d) -> (b h) n d", h=h) for t in (q, k, v)
      )
  
      sim = einsum("b i d, b j d -> b i j", q, k) * self.scale
  
      if mask is not None:
          mask = rearrange(mask, "b ... -> b (...)")
          max_neg_value = -torch.finfo(sim.dtype).max
          mask = repeat(mask, "b j -> (b h) () j", h=h)
          sim.masked_fill_(~mask, max_neg_value)
  
      # attention, what we cannot get enough of
      attn = sim.softmax(dim=-1)
  
      out = einsum("b i j, b j d -> b i d", attn, v)
      out = rearrange(out, "(b h) n d -> b n (h d)", h=h)
      return self.to_out(out)
  
imports: import datetime
  import glob
  import html
  import os
  import inspect
  from contextlib import closing
  import modules.textual_inversion.dataset
  import torch
  import tqdm
  from einops import rearrange, repeat
  from ldm.util import default
  from modules import devices, sd_models, shared, sd_samplers, hashes, sd_hijack_checkpoint, errors
  from modules.textual_inversion import textual_inversion, logging
  from modules.textual_inversion.learn_schedule import LearnRateScheduler
  from torch import einsum
  from torch.nn.init import normal_, xavier_normal_, xavier_uniform_, kaiming_normal_, kaiming_uniform_, zeros_
  from collections import deque
  from statistics import stdev, mean
label_desc: Use image node_id 7 for calling a global function with example usage: attention_CrossAttention_forward(x, context, mask) and returns: self