Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 4 min read Incident mediumAdept

Fixing Gradient Accumulation

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.

Assumes: gradient accumulationcross-entropy lossPyTorch training loops

Related posts

  • 🐯 Liger GRPO meets TRL

    Hugging Face hardMaster

    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. It chunks the lm_head input along the batch and computes each chunk's gradients during the forward pass, so full logits never need to be held for backward. Benchmarks on Qwen3-0.6B with gsm8k show savings growing with batch size. FSDP and PEFT support allow 1.5–1.8x larger batch sizes across Qwen3 sizes, and LoRA removes the need for a separate reference model. Setup is a `use_liger_loss=True` flag, with optional vLLM-server generation for faster rollouts.

  • Accelerating Transformers Fine-Tuning with NVIDIA NeMo AutoModel

    Hugging Face hardMaster

    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. Users swap one import and get 3.4-3.7x higher MoE fine-tuning throughput and 29-32% lower peak memory on Qwen3-30B-A3B and Nemotron 3 Nano on 8 H100s. NeMo AutoModel's benchmarks used a balanced routing gate while v4/v5 ran their native routers. The same path enables full fine-tuning of the 550B Nemotron 3 Ultra across 128 GPUs, where v5 runs out of memory. Explanations cover why v4 deadlocks under FSDP (data-dependent per-expert loops cause mismatched collectives), v5's expert backends (eager, batched_mm, grouped_mm), and reversible dynamic weight loading that keeps checkpoints in standard HF format.

  • Unlocking Agentic RL Training for GPT-OSS: A Practical Retrospective

    Hugging Face hardMaster

    Agentic RL training of GPT-OSS-20B in verl initially failed with exploding gradient norms, KL and entropy, and flat rewards. The LinkedIn team traced this to three problems. First, MoE routing nondeterminism made on-policy PPO importance ratios deviate from 1, fixed by reusing the detached current log-prob as the old log-prob. Second, a training–inference mismatch between SGLang/vLLM and FSDP was partly mitigated with sequence-level rollout correction. Third, and most important, FlashAttention v2 lacks support for GPT-OSS attention sinks, so the team took the forward pass from vLLM's FlashAttention v3 fork and implemented the sink-gradient backward pass, deriving the gradient formula. With the fix, training is stable and rewards improve steadily on GSM8K, verifiable instruction following and multi-turn ReTool tool use; the post also covers FSDP memory blow-ups caused by repeated MoE expert materialization.

  • Accelerate 1.0.0

    Hugging Face mediumAdept

    Hugging Face has published the first release candidates for Accelerate 1.0.0. The library has grown from a thin multi-GPU/TPU training-loop wrapper into the base layer for transformers, diffusers, peft and trl, covering six accelerator types, a launch CLI and big-model inference via device_map="auto". Before 1.0 the team added FP8 support through MS-AMP and TransformerEngine, multi-model DeepSpeed orchestration, torch.compile for big model inference, torch.distributed.pipelining and StatefulDataLoader. Migration notes list the breaking deprecations, such as moving dataloader flags into DataLoaderConfiguration, using AutocastKwargs and renaming is_tpu_available to is_torch_xla_available.

  • Hugging Face's TensorFlow Philosophy

    Hugging Face mediumAdept

    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. If compile() is called without a loss argument, a sensible default loss is supplied (e.g. masked LM loss that matches BERT's original training), but a user-specified loss overrides it. Labels can now be passed the standard Keras way, and prepare_tf_dataset() streams disk-backed datasets with per-batch padding. XLA via jit_compile=True brings generation and training to roughly JAX-level speed, with the caveat that variable input shapes force recompilation.

The quest for software engineering knowledge.

Look:
Layout: