diff --git a/src/diffusers/guiders/perturbed_attention_guidance.py b/src/diffusers/guiders/perturbed_attention_guidance.py index 904d319..30d728b 100644 --- a/src/diffusers/guiders/perturbed_attention_guidance.py +++ b/src/diffusers/guiders/perturbed_attention_guidance.py @@ -116,6 +116,12 @@ def __init__( raise ValueError( "`perturbed_guidance_layers` must be provided if `perturbed_guidance_config` is not specified." ) + if isinstance(perturbed_guidance_layers, int): + perturbed_guidance_layers = [perturbed_guidance_layers] + if not isinstance(perturbed_guidance_layers, list): + raise ValueError( + f"Expected `perturbed_guidance_layers` to be an int or a list of ints, but got {type(perturbed_guidance_layers)}." + ) perturbed_guidance_config = LayerSkipConfig( indices=perturbed_guidance_layers, fqn="auto", diff --git a/tests/guiders/test_perturbed_attention_guidance.py b/tests/guiders/test_perturbed_attention_guidance.py new file mode 100644 index 0000000..8176bc5 --- /dev/null +++ b/tests/guiders/test_perturbed_attention_guidance.py @@ -0,0 +1,35 @@ +# 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.perturbed_attention_guidance import PerturbedAttentionGuidance + + +class TestPerturbedAttentionGuidanceConfig: + def test_perturbed_guidance_layers_int_shorthand_builds_list_indices(self): + guider = PerturbedAttentionGuidance( + guidance_scale=7.5, + perturbed_guidance_layers=7, + perturbed_guidance_scale=2.8, + ) + + assert guider.skip_layer_config[0].indices == [7] + + def test_perturbed_guidance_layers_list_shorthand_builds_list_indices(self): + guider = PerturbedAttentionGuidance( + guidance_scale=7.5, + perturbed_guidance_layers=[7, 8, 9], + perturbed_guidance_scale=2.8, + ) + + assert guider.skip_layer_config[0].indices == [7, 8, 9]