Skip to content

Commit cac3f5f

Browse files
skurovecAny-Winter-4079
authored andcommitted
fix for "1 leaked semaphore objects to clean up at shutdown" on M1
Implements fix by @Any-Winter-4079 referenced in #1016 (comment)
1 parent 7e33560 commit cac3f5f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

ldm/modules/diffusionmodules/model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ def __init__(self, in_channels, with_conv):
4949
padding=1)
5050

5151
def forward(self, x):
52+
cpu_m1_cond = True if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available() and \
53+
x.size()[0] * x.size()[1] * x.size()[2] * x.size()[3] % 2**27 == 0 else False
54+
if cpu_m1_cond:
55+
x = x.to('cpu') # send to cpu
5256
x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest")
5357
if self.with_conv:
5458
x = self.conv(x)
59+
if cpu_m1_cond:
60+
x = x.to('mps') # return to mps
5561
return x
5662

5763

@@ -117,6 +123,14 @@ def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False,
117123
padding=0)
118124

119125
def forward(self, x, temb):
126+
if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
127+
x_size = x.size()
128+
if (x_size[0] * x_size[1] * x_size[2] * x_size[3]) % 2**29 == 0:
129+
self.to('cpu')
130+
x = x.to('cpu')
131+
else:
132+
self.to('mps')
133+
x = x.to('mps')
120134
h = self.norm1(x)
121135
h = silu(h)
122136
h = self.conv1(h)

0 commit comments

Comments
 (0)