Skip to content

Commit 6b04d61

Browse files
authored
[Styling] stylify using ruff (huggingface#5841)
* ruff format * not need to use doc-builder's black styling as the doc is styled in ruff * make fix-copies * comment * use run_ruff
1 parent 9c7f7fc commit 6b04d61

134 files changed

Lines changed: 430 additions & 297 deletions

File tree

Some content is hidden

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

.github/workflows/pr_quality.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ jobs:
2727
pip install .[quality]
2828
- name: Check quality
2929
run: |
30-
black --check examples tests src utils scripts
31-
ruff examples tests src utils scripts
32-
doc-builder style src/diffusers docs/source --max_len 119 --check_only --path_to_docs docs/source
30+
ruff check examples tests src utils scripts
31+
ruff format examples tests src utils scripts --check
3332
3433
check_repository_consistency:
3534
runs-on: ubuntu-latest

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ Diffusers has grown a lot. Here is the command for it:
410410
$ make test
411411
```
412412

413-
🧨 Diffusers relies on `black` and `isort` to format its source code
413+
🧨 Diffusers relies on `ruff` and `isort` to format its source code
414414
consistently. After you make changes, apply automatic style corrections and code verifications
415415
that can't be automated in one go with:
416416

Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ modified_only_fixup:
99
$(eval modified_py_files := $(shell python utils/get_modified_files.py $(check_dirs)))
1010
@if test -n "$(modified_py_files)"; then \
1111
echo "Checking/fixing $(modified_py_files)"; \
12-
black $(modified_py_files); \
13-
ruff $(modified_py_files); \
12+
ruff check $(modified_py_files) --fix; \
13+
ruff format $(modified_py_files);\
1414
else \
1515
echo "No library .py files were modified"; \
1616
fi
@@ -40,23 +40,21 @@ repo-consistency:
4040
# this target runs checks on all files
4141

4242
quality:
43-
black --check $(check_dirs)
44-
ruff $(check_dirs)
45-
doc-builder style src/diffusers docs/source --max_len 119 --check_only --path_to_docs docs/source
43+
ruff check $(check_dirs) setup.py
44+
ruff format --check $(check_dirs) setup.py
4645
python utils/check_doc_toc.py
4746

4847
# Format source code automatically and check is there are any problems left that need manual fixing
4948

5049
extra_style_checks:
5150
python utils/custom_init_isort.py
52-
doc-builder style src/diffusers docs/source --max_len 119 --path_to_docs docs/source
5351
python utils/check_doc_toc.py --fix_and_overwrite
5452

5553
# this target runs checks on all files and potentially modifies some of them
5654

5755
style:
58-
black $(check_dirs)
59-
ruff $(check_dirs) --fix
56+
ruff check $(check_dirs) setup.py --fix
57+
ruff format $(check_dirs) setup.py
6058
${MAKE} autogenerate_code
6159
${MAKE} extra_style_checks
6260

examples/community/composable_stable_diffusion.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class ComposableStableDiffusionPipeline(DiffusionPipeline):
6565
feature_extractor ([`CLIPImageProcessor`]):
6666
Model that extracts features from generated images to be used as inputs for the `safety_checker`.
6767
"""
68+
6869
_optional_components = ["safety_checker", "feature_extractor"]
6970

7071
def __init__(

examples/community/latent_consistency_img2img.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ def __init__(
564564
self.betas = torch.linspace(beta_start, beta_end, num_train_timesteps, dtype=torch.float32)
565565
elif beta_schedule == "scaled_linear":
566566
# this schedule is very specific to the latent diffusion model.
567-
self.betas = (
568-
torch.linspace(beta_start**0.5, beta_end**0.5, num_train_timesteps, dtype=torch.float32) ** 2
569-
)
567+
self.betas = torch.linspace(beta_start**0.5, beta_end**0.5, num_train_timesteps, dtype=torch.float32) ** 2
570568
elif beta_schedule == "squaredcos_cap_v2":
571569
# Glide cosine schedule
572570
self.betas = betas_for_alpha_bar(num_train_timesteps)

examples/community/latent_consistency_txt2img.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,7 @@ def __init__(
469469
self.betas = torch.linspace(beta_start, beta_end, num_train_timesteps, dtype=torch.float32)
470470
elif beta_schedule == "scaled_linear":
471471
# this schedule is very specific to the latent diffusion model.
472-
self.betas = (
473-
torch.linspace(beta_start**0.5, beta_end**0.5, num_train_timesteps, dtype=torch.float32) ** 2
474-
)
472+
self.betas = torch.linspace(beta_start**0.5, beta_end**0.5, num_train_timesteps, dtype=torch.float32) ** 2
475473
elif beta_schedule == "squaredcos_cap_v2":
476474
# Glide cosine schedule
477475
self.betas = betas_for_alpha_bar(num_train_timesteps)

examples/community/lpw_stable_diffusion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def parse_prompt_attention(text):
5656
(abc) - increases attention to abc by a multiplier of 1.1
5757
(abc:3.12) - increases attention to abc by a multiplier of 3.12
5858
[abc] - decreases attention to abc by a multiplier of 1.1
59-
\( - literal character '('
60-
\[ - literal character '['
61-
\) - literal character ')'
62-
\] - literal character ']'
59+
\\( - literal character '('
60+
\\[ - literal character '['
61+
\\) - literal character ')'
62+
\\] - literal character ']'
6363
\\ - literal character '\'
6464
anything else - just text
6565
>>> parse_prompt_attention('normal text')
@@ -68,7 +68,7 @@ def parse_prompt_attention(text):
6868
[['an ', 1.0], ['important', 1.1], [' word', 1.0]]
6969
>>> parse_prompt_attention('(unbalanced')
7070
[['unbalanced', 1.1]]
71-
>>> parse_prompt_attention('\(literal\]')
71+
>>> parse_prompt_attention('\\(literal\\]')
7272
[['(literal]', 1.0]]
7373
>>> parse_prompt_attention('(unnecessary)(parens)')
7474
[['unnecessaryparens', 1.1]]

examples/community/lpw_stable_diffusion_onnx.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ def parse_prompt_attention(text):
8282
(abc) - increases attention to abc by a multiplier of 1.1
8383
(abc:3.12) - increases attention to abc by a multiplier of 3.12
8484
[abc] - decreases attention to abc by a multiplier of 1.1
85-
\( - literal character '('
86-
\[ - literal character '['
87-
\) - literal character ')'
88-
\] - literal character ']'
85+
\\( - literal character '('
86+
\\[ - literal character '['
87+
\\) - literal character ')'
88+
\\] - literal character ']'
8989
\\ - literal character '\'
9090
anything else - just text
9191
>>> parse_prompt_attention('normal text')
@@ -94,7 +94,7 @@ def parse_prompt_attention(text):
9494
[['an ', 1.0], ['important', 1.1], [' word', 1.0]]
9595
>>> parse_prompt_attention('(unbalanced')
9696
[['unbalanced', 1.1]]
97-
>>> parse_prompt_attention('\(literal\]')
97+
>>> parse_prompt_attention('\\(literal\\]')
9898
[['(literal]', 1.0]]
9999
>>> parse_prompt_attention('(unnecessary)(parens)')
100100
[['unnecessaryparens', 1.1]]
@@ -433,6 +433,7 @@ class OnnxStableDiffusionLongPromptWeightingPipeline(OnnxStableDiffusionPipeline
433433
This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
434434
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)
435435
"""
436+
436437
if version.parse(version.parse(diffusers.__version__).base_version) >= version.parse("0.9.0"):
437438

438439
def __init__(

examples/community/lpw_stable_diffusion_xl.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def parse_prompt_attention(text):
4646
(abc) - increases attention to abc by a multiplier of 1.1
4747
(abc:3.12) - increases attention to abc by a multiplier of 3.12
4848
[abc] - decreases attention to abc by a multiplier of 1.1
49-
\( - literal character '('
50-
\[ - literal character '['
51-
\) - literal character ')'
52-
\] - literal character ']'
49+
\\( - literal character '('
50+
\\[ - literal character '['
51+
\\) - literal character ')'
52+
\\] - literal character ']'
5353
\\ - literal character '\'
5454
anything else - just text
5555
@@ -59,7 +59,7 @@ def parse_prompt_attention(text):
5959
[['an ', 1.0], ['important', 1.1], [' word', 1.0]]
6060
>>> parse_prompt_attention('(unbalanced')
6161
[['unbalanced', 1.1]]
62-
>>> parse_prompt_attention('\(literal\]')
62+
>>> parse_prompt_attention('\\(literal\\]')
6363
[['(literal]', 1.0]]
6464
>>> parse_prompt_attention('(unnecessary)(parens)')
6565
[['unnecessaryparens', 1.1]]

examples/community/magic_mix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ def __call__(
127127
timesteps=t,
128128
)
129129

130-
input = (mix_factor * latents) + (
131-
1 - mix_factor
132-
) * orig_latents # interpolating between layout noise and conditionally generated noise to preserve layout sematics
130+
input = (
131+
(mix_factor * latents) + (1 - mix_factor) * orig_latents
132+
) # interpolating between layout noise and conditionally generated noise to preserve layout sematics
133133
input = torch.cat([input] * 2)
134134

135135
else: # content generation phase

0 commit comments

Comments
 (0)