---
title: "Fixing Gradient Accumulation"
description: "Gradient accumulation in the transformers Trainer did not match full-batch training because the default model loss averaged per-batch cross-entropy instead of summing token losses across all accumulated batches and dividing by the total count of non-padding tokens."
url: "https://swe.quest/articles/fixing-gradient-accumulation-48a9d96c28c11b53"
canonical: "https://huggingface.co/blog/gradient_accumulation"
source: "Hugging Face"
published: "2024-10-16"
difficulty: "medium"
kind: "Incident"
reading_minutes: 4
tags: ["model-training","python","debugging"]
---

# Fixing Gradient Accumulation

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-10-16, 4 min read, Incident. Original: [https://huggingface.co/blog/gradient\_accumulation](https://huggingface.co/blog/gradient_accumulation)

## Summary

Gradient accumulation in the transformers Trainer did not match full-batch training because the default model loss averaged per-batch cross-entropy instead of summing token losses across all accumulated batches and dividing by the total count of non-padding tokens. The fix switches the causal LM loss to sum reduction normalized by a passed-in num\_items. Models inheriting PreTrainedModel also gain a loss\_function property configurable via config.loss\_type and LOSS\_MAPPING, and the Trainer will accept user-supplied loss functions. The fix is available by installing transformers from main.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)

## Prerequisites

- gradient accumulation
- cross-entropy loss
- PyTorch training loops ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))

## Keywords

Hugging Face Transformers, Trainer, Unsloth, PyTorch, gradient accumulation

## Related posts

- [🐯 Liger GRPO meets TRL](https://swe.quest/articles/liger-grpo-meets-trl-4a551ec5e4d7e307.md): Hugging Face · 2025-05-25 · Hard · 13 min read · Model training, LLMs, Performance engineering
  Liger's chunked loss, extended to GRPO and integrated into TRL's GRPOTrainer, cuts peak training memory by up to 40% with no change in reward curves.
- [Accelerating Transformers Fine-Tuning with NVIDIA NeMo AutoModel](https://swe.quest/articles/accelerating-transformers-fine-tuning-with-nvidia-nemo-automodel-c196d721b8de0a9a.md): Hugging Face · 2026-06-24 · Hard · 10 min read · Model training, Performance engineering, LLMs, Python
  NeMo AutoModel subclasses Transformers v5's AutoModelForCausalLM and adds Expert Parallelism as a dedicated mesh orthogonal to data parallelism, DeepEP fused all-to-all dispatch, and TransformerEngine kernels.
- [Unlocking Agentic RL Training for GPT-OSS: A Practical Retrospective](https://swe.quest/articles/unlocking-agentic-rl-training-for-gpt-oss-a-practical-retrospective-2439023c679282b9.md): Hugging Face · 2026-01-27 · Hard · 21 min read · Model training, LLMs, AI agents, Debugging
  Agentic RL training of GPT-OSS-20B in verl initially failed with exploding gradient norms, KL and entropy, and flat rewards.
- [Accelerate 1.0.0](https://swe.quest/articles/accelerate-1-0-0-3876b57c9168d12c.md): Hugging Face · 2024-09-13 · Medium · 5 min read · Model training, Python, Machine learning
  Hugging Face has published the first release candidates for Accelerate 1.0.0.
- [Hugging Face's TensorFlow Philosophy](https://swe.quest/articles/hugging-face-s-tensorflow-philosophy-1d3751167c605c69.md): Hugging Face · 2022-08-12 · Medium · 14 min read · Machine learning, Python, Model training, Compilers
  Hugging Face's transformers library treats TensorFlow models as first-class Keras citizens: every model is a Keras Model and every layer a Keras Layer, so fit(), compile() and predict() work directly, and models compose into custom hybrids via subclassing.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
