Compare commits

..

5 Commits
1.4.2 ... 1.5.0

Author SHA1 Message Date
Phil Wang
be3bb868bf add gradient checkpointing for all resnet blocks 2022-08-02 19:21:44 -07:00
Phil Wang
451de34871 enforce clip anytorch version 2022-07-30 10:07:55 -07:00
Phil Wang
f22e8c8741 make open clip available for use with dalle2 pytorch 2022-07-30 09:02:31 -07:00
Phil Wang
87432e93ad quick fix for linear attention 2022-07-29 13:17:12 -07:00
Phil Wang
d167378401 add cosine sim for self attention as well, as a setting 2022-07-29 12:48:20 -07:00
4 changed files with 149 additions and 20 deletions

View File

@@ -627,6 +627,18 @@ images = dalle2(
# save your image (in this example, of size 256x256) # save your image (in this example, of size 256x256)
``` ```
Alternatively, you can also use <a href="https://github.com/mlfoundations/open_clip">Open Clip</a>
```bash
$ pip install open-clip-torch
```
```python
from dalle2_pytorch import OpenClipAdapter
clip = OpenClipAdapter()
```
Now you'll just have to worry about training the Prior and the Decoder! Now you'll just have to worry about training the Prior and the Decoder!
## Inpainting ## Inpainting

View File

@@ -8,6 +8,7 @@ from pathlib import Path
import torch import torch
import torch.nn.functional as F import torch.nn.functional as F
from torch.utils.checkpoint import checkpoint
from torch import nn, einsum from torch import nn, einsum
import torchvision.transforms as T import torchvision.transforms as T
@@ -108,6 +109,28 @@ def pad_tuple_to_length(t, length, fillvalue = None):
return t return t
return (*t, *((fillvalue,) * remain_length)) return (*t, *((fillvalue,) * remain_length))
# checkpointing helper function
def make_checkpointable(fn, **kwargs):
if isinstance(fn, nn.ModuleList):
return [maybe(make_checkpointable)(el, **kwargs) for el in fn]
condition = kwargs.pop('condition', None)
if exists(condition) and not condition(fn):
return fn
@wraps(fn)
def inner(*args):
input_needs_grad = any([isinstance(el, torch.Tensor) and el.requires_grad for el in args])
if not input_needs_grad:
return fn(*args)
return checkpoint(fn, *args)
return inner
# for controlling freezing of CLIP # for controlling freezing of CLIP
def set_module_requires_grad_(module, requires_grad): def set_module_requires_grad_(module, requires_grad):
@@ -339,6 +362,75 @@ class OpenAIClipAdapter(BaseClipAdapter):
image_embed = self.clip.encode_image(image) image_embed = self.clip.encode_image(image)
return EmbeddedImage(l2norm(image_embed.float()), None) return EmbeddedImage(l2norm(image_embed.float()), None)
class OpenClipAdapter(BaseClipAdapter):
def __init__(
self,
name = 'ViT-B/32',
pretrained = 'laion400m_e32'
):
import open_clip
clip, _, preprocess = open_clip.create_model_and_transforms(name, pretrained = pretrained)
super().__init__(clip)
self.eos_id = 49407
text_attention_final = self.find_layer('ln_final')
self.handle = text_attention_final.register_forward_hook(self._hook)
self.clip_normalize = preprocess.transforms[-1]
self.cleared = False
def find_layer(self, layer):
modules = dict([*self.clip.named_modules()])
return modules.get(layer, None)
def clear(self):
if self.cleared:
return
self.handle()
def _hook(self, _, inputs, outputs):
self.text_encodings = outputs
@property
def dim_latent(self):
return 512
@property
def image_size(self):
return self.clip.visual.image_size
@property
def image_channels(self):
return 3
@property
def max_text_len(self):
return self.clip.context_length
@torch.no_grad()
def embed_text(self, text):
text = text[..., :self.max_text_len]
is_eos_id = (text == self.eos_id)
text_mask_excluding_eos = is_eos_id.cumsum(dim = -1) == 0
text_mask = F.pad(text_mask_excluding_eos, (1, -1), value = True)
assert not self.cleared
text_embed = self.clip.encode_text(text)
text_encodings = self.text_encodings
text_encodings = text_encodings.masked_fill(~text_mask[..., None], 0.)
del self.text_encodings
return EmbeddedText(l2norm(text_embed.float()), text_encodings.float())
@torch.no_grad()
def embed_image(self, image):
assert not self.cleared
image = self.validate_and_resize_image(image)
image = self.clip_normalize(image)
image_embed = self.clip.encode_image(image)
return EmbeddedImage(l2norm(image_embed.float()), None)
# classifier free guidance functions # classifier free guidance functions
def prob_mask_like(shape, prob, device): def prob_mask_like(shape, prob, device):
@@ -701,11 +793,12 @@ class Attention(nn.Module):
dropout = 0., dropout = 0.,
causal = False, causal = False,
rotary_emb = None, rotary_emb = None,
pb_relax_alpha = 128 cosine_sim = True,
cosine_sim_scale = 16
): ):
super().__init__() super().__init__()
self.pb_relax_alpha = pb_relax_alpha self.scale = cosine_sim_scale if cosine_sim else (dim_head ** -0.5)
self.scale = dim_head ** -0.5 * (pb_relax_alpha ** -1) self.cosine_sim = cosine_sim
self.heads = heads self.heads = heads
inner_dim = dim_head * heads inner_dim = dim_head * heads
@@ -745,6 +838,13 @@ class Attention(nn.Module):
k = torch.cat((nk, k), dim = -2) k = torch.cat((nk, k), dim = -2)
v = torch.cat((nv, v), dim = -2) v = torch.cat((nv, v), dim = -2)
# whether to use cosine sim
if self.cosine_sim:
q, k = map(l2norm, (q, k))
q, k = map(lambda t: t * math.sqrt(self.scale), (q, k))
# calculate query / key similarities # calculate query / key similarities
sim = einsum('b h i d, b j d -> b h i j', q, k) sim = einsum('b h i d, b j d -> b h i j', q, k)
@@ -770,9 +870,6 @@ class Attention(nn.Module):
# attention # attention
sim = sim - sim.amax(dim = -1, keepdim = True).detach()
sim = sim * self.pb_relax_alpha
attn = sim.softmax(dim = -1) attn = sim.softmax(dim = -1)
attn = self.dropout(attn) attn = self.dropout(attn)
@@ -1485,7 +1582,8 @@ class LinearAttention(nn.Module):
self, self,
dim, dim,
dim_head = 32, dim_head = 32,
heads = 8 heads = 8,
**kwargs
): ):
super().__init__() super().__init__()
self.scale = dim_head ** -0.5 self.scale = dim_head ** -0.5
@@ -1604,6 +1702,7 @@ class Unet(nn.Module):
lowres_noise_cond = False, # for conditioning on low resolution noising, based on Imagen lowres_noise_cond = False, # for conditioning on low resolution noising, based on Imagen
sparse_attn = False, sparse_attn = False,
cosine_sim_cross_attn = False, cosine_sim_cross_attn = False,
cosine_sim_self_attn = False,
attend_at_middle = True, # whether to have a layer of attention at the bottleneck (can turn off for higher resolution in cascading DDPM, before bringing in efficient attention) attend_at_middle = True, # whether to have a layer of attention at the bottleneck (can turn off for higher resolution in cascading DDPM, before bringing in efficient attention)
cond_on_text_encodings = False, cond_on_text_encodings = False,
max_text_len = 256, max_text_len = 256,
@@ -1622,6 +1721,7 @@ class Unet(nn.Module):
pixel_shuffle_upsample = True, pixel_shuffle_upsample = True,
final_conv_kernel_size = 1, final_conv_kernel_size = 1,
combine_upsample_fmaps = False, # whether to combine the outputs of all upsample blocks, as in unet squared paper combine_upsample_fmaps = False, # whether to combine the outputs of all upsample blocks, as in unet squared paper
checkpoint_during_training = False,
**kwargs **kwargs
): ):
super().__init__() super().__init__()
@@ -1724,7 +1824,7 @@ class Unet(nn.Module):
# attention related params # attention related params
attn_kwargs = dict(heads = attn_heads, dim_head = attn_dim_head) attn_kwargs = dict(heads = attn_heads, dim_head = attn_dim_head, cosine_sim = cosine_sim_self_attn)
self_attn = cast_tuple(self_attn, num_stages) self_attn = cast_tuple(self_attn, num_stages)
@@ -1832,6 +1932,10 @@ class Unet(nn.Module):
zero_init_(self.to_out) # since both OpenAI and @crowsonkb are doing it zero_init_(self.to_out) # since both OpenAI and @crowsonkb are doing it
# whether to checkpoint during training
self.checkpoint_during_training = checkpoint_during_training
# if the current settings for the unet are not correct # if the current settings for the unet are not correct
# for cascading DDPM, then reinit the unet with the right settings # for cascading DDPM, then reinit the unet with the right settings
def cast_model_parameters( def cast_model_parameters(
@@ -1889,7 +1993,8 @@ class Unet(nn.Module):
image_cond_drop_prob = 0., image_cond_drop_prob = 0.,
text_cond_drop_prob = 0., text_cond_drop_prob = 0.,
blur_sigma = None, blur_sigma = None,
blur_kernel_size = None blur_kernel_size = None,
disable_checkpoint = False
): ):
batch_size, device = x.shape[0], x.device batch_size, device = x.shape[0], x.device
@@ -2011,17 +2116,29 @@ class Unet(nn.Module):
c = self.norm_cond(c) c = self.norm_cond(c)
mid_c = self.norm_mid_cond(mid_c) mid_c = self.norm_mid_cond(mid_c)
# gradient checkpointing
can_checkpoint = self.training and self.checkpoint_during_training and not disable_checkpoint
apply_checkpoint_fn = make_checkpointable if can_checkpoint else identity
# make checkpointable modules
init_resnet_block, mid_block1, mid_attn, mid_block2, final_resnet_block = [maybe(apply_checkpoint_fn)(module) for module in (self.init_resnet_block, self.mid_block1, self.mid_attn, self.mid_block2, self.final_resnet_block)]
can_checkpoint_cond = lambda m: isinstance(m, ResnetBlock)
downs, ups = [maybe(apply_checkpoint_fn)(m, condition = can_checkpoint_cond) for m in (self.downs, self.ups)]
# initial resnet block # initial resnet block
if exists(self.init_resnet_block): if exists(init_resnet_block):
x = self.init_resnet_block(x, t) x = init_resnet_block(x, t)
# go through the layers of the unet, down and up # go through the layers of the unet, down and up
down_hiddens = [] down_hiddens = []
up_hiddens = [] up_hiddens = []
for pre_downsample, init_block, resnet_blocks, attn, post_downsample in self.downs: for pre_downsample, init_block, resnet_blocks, attn, post_downsample in downs:
if exists(pre_downsample): if exists(pre_downsample):
x = pre_downsample(x) x = pre_downsample(x)
@@ -2037,16 +2154,16 @@ class Unet(nn.Module):
if exists(post_downsample): if exists(post_downsample):
x = post_downsample(x) x = post_downsample(x)
x = self.mid_block1(x, t, mid_c) x = mid_block1(x, t, mid_c)
if exists(self.mid_attn): if exists(mid_attn):
x = self.mid_attn(x) x = mid_attn(x)
x = self.mid_block2(x, t, mid_c) x = mid_block2(x, t, mid_c)
connect_skip = lambda fmap: torch.cat((fmap, down_hiddens.pop() * self.skip_connect_scale), dim = 1) connect_skip = lambda fmap: torch.cat((fmap, down_hiddens.pop() * self.skip_connect_scale), dim = 1)
for init_block, resnet_blocks, attn, upsample in self.ups: for init_block, resnet_blocks, attn, upsample in ups:
x = connect_skip(x) x = connect_skip(x)
x = init_block(x, t, c) x = init_block(x, t, c)
@@ -2063,7 +2180,7 @@ class Unet(nn.Module):
x = torch.cat((x, r), dim = 1) x = torch.cat((x, r), dim = 1)
x = self.final_resnet_block(x, t) x = final_resnet_block(x, t)
if exists(lowres_cond_img): if exists(lowres_cond_img):
x = torch.cat((x, lowres_cond_img), dim = 1) x = torch.cat((x, lowres_cond_img), dim = 1)

View File

@@ -1 +1 @@
__version__ = '1.4.2' __version__ = '1.5.0'

View File

@@ -26,7 +26,7 @@ setup(
install_requires=[ install_requires=[
'accelerate', 'accelerate',
'click', 'click',
'clip-anytorch', 'clip-anytorch>=2.4.0',
'coca-pytorch>=0.0.5', 'coca-pytorch>=0.0.5',
'ema-pytorch>=0.0.7', 'ema-pytorch>=0.0.7',
'einops>=0.4', 'einops>=0.4',