PEFT training with gradient checkpointing fails
Author: qgallouedec
Pull request: no
Labels: name: bug

### System Info


- `transformers` version: 5.0.0.dev0
- PEFT : 0.18.0
- Platform: Linux-5.15.0-1048-aws-x86_64-with-glibc2.31
- Python version: 3.12.12
- Huggingface_hub version: 1.1.5
- Safetensors version: 0.7.0
- Accelerate version: 1.12.0.dev0
- Accelerate config:    not found
- DeepSpeed version: 0.18.2
- PyTorch version (accelerator?): 2.9.0+cu128 (CUDA)
- Using distributed or parallel set-up in script?: No
- Using GPU in script?: Yes
- GPU type: NVIDIA H100 80GB HBM3

### Who can help?

@BenjaminBossan @SunMarc 

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

```python
import numpy as np
from datasets import Dataset
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM, Trainer, TrainingArguments

data = np.random.randint(0, 1000, (16, 64)).tolist()
dataset = Dataset.from_dict({"input_ids": data, "labels": data})
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-0.6B", dtype="auto", device_map="auto")
model = get_peft_model(model, LoraConfig())
trainer = Trainer(
    model=model,
    args=TrainingArguments(gradient_checkpointing=True),
    train_dataset=dataset,
)
trainer.train()
```


```
Loading weights: 100%|██████████████████████████████████████████████████████| 311/311 [00:00<00:00, 1662.75it/s, Materializing param=lm_head.weight]
The model is already on multiple devices. Skipping the move to device specified in `args`.
  0%|                                                                                                                         | 0/6 [00:00<?, ?it/s]/fsx/qgallouedec/miniconda3/envs/trl/lib/python3.12/site-packages/torch/utils/checkpoint.py:85: UserWarning: None of the inputs have requires_grad=True. Gradients will be None
  warnings.warn(
Traceback (most recent call last):
  File "/fsx/qgallouedec/trl/sandbox/prepare_model_fp32_lora_gc_on.py", line 23, in <module>
  File "/fsx/qgallouedec/trl/sandbox/prepare_model_fp32_lora_gc_on.py", line 19, in main
    
  File "/fsx/qgallouedec/transformers/src/transformers/trainer.py", line 2155, in train
    return inner_training_loop(
           ^^^^^^^^^^^^^^^^^^^^
  File "/fsx/qgallouedec/transformers/src/transformers/trainer.py", line 2486, in _inner_training_loop
    tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/qgallouedec/transformers/src/transformers/trainer.py", line 3793, in training_step
    self.accelerator.backward(loss, **kwargs)
  File "/fsx/qgallouedec/miniconda3/envs/trl/lib/python3.12/site-packages/accelerate/accelerator.py", line 2760, in backward
    loss.backward(**kwargs)
  File "/fsx/qgallouedec/miniconda3/envs/trl/lib/python3.12/site-packages/torch/_tensor.py", line 625, in backward
    torch.autograd.backward(
  File "/fsx/qgallouedec/miniconda3/envs/trl/lib/python3.12/site-packages/torch/autograd/__init__.py", line 354, in backward
    _engine_run_backward(
  File "/fsx/qgallouedec/miniconda3/envs/trl/lib/python3.12/site-packages/torch/autograd/graph.py", line 841, in _engine_run_backward
    return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
```



### Expected behavior

The above config shouldn't fail.

To solve the issue, we just have to add

```python
model.enable_input_require_grads()
```

But, the issue is that the Trainer should do this itself. I think that the issue come from these lines:

https://github.com/huggingface/transformers/blob/453a246113254b173e044f13a22c9e6b56259aee/src/transformers/modeling_utils.py#L3043-L3048

when the model is a `PEFTModel`, as in the above script, the condition is not `True`, but in my understanding, it should be `True`. Maybe we have to check whether the model is a PeftModel differently?

Comments:
@qgallouedec I have made a pr on this issue please check it.
#42494 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Still need to be addressed 

@qgallouedec has the issue still not resolved ?

Indeed, this was solved in #42558