# Some question about training\_step Function in Class Trainer

**URL:** <https://discuss.huggingface.co/t/some-question-about-training-step-function-in-class-trainer/108361>\
**Category:** Beginners\
**Created:** [September 24, 2024, 11:23am UTC](https://discuss.huggingface.co/t/some-question-about-training-step-function-in-class-trainer/108361 "2024-09-24T11:23:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![satwoman](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@satwoman](https://discuss.huggingface.co/u/satwoman)\
**Post date:** [September 24, 2024, 11:23am UTC](https://discuss.huggingface.co/t/some-question-about-training-step-function-in-class-trainer/108361/1 "2024-09-24T11:23:59Z")

</div>

I’m analyzing the Class Trainer source code and have a question regarding the training\_step function. In this function, it appears that the backward pass is performed before the function returns the loss. Specifically, after calculating the loss, the code calls scaled\_loss.backward() (or self.accelerator.backward(loss)) before returning loss.detach() / self.args.gradient\_accumulation\_steps.

Does this mean that the function first performs backpropagation on the computed loss and then divides the loss by self.args.gradient\_accumulation\_steps before returning it? How does this fit into the gradient accumulation strategy?

The source code is very short. I hope you can help me take a look.🥹  
Thanks for your help!  
[Link to the source code of the training\_step function.](https://github.com/huggingface/transformers/blob/v4.44.2/src/transformers/trainer.py#L3293)
