From 81fa2d688d6a04e834ecf9b12606f95d4308c3ea Mon Sep 17 00:00:00 2001 From: Anton Lozhkov Date: Fri, 18 Nov 2022 15:33:57 +0100 Subject: [PATCH] Avoid nested fix-copies (#1332) * Avoid nested `# Copied from` statements during `make fix-copies` * style --- .../alt_diffusion/pipeline_alt_diffusion_img2img.py | 11 ----------- utils/check_copies.py | 4 ++++ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py b/src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py index fc530603..9a50ef4a 100644 --- a/src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py +++ b/src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py @@ -81,7 +81,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): Model that extracts features from generated images to be used as inputs for the `safety_checker`. """ - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.__init__ def __init__( self, vae: AutoencoderKL, @@ -148,7 +147,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): feature_extractor=feature_extractor, ) - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.enable_attention_slicing def enable_attention_slicing(self, slice_size: Optional[Union[str, int]] = "auto"): r""" Enable sliced attention computation. @@ -168,7 +166,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): slice_size = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(slice_size) - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.disable_attention_slicing def disable_attention_slicing(self): r""" Disable sliced attention computation. If `enable_attention_slicing` was previously invoked, this method will go @@ -177,7 +174,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): # set slice_size = `None` to disable `attention slicing` self.enable_attention_slicing(None) - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.enable_sequential_cpu_offload def enable_sequential_cpu_offload(self, gpu_id=0): r""" Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet, @@ -196,7 +192,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): cpu_offload(cpu_offloaded_model, device) @property - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline._execution_device def _execution_device(self): r""" Returns the device on which the pipeline's models will be executed. After calling @@ -214,7 +209,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): return torch.device(module._hf_hook.execution_device) return self.device - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.enable_xformers_memory_efficient_attention def enable_xformers_memory_efficient_attention(self): r""" Enable memory efficient attention as implemented in xformers. @@ -227,14 +221,12 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): """ self.unet.set_use_memory_efficient_attention_xformers(True) - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.disable_xformers_memory_efficient_attention def disable_xformers_memory_efficient_attention(self): r""" Disable memory efficient attention as implemented in xformers. """ self.unet.set_use_memory_efficient_attention_xformers(False) - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline._encode_prompt def _encode_prompt(self, prompt, device, num_images_per_prompt, do_classifier_free_guidance, negative_prompt): r""" Encodes the prompt into text encoder hidden states. @@ -340,7 +332,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): return text_embeddings - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.run_safety_checker def run_safety_checker(self, image, device, dtype): if self.safety_checker is not None: safety_checker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(device) @@ -351,7 +342,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): has_nsfw_concept = None return image, has_nsfw_concept - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.decode_latents def decode_latents(self, latents): latents = 1 / 0.18215 * latents image = self.vae.decode(latents).sample @@ -360,7 +350,6 @@ class AltDiffusionImg2ImgPipeline(DiffusionPipeline): image = image.cpu().permute(0, 2, 3, 1).float().numpy() return image - # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.AltDiffusionPipeline.prepare_extra_step_kwargs def prepare_extra_step_kwargs(self, generator, eta): # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. diff --git a/utils/check_copies.py b/utils/check_copies.py index 395cefb9..16782397 100644 --- a/utils/check_copies.py +++ b/utils/check_copies.py @@ -153,6 +153,10 @@ def is_copy_consistent(filename, overwrite=False): observed_code_lines = lines[start_index:line_index] observed_code = "".join(observed_code_lines) + # Remove any nested `Copied from` comments to avoid circular copies + theoretical_code = [line for line in theoretical_code.split("\n") if _re_copy_warning.search(line) is None] + theoretical_code = "\n".join(theoretical_code) + # Before comparing, use the `replace_pattern` on the original code. if len(replace_pattern) > 0: patterns = replace_pattern.replace("with", "").split(",")