class_docstr: A large testing suite for testing common functionality of the PEFT models.
  
  Attributes:
      torch_device (`torch.device`):
          The device on which the tests will be run.
      transformers_class (`transformers.PreTrainedModel`):
          The transformers class that is being tested.
class_docstr_tok: ["A","large","testing","suite","for","testing","common","functionality","of","the","PEFT","models",".","Attributes",":","torch_device","(","`","torch.device","`",")",":","The","device","on","which","the","tests","will","be","run",".","transformers_class","(","`","transformers.PreTrainedModel","`",")",":","The","transformers","class","that","is","being","tested","."]
code: def _test_merge_layers(self, model_id, config_cls, config_kwargs):
      if config_cls not in (LoraConfig, IA3Config):
          # Merge layers only supported for LoRA and IA³
          return
      if ("gpt2" in model_id.lower()) and (config_cls != LoraConfig):
          self.skipTest(
              "Merging GPT2 adapters not supported for IA³ (yet)"
          )
  
      model = self.transformers_class.from_pretrained(model_id)
      config = config_cls(
          base_model_name_or_path=model_id,
          **config_kwargs,
      )
      model = get_peft_model(model, config)
      model = model.to(self.torch_device)
  
      if config.peft_type not in ("IA3", "LORA"):
          with self.assertRaises(AttributeError):
              model = model.merge_and_unload()
  
      dummy_input = self.prepare_inputs_for_testing()
      model.eval()
      logits = model(**dummy_input)[0]
  
      model.merge_adapter()
      logits_merged = model(**dummy_input)[0]
      model.unmerge_adapter()
      logits_unmerged = model(**dummy_input)[0]
  
      model = model.merge_and_unload()
      logits_merged_unloaded = model(**dummy_input)[0]
  
      atol, rtol = 1e-4, 1e-4
      if (config.peft_type == "IA3") and (model_id == "Conv2d"):
          # for some reason, the IA³ Conv2d introduces a larger error
          atol, rtol = 0.3, 0.01
      self.assertTrue(
          torch.allclose(logits, logits_merged, atol=atol, rtol=rtol)
      )
      self.assertTrue(
          torch.allclose(logits, logits_unmerged, atol=atol, rtol=rtol)
      )
      self.assertTrue(
          torch.allclose(
              logits, logits_merged_unloaded, atol=atol, rtol=rtol
          )
      )
  
      # For this test to work, weights should not be initialized to identity transform (e.g.
      # init_lora_weights should be False).
      transformers_model = self.transformers_class.from_pretrained(
          model_id
      ).to(self.torch_device)
      logits_transformers = transformers_model(**dummy_input)[0]
      self.assertFalse(
          torch.allclose(
              logits_merged, logits_transformers, atol=1e-10, rtol=1e-10
          )
      )
  
      # test that the logits are identical after a save-load-roundtrip
      if hasattr(model, "save_pretrained"):
          # model is a transformers model
          with tempfile.TemporaryDirectory() as tmp_dirname:
              model.save_pretrained(tmp_dirname)
              model_from_pretrained = (
                  self.transformers_class.from_pretrained(
                      tmp_dirname
                  ).to(self.torch_device)
              )
      else:
          # model is not a transformers model
          model_from_pretrained = pickle.loads(pickle.dumps(model))
  
      logits_merged_from_pretrained = model_from_pretrained(
          **dummy_input
      )[0]
      self.assertTrue(
          torch.allclose(
              logits_merged,
              logits_merged_from_pretrained,
              atol=atol,
              rtol=rtol,
          )
      )
  
imports: import copy
  import json
  import os
  import pickle
  import re
  import tempfile
  from collections import OrderedDict
  from dataclasses import replace
  import torch
  import yaml
  from diffusers import StableDiffusionPipeline
  from peft import AdaLoraConfig, IA3Config, LoraConfig, PeftModel, PeftType, PrefixTuningConfig, PromptEncoderConfig, PromptLearningConfig, PromptTuningConfig, get_peft_model, get_peft_model_state_dict, prepare_model_for_int8_training
  from peft.tuners.lora import LoraLayer
  from peft.utils import _get_submodules, infer_device
  from .testing_utils import get_state_dict
label_desc: Use image node_id 12 for calling the PeftCommonTester obj's underlying member method code with example usage: obj._test_merge_layers(model_id, config_cls, config_kwargs) without return types