Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
stable-diffusion-webui
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
stable-diffusion-webui
Commits
9d402124
Commit
9d402124
authored
Sep 13, 2022
by
AUTOMATIC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first attempt to produce crrect seeds in batch
parent
85b97cc4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
2 deletions
+51
-2
devices.py
modules/devices.py
+10
-0
processing.py
modules/processing.py
+16
-2
sd_samplers.py
modules/sd_samplers.py
+25
-0
No files found.
modules/devices.py
View file @
9d402124
...
@@ -48,3 +48,13 @@ def randn(seed, shape):
...
@@ -48,3 +48,13 @@ def randn(seed, shape):
torch
.
manual_seed
(
seed
)
torch
.
manual_seed
(
seed
)
return
torch
.
randn
(
shape
,
device
=
device
)
return
torch
.
randn
(
shape
,
device
=
device
)
def
randn_without_seed
(
shape
):
# Pytorch currently doesn't handle setting randomness correctly when the metal backend is used.
if
device
.
type
==
'mps'
:
generator
=
torch
.
Generator
(
device
=
cpu
)
noise
=
torch
.
randn
(
shape
,
generator
=
generator
,
device
=
cpu
)
.
to
(
device
)
return
noise
return
torch
.
randn
(
shape
,
device
=
device
)
modules/processing.py
View file @
9d402124
...
@@ -119,8 +119,14 @@ def slerp(val, low, high):
...
@@ -119,8 +119,14 @@ def slerp(val, low, high):
return
res
return
res
def
create_random_tensors
(
shape
,
seeds
,
subseeds
=
None
,
subseed_strength
=
0.0
,
seed_resize_from_h
=
0
,
seed_resize_from_w
=
0
):
def
create_random_tensors
(
shape
,
seeds
,
subseeds
=
None
,
subseed_strength
=
0.0
,
seed_resize_from_h
=
0
,
seed_resize_from_w
=
0
,
p
=
None
):
xs
=
[]
xs
=
[]
if
p
is
not
None
and
p
.
sampler
is
not
None
and
len
(
seeds
)
>
1
:
sampler_noises
=
[[]
for
_
in
range
(
p
.
sampler
.
number_of_needed_noises
(
p
))]
else
:
sampler_noises
=
None
for
i
,
seed
in
enumerate
(
seeds
):
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
)
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
)
...
@@ -155,9 +161,17 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
...
@@ -155,9 +161,17 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
x
[:,
ty
:
ty
+
h
,
tx
:
tx
+
w
]
=
noise
[:,
dy
:
dy
+
h
,
dx
:
dx
+
w
]
x
[:,
ty
:
ty
+
h
,
tx
:
tx
+
w
]
=
noise
[:,
dy
:
dy
+
h
,
dx
:
dx
+
w
]
noise
=
x
noise
=
x
if
sampler_noises
is
not
None
:
cnt
=
p
.
sampler
.
number_of_needed_noises
(
p
)
for
j
in
range
(
cnt
):
sampler_noises
[
j
]
.
append
(
devices
.
randn_without_seed
(
tuple
(
noise_shape
)))
xs
.
append
(
noise
)
xs
.
append
(
noise
)
if
sampler_noises
is
not
None
:
p
.
sampler
.
sampler_noises
=
[
torch
.
stack
(
n
)
.
to
(
shared
.
device
)
for
n
in
sampler_noises
]
x
=
torch
.
stack
(
xs
)
.
to
(
shared
.
device
)
x
=
torch
.
stack
(
xs
)
.
to
(
shared
.
device
)
return
x
return
x
...
@@ -254,7 +268,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
...
@@ -254,7 +268,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
comments
+=
model_hijack
.
comments
comments
+=
model_hijack
.
comments
# we manually generate all input noises because each one should have a specific seed
# we manually generate all input noises because each one should have a specific seed
x
=
create_random_tensors
([
opt_C
,
p
.
height
//
opt_f
,
p
.
width
//
opt_f
],
seeds
=
seeds
,
subseeds
=
subseeds
,
subseed_strength
=
p
.
subseed_strength
,
seed_resize_from_h
=
p
.
seed_resize_from_h
,
seed_resize_from_w
=
p
.
seed_resize_from_w
)
x
=
create_random_tensors
([
opt_C
,
p
.
height
//
opt_f
,
p
.
width
//
opt_f
],
seeds
=
seeds
,
subseeds
=
subseeds
,
subseed_strength
=
p
.
subseed_strength
,
seed_resize_from_h
=
p
.
seed_resize_from_h
,
seed_resize_from_w
=
p
.
seed_resize_from_w
,
p
=
p
)
if
p
.
n_iter
>
1
:
if
p
.
n_iter
>
1
:
shared
.
state
.
job
=
f
"Batch {n+1} out of {p.n_iter}"
shared
.
state
.
job
=
f
"Batch {n+1} out of {p.n_iter}"
...
...
modules/sd_samplers.py
View file @
9d402124
...
@@ -93,6 +93,10 @@ class VanillaStableDiffusionSampler:
...
@@ -93,6 +93,10 @@ class VanillaStableDiffusionSampler:
self
.
mask
=
None
self
.
mask
=
None
self
.
nmask
=
None
self
.
nmask
=
None
self
.
init_latent
=
None
self
.
init_latent
=
None
self
.
sampler_noises
=
None
def
number_of_needed_noises
(
self
,
p
):
return
0
def
sample_img2img
(
self
,
p
,
x
,
noise
,
conditioning
,
unconditional_conditioning
):
def
sample_img2img
(
self
,
p
,
x
,
noise
,
conditioning
,
unconditional_conditioning
):
t_enc
=
int
(
min
(
p
.
denoising_strength
,
0.999
)
*
p
.
steps
)
t_enc
=
int
(
min
(
p
.
denoising_strength
,
0.999
)
*
p
.
steps
)
...
@@ -171,16 +175,37 @@ def extended_trange(count, *args, **kwargs):
...
@@ -171,16 +175,37 @@ def extended_trange(count, *args, **kwargs):
shared
.
total_tqdm
.
update
()
shared
.
total_tqdm
.
update
()
original_randn_like
=
torch
.
randn_like
class
KDiffusionSampler
:
class
KDiffusionSampler
:
def
__init__
(
self
,
funcname
,
sd_model
):
def
__init__
(
self
,
funcname
,
sd_model
):
self
.
model_wrap
=
k_diffusion
.
external
.
CompVisDenoiser
(
sd_model
)
self
.
model_wrap
=
k_diffusion
.
external
.
CompVisDenoiser
(
sd_model
)
self
.
funcname
=
funcname
self
.
funcname
=
funcname
self
.
func
=
getattr
(
k_diffusion
.
sampling
,
self
.
funcname
)
self
.
func
=
getattr
(
k_diffusion
.
sampling
,
self
.
funcname
)
self
.
model_wrap_cfg
=
CFGDenoiser
(
self
.
model_wrap
)
self
.
model_wrap_cfg
=
CFGDenoiser
(
self
.
model_wrap
)
self
.
sampler_noises
=
None
self
.
sampler_noise_index
=
0
k_diffusion
.
sampling
.
torch
.
randn_like
=
self
.
randn_like
def
callback_state
(
self
,
d
):
def
callback_state
(
self
,
d
):
store_latent
(
d
[
"denoised"
])
store_latent
(
d
[
"denoised"
])
def
number_of_needed_noises
(
self
,
p
):
return
p
.
steps
def
randn_like
(
self
,
x
):
noise
=
self
.
sampler_noises
[
self
.
sampler_noise_index
]
if
self
.
sampler_noises
is
not
None
and
self
.
sampler_noise_index
<
len
(
self
.
sampler_noises
)
else
None
if
noise
is
not
None
and
x
.
shape
==
noise
.
shape
:
res
=
noise
else
:
print
(
'generating'
)
res
=
original_randn_like
(
x
)
self
.
sampler_noise_index
+=
1
return
res
def
sample_img2img
(
self
,
p
,
x
,
noise
,
conditioning
,
unconditional_conditioning
):
def
sample_img2img
(
self
,
p
,
x
,
noise
,
conditioning
,
unconditional_conditioning
):
t_enc
=
int
(
min
(
p
.
denoising_strength
,
0.999
)
*
p
.
steps
)
t_enc
=
int
(
min
(
p
.
denoising_strength
,
0.999
)
*
p
.
steps
)
sigmas
=
self
.
model_wrap
.
get_sigmas
(
p
.
steps
)
sigmas
=
self
.
model_wrap
.
get_sigmas
(
p
.
steps
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment