Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. Args: scheduler (`SchedulerMixin`): The scheduler to get timesteps from.
(
scheduler,
num_inference_steps: Optional[int] = None,
device: Optional[Union[str, torch.device]] = None,
timesteps: Optional[List[int]] = None,
**kwargs,
)
| 151 | |
| 152 | |
| 153 | def retrieve_timesteps( |
| 154 | scheduler, |
| 155 | num_inference_steps: Optional[int] = None, |
| 156 | device: Optional[Union[str, torch.device]] = None, |
| 157 | timesteps: Optional[List[int]] = None, |
| 158 | **kwargs, |
| 159 | ): |
| 160 | """ |
| 161 | Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles |
| 162 | custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`. |
| 163 | |
| 164 | Args: |
| 165 | scheduler (`SchedulerMixin`): |
| 166 | The scheduler to get timesteps from. |
| 167 | num_inference_steps (`int`): |
| 168 | The number of diffusion steps used when generating samples with a pre-trained model. If used, |
| 169 | `timesteps` must be `None`. |
| 170 | device (`str` or `torch.device`, *optional*): |
| 171 | The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. |
| 172 | timesteps (`List[int]`, *optional*): |
| 173 | Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default |
| 174 | timestep spacing strategy of the scheduler is used. If `timesteps` is passed, `num_inference_steps` |
| 175 | must be `None`. |
| 176 | |
| 177 | Returns: |
| 178 | `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the |
| 179 | second element is the number of inference steps. |
| 180 | """ |
| 181 | if timesteps is not None: |
| 182 | accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys()) |
| 183 | if not accepts_timesteps: |
| 184 | raise ValueError( |
| 185 | f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom" |
| 186 | f" timestep schedules. Please check whether you are using the correct scheduler." |
| 187 | ) |
| 188 | scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs) |
| 189 | timesteps = scheduler.timesteps |
| 190 | num_inference_steps = len(timesteps) |
| 191 | else: |
| 192 | scheduler.set_timesteps(num_inference_steps, device=device, **kwargs) |
| 193 | timesteps = scheduler.timesteps |
| 194 | return timesteps, num_inference_steps |
| 195 | |
| 196 | |
| 197 | class GlueGenStableDiffusionPipeline(DiffusionPipeline, StableDiffusionMixin, StableDiffusionLoraLoaderMixin): |