-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Deterministic issue in DiffusionPipeline when getting dtype or device #10671
Comments
cc @sayakpaul @DN6 This is indeed a problem (with determining device as well), requiring some additional device-determining gymnastics for custom offloading strategy (#10503) |
Thanks for the investigation and the detailed reproducer (all reproducers should be like yours)! So, really thank you! The order problem can be fixed with simple changes: Patchdiff --git a/src/diffusers/pipelines/pipeline_utils.py b/src/diffusers/pipelines/pipeline_utils.py
index 0c1371c75..945ac24d0 100644
--- a/src/diffusers/pipelines/pipeline_utils.py
+++ b/src/diffusers/pipelines/pipeline_utils.py
@@ -507,6 +507,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
modules = [m for m in modules if isinstance(m, torch.nn.Module)]
for module in modules:
+ print(f"module.dtype is {module.dtype} using {type(module).__name__} from {module_names}.") # <--- Add this line
return module.dtype
return torch.float32
@@ -1577,7 +1578,7 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
expected_modules.add(name)
optional_parameters.remove(name)
- return expected_modules, optional_parameters
+ return sorted(expected_modules), sorted(optional_parameters)
@classmethod
def _get_signature_types(cls):
@@ -1619,10 +1620,12 @@ class DiffusionPipeline(ConfigMixin, PushToHubMixin):
k: getattr(self, k) for k in self.config.keys() if not k.startswith("_") and k not in optional_parameters
}
- if set(components.keys()) != expected_modules:
+ actual = sorted(set(components.keys()))
+ expected = sorted(expected_modules)
+ if actual != expected:
raise ValueError(
f"{self} has been incorrectly initialized or {self.__class__} is incorrectly implemented. Expected"
- f" {expected_modules} to be defined, but {components.keys()} are defined."
+ f" {expected} to be defined, but {actual} are defined."
)
return components
Would you maybe interested in opening a PR for this? |
Thank you for your answer and for the patch. Yes, no problem, I will open a PR. |
Describe the bug
This issue is a follow-up of this PR.
The idea was to fix an issue leading to the following error message:
This was due to a dtype mismatch between image input encoded with a VAE using float32 precision and unet configured with float16 precision.
Although this bug is fixed, I would like to mention how difficult it was to debug since it didn't occur every time. After debugging I found that the
dtype
property of the diffusion pipeline does not return a value deterministically (it seems the same applies to the device). It uses the dtype of the first pipeline component it finds, but the list of pipeline components is not sorted.I think this may lead to other mistakes in the future and may require changes.
Reproduction
To reproduce the deterministic issue, you can first modify the
DiffusionPipeline
dtype
property in thesrc/diffusers/pipelines/pipeline_utils.py
file to add some logs:Then create a new python script file with the following content:
Run this script multiple times and observe the results (see "Logs" section below).
Logs
System Info
Who can help?
No response
The text was updated successfully, but these errors were encountered: