Repository: huggingface/transformers
Pull request: fix: learning_rate logged as tensor causing save issue with deepspeed

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

I found that fine-tuning Llama4 text-only mode with flex attention and deepspeed zero1 causes the learning_rate to be logged as a tensor. 

The error is: `TypeError: Object of type Tensor is not JSON serializable`. Looking at past issues point to it being a deepspeed/transformers issue. However, I tried to swap deepspeed versions across 0.14.4, 0.15.4, 0.16.7 and received the same issue. (0.13.1 has some api incompatibility with transformers)

I found that the learning_rate was logged as tensor

before fix

```                                                                                                                                               
{'loss': 1.1754, 'learning_rate': tensor(1.0000e-05), 'epoch': 0.0}           
{'loss': 1.0044, 'learning_rate': tensor(1.5000e-05), 'epoch': 0.0}           
{'loss': 0.9941, 'learning_rate': tensor(2.0000e-05), 'epoch': 0.0}           
{'loss': 1.1097, 'learning_rate': tensor(2.5000e-05), 'epoch': 0.0} 
```
(ignore missing grad_norm, I was testing whether it was grad_norm's fault)

with fix
```
{'loss': 1.1754, 'grad_norm': 0.7218227982521057, 'learning_rate': 9.999999747378752e-06, 'epoch': 0.0}                                                      
{'loss': 1.0051, 'grad_norm': 0.6964300870895386, 'learning_rate': 1.500000053056283e-05, 'epoch': 0.0}                                                      
{'loss': 0.993, 'grad_norm': 0.6806543469429016, 'learning_rate': 1.9999999494757503e-05, 'epoch': 0.0}                                                      
{'loss': 1.1091, 'grad_norm': 0.7640533447265625, 'learning_rate': 2.499999936844688e-05, 'epoch': 0.0}                                                      
{'loss': 1.2739, 'grad_norm': 0.8041514158248901, 'learning_rate': 3.000000106112566e-05, 'epoch': 0.0}
```

This was run in Axolotl. Config [here](https://gist.github.com/NanoCode012/9dcdb653dea7f9da0d9ea8970763ee3e)


## 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: @eustlb
- graph models: @clefourrier

Library:

- flax: @gante and @Rocketknight1
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @zach-huggingface and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @SunMarc @zach-huggingface
- 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: @Rocketknight1
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Changed files:
filename: src/transformers/trainer.py
patch: @@ -3074,7 +3074,9 @@ def _maybe_log_save_evaluate(
             if grad_norm is not None:
                 logs["grad_norm"] = grad_norm.item() if isinstance(grad_norm, torch.Tensor) else grad_norm
             if learning_rate is not None:
-                logs["learning_rate"] = learning_rate
+                logs["learning_rate"] = (
+                    learning_rate.item() if isinstance(learning_rate, torch.Tensor) else learning_rate
+                )
             else:
                 logs["learning_rate"] = self._get_learning_rate()