Skip to content

Instantly share code, notes, and snippets.

@NoCrypt
Last active September 14, 2023 03:52
Show Gist options
  • Save NoCrypt/6e7331eda3d670c2156852ae5e300a42 to your computer and use it in GitHub Desktop.
Save NoCrypt/6e7331eda3d670c2156852ae5e300a42 to your computer and use it in GitHub Desktop.
Patches 🩹 git apply --ignore-whitespace <file>
diff --git a/modules/sd_samplers_kdiffusion.py b/modules/sd_samplers_kdiffusion.py
index 5552a8dc..8aa47eb0 100644
--- a/modules/sd_samplers_kdiffusion.py
+++ b/modules/sd_samplers_kdiffusion.py
@@ -19,6 +19,7 @@ samplers_k_diffusion = [
('DPM2 a', 'sample_dpm_2_ancestral', ['k_dpm_2_a'], {'discard_next_to_last_sigma': True, "uses_ensd": True}),
('DPM++ 2S a', 'sample_dpmpp_2s_ancestral', ['k_dpmpp_2s_a'], {"uses_ensd": True, "second_order": True}),
('DPM++ 2M', 'sample_dpmpp_2m', ['k_dpmpp_2m'], {}),
+ ('DPM++ 2M v2', 'sample_dpmpp_2m_v2', ['k_dpmpp_2m'], {}),
('DPM++ SDE', 'sample_dpmpp_sde', ['k_dpmpp_sde'], {"second_order": True, "brownian_noise": True}),
('DPM++ 2M SDE', 'sample_dpmpp_2m_sde', ['k_dpmpp_2m_sde_ka'], {"brownian_noise": True}),
('DPM fast', 'sample_dpm_fast', ['k_dpm_fast'], {"uses_ensd": True}),
@@ -28,6 +29,7 @@ samplers_k_diffusion = [
('DPM2 a Karras', 'sample_dpm_2_ancestral', ['k_dpm_2_a_ka'], {'scheduler': 'karras', 'discard_next_to_last_sigma': True, "uses_ensd": True, "second_order": True}),
('DPM++ 2S a Karras', 'sample_dpmpp_2s_ancestral', ['k_dpmpp_2s_a_ka'], {'scheduler': 'karras', "uses_ensd": True, "second_order": True}),
('DPM++ 2M Karras', 'sample_dpmpp_2m', ['k_dpmpp_2m_ka'], {'scheduler': 'karras'}),
+ ('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', "second_order": True, "brownian_noise": True}),
('DPM++ 2M SDE Karras', 'sample_dpmpp_2m_sde', ['k_dpmpp_2m_sde_ka'], {'scheduler': 'karras', "brownian_noise": True}),
]
diff --git a/repositories/k-diffusion/k_diffusion/sampling.py b/repositories/k-diffusion/k_diffusion/sampling.py
index 6656e80b..8efb2fc4 100644
--- a/repositories/k-diffusion/k_diffusion/sampling.py
+++ b/repositories/k-diffusion/k_diffusion/sampling.py
@@ -649,3 +649,38 @@ def sample_dpmpp_2m_sde(model, x, sigmas, extra_args=None, callback=None, disabl
old_denoised = denoised
h_last = h
return x
+
+@torch.no_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
diff --git a/modules/cache.py b/modules/cache.py
index 71fe6302..eded3461 100644
--- a/modules/cache.py
+++ b/modules/cache.py
@@ -97,24 +97,28 @@ def cached_data_for_file(subsection, title, filename, func):
If the data generation fails, None is returned to indicate the failure. Otherwise, the generated
or cached data is returned as a dictionary.
"""
+ try:
+ existing_cache = cache(subsection)
+ ondisk_mtime = os.path.getmtime(filename)
- existing_cache = cache(subsection)
- ondisk_mtime = os.path.getmtime(filename)
+ entry = existing_cache.get(title)
+ if entry:
+ cached_mtime = entry.get("mtime", 0)
+ if ondisk_mtime > cached_mtime:
+ entry = None
- entry = existing_cache.get(title)
- if entry:
- cached_mtime = entry.get("mtime", 0)
- if ondisk_mtime > cached_mtime:
- entry = None
+ if not entry or 'value' not in entry:
+ value = func()
+ if value is None:
+ return None
- if not entry or 'value' not in entry:
- value = func()
- if value is None:
- return None
+ entry = {'mtime': ondisk_mtime, 'value': value}
+ existing_cache[title] = entry
- entry = {'mtime': ondisk_mtime, 'value': value}
- existing_cache[title] = entry
+ dump_cache()
+ except:
+ # Let's say "screw ya, IDGAF!!!!!!!!!!!!"
+ return None
- dump_cache()
return entry['value']
diff --git a/modules/extensions.py b/modules/extensions.py
index c561159a..a1426f2f 100644
--- a/modules/extensions.py
+++ b/modules/extensions.py
@@ -41,8 +41,11 @@ class Extension:
return {x: getattr(self, x) for x in self.cached_fields}
def from_dict(self, d):
+ try:
for field in self.cached_fields:
setattr(self, field, d[field])
+ except:
+ pass
def read_info_from_repo(self):
if self.is_builtin or self.have_info_from_repo:
diff --git a/repositories/stable-diffusion-stability-ai/ldm/util.py b/repositories/stable-diffusion-stability-ai/ldm/util.py
index 8c09ca1..681ffcf 100644
--- a/repositories/stable-diffusion-stability-ai/ldm/util.py
+++ b/repositories/stable-diffusion-stability-ai/ldm/util.py
@@ -76,7 +76,7 @@ def instantiate_from_config(config):
elif config == "__is_unconditional__":
return None
raise KeyError("Expected key `target` to instantiate.")
- return get_obj_from_str(config["target"])(**config.get("params", dict()))
+ return get_obj_from_str(config["target"])(**config.get("params", dict())).cuda()
def get_obj_from_str(string, reload=False):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment