Skip to content

Commit 4a34307

Browse files
authored
add: utility to format our docs too 📜 (#7314)
* add: utility to format our docs too 📜 * debugging saga * fix: message * checking * should be fixed. * revert pipeline_fixture * remove empty line * make style * fix: setup.py * style.
1 parent 8e963d1 commit 4a34307

File tree

84 files changed

+592
-530
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+592
-530
lines changed

.github/workflows/pr_test_peft_backend.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ jobs:
3232
python -m pip install --upgrade pip
3333
pip install .[quality]
3434
- name: Check quality
35-
run: |
36-
ruff check examples tests src utils scripts
37-
ruff format examples tests src utils scripts --check
35+
run: make quality
3836
- name: Check if failure
3937
if: ${{ failure() }}
4038
run: |
@@ -53,7 +51,7 @@ jobs:
5351
run: |
5452
python -m pip install --upgrade pip
5553
pip install .[quality]
56-
- name: Check quality
54+
- name: Check repo consistency
5755
run: |
5856
python utils/check_copies.py
5957
python utils/check_dummies.py

.github/workflows/pr_tests.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ jobs:
4040
python -m pip install --upgrade pip
4141
pip install .[quality]
4242
- name: Check quality
43-
run: |
44-
ruff check examples tests src utils scripts
45-
ruff format examples tests src utils scripts --check
43+
run: make quality
4644
- name: Check if failure
4745
if: ${{ failure() }}
4846
run: |
@@ -61,7 +59,7 @@ jobs:
6159
run: |
6260
python -m pip install --upgrade pip
6361
pip install .[quality]
64-
- name: Check quality
62+
- name: Check repo consistency
6563
run: |
6664
python utils/check_copies.py
6765
python utils/check_dummies.py

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ repo-consistency:
4242
quality:
4343
ruff check $(check_dirs) setup.py
4444
ruff format --check $(check_dirs) setup.py
45+
doc-builder style src/diffusers docs/source --max_len 119 --check_only
4546
python utils/check_doc_toc.py
4647

4748
# Format source code automatically and check is there are any problems left that need manual fixing
@@ -55,6 +56,7 @@ extra_style_checks:
5556
style:
5657
ruff check $(check_dirs) setup.py --fix
5758
ruff format $(check_dirs) setup.py
59+
doc-builder style src/diffusers docs/source --max_len 119
5860
${MAKE} autogenerate_code
5961
${MAKE} extra_style_checks
6062

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"torchvision",
135135
"transformers>=4.25.1",
136136
"urllib3<=2.0.0",
137+
"black",
137138
]
138139

139140
# this is a lookup table with items like:

src/diffusers/dependency_versions_table.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@
4242
"torchvision": "torchvision",
4343
"transformers": "transformers>=4.25.1",
4444
"urllib3": "urllib3<=2.0.0",
45+
"black": "black",
4546
}

src/diffusers/image_processor.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ def blur(image: PIL.Image.Image, blur_factor: int = 4) -> PIL.Image.Image:
173173
@staticmethod
174174
def get_crop_region(mask_image: PIL.Image.Image, width: int, height: int, pad=0):
175175
"""
176-
Finds a rectangular region that contains all masked ares in an image, and expands region to match the aspect ratio of the original image;
177-
for example, if user drew mask in a 128x32 region, and the dimensions for processing are 512x512, the region will be expanded to 128x128.
176+
Finds a rectangular region that contains all masked ares in an image, and expands region to match the aspect
177+
ratio of the original image; for example, if user drew mask in a 128x32 region, and the dimensions for
178+
processing are 512x512, the region will be expanded to 128x128.
178179
179180
Args:
180181
mask_image (PIL.Image.Image): Mask image.
@@ -183,7 +184,8 @@ def get_crop_region(mask_image: PIL.Image.Image, width: int, height: int, pad=0)
183184
pad (int, optional): Padding to be added to the crop region. Defaults to 0.
184185
185186
Returns:
186-
tuple: (x1, y1, x2, y2) represent a rectangular region that contains all masked ares in an image and matches the original aspect ratio.
187+
tuple: (x1, y1, x2, y2) represent a rectangular region that contains all masked ares in an image and
188+
matches the original aspect ratio.
187189
"""
188190

189191
mask_image = mask_image.convert("L")
@@ -265,7 +267,8 @@ def _resize_and_fill(
265267
height: int,
266268
) -> PIL.Image.Image:
267269
"""
268-
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image.
270+
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center
271+
the image within the dimensions, filling empty with data from image.
269272
270273
Args:
271274
image: The image to resize.
@@ -309,7 +312,8 @@ def _resize_and_crop(
309312
height: int,
310313
) -> PIL.Image.Image:
311314
"""
312-
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess.
315+
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center
316+
the image within the dimensions, cropping the excess.
313317
314318
Args:
315319
image: The image to resize.
@@ -346,12 +350,12 @@ def resize(
346350
The width to resize to.
347351
resize_mode (`str`, *optional*, defaults to `default`):
348352
The resize mode to use, can be one of `default` or `fill`. If `default`, will resize the image to fit
349-
within the specified width and height, and it may not maintaining the original aspect ratio.
350-
If `fill`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
351-
within the dimensions, filling empty with data from image.
352-
If `crop`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
353-
within the dimensions, cropping the excess.
354-
Note that resize_mode `fill` and `crop` are only supported for PIL image input.
353+
within the specified width and height, and it may not maintaining the original aspect ratio. If `fill`,
354+
will resize the image to fit within the specified width and height, maintaining the aspect ratio, and
355+
then center the image within the dimensions, filling empty with data from image. If `crop`, will resize
356+
the image to fit within the specified width and height, maintaining the aspect ratio, and then center
357+
the image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only
358+
supported for PIL image input.
355359
356360
Returns:
357361
`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`:
@@ -456,19 +460,21 @@ def preprocess(
456460
457461
Args:
458462
image (`pipeline_image_input`):
459-
The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of supported formats.
463+
The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of
464+
supported formats.
460465
height (`int`, *optional*, defaults to `None`):
461-
The height in preprocessed image. If `None`, will use the `get_default_height_width()` to get default height.
466+
The height in preprocessed image. If `None`, will use the `get_default_height_width()` to get default
467+
height.
462468
width (`int`, *optional*`, defaults to `None`):
463-
The width in preprocessed. If `None`, will use get_default_height_width()` to get the default width.
469+
The width in preprocessed. If `None`, will use get_default_height_width()` to get the default width.
464470
resize_mode (`str`, *optional*, defaults to `default`):
465-
The resize mode, can be one of `default` or `fill`. If `default`, will resize the image to fit
466-
within the specified width and height, and it may not maintaining the original aspect ratio.
467-
If `fill`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
468-
within the dimensions, filling empty with data from image.
469-
If `crop`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
470-
within the dimensions, cropping the excess.
471-
Note that resize_mode `fill` and `crop` are only supported for PIL image input.
471+
The resize mode, can be one of `default` or `fill`. If `default`, will resize the image to fit within
472+
the specified width and height, and it may not maintaining the original aspect ratio. If `fill`, will
473+
resize the image to fit within the specified width and height, maintaining the aspect ratio, and then
474+
center the image within the dimensions, filling empty with data from image. If `crop`, will resize the
475+
image to fit within the specified width and height, maintaining the aspect ratio, and then center the
476+
image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only
477+
supported for PIL image input.
472478
crops_coords (`List[Tuple[int, int, int, int]]`, *optional*, defaults to `None`):
473479
The crop coordinates for each image in the batch. If `None`, will not crop the image.
474480
"""
@@ -930,8 +936,8 @@ def __init__(
930936
@staticmethod
931937
def downsample(mask: torch.FloatTensor, batch_size: int, num_queries: int, value_embed_dim: int):
932938
"""
933-
Downsamples the provided mask tensor to match the expected dimensions for scaled dot-product attention.
934-
If the aspect ratio of the mask does not match the aspect ratio of the output image, a warning is issued.
939+
Downsamples the provided mask tensor to match the expected dimensions for scaled dot-product attention. If the
940+
aspect ratio of the mask does not match the aspect ratio of the output image, a warning is issued.
935941
936942
Args:
937943
mask (`torch.FloatTensor`):

src/diffusers/loaders/ip_adapter.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ def load_ip_adapter(
6767
- A [torch state
6868
dict](https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict).
6969
subfolder (`str` or `List[str]`):
70-
The subfolder location of a model file within a larger model repository on the Hub or locally.
71-
If a list is passed, it should have the same length as `weight_name`.
70+
The subfolder location of a model file within a larger model repository on the Hub or locally. If a
71+
list is passed, it should have the same length as `weight_name`.
7272
weight_name (`str` or `List[str]`):
7373
The name of the weight file to load. If a list is passed, it should have the same length as
7474
`weight_name`.
7575
image_encoder_folder (`str`, *optional*, defaults to `image_encoder`):
7676
The subfolder location of the image encoder within a larger model repository on the Hub or locally.
77-
Pass `None` to not load the image encoder. If the image encoder is located in a folder inside `subfolder`,
78-
you only need to pass the name of the folder that contains image encoder weights, e.g. `image_encoder_folder="image_encoder"`.
79-
If the image encoder is located in a folder other than `subfolder`, you should pass the path to the folder that contains image encoder weights,
80-
for example, `image_encoder_folder="different_subfolder/image_encoder"`.
77+
Pass `None` to not load the image encoder. If the image encoder is located in a folder inside
78+
`subfolder`, you only need to pass the name of the folder that contains image encoder weights, e.g.
79+
`image_encoder_folder="image_encoder"`. If the image encoder is located in a folder other than
80+
`subfolder`, you should pass the path to the folder that contains image encoder weights, for example,
81+
`image_encoder_folder="different_subfolder/image_encoder"`.
8182
cache_dir (`Union[str, os.PathLike]`, *optional*):
8283
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
8384
is not used.

src/diffusers/loaders/peft.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
class PeftAdapterMixin:
2121
"""
2222
A class containing all functions for loading and using adapters weights that are supported in PEFT library. For
23-
more details about adapters and injecting them in a transformer-based model, check out the PEFT [documentation](https://huggingface.co/docs/peft/index).
23+
more details about adapters and injecting them in a transformer-based model, check out the PEFT
24+
[documentation](https://huggingface.co/docs/peft/index).
2425
2526
Install the latest version of PEFT, and use this mixin to:
2627
@@ -143,8 +144,8 @@ def disable_adapters(self) -> None:
143144

144145
def enable_adapters(self) -> None:
145146
"""
146-
Enable adapters that are attached to the model. The model uses `self.active_adapters()` to retrieve the
147-
list of adapters to enable.
147+
Enable adapters that are attached to the model. The model uses `self.active_adapters()` to retrieve the list of
148+
adapters to enable.
148149
149150
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
150151
[documentation](https://huggingface.co/docs/peft).

src/diffusers/loaders/single_file.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,24 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
198198
model_type (`str`, *optional*):
199199
The type of model to load. If not provided, the model type will be inferred from the checkpoint file.
200200
image_size (`int`, *optional*):
201-
The size of the image output. It's used to configure the `sample_size` parameter of the UNet and VAE model.
201+
The size of the image output. It's used to configure the `sample_size` parameter of the UNet and VAE
202+
model.
202203
load_safety_checker (`bool`, *optional*, defaults to `False`):
203-
Whether to load the safety checker model or not. By default, the safety checker is not loaded unless a `safety_checker` component is passed to the `kwargs`.
204+
Whether to load the safety checker model or not. By default, the safety checker is not loaded unless a
205+
`safety_checker` component is passed to the `kwargs`.
204206
num_in_channels (`int`, *optional*):
205-
Specify the number of input channels for the UNet model. Read more about how to configure UNet model with this parameter
207+
Specify the number of input channels for the UNet model. Read more about how to configure UNet model
208+
with this parameter
206209
[here](https://huggingface.co/docs/diffusers/training/adapt_a_model#configure-unet2dconditionmodel-parameters).
207210
scaling_factor (`float`, *optional*):
208-
The scaling factor to use for the VAE model. If not provided, it is inferred from the config file first.
209-
If the scaling factor is not found in the config file, the default value 0.18215 is used.
211+
The scaling factor to use for the VAE model. If not provided, it is inferred from the config file
212+
first. If the scaling factor is not found in the config file, the default value 0.18215 is used.
210213
scheduler_type (`str`, *optional*):
211-
The type of scheduler to load. If not provided, the scheduler type will be inferred from the checkpoint file.
214+
The type of scheduler to load. If not provided, the scheduler type will be inferred from the checkpoint
215+
file.
212216
prediction_type (`str`, *optional*):
213-
The type of prediction to load. If not provided, the prediction type will be inferred from the checkpoint file.
217+
The type of prediction to load. If not provided, the prediction type will be inferred from the
218+
checkpoint file.
214219
kwargs (remaining dictionary of keyword arguments, *optional*):
215220
Can be used to overwrite load and saveable variables (the pipeline components of the specific pipeline
216221
class). The overwritten components are passed directly to the pipelines `__init__` method. See example

src/diffusers/loaders/textual_inversion.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -487,20 +487,35 @@ def unload_textual_inversion(
487487
488488
# Example 3: unload from SDXL
489489
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
490-
embedding_path = hf_hub_download(repo_id="linoyts/web_y2k", filename="web_y2k_emb.safetensors", repo_type="model")
490+
embedding_path = hf_hub_download(
491+
repo_id="linoyts/web_y2k", filename="web_y2k_emb.safetensors", repo_type="model"
492+
)
491493
492494
# load embeddings to the text encoders
493495
state_dict = load_file(embedding_path)
494496
495497
# load embeddings of text_encoder 1 (CLIP ViT-L/14)
496-
pipeline.load_textual_inversion(state_dict["clip_l"], token=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder, tokenizer=pipeline.tokenizer)
498+
pipeline.load_textual_inversion(
499+
state_dict["clip_l"],
500+
token=["<s0>", "<s1>"],
501+
text_encoder=pipeline.text_encoder,
502+
tokenizer=pipeline.tokenizer,
503+
)
497504
# load embeddings of text_encoder 2 (CLIP ViT-G/14)
498-
pipeline.load_textual_inversion(state_dict["clip_g"], token=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder_2, tokenizer=pipeline.tokenizer_2)
505+
pipeline.load_textual_inversion(
506+
state_dict["clip_g"],
507+
token=["<s0>", "<s1>"],
508+
text_encoder=pipeline.text_encoder_2,
509+
tokenizer=pipeline.tokenizer_2,
510+
)
499511
500512
# Unload explicitly from both text encoders abd tokenizers
501-
pipeline.unload_textual_inversion(tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder, tokenizer=pipeline.tokenizer)
502-
pipeline.unload_textual_inversion(tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder_2, tokenizer=pipeline.tokenizer_2)
503-
513+
pipeline.unload_textual_inversion(
514+
tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder, tokenizer=pipeline.tokenizer
515+
)
516+
pipeline.unload_textual_inversion(
517+
tokens=["<s0>", "<s1>"], text_encoder=pipeline.text_encoder_2, tokenizer=pipeline.tokenizer_2
518+
)
504519
```
505520
"""
506521

src/diffusers/loaders/unet_loader_utils.py

+14-27
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,24 @@ def _maybe_expand_lora_scales_for_one_adapter(
7474
7575
E.g. turns
7676
```python
77-
scales = {
78-
'down': 2,
79-
'mid': 3,
80-
'up': {
81-
'block_0': 4,
82-
'block_1': [5, 6, 7]
83-
}
84-
}
85-
blocks_with_transformer = {
86-
'down': [1,2],
87-
'up': [0,1]
88-
}
89-
transformer_per_block = {
90-
'down': 2,
91-
'up': 3
92-
}
77+
scales = {"down": 2, "mid": 3, "up": {"block_0": 4, "block_1": [5, 6, 7]}}
78+
blocks_with_transformer = {"down": [1, 2], "up": [0, 1]}
79+
transformer_per_block = {"down": 2, "up": 3}
9380
```
9481
into
9582
```python
9683
{
97-
'down.block_1.0': 2,
98-
'down.block_1.1': 2,
99-
'down.block_2.0': 2,
100-
'down.block_2.1': 2,
101-
'mid': 3,
102-
'up.block_0.0': 4,
103-
'up.block_0.1': 4,
104-
'up.block_0.2': 4,
105-
'up.block_1.0': 5,
106-
'up.block_1.1': 6,
107-
'up.block_1.2': 7,
84+
"down.block_1.0": 2,
85+
"down.block_1.1": 2,
86+
"down.block_2.0": 2,
87+
"down.block_2.1": 2,
88+
"mid": 3,
89+
"up.block_0.0": 4,
90+
"up.block_0.1": 4,
91+
"up.block_0.2": 4,
92+
"up.block_1.0": 5,
93+
"up.block_1.1": 6,
94+
"up.block_1.2": 7,
10895
}
10996
```
11097
"""

src/diffusers/models/attention_processor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1298,9 +1298,9 @@ def __call__(
12981298

12991299
class FusedAttnProcessor2_0:
13001300
r"""
1301-
Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0).
1302-
It uses fused projection layers. For self-attention modules, all projection matrices (i.e., query,
1303-
key, value) are fused. For cross-attention modules, key and value projection matrices are fused.
1301+
Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0). It uses
1302+
fused projection layers. For self-attention modules, all projection matrices (i.e., query, key, value) are fused.
1303+
For cross-attention modules, key and value projection matrices are fused.
13041304
13051305
<Tip warning={true}>
13061306

0 commit comments

Comments
 (0)