File size: 2,998 Bytes
bf7b947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
From 4394a62004260c3b9d781488e85f959a70910af1 Mon Sep 17 00:00:00 2001
Date: Sat, 8 Apr 2023 15:11:43 +1000
Subject: [PATCH] add DPMPP 2M V2

---
 modules/sd_samplers_kdiffusion.py | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/modules/sd_samplers_kdiffusion.py b/modules/sd_samplers_kdiffusion.py
index 93f0e55a..9202f4d4 100644
--- a/modules/sd_samplers_kdiffusion.py
+++ b/modules/sd_samplers_kdiffusion.py
@@ -27,12 +27,12 @@ samplers_k_diffusion = [
     ('DPM2 a Karras', 'sample_dpm_2_ancestral', ['k_dpm_2_a_ka'], {'scheduler': 'karras', 'discard_next_to_last_sigma': True}),
     ('DPM++ 2S a Karras', 'sample_dpmpp_2s_ancestral', ['k_dpmpp_2s_a_ka'], {'scheduler': 'karras'}),
     ('DPM++ 2M Karras', 'sample_dpmpp_2m', ['k_dpmpp_2m_ka'], {'scheduler': 'karras'}),
+    ('DPM++ 2M v2', 'sample_dpmpp_2m_v2', ['k_dpmpp_2m'], {}),
+    ('DPM++ 2M Karras v2', 'sample_dpmpp_2m_v2', ['k_dpmpp_2m_ka'], {'scheduler': 'karras'}),
     ('DPM++ SDE Karras', 'sample_dpmpp_sde', ['k_dpmpp_sde_ka'], {'scheduler': 'karras'}),
 ]

--

---
 k_diffusion/sampling.py | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/repositories/k-diffusion/k_diffusion/sampling.py b/repositories/k-diffusion/k_diffusion/sampling.py
index f050f88..1b0b282 100644
--- a/repositories/k-diffusion/k_diffusion/sampling.py
+++ b/repositories/k-diffusion/k_diffusion/sampling.py
@@ -605,4 +605,39 @@ def sample_dpmpp_2m(model, x, sigmas, extra_args=None, callback=None, disable=No
             x = (sigma_fn(t_next) / sigma_fn(t)) * x - (-h).expm1() * denoised_d
         old_denoised = denoised
     return x
+
+
[email protected]_grad()
+def sample_dpmpp_2m_v2(model, x, sigmas, extra_args=None, callback=None, disable=None):
+    """DPM-Solver++(2M)V2."""
+    extra_args = {} if extra_args is None else extra_args
+    s_in = x.new_ones([x.shape[0]])
+    sigma_fn = lambda t: t.neg().exp()
+    t_fn = lambda sigma: sigma.log().neg()
+    old_denoised = None
+
+    for i in trange(len(sigmas) - 1, disable=disable):
+        denoised = model(x, sigmas[i] * s_in, **extra_args)
+        if callback is not None:
+            callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
+        t, t_next = t_fn(sigmas[i]), t_fn(sigmas[i + 1])
+        h = t_next - t
+
+        t_min = min(sigma_fn(t_next), sigma_fn(t))
+        t_max = max(sigma_fn(t_next), sigma_fn(t))
+
+        if old_denoised is None or sigmas[i + 1] == 0:
+            x = (t_min / t_max) * x - (-h).expm1() * denoised
+        else:
+            h_last = t - t_fn(sigmas[i - 1])
+
+            h_min = min(h_last, h)
+            h_max = max(h_last, h)
+            r = h_max / h_min
+
+            h_d = (h_max + h_min) / 2
+            denoised_d = (1 + 1 / (2 * r)) * denoised - (1 / (2 * r)) * old_denoised
+            x = (t_min / t_max) * x - (-h_d).expm1() * denoised_d
+
+        old_denoised = denoised
+    return x
--
2.34.1