-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
72 lines (65 loc) · 2.69 KB
/
common.py
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
import math
from typing import Any, Tuple
import flax.linen as nn
from flax.linen.initializers import xavier_uniform
import jax
from jax import lax
import jax.numpy as jnp
from einops import rearrange
class TimestepEmbedder(nn.Module):
"""
Embeds scalar timesteps into vector representations.
"""
hidden_size: int
frequency_embedding_size: int = 256
@nn.compact
def __call__(self, t):
x = self.timestep_embedding(t)
x = nn.Dense(self.hidden_size, kernel_init=nn.initializers.normal(0.02))(x)
x = nn.silu(x)
x = nn.Dense(self.hidden_size, kernel_init=nn.initializers.normal(0.02))(x)
return x
# t is between [0, max_period]. It's the INTEGER timestep, not the fractional (0,1).;
def timestep_embedding(self, t, max_period=10000):
"""
Create sinusoidal timestep embeddings.
:param t: a 1-D Tensor of N indices, one per batch element.
These may be fractional.
:param dim: the dimension of the output.
:param max_period: controls the minimum frequency of the embeddings.
:return: an (N, D) Tensor of positional embeddings.
"""
# https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py
t = jax.lax.convert_element_type(t, jnp.float32)
dim = self.frequency_embedding_size
half = dim // 2
freqs = jnp.exp( -math.log(max_period) * jnp.arange(start=0, stop=half, dtype=jnp.float32) / half)
args = t[:, None] * freqs[None]
embedding = jnp.concatenate([jnp.cos(args), jnp.sin(args)], axis=-1)
return embedding
class LabelEmbedder(nn.Module):
"""
Embeds class labels into vector representations. Also handles label dropout for classifier-free guidance.
"""
dropout_prob: float
num_classes: int
hidden_size: int
def token_drop(self, labels, force_drop_ids=None):
"""
Drops labels to enable classifier-free guidance.
"""
if force_drop_ids is None:
rng = self.make_rng('label_dropout')
drop_ids = jax.random.bernoulli(rng, self.dropout_prob, (labels.shape[0],))
else:
drop_ids = force_drop_ids == 1
labels = jnp.where(drop_ids, self.num_classes, labels)
return labels
@nn.compact
def __call__(self, labels, train, force_drop_ids=None):
embedding_table = nn.Embed(self.num_classes + 1, self.hidden_size, embedding_init=nn.initializers.normal(0.02))
use_dropout = self.dropout_prob > 0
if (train and use_dropout) or (force_drop_ids is not None):
labels = self.token_drop(labels, force_drop_ids)
embeddings = embedding_table(labels)
return embeddings