class_docstr: A testing suite that makes sure that the PeftModel class is correctly integrated into the transformers library.
class_docstr_tok: ["A","testing","suite","that","makes","sure","that","the","PeftModel","class","is","correctly","integrated","into","the","transformers","library","."]
docstr: Simple test that tests if `add_adapter` works as expected when training with
  gradient checkpointing.
docstr_tok: ["Simple","test","that","tests","if","`","add_adapter","`","works","as","expected","when","training","with","gradient","checkpointing","."]
code: def test_peft_add_adapter_training_gradient_checkpointing(self):
      """
      Simple test that tests if `add_adapter` works as expected when training with
      gradient checkpointing.
      """
      from peft import LoraConfig
  
      for model_id in self.transformers_test_model_ids:
          for (
              transformers_class
          ) in self.transformers_test_model_classes:
              model = transformers_class.from_pretrained(model_id).to(
                  torch_device
              )
  
              peft_config = LoraConfig(init_lora_weights=False)
  
              model.add_adapter(peft_config)
  
              self.assertTrue(
                  self._check_lora_correctly_converted(model)
              )
  
              # When attaching adapters the input embeddings will stay frozen, this will
              # lead to the output embedding having requires_grad=False.
              dummy_input = torch.LongTensor(
                  [[0, 1, 2, 3, 4, 5, 6, 7]]
              ).to(torch_device)
              frozen_output = model.get_input_embeddings()(dummy_input)
              self.assertTrue(frozen_output.requires_grad is False)
  
              model.gradient_checkpointing_enable()
  
              # Since here we attached the hook, the input should have requires_grad to set
              # properly
              non_frozen_output = model.get_input_embeddings()(
                  dummy_input
              )
              self.assertTrue(non_frozen_output.requires_grad is True)
  
              # To repro the Trainer issue
              dummy_input.requires_grad = False
  
              for name, param in model.named_parameters():
                  if "lora" in name.lower():
                      self.assertTrue(param.requires_grad)
  
              logits = model(dummy_input).logits
              loss = logits.mean()
              loss.backward()
  
              for name, param in model.named_parameters():
                  if param.requires_grad:
                      self.assertTrue("lora" in name.lower())
                      self.assertTrue(param.grad is not None)
  
imports: import os
  import tempfile
  import unittest
  from huggingface_hub import hf_hub_download
  from transformers import AutoModelForCausalLM, OPTForCausalLM
  from transformers.testing_utils import require_peft, require_torch, require_torch_gpu, slow, torch_device
  from transformers.utils import is_torch_available
label_desc: Use image node_id 9 for calling the PeftIntegrationTester obj's underlying member method code with example usage: obj.test_peft_add_adapter_training_gradient_checkpointing() without return types