File size: 2,034 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
From 36078b25801787f0a0f145143637f46d33d8c389 Mon Sep 17 00:00:00 2001
From: Ashen <[email protected]>
Date: Fri, 7 Apr 2023 22:04:35 -0700
Subject: [PATCH] karras v2 experimental

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

diff --git a/k_diffusion/sampling.py b/k_diffusion/sampling.py
index f050f88..4d5df2a 100644
--- a/k_diffusion/sampling.py
+++ b/k_diffusion/sampling.py
@@ -605,3 +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_test(model, x, sigmas, extra_args=None, callback=None, disable=None):
+    """DPM-Solver++(2M)."""
+    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
\ No newline at end of file
-- 
2.40.0