text
stringlengths
1
1.02k
class_index
int64
0
1.38k
source
stringclasses
431 values
self.alphas = 1.0 - self.betas self.alphas_cumprod = torch.cumprod(self.alphas, dim=0) # set all values self.set_timesteps(num_train_timesteps, None, num_train_timesteps) self.use_karras_sigmas = use_karras_sigmas self._step_index = None self._begin_index = None ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) pos = 1 if le...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
@property def begin_index(self): """ The index for the first timestep. It should be set from pipeline with `set_begin_index` method. """ return self._begin_index # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index def...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
Args: sample (`torch.Tensor`): The input sample. timestep (`int`, *optional*): The current timestep in the diffusion chain. Returns: `torch.Tensor`: A scaled input sample. """ if self.step_index is None: ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
Args: num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
raise ValueError("Must pass exactly one of `num_inference_steps` or `custom_timesteps`.") if num_inference_steps is not None and timesteps is not None: raise ValueError("Can only pass one of `num_inference_steps` or `custom_timesteps`.") if timesteps is not None and self.config.use_karras_si...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
num_inference_steps = num_inference_steps or len(timesteps) self.num_inference_steps = num_inference_steps num_train_timesteps = num_train_timesteps or self.config.num_train_timesteps
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
if timesteps is not None: timesteps = np.array(timesteps, dtype=np.float32) else: # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": timesteps = np.linspa...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 timesteps = (np.arange(num_train_timesteps, 0, -step_ratio)).round().copy().astype(np.float32) timesteps -= 1 else: raise...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5) log_sigmas = np.log(sigmas) sigmas = np.interp(timesteps, np.arange(0, len(sigmas)), sigmas) if self.config.use_karras_sigmas: sigmas = self._convert_to_karras(in_sigmas=sigmas, num_inference_steps=self.num_...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
sigmas = np.concatenate([sigmas, [0.0]]).astype(np.float32) sigmas = torch.from_numpy(sigmas).to(device=device) self.sigmas = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2), sigmas[-1:]]) timesteps = torch.from_numpy(timesteps) timesteps = torch.cat([timesteps[:1], timesteps[1...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# get sigmas range low_idx = np.cumsum((dists >= 0), axis=0).argmax(axis=0).clip(max=log_sigmas.shape[0] - 2) high_idx = low_idx + 1 low = log_sigmas[low_idx] high = log_sigmas[high_idx] # interpolate sigmas w = (low - log_sigma) / (low - high) w = np.clip(w, 0,...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# Hack to make sure that other schedulers which copy this function don't break # TODO: Add this logic to the other schedulers if hasattr(self.config, "sigma_min"): sigma_min = self.config.sigma_min else: sigma_min = None if hasattr(self.config, "sigma_max"): ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._convert_to_exponential def _convert_to_exponential(self, in_sigmas: torch.Tensor, num_inference_steps: int) -> torch.Tensor: """Constructs an exponential noise schedule.""" # Hack to make sure that other schedulers...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._convert_to_beta def _convert_to_beta( self, in_sigmas: torch.Tensor, num_inference_steps: int, alpha: float = 0.6, beta: float = 0.6 ) -> torch.Tensor: """From "Beta Sampling is All You Need" [arXiv:2407.12173] ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
sigmas = np.array( [ sigma_min + (ppf * (sigma_max - sigma_min)) for ppf in [ scipy.stats.beta.ppf(timestep, alpha, beta) for timestep in 1 - np.linspace(0, 1, num_inference_steps) ] ] ) retur...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
def step( self, model_output: Union[torch.Tensor, np.ndarray], timestep: Union[float, torch.Tensor], sample: Union[torch.Tensor, np.ndarray], return_dict: bool = True, ) -> Union[HeunDiscreteSchedulerOutput, Tuple]: """ Predict the sample from the previous tim...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
Returns: [`~schedulers.scheduling_heun_discrete.HeunDiscreteSchedulerOutput`] or `tuple`: If return_dict is `True`, [`~schedulers.scheduling_heun_discrete.HeunDiscreteSchedulerOutput`] is returned, otherwise a tuple is returned where the first element is the sample tensor. ...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API gamma = 0 sigma_hat = sigma * (gamma + 1) # Note: sigma_hat == sigma for now
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": sigma_input = sigma_hat if self.state_in_first_order else sigma_next pred_original_sample = sample - sigma_input * model_output elif self.config.prediction_...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
if self.config.clip_sample: pred_original_sample = pred_original_sample.clamp( -self.config.clip_sample_range, self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order derivative = (sample - pre...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# free dt and derivative # Note, this puts the scheduler in "first order mode" self.prev_derivative = None self.dt = None self.sample = None prev_sample = sample + derivative * dt # upon completion increase step index by one self._step_index += 1...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.add_noise def add_noise( self, original_samples: torch.Tensor, noise: torch.Tensor, timesteps: torch.Tensor, ) -> torch.Tensor: # Make sure sigmas and timesteps have the same device and dt...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
# self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index if self.begin_index is None: step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps] elif self.step_index is not None: # add_noise is called a...
1,292
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_heun_discrete.py
class KDPM2DiscreteSchedulerOutput(BaseOutput): """ Output class for the scheduler's `step` function output. Args: prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used...
1,293
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
class KDPM2DiscreteScheduler(SchedulerMixin, ConfigMixin): """ KDPM2DiscreteScheduler is inspired by the DPMSolver2 and Algorithm 2 from the [Elucidating the Design Space of Diffusion-Based Generative Models](https://huggingface.co/papers/2206.00364) paper. This model inherits from [`SchedulerMixin`] a...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
Args: num_train_timesteps (`int`, defaults to 1000): The number of diffusion steps to train the model. beta_start (`float`, defaults to 0.00085): The starting `beta` value of inference. beta_end (`float`, defaults to 0.012): The final `beta` value. bet...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
use_exponential_sigmas (`bool`, *optional*, defaults to `False`): Whether to use exponential sigmas for step sizes in the noise schedule during the sampling process. use_beta_sigmas (`bool`, *optional*, defaults to `False`): Whether to use beta sigmas for step sizes in the noise schedule...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information. steps_offset (`int`, defaults to 0): An offset added to the inference steps, as required by some model families. """
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
_compatibles = [e.name for e in KarrasDiffusionSchedulers] order = 2
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
@register_to_config def __init__( self, num_train_timesteps: int = 1000, beta_start: float = 0.00085, # sensible defaults beta_end: float = 0.012, beta_schedule: str = "linear", trained_betas: Optional[Union[np.ndarray, List[float]]] = None, use_karras_sigmas...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
) if trained_betas is not None: self.betas = torch.tensor(trained_betas, dtype=torch.float32) elif beta_schedule == "linear": self.betas = torch.linspace(beta_start, beta_end, num_train_timesteps, dtype=torch.float32) elif beta_schedule == "scaled_linear": # t...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
self.alphas = 1.0 - self.betas self.alphas_cumprod = torch.cumprod(self.alphas, dim=0) # set all values self.set_timesteps(num_train_timesteps, None, num_train_timesteps) self._step_index = None self._begin_index = None self.sigmas = self.sigmas.to("cpu") # to avoid t...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index def set_begin_index(self, begin_index: int = 0): """ Sets the begin index for the scheduler. This function should be run from pipeline before the inference. Args: begin_...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
Returns: `torch.Tensor`: A scaled input sample. """ if self.step_index is None: self._init_step_index(timestep) if self.state_in_first_order: sigma = self.sigmas[self.step_index] else: sigma = self.sigmas_interpol[self.step...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
Args: num_inference_steps (`int`): The number of diffusion steps used when generating samples with a pre-trained model. device (`str` or `torch.device`, *optional*): The device to which the timesteps should be moved to. If `None`, the timesteps are not moved. ...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": timesteps = np.linspace(0, num_train_timesteps - 1, num_inference_steps, dtype=np.float32)[::-1].copy() elif self.config.timestep_spaci...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
timesteps = (np.arange(num_train_timesteps, 0, -step_ratio)).round().copy().astype(np.float32) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." )
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5) log_sigmas = np.log(sigmas) sigmas = np.interp(timesteps, np.arange(0, len(sigmas)), sigmas) if self.config.use_karras_sigmas: sigmas = self._convert_to_karras(in_sigmas=sigmas, num_inference_steps=num_infer...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
self.log_sigmas = torch.from_numpy(log_sigmas).to(device=device) sigmas = np.concatenate([sigmas, [0.0]]).astype(np.float32) sigmas = torch.from_numpy(sigmas).to(device=device) # interpolate sigmas sigmas_interpol = sigmas.log().lerp(sigmas.roll(1).log(), 0.5).exp() self.sigmas...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# interpolate timesteps sigmas_interpol = sigmas_interpol.cpu() log_sigmas = self.log_sigmas.cpu() timesteps_interpol = np.array( [self._sigma_to_t(sigma_interpol, log_sigmas) for sigma_interpol in sigmas_interpol] ) timesteps_interpol = torch.from_numpy(timesteps_int...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.index_for_timestep def index_for_timestep(self, timestep, schedule_timesteps=None): if schedule_timesteps is None: schedule_timesteps = self.timesteps indices = (schedule_timesteps == timestep).nonzero()...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler._init_step_index def _init_step_index(self, timestep): if self.begin_index is None: if isinstance(timestep, torch.Tensor): timestep = timestep.to(self.timesteps.device) self._step_inde...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# interpolate sigmas w = (low - log_sigma) / (low - high) w = np.clip(w, 0, 1) # transform interpolation to time range t = (1 - w) * low_idx + w * high_idx t = t.reshape(sigma.shape) return t # Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscrete...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
sigma_min = sigma_min if sigma_min is not None else in_sigmas[-1].item() sigma_max = sigma_max if sigma_max is not None else in_sigmas[0].item() rho = 7.0 # 7.0 is the value used in the paper ramp = np.linspace(0, 1, num_inference_steps) min_inv_rho = sigma_min ** (1 / rho) max...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
if hasattr(self.config, "sigma_max"): sigma_max = self.config.sigma_max else: sigma_max = None sigma_min = sigma_min if sigma_min is not None else in_sigmas[-1].item() sigma_max = sigma_max if sigma_max is not None else in_sigmas[0].item() sigmas = np.exp(np.lin...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# Hack to make sure that other schedulers which copy this function don't break # TODO: Add this logic to the other schedulers if hasattr(self.config, "sigma_min"): sigma_min = self.config.sigma_min else: sigma_min = None if hasattr(self.config, "sigma_max"): ...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
def step( self, model_output: Union[torch.Tensor, np.ndarray], timestep: Union[float, torch.Tensor], sample: Union[torch.Tensor, np.ndarray], return_dict: bool = True, ) -> Union[KDPM2DiscreteSchedulerOutput, Tuple]: """ Predict the sample from the previous ti...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
Returns: [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] or `tuple`: If return_dict is `True`, [`~schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteSchedulerOutput`] is returned, otherwise a tuple is returned where the first element is the sample te...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API gamma = 0 sigma_hat = sigma * (gamma + 1) # Note: sigma_hat == sigma for now
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": sigma_input = sigma_hat if self.state_in_first_order else sigma_interpol pred_original_sample = sample - sigma_input * model_output elif self.config.predict...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order derivative = (sample - pred_original_sample) / sigma_hat # 3. delta timestep dt = sigma_interpol - sigma_hat # store for 2nd order step self.sample = sample else...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# Copied from diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler.add_noise def add_noise( self, original_samples: torch.Tensor, noise: torch.Tensor, timesteps: torch.Tensor, ) -> torch.Tensor: # Make sure sigmas and timesteps have the same device and dt...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
# self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index if self.begin_index is None: step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timesteps] elif self.step_index is not None: # add_noise is called a...
1,294
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_k_dpm_2_discrete.py
class PNDMSchedulerState: common: CommonSchedulerState final_alpha_cumprod: jnp.ndarray # setable values init_noise_sigma: jnp.ndarray timesteps: jnp.ndarray num_inference_steps: Optional[int] = None prk_timesteps: Optional[jnp.ndarray] = None plms_timesteps: Optional[jnp.ndarray] = Non...
1,295
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
class FlaxPNDMSchedulerOutput(FlaxSchedulerOutput): state: PNDMSchedulerState
1,296
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
class FlaxPNDMScheduler(FlaxSchedulerMixin, ConfigMixin): """ Pseudo numerical methods for diffusion models (PNDM) proposes using more advanced ODE integration techniques, namely Runge-Kutta method and a linear multi-step method. [`~ConfigMixin`] takes care of storing all config attributes that are pas...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
Args: num_train_timesteps (`int`): number of diffusion steps used to train the model. beta_start (`float`): the starting `beta` value of inference. beta_end (`float`): the final `beta` value. beta_schedule (`str`): the beta schedule, a mapping from a beta range to a sequence ...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
step there is no previous alpha. When this option is `True` the previous alpha product is fixed to `1`, otherwise it uses the value of alpha at step 0. steps_offset (`int`, default `0`): An offset added to the inference steps, as required by some model families. prediction_type (...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
_compatibles = [e.name for e in FlaxKarrasDiffusionSchedulers] dtype: jnp.dtype pndm_order: int @property def has_state(self): return True @register_to_config def __init__( self, num_train_timesteps: int = 1000, beta_start: float = 0.0001, beta_end: flo...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
def create_state(self, common: Optional[CommonSchedulerState] = None) -> PNDMSchedulerState: if common is None: common = CommonSchedulerState.create(self) # At every step in ddim, we are looking into the previous alphas_cumprod # For the final step, there is no previous alphas_cumpr...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
return PNDMSchedulerState.create( common=common, final_alpha_cumprod=final_alpha_cumprod, init_noise_sigma=init_noise_sigma, timesteps=timesteps, ) def set_timesteps(self, state: PNDMSchedulerState, num_inference_steps: int, shape: Tuple) -> PNDMSchedulerStat...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
step_ratio = self.config.num_train_timesteps // num_inference_steps # creates integer timesteps by multiplying by ratio # rounding to avoid issues when num_inference_step is power of 3 _timesteps = (jnp.arange(0, num_inference_steps) * step_ratio).round() + self.config.steps_offset if s...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
else: prk_timesteps = _timesteps[-self.pndm_order :].repeat(2) + jnp.tile( jnp.array([0, self.config.num_train_timesteps // num_inference_steps // 2], dtype=jnp.int32), self.pndm_order, ) prk_timesteps = (prk_timesteps[:-1].repeat(2)[1:-1])[::-1] ...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
return state.replace( timesteps=timesteps, num_inference_steps=num_inference_steps, prk_timesteps=prk_timesteps, plms_timesteps=plms_timesteps, cur_model_output=cur_model_output, counter=counter, cur_sample=cur_sample, ets=e...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
def step( self, state: PNDMSchedulerState, model_output: jnp.ndarray, timestep: int, sample: jnp.ndarray, return_dict: bool = True, ) -> Union[FlaxPNDMSchedulerOutput, Tuple]: """ Predict the sample at the previous timestep by reversing the SDE. Core f...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
Args: state (`PNDMSchedulerState`): the `FlaxPNDMScheduler` state data class instance. model_output (`jnp.ndarray`): direct output from learned diffusion model. timestep (`int`): current discrete timestep in the diffusion chain. sample (`jnp.ndarray`): cur...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
if self.config.skip_prk_steps: prev_sample, state = self.step_plms(state, model_output, timestep, sample) else: prk_prev_sample, prk_state = self.step_prk(state, model_output, timestep, sample) plms_prev_sample, plms_state = self.step_plms(state, model_output, timestep, sampl...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
return FlaxPNDMSchedulerOutput(prev_sample=prev_sample, state=state) def step_prk( self, state: PNDMSchedulerState, model_output: jnp.ndarray, timestep: int, sample: jnp.ndarray, ) -> Union[FlaxPNDMSchedulerOutput, Tuple]: """ Step function propagating th...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
Returns: [`FlaxPNDMSchedulerOutput`] or `tuple`: [`FlaxPNDMSchedulerOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is the sample tensor. """ if state.num_inference_steps is None: raise ValueError( "N...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
state = state.replace( cur_model_output=jax.lax.select_n( state.counter % 4, state.cur_model_output + 1 / 6 * model_output, # remainder 0 state.cur_model_output + 1 / 3 * model_output, # remainder 1 state.cur_model_output + 1 / 3 * model_outp...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
cur_sample = state.cur_sample prev_sample = self._get_prev_sample(state, cur_sample, timestep, prev_timestep, model_output) state = state.replace(counter=state.counter + 1) return (prev_sample, state) def step_plms( self, state: PNDMSchedulerState, model_output: jnp...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
Args: state (`PNDMSchedulerState`): the `FlaxPNDMScheduler` state data class instance. model_output (`jnp.ndarray`): direct output from learned diffusion model. timestep (`int`): current discrete timestep in the diffusion chain. sample (`jnp.ndarray`): cur...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
# NOTE: There is no way to check in the jitted runtime if the prk mode was ran before prev_timestep = timestep - self.config.num_train_timesteps // state.num_inference_steps prev_timestep = jnp.where(prev_timestep > 0, prev_timestep, 0) # Reference: # if state.counter != 1: # ...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
# Reference: # if len(state.ets) == 1 and state.counter == 0: # model_output = model_output # state.cur_sample = sample # elif len(state.ets) == 1 and state.counter == 1: # model_output = (model_output + state.ets[-1]) / 2 # sample = state.cur_sample ...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
state = state.replace( ets=jax.lax.select( state.counter != 1, state.ets.at[0:3].set(state.ets[1:4]).at[3].set(model_output), # counter != 1 state.ets, # counter 1 ), cur_sample=jax.lax.select( state.counter != 1, ...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
sample = state.cur_sample model_output = state.cur_model_output prev_sample = self._get_prev_sample(state, sample, timestep, prev_timestep, model_output) state = state.replace(counter=state.counter + 1) return (prev_sample, state) def _get_prev_sample(self, state: PNDMSchedulerStat...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
# Notation (<variable name> -> <name in paper> # alpha_prod_t -> α_t # alpha_prod_t_prev -> α_(t−δ) # beta_prod_t -> (1 - α_t) # beta_prod_t_prev -> (1 - α_(t−δ)) # sample -> x_t # model_output -> e_θ(x_t, t) # prev_sample -> x_(t−δ) alpha_prod_t = state.c...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
# corresponds to (α_(t−δ) - α_t) divided by # denominator of x_t in formula (9) and plus 1 # Note: (α_(t−δ) - α_t) / (sqrt(α_t) * (sqrt(α_(t−δ)) + sqr(α_t))) = # sqrt(α_(t−δ)) / sqrt(α_t)) sample_coeff = (alpha_prod_t_prev / alpha_prod_t) ** (0.5) # corresponds to denominator of...
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
def __len__(self): return self.config.num_train_timesteps
1,297
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_pndm_flax.py
class DDIMSchedulerOutput(BaseOutput): """ Output class for the scheduler's `step` function output. Args: prev_sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)` for images): Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next ...
1,298
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
class CogVideoXDPMScheduler(SchedulerMixin, ConfigMixin): """ `DDIMScheduler` extends the denoising procedure introduced in denoising diffusion probabilistic models (DDPMs) with non-Markovian guidance. This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation f...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
Args: num_train_timesteps (`int`, defaults to 1000): The number of diffusion steps to train the model. beta_start (`float`, defaults to 0.0001): The starting `beta` value of inference. beta_end (`float`, defaults to 0.02): The final `beta` value. beta_...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
Each diffusion step uses the alphas product value at that step and at the previous one. For the final step there is no previous alpha. When this option is `True` the previous alpha product is fixed to `1`, otherwise it uses the alpha value at step 0. steps_offset (`int`, defaults to 0): ...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
dynamic_thresholding_ratio (`float`, defaults to 0.995): The ratio for the dynamic thresholding method. Valid only when `thresholding=True`. sample_max_value (`float`, defaults to 1.0): The threshold value for dynamic thresholding. Valid only when `thresholding=True`. timestep_sp...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
[`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506). """
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
_compatibles = [e.name for e in KarrasDiffusionSchedulers] order = 1
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
@register_to_config def __init__( self, num_train_timesteps: int = 1000, beta_start: float = 0.00085, beta_end: float = 0.0120, beta_schedule: str = "scaled_linear", trained_betas: Optional[Union[np.ndarray, List[float]]] = None, clip_sample: bool = True, ...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
# this schedule is very specific to the latent diffusion model. self.betas = torch.linspace(beta_start**0.5, beta_end**0.5, num_train_timesteps, dtype=torch.float64) ** 2 elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule self.betas = betas_for_alpha_bar(num_t...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
self.alphas = 1.0 - self.betas self.alphas_cumprod = torch.cumprod(self.alphas, dim=0) # Modify: SNR shift following SD3 self.alphas_cumprod = self.alphas_cumprod / (snr_shift_scale + (1 - snr_shift_scale) * self.alphas_cumprod) # Rescale for zero SNR if rescale_betas_zero_snr:...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
# setable values self.num_inference_steps = None self.timesteps = torch.from_numpy(np.arange(0, num_train_timesteps)[::-1].copy().astype(np.int64)) def _get_variance(self, timestep, prev_timestep): alpha_prod_t = self.alphas_cumprod[timestep] alpha_prod_t_prev = self.alphas_cumprod[...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
Args: sample (`torch.Tensor`): The input sample. timestep (`int`, *optional*): The current timestep in the diffusion chain. Returns: `torch.Tensor`: A scaled input sample. """ return sample def set_timestep...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
if num_inference_steps > self.config.num_train_timesteps: raise ValueError( f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:" f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle"...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": timesteps = ( np.linspace(0, self.config.num_train_timesteps - 1, num_inference_steps) .round()[::-1] ...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
# creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 timesteps = np.round(np.arange(self.config.num_train_timesteps, 0, -step_ratio)).astype(np.int64) timesteps -= 1 else: raise ValueError( ...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
self.timesteps = torch.from_numpy(timesteps).to(device) def get_variables(self, alpha_prod_t, alpha_prod_t_prev, alpha_prod_t_back=None): lamb = ((alpha_prod_t / (1 - alpha_prod_t)) ** 0.5).log() lamb_next = ((alpha_prod_t_prev / (1 - alpha_prod_t_prev)) ** 0.5).log() h = lamb_next - lamb ...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
if alpha_prod_t_back is not None: mult3 = 1 + 1 / (2 * r) mult4 = 1 / (2 * r) return mult1, mult2, mult3, mult4 else: return mult1, mult2 def step( self, model_output: torch.Tensor, old_pred_original_sample: torch.Tensor, times...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
Args: model_output (`torch.Tensor`): The direct output from learned diffusion model. timestep (`float`): The current discrete timestep in the diffusion chain. sample (`torch.Tensor`): A current instance of a sample created by the diffus...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
variance_noise (`torch.Tensor`): Alternative to generating noise with `generator` by directly providing the noise for the variance itself. Useful for methods such as [`CycleDiffusion`]. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to re...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py
Returns: [`~schedulers.scheduling_ddim.DDIMSchedulerOutput`] or `tuple`: If return_dict is `True`, [`~schedulers.scheduling_ddim.DDIMSchedulerOutput`] is returned, otherwise a tuple is returned where the first element is the sample tensor. """ if self.num_inf...
1,299
/Users/nielsrogge/Documents/python_projecten/diffusers/src/diffusers/schedulers/scheduling_dpm_cogvideox.py