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_save_pretrained_selected_adapters(
      self, model_id, config_cls, config_kwargs, safe_serialization=True
  ):
      if issubclass(config_cls, AdaLoraConfig):
          # AdaLora does not support adding more than 1 adapter
          return
  
      # ensure that the weights are randomly initialized
      if issubclass(config_cls, LoraConfig):
          config_kwargs = config_kwargs.copy()
          config_kwargs["init_lora_weights"] = False
      if issubclass(config_cls, IA3Config):
          config_kwargs = config_kwargs.copy()
          config_kwargs["init_ia3_weights"] = False
  
      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)
  
      new_adapter_config = config_cls(
          base_model_name_or_path=model_id,
          **config_kwargs,
      )
  
      model.add_adapter("new_adapter", new_adapter_config)
  
      with tempfile.TemporaryDirectory() as tmp_dirname:
          if safe_serialization:
              model.save_pretrained(tmp_dirname)
          else:
              model.save_pretrained(
                  tmp_dirname, safe_serialization=False
              )
  
          model_from_pretrained = (
              self.transformers_class.from_pretrained(model_id)
          )
          model_from_pretrained = PeftModel.from_pretrained(
              model_from_pretrained, tmp_dirname
          )
  
          new_adapter_dir = os.path.join(tmp_dirname, "new_adapter")
          model_from_pretrained.load_adapter(
              new_adapter_dir, "new_adapter"
          )
  
          # check if the state dicts are equal
          if issubclass(config_cls, PromptEncoderConfig):
              # For prompt encoding, when loading the whole state_dict, there are differences, therefore, only load
              # adapter-specific weights for comparison.
              # TODO: is this expected?
              state_dict = get_peft_model_state_dict(
                  model, unwrap_compiled=True
              )
              state_dict_from_pretrained = get_peft_model_state_dict(
                  model_from_pretrained, unwrap_compiled=True
              )
          else:
              state_dict = get_state_dict(model, unwrap_compiled=True)
              state_dict_from_pretrained = get_state_dict(
                  model_from_pretrained, unwrap_compiled=True
              )
  
          # check if same keys
          self.assertEqual(
              state_dict.keys(), state_dict_from_pretrained.keys()
          )
  
          # check if tensors equal
          for key in state_dict.keys():
              self.assertTrue(
                  torch.allclose(
                      state_dict[key].to(self.torch_device),
                      state_dict_from_pretrained[key].to(
                          self.torch_device
                      ),
                  )
              )
  
          target_adapter_filename = (
              "adapter_model.safetensors"
              if safe_serialization
              else "adapter_model.bin"
          )
  
          # check if `adapter_model.safetensors` is present
          self.assertTrue(
              os.path.exists(
                  os.path.join(tmp_dirname, target_adapter_filename)
              )
          )
          self.assertTrue(
              os.path.exists(
                  os.path.join(new_adapter_dir, target_adapter_filename)
              )
          )
  
          # check if `adapter_config.json` is present
          self.assertTrue(
              os.path.exists(
                  os.path.join(tmp_dirname, "adapter_config.json")
              )
          )
          self.assertTrue(
              os.path.exists(
                  os.path.join(new_adapter_dir, "adapter_config.json")
              )
          )
  
          # check if `model.safetensors` is not present
          self.assertFalse(
              os.path.exists(
                  os.path.join(tmp_dirname, "model.safetensors")
              )
          )
          self.assertFalse(
              os.path.exists(
                  os.path.join(new_adapter_dir, "model.safetensors")
              )
          )
  
          # check if `config.json` is not present
          self.assertFalse(
              os.path.exists(os.path.join(tmp_dirname, "config.json"))
          )
          self.assertFalse(
              os.path.exists(
                  os.path.join(new_adapter_dir, "config.json")
              )
          )
  
          self.check_modelcard(tmp_dirname, model)
          self.check_config_json(tmp_dirname, model)
  
      with tempfile.TemporaryDirectory() as tmp_dirname:
          model.save_pretrained(
              tmp_dirname, selected_adapters=["default"]
          )
  
          model_from_pretrained = (
              self.transformers_class.from_pretrained(model_id)
          )
          model_from_pretrained = PeftModel.from_pretrained(
              model_from_pretrained, tmp_dirname
          )
  
          self.assertTrue(
              "default" in model_from_pretrained.peft_config.keys()
          )
          self.assertTrue(
              "new_adapter"
              not in model_from_pretrained.peft_config.keys()
          )
  
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 8 for calling the PeftCommonTester obj's underlying member method code with example usage: obj._test_save_pretrained_selected_adapters(model_id, config_cls, config_kwargs, safe_serialization) without return types