    def _finalize_best_model_state(self) -> None:
        """
        The best model weights might be saved in sharded files, in which case we gather them
        up and save them to a single 'best.th' file.
        """
        if (
            self._serialization_dir
            and self._ddp_wrapped_model is not None
            and self._ddp_wrapped_model.is_sharded
        ):
            logger.info("Consolidating sharded model states")
            sharded_model_state_files = list(
                glob.iglob(os.path.join(self._serialization_dir, "best_w*.th"))
            )
            full_model_state = self._ddp_wrapped_model.consolidate_sharded_state(
                sharded_model_state_files
            )
            self._best_model_filename = os.path.join(self._serialization_dir, "best.th")
            torch.save(full_model_state, self._best_model_filename)
The best model weights might be saved in sharded files, in which case we gather them
up and save them to a single 'best.th' file.
allennlp/training/gradient_descent_trainer.py