Run sampling.
(args)
| 33 | |
| 34 | @hydra.main(config_path="config", config_name="default", version_base=None) |
| 35 | def main(args): |
| 36 | """ |
| 37 | Run sampling. |
| 38 | """ |
| 39 | sample_mode = args.sample_mode |
| 40 | torch.backends.cuda.matmul.allow_tf32 = ( |
| 41 | args.allow_tf32 |
| 42 | ) # True: fast but may lead to some small numerical differences |
| 43 | assert ( |
| 44 | torch.cuda.is_available() |
| 45 | ), "Sampling with DDP requires at least one GPU. sample.py supports CPU-only usage" |
| 46 | torch.set_grad_enabled(False) |
| 47 | |
| 48 | from accelerate.utils import AutocastKwargs |
| 49 | |
| 50 | if True: |
| 51 | kwargs = AutocastKwargs(enabled=False) |
| 52 | # https://github.com/pytorch/pytorch/issues/40497#issuecomment-709846922 |
| 53 | # https://github.com/huggingface/accelerate/issues/2487#issuecomment-1969997224 |
| 54 | else: |
| 55 | kwargs = {} |
| 56 | accelerator = accelerate.Accelerator(kwargs_handlers=[kwargs]) |
| 57 | device = accelerator.device |
| 58 | accelerate.utils.set_seed(args.global_seed, device_specific=True) |
| 59 | rank = accelerator.state.process_index |
| 60 | print( |
| 61 | f"Starting rank={rank}, world_size={accelerator.state.num_processes}, device={device}." |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | assert args.ckpt is not None, "Must specify a checkpoint to sample from" |
| 66 | model, in_channels, input_size = get_model(args, device) |
| 67 | if rank == 0: |
| 68 | print(f"in_channels={in_channels}, input_size={input_size}") |
| 69 | |
| 70 | if True: |
| 71 | state_dict = torch.load(args.ckpt, map_location=lambda storage, loc: storage) |
| 72 | _model_dict = state_dict["ema"] |
| 73 | _model_dict = {k.replace("module.", ""): v for k, v in _model_dict.items()} |
| 74 | model.load_state_dict(_model_dict) |
| 75 | model = model.to(device) |
| 76 | requires_grad(model, False) |
| 77 | if rank == 0: |
| 78 | print(f"Loaded checkpoint from {args.ckpt}") |
| 79 | |
| 80 | |
| 81 | model.eval() # important! |
| 82 | if is_video(args): |
| 83 | _metric = MyMetric(choices=["fvd"], device=device) |
| 84 | print("using videos metrics") |
| 85 | else: |
| 86 | _metric = MyMetric( |
| 87 | choices=["fid",], |
| 88 | device=device, |
| 89 | ) |
| 90 | print("using image metrics") |
| 91 | |
| 92 | local_bs = args.offline_sample_local_bs |
no test coverage detected