From de303be49380482b416fb31f923c19902e80f0c1 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 23 Jun 2026 22:14:36 +0000 Subject: [PATCH] Fix LayerSkipConfig shorthand passing int instead of list indices When SkipLayerGuidance or AutoGuidance build LayerSkipConfig from layer index shorthand, pass indices=[layer] instead of a bare int. The int was stored in indices and crashed hook installation with TypeError. Also invoke ruff via python -m in check_copies so the copy checker runs when ruff is not on PATH. Co-authored-by: Simon Lynch --- src/diffusers/guiders/auto_guidance.py | 2 +- src/diffusers/guiders/skip_layer_guidance.py | 2 +- tests/guiders/test_skip_layer_guidance.py | 40 ++++++++++++++++++++ utils/check_copies.py | 3 +- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/guiders/test_skip_layer_guidance.py diff --git a/src/diffusers/guiders/auto_guidance.py b/src/diffusers/guiders/auto_guidance.py index d6b6d3c..f7534da 100644 --- a/src/diffusers/guiders/auto_guidance.py +++ b/src/diffusers/guiders/auto_guidance.py @@ -104,7 +104,7 @@ def __init__( f"Expected `auto_guidance_layers` to be an int or a list of ints, but got {type(auto_guidance_layers)}." ) auto_guidance_config = [ - LayerSkipConfig(layer, fqn="auto", dropout=dropout) for layer in auto_guidance_layers + LayerSkipConfig(indices=[layer], fqn="auto", dropout=dropout) for layer in auto_guidance_layers ] if isinstance(auto_guidance_config, dict): diff --git a/src/diffusers/guiders/skip_layer_guidance.py b/src/diffusers/guiders/skip_layer_guidance.py index cb7e85e..c918bad 100644 --- a/src/diffusers/guiders/skip_layer_guidance.py +++ b/src/diffusers/guiders/skip_layer_guidance.py @@ -136,7 +136,7 @@ def __init__( raise ValueError( f"Expected `skip_layer_guidance_layers` to be an int or a list of ints, but got {type(skip_layer_guidance_layers)}." ) - skip_layer_config = [LayerSkipConfig(layer, fqn="auto") for layer in skip_layer_guidance_layers] + skip_layer_config = [LayerSkipConfig(indices=[layer], fqn="auto") for layer in skip_layer_guidance_layers] if isinstance(skip_layer_config, dict): skip_layer_config = LayerSkipConfig.from_dict(skip_layer_config) diff --git a/tests/guiders/test_skip_layer_guidance.py b/tests/guiders/test_skip_layer_guidance.py new file mode 100644 index 0000000..5753b42 --- /dev/null +++ b/tests/guiders/test_skip_layer_guidance.py @@ -0,0 +1,40 @@ +# Copyright 2025 HuggingFace Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from diffusers.guiders.auto_guidance import AutoGuidance +from diffusers.guiders.skip_layer_guidance import SkipLayerGuidance + + +class TestSkipLayerGuidanceConfig: + def test_skip_layer_guidance_layers_shorthand_builds_list_indices(self): + guider = SkipLayerGuidance( + guidance_scale=7.0, + skip_layer_guidance_layers=[7, 8, 9], + skip_layer_guidance_scale=2.8, + ) + + assert [config.indices for config in guider.skip_layer_config] == [[7], [8], [9]] + for config in guider.skip_layer_config: + assert 7 in config.indices or 8 in config.indices or 9 in config.indices + + def test_auto_guidance_layers_shorthand_builds_list_indices(self): + guider = AutoGuidance( + guidance_scale=7.0, + auto_guidance_layers=[3, 4], + dropout=1.0, + ) + + assert [config.indices for config in guider.auto_guidance_config] == [[3], [4]] + for config in guider.auto_guidance_config: + assert config.indices[0] in config.indices diff --git a/utils/check_copies.py b/utils/check_copies.py index 001366c..338a25d 100644 --- a/utils/check_copies.py +++ b/utils/check_copies.py @@ -18,6 +18,7 @@ import os import re import subprocess +import sys # All paths are set with the intent you should run this script from the root of the repo with the command @@ -94,7 +95,7 @@ def get_indent(code): def run_ruff(code): - command = ["ruff", "format", "-", "--config", "pyproject.toml", "--silent"] + command = [sys.executable, "-m", "ruff", "format", "-", "--config", "pyproject.toml", "--silent"] process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) stdout, _ = process.communicate(input=code.encode()) return stdout.decode()