code: def text_encoder_attn_modules(text_encoder):
      attn_modules = []
  
      if isinstance(
          text_encoder, (CLIPTextModel, CLIPTextModelWithProjection)
      ):
          for i, layer in enumerate(
              text_encoder.text_model.encoder.layers
          ):
              name = f"text_model.encoder.layers.{i}.self_attn"
              mod = layer.self_attn
              attn_modules.append((name, mod))
      else:
          raise ValueError(
              f"do not know how to get attention modules for: {text_encoder.__class__.__name__}"
          )
  
      return attn_modules
  
imports: from typing import Optional, Tuple, Union
  import torch
  import torch.nn.functional
  from torch import nn
  from ..utils import logging
  from ..utils.import_utils import is_transformers_available
label_desc: Use image node_id 1 for calling a global function with example usage: text_encoder_attn_modules(text_encoder) and returns: attn_modules