Spaces:
Running
on
A10G
Running
on
A10G
Update src/euler_scheduler.py
Browse filesadded function set_noise_list_device(device)
- src/euler_scheduler.py +589 -583
src/euler_scheduler.py
CHANGED
|
@@ -1,584 +1,590 @@
|
|
| 1 |
-
# Code is based on ReNoise https://github.com/garibida/ReNoise-Inversion
|
| 2 |
-
|
| 3 |
-
from diffusers import EulerAncestralDiscreteScheduler
|
| 4 |
-
from diffusers.utils import BaseOutput
|
| 5 |
-
import torch
|
| 6 |
-
from typing import List, Optional, Tuple, Union
|
| 7 |
-
import numpy as np
|
| 8 |
-
|
| 9 |
-
from src.eunms import Epsilon_Update_Type
|
| 10 |
-
|
| 11 |
-
class EulerAncestralDiscreteSchedulerOutput(BaseOutput):
|
| 12 |
-
"""
|
| 13 |
-
Output class for the scheduler's `step` function output.
|
| 14 |
-
|
| 15 |
-
Args:
|
| 16 |
-
prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 17 |
-
Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
|
| 18 |
-
denoising loop.
|
| 19 |
-
pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 20 |
-
The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
|
| 21 |
-
`pred_original_sample` can be used to preview progress or for guidance.
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
prev_sample: torch.FloatTensor
|
| 25 |
-
pred_original_sample: Optional[torch.FloatTensor] = None
|
| 26 |
-
|
| 27 |
-
class MyEulerAncestralDiscreteScheduler(EulerAncestralDiscreteScheduler):
|
| 28 |
-
def set_noise_list(self, noise_list):
|
| 29 |
-
self.noise_list = noise_list
|
| 30 |
-
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
`torch.FloatTensor
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
self.
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
prev_sample =
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
self.
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
self.
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
self.
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
)
|
| 333 |
-
|
| 334 |
-
self.
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
#
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
#
|
| 373 |
-
|
| 374 |
-
prev_sample =
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
self.
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
#
|
| 425 |
-
#
|
| 426 |
-
#
|
| 427 |
-
#
|
| 428 |
-
#
|
| 429 |
-
#
|
| 430 |
-
#
|
| 431 |
-
#
|
| 432 |
-
#
|
| 433 |
-
#
|
| 434 |
-
#
|
| 435 |
-
#
|
| 436 |
-
#
|
| 437 |
-
#
|
| 438 |
-
|
| 439 |
-
#
|
| 440 |
-
#
|
| 441 |
-
#
|
| 442 |
-
#
|
| 443 |
-
# )
|
| 444 |
-
|
| 445 |
-
# self.
|
| 446 |
-
|
| 447 |
-
#
|
| 448 |
-
|
| 449 |
-
#
|
| 450 |
-
|
| 451 |
-
#
|
| 452 |
-
|
| 453 |
-
#
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
#
|
| 457 |
-
#
|
| 458 |
-
|
| 459 |
-
#
|
| 460 |
-
#
|
| 461 |
-
|
| 462 |
-
#
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
#
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
# if
|
| 471 |
-
#
|
| 472 |
-
|
| 473 |
-
#
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
#
|
| 480 |
-
#
|
| 481 |
-
#
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
#
|
| 485 |
-
#
|
| 486 |
-
#
|
| 487 |
-
#
|
| 488 |
-
#
|
| 489 |
-
#
|
| 490 |
-
|
| 491 |
-
#
|
| 492 |
-
#
|
| 493 |
-
#
|
| 494 |
-
#
|
| 495 |
-
#
|
| 496 |
-
|
| 497 |
-
#
|
| 498 |
-
#
|
| 499 |
-
#
|
| 500 |
-
#
|
| 501 |
-
#
|
| 502 |
-
#
|
| 503 |
-
|
| 504 |
-
#
|
| 505 |
-
#
|
| 506 |
-
#
|
| 507 |
-
#
|
| 508 |
-
#
|
| 509 |
-
|
| 510 |
-
#
|
| 511 |
-
|
| 512 |
-
#
|
| 513 |
-
#
|
| 514 |
-
#
|
| 515 |
-
|
| 516 |
-
#
|
| 517 |
-
|
| 518 |
-
#
|
| 519 |
-
#
|
| 520 |
-
#
|
| 521 |
-
#
|
| 522 |
-
#
|
| 523 |
-
#
|
| 524 |
-
|
| 525 |
-
#
|
| 526 |
-
#
|
| 527 |
-
#
|
| 528 |
-
#
|
| 529 |
-
# )
|
| 530 |
-
|
| 531 |
-
# self.
|
| 532 |
-
|
| 533 |
-
#
|
| 534 |
-
|
| 535 |
-
#
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
#
|
| 540 |
-
|
| 541 |
-
#
|
| 542 |
-
#
|
| 543 |
-
|
| 544 |
-
#
|
| 545 |
-
#
|
| 546 |
-
#
|
| 547 |
-
#
|
| 548 |
-
#
|
| 549 |
-
# )
|
| 550 |
-
|
| 551 |
-
#
|
| 552 |
-
#
|
| 553 |
-
#
|
| 554 |
-
#
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
#
|
| 558 |
-
#
|
| 559 |
-
|
| 560 |
-
#
|
| 561 |
-
|
| 562 |
-
#
|
| 563 |
-
|
| 564 |
-
#
|
| 565 |
-
|
| 566 |
-
#
|
| 567 |
-
|
| 568 |
-
#
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
#
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
#
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
# self.
|
| 578 |
-
|
| 579 |
-
#
|
| 580 |
-
#
|
| 581 |
-
|
| 582 |
-
#
|
| 583 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
# )
|
|
|
|
| 1 |
+
# Code is based on ReNoise https://github.com/garibida/ReNoise-Inversion
|
| 2 |
+
|
| 3 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
| 4 |
+
from diffusers.utils import BaseOutput
|
| 5 |
+
import torch
|
| 6 |
+
from typing import List, Optional, Tuple, Union
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
from src.eunms import Epsilon_Update_Type
|
| 10 |
+
|
| 11 |
+
class EulerAncestralDiscreteSchedulerOutput(BaseOutput):
|
| 12 |
+
"""
|
| 13 |
+
Output class for the scheduler's `step` function output.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 17 |
+
Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
|
| 18 |
+
denoising loop.
|
| 19 |
+
pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
|
| 20 |
+
The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
|
| 21 |
+
`pred_original_sample` can be used to preview progress or for guidance.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
prev_sample: torch.FloatTensor
|
| 25 |
+
pred_original_sample: Optional[torch.FloatTensor] = None
|
| 26 |
+
|
| 27 |
+
class MyEulerAncestralDiscreteScheduler(EulerAncestralDiscreteScheduler):
|
| 28 |
+
def set_noise_list(self, noise_list):
|
| 29 |
+
self.noise_list = noise_list
|
| 30 |
+
|
| 31 |
+
def set_noise_list_device(self, device):
|
| 32 |
+
if self.noise_list[0].device == device:
|
| 33 |
+
return
|
| 34 |
+
for i in range(len(self.noise_list)):
|
| 35 |
+
self.noise_list[i] = self.noise_list[i].to(device)
|
| 36 |
+
|
| 37 |
+
def get_noise_to_remove(self):
|
| 38 |
+
sigma_from = self.sigmas[self.step_index]
|
| 39 |
+
sigma_to = self.sigmas[self.step_index + 1]
|
| 40 |
+
sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 41 |
+
|
| 42 |
+
return self.noise_list[self.step_index] * sigma_up\
|
| 43 |
+
|
| 44 |
+
def scale_model_input(
|
| 45 |
+
self, sample: torch.FloatTensor, timestep: Union[float, torch.FloatTensor]
|
| 46 |
+
) -> torch.FloatTensor:
|
| 47 |
+
"""
|
| 48 |
+
Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
|
| 49 |
+
current timestep. Scales the denoising model input by `(sigma**2 + 1) ** 0.5` to match the Euler algorithm.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
sample (`torch.FloatTensor`):
|
| 53 |
+
The input sample.
|
| 54 |
+
timestep (`int`, *optional*):
|
| 55 |
+
The current timestep in the diffusion chain.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
`torch.FloatTensor`:
|
| 59 |
+
A scaled input sample.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
self._init_step_index(timestep.view((1)))
|
| 63 |
+
return EulerAncestralDiscreteScheduler.scale_model_input(self, sample, timestep)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def step(
|
| 67 |
+
self,
|
| 68 |
+
model_output: torch.FloatTensor,
|
| 69 |
+
timestep: Union[float, torch.FloatTensor],
|
| 70 |
+
sample: torch.FloatTensor,
|
| 71 |
+
generator: Optional[torch.Generator] = None,
|
| 72 |
+
return_dict: bool = True,
|
| 73 |
+
) -> Union[EulerAncestralDiscreteSchedulerOutput, Tuple]:
|
| 74 |
+
"""
|
| 75 |
+
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 76 |
+
process from the learned model outputs (most often the predicted noise).
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
model_output (`torch.FloatTensor`):
|
| 80 |
+
The direct output from learned diffusion model.
|
| 81 |
+
timestep (`float`):
|
| 82 |
+
The current discrete timestep in the diffusion chain.
|
| 83 |
+
sample (`torch.FloatTensor`):
|
| 84 |
+
A current instance of a sample created by the diffusion process.
|
| 85 |
+
generator (`torch.Generator`, *optional*):
|
| 86 |
+
A random number generator.
|
| 87 |
+
return_dict (`bool`):
|
| 88 |
+
Whether or not to return a
|
| 89 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or tuple.
|
| 90 |
+
|
| 91 |
+
Returns:
|
| 92 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or `tuple`:
|
| 93 |
+
If return_dict is `True`,
|
| 94 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] is returned,
|
| 95 |
+
otherwise a tuple is returned where the first element is the sample tensor.
|
| 96 |
+
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
if (
|
| 100 |
+
isinstance(timestep, int)
|
| 101 |
+
or isinstance(timestep, torch.IntTensor)
|
| 102 |
+
or isinstance(timestep, torch.LongTensor)
|
| 103 |
+
):
|
| 104 |
+
raise ValueError(
|
| 105 |
+
(
|
| 106 |
+
"Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
| 107 |
+
" `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
| 108 |
+
" one of the `scheduler.timesteps` as a timestep."
|
| 109 |
+
),
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if not self.is_scale_input_called:
|
| 113 |
+
logger.warning(
|
| 114 |
+
"The `scale_model_input` function should be called before `step` to ensure correct denoising. "
|
| 115 |
+
"See `StableDiffusionPipeline` for a usage example."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
self._init_step_index(timestep.view((1)))
|
| 119 |
+
|
| 120 |
+
sigma = self.sigmas[self.step_index]
|
| 121 |
+
|
| 122 |
+
# Upcast to avoid precision issues when computing prev_sample
|
| 123 |
+
sample = sample.to(torch.float32)
|
| 124 |
+
|
| 125 |
+
# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
|
| 126 |
+
if self.config.prediction_type == "epsilon":
|
| 127 |
+
pred_original_sample = sample - sigma * model_output
|
| 128 |
+
elif self.config.prediction_type == "v_prediction":
|
| 129 |
+
# * c_out + input * c_skip
|
| 130 |
+
pred_original_sample = model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
|
| 131 |
+
elif self.config.prediction_type == "sample":
|
| 132 |
+
raise NotImplementedError("prediction_type not implemented yet: sample")
|
| 133 |
+
else:
|
| 134 |
+
raise ValueError(
|
| 135 |
+
f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
sigma_from = self.sigmas[self.step_index]
|
| 139 |
+
sigma_to = self.sigmas[self.step_index + 1]
|
| 140 |
+
sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 141 |
+
sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
| 142 |
+
|
| 143 |
+
# 2. Convert to an ODE derivative
|
| 144 |
+
# derivative = (sample - pred_original_sample) / sigma
|
| 145 |
+
derivative = model_output
|
| 146 |
+
|
| 147 |
+
dt = sigma_down - sigma
|
| 148 |
+
|
| 149 |
+
prev_sample = sample + derivative * dt
|
| 150 |
+
|
| 151 |
+
device = model_output.device
|
| 152 |
+
# noise = randn_tensor(model_output.shape, dtype=model_output.dtype, device=device, generator=generator)
|
| 153 |
+
# prev_sample = prev_sample + noise * sigma_up
|
| 154 |
+
|
| 155 |
+
prev_sample = prev_sample + self.noise_list[self.step_index] * sigma_up
|
| 156 |
+
|
| 157 |
+
# Cast sample back to model compatible dtype
|
| 158 |
+
prev_sample = prev_sample.to(model_output.dtype)
|
| 159 |
+
|
| 160 |
+
# upon completion increase step index by one
|
| 161 |
+
self._step_index += 1
|
| 162 |
+
|
| 163 |
+
if not return_dict:
|
| 164 |
+
return (prev_sample,)
|
| 165 |
+
|
| 166 |
+
return EulerAncestralDiscreteSchedulerOutput(
|
| 167 |
+
prev_sample=prev_sample, pred_original_sample=pred_original_sample
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
def step_and_update_noise(
|
| 171 |
+
self,
|
| 172 |
+
model_output: torch.FloatTensor,
|
| 173 |
+
timestep: Union[float, torch.FloatTensor],
|
| 174 |
+
sample: torch.FloatTensor,
|
| 175 |
+
expected_prev_sample: torch.FloatTensor,
|
| 176 |
+
update_epsilon_type=Epsilon_Update_Type.OVERRIDE,
|
| 177 |
+
generator: Optional[torch.Generator] = None,
|
| 178 |
+
return_dict: bool = True,
|
| 179 |
+
) -> Union[EulerAncestralDiscreteSchedulerOutput, Tuple]:
|
| 180 |
+
"""
|
| 181 |
+
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 182 |
+
process from the learned model outputs (most often the predicted noise).
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
model_output (`torch.FloatTensor`):
|
| 186 |
+
The direct output from learned diffusion model.
|
| 187 |
+
timestep (`float`):
|
| 188 |
+
The current discrete timestep in the diffusion chain.
|
| 189 |
+
sample (`torch.FloatTensor`):
|
| 190 |
+
A current instance of a sample created by the diffusion process.
|
| 191 |
+
generator (`torch.Generator`, *optional*):
|
| 192 |
+
A random number generator.
|
| 193 |
+
return_dict (`bool`):
|
| 194 |
+
Whether or not to return a
|
| 195 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or tuple.
|
| 196 |
+
|
| 197 |
+
Returns:
|
| 198 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or `tuple`:
|
| 199 |
+
If return_dict is `True`,
|
| 200 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] is returned,
|
| 201 |
+
otherwise a tuple is returned where the first element is the sample tensor.
|
| 202 |
+
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
if (
|
| 206 |
+
isinstance(timestep, int)
|
| 207 |
+
or isinstance(timestep, torch.IntTensor)
|
| 208 |
+
or isinstance(timestep, torch.LongTensor)
|
| 209 |
+
):
|
| 210 |
+
raise ValueError(
|
| 211 |
+
(
|
| 212 |
+
"Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
| 213 |
+
" `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
| 214 |
+
" one of the `scheduler.timesteps` as a timestep."
|
| 215 |
+
),
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
if not self.is_scale_input_called:
|
| 219 |
+
logger.warning(
|
| 220 |
+
"The `scale_model_input` function should be called before `step` to ensure correct denoising. "
|
| 221 |
+
"See `StableDiffusionPipeline` for a usage example."
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
self._init_step_index(timestep.view((1)))
|
| 225 |
+
|
| 226 |
+
sigma = self.sigmas[self.step_index]
|
| 227 |
+
|
| 228 |
+
# Upcast to avoid precision issues when computing prev_sample
|
| 229 |
+
sample = sample.to(torch.float32)
|
| 230 |
+
|
| 231 |
+
# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
|
| 232 |
+
if self.config.prediction_type == "epsilon":
|
| 233 |
+
pred_original_sample = sample - sigma * model_output
|
| 234 |
+
elif self.config.prediction_type == "v_prediction":
|
| 235 |
+
# * c_out + input * c_skip
|
| 236 |
+
pred_original_sample = model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
|
| 237 |
+
elif self.config.prediction_type == "sample":
|
| 238 |
+
raise NotImplementedError("prediction_type not implemented yet: sample")
|
| 239 |
+
else:
|
| 240 |
+
raise ValueError(
|
| 241 |
+
f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
sigma_from = self.sigmas[self.step_index]
|
| 245 |
+
sigma_to = self.sigmas[self.step_index + 1]
|
| 246 |
+
sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 247 |
+
sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
| 248 |
+
|
| 249 |
+
# 2. Convert to an ODE derivative
|
| 250 |
+
# derivative = (sample - pred_original_sample) / sigma
|
| 251 |
+
derivative = model_output
|
| 252 |
+
|
| 253 |
+
dt = sigma_down - sigma
|
| 254 |
+
|
| 255 |
+
prev_sample = sample + derivative * dt
|
| 256 |
+
|
| 257 |
+
device = model_output.device
|
| 258 |
+
# noise = randn_tensor(model_output.shape, dtype=model_output.dtype, device=device, generator=generator)
|
| 259 |
+
# prev_sample = prev_sample + noise * sigma_up
|
| 260 |
+
|
| 261 |
+
if sigma_up > 0:
|
| 262 |
+
req_noise = (expected_prev_sample - prev_sample) / sigma_up
|
| 263 |
+
if update_epsilon_type == Epsilon_Update_Type.OVERRIDE:
|
| 264 |
+
self.noise_list[self.step_index] = req_noise
|
| 265 |
+
else:
|
| 266 |
+
for i in range(10):
|
| 267 |
+
n = torch.autograd.Variable(self.noise_list[self.step_index].detach().clone(), requires_grad=True)
|
| 268 |
+
loss = torch.norm(n - req_noise.detach())
|
| 269 |
+
loss.backward()
|
| 270 |
+
self.noise_list[self.step_index] -= n.grad.detach() * 1.8
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
prev_sample = prev_sample + self.noise_list[self.step_index] * sigma_up
|
| 274 |
+
|
| 275 |
+
# Cast sample back to model compatible dtype
|
| 276 |
+
prev_sample = prev_sample.to(model_output.dtype)
|
| 277 |
+
|
| 278 |
+
# upon completion increase step index by one
|
| 279 |
+
self._step_index += 1
|
| 280 |
+
|
| 281 |
+
if not return_dict:
|
| 282 |
+
return (prev_sample,)
|
| 283 |
+
|
| 284 |
+
return EulerAncestralDiscreteSchedulerOutput(
|
| 285 |
+
prev_sample=prev_sample, pred_original_sample=pred_original_sample
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
def inv_step(
|
| 289 |
+
self,
|
| 290 |
+
model_output: torch.FloatTensor,
|
| 291 |
+
timestep: Union[float, torch.FloatTensor],
|
| 292 |
+
sample: torch.FloatTensor,
|
| 293 |
+
generator: Optional[torch.Generator] = None,
|
| 294 |
+
return_dict: bool = True,
|
| 295 |
+
) -> Union[EulerAncestralDiscreteSchedulerOutput, Tuple]:
|
| 296 |
+
"""
|
| 297 |
+
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 298 |
+
process from the learned model outputs (most often the predicted noise).
|
| 299 |
+
|
| 300 |
+
Args:
|
| 301 |
+
model_output (`torch.FloatTensor`):
|
| 302 |
+
The direct output from learned diffusion model.
|
| 303 |
+
timestep (`float`):
|
| 304 |
+
The current discrete timestep in the diffusion chain.
|
| 305 |
+
sample (`torch.FloatTensor`):
|
| 306 |
+
A current instance of a sample created by the diffusion process.
|
| 307 |
+
generator (`torch.Generator`, *optional*):
|
| 308 |
+
A random number generator.
|
| 309 |
+
return_dict (`bool`):
|
| 310 |
+
Whether or not to return a
|
| 311 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or tuple.
|
| 312 |
+
|
| 313 |
+
Returns:
|
| 314 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or `tuple`:
|
| 315 |
+
If return_dict is `True`,
|
| 316 |
+
[`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] is returned,
|
| 317 |
+
otherwise a tuple is returned where the first element is the sample tensor.
|
| 318 |
+
|
| 319 |
+
"""
|
| 320 |
+
|
| 321 |
+
if (
|
| 322 |
+
isinstance(timestep, int)
|
| 323 |
+
or isinstance(timestep, torch.IntTensor)
|
| 324 |
+
or isinstance(timestep, torch.LongTensor)
|
| 325 |
+
):
|
| 326 |
+
raise ValueError(
|
| 327 |
+
(
|
| 328 |
+
"Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
| 329 |
+
" `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
| 330 |
+
" one of the `scheduler.timesteps` as a timestep."
|
| 331 |
+
),
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
if not self.is_scale_input_called:
|
| 335 |
+
logger.warning(
|
| 336 |
+
"The `scale_model_input` function should be called before `step` to ensure correct denoising. "
|
| 337 |
+
"See `StableDiffusionPipeline` for a usage example."
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
self._init_step_index(timestep.view((1)))
|
| 341 |
+
|
| 342 |
+
sigma = self.sigmas[self.step_index]
|
| 343 |
+
|
| 344 |
+
# Upcast to avoid precision issues when computing prev_sample
|
| 345 |
+
sample = sample.to(torch.float32)
|
| 346 |
+
|
| 347 |
+
# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
|
| 348 |
+
if self.config.prediction_type == "epsilon":
|
| 349 |
+
pred_original_sample = sample - sigma * model_output
|
| 350 |
+
elif self.config.prediction_type == "v_prediction":
|
| 351 |
+
# * c_out + input * c_skip
|
| 352 |
+
pred_original_sample = model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
|
| 353 |
+
elif self.config.prediction_type == "sample":
|
| 354 |
+
raise NotImplementedError("prediction_type not implemented yet: sample")
|
| 355 |
+
else:
|
| 356 |
+
raise ValueError(
|
| 357 |
+
f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
sigma_from = self.sigmas[self.step_index]
|
| 361 |
+
sigma_to = self.sigmas[self.step_index+1]
|
| 362 |
+
# sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 363 |
+
sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2).abs() / sigma_from**2) ** 0.5
|
| 364 |
+
# sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
| 365 |
+
sigma_down = sigma_to**2 / sigma_from
|
| 366 |
+
|
| 367 |
+
# 2. Convert to an ODE derivative
|
| 368 |
+
# derivative = (sample - pred_original_sample) / sigma
|
| 369 |
+
derivative = model_output
|
| 370 |
+
|
| 371 |
+
dt = sigma_down - sigma
|
| 372 |
+
# dt = sigma_down - sigma_from
|
| 373 |
+
|
| 374 |
+
prev_sample = sample - derivative * dt
|
| 375 |
+
|
| 376 |
+
device = model_output.device
|
| 377 |
+
# noise = randn_tensor(model_output.shape, dtype=model_output.dtype, device=device, generator=generator)
|
| 378 |
+
# prev_sample = prev_sample + noise * sigma_up
|
| 379 |
+
|
| 380 |
+
prev_sample = prev_sample - self.noise_list[self.step_index] * sigma_up
|
| 381 |
+
|
| 382 |
+
# Cast sample back to model compatible dtype
|
| 383 |
+
prev_sample = prev_sample.to(model_output.dtype)
|
| 384 |
+
|
| 385 |
+
# upon completion increase step index by one
|
| 386 |
+
self._step_index += 1
|
| 387 |
+
|
| 388 |
+
if not return_dict:
|
| 389 |
+
return (prev_sample,)
|
| 390 |
+
|
| 391 |
+
return EulerAncestralDiscreteSchedulerOutput(
|
| 392 |
+
prev_sample=prev_sample, pred_original_sample=pred_original_sample
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
def get_all_sigmas(self) -> torch.FloatTensor:
|
| 396 |
+
sigmas = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
|
| 397 |
+
sigmas = np.concatenate([sigmas[::-1], [0.0]]).astype(np.float32)
|
| 398 |
+
return torch.from_numpy(sigmas)
|
| 399 |
+
|
| 400 |
+
def add_noise_off_schedule(
|
| 401 |
+
self,
|
| 402 |
+
original_samples: torch.FloatTensor,
|
| 403 |
+
noise: torch.FloatTensor,
|
| 404 |
+
timesteps: torch.FloatTensor,
|
| 405 |
+
) -> torch.FloatTensor:
|
| 406 |
+
# Make sure sigmas and timesteps have the same device and dtype as original_samples
|
| 407 |
+
sigmas = self.get_all_sigmas()
|
| 408 |
+
sigmas = sigmas.to(device=original_samples.device, dtype=original_samples.dtype)
|
| 409 |
+
if original_samples.device.type == "mps" and torch.is_floating_point(timesteps):
|
| 410 |
+
# mps does not support float64
|
| 411 |
+
timesteps = timesteps.to(original_samples.device, dtype=torch.float32)
|
| 412 |
+
else:
|
| 413 |
+
timesteps = timesteps.to(original_samples.device)
|
| 414 |
+
|
| 415 |
+
step_indices = 1000 - int(timesteps.item())
|
| 416 |
+
|
| 417 |
+
sigma = sigmas[step_indices].flatten()
|
| 418 |
+
while len(sigma.shape) < len(original_samples.shape):
|
| 419 |
+
sigma = sigma.unsqueeze(-1)
|
| 420 |
+
|
| 421 |
+
noisy_samples = original_samples + noise * sigma
|
| 422 |
+
return noisy_samples
|
| 423 |
+
|
| 424 |
+
# def update_noise_for_friendly_inversion(
|
| 425 |
+
# self,
|
| 426 |
+
# model_output: torch.FloatTensor,
|
| 427 |
+
# timestep: Union[float, torch.FloatTensor],
|
| 428 |
+
# z_t: torch.FloatTensor,
|
| 429 |
+
# z_tp1: torch.FloatTensor,
|
| 430 |
+
# return_dict: bool = True,
|
| 431 |
+
# ) -> Union[EulerAncestralDiscreteSchedulerOutput, Tuple]:
|
| 432 |
+
# if (
|
| 433 |
+
# isinstance(timestep, int)
|
| 434 |
+
# or isinstance(timestep, torch.IntTensor)
|
| 435 |
+
# or isinstance(timestep, torch.LongTensor)
|
| 436 |
+
# ):
|
| 437 |
+
# raise ValueError(
|
| 438 |
+
# (
|
| 439 |
+
# "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
| 440 |
+
# " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
| 441 |
+
# " one of the `scheduler.timesteps` as a timestep."
|
| 442 |
+
# ),
|
| 443 |
+
# )
|
| 444 |
+
|
| 445 |
+
# if not self.is_scale_input_called:
|
| 446 |
+
# logger.warning(
|
| 447 |
+
# "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
|
| 448 |
+
# "See `StableDiffusionPipeline` for a usage example."
|
| 449 |
+
# )
|
| 450 |
+
|
| 451 |
+
# self._init_step_index(timestep.view((1)))
|
| 452 |
+
|
| 453 |
+
# sigma = self.sigmas[self.step_index]
|
| 454 |
+
|
| 455 |
+
# sigma_from = self.sigmas[self.step_index]
|
| 456 |
+
# sigma_to = self.sigmas[self.step_index+1]
|
| 457 |
+
# # sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 458 |
+
# sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2).abs() / sigma_from**2) ** 0.5
|
| 459 |
+
# # sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
| 460 |
+
# sigma_down = sigma_to**2 / sigma_from
|
| 461 |
+
|
| 462 |
+
# # 2. Conv = (sample - pred_original_sample) / sigma
|
| 463 |
+
# derivative = model_output
|
| 464 |
+
|
| 465 |
+
# dt = sigma_down - sigma
|
| 466 |
+
# # dt = sigma_down - sigma_from
|
| 467 |
+
|
| 468 |
+
# prev_sample = z_t - derivative * dt
|
| 469 |
+
|
| 470 |
+
# if sigma_up > 0:
|
| 471 |
+
# self.noise_list[self.step_index] = (prev_sample - z_tp1) / sigma_up
|
| 472 |
+
|
| 473 |
+
# prev_sample = prev_sample - self.noise_list[self.step_index] * sigma_up
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
# if not return_dict:
|
| 477 |
+
# return (prev_sample,)
|
| 478 |
+
|
| 479 |
+
# return EulerAncestralDiscreteSchedulerOutput(
|
| 480 |
+
# prev_sample=prev_sample, pred_original_sample=None
|
| 481 |
+
# )
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
# def step_friendly_inversion(
|
| 485 |
+
# self,
|
| 486 |
+
# model_output: torch.FloatTensor,
|
| 487 |
+
# timestep: Union[float, torch.FloatTensor],
|
| 488 |
+
# sample: torch.FloatTensor,
|
| 489 |
+
# generator: Optional[torch.Generator] = None,
|
| 490 |
+
# return_dict: bool = True,
|
| 491 |
+
# expected_next_sample: torch.FloatTensor = None,
|
| 492 |
+
# ) -> Union[EulerAncestralDiscreteSchedulerOutput, Tuple]:
|
| 493 |
+
# """
|
| 494 |
+
# Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
| 495 |
+
# process from the learned model outputs (most often the predicted noise).
|
| 496 |
+
|
| 497 |
+
# Args:
|
| 498 |
+
# model_output (`torch.FloatTensor`):
|
| 499 |
+
# The direct output from learned diffusion model.
|
| 500 |
+
# timestep (`float`):
|
| 501 |
+
# The current discrete timestep in the diffusion chain.
|
| 502 |
+
# sample (`torch.FloatTensor`):
|
| 503 |
+
# A current instance of a sample created by the diffusion process.
|
| 504 |
+
# generator (`torch.Generator`, *optional*):
|
| 505 |
+
# A random number generator.
|
| 506 |
+
# return_dict (`bool`):
|
| 507 |
+
# Whether or not to return a
|
| 508 |
+
# [`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or tuple.
|
| 509 |
+
|
| 510 |
+
# Returns:
|
| 511 |
+
# [`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] or `tuple`:
|
| 512 |
+
# If return_dict is `True`,
|
| 513 |
+
# [`~schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteSchedulerOutput`] is returned,
|
| 514 |
+
# otherwise a tuple is returned where the first element is the sample tensor.
|
| 515 |
+
|
| 516 |
+
# """
|
| 517 |
+
|
| 518 |
+
# if (
|
| 519 |
+
# isinstance(timestep, int)
|
| 520 |
+
# or isinstance(timestep, torch.IntTensor)
|
| 521 |
+
# or isinstance(timestep, torch.LongTensor)
|
| 522 |
+
# ):
|
| 523 |
+
# raise ValueError(
|
| 524 |
+
# (
|
| 525 |
+
# "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
| 526 |
+
# " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
| 527 |
+
# " one of the `scheduler.timesteps` as a timestep."
|
| 528 |
+
# ),
|
| 529 |
+
# )
|
| 530 |
+
|
| 531 |
+
# if not self.is_scale_input_called:
|
| 532 |
+
# logger.warning(
|
| 533 |
+
# "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
|
| 534 |
+
# "See `StableDiffusionPipeline` for a usage example."
|
| 535 |
+
# )
|
| 536 |
+
|
| 537 |
+
# self._init_step_index(timestep.view((1)))
|
| 538 |
+
|
| 539 |
+
# sigma = self.sigmas[self.step_index]
|
| 540 |
+
|
| 541 |
+
# # Upcast to avoid precision issues when computing prev_sample
|
| 542 |
+
# sample = sample.to(torch.float32)
|
| 543 |
+
|
| 544 |
+
# # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
|
| 545 |
+
# if self.config.prediction_type == "epsilon":
|
| 546 |
+
# pred_original_sample = sample - sigma * model_output
|
| 547 |
+
# elif self.config.prediction_type == "v_prediction":
|
| 548 |
+
# # * c_out + input * c_skip
|
| 549 |
+
# pred_original_sample = model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
|
| 550 |
+
# elif self.config.prediction_type == "sample":
|
| 551 |
+
# raise NotImplementedError("prediction_type not implemented yet: sample")
|
| 552 |
+
# else:
|
| 553 |
+
# raise ValueError(
|
| 554 |
+
# f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"
|
| 555 |
+
# )
|
| 556 |
+
|
| 557 |
+
# sigma_from = self.sigmas[self.step_index]
|
| 558 |
+
# sigma_to = self.sigmas[self.step_index + 1]
|
| 559 |
+
# sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
|
| 560 |
+
# sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
|
| 561 |
+
|
| 562 |
+
# # 2. Convert to an ODE derivative
|
| 563 |
+
# # derivative = (sample - pred_original_sample) / sigma
|
| 564 |
+
# derivative = model_output
|
| 565 |
+
|
| 566 |
+
# dt = sigma_down - sigma
|
| 567 |
+
|
| 568 |
+
# prev_sample = sample + derivative * dt
|
| 569 |
+
|
| 570 |
+
# device = model_output.device
|
| 571 |
+
# # noise = randn_tensor(model_output.shape, dtype=model_output.dtype, device=device, generator=generator)
|
| 572 |
+
# # prev_sample = prev_sample + noise * sigma_up
|
| 573 |
+
|
| 574 |
+
# if sigma_up > 0:
|
| 575 |
+
# self.noise_list[self.step_index] = (expected_next_sample - prev_sample) / sigma_up
|
| 576 |
+
|
| 577 |
+
# prev_sample = prev_sample + self.noise_list[self.step_index] * sigma_up
|
| 578 |
+
|
| 579 |
+
# # Cast sample back to model compatible dtype
|
| 580 |
+
# prev_sample = prev_sample.to(model_output.dtype)
|
| 581 |
+
|
| 582 |
+
# # upon completion increase step index by one
|
| 583 |
+
# self._step_index += 1
|
| 584 |
+
|
| 585 |
+
# if not return_dict:
|
| 586 |
+
# return (prev_sample,)
|
| 587 |
+
|
| 588 |
+
# return EulerAncestralDiscreteSchedulerOutput(
|
| 589 |
+
# prev_sample=prev_sample, pred_original_sample=pred_original_sample
|
| 590 |
# )
|