Download smoke.py from matCercola18/DiffusionControl: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/matCercola18/DiffusionControl/resolve/main/smoke.py
- Command line
-
hf download hf://matCercola18/DiffusionControl/smoke.py
-
curl -L -o smoke.py https://huggingface.co/matCercola18/DiffusionControl/resolve/main/smoke.py
1.29 kB
| import numpy as np; np.trapezoid = getattr(np,"trapezoid",np.trapz) | |
| import time, torch, control | |
| from diffusers import DDPMScheduler | |
| import paths | |
| from nonlinear.testing.test import load_checkpoint_cfg, tf_to_coeffs_vector, MAX_ORDER | |
| from utils.models.ConditionalDiffusion_mlp import ReverseDiffusionModel | |
| from utils.samplers import sample_diffusion | |
| dev="cuda" | |
| ckpt_p, ds_p = paths.nonlinear_default() | |
| ck=torch.load(ckpt_p, map_location=dev, weights_only=False) | |
| mcfg=load_checkpoint_cfg(ck["cfg"]); mcfg.device=dev | |
| model=ReverseDiffusionModel(mcfg).to(dev); model.load_state_dict(ck["model"]); model.eval() | |
| ns=DDPMScheduler(num_train_timesteps=mcfg.num_timesteps, beta_schedule="squaredcos_cap_v2") | |
| sc=ck["dataset_scalers"]; G_scale=float(np.asarray(sc["G_scale"]).reshape(-1)[0]) | |
| g=tf_to_coeffs_vector(control.tf([1.0],[1.0,1.0]), max_len=MAX_ORDER+1) | |
| cond=torch.tensor(np.asarray(g)/G_scale,dtype=torch.float32,device=dev).unsqueeze(0).repeat(8,1) | |
| torch.manual_seed(0); torch.cuda.synchronize(); t0=time.perf_counter() | |
| s=sample_diffusion(model,cond,(8,mcfg.output_dim),ns,device=dev,use_cfg=False) | |
| torch.cuda.synchronize() | |
| print("SAMPLED", tuple(s.shape), "in %.2fs for 8 (%.1f ms/ctrl)"%(time.perf_counter()-t0,(time.perf_counter()-t0)/8*1000)) | |
| print("finite:", bool(torch.isfinite(s).all())) | |