Skip to content

Commit f0b7aba

Browse files
committed
timesteps needs to be discrete
1 parent 6485645 commit f0b7aba

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

examples/community/clip_guided_stable_diffusion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def __call__(
274274
# the model input needs to be scaled to match the continuous ODE formulation in K-LMS
275275
latent_model_input = latent_model_input / ((sigma**2 + 1) ** 0.5)
276276

277-
# # predict the noise residual
277+
# predict the noise residual
278278
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample
279279

280280
# perform classifier free guidance

src/diffusers/schedulers/scheduling_lms_discrete.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(
9696

9797
# setable values
9898
self.num_inference_steps = None
99-
self.timesteps = np.arange(0, num_train_timesteps)[::-1].copy()
99+
self.timesteps = np.arange(0, num_train_timesteps)[::-1] # to be consistent has to be smaller than sigmas by 1
100100
self.derivatives = []
101101

102102
def get_lms_coefficient(self, order, t, current_order):
@@ -130,16 +130,17 @@ def set_timesteps(self, num_inference_steps: int):
130130
the number of diffusion steps used when generating samples with a pre-trained model.
131131
"""
132132
self.num_inference_steps = num_inference_steps
133-
self.timesteps = np.linspace(self.config.num_train_timesteps - 1, 0, num_inference_steps, dtype=float)
133+
timesteps = np.linspace(self.config.num_train_timesteps - 1, 0, num_inference_steps, dtype=float)
134134

135-
low_idx = np.floor(self.timesteps).astype(int)
136-
high_idx = np.ceil(self.timesteps).astype(int)
137-
frac = np.mod(self.timesteps, 1.0)
135+
low_idx = np.floor(timesteps).astype(int)
136+
high_idx = np.ceil(timesteps).astype(int)
137+
frac = np.mod(timesteps, 1.0)
138138
sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
139139
sigmas = (1 - frac) * sigmas[low_idx] + frac * sigmas[high_idx]
140140
sigmas = np.concatenate([sigmas, [0.0]]).astype(np.float32)
141141
self.sigmas = torch.from_numpy(sigmas)
142142

143+
self.timesteps = timesteps.astype(int)
143144
self.derivatives = []
144145

145146
def step(

tests/test_scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,5 +876,5 @@ def test_full_loop_no_noise(self):
876876
result_sum = torch.sum(torch.abs(sample))
877877
result_mean = torch.mean(torch.abs(sample))
878878

879-
assert abs(result_sum.item() - 1006.388) < 1e-2
879+
assert abs(result_sum.item() - 1006.370) < 1e-2
880880
assert abs(result_mean.item() - 1.31) < 1e-3

0 commit comments

Comments
 (0)