Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
4447547e
Unverified
Commit
4447547e
authored
Aug 25, 2023
by
Sayak Paul
Committed by
GitHub
Aug 25, 2023
Browse files
[Examples] fix sdxl dreambooth lora checkpointing. (#4749)
* fix sdxl dreambooth lora checkpointing. * style
parent
52222947
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
2 deletions
+77
-2
examples/dreambooth/train_dreambooth_lora_sdxl.py
examples/dreambooth/train_dreambooth_lora_sdxl.py
+6
-2
examples/test_examples.py
examples/test_examples.py
+71
-0
No files found.
examples/dreambooth/train_dreambooth_lora_sdxl.py
View file @
4447547e
...
@@ -843,11 +843,15 @@ def main(args):
...
@@ -843,11 +843,15 @@ def main(args):
lora_state_dict
,
network_alphas
=
LoraLoaderMixin
.
lora_state_dict
(
input_dir
)
lora_state_dict
,
network_alphas
=
LoraLoaderMixin
.
lora_state_dict
(
input_dir
)
LoraLoaderMixin
.
load_lora_into_unet
(
lora_state_dict
,
network_alphas
=
network_alphas
,
unet
=
unet_
)
LoraLoaderMixin
.
load_lora_into_unet
(
lora_state_dict
,
network_alphas
=
network_alphas
,
unet
=
unet_
)
text_encoder_state_dict
=
{
k
:
v
for
k
,
v
in
lora_state_dict
.
items
()
if
"text_encoder."
in
k
}
LoraLoaderMixin
.
load_lora_into_text_encoder
(
LoraLoaderMixin
.
load_lora_into_text_encoder
(
lora
_state_dict
,
network_alphas
=
network_alphas
,
text_encoder
=
text_encoder_one_
text_encoder
_state_dict
,
network_alphas
=
network_alphas
,
text_encoder
=
text_encoder_one_
)
)
text_encoder_2_state_dict
=
{
k
:
v
for
k
,
v
in
lora_state_dict
.
items
()
if
"text_encoder_2."
in
k
}
LoraLoaderMixin
.
load_lora_into_text_encoder
(
LoraLoaderMixin
.
load_lora_into_text_encoder
(
lora
_state_dict
,
network_alphas
=
network_alphas
,
text_encoder
=
text_encoder_two_
text_encoder_2
_state_dict
,
network_alphas
=
network_alphas
,
text_encoder
=
text_encoder_two_
)
)
accelerator
.
register_save_state_pre_hook
(
save_model_hook
)
accelerator
.
register_save_state_pre_hook
(
save_model_hook
)
...
...
examples/test_examples.py
View file @
4447547e
...
@@ -421,6 +421,77 @@ class ExamplesTestsAccelerate(unittest.TestCase):
...
@@ -421,6 +421,77 @@ class ExamplesTestsAccelerate(unittest.TestCase):
)
)
self
.
assertTrue
(
starts_with_unet
)
self
.
assertTrue
(
starts_with_unet
)
def
test_dreambooth_lora_sdxl_checkpointing_checkpoints_total_limit
(
self
):
pipeline_path
=
"hf-internal-testing/tiny-stable-diffusion-xl-pipe"
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
test_args
=
f
"""
examples/dreambooth/train_dreambooth_lora_sdxl.py
--pretrained_model_name_or_path
{
pipeline_path
}
--instance_data_dir docs/source/en/imgs
--instance_prompt photo
--resolution 64
--train_batch_size 1
--gradient_accumulation_steps 1
--max_train_steps 7
--checkpointing_steps=2
--checkpoints_total_limit=2
--learning_rate 5.0e-04
--scale_lr
--lr_scheduler constant
--lr_warmup_steps 0
--output_dir
{
tmpdir
}
"""
.
split
()
run_command
(
self
.
_launch_args
+
test_args
)
pipe
=
DiffusionPipeline
.
from_pretrained
(
pipeline_path
)
pipe
.
load_lora_weights
(
tmpdir
)
pipe
(
"a prompt"
,
num_inference_steps
=
2
)
# check checkpoint directories exist
self
.
assertEqual
(
{
x
for
x
in
os
.
listdir
(
tmpdir
)
if
"checkpoint"
in
x
},
# checkpoint-2 should have been deleted
{
"checkpoint-4"
,
"checkpoint-6"
},
)
def
test_dreambooth_lora_sdxl_text_encoder_checkpointing_checkpoints_total_limit
(
self
):
pipeline_path
=
"hf-internal-testing/tiny-stable-diffusion-xl-pipe"
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
test_args
=
f
"""
examples/dreambooth/train_dreambooth_lora_sdxl.py
--pretrained_model_name_or_path
{
pipeline_path
}
--instance_data_dir docs/source/en/imgs
--instance_prompt photo
--resolution 64
--train_batch_size 1
--gradient_accumulation_steps 1
--max_train_steps 7
--checkpointing_steps=2
--checkpoints_total_limit=2
--train_text_encoder
--learning_rate 5.0e-04
--scale_lr
--lr_scheduler constant
--lr_warmup_steps 0
--output_dir
{
tmpdir
}
"""
.
split
()
run_command
(
self
.
_launch_args
+
test_args
)
pipe
=
DiffusionPipeline
.
from_pretrained
(
pipeline_path
)
pipe
.
load_lora_weights
(
tmpdir
)
pipe
(
"a prompt"
,
num_inference_steps
=
2
)
# check checkpoint directories exist
self
.
assertEqual
(
{
x
for
x
in
os
.
listdir
(
tmpdir
)
if
"checkpoint"
in
x
},
# checkpoint-2 should have been deleted
{
"checkpoint-4"
,
"checkpoint-6"
},
)
def
test_custom_diffusion
(
self
):
def
test_custom_diffusion
(
self
):
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
test_args
=
f
"""
test_args
=
f
"""
...
...
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