Skip to content

Commit

Permalink
use .to(device) to avoid copy, within one_unet_in_gpu context
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Mar 7, 2023
1 parent 848e8a4 commit 580274b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions dalle2_pytorch/dalle2_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2727,11 +2727,16 @@ def one_unet_in_gpu(self, unet_number = None, unet = None):
if exists(unet_number):
unet = self.get_unet(unet_number)

# devices

cuda, cpu = torch.device('cuda'), torch.device('cpu')

self.cuda()

devices = [module_device(unet) for unet in self.unets]
self.unets.cpu()
unet.cuda()

self.unets.to(cpu)
unet.to(cuda)

yield

Expand Down Expand Up @@ -3114,7 +3119,8 @@ def sample(
distributed = False,
inpaint_image = None,
inpaint_mask = None,
inpaint_resample_times = 5
inpaint_resample_times = 5,
one_unet_in_gpu_at_time = True
):
assert self.unconditional or exists(image_embed), 'image embed must be present on sampling from decoder unless if trained unconditionally'

Expand All @@ -3137,6 +3143,7 @@ def sample(
assert image.shape[0] == batch_size, 'image must have batch size of {} if starting at unet number > 1'.format(batch_size)
prev_unet_output_size = self.image_sizes[start_at_unet_number - 2]
img = resize_image_to(image, prev_unet_output_size, nearest = True)

is_cuda = next(self.parameters()).is_cuda

num_unets = self.num_unets
Expand All @@ -3146,7 +3153,7 @@ def sample(
if unet_number < start_at_unet_number:
continue # It's the easiest way to do it

context = self.one_unet_in_gpu(unet = unet) if is_cuda else null_context()
context = self.one_unet_in_gpu(unet = unet) if is_cuda and one_unet_in_gpu_at_time else null_context()

with context:
# prepare low resolution conditioning for upsamplers
Expand Down
2 changes: 1 addition & 1 deletion dalle2_pytorch/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.12.3'
__version__ = '1.12.4'

0 comments on commit 580274b

Please sign in to comment.