Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
ace9bf4
initial commit
DN6 Aug 25, 2023
8b0b0cf
move modules to import struct
DN6 Aug 29, 2023
e0b971a
add dummy objects and _LazyModule
DN6 Aug 29, 2023
7ed3edb
merge upstream
DN6 Aug 30, 2023
b39ab6d
add lazy import to schedulers
DN6 Aug 30, 2023
25b52de
clean up unused imports
DN6 Aug 31, 2023
0f2cbb2
lazy import on models module
DN6 Aug 31, 2023
be58b3c
lazy import for schedulers module
DN6 Aug 31, 2023
9f536bf
add lazy import to pipelines module
DN6 Aug 31, 2023
cd1aa81
lazy import altdiffusion
DN6 Aug 31, 2023
c37a7fa
lazy import audio diffusion
DN6 Aug 31, 2023
52b9ea7
lazy import audioldm
DN6 Aug 31, 2023
3ecae3f
lazy import consistency model
DN6 Aug 31, 2023
7de8650
lazy import controlnet
DN6 Aug 31, 2023
784b861
lazy import dance diffusion ddim ddpm
DN6 Aug 31, 2023
5672ea4
lazy import deepfloyd
DN6 Aug 31, 2023
d2a9cfe
lazy import kandinksy
DN6 Aug 31, 2023
3f23b3b
lazy imports
DN6 Aug 31, 2023
d28da56
lazy import semantic diffusion
DN6 Aug 31, 2023
7b4e7c3
lazy imports
DN6 Aug 31, 2023
b25e77e
lazy import stable diffusion
DN6 Aug 31, 2023
298670b
move sd output to its own module
DN6 Aug 31, 2023
598d87f
clean up
DN6 Aug 31, 2023
13c8787
lazy import t2iadapter
DN6 Aug 31, 2023
2208e27
lazy import unclip
DN6 Aug 31, 2023
4483aca
lazy import versatile and vq diffsuion
DN6 Aug 31, 2023
5009fe3
lazy import vq diffusion
DN6 Aug 31, 2023
7debdd7
helper to fetch objects from modules
DN6 Aug 31, 2023
8f8a031
lazy import sdxl
DN6 Aug 31, 2023
4a5ca53
lazy import txt2vid
DN6 Aug 31, 2023
1b44eff
lazy import stochastic karras
DN6 Aug 31, 2023
1eaedac
fix model imports
DN6 Aug 31, 2023
8546b3b
fix bug
DN6 Aug 31, 2023
95eb819
lazy import
DN6 Aug 31, 2023
20a27b4
clean up
DN6 Aug 31, 2023
dae1cb9
clean up
DN6 Sep 1, 2023
b1a6a5a
fixes for tests
DN6 Sep 1, 2023
b48cf38
fixes for tests
DN6 Sep 1, 2023
2011ec3
clean up
DN6 Sep 1, 2023
14b3a11
remove import of torch_utils from utils module
DN6 Sep 4, 2023
d4bbf47
clean up
DN6 Sep 4, 2023
b5aaafe
clean up
DN6 Sep 4, 2023
0c9b084
fix mistake import statement
DN6 Sep 4, 2023
165e488
dedicated modules for exporting and loading
DN6 Sep 4, 2023
9e66f7b
remove testing utils from utils module
DN6 Sep 4, 2023
78ef943
resolve merge conflicts
DN6 Sep 4, 2023
b52552e
fixes from merge conflicts
DN6 Sep 4, 2023
f3e7f45
Merge branch 'main' into lazy-import
patrickvonplaten Sep 4, 2023
cd677a4
Update src/diffusers/pipelines/kandinsky2_2/__init__.py
patrickvonplaten Sep 4, 2023
16d7ccc
Merge branch 'main' into lazy-import
patrickvonplaten Sep 4, 2023
1d016f5
Merge branch 'main' into lazy-import
patrickvonplaten Sep 4, 2023
0eb238c
Merge branch 'main' of https://github.com/huggingface/diffusers into …
patrickvonplaten Sep 5, 2023
b77cdc6
fix docs
patrickvonplaten Sep 5, 2023
fefee10
fix alt diffusion copied from
patrickvonplaten Sep 5, 2023
fb87c62
fix check dummies
patrickvonplaten Sep 5, 2023
2a40957
fix more docs
patrickvonplaten Sep 5, 2023
75c3868
Merge branch 'main' into lazy-import
patrickvonplaten Sep 5, 2023
b2d596c
remove accelerate import from utils module
DN6 Sep 6, 2023
8b8547a
add type checking
DN6 Sep 6, 2023
775f3ca
make style
DN6 Sep 6, 2023
eedf9e1
fix check dummies
patrickvonplaten Sep 6, 2023
2b82164
remove torch import from xformers check
DN6 Sep 6, 2023
e1cb44c
clean up error message
DN6 Sep 6, 2023
d89f569
Merge branch 'main' into lazy-import
patrickvonplaten Sep 6, 2023
caab88c
Merge branch 'main' into lazy-import
DN6 Sep 7, 2023
287cfa7
fixes after upstream merges
DN6 Sep 7, 2023
5fb4ce0
Merge branch 'main' into lazy-import
DN6 Sep 7, 2023
a5ce781
dummy objects fix
DN6 Sep 7, 2023
3a0a45c
fix tests
DN6 Sep 8, 2023
52a76ee
remove unused module import
DN6 Sep 8, 2023
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
16 changes: 6 additions & 10 deletions docs/source/en/api/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

Utility and helper functions for working with 🤗 Diffusers.

## randn_tensor

[[autodoc]] diffusers.utils.randn_tensor

## numpy_to_pil

[[autodoc]] utils.pil_utils.numpy_to_pil
[[autodoc]] utils.numpy_to_pil

## pt_to_pil

[[autodoc]] utils.pil_utils.pt_to_pil
[[autodoc]] utils.pt_to_pil

## load_image

[[autodoc]] utils.testing_utils.load_image
[[autodoc]] utils.load_image

## export_to_gif

[[autodoc]] utils.testing_utils.export_to_gif
[[autodoc]] utils.export_to_gif

## export_to_video

[[autodoc]] utils.testing_utils.export_to_video
[[autodoc]] utils.export_to_video

## make_image_grid

[[autodoc]] utils.pil_utils.make_image_grid
[[autodoc]] utils.pil_utils.make_image_grid
22 changes: 11 additions & 11 deletions docs/source/en/using-diffusers/reproducibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This is why it's important to understand how to control sources of randomness in

## Control randomness

During inference, pipelines rely heavily on random sampling operations which include creating the
During inference, pipelines rely heavily on random sampling operations which include creating the
Gaussian noise tensors to denoise and adding noise to the scheduling step.

Take a look at the tensor values in the [`DDIMPipeline`] after two inference steps:
Expand All @@ -47,7 +47,7 @@ image = ddim(num_inference_steps=2, output_type="np").images
print(np.abs(image).sum())
```

Running the code above prints one value, but if you run it again you get a different value. What is going on here?
Running the code above prints one value, but if you run it again you get a different value. What is going on here?

Every time the pipeline is run, [`torch.randn`](https://pytorch.org/docs/stable/generated/torch.randn.html) uses a different random seed to create Gaussian noise which is denoised stepwise. This leads to a different result each time it is run, which is great for diffusion pipelines since it generates a different random image each time.

Expand Down Expand Up @@ -81,16 +81,16 @@ If you run this code example on your specific hardware and PyTorch version, you

<Tip>

💡 It might be a bit unintuitive at first to pass `Generator` objects to the pipeline instead of
just integer values representing the seed, but this is the recommended design when dealing with
probabilistic models in PyTorch as `Generator`'s are *random states* that can be
💡 It might be a bit unintuitive at first to pass `Generator` objects to the pipeline instead of
just integer values representing the seed, but this is the recommended design when dealing with
probabilistic models in PyTorch as `Generator`'s are *random states* that can be
passed to multiple pipelines in a sequence.

</Tip>

### GPU

Writing a reproducible pipeline on a GPU is a bit trickier, and full reproducibility across different hardware is not guaranteed because matrix multiplication - which diffusion pipelines require a lot of - is less deterministic on a GPU than a CPU. For example, if you run the same code example above on a GPU:
Writing a reproducible pipeline on a GPU is a bit trickier, and full reproducibility across different hardware is not guaranteed because matrix multiplication - which diffusion pipelines require a lot of - is less deterministic on a GPU than a CPU. For example, if you run the same code example above on a GPU:

```python
import torch
Expand All @@ -113,7 +113,7 @@ print(np.abs(image).sum())

The result is not the same even though you're using an identical seed because the GPU uses a different random number generator than the CPU.

To circumvent this problem, 🧨 Diffusers has a [`~diffusers.utils.randn_tensor`] function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The `randn_tensor` function is used everywhere inside the pipeline, allowing the user to **always** pass a CPU `Generator` even if the pipeline is run on a GPU.
To circumvent this problem, 🧨 Diffusers has a [`~diffusers.utils.torch_utils.randn_tensor`] function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The `randn_tensor` function is used everywhere inside the pipeline, allowing the user to **always** pass a CPU `Generator` even if the pipeline is run on a GPU.

You'll see the results are much closer now!

Expand All @@ -139,14 +139,14 @@ print(np.abs(image).sum())
<Tip>

💡 If reproducibility is important, we recommend always passing a CPU generator.
The performance loss is often neglectable, and you'll generate much more similar
The performance loss is often neglectable, and you'll generate much more similar
values than if the pipeline had been run on a GPU.

</Tip>

Finally, for more complex pipelines such as [`UnCLIPPipeline`], these are often extremely
susceptible to precision error propagation. Don't expect similar results across
different GPU hardware or PyTorch versions. In this case, you'll need to run
Finally, for more complex pipelines such as [`UnCLIPPipeline`], these are often extremely
susceptible to precision error propagation. Don't expect similar results across
different GPU hardware or PyTorch versions. In this case, you'll need to run
exactly the same hardware and PyTorch version for full reproducibility.

## Deterministic algorithms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
UNet2DConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.utils import (
PIL_INTERPOLATION,
randn_tensor,
)
from diffusers.utils import PIL_INTERPOLATION
from diffusers.utils.torch_utils import randn_tensor


def preprocess(image, w, h):
Expand Down
7 changes: 2 additions & 5 deletions examples/community/clip_guided_stable_diffusion_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
UNet2DConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.utils import (
PIL_INTERPOLATION,
deprecate,
randn_tensor,
)
from diffusers.utils import PIL_INTERPOLATION, deprecate
from diffusers.utils.torch_utils import randn_tensor


EXAMPLE_DOC_STRING = """
Expand Down
2 changes: 1 addition & 1 deletion examples/community/ddim_noise_comparative_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
from diffusers.schedulers import DDIMScheduler
from diffusers.utils import randn_tensor
from diffusers.utils.torch_utils import randn_tensor


trans = transforms.Compose(
Expand Down
2 changes: 1 addition & 1 deletion examples/community/lpw_stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
is_accelerate_available,
is_accelerate_version,
logging,
randn_tensor,
)
from diffusers.utils.torch_utils import randn_tensor


# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/community/lpw_stable_diffusion_xl.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
is_accelerate_version,
is_invisible_watermark_available,
logging,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


if is_invisible_watermark_available():
Expand Down
2 changes: 1 addition & 1 deletion examples/community/pipeline_fabric.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import List, Optional, Union

import torch
from diffuser.utils.torch_utils import randn_tensor
from packaging import version
from PIL import Image
from transformers import CLIPTextModel, CLIPTokenizer
Expand All @@ -30,7 +31,6 @@
from diffusers.utils import (
deprecate,
logging,
randn_tensor,
replace_example_docstring,
)

Expand Down
2 changes: 1 addition & 1 deletion examples/community/pipeline_zero1to3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
is_accelerate_available,
is_accelerate_version,
logging,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
2 changes: 1 addition & 1 deletion examples/community/run_onnx_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import PIL.Image
import torch
from diffuser.utils.torch_utils import randn_tensor
from PIL import Image
from transformers import CLIPTokenizer

Expand All @@ -19,7 +20,6 @@
from diffusers.utils import (
deprecate,
logging,
randn_tensor,
replace_example_docstring,
)

Expand Down
2 changes: 1 addition & 1 deletion examples/community/run_tensorrt_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pycuda.driver as cuda
import tensorrt as trt
import torch
from diffuser.utils.torch_utils import randn_tensor
from PIL import Image
from pycuda.tools import make_default_context
from transformers import CLIPTokenizer
Expand All @@ -23,7 +24,6 @@
from diffusers.utils import (
deprecate,
logging,
randn_tensor,
replace_example_docstring,
)

Expand Down
2 changes: 1 addition & 1 deletion examples/community/stable_diffusion_controlnet_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
PIL_INTERPOLATION,
is_accelerate_available,
is_accelerate_version,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
2 changes: 1 addition & 1 deletion examples/community/stable_diffusion_controlnet_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
PIL_INTERPOLATION,
is_accelerate_available,
is_accelerate_version,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
PIL_INTERPOLATION,
is_accelerate_available,
is_accelerate_version,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/stable_diffusion_controlnet_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from diffusers.models.unet_2d_blocks import CrossAttnDownBlock2D, CrossAttnUpBlock2D, DownBlock2D, UpBlock2D
from diffusers.pipelines.controlnet.multicontrolnet import MultiControlNetModel
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.utils import is_compiled_module, logging, randn_tensor
from diffusers.utils import logging
from diffusers.utils.torch_utils import is_compiled_module, randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
2 changes: 1 addition & 1 deletion examples/community/stable_diffusion_ipex.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
is_accelerate_available,
is_accelerate_version,
logging,
randn_tensor,
replace_example_docstring,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/stable_diffusion_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from diffusers.models.unet_2d_blocks import CrossAttnDownBlock2D, CrossAttnUpBlock2D, DownBlock2D, UpBlock2D
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import rescale_noise_cfg
from diffusers.utils import PIL_INTERPOLATION, logging, randn_tensor
from diffusers.utils import PIL_INTERPOLATION, logging
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
2 changes: 1 addition & 1 deletion examples/community/stable_diffusion_repaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
is_accelerate_available,
is_accelerate_version,
logging,
randn_tensor,
)
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/stable_diffusion_xl_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
UpBlock2D,
)
from diffusers.pipelines.stable_diffusion_xl import StableDiffusionXLPipelineOutput
from diffusers.utils import PIL_INTERPOLATION, logging, randn_tensor
from diffusers.utils import PIL_INTERPOLATION, logging
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/stable_unclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from diffusers.models import PriorTransformer
from diffusers.pipelines import DiffusionPipeline, StableDiffusionImageVariationPipeline
from diffusers.schedulers import UnCLIPScheduler
from diffusers.utils import logging, randn_tensor
from diffusers.utils import logging
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/unclip_image_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
UNet2DModel,
)
from diffusers.pipelines.unclip import UnCLIPTextProjModel
from diffusers.utils import is_accelerate_available, logging, randn_tensor
from diffusers.utils import is_accelerate_available, logging
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion examples/community/unclip_text_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
UNet2DModel,
)
from diffusers.pipelines.unclip import UnCLIPTextProjModel
from diffusers.utils import is_accelerate_available, logging, randn_tensor
from diffusers.utils import is_accelerate_available, logging
from diffusers.utils.torch_utils import randn_tensor


logger = logging.get_logger(__name__) # pylint: disable=invalid-name
Expand Down
Loading