Download code/train_edm2.py from rcell233/magvits_data: direct link, hf CLI and curl.
- Browser
- Download file 9.3 kB
-
https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/train_edm2.py
- Command line
-
hf download hf://datasets/rcell233/magvits_data/code/train_edm2.py
-
curl -L -o train_edm2.py https://huggingface.co/datasets/rcell233/magvits_data/resolve/main/code/train_edm2.py
9.3 kB
| # Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |
| # | |
| # This work is licensed under a Creative Commons | |
| # Attribution-NonCommercial-ShareAlike 4.0 International License. | |
| # You should have received a copy of the license along with this | |
| # work. If not, see http://creativecommons.org/licenses/by-nc-sa/4.0/ | |
| """Train diffusion models according to the EDM2 recipe from the paper | |
| "Analyzing and Improving the Training Dynamics of Diffusion Models".""" | |
| import os | |
| import re | |
| import json | |
| import click | |
| import torch | |
| import dnnlib | |
| from torch_utils import distributed as dist | |
| import training.training_loop | |
| #---------------------------------------------------------------------------- | |
| # Configuration presets. | |
| config_presets = { | |
| 'edm2-img512-xs': dnnlib.EasyDict(duration=2048<<20, batch=2048, channels=128, lr=0.0120, decay=70000, dropout=0.00, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img512-s': dnnlib.EasyDict(duration=2048<<20, batch=2048, channels=192, lr=0.0100, decay=70000, dropout=0.00, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img512-m': dnnlib.EasyDict(duration=2048<<20, batch=2048, channels=256, lr=0.0090, decay=70000, dropout=0.10, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img512-l': dnnlib.EasyDict(duration=1792<<20, batch=2048, channels=320, lr=0.0080, decay=70000, dropout=0.10, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img512-xl': dnnlib.EasyDict(duration=1280<<20, batch=2048, channels=384, lr=0.0070, decay=70000, dropout=0.10, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img512-xxl': dnnlib.EasyDict(duration=896<<20, batch=2048, channels=448, lr=0.0065, decay=70000, dropout=0.10, P_mean=-0.4, P_std=1.0), | |
| 'edm2-img64-s': dnnlib.EasyDict(duration=1024<<20, batch=2048, channels=192, lr=0.0100, decay=35000, dropout=0.00, P_mean=-0.8, P_std=1.6), | |
| 'edm2-img64-m': dnnlib.EasyDict(duration=2048<<20, batch=2048, channels=256, lr=0.0090, decay=35000, dropout=0.10, P_mean=-0.8, P_std=1.6), | |
| 'edm2-img64-l': dnnlib.EasyDict(duration=1024<<20, batch=2048, channels=320, lr=0.0080, decay=35000, dropout=0.10, P_mean=-0.8, P_std=1.6), | |
| 'edm2-img64-xl': dnnlib.EasyDict(duration=640<<20, batch=2048, channels=384, lr=0.0070, decay=35000, dropout=0.10, P_mean=-0.8, P_std=1.6), | |
| } | |
| #---------------------------------------------------------------------------- | |
| # Setup arguments for training.training_loop.training_loop(). | |
| def setup_training_config(preset='edm2-img512-s', **opts): | |
| opts = dnnlib.EasyDict(opts) | |
| c = dnnlib.EasyDict() | |
| # Preset. | |
| if preset not in config_presets: | |
| raise click.ClickException(f'Invalid configuration preset "{preset}"') | |
| for key, value in config_presets[preset].items(): | |
| if opts.get(key, None) is None: | |
| opts[key] = value | |
| # Hyperparameters. | |
| c.update(total_nimg=opts.duration, batch_size=opts.batch) | |
| c.loss_kwargs = dnnlib.EasyDict(class_name='training.training_loop.EDM2Loss', P_mean=opts.P_mean, P_std=opts.P_std) | |
| c.lr_kwargs = dnnlib.EasyDict(func_name='training.training_loop.learning_rate_schedule', ref_lr=opts.lr, ref_batches=opts.decay) | |
| # Performance-related options. | |
| c.batch_gpu = opts.get('batch_gpu', 0) or None | |
| c.use_fp16 = opts.get('fp16', True) | |
| c.loss_scaling = opts.get('ls', 1) | |
| c.cudnn_benchmark = opts.get('bench', True) | |
| # I/O-related options. | |
| c.status_nimg = opts.get('status', 0) or None | |
| c.snapshot_nimg = opts.get('snapshot', 0) or None | |
| c.checkpoint_nimg = opts.get('checkpoint', 0) or None | |
| c.seed = opts.get('seed', 0) | |
| return c | |
| #---------------------------------------------------------------------------- | |
| # Print training configuration. | |
| def print_training_config(run_dir, c): | |
| dist.print0() | |
| dist.print0('Training config:') | |
| dist.print0(json.dumps(c, indent=2)) | |
| dist.print0() | |
| dist.print0(f'Output directory: {run_dir}') | |
| dist.print0(f'Number of GPUs: {dist.get_world_size()}') | |
| dist.print0(f'Batch size: {c.batch_size}') | |
| dist.print0(f'Mixed-precision: {c.use_fp16}') | |
| dist.print0() | |
| #---------------------------------------------------------------------------- | |
| # Launch training. | |
| def launch_training(run_dir, c): | |
| if dist.get_rank() == 0 and not os.path.isdir(run_dir): | |
| dist.print0('Creating output directory...') | |
| os.makedirs(run_dir) | |
| with open(os.path.join(run_dir, 'training_options.json'), 'wt') as f: | |
| json.dump(c, f, indent=2) | |
| torch.distributed.barrier() | |
| dnnlib.util.Logger(file_name=os.path.join(run_dir, 'log.txt'), file_mode='a', should_flush=True) | |
| training.training_loop.training_loop(run_dir=run_dir, **c) | |
| #---------------------------------------------------------------------------- | |
| # Parse an integer with optional power-of-two suffix: | |
| # 'Ki' = kibi = 2^10 | |
| # 'Mi' = mebi = 2^20 | |
| # 'Gi' = gibi = 2^30 | |
| def parse_nimg(s): | |
| if isinstance(s, int): | |
| return s | |
| if s.endswith('Ki'): | |
| return int(s[:-2]) << 10 | |
| if s.endswith('Mi'): | |
| return int(s[:-2]) << 20 | |
| if s.endswith('Gi'): | |
| return int(s[:-2]) << 30 | |
| return int(s) | |
| #---------------------------------------------------------------------------- | |
| # Command line interface. | |
| # Main options. | |
| # Hyperparameters. | |
| # Performance-related options. | |
| # I/O-related options. | |
| def cmdline(outdir, dry_run, **opts): | |
| """Train diffusion models according to the EDM2 recipe from the paper | |
| "Analyzing and Improving the Training Dynamics of Diffusion Models". | |
| Examples: | |
| \b | |
| # Train XS-sized model for ImageNet-512 using 8 GPUs | |
| torchrun --standalone --nproc_per_node=8 train_edm2.py \\ | |
| --outdir=training-runs/00000-edm2-img512-xs \\ | |
| --data=datasets/img512-sd.zip \\ | |
| --preset=edm2-img512-xs \\ | |
| --batch-gpu=32 | |
| \b | |
| # To resume training, run the same command again. | |
| """ | |
| torch.multiprocessing.set_start_method('spawn') | |
| dist.init() | |
| dist.print0('Setting up training config...') | |
| c = setup_training_config(**opts) | |
| print_training_config(run_dir=outdir, c=c) | |
| if dry_run: | |
| dist.print0('Dry run; exiting.') | |
| else: | |
| launch_training(run_dir=outdir, c=c) | |
| #---------------------------------------------------------------------------- | |
| if __name__ == "__main__": | |
| cmdline() | |
| #---------------------------------------------------------------------------- | |