Repository navigation
Expand file tree
/
Copy pathdiffusion.py
More file actions
188 lines (157 loc) · 5.86 KB
/
Copy pathdiffusion.py
File metadata and controls
188 lines (157 loc) · 5.86 KB
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
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
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
150
151
152
153
154
155
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
# diffusion.py
import jax
import jax.numpy as jnp
from jax import random
from typing import Tuple, Optional
from functools import partial
class DiffusionProcess:
def __init__(
self,
num_timesteps: int = 1000,
beta_start: float = 0.0001,
beta_end: float = 0.02,
dtype: jnp.dtype = jnp.float32
):
self.num_timesteps = num_timesteps
self.dtype = dtype
# Define noise schedule
self.betas = jnp.linspace(beta_start, beta_end, num_timesteps, dtype=dtype)
self.alphas = 1.0 - self.betas
self.alphas_cumprod = jnp.cumprod(self.alphas)
# Pre-compute values for sampling
self.sqrt_alphas_cumprod = jnp.sqrt(self.alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = jnp.sqrt(1.0 - self.alphas_cumprod)
# Pre-compute values for inference
self.sqrt_recip_alphas = jnp.sqrt(1.0 / self.alphas)
alphas_cumprod_prev = jnp.append(jnp.array([1.0]), self.alphas_cumprod[:-1])
self.posterior_variance = self.betas * (1.0 - alphas_cumprod_prev) / (1.0 - self.alphas_cumprod)
def _sample_timesteps_impl(self, key: jnp.ndarray, batch_size: int) -> jnp.ndarray:
"""Implementation of timestep sampling, separated for proper JIT compilation"""
return random.randint(
key,
shape=(batch_size,),
minval=0,
maxval=self.num_timesteps,
dtype=jnp.int32
)
def sample_timesteps(self, key: jnp.ndarray, batch_size: int) -> jnp.ndarray:
"""
Sample random timesteps for a batch.
Args:
key: JAX PRNG key
batch_size: Size of batch to generate timesteps for
Returns:
timesteps: Random timesteps array
"""
# Create a JIT-compiled version of the implementation with batch_size as static
sample_fn = jax.jit(
self._sample_timesteps_impl,
static_argnums=(1,)
)
return sample_fn(key, batch_size)
@partial(jax.jit, static_argnums=(0,))
def add_noise(
self,
key: jnp.ndarray,
sketch_coords: jnp.ndarray,
timesteps: jnp.ndarray
) -> Tuple[jnp.ndarray, jnp.ndarray]:
"""
Add noise to sketch coordinates based on timestep.
"""
noise = random.normal(
key,
sketch_coords.shape,
dtype=self.dtype
)
sqrt_alphas = jnp.take(self.sqrt_alphas_cumprod, timesteps)
sqrt_one_minus_alphas = jnp.take(self.sqrt_one_minus_alphas_cumprod, timesteps)
sqrt_alphas = sqrt_alphas[:, None, None]
sqrt_one_minus_alphas = sqrt_one_minus_alphas[:, None, None]
noised_coords = sqrt_alphas * sketch_coords + sqrt_one_minus_alphas * noise
return noised_coords, noise
@partial(jax.jit, static_argnums=(0,))
def remove_noise(
self,
noised_coords: jnp.ndarray,
pred_noise: jnp.ndarray,
timesteps: jnp.ndarray
) -> jnp.ndarray:
"""
Remove predicted noise from noised coordinates.
"""
sqrt_alphas = jnp.take(self.sqrt_alphas_cumprod, timesteps)
sqrt_one_minus_alphas = jnp.take(self.sqrt_one_minus_alphas_cumprod, timesteps)
sqrt_alphas = sqrt_alphas[:, None, None]
sqrt_one_minus_alphas = sqrt_one_minus_alphas[:, None, None]
return (noised_coords - sqrt_one_minus_alphas * pred_noise) / sqrt_alphas
@partial(jax.jit, static_argnums=(0,))
def q_sample(
self,
key: jnp.ndarray,
sketch_coords: jnp.ndarray,
timesteps: jnp.ndarray
) -> jnp.ndarray:
"""Sample from the forward diffusion process."""
noised_coords, _ = self.add_noise(key, sketch_coords, timesteps)
return noised_coords
@partial(jax.jit, static_argnums=(0,))
def p_sample(
self,
key: jnp.ndarray,
model_fn,
noised_coords: jnp.ndarray,
timesteps: jnp.ndarray,
clip_denoised: bool = True
) -> jnp.ndarray:
"""Sample from the reverse diffusion process (single step)."""
pred_noise = model_fn(noised_coords, timesteps)
pred_coords = self.remove_noise(noised_coords, pred_noise, timesteps)
if clip_denoised:
pred_coords = jnp.clip(pred_coords, -1.0, 1.0)
return pred_coords
def test_diffusion():
"""Test the diffusion process implementation"""
import matplotlib.pyplot as plt
# Initialize
key = random.PRNGKey(0)
diffusion = DiffusionProcess()
# Create dummy data
batch_size = 4
num_points = 100
key, subkey = random.split(key)
sketch_coords = random.normal(
subkey,
shape=(batch_size, num_points, 2)
)
# Sample timesteps
key, subkey = random.split(key)
timesteps = diffusion.sample_timesteps(subkey, batch_size)
# Add noise
key, subkey = random.split(key)
noised_coords, noise = diffusion.add_noise(subkey, sketch_coords, timesteps)
# Test shapes
print(f"Original coords shape: {sketch_coords.shape}")
print(f"Noised coords shape: {noised_coords.shape}")
print(f"Timesteps shape: {timesteps.shape}")
# Visualize noising process for first example
plt.figure(figsize=(15, 5))
# Original
plt.subplot(131)
plt.scatter(sketch_coords[0, :, 0], sketch_coords[0, :, 1], s=1)
plt.title("Original")
plt.axis('equal')
# Noised
plt.subplot(132)
plt.scatter(noised_coords[0, :, 0], noised_coords[0, :, 1], s=1)
plt.title(f"Noised (t={timesteps[0]})")
plt.axis('equal')
# Noise
plt.subplot(133)
plt.scatter(noise[0, :, 0], noise[0, :, 1], s=1)
plt.title("Added Noise")
plt.axis('equal')
plt.tight_layout()
plt.show()
if __name__ == "__main__":
test_diffusion()