|
| 1 | +import sys |
| 2 | +import numpy as np |
| 3 | +from typing import List, Optional, Tuple, Union |
| 4 | +from diffusers import ( |
| 5 | + LMSDiscreteScheduler, |
| 6 | + PNDMScheduler, |
| 7 | + DDIMScheduler, |
| 8 | + DPMSolverMultistepScheduler, |
| 9 | + EulerDiscreteScheduler, |
| 10 | +) |
| 11 | +from diffusers.configuration_utils import register_to_config |
| 12 | +from utils import compile_through_fx, get_shark_model |
| 13 | +from stable_args import args |
| 14 | +import torch |
| 15 | + |
| 16 | +SCHEDULER_BUCKET = "gs://shark_tank/stable_diffusion/schedulers" |
| 17 | + |
| 18 | +model_input = { |
| 19 | + "euler": { |
| 20 | + "latent": torch.randn(1, 4, 64, 64), |
| 21 | + "output": torch.randn(1, 4, 64, 64), |
| 22 | + "sigma": torch.tensor(1).to(torch.float32), |
| 23 | + "dt": torch.tensor(1).to(torch.float32), |
| 24 | + }, |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class SharkEulerDiscreteScheduler(EulerDiscreteScheduler): |
| 29 | + @register_to_config |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + num_train_timesteps: int = 1000, |
| 33 | + beta_start: float = 0.0001, |
| 34 | + beta_end: float = 0.02, |
| 35 | + beta_schedule: str = "linear", |
| 36 | + trained_betas: Optional[Union[np.ndarray, List[float]]] = None, |
| 37 | + prediction_type: str = "epsilon", |
| 38 | + ): |
| 39 | + super().__init__( |
| 40 | + num_train_timesteps, |
| 41 | + beta_start, |
| 42 | + beta_end, |
| 43 | + beta_schedule, |
| 44 | + trained_betas, |
| 45 | + prediction_type, |
| 46 | + ) |
| 47 | + |
| 48 | + def compile(self): |
| 49 | + example_latent = model_input["euler"]["latent"] |
| 50 | + example_output = model_input["euler"]["output"] |
| 51 | + if args.precision == "fp16": |
| 52 | + example_latent = example_latent.half() |
| 53 | + example_output = example_output.half() |
| 54 | + example_sigma = model_input["euler"]["sigma"] |
| 55 | + example_dt = model_input["euler"]["dt"] |
| 56 | + |
| 57 | + class ScalingModel(torch.nn.Module): |
| 58 | + def __init__(self): |
| 59 | + super().__init__() |
| 60 | + |
| 61 | + def forward(self, latent, sigma): |
| 62 | + return latent / ((sigma**2 + 1) ** 0.5) |
| 63 | + |
| 64 | + class SchedulerStepModel(torch.nn.Module): |
| 65 | + def __init__(self): |
| 66 | + super().__init__() |
| 67 | + |
| 68 | + def forward(self, noise_pred, sigma, latent, dt): |
| 69 | + pred_original_sample = latent - sigma * noise_pred |
| 70 | + derivative = (latent - pred_original_sample) / sigma |
| 71 | + return latent + derivative * dt |
| 72 | + |
| 73 | + iree_flags = [] |
| 74 | + if len(args.iree_vulkan_target_triple) > 0: |
| 75 | + iree_flags.append( |
| 76 | + f"-iree-vulkan-target-triple={args.iree_vulkan_target_triple}" |
| 77 | + ) |
| 78 | + # Disable bindings fusion to work with moltenVK. |
| 79 | + if sys.platform == "darwin": |
| 80 | + iree_flags.append("-iree-stream-fuse-binding=false") |
| 81 | + |
| 82 | + if args.import_mlir: |
| 83 | + scaling_model = ScalingModel() |
| 84 | + self.scaling_model = compile_through_fx( |
| 85 | + scaling_model, |
| 86 | + (example_latent, example_sigma), |
| 87 | + model_name="euler_scale_model_input_" + args.precision, |
| 88 | + extra_args=iree_flags, |
| 89 | + ) |
| 90 | + |
| 91 | + step_model = SchedulerStepModel() |
| 92 | + self.step_model = compile_through_fx( |
| 93 | + step_model, |
| 94 | + (example_output, example_sigma, example_latent, example_dt), |
| 95 | + model_name="euler_step_" + args.precision, |
| 96 | + extra_args=iree_flags, |
| 97 | + ) |
| 98 | + else: |
| 99 | + self.scaling_model = get_shark_model( |
| 100 | + SCHEDULER_BUCKET, |
| 101 | + "euler_scale_model_input_" + args.precision, |
| 102 | + iree_flags, |
| 103 | + ) |
| 104 | + self.step_model = get_shark_model( |
| 105 | + SCHEDULER_BUCKET, "euler_step_" + args.precision, iree_flags |
| 106 | + ) |
| 107 | + |
| 108 | + def scale_model_input(self, sample, timestep): |
| 109 | + step_index = (self.timesteps == timestep).nonzero().item() |
| 110 | + sigma = self.sigmas[step_index] |
| 111 | + return self.scaling_model.forward( |
| 112 | + ( |
| 113 | + sample, |
| 114 | + sigma, |
| 115 | + ), |
| 116 | + send_to_host=False, |
| 117 | + ) |
| 118 | + |
| 119 | + def step(self, noise_pred, timestep, latent): |
| 120 | + step_index = (self.timesteps == timestep).nonzero().item() |
| 121 | + sigma = self.sigmas[step_index] |
| 122 | + dt = self.sigmas[step_index + 1] - sigma |
| 123 | + return self.step_model.forward( |
| 124 | + ( |
| 125 | + noise_pred, |
| 126 | + sigma, |
| 127 | + latent, |
| 128 | + dt, |
| 129 | + ), |
| 130 | + send_to_host=False, |
| 131 | + ) |
0 commit comments