code: def inject_adaptation_to_linear_layer(
      model: nn.Module,
      efficient_finetune: str,
      lora_r: int = None,
      lora_alpha: int = None,
      filter: Optional[List[str]] = None,
      module_filter: Optional[List[str]] = None,
      extra_trainable_params: Optional[List[str]] = None,
  ) -> nn.Module:
      """
      Injects trainable adatio Low-Rank decomposition matrices (LoRA) into linear
      layers of a PyTorch model. Used for efficient fine-tuning of large
      pre-trained models.
  
      Parameters
      ----------
      model
          A PyTorch model.
      efficient_finetune
          Efficient finetuning method that should be applied.
      lora_r
          The rank r of the low-rank decomposition.
      lora_alpha
          The scaling factor. Can be set to same value as r in
          most cases, as initialization is scaled already.
      filter
          Apply loRA only to linear layers filtered by name (e.g. "query.").
          If None, loRA is applied to all linear Layers in module.
      module_filter
          Apply loRA only to modules filtered by name (e.g. ".*EncDecAttention|.*DenseReluDense")
          If None, loRA is considered for all modules
      extra_trainable_params
          Not to apply loRA to modules filtered by name, and these modules are not frozen during training (e.g. "mask_decoder").
          If None, all the modules except for those applied loRA are frozen.
      Returns
      -------
      Model with injected LoRA modules.
      """
      for m_name, module in dict(model.named_modules()).items():
          if extra_trainable_params and any(
              re.match(filter_layer, m_name)
              for filter_layer in extra_trainable_params
          ):
              continue
          if hasattr(model, "frozen_layers") and any(
              re.search(filter_layer, m_name)
              for filter_layer in model.frozen_layers
          ):
              continue
          if not module_filter or any(
              re.match(filter_module, m_name)
              for filter_module in module_filter
          ):
              for c_name, layer in dict(
                  module.named_children()
              ).items():
                  if not filter or any(
                      re.match(filter_layer, c_name)
                      for filter_layer in filter
                  ):
                      assert isinstance(
                          layer, nn.Linear
                      ), f"LoRA can only be applied to torch.nn.Linear, but {layer} is {type(layer)}."
                      adaptation_layer = create_adaptation(
                          efficient_finetune, layer, lora_r, lora_alpha
                      )
                      adaptation_layer.weight = layer.weight
                      adaptation_layer.bias = layer.bias
                      setattr(module, c_name, adaptation_layer)
  
      return model
  
imports: import logging
  import re
  import warnings
  from typing import Dict, List, Optional, Tuple
  import torch
  import torch._dynamo
  import torch.nn.functional
  from torch import nn
  from torch.nn.modules.loss import _Loss
  from transformers import AutoConfig, AutoModel, AutoTokenizer, BertTokenizer, CLIPTokenizer, ElectraTokenizer
  from transformers.models.mask2former.modeling_mask2former import Mask2FormerLoss
  from ..constants import AUTOMM, CLASS_LOGITS, COLUMN_FEATURES, FEATURES, LOGITS, MASKS, OCR, PEFT_ADDITIVE_STRATEGIES, REGRESSION, SEMANTIC_MASK, SEMANTIC_SEGMENTATION
  from .adaptation_layers import IA3Linear, IA3LoRALinear, LoRALinear
label_desc: Use image node_id 10 for calling a global function with example usage: inject_adaptation_to_linear_layer(model, efficient_finetune, lora_r, lora_alpha, filter, module_filter, extra_trainable_params) and returns: model