| 11 | |
| 12 | |
| 13 | class DPMSolverSampler(object): |
| 14 | def __init__(self, model, **kwargs): |
| 15 | super().__init__() |
| 16 | self.model = model |
| 17 | to_torch = lambda x: x.clone().detach().to(torch.float32).to(model.device) |
| 18 | self.register_buffer('alphas_cumprod', to_torch(model.alphas_cumprod)) |
| 19 | |
| 20 | def register_buffer(self, name, attr): |
| 21 | if type(attr) == torch.Tensor: |
| 22 | if attr.device != torch.device("cuda"): |
| 23 | attr = attr.to(torch.device("cuda")) |
| 24 | setattr(self, name, attr) |
| 25 | |
| 26 | @torch.no_grad() |
| 27 | def sample(self, |
| 28 | S, |
| 29 | batch_size, |
| 30 | shape, |
| 31 | conditioning=None, |
| 32 | callback=None, |
| 33 | normals_sequence=None, |
| 34 | img_callback=None, |
| 35 | quantize_x0=False, |
| 36 | eta=0., |
| 37 | mask=None, |
| 38 | x0=None, |
| 39 | temperature=1., |
| 40 | noise_dropout=0., |
| 41 | score_corrector=None, |
| 42 | corrector_kwargs=None, |
| 43 | verbose=True, |
| 44 | x_T=None, |
| 45 | log_every_t=100, |
| 46 | unconditional_guidance_scale=1., |
| 47 | unconditional_conditioning=None, |
| 48 | # this has to come in the same format as the conditioning, # e.g. as encoded tokens, ... |
| 49 | **kwargs |
| 50 | ): |
| 51 | if conditioning is not None: |
| 52 | if isinstance(conditioning, dict): |
| 53 | cbs = conditioning[list(conditioning.keys())[0]].shape[0] |
| 54 | if cbs != batch_size: |
| 55 | print(f"Warning: Got {cbs} conditionings but batch-size is {batch_size}") |
| 56 | else: |
| 57 | if conditioning.shape[0] != batch_size: |
| 58 | print(f"Warning: Got {conditioning.shape[0]} conditionings but batch-size is {batch_size}") |
| 59 | |
| 60 | # sampling |
| 61 | C, H, W = shape |
| 62 | size = (batch_size, C, H, W) |
| 63 | |
| 64 | print(f'Data shape for DPM-Solver sampling is {size}, sampling steps {S}') |
| 65 | |
| 66 | device = self.model.betas.device |
| 67 | if x_T is None: |
| 68 | img = torch.randn(size, device=device) |
| 69 | else: |
| 70 | img = x_T |
nothing calls this directly
no outgoing calls
no test coverage detected