| 
									
										
										
										
											2022-12-10 09:14:30 +03:00
										 |  |  | import torch | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  | from packaging import version | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from modules import devices | 
					
						
							|  |  |  | from modules.sd_hijack_utils import CondFunc | 
					
						
							| 
									
										
										
										
											2022-12-10 09:14:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TorchHijackForUnet: | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     This is torch, but with cat that resizes tensors to appropriate dimensions if they do not match; | 
					
						
							| 
									
										
										
										
											2022-12-14 21:01:32 -05:00
										 |  |  |     this makes it possible to create pictures with dimensions that are multiples of 8 rather than 64 | 
					
						
							| 
									
										
										
										
											2022-12-10 09:14:30 +03:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getattr__(self, item): | 
					
						
							|  |  |  |         if item == 'cat': | 
					
						
							|  |  |  |             return self.cat | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if hasattr(torch, item): | 
					
						
							|  |  |  |             return getattr(torch, item) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 22:17:58 +03:00
										 |  |  |         raise AttributeError(f"'{type(self).__name__}' object has no attribute '{item}'") | 
					
						
							| 
									
										
										
										
											2022-12-10 09:14:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def cat(self, tensors, *args, **kwargs): | 
					
						
							|  |  |  |         if len(tensors) == 2: | 
					
						
							|  |  |  |             a, b = tensors | 
					
						
							|  |  |  |             if a.shape[-2:] != b.shape[-2:]: | 
					
						
							|  |  |  |                 a = torch.nn.functional.interpolate(a, b.shape[-2:], mode="nearest") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             tensors = (a, b) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return torch.cat(tensors, *args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | th = TorchHijackForUnet() | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Below are monkey patches to enable upcasting a float16 UNet for float32 sampling | 
					
						
							|  |  |  | def apply_model(orig_func, self, x_noisy, t, cond, **kwargs): | 
					
						
							| 
									
										
										
										
											2023-01-25 20:11:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if isinstance(cond, dict): | 
					
						
							|  |  |  |         for y in cond.keys(): | 
					
						
							| 
									
										
										
										
											2023-07-17 23:39:38 -04:00
										 |  |  |             if isinstance(cond[y], list): | 
					
						
							|  |  |  |                 cond[y] = [x.to(devices.dtype_unet) if isinstance(x, torch.Tensor) else x for x in cond[y]] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 cond[y] = cond[y].to(devices.dtype_unet) if isinstance(cond[y], torch.Tensor) else cond[y] | 
					
						
							| 
									
										
										
										
											2023-01-25 20:11:01 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  |     with devices.autocast(): | 
					
						
							|  |  |  |         return orig_func(self, x_noisy.to(devices.dtype_unet), t.to(devices.dtype_unet), cond, **kwargs).float() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 00:05:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  | class GELUHijack(torch.nn.GELU, torch.nn.Module): | 
					
						
							|  |  |  |     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |         torch.nn.GELU.__init__(self, *args, **kwargs) | 
					
						
							|  |  |  |     def forward(self, x): | 
					
						
							|  |  |  |         if devices.unet_needs_upcast: | 
					
						
							|  |  |  |             return torch.nn.GELU.forward(self.float(), x.float()).to(devices.dtype_unet) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return torch.nn.GELU.forward(self, x) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 00:05:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ddpm_edit_hijack = None | 
					
						
							|  |  |  | def hijack_ddpm_edit(): | 
					
						
							|  |  |  |     global ddpm_edit_hijack | 
					
						
							|  |  |  |     if not ddpm_edit_hijack: | 
					
						
							|  |  |  |         CondFunc('modules.models.diffusion.ddpm_edit.LatentDiffusion.decode_first_stage', first_stage_sub, first_stage_cond) | 
					
						
							|  |  |  |         CondFunc('modules.models.diffusion.ddpm_edit.LatentDiffusion.encode_first_stage', first_stage_sub, first_stage_cond) | 
					
						
							|  |  |  |         ddpm_edit_hijack = CondFunc('modules.models.diffusion.ddpm_edit.LatentDiffusion.apply_model', apply_model, unet_needs_upcast) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  | unet_needs_upcast = lambda *args, **kwargs: devices.unet_needs_upcast | 
					
						
							|  |  |  | CondFunc('ldm.models.diffusion.ddpm.LatentDiffusion.apply_model', apply_model, unet_needs_upcast) | 
					
						
							| 
									
										
										
										
											2023-01-27 10:19:43 -05:00
										 |  |  | CondFunc('ldm.modules.diffusionmodules.openaimodel.timestep_embedding', lambda orig_func, timesteps, *args, **kwargs: orig_func(timesteps, *args, **kwargs).to(torch.float32 if timesteps.dtype == torch.int64 else devices.dtype_unet), unet_needs_upcast) | 
					
						
							| 
									
										
										
										
											2023-03-24 16:25:42 +04:00
										 |  |  | if version.parse(torch.__version__) <= version.parse("1.13.2") or torch.cuda.is_available(): | 
					
						
							| 
									
										
										
										
											2023-01-24 23:51:45 -05:00
										 |  |  |     CondFunc('ldm.modules.diffusionmodules.util.GroupNorm32.forward', lambda orig_func, self, *args, **kwargs: orig_func(self.float(), *args, **kwargs), unet_needs_upcast) | 
					
						
							|  |  |  |     CondFunc('ldm.modules.attention.GEGLU.forward', lambda orig_func, self, x: orig_func(self.float(), x.float()).to(devices.dtype_unet), unet_needs_upcast) | 
					
						
							|  |  |  |     CondFunc('open_clip.transformer.ResidualAttentionBlock.__init__', lambda orig_func, *args, **kwargs: kwargs.update({'act_layer': GELUHijack}) and False or orig_func(*args, **kwargs), lambda _, *args, **kwargs: kwargs.get('act_layer') is None or kwargs['act_layer'] == torch.nn.GELU) | 
					
						
							| 
									
										
										
										
											2023-01-27 10:19:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | first_stage_cond = lambda _, self, *args, **kwargs: devices.unet_needs_upcast and self.model.diffusion_model.dtype == torch.float16 | 
					
						
							|  |  |  | first_stage_sub = lambda orig_func, self, x, **kwargs: orig_func(self, x.to(devices.dtype_vae), **kwargs) | 
					
						
							|  |  |  | CondFunc('ldm.models.diffusion.ddpm.LatentDiffusion.decode_first_stage', first_stage_sub, first_stage_cond) | 
					
						
							|  |  |  | CondFunc('ldm.models.diffusion.ddpm.LatentDiffusion.encode_first_stage', first_stage_sub, first_stage_cond) | 
					
						
							|  |  |  | CondFunc('ldm.models.diffusion.ddpm.LatentDiffusion.get_first_stage_encoding', lambda orig_func, *args, **kwargs: orig_func(*args, **kwargs).float(), first_stage_cond) | 
					
						
							| 
									
										
										
										
											2023-07-17 23:39:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | CondFunc('sgm.modules.diffusionmodules.wrappers.OpenAIWrapper.forward', apply_model, unet_needs_upcast) | 
					
						
							|  |  |  | CondFunc('sgm.modules.diffusionmodules.openaimodel.timestep_embedding', lambda orig_func, timesteps, *args, **kwargs: orig_func(timesteps, *args, **kwargs).to(torch.float32 if timesteps.dtype == torch.int64 else devices.dtype_unet), unet_needs_upcast) |