{"record":{"id":"9ea36d103ffcc769","repo":"Lightning-AI/pytorch-lightning","slug":"you-have-set-accumulate-grad-batches-and-are-usi","errorCode":null,"errorMessage":"You have set `accumulate_grad_batches` and are using the `GradientAccumulationScheduler` callback. Either remove `accumulate_grad_batches` from the Trainer or remove the callback.","messagePattern":"You have set `accumulate_grad_batches` and are using the `GradientAccumulationScheduler` callback\\. Either remove `accumulate_grad_batches` from the Trainer or remove the callback\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/gradient_accumulation_scheduler.py","lineNumber":133,"sourceCode":"        going_to_accumulate_grad_batches = self.going_to_accumulate_grad_batches()\n        has_overridden_optimization_functions = overridden_optimizer_step or overridden_optimizer_zero_grad\n        if has_overridden_optimization_functions and going_to_accumulate_grad_batches:\n            rank_zero_warn(\n                \"When using `Trainer(accumulate_grad_batches != 1)` and overriding\"\n                \" `LightningModule.optimizer_{step,zero_grad}`, the hooks will not be called on every batch\"\n                \" (rather, they are called on every optimization step).\"\n            )\n\n        # local import to avoid circular import\n        from lightning.pytorch.strategies import DeepSpeedStrategy\n\n        if isinstance(trainer.strategy, DeepSpeedStrategy):\n            raise RuntimeError(\n                f\"The `{type(trainer.strategy).__name__}` does not support `accumulate_grad_batches` changing\"\n                \" between epochs.\"\n            )\n        if trainer.accumulate_grad_batches != 1:\n            raise ValueError(\n                \"You have set `accumulate_grad_batches` and are using the `GradientAccumulationScheduler`\"\n                \" callback. Either remove `accumulate_grad_batches` from the Trainer or remove the callback.\"\n            )\n\n    @override\n    def on_train_epoch_start(self, trainer: \"pl.Trainer\", *_: Any) -> None:\n        trainer.accumulate_grad_batches = self.get_accumulate_grad_batches(trainer.current_epoch)\n","sourceCodeStart":115,"sourceCodeEnd":141,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/gradient_accumulation_scheduler.py#L115-L141","documentation":"GradientAccumulationScheduler itself sets `trainer.accumulate_grad_batches` per epoch, so a Trainer-level value other than 1 would conflict. `on_train_start` raises ValueError if `trainer.accumulate_grad_batches != 1`, telling you to remove one of the two mechanisms.","triggerScenarios":"`Trainer(accumulate_grad_batches=4, callbacks=[GradientAccumulationScheduler({0: 8})])` — any value besides 1 on the Trainer triggers it at train start (with non-DeepSpeed strategies).","commonSituations":"Copy-pasting a Trainer that already had accumulate_grad_batches and then adding the scheduler callback; sweeps where both knobs are set independently.","solutions":["Delete `accumulate_grad_batches` from the Trainer and encode the desired accumulation in the scheduler dict (e.g. start value `{0: 4}`)","Or drop the callback and keep a single fixed Trainer value","Note DeepSpeed users hit the previous RuntimeError (291) instead"],"exampleFix":"# before\nTrainer(accumulate_grad_batches=4, callbacks=[GradientAccumulationScheduler({0: 8})])\n# after\nTrainer(callbacks=[GradientAccumulationScheduler({0: 4, 5: 8})])","handlingStrategy":"validation","validationCode":"has_scheduler = any(isinstance(c, GradientAccumulationScheduler) for c in callbacks)\nif has_scheduler:\n    assert trainer_kwargs.get('accumulate_grad_batches', 1) == 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep accumulation configured in exactly one place","Centralize Trainer construction so conflicting flags can't both be set by different config sections"],"tags":["lightning","gradient-accumulation","config-conflict","trainer-options"],"backgroundTag":"conflicting-config-options","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}