stable-diffusion-webui
170 строк · 6.4 Кб
1import torch
2
3from modules import devices, rng_philox, shared
4
5
6def randn(seed, shape, generator=None):
7"""Generate a tensor with random numbers from a normal distribution using seed.
8
9Uses the seed parameter to set the global torch seed; to generate more with that seed, use randn_like/randn_without_seed."""
10
11manual_seed(seed)
12
13if shared.opts.randn_source == "NV":
14return torch.asarray((generator or nv_rng).randn(shape), device=devices.device)
15
16if shared.opts.randn_source == "CPU" or devices.device.type == 'mps':
17return torch.randn(shape, device=devices.cpu, generator=generator).to(devices.device)
18
19return torch.randn(shape, device=devices.device, generator=generator)
20
21
22def randn_local(seed, shape):
23"""Generate a tensor with random numbers from a normal distribution using seed.
24
25Does not change the global random number generator. You can only generate the seed's first tensor using this function."""
26
27if shared.opts.randn_source == "NV":
28rng = rng_philox.Generator(seed)
29return torch.asarray(rng.randn(shape), device=devices.device)
30
31local_device = devices.cpu if shared.opts.randn_source == "CPU" or devices.device.type == 'mps' else devices.device
32local_generator = torch.Generator(local_device).manual_seed(int(seed))
33return torch.randn(shape, device=local_device, generator=local_generator).to(devices.device)
34
35
36def randn_like(x):
37"""Generate a tensor with random numbers from a normal distribution using the previously initialized genrator.
38
39Use either randn() or manual_seed() to initialize the generator."""
40
41if shared.opts.randn_source == "NV":
42return torch.asarray(nv_rng.randn(x.shape), device=x.device, dtype=x.dtype)
43
44if shared.opts.randn_source == "CPU" or x.device.type == 'mps':
45return torch.randn_like(x, device=devices.cpu).to(x.device)
46
47return torch.randn_like(x)
48
49
50def randn_without_seed(shape, generator=None):
51"""Generate a tensor with random numbers from a normal distribution using the previously initialized genrator.
52
53Use either randn() or manual_seed() to initialize the generator."""
54
55if shared.opts.randn_source == "NV":
56return torch.asarray((generator or nv_rng).randn(shape), device=devices.device)
57
58if shared.opts.randn_source == "CPU" or devices.device.type == 'mps':
59return torch.randn(shape, device=devices.cpu, generator=generator).to(devices.device)
60
61return torch.randn(shape, device=devices.device, generator=generator)
62
63
64def manual_seed(seed):
65"""Set up a global random number generator using the specified seed."""
66
67if shared.opts.randn_source == "NV":
68global nv_rng
69nv_rng = rng_philox.Generator(seed)
70return
71
72torch.manual_seed(seed)
73
74
75def create_generator(seed):
76if shared.opts.randn_source == "NV":
77return rng_philox.Generator(seed)
78
79device = devices.cpu if shared.opts.randn_source == "CPU" or devices.device.type == 'mps' else devices.device
80generator = torch.Generator(device).manual_seed(int(seed))
81return generator
82
83
84# from https://discuss.pytorch.org/t/help-regarding-slerp-function-for-generative-model-sampling/32475/3
85def slerp(val, low, high):
86low_norm = low/torch.norm(low, dim=1, keepdim=True)
87high_norm = high/torch.norm(high, dim=1, keepdim=True)
88dot = (low_norm*high_norm).sum(1)
89
90if dot.mean() > 0.9995:
91return low * val + high * (1 - val)
92
93omega = torch.acos(dot)
94so = torch.sin(omega)
95res = (torch.sin((1.0-val)*omega)/so).unsqueeze(1)*low + (torch.sin(val*omega)/so).unsqueeze(1) * high
96return res
97
98
99class ImageRNG:
100def __init__(self, shape, seeds, subseeds=None, subseed_strength=0.0, seed_resize_from_h=0, seed_resize_from_w=0):
101self.shape = tuple(map(int, shape))
102self.seeds = seeds
103self.subseeds = subseeds
104self.subseed_strength = subseed_strength
105self.seed_resize_from_h = seed_resize_from_h
106self.seed_resize_from_w = seed_resize_from_w
107
108self.generators = [create_generator(seed) for seed in seeds]
109
110self.is_first = True
111
112def first(self):
113noise_shape = self.shape if self.seed_resize_from_h <= 0 or self.seed_resize_from_w <= 0 else (self.shape[0], int(self.seed_resize_from_h) // 8, int(self.seed_resize_from_w // 8))
114
115xs = []
116
117for i, (seed, generator) in enumerate(zip(self.seeds, self.generators)):
118subnoise = None
119if self.subseeds is not None and self.subseed_strength != 0:
120subseed = 0 if i >= len(self.subseeds) else self.subseeds[i]
121subnoise = randn(subseed, noise_shape)
122
123if noise_shape != self.shape:
124noise = randn(seed, noise_shape)
125else:
126noise = randn(seed, self.shape, generator=generator)
127
128if subnoise is not None:
129noise = slerp(self.subseed_strength, noise, subnoise)
130
131if noise_shape != self.shape:
132x = randn(seed, self.shape, generator=generator)
133dx = (self.shape[2] - noise_shape[2]) // 2
134dy = (self.shape[1] - noise_shape[1]) // 2
135w = noise_shape[2] if dx >= 0 else noise_shape[2] + 2 * dx
136h = noise_shape[1] if dy >= 0 else noise_shape[1] + 2 * dy
137tx = 0 if dx < 0 else dx
138ty = 0 if dy < 0 else dy
139dx = max(-dx, 0)
140dy = max(-dy, 0)
141
142x[:, ty:ty + h, tx:tx + w] = noise[:, dy:dy + h, dx:dx + w]
143noise = x
144
145xs.append(noise)
146
147eta_noise_seed_delta = shared.opts.eta_noise_seed_delta or 0
148if eta_noise_seed_delta:
149self.generators = [create_generator(seed + eta_noise_seed_delta) for seed in self.seeds]
150
151return torch.stack(xs).to(shared.device)
152
153def next(self):
154if self.is_first:
155self.is_first = False
156return self.first()
157
158xs = []
159for generator in self.generators:
160x = randn_without_seed(self.shape, generator=generator)
161xs.append(x)
162
163return torch.stack(xs).to(shared.device)
164
165
166devices.randn = randn
167devices.randn_local = randn_local
168devices.randn_like = randn_like
169devices.randn_without_seed = randn_without_seed
170devices.manual_seed = manual_seed
171