code: def get_peft_model(
      model,
      r=16,
      target_modules=[
          "q_proj",
          "k_proj",
          "v_proj",
          "o_proj",
          "gate_proj",
          "up_proj",
          "down_proj",
      ],
      lora_alpha=16,
      lora_dropout=0,
      bias="none",
      layers_to_transform=None,
      use_gradient_checkpointing=True,
      random_state=3407,
      max_seq_length=2048,  # not used anymore
      **kwargs,
  ):
      assert max_seq_length <= model.max_seq_length
  
      if lora_dropout != 0:
          logger.warning_once(
              f"Unsloth: Dropout = 0 is supported for fast patching. You are using dropout = {lora_dropout}.\n"
              f"Unsloth will patch all other layers, except LoRA matrices, causing a performance hit."
          )
      if bias != "none":
          logger.warning_once(
              f"Unsloth: bias = `none` is supported for fast patching. You are using bias = {bias}.\n"
              f"Unsloth will patch all other layers, except LoRA matrices, causing a performance hit."
          )
  
      transformers_set_seed(random_state)
  
      accepted_modules = frozenset(
          (
              "q_proj",
              "k_proj",
              "v_proj",
              "o_proj",
              "gate_proj",
              "up_proj",
              "down_proj",
          ),
      )
      model.config.update({"unsloth_version": __version__})
      for module in target_modules:
          assert module in accepted_modules
      pass
  
      # Get LoRA
      lora_config = LoraConfig(
          r=r,
          lora_alpha=lora_alpha,
          target_modules=target_modules,
          lora_dropout=lora_dropout,
          bias=bias,
          task_type=TaskType.CAUSAL_LM,
          layers_to_transform=layers_to_transform,
          **kwargs,
      )
  
      model = prepare_model_for_kbit_training(
          model,
          use_gradient_checkpointing=use_gradient_checkpointing,
          use_reentrant=True,
      )
      model = _get_peft_model(model, lora_config)
  
      # Do patching
      n_mlp = 0
      n_qkv = 0
      n_o = 0
      if lora_dropout == 0 and bias == "none":
          for idx, layer in enumerate(model.model.model.layers):
              # MLP patching
              gate_proj = layer.mlp.gate_proj
              up_proj = layer.mlp.up_proj
              down_proj = layer.mlp.down_proj
  
              if (
                  hasattr(gate_proj, "lora_A")
                  and hasattr(up_proj, "lora_A")
                  and hasattr(down_proj, "lora_A")
                  and (
                      gate_proj.base_layer
                      if hasattr(gate_proj, "base_layer")
                      else gate_proj
                  ).bias
                  is None
                  and (
                      up_proj.base_layer
                      if hasattr(up_proj, "base_layer")
                      else up_proj
                  ).bias
                  is None
                  and (
                      down_proj.base_layer
                      if hasattr(down_proj, "base_layer")
                      else down_proj
                  ).bias
                  is None
              ):
                  # https://stackoverflow.com/questions/50599045/python-replacing-a-function-within-a-class-of-a-module
                  layer.mlp.forward = types.MethodType(
                      apply_lora_mlp, layer.mlp
                  )
                  n_mlp += 1
              else:
                  logger.warning_once(
                      "Unsloth cannot patch MLP layers with our manual autograd engine since either LoRA adapters\n"
                      "are not enabled or a bias term (like in Qwen) is used."
                  )
              pass
  
              # QKV attention patching
              q_proj = layer.self_attn.q_proj
              k_proj = layer.self_attn.k_proj
              v_proj = layer.self_attn.v_proj
              if (
                  hasattr(q_proj, "lora_A")
                  and hasattr(k_proj, "lora_A")
                  and hasattr(v_proj, "lora_A")
                  and (
                      q_proj.base_layer
                      if hasattr(q_proj, "base_layer")
                      else q_proj
                  ).bias
                  is None
                  and (
                      k_proj.base_layer
                      if hasattr(k_proj, "base_layer")
                      else k_proj
                  ).bias
                  is None
                  and (
                      v_proj.base_layer
                      if hasattr(v_proj, "base_layer")
                      else v_proj
                  ).bias
                  is None
              ):
                  layer.self_attn.apply_qkv = apply_lora_qkv
                  n_qkv += 1
              else:
                  logger.warning_once(
                      "Unsloth cannot patch Attention layers with our manual autograd engine since either LoRA adapters\n"
                      "are not enabled or a bias term (like in Qwen) is used."
                  )
              pass
  
              # O attention patching
              o_proj = layer.self_attn.o_proj
              if (
                  hasattr(o_proj, "lora_A")
                  and (
                      o_proj.base_layer
                      if hasattr(o_proj, "base_layer")
                      else o_proj
                  ).bias
                  is None
              ):
                  layer.self_attn.apply_o = apply_lora_o
                  n_o += 1
              else:
                  logger.warning_once(
                      "Unsloth cannot patch O projection layer with our manual autograd engine since either LoRA adapters\n"
                      "are not enabled or a bias term (like in Qwen) is used."
                  )
              pass
          pass
      pass
  
      logger.warning_once(
          f"Unsloth {__version__} patched {len(model.model.model.layers)} layers with "
          f"{n_qkv} QKV layers, {n_o} O layers and {n_mlp} MLP layers.",
      )
  
      # Patch cross entropy loss labels
      # Fixes https://github.com/unslothai/unsloth/issues/10
      max_seq_length = model.max_seq_length
      extra_ignored_labels = torch.full(
          (max_seq_length, 1), -100, device="cuda"
      )
      model.model.extra_ignored_labels = extra_ignored_labels
      internal_model = model
      while hasattr(internal_model, "model"):
          internal_model.max_seq_length = max_seq_length
          internal_model = internal_model.model
      pass
      internal_model.max_seq_length = max_seq_length
      return model
  
imports: import torch
  from typing import Optional, Tuple, List, Union
  from torch.nn.functional import scaled_dot_product_attention
  from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask
  from transformers.models.llama.modeling_llama import logger, BaseModelOutputWithPast, CausalLMOutputWithPast
  from ..kernels import *
  from ._utils import *
  from ._utils import __version__
  from transformers.models.llama.modeling_llama import LlamaAttention, LlamaDecoderLayer, LlamaModel, LlamaForCausalLM
  from peft import PeftModelForCausalLM
  import gc
  import peft
  import bitsandbytes
  import numpy
  import types
  from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, AutoConfig
  from transformers import set_seed
  from peft import LoraConfig, TaskType, get_peft_model
label_desc: Use image node_id 4 for calling the FastLlamaModel obj's underlying member method code with example usage: obj.get_peft_model(model, r, target_modules, lora_alpha, lora_dropout, bias, layers_to_transform, use_gradient_checkpointing, random_state, max_seq_length) and returns: model