Repository: huggingface/diffusers
File: tests/lora/utils.py (Python)

Code before:

        with tempfile.TemporaryDirectory() as tmpdirname:
            modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True)
            lora_state_dicts = self._get_lora_state_dicts(modules_to_save)
            self.pipeline_class.save_lora_weights(
                save_directory=tmpdirname, safe_serialization=True, **lora_state_dicts
            )

            components, _, _ = self.get_dummy_components()
            pipe = self.pipeline_class(**components)
            pipe.to(torch_device)

            denoiser = pipe.transformer if self.unet_kwargs is None else pipe.unet

            # Enable Group Offloading (leaf_level)
            apply_group_offloading(
                denoiser,
                onload_device=torch_device,
                offload_device="cpu",
                offload_type="leaf_level",
            )

            pipe.load_lora_weights(tmpdirname, adapter_name="default")
            pipe(**inputs, generator=torch.manual_seed(0))
            # Delete the adapter
            pipe.delete_adapters("default")
            pipe(**inputs, generator=torch.manual_seed(0))

Review comment (suggestion):
We can make this test tighter by also adding some assertions:

```diff
diff --git a/tests/lora/utils.py b/tests/lora/utils.py
index efe68dd3c..7e19aac29 100644
--- a/tests/lora/utils.py
+++ b/tests/lora/utils.py
@@ -2403,7 +2403,9 @@ class PeftLoraLoaderMixinTests:
             )
 
             pipe.load_lora_weights(tmpdirname, adapter_name="default")
-            pipe(**inputs, generator=torch.manual_seed(0))
+            out_lora = pipe(**inputs, generator=torch.manual_seed(0))[0]
+            
             # Delete the adapter
             pipe.delete_adapters("default")
-            pipe(**inputs, generator=torch.manual_seed(0))
\ No newline at end of file
+            out_no_lora = pipe(**inputs, generator=torch.manual_seed(0))[0]
+            self.assertFalse(np.allclose(out_lora, out_no_lora, atol=1e-3, rtol=1e-3))
\ No newline at end of file

```

Code after:

        try:
            with tempfile.TemporaryDirectory() as tmpdirname:
                modules_to_save = self._get_modules_to_save(pipe, has_denoiser=True)
                lora_state_dicts = self._get_lora_state_dicts(modules_to_save)
                self.pipeline_class.save_lora_weights(
                    save_directory=tmpdirname, safe_serialization=True, **lora_state_dicts
                )

                components, _, _ = self.get_dummy_components()
                pipe = self.pipeline_class(**components)
                denoiser = pipe.transformer if self.unet_kwargs is None else pipe.unet
                pipe.to(torch_device)

                # Enable Group Offloading (leaf_level for more granular testing)
                apply_group_offloading(
                    denoiser,
                    onload_device=torch_device,
                    offload_device="cpu",
                    offload_type="leaf_level",
                )

                pipe.load_lora_weights(tmpdirname, adapter_name="default")

                out_lora = pipe(**inputs, generator=torch.manual_seed(0))[0]

                # Delete the adapter
                pipe.delete_adapters("default")

                out_no_lora = pipe(**inputs, generator=torch.manual_seed(0))[0]

                self.assertFalse(np.allclose(out_lora, out_no_lora, atol=1e-3, rtol=1e-3))
        finally:
            # Clean up the hooks to prevent state leak
            if hasattr(denoiser, "_diffusers_hook"):
                denoiser._diffusers_hook.remove_hook(_GROUP_OFFLOADING, recurse=True)