# Weird behavior when saving checkpoint in DDP

**URL:** <https://discuss.huggingface.co/t/weird-behavior-when-saving-checkpoint-in-ddp/101908>\
**Category:** 🤗Accelerate\
**Created:** [August 11, 2024, 9:17pm UTC](https://discuss.huggingface.co/t/weird-behavior-when-saving-checkpoint-in-ddp/101908 "2024-08-11T21:17:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danbochman](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/danbochman/32/29307_2.png) [@danbochman](https://discuss.huggingface.co/u/danbochman)\
**Post date:** [August 11, 2024, 9:17pm UTC](https://discuss.huggingface.co/t/weird-behavior-when-saving-checkpoint-in-ddp/101908/1 "2024-08-11T21:17:46Z")

</div>

Hi,

I am training on 4 A100s using `accelerate` in bf16 precision and torch.compile. Training is overall stable but sometimes collapses (but doesn’t diverge) when saving a checkpoint. The collapse manifests as very low gradient norms and the optimizer skipping steps. Sometimes the training recovers after the next checkpoint save (every 5k steps in the charts). Something weird happens whenever I save a checkpoint (or whenever I call `accelerator.wait_for_everyone()`), but I have no idea what…

This is how I save:

```python
    def save(self, path, overwrite=True):
        if not self.can_checkpoint:
            return

        fs = self.fs

        assert not (fs.exists(path) and not overwrite)

        save_obj = dict(
            model=self.unwrapped_model.state_dict(),
            steps=self.steps.cpu(),
            true_steps=self.true_steps.cpu(),
            optimizer=self.optimizer.state_dict(),
        )
        if exists(self.scheduler):
            save_obj["scheduler"] = self.scheduler.state_dict()

        if self.use_ema:
            save_obj["ema"] = self.unwrapped_ema_model.state_dict()

        # save to path
        with fs.open(path, "wb") as f:
            self.accelerator.save(save_obj, f)

        self.logger.info(f"checkpoint saved to {path}")

```

I would greatly appreciate any help… and many TIA

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/9/b/9b350b33b7617d1fb4271bc35d998d4b63c5ee67.png)
