Skip to content

[Single File] Allow loading T5 encoder in mixed precision #8778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/diffusers/loaders/single_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,4 @@ def load_module(name, value):

pipe = pipeline_class(**init_kwargs)

if torch_dtype is not None:
pipe.to(dtype=torch_dtype)
Comment on lines -558 to -559
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this handled then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the model level by passing in dtype to their respective loading methods


return pipe
13 changes: 13 additions & 0 deletions src/diffusers/loaders/single_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,4 +1808,17 @@ def create_diffusers_t5_model_from_checkpoint(

else:
model.load_state_dict(diffusers_format_checkpoint)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh is this related to this?
#8604 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly. When you cast the entire model to fp16 the images will be corrupted. You have to use the logic in from_pretrained.

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
28 changes: 28 additions & 0 deletions tests/single_file/single_file_testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ def test_single_file_components_with_diffusers_config_local_files_only(

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:
def _compare_component_configs(self, pipe, single_file_pipe):
Expand Down Expand Up @@ -378,3 +392,17 @@ def test_single_file_format_inference_is_same_as_pretrained(self, expected_max_d
max_diff = numpy_cosine_similarity_distance(image.flatten(), image_single_file.flatten())

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
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
local_files_only=True,
)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
local_files_only=True,
)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
local_files_only=True,
)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,11 @@ def test_single_file_components_with_original_config_local_files_only(self):
local_files_only=True,
)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,12 @@ def test_single_file_components_with_diffusers_config_local_files_only(self):
local_files_only=True,
)
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)