code: def check_target_module_exists(
      config, key: str
  ) -> bool | re.Match[str] | None:
      """A helper method to check if the passed module's key name matches any of the target modules in the adapter_config.
  
      Args:
          config (`LoraConfig` | `LycorisConfig`): A config to match target modules from
          key (`str`): A key to search any matches in config
  
      Returns:
          `bool` | `re.Match[str]` | `None`: True of match object if key matches any target modules from config, False or
          None if no match found
      """
      if isinstance(config.target_modules, str):
          target_module_found = re.fullmatch(config.target_modules, key)
      elif key in config.target_modules:
          # this module is specified directly in target_modules
          target_module_found = True
      else:
          target_module_found = any(
              key.endswith(f".{target_key}")
              for target_key in config.target_modules
          )
  
          layer_indexes = getattr(config, "layers_to_transform", None)
          layers_pattern = getattr(config, "layers_pattern", None)
  
          is_using_layer_indexes = layer_indexes is not None and (
              len(layer_indexes) != 0
              if isinstance(layer_indexes, list)
              else True
          )
          if is_using_layer_indexes and target_module_found:
              layer_index = None
              # TODO: It's still unclear how empty layers_pattern (None, [], or "") should behave
              # For now, empty layers_pattern means any layer pattern is ok
              if layers_pattern is None or len(layers_pattern) == 0:
                  layer_index = re.match(r".*\.[^.]*\.(\d+)\.", key)
              else:
                  layers_pattern = (
                      [layers_pattern]
                      if isinstance(layers_pattern, str)
                      else layers_pattern
                  )
                  for pattern in layers_pattern:
                      layer_index = re.match(
                          r".*\.{layer}\.(\d+)\.".format(layer=pattern),
                          key,
                      )
                      if layer_index is not None:
                          break
  
              if layer_index is None:
                  target_module_found = False
              else:
                  layer_index = int(layer_index.group(1))
                  if isinstance(layer_indexes, int):
                      target_module_found = layer_index == layer_indexes
                  else:
                      target_module_found = layer_index in layer_indexes
  
      return target_module_found
  
imports: from __future__ import annotations
  import logging
  import re
  import warnings
  from abc import ABC, abstractmethod
  from contextlib import contextmanager
  from typing import Any, List, Optional, Union
  import torch
  from accelerate.hooks import AlignDevicesHook
  from accelerate.utils import named_module_tensors, offload_state_dict
  from torch import nn
  from transformers import PreTrainedModel
  from transformers.pytorch_utils import Conv1D
  from peft.utils import INCLUDE_LINEAR_LAYERS_SHORTHAND
  from ..config import PeftConfig
  from ..utils import ModulesToSaveWrapper, _get_submodules
label_desc: Use image node_id 2 for calling a global function with example usage: check_target_module_exists(config, key) and returns: target_module_found