Skip to content

Commit 92a57a8

Browse files
Fix kandinsky remove safety (#4065)
* Finish * make style
1 parent d7280b7 commit 92a57a8

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

src/diffusers/pipelines/kandinsky/pipeline_kandinsky.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,6 @@ def enable_model_cpu_offload(self, gpu_id=0):
273273
for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
274274
_, hook = cpu_offload_with_hook(cpu_offloaded_model, device, prev_module_hook=hook)
275275

276-
if self.safety_checker is not None:
277-
_, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)
278-
279276
# We'll offload the last model manually.
280277
self.final_offload_hook = hook
281278

src/diffusers/pipelines/kandinsky/pipeline_kandinsky_img2img.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ def enable_model_cpu_offload(self, gpu_id=0):
308308
for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
309309
_, hook = cpu_offload_with_hook(cpu_offloaded_model, device, prev_module_hook=hook)
310310

311-
if self.safety_checker is not None:
312-
_, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)
313-
314311
# We'll offload the last model manually.
315312
self.final_offload_hook = hook
316313

src/diffusers/pipelines/kandinsky/pipeline_kandinsky_inpaint.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,6 @@ def enable_model_cpu_offload(self, gpu_id=0):
433433
for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
434434
_, hook = cpu_offload_with_hook(cpu_offloaded_model, device, prev_module_hook=hook)
435435

436-
if self.safety_checker is not None:
437-
_, hook = cpu_offload_with_hook(self.safety_checker, device, prev_module_hook=hook)
438-
439436
# We'll offload the last model manually.
440437
self.final_offload_hook = hook
441438

tests/pipelines/kandinsky/test_kandinsky.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ def test_kandinsky(self):
230230
np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2
231231
), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"
232232

233+
@require_torch_gpu
234+
def test_offloads(self):
235+
pipes = []
236+
components = self.get_dummy_components()
237+
sd_pipe = self.pipeline_class(**components).to(torch_device)
238+
pipes.append(sd_pipe)
239+
240+
components = self.get_dummy_components()
241+
sd_pipe = self.pipeline_class(**components)
242+
sd_pipe.enable_model_cpu_offload()
243+
pipes.append(sd_pipe)
244+
245+
components = self.get_dummy_components()
246+
sd_pipe = self.pipeline_class(**components)
247+
sd_pipe.enable_sequential_cpu_offload()
248+
pipes.append(sd_pipe)
249+
250+
image_slices = []
251+
for pipe in pipes:
252+
inputs = self.get_dummy_inputs(torch_device)
253+
image = pipe(**inputs).images
254+
255+
image_slices.append(image[0, -3:, -3:, -1].flatten())
256+
257+
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3
258+
assert np.abs(image_slices[0] - image_slices[2]).max() < 1e-3
259+
233260

234261
@slow
235262
@require_torch_gpu

tests/pipelines/kandinsky/test_kandinsky_img2img.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,33 @@ def test_kandinsky_img2img(self):
242242
np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2
243243
), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"
244244

245+
@require_torch_gpu
246+
def test_offloads(self):
247+
pipes = []
248+
components = self.get_dummy_components()
249+
sd_pipe = self.pipeline_class(**components).to(torch_device)
250+
pipes.append(sd_pipe)
251+
252+
components = self.get_dummy_components()
253+
sd_pipe = self.pipeline_class(**components)
254+
sd_pipe.enable_model_cpu_offload()
255+
pipes.append(sd_pipe)
256+
257+
components = self.get_dummy_components()
258+
sd_pipe = self.pipeline_class(**components)
259+
sd_pipe.enable_sequential_cpu_offload()
260+
pipes.append(sd_pipe)
261+
262+
image_slices = []
263+
for pipe in pipes:
264+
inputs = self.get_dummy_inputs(torch_device)
265+
image = pipe(**inputs).images
266+
267+
image_slices.append(image[0, -3:, -3:, -1].flatten())
268+
269+
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3
270+
assert np.abs(image_slices[0] - image_slices[2]).max() < 1e-3
271+
245272

246273
@slow
247274
@require_torch_gpu

tests/pipelines/kandinsky/test_kandinsky_inpaint.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,33 @@ def test_kandinsky_inpaint(self):
250250
def test_inference_batch_single_identical(self):
251251
super().test_inference_batch_single_identical(expected_max_diff=3e-3)
252252

253+
@require_torch_gpu
254+
def test_offloads(self):
255+
pipes = []
256+
components = self.get_dummy_components()
257+
sd_pipe = self.pipeline_class(**components).to(torch_device)
258+
pipes.append(sd_pipe)
259+
260+
components = self.get_dummy_components()
261+
sd_pipe = self.pipeline_class(**components)
262+
sd_pipe.enable_model_cpu_offload()
263+
pipes.append(sd_pipe)
264+
265+
components = self.get_dummy_components()
266+
sd_pipe = self.pipeline_class(**components)
267+
sd_pipe.enable_sequential_cpu_offload()
268+
pipes.append(sd_pipe)
269+
270+
image_slices = []
271+
for pipe in pipes:
272+
inputs = self.get_dummy_inputs(torch_device)
273+
image = pipe(**inputs).images
274+
275+
image_slices.append(image[0, -3:, -3:, -1].flatten())
276+
277+
assert np.abs(image_slices[0] - image_slices[1]).max() < 1e-3
278+
assert np.abs(image_slices[0] - image_slices[2]).max() < 1e-3
279+
253280

254281
@slow
255282
@require_torch_gpu

0 commit comments

Comments
 (0)