Pass callbacks kwarg to study.optimize() in run_hp_search_optuna()
Author: milos7250
Pull request: yes

# What does this PR do?

This PR allows the user to specify additional callbacks for optuna's `study.optimize()`. Previously, the callbacks kwarg would be passed to `optuna.create_study()` and would cause an Exception.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Is it possible to add an example of callback usage to make it clearer how this should be used?

A general example of setting up a callback can be found [on the optuna readthedocs](https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/007_optuna_callback.html). The idea is to allow users to have greater control over results in individual trials (e.g. adding more pruning conditions or further logging).

This PR allows the user to pass a callbacks argument to `trainer.hyperparameter_search()` containing list of callbacks to be used in the study. For example:

```python
class StopWhenTrialKeepBeingPrunedCallback:
    def __init__(self, threshold: int):
        self.threshold = threshold
        self._consequtive_pruned_count = 0

    def __call__(self, study: optuna.study.Study, trial: [optuna.trial.FrozenTrial](https://docs.python.org/3/library/abc.html#abc.ABC)) -> None:
        if trial.state == optuna.trial.TrialState.PRUNED:
            self._consequtive_pruned_count += 1
        else:
            self._consequtive_pruned_count = 0

        if self._consequtive_pruned_count >= self.threshold:
            study.stop()

trainer.hyperparameter_search(
    n_trials=25,
    direction="minimize",
    backend="optuna",
    hp_space=hyperparameter_search_ranges,
    study_name="my_study",
    storage=f"sqlite:///optuna.db",
    callbacks=[StopWhenTrialKeepBeingPrunedCallback(5)],
)
```

One more thing I've realized, the docstring for `hyperparameter_search` currently only mentions that `**kwargs` are passed to `optuna.create_study()`, this would need to be updated for this PR as callbacks are passed to `study.optimize()`. I am also not sure how this change would affect other hp search backends

This feature would be a really nice addition to the HPO optuna logic. What is the current status of this PR?

> This feature would be a really nice addition to the HPO optuna logic. What is the current status of this PR?

No changes since my previous comment. I have only worked with optuna, I think for this to be merged similar options should be introduced to other backends. There's also no tests or updated documentation yet.