mirror of
https://github.com/lucidrains/DALLE2-pytorch.git
synced 2026-02-18 17:34:21 +01:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63195cc2cb | ||
|
|
a2ef69af66 | ||
|
|
5fff22834e |
@@ -811,7 +811,7 @@ Once built, images will be saved to the same directory the command is invoked
|
|||||||
- [x] use inheritance just this once for sharing logic between decoder and prior network ddpms
|
- [x] use inheritance just this once for sharing logic between decoder and prior network ddpms
|
||||||
- [x] bring in vit-vqgan https://arxiv.org/abs/2110.04627 for the latent diffusion
|
- [x] bring in vit-vqgan https://arxiv.org/abs/2110.04627 for the latent diffusion
|
||||||
- [x] abstract interface for CLIP adapter class, so other CLIPs can be brought in
|
- [x] abstract interface for CLIP adapter class, so other CLIPs can be brought in
|
||||||
- [ ] take care of mixed precision as well as gradient accumulation within decoder trainer
|
- [x] take care of mixed precision as well as gradient accumulation within decoder trainer
|
||||||
- [ ] become an expert with unets, cleanup unet code, make it fully configurable, port all learnings over to https://github.com/lucidrains/x-unet
|
- [ ] become an expert with unets, cleanup unet code, make it fully configurable, port all learnings over to https://github.com/lucidrains/x-unet
|
||||||
- [ ] copy the cascading ddpm code to a separate repo (perhaps https://github.com/lucidrains/denoising-diffusion-pytorch) as the main contribution of dalle2 really is just the prior network
|
- [ ] copy the cascading ddpm code to a separate repo (perhaps https://github.com/lucidrains/denoising-diffusion-pytorch) as the main contribution of dalle2 really is just the prior network
|
||||||
- [ ] transcribe code to Jax, which lowers the activation energy for distributed training, given access to TPUs
|
- [ ] transcribe code to Jax, which lowers the activation energy for distributed training, given access to TPUs
|
||||||
|
|||||||
@@ -3,12 +3,19 @@ from functools import partial
|
|||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
|
from torch.cuda.amp import autocast, GradScaler
|
||||||
|
|
||||||
from dalle2_pytorch.dalle2_pytorch import Decoder
|
from dalle2_pytorch.dalle2_pytorch import Decoder
|
||||||
from dalle2_pytorch.optimizer import get_optimizer
|
from dalle2_pytorch.optimizer import get_optimizer
|
||||||
|
|
||||||
# helper functions
|
# helper functions
|
||||||
|
|
||||||
|
def exists(val):
|
||||||
|
return val is not None
|
||||||
|
|
||||||
|
def cast_tuple(val, length = 1):
|
||||||
|
return val if isinstance(val, tuple) else ((val,) * length)
|
||||||
|
|
||||||
def pick_and_pop(keys, d):
|
def pick_and_pop(keys, d):
|
||||||
values = list(map(lambda key: d.pop(key), keys))
|
values = list(map(lambda key: d.pop(key), keys))
|
||||||
return dict(zip(keys, values))
|
return dict(zip(keys, values))
|
||||||
@@ -89,6 +96,10 @@ class DecoderTrainer(nn.Module):
|
|||||||
self,
|
self,
|
||||||
decoder,
|
decoder,
|
||||||
use_ema = True,
|
use_ema = True,
|
||||||
|
lr = 3e-4,
|
||||||
|
wd = 1e-2,
|
||||||
|
max_grad_norm = None,
|
||||||
|
amp = False,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -106,24 +117,66 @@ class DecoderTrainer(nn.Module):
|
|||||||
|
|
||||||
self.ema_unets = nn.ModuleList([])
|
self.ema_unets = nn.ModuleList([])
|
||||||
|
|
||||||
for ind, unet in enumerate(self.decoder.unets):
|
self.amp = amp
|
||||||
optimizer = get_optimizer(unet.parameters(), **kwargs)
|
|
||||||
|
# be able to finely customize learning rate, weight decay
|
||||||
|
# per unet
|
||||||
|
|
||||||
|
lr, wd = map(partial(cast_tuple, length = self.num_unets), (lr, wd))
|
||||||
|
|
||||||
|
for ind, (unet, unet_lr, unet_wd) in enumerate(zip(self.decoder.unets, lr, wd)):
|
||||||
|
optimizer = get_optimizer(
|
||||||
|
unet.parameters(),
|
||||||
|
lr = unet_lr,
|
||||||
|
wd = unet_wd,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
setattr(self, f'optim{ind}', optimizer) # cannot use pytorch ModuleList for some reason with optimizers
|
setattr(self, f'optim{ind}', optimizer) # cannot use pytorch ModuleList for some reason with optimizers
|
||||||
|
|
||||||
if self.use_ema:
|
if self.use_ema:
|
||||||
self.ema_unets.append(EMA(unet, **ema_kwargs))
|
self.ema_unets.append(EMA(unet, **ema_kwargs))
|
||||||
|
|
||||||
|
scaler = GradScaler(enabled = amp)
|
||||||
|
setattr(self, f'scaler{ind}', scaler)
|
||||||
|
|
||||||
|
# gradient clipping if needed
|
||||||
|
|
||||||
|
self.max_grad_norm = max_grad_norm
|
||||||
|
|
||||||
|
def scale(self, loss, *, unet_number):
|
||||||
|
assert 1 <= unet_number <= self.num_unets
|
||||||
|
index = unet_number - 1
|
||||||
|
scaler = getattr(self, f'scaler{index}')
|
||||||
|
return scaler.scale(loss)
|
||||||
|
|
||||||
def update(self, unet_number):
|
def update(self, unet_number):
|
||||||
assert 1 <= unet_number <= self.num_unets
|
assert 1 <= unet_number <= self.num_unets
|
||||||
index = unet_number - 1
|
index = unet_number - 1
|
||||||
|
unet = self.decoder.unets[index]
|
||||||
|
|
||||||
|
if exists(self.max_grad_norm):
|
||||||
|
nn.utils.clip_grad_norm_(unet.parameters(), self.max_grad_norm)
|
||||||
|
|
||||||
optimizer = getattr(self, f'optim{index}')
|
optimizer = getattr(self, f'optim{index}')
|
||||||
optimizer.step()
|
scaler = getattr(self, f'scaler{index}')
|
||||||
|
|
||||||
|
scaler.step(optimizer)
|
||||||
|
scaler.update()
|
||||||
optimizer.zero_grad()
|
optimizer.zero_grad()
|
||||||
|
|
||||||
if self.use_ema:
|
if self.use_ema:
|
||||||
ema_unet = self.ema_unets[index]
|
ema_unet = self.ema_unets[index]
|
||||||
ema_unet.update()
|
ema_unet.update()
|
||||||
|
|
||||||
def forward(self, x, *, unet_number, **kwargs):
|
def forward(
|
||||||
return self.decoder(x, unet_number = unet_number, **kwargs)
|
self,
|
||||||
|
x,
|
||||||
|
*,
|
||||||
|
unet_number,
|
||||||
|
divisor = 1,
|
||||||
|
**kwargs
|
||||||
|
):
|
||||||
|
with autocast(enabled = self.amp):
|
||||||
|
loss = self.decoder(x, unet_number = unet_number, **kwargs)
|
||||||
|
return self.scale(loss / divisor, unet_number = unet_number)
|
||||||
|
|||||||
Reference in New Issue
Block a user