Instructions to use DROPTABLE/chxprt with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use DROPTABLE/chxprt with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("DROPTABLE/chxprt", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
changed
Browse files- ambient_diffusion.py +17 -18
ambient_diffusion.py
CHANGED
|
@@ -59,7 +59,7 @@ def make_ambient_batch(
|
|
| 59 |
A = sample_inpainting_mask((B, C, H, W), p=p, device=device)
|
| 60 |
|
| 61 |
# 2. add diffusion noise → x_t , then A x_t
|
| 62 |
-
noise
|
| 63 |
|
| 64 |
if timesteps.ndim == 0:
|
| 65 |
timesteps = torch.full((B,), timesteps, device=device, dtype=torch.long)
|
|
@@ -114,10 +114,10 @@ class AmbientDDPMPipeline(DDPMPipeline):
|
|
| 114 |
device = self.device
|
| 115 |
h = w = self.unet.config.sample_size
|
| 116 |
|
| 117 |
-
mask
|
| 118 |
-
|
| 119 |
-
)
|
| 120 |
|
|
|
|
| 121 |
x = torch.randn(
|
| 122 |
(batch_size, 1, h, w),
|
| 123 |
generator=generator,
|
|
@@ -126,29 +126,28 @@ class AmbientDDPMPipeline(DDPMPipeline):
|
|
| 126 |
|
| 127 |
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
| 128 |
|
| 129 |
-
|
| 130 |
if class_labels is None:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
)
|
| 137 |
else:
|
| 138 |
cond_lbls = class_labels.to(device)
|
| 139 |
|
| 140 |
uncond_lbls = torch.zeros_like(cond_lbls)
|
| 141 |
|
| 142 |
for t in self.scheduler.timesteps:
|
|
|
|
|
|
|
| 143 |
|
| 144 |
if guidance_scale == 1.0 or class_labels is None:
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
eps = self.unet(net_inp, t, class_labels=cond_lbls).sample
|
| 148 |
else:
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
-
inp = torch.cat([net_inp, net_inp], dim=0) # (2B,C+1,H,W)
|
| 152 |
lbls = torch.cat([cond_lbls, uncond_lbls], dim=0)
|
| 153 |
tids = t.expand(2 * batch_size)
|
| 154 |
|
|
@@ -157,7 +156,7 @@ class AmbientDDPMPipeline(DDPMPipeline):
|
|
| 157 |
)
|
| 158 |
eps = eps_uncond + guidance_scale * (eps_cond - eps_uncond)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
x0_hat = eps
|
| 162 |
sigma = self.scheduler._get_variance(t).sqrt()
|
| 163 |
gamma = sigma ** 2 / (sigma ** 2 + 1)
|
|
|
|
| 59 |
A = sample_inpainting_mask((B, C, H, W), p=p, device=device)
|
| 60 |
|
| 61 |
# 2. add diffusion noise → x_t , then A x_t
|
| 62 |
+
noise = torch.randn_like(clean)
|
| 63 |
|
| 64 |
if timesteps.ndim == 0:
|
| 65 |
timesteps = torch.full((B,), timesteps, device=device, dtype=torch.long)
|
|
|
|
| 114 |
device = self.device
|
| 115 |
h = w = self.unet.config.sample_size
|
| 116 |
|
| 117 |
+
# Create a dummy mask that's all ones (no masking)
|
| 118 |
+
dummy_mask = torch.ones((batch_size, 1, h, w), device=device)
|
|
|
|
| 119 |
|
| 120 |
+
# Start with random noise
|
| 121 |
x = torch.randn(
|
| 122 |
(batch_size, 1, h, w),
|
| 123 |
generator=generator,
|
|
|
|
| 126 |
|
| 127 |
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
| 128 |
|
| 129 |
+
# Set up conditioning labels
|
| 130 |
if class_labels is None:
|
| 131 |
+
cond_lbls = torch.zeros(
|
| 132 |
+
(batch_size, getattr(self.unet.config, "multihot_dim", 1)),
|
| 133 |
+
dtype=torch.long if not hasattr(self.unet.config, "multihot_dim") else torch.float,
|
| 134 |
+
device=device,
|
| 135 |
+
)
|
|
|
|
| 136 |
else:
|
| 137 |
cond_lbls = class_labels.to(device)
|
| 138 |
|
| 139 |
uncond_lbls = torch.zeros_like(cond_lbls)
|
| 140 |
|
| 141 |
for t in self.scheduler.timesteps:
|
| 142 |
+
# Always include mask channel to match training, but mask is all ones
|
| 143 |
+
x_with_mask = torch.cat([x, dummy_mask], dim=1)
|
| 144 |
|
| 145 |
if guidance_scale == 1.0 or class_labels is None:
|
| 146 |
+
# Single conditional/unconditional pass
|
| 147 |
+
eps = self.unet(x_with_mask, t, class_labels=cond_lbls).sample
|
|
|
|
| 148 |
else:
|
| 149 |
+
# Classifier-free guidance with duplicate batch
|
| 150 |
+
inp = torch.cat([x_with_mask, x_with_mask], dim=0)
|
|
|
|
| 151 |
lbls = torch.cat([cond_lbls, uncond_lbls], dim=0)
|
| 152 |
tids = t.expand(2 * batch_size)
|
| 153 |
|
|
|
|
| 156 |
)
|
| 157 |
eps = eps_uncond + guidance_scale * (eps_cond - eps_uncond)
|
| 158 |
|
| 159 |
+
# Standard update (same as before)
|
| 160 |
x0_hat = eps
|
| 161 |
sigma = self.scheduler._get_variance(t).sqrt()
|
| 162 |
gamma = sigma ** 2 / (sigma ** 2 + 1)
|