Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
diffusers
Commits
0f14335a
Unverified
Commit
0f14335a
authored
Mar 28, 2023
by
Aki Sakurai
Committed by
GitHub
Mar 28, 2023
Browse files
StableDiffusionLongPromptWeightingPipeline: Do not hardcode pad token (#2832)
parent
8bdf4236
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
4 deletions
+10
-4
examples/community/lpw_stable_diffusion.py
examples/community/lpw_stable_diffusion.py
+5
-2
examples/community/lpw_stable_diffusion_onnx.py
examples/community/lpw_stable_diffusion_onnx.py
+5
-2
No files found.
examples/community/lpw_stable_diffusion.py
View file @
0f14335a
...
...
@@ -179,14 +179,14 @@ def get_prompts_with_weights(pipe: StableDiffusionPipeline, prompt: List[str], m
return
tokens
,
weights
def
pad_tokens_and_weights
(
tokens
,
weights
,
max_length
,
bos
,
eos
,
no_boseos_middle
=
True
,
chunk_length
=
77
):
def
pad_tokens_and_weights
(
tokens
,
weights
,
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
True
,
chunk_length
=
77
):
r
"""
Pad the tokens (with starting and ending tokens) and weights (with 1.0) to max_length.
"""
max_embeddings_multiples
=
(
max_length
-
2
)
//
(
chunk_length
-
2
)
weights_length
=
max_length
if
no_boseos_middle
else
max_embeddings_multiples
*
chunk_length
for
i
in
range
(
len
(
tokens
)):
tokens
[
i
]
=
[
bos
]
+
tokens
[
i
]
+
[
eos
]
*
(
max_length
-
1
-
len
(
tokens
[
i
])
)
tokens
[
i
]
=
[
bos
]
+
tokens
[
i
]
+
[
pad
]
*
(
max_length
-
1
-
len
(
tokens
[
i
])
-
1
)
+
[
eos
]
if
no_boseos_middle
:
weights
[
i
]
=
[
1.0
]
+
weights
[
i
]
+
[
1.0
]
*
(
max_length
-
1
-
len
(
weights
[
i
]))
else
:
...
...
@@ -317,12 +317,14 @@ def get_weighted_text_embeddings(
# pad the length of tokens and weights
bos
=
pipe
.
tokenizer
.
bos_token_id
eos
=
pipe
.
tokenizer
.
eos_token_id
pad
=
getattr
(
pipe
.
tokenizer
,
"pad_token_id"
,
eos
)
prompt_tokens
,
prompt_weights
=
pad_tokens_and_weights
(
prompt_tokens
,
prompt_weights
,
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
no_boseos_middle
,
chunk_length
=
pipe
.
tokenizer
.
model_max_length
,
)
...
...
@@ -334,6 +336,7 @@ def get_weighted_text_embeddings(
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
no_boseos_middle
,
chunk_length
=
pipe
.
tokenizer
.
model_max_length
,
)
...
...
examples/community/lpw_stable_diffusion_onnx.py
View file @
0f14335a
...
...
@@ -196,14 +196,14 @@ def get_prompts_with_weights(pipe, prompt: List[str], max_length: int):
return
tokens
,
weights
def
pad_tokens_and_weights
(
tokens
,
weights
,
max_length
,
bos
,
eos
,
no_boseos_middle
=
True
,
chunk_length
=
77
):
def
pad_tokens_and_weights
(
tokens
,
weights
,
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
True
,
chunk_length
=
77
):
r
"""
Pad the tokens (with starting and ending tokens) and weights (with 1.0) to max_length.
"""
max_embeddings_multiples
=
(
max_length
-
2
)
//
(
chunk_length
-
2
)
weights_length
=
max_length
if
no_boseos_middle
else
max_embeddings_multiples
*
chunk_length
for
i
in
range
(
len
(
tokens
)):
tokens
[
i
]
=
[
bos
]
+
tokens
[
i
]
+
[
eos
]
*
(
max_length
-
1
-
len
(
tokens
[
i
])
)
tokens
[
i
]
=
[
bos
]
+
tokens
[
i
]
+
[
pad
]
*
(
max_length
-
1
-
len
(
tokens
[
i
])
-
1
)
+
[
eos
]
if
no_boseos_middle
:
weights
[
i
]
=
[
1.0
]
+
weights
[
i
]
+
[
1.0
]
*
(
max_length
-
1
-
len
(
weights
[
i
]))
else
:
...
...
@@ -342,12 +342,14 @@ def get_weighted_text_embeddings(
# pad the length of tokens and weights
bos
=
pipe
.
tokenizer
.
bos_token_id
eos
=
pipe
.
tokenizer
.
eos_token_id
pad
=
getattr
(
pipe
.
tokenizer
,
"pad_token_id"
,
eos
)
prompt_tokens
,
prompt_weights
=
pad_tokens_and_weights
(
prompt_tokens
,
prompt_weights
,
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
no_boseos_middle
,
chunk_length
=
pipe
.
tokenizer
.
model_max_length
,
)
...
...
@@ -359,6 +361,7 @@ def get_weighted_text_embeddings(
max_length
,
bos
,
eos
,
pad
,
no_boseos_middle
=
no_boseos_middle
,
chunk_length
=
pipe
.
tokenizer
.
model_max_length
,
)
...
...
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