Download diffsynth/diffusion/logger.py from Jingkuan/BWM-Turbo: direct link, hf CLI and curl.
- Browser
- Download file 4.68 kB
-
https://huggingface.co/Jingkuan/BWM-Turbo/resolve/main/diffsynth/diffusion/logger.py
- Command line
-
hf download hf://Jingkuan/BWM-Turbo/diffsynth/diffusion/logger.py
-
curl -L -o logger.py https://huggingface.co/Jingkuan/BWM-Turbo/resolve/main/diffsynth/diffusion/logger.py
4.68 kB
| import os, json, torch | |
| from accelerate import Accelerator | |
| class ModelLogger: | |
| def __init__(self, output_path, remove_prefix_in_ckpt=None, state_dict_converter=lambda x:x, config=None): | |
| self.output_path = output_path | |
| self.remove_prefix_in_ckpt = remove_prefix_in_ckpt | |
| self.state_dict_converter = state_dict_converter | |
| self.num_steps = 0 | |
| self.config = config | |
| if self.output_path is not None: | |
| os.makedirs(self.output_path, exist_ok=True) | |
| self._save_config(self.output_path) | |
| def _save_config(self, output_dir): | |
| if self.config is None: | |
| return | |
| config_path = os.path.join(output_dir, "config.json") | |
| with open(config_path, "w", encoding="utf-8") as f: | |
| json.dump(self.config, f, indent=2, ensure_ascii=True) | |
| def _gather_trainable_state_dict(self, accelerator: Accelerator, model: torch.nn.Module): | |
| # ZeRO-3 consolidation is a collective operation, so every rank must | |
| # participate even though only the main rank writes the checkpoint. | |
| state_dict = accelerator.get_state_dict(model) | |
| if not accelerator.is_main_process: | |
| return None | |
| state_dict = accelerator.unwrap_model(model).export_trainable_state_dict( | |
| state_dict, | |
| remove_prefix=self.remove_prefix_in_ckpt, | |
| ) | |
| return self.state_dict_converter(state_dict) | |
| def on_step_end( | |
| self, | |
| accelerator: Accelerator, | |
| model: torch.nn.Module, | |
| save_steps=None, | |
| loss=None, | |
| grad_norm=None, | |
| optimizer=None, | |
| epoch=None, | |
| force_step=True, | |
| ): | |
| if not force_step: | |
| return | |
| self.num_steps += 1 | |
| if save_steps is not None and self.num_steps % save_steps == 0: | |
| self.save_model_to_dir(accelerator, model, f"step-{self.num_steps}") | |
| if self.num_steps % 1 == 0: | |
| metrics = {} | |
| if loss is not None: | |
| metrics["train_loss"] = loss.item() if isinstance(loss, torch.Tensor) else loss | |
| if grad_norm is not None: | |
| metrics["grad_norm"] = grad_norm.item() if isinstance(grad_norm, torch.Tensor) else grad_norm | |
| if optimizer is not None: | |
| metrics["learning_rate"] = optimizer.param_groups[0]["lr"] | |
| if epoch is not None: | |
| metrics["epoch"] = epoch | |
| if len(metrics) > 0: | |
| accelerator.log(metrics, step=self.num_steps) | |
| metric_str = " ".join(f"{k}={v}" for k, v in metrics.items()) | |
| accelerator.print(f"[step {self.num_steps}] {metric_str}") | |
| def on_epoch_end(self, accelerator: Accelerator, model: torch.nn.Module, epoch_id): | |
| accelerator.wait_for_everyone() | |
| checkpoint_dir = os.path.join(self.output_path, f"epoch-{epoch_id}") | |
| os.makedirs(checkpoint_dir, exist_ok=True) | |
| state_dict = self._gather_trainable_state_dict(accelerator, model) | |
| if accelerator.is_main_process: | |
| self._save_config(checkpoint_dir) | |
| path = os.path.join(checkpoint_dir, f"epoch-{epoch_id}.safetensors") | |
| accelerator.save(state_dict, path, safe_serialization=True) | |
| def on_training_end(self, accelerator: Accelerator, model: torch.nn.Module, save_steps=None): | |
| if save_steps is not None and self.num_steps % save_steps != 0: | |
| self.save_model_to_dir(accelerator, model, f"step-{self.num_steps}") | |
| def save_model(self, accelerator: Accelerator, model: torch.nn.Module, file_name): | |
| accelerator.wait_for_everyone() | |
| state_dict = self._gather_trainable_state_dict(accelerator, model) | |
| if accelerator.is_main_process: | |
| os.makedirs(self.output_path, exist_ok=True) | |
| self._save_config(self.output_path) | |
| path = os.path.join(self.output_path, file_name) | |
| accelerator.save(state_dict, path, safe_serialization=True) | |
| def save_model_to_dir(self, accelerator: Accelerator, model: torch.nn.Module, checkpoint_name): | |
| accelerator.wait_for_everyone() | |
| state_dict = self._gather_trainable_state_dict(accelerator, model) | |
| if accelerator.is_main_process: | |
| checkpoint_dir = os.path.join(self.output_path, checkpoint_name) | |
| os.makedirs(checkpoint_dir, exist_ok=True) | |
| self._save_config(checkpoint_dir) | |
| path = os.path.join(checkpoint_dir, f"{checkpoint_name}.safetensors") | |
| accelerator.save(state_dict, path, safe_serialization=True) | |