Make schedulers picklable by making lr_lambda fns global
Author: connor-henderson
Pull request: yes

# What does this PR do?
Make schedulers picklable by making lr_lambda fns global, at the cost of the extra step of arg passing and using `partial`.

Closes #21689

Implements the change mentioned in the issue across the following functions in optimizations.py:
`get_constant_schedule`
`get_constant_schedule_with_warmup`
`get_linear_schedule_with_warmup`
`get_cosine_schedule_with_warmup`
`get_cosine_with_hard_restarts_schedule_with_warmup`
`get_inverse_sqrt_schedule`



## 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#start-contributing-pull-requests),
      Pull Request section?
- [x] 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. [link](https://github.com/huggingface/transformers/issues/21689)
- [ ] 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.

@sgugger

<!-- 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 and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer: @stas00, Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

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: @sgugger
- TensorFlow: @Rocketknight1

 -->


Comments:
_The documentation is not available anymore as the PR was closed or merged._

It may be a good idea to test this feature:

In file `tests/optimization/test_optimization.py`:
Add class
```python
class LambdaScheduleWrapper:
    """See https://github.com/huggingface/transformers/issues/21689"""
    def __init__(self, fn):
        self.fn = fn

    def __call__(self, *args, **kwargs):
        return self.fn(*args, **kwargs)

    @classmethod
    def wrap_scheduler(cls, scheduler: LambdaLR):
        scheduler.lr_lambdas = list(map(cls, scheduler.lr_lambdas))
```

And wrap the schedulers before testing the reload process in `ScheduleInitTest.test_schedulers`:
```python
            ...
            scheduler = scheduler_func(self.optimizer, **kwargs)
++          LambdaScheduleWrapper.wrap_scheduler(scheduler)  # wrap to test picklability of the schedule
            lrs_2 = unwrap_and_save_reload_schedule(scheduler, self.num_steps)
            self.assertListEqual(lrs_1, lrs_2, msg=f"failed for {scheduler_func} in save and reload")
```


> It may be a good idea to test this feature:

Thank you for filing the issue and sharing this test! I'll leave the decision of whether we should include this test to the maintainers. It may come down to tests being explicitly for functionality as opposed to something like picklability.

Yes it would be nice to have a test such as the one above.

Thank you for the comments and sorry for the delay

Thanks again for your contribution!