Skip to content

Commit 580274b

Browse files
committed
use .to(device) to avoid copy, within one_unet_in_gpu context
1 parent 848e8a4 commit 580274b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

dalle2_pytorch/dalle2_pytorch.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2727,11 +2727,16 @@ def one_unet_in_gpu(self, unet_number = None, unet = None):
27272727
if exists(unet_number):
27282728
unet = self.get_unet(unet_number)
27292729

2730+
# devices
2731+
2732+
cuda, cpu = torch.device('cuda'), torch.device('cpu')
2733+
27302734
self.cuda()
27312735

27322736
devices = [module_device(unet) for unet in self.unets]
2733-
self.unets.cpu()
2734-
unet.cuda()
2737+
2738+
self.unets.to(cpu)
2739+
unet.to(cuda)
27352740

27362741
yield
27372742

@@ -3114,7 +3119,8 @@ def sample(
31143119
distributed = False,
31153120
inpaint_image = None,
31163121
inpaint_mask = None,
3117-
inpaint_resample_times = 5
3122+
inpaint_resample_times = 5,
3123+
one_unet_in_gpu_at_time = True
31183124
):
31193125
assert self.unconditional or exists(image_embed), 'image embed must be present on sampling from decoder unless if trained unconditionally'
31203126

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

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

3149-
context = self.one_unet_in_gpu(unet = unet) if is_cuda else null_context()
3156+
context = self.one_unet_in_gpu(unet = unet) if is_cuda and one_unet_in_gpu_at_time else null_context()
31503157

31513158
with context:
31523159
# prepare low resolution conditioning for upsamplers

dalle2_pytorch/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.12.3'
1+
__version__ = '1.12.4'

0 commit comments

Comments
 (0)