[Trainer] [Breaking change] `use_cache` default to `False`
Author: SunMarc
Pull request: yes

# What does this PR do?

This PR adds a new arg that allows users to change the value of `use_cache` in the model config. For training, usually we don't need to use a cache and this created quite a lot of issues in the past (incompatibility, regression, lower perfs). Now that v5 is coming, let's do this change. 

cc @gante 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_41585). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

>  If that is right, I think we don't need the exception for PEFT. Those PEFT methods that need to use the cache will create it and pass it explicitly as the past_key_values argument. Thus, it will be used even if use_cache = False, right?

If those methods create the `past_key_values` argument without checking `use_cache` argument, then probably yes. Do you have tests in peft for those methods ? cc @gante for visibility 

> If those methods create the `past_key_values` argument without checking `use_cache` argument, then probably yes. Do you have tests in peft for those methods ?

I ran a couple of tests in PEFT on this branch*:

`pytest tests/test_decoder_models.py tests/test_encoder_decoder_models.py -k "prefix or prompt"`

Even if I hard-code `self.model.config.use_cache = False`, they pass, so I think we're good even without the PEFT exception.

*I had to also fix the `DynamicCache` init issue discussed on the other PR.

> I ran a couple of tests in PEFT on this branch*:
> 
> pytest tests/test_decoder_models.py tests/test_encoder_decoder_models.py -k "prefix or prompt"
> 
> Even if I hard-code self.model.config.use_cache = False, they pass, so I think we're good even without the PEFT exception.
> 
> *I had to also fix the DynamicCache init issue discussed on the other PR.

Thanks for checking ! I reverted the changes about peft and just set the `default` to False. 

> SG, we also have by default in the check_model_inputs something that sets use_cache to false if training
```python
    def wrapped_fn(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            use_cache_arg_index = None
            if "use_cache" in func.__code__.co_varnames:
                use_cache_arg_index = func.__code__.co_varnames.index("use_cache") - 1  # -1 for self

            if (
                use_cache_arg_index is not None
                and len(args) > use_cache_arg_index
                and args[use_cache_arg_index] is not None
            ):
                use_cache = args[use_cache_arg_index]
            elif kwargs.get("use_cache") is not None:
                use_cache = kwargs["use_cache"]
            else:
                use_cache = getattr(self.config, "use_cache", None)

            if use_cache is not None:
                if getattr(self, "gradient_checkpointing", False) and self.training and use_cache:
                    logger.warning_once(
                        "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
                    )
                    use_cache = False
```

Only when the users tries to use gradient accumulation also !