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
0bab9d6b
Unverified
Commit
0bab9d6b
authored
Jul 05, 2024
by
Dhruv Nair
Committed by
GitHub
Jul 05, 2024
Browse files
[Single File] Allow loading T5 encoder in mixed precision (#8778)
* update * update * update * update
parent
2e2684f0
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
85 additions
and
3 deletions
+85
-3
src/diffusers/loaders/single_file.py
src/diffusers/loaders/single_file.py
+0
-3
src/diffusers/loaders/single_file_utils.py
src/diffusers/loaders/single_file_utils.py
+13
-0
tests/single_file/single_file_testing_utils.py
tests/single_file/single_file_testing_utils.py
+28
-0
tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py
...e/test_stable_diffusion_controlnet_img2img_single_file.py
+9
-0
tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py
...e/test_stable_diffusion_controlnet_inpaint_single_file.py
+9
-0
tests/single_file/test_stable_diffusion_controlnet_single_file.py
...ngle_file/test_stable_diffusion_controlnet_single_file.py
+9
-0
tests/single_file/test_stable_diffusion_xl_adapter_single_file.py
...ngle_file/test_stable_diffusion_xl_adapter_single_file.py
+8
-0
tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py
...e_file/test_stable_diffusion_xl_controlnet_single_file.py
+9
-0
No files found.
src/diffusers/loaders/single_file.py
View file @
0bab9d6b
...
@@ -555,7 +555,4 @@ class FromSingleFileMixin:
...
@@ -555,7 +555,4 @@ class FromSingleFileMixin:
pipe
=
pipeline_class
(
**
init_kwargs
)
pipe
=
pipeline_class
(
**
init_kwargs
)
if
torch_dtype
is
not
None
:
pipe
.
to
(
dtype
=
torch_dtype
)
return
pipe
return
pipe
src/diffusers/loaders/single_file_utils.py
View file @
0bab9d6b
...
@@ -1808,4 +1808,17 @@ def create_diffusers_t5_model_from_checkpoint(
...
@@ -1808,4 +1808,17 @@ def create_diffusers_t5_model_from_checkpoint(
else
:
else
:
model
.
load_state_dict
(
diffusers_format_checkpoint
)
model
.
load_state_dict
(
diffusers_format_checkpoint
)
use_keep_in_fp32_modules
=
(
cls
.
_keep_in_fp32_modules
is
not
None
)
and
(
torch_dtype
==
torch
.
float16
)
if
use_keep_in_fp32_modules
:
keep_in_fp32_modules
=
model
.
_keep_in_fp32_modules
else
:
keep_in_fp32_modules
=
[]
if
keep_in_fp32_modules
is
not
None
:
for
name
,
param
in
model
.
named_parameters
():
if
any
(
module_to_keep_in_fp32
in
name
.
split
(
"."
)
for
module_to_keep_in_fp32
in
keep_in_fp32_modules
):
# param = param.to(torch.float32) does not work here as only in the local scope.
param
.
data
=
param
.
data
.
to
(
torch
.
float32
)
return
model
return
model
tests/single_file/single_file_testing_utils.py
View file @
0bab9d6b
...
@@ -201,6 +201,20 @@ class SDSingleFileTesterMixin:
...
@@ -201,6 +201,20 @@ class SDSingleFileTesterMixin:
self
.
_compare_component_configs
(
pipe
,
single_file_pipe
)
self
.
_compare_component_configs
(
pipe
,
single_file_pipe
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
,
single_file_pipe
=
None
,
):
single_file_pipe
=
single_file_pipe
or
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
torch_dtype
=
torch
.
float16
)
for
component_name
,
component
in
single_file_pipe
.
components
.
items
():
if
not
isinstance
(
component
,
torch
.
nn
.
Module
):
continue
assert
component
.
dtype
==
torch
.
float16
class
SDXLSingleFileTesterMixin
:
class
SDXLSingleFileTesterMixin
:
def
_compare_component_configs
(
self
,
pipe
,
single_file_pipe
):
def
_compare_component_configs
(
self
,
pipe
,
single_file_pipe
):
...
@@ -378,3 +392,17 @@ class SDXLSingleFileTesterMixin:
...
@@ -378,3 +392,17 @@ class SDXLSingleFileTesterMixin:
max_diff
=
numpy_cosine_similarity_distance
(
image
.
flatten
(),
image_single_file
.
flatten
())
max_diff
=
numpy_cosine_similarity_distance
(
image
.
flatten
(),
image_single_file
.
flatten
())
assert
max_diff
<
expected_max_diff
assert
max_diff
<
expected_max_diff
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
,
single_file_pipe
=
None
,
):
single_file_pipe
=
single_file_pipe
or
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
torch_dtype
=
torch
.
float16
)
for
component_name
,
component
in
single_file_pipe
.
components
.
items
():
if
not
isinstance
(
component
,
torch
.
nn
.
Module
):
continue
assert
component
.
dtype
==
torch
.
float16
tests/single_file/test_stable_diffusion_controlnet_img2img_single_file.py
View file @
0bab9d6b
...
@@ -180,3 +180,12 @@ class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SD
...
@@ -180,3 +180,12 @@ class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SD
local_files_only
=
True
,
local_files_only
=
True
,
)
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
):
controlnet
=
ControlNetModel
.
from_pretrained
(
"lllyasviel/control_v11p_sd15_canny"
,
torch_dtype
=
torch
.
float16
,
variant
=
"fp16"
)
single_file_pipe
=
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
controlnet
=
controlnet
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
)
super
().
test_single_file_setting_pipeline_dtype_to_fp16
(
single_file_pipe
)
tests/single_file/test_stable_diffusion_controlnet_inpaint_single_file.py
View file @
0bab9d6b
...
@@ -181,3 +181,12 @@ class StableDiffusionControlNetInpaintPipelineSingleFileSlowTests(unittest.TestC
...
@@ -181,3 +181,12 @@ class StableDiffusionControlNetInpaintPipelineSingleFileSlowTests(unittest.TestC
local_files_only
=
True
,
local_files_only
=
True
,
)
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
):
controlnet
=
ControlNetModel
.
from_pretrained
(
"lllyasviel/control_v11p_sd15_canny"
,
torch_dtype
=
torch
.
float16
,
variant
=
"fp16"
)
single_file_pipe
=
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
controlnet
=
controlnet
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
)
super
().
test_single_file_setting_pipeline_dtype_to_fp16
(
single_file_pipe
)
tests/single_file/test_stable_diffusion_controlnet_single_file.py
View file @
0bab9d6b
...
@@ -169,3 +169,12 @@ class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SD
...
@@ -169,3 +169,12 @@ class StableDiffusionControlNetPipelineSingleFileSlowTests(unittest.TestCase, SD
local_files_only
=
True
,
local_files_only
=
True
,
)
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
):
controlnet
=
ControlNetModel
.
from_pretrained
(
"lllyasviel/control_v11p_sd15_canny"
,
torch_dtype
=
torch
.
float16
,
variant
=
"fp16"
)
single_file_pipe
=
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
controlnet
=
controlnet
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
)
super
().
test_single_file_setting_pipeline_dtype_to_fp16
(
single_file_pipe
)
tests/single_file/test_stable_diffusion_xl_adapter_single_file.py
View file @
0bab9d6b
...
@@ -200,3 +200,11 @@ class StableDiffusionXLAdapterPipelineSingleFileSlowTests(unittest.TestCase, SDX
...
@@ -200,3 +200,11 @@ class StableDiffusionXLAdapterPipelineSingleFileSlowTests(unittest.TestCase, SDX
local_files_only
=
True
,
local_files_only
=
True
,
)
)
self
.
_compare_component_configs
(
pipe
,
pipe_single_file
)
self
.
_compare_component_configs
(
pipe
,
pipe_single_file
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
):
adapter
=
T2IAdapter
.
from_pretrained
(
"TencentARC/t2i-adapter-lineart-sdxl-1.0"
,
torch_dtype
=
torch
.
float16
)
single_file_pipe
=
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
adapter
=
adapter
,
torch_dtype
=
torch
.
float16
)
super
().
test_single_file_setting_pipeline_dtype_to_fp16
(
single_file_pipe
)
tests/single_file/test_stable_diffusion_xl_controlnet_single_file.py
View file @
0bab9d6b
...
@@ -195,3 +195,12 @@ class StableDiffusionXLControlNetPipelineSingleFileSlowTests(unittest.TestCase,
...
@@ -195,3 +195,12 @@ class StableDiffusionXLControlNetPipelineSingleFileSlowTests(unittest.TestCase,
local_files_only
=
True
,
local_files_only
=
True
,
)
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
super
().
_compare_component_configs
(
pipe
,
pipe_single_file
)
def
test_single_file_setting_pipeline_dtype_to_fp16
(
self
):
controlnet
=
ControlNetModel
.
from_pretrained
(
"diffusers/controlnet-depth-sdxl-1.0"
,
torch_dtype
=
torch
.
float16
,
variant
=
"fp16"
)
single_file_pipe
=
self
.
pipeline_class
.
from_single_file
(
self
.
ckpt_path
,
controlnet
=
controlnet
,
safety_checker
=
None
,
torch_dtype
=
torch
.
float16
)
super
().
test_single_file_setting_pipeline_dtype_to_fp16
(
single_file_pipe
)
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