diff options
author | AUTOMATIC1111 <16777216c@gmail.com> | 2022-09-12 16:58:06 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-12 16:58:06 +0000 |
commit | 11e03b9abdb4dbf38151bbf290b77122ff20bddb (patch) | |
tree | 813bee165839f7e20e687ec0dbaafe5535cd71c4 /modules/processing.py | |
parent | a655e90fbe4b2f9574e163102ece4dad217ff6de (diff) | |
parent | b7f95869b4542d356a12da6860b1e6c227784560 (diff) | |
download | stable-diffusion-webui-gfx803-11e03b9abdb4dbf38151bbf290b77122ff20bddb.tar.gz stable-diffusion-webui-gfx803-11e03b9abdb4dbf38151bbf290b77122ff20bddb.tar.bz2 stable-diffusion-webui-gfx803-11e03b9abdb4dbf38151bbf290b77122ff20bddb.zip |
Merge pull request #294 from EliasOenal/master
Fixes for mps/Metal: use of seeds, img2img, CodeFormer
Diffstat (limited to 'modules/processing.py')
-rw-r--r-- | modules/processing.py | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/modules/processing.py b/modules/processing.py index 568f6098..1e6745cc 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -103,18 +103,33 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see for i, seed in enumerate(seeds):
noise_shape = shape if seed_resize_from_h <= 0 or seed_resize_from_w <= 0 else (shape[0], seed_resize_from_h//8, seed_resize_from_w//8)
+ # Pytorch currently doesn't handle seeting randomness correctly when the metal backend is used.
+ generator = torch
+ if shared.device.type == 'mps':
+ shared.device_seed_type = 'cpu'
+ generator = torch.Generator(device=shared.device_seed_type)
+
subnoise = None
if subseeds is not None:
subseed = 0 if i >= len(subseeds) else subseeds[i]
- torch.manual_seed(subseed)
- subnoise = torch.randn(noise_shape, device=shared.device)
+ generator.manual_seed(subseed)
+
+ if shared.device.type != shared.device_seed_type:
+ subnoise = torch.randn(noise_shape, generator=generator, device=shared.device_seed_type).to(shared.device)
+ else:
+ subnoise = torch.randn(noise_shape, device=shared.device)
# randn results depend on device; gpu and cpu get different results for same seed;
# the way I see it, it's better to do this on CPU, so that everyone gets same result;
# but the original script had it like this, so I do not dare change it for now because
# it will break everyone's seeds.
- torch.manual_seed(seed)
- noise = torch.randn(noise_shape, device=shared.device)
+ # When using the mps backend falling back to the cpu device is needed, since mps currently
+ # does not implement seeding properly.
+ generator.manual_seed(seed)
+ if shared.device.type != shared.device_seed_type:
+ noise = torch.randn(noise_shape, generator=generator, device=shared.device_seed_type).to(shared.device)
+ else:
+ noise = torch.randn(noise_shape, device=shared.device)
if subnoise is not None:
#noise = subnoise * subseed_strength + noise * (1 - subseed_strength)
@@ -124,9 +139,11 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see #noise = torch.nn.functional.interpolate(noise.unsqueeze(1), size=shape[1:], mode="bilinear").squeeze()
# noise_shape = (64, 80)
# shape = (64, 72)
-
- torch.manual_seed(seed)
- x = torch.randn(shape, device=shared.device)
+ generator.manual_seed(seed)
+ if shared.device.type != shared.device_seed_type:
+ x = torch.randn(shape, generator=generator, device=shared.device_seed_type).to(shared.device)
+ else:
+ x = torch.randn(shape, device=shared.device)
dx = (shape[2] - noise_shape[2]) // 2 # -4
dy = (shape[1] - noise_shape[1]) // 2
w = noise_shape[2] if dx >= 0 else noise_shape[2] + 2 * dx
@@ -465,7 +482,10 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): if self.image_mask is not None:
init_mask = latent_mask
latmask = init_mask.convert('RGB').resize((self.init_latent.shape[3], self.init_latent.shape[2]))
- latmask = np.moveaxis(np.array(latmask, dtype=np.float64), 2, 0) / 255
+ precision = np.float64
+ if shared.device.type == 'mps': # mps backend does not support float64
+ precision = np.float32
+ latmask = np.moveaxis(np.array(latmask, dtype=precision), 2, 0) / 255
latmask = latmask[0]
latmask = np.around(latmask)
latmask = np.tile(latmask[None], (4, 1, 1))
|