autoregressive-diffusion-pytorch 0.2.4__tar.gz → 0.2.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: autoregressive-diffusion-pytorch
3
- Version: 0.2.4
3
+ Version: 0.2.7
4
4
  Summary: Autoregressive Diffusion - Pytorch
5
5
  Project-URL: Homepage, https://pypi.org/project/autoregressive-diffusion-pytorch/
6
6
  Project-URL: Repository, https://github.com/lucidrains/autoregressive-diffusion-pytorch
@@ -33,6 +33,9 @@ def default(v, d):
33
33
  def divisible_by(num, den):
34
34
  return (num % den) == 0
35
35
 
36
+ def cast_tuple(t):
37
+ return (t,) if not isinstance(t, tuple) else t
38
+
36
39
  # tensor helpers
37
40
 
38
41
  def log(t, eps = 1e-20):
@@ -58,41 +61,7 @@ def pack_one(t, pattern):
58
61
 
59
62
  return packed, unpack_one
60
63
 
61
- # sinusoidal embedding
62
-
63
- class AdaptiveLayerNorm(Module):
64
- def __init__(
65
- self,
66
- dim,
67
- dim_condition = None
68
- ):
69
- super().__init__()
70
- dim_condition = default(dim_condition, dim)
71
-
72
- self.ln = nn.LayerNorm(dim, elementwise_affine = False)
73
- self.to_gamma = nn.Linear(dim_condition, dim, bias = False)
74
- nn.init.zeros_(self.to_gamma.weight)
75
-
76
- def forward(self, x, *, condition):
77
- normed = self.ln(x)
78
- gamma = self.to_gamma(condition)
79
- return normed * (gamma + 1.)
80
-
81
- class LearnedSinusoidalPosEmb(Module):
82
- def __init__(self, dim):
83
- super().__init__()
84
- assert divisible_by(dim, 2)
85
- half_dim = dim // 2
86
- self.weights = nn.Parameter(torch.randn(half_dim))
87
-
88
- def forward(self, x):
89
- x = rearrange(x, 'b -> b 1')
90
- freqs = x * rearrange(self.weights, 'd -> 1 d') * 2 * pi
91
- fouriered = torch.cat((freqs.sin(), freqs.cos()), dim = -1)
92
- fouriered = torch.cat((x, fouriered), dim = -1)
93
- return fouriered
94
-
95
- # gaussian diffusion
64
+ # rectified flow
96
65
 
97
66
  class Flow(Module):
98
67
  def __init__(
@@ -176,12 +145,12 @@ class AutoregressiveFlow(Module):
176
145
  self,
177
146
  dim,
178
147
  *,
179
- max_seq_len,
148
+ max_seq_len: int | tuple[int, ...],
180
149
  depth = 8,
181
150
  dim_head = 64,
182
151
  heads = 8,
183
152
  mlp_depth = 3,
184
- mlp_width = None,
153
+ mlp_width = 1024,
185
154
  dim_input = None,
186
155
  decoder_kwargs: dict = dict(),
187
156
  mlp_kwargs: dict = dict(),
@@ -190,8 +159,11 @@ class AutoregressiveFlow(Module):
190
159
  super().__init__()
191
160
 
192
161
  self.start_token = nn.Parameter(torch.zeros(dim))
193
- self.max_seq_len = max_seq_len
194
- self.abs_pos_emb = nn.Embedding(max_seq_len, dim)
162
+
163
+ max_seq_len = cast_tuple(max_seq_len)
164
+ self.abs_pos_emb = nn.ParameterList([nn.Parameter(torch.zeros(seq_len, dim)) for seq_len in max_seq_len])
165
+
166
+ self.max_seq_len = math.prod(max_seq_len)
195
167
 
196
168
  dim_input = default(dim_input, dim)
197
169
  self.dim_input = dim_input
@@ -205,11 +177,13 @@ class AutoregressiveFlow(Module):
205
177
  **decoder_kwargs
206
178
  )
207
179
 
180
+ self.to_cond_emb = nn.Linear(dim, dim, bias = False)
181
+
208
182
  self.denoiser = MLP(
209
183
  dim_cond = dim,
210
184
  dim_input = dim_input,
211
185
  depth = mlp_depth,
212
- width = default(mlp_width, dim),
186
+ width = mlp_width,
213
187
  **mlp_kwargs
214
188
  )
215
189
 
@@ -223,6 +197,16 @@ class AutoregressiveFlow(Module):
223
197
  def device(self):
224
198
  return next(self.transformer.parameters()).device
225
199
 
200
+ def axial_pos_emb(self):
201
+ # prepare maybe axial positional embedding
202
+
203
+ pos_emb, *rest_pos_embs = self.abs_pos_emb
204
+
205
+ for rest_pos_emb in rest_pos_embs:
206
+ pos_emb = einx.add('i d, j d -> (i j) d', pos_emb, rest_pos_emb)
207
+
208
+ return F.pad(pos_emb, (0, 0, 1, 0), value = 0.)
209
+
226
210
  @torch.no_grad()
227
211
  def sample(
228
212
  self,
@@ -245,12 +229,18 @@ class AutoregressiveFlow(Module):
245
229
  cond = self.proj_in(out)
246
230
 
247
231
  cond = torch.cat((start_tokens, cond), dim = 1)
248
- cond = cond + self.abs_pos_emb(torch.arange(cond.shape[1], device = self.device))
232
+
233
+ seq_len = cond.shape[-2]
234
+ axial_pos_emb = self.axial_pos_emb()
235
+ cond += axial_pos_emb[:seq_len]
249
236
 
250
237
  cond, cache = self.transformer(cond, cache = cache, return_hiddens = True)
251
238
 
252
239
  last_cond = cond[:, -1]
253
240
 
241
+ last_cond += axial_pos_emb[seq_len]
242
+ last_cond = self.to_cond_emb(last_cond)
243
+
254
244
  denoised_pred = self.flow.sample(cond = last_cond)
255
245
 
256
246
  denoised_pred = rearrange(denoised_pred, 'b d -> b 1 d')
@@ -281,10 +271,15 @@ class AutoregressiveFlow(Module):
281
271
  start_token = repeat(self.start_token, 'd -> b 1 d', b = b)
282
272
 
283
273
  seq = torch.cat((start_token, seq), dim = 1)
284
- seq = seq + self.abs_pos_emb(torch.arange(seq_len, device = self.device))
274
+
275
+ axial_pos_emb = self.axial_pos_emb()
276
+ seq = seq + axial_pos_emb[:seq_len]
285
277
 
286
278
  cond = self.transformer(seq)
287
279
 
280
+ cond = cond + axial_pos_emb[1:(seq_len + 1)]
281
+ cond = self.to_cond_emb(cond)
282
+
288
283
  # pack batch and sequence dimensions, so to train each token with different noise levels
289
284
 
290
285
  target, _ = pack_one(target, '* d')
@@ -313,7 +308,8 @@ class ImageAutoregressiveFlow(Module):
313
308
  super().__init__()
314
309
  assert divisible_by(image_size, patch_size)
315
310
 
316
- num_patches = (image_size // patch_size) ** 2
311
+ patch_height_width = image_size // patch_size
312
+ num_patches = patch_height_width ** 2
317
313
  dim_in = channels * patch_size ** 2
318
314
 
319
315
  self.image_size = image_size
@@ -328,7 +324,7 @@ class ImageAutoregressiveFlow(Module):
328
324
  self.model = AutoregressiveFlow(
329
325
  **model,
330
326
  dim_input = dim_in,
331
- max_seq_len = num_patches
327
+ max_seq_len = (patch_height_width, patch_height_width)
332
328
  )
333
329
 
334
330
  self.to_image = Rearrange('b (h w) (c p1 p2) -> b c (h p1) (w p2)', p1 = patch_size, p2 = patch_size, h = int(math.sqrt(num_patches)))
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "autoregressive-diffusion-pytorch"
3
- version = "0.2.4"
3
+ version = "0.2.7"
4
4
  description = "Autoregressive Diffusion - Pytorch"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }