Skip to content

Commit c12a05b

Browse files
committed
update to to not assume pipeline has hf_device_map
1 parent 2e0f5c8 commit c12a05b

File tree

3 files changed

+86
-78
lines changed

3 files changed

+86
-78
lines changed

src/diffusers/pipelines/modular_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def module_is_offloaded(module):
966966
"It seems like you have activated sequential model offloading by calling `enable_sequential_cpu_offload`, but are now attempting to move the pipeline to GPU. This is not compatible with offloading. Please, move your pipeline `.to('cpu')` or consider removing the move altogether if you use sequential offloading."
967967
)
968968

969-
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1
969+
is_pipeline_device_mapped = hasattr(self, "hf_device_map") and self.hf_device_map is not None and len(self.hf_device_map) > 1
970970
if is_pipeline_device_mapped:
971971
raise ValueError(
972972
"It seems like you have activated a device mapping strategy on the pipeline which doesn't allow explicit device placement using `to()`. You can call `reset_device_map()` first and then call `to()`."

src/diffusers/pipelines/pipeline_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def module_is_offloaded(module):
422422
"You are trying to call `.to('cuda')` on a pipeline that has models quantized with `bitsandbytes`. Your current `accelerate` installation does not support it. Please upgrade the installation."
423423
)
424424

425-
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1
425+
is_pipeline_device_mapped = hasattr(self, "hf_device_map") and self.hf_device_map is not None and len(self.hf_device_map) > 1
426426
if is_pipeline_device_mapped:
427427
raise ValueError(
428428
"It seems like you have activated a device mapping strategy on the pipeline which doesn't allow explicit device placement using `to()`. You can call `reset_device_map()` first and then call `to()`."
@@ -1030,7 +1030,7 @@ def enable_model_cpu_offload(self, gpu_id: Optional[int] = None, device: Union[t
10301030
The PyTorch device type of the accelerator that shall be used in inference. If not specified, it will
10311031
default to "cuda".
10321032
"""
1033-
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1
1033+
is_pipeline_device_mapped = hasattr(self, "hf_device_map") and self.hf_device_map is not None and len(self.hf_device_map) > 1
10341034
if is_pipeline_device_mapped:
10351035
raise ValueError(
10361036
"It seems like you have activated a device mapping strategy on the pipeline so calling `enable_model_cpu_offload() isn't allowed. You can call `reset_device_map()` first and then call `enable_model_cpu_offload()`."
@@ -1138,7 +1138,7 @@ def enable_sequential_cpu_offload(self, gpu_id: Optional[int] = None, device: Un
11381138
raise ImportError("`enable_sequential_cpu_offload` requires `accelerate v0.14.0` or higher")
11391139
self.remove_all_hooks()
11401140

1141-
is_pipeline_device_mapped = self.hf_device_map is not None and len(self.hf_device_map) > 1
1141+
is_pipeline_device_mapped = hasattr(self, "hf_device_map") and self.hf_device_map is not None and len(self.hf_device_map) > 1
11421142
if is_pipeline_device_mapped:
11431143
raise ValueError(
11441144
"It seems like you have activated a device mapping strategy on the pipeline so calling `enable_sequential_cpu_offload() isn't allowed. You can call `reset_device_map()` first and then call `enable_sequential_cpu_offload()`."

src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_modular.py

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -544,80 +544,88 @@ def __call__(self, pipeline, state: PipelineState) -> PipelineState:
544544
return pipeline, state
545545

546546

547-
class StableDiffusionXLInpaintPrepareLatentsStep(PipelineBlock):
548-
expected_components = ["vae", "scheduler"]
549-
model_name = "stable-diffusion-xl"
550-
551-
@property
552-
def inputs(self) -> List[Tuple[str, Any]]:
553-
return [
554-
("height", None),
555-
("width", None),
556-
("generator", None),
557-
("latents", None),
558-
("num_images_per_prompt", 1),
559-
("device", None),
560-
("dtype", None),
561-
("image", None),
562-
("denoising_start", None),
563-
]
564-
565-
@property
566-
def intermediates_inputs(self) -> List[str]:
567-
return ["batch_size", "latent_timestep", "prompt_embeds"]
568-
569-
@property
570-
def intermediates_outputs(self) -> List[str]:
571-
return ["latents"]
572-
573-
def __init__(self):
574-
super().__init__()
575-
self.auxiliaries["image_processor"] = VaeImageProcessor()
576-
self.components["vae"] = None
577-
self.components["scheduler"] = None
578-
579-
@torch.no_grad()
580-
def __call__(self, pipeline: DiffusionPipeline, state: PipelineState) -> PipelineState:
581-
latents = state.get_input("latents")
582-
num_images_per_prompt = state.get_input("num_images_per_prompt")
583-
generator = state.get_input("generator")
584-
device = state.get_input("device")
585-
dtype = state.get_input("dtype")
586-
587-
# image to image only
588-
image = state.get_input("image")
589-
denoising_start = state.get_input("denoising_start")
590-
591-
batch_size = state.get_intermediate("batch_size")
592-
prompt_embeds = state.get_intermediate("prompt_embeds")
593-
# image to image only
594-
latent_timestep = state.get_intermediate("latent_timestep")
595-
596-
if dtype is None and prompt_embeds is not None:
597-
dtype = prompt_embeds.dtype
598-
elif dtype is None:
599-
dtype = pipeline.vae.dtype
600-
601-
if device is None:
602-
device = pipeline._execution_device
603-
604-
image = pipeline.image_processor.preprocess(image)
605-
add_noise = True if denoising_start is None else False
606-
if latents is None:
607-
latents = pipeline.prepare_latents_img2img(
608-
image,
609-
latent_timestep,
610-
batch_size,
611-
num_images_per_prompt,
612-
dtype,
613-
device,
614-
generator,
615-
add_noise,
616-
)
617-
618-
state.add_intermediate("latents", latents)
619-
620-
return pipeline, state
547+
# class StableDiffusionXLInpaintPrepareLatentsStep(PipelineBlock):
548+
# expected_components = ["vae", "scheduler"]
549+
# model_name = "stable-diffusion-xl"
550+
551+
# @property
552+
# def inputs(self) -> List[Tuple[str, Any]]:
553+
# return [
554+
# ("height", None),
555+
# ("width", None),
556+
# ("generator", None),
557+
# ("latents", None),
558+
# ("num_images_per_prompt", 1),
559+
# ("device", None),
560+
# ("dtype", None),
561+
# ("image", None),
562+
# ("denoising_start", None),
563+
# ]
564+
565+
# @property
566+
# def intermediates_inputs(self) -> List[str]:
567+
# return ["batch_size", "latent_timestep", "prompt_embeds"]
568+
569+
# @property
570+
# def intermediates_outputs(self) -> List[str]:
571+
# return ["latents"]
572+
573+
# def __init__(self):
574+
# super().__init__()
575+
# self.auxiliaries["image_processor"] = VaeImageProcessor()
576+
# self.components["vae"] = None
577+
# self.components["scheduler"] = None
578+
579+
# @torch.no_grad()
580+
# def __call__(self, pipeline: DiffusionPipeline, state: PipelineState) -> PipelineState:
581+
# latents = state.get_input("latents")
582+
# num_images_per_prompt = state.get_input("num_images_per_prompt")
583+
# generator = state.get_input("generator")
584+
# device = state.get_input("device")
585+
# dtype = state.get_input("dtype")
586+
587+
# # image to image only
588+
# image = state.get_input("image")
589+
# denoising_start = state.get_input("denoising_start")
590+
591+
# # inpaint only
592+
# strength = state.get_input("strength")
593+
# padding_mask_crop = state.get_input("padding_mask_crop")
594+
# mask_image = state.get_input("mask_image")
595+
# masked_image_latents = state.get_input("masked_image_latents")
596+
597+
598+
599+
# batch_size = state.get_intermediate("batch_size")
600+
# prompt_embeds = state.get_intermediate("prompt_embeds")
601+
# # image to image only
602+
# latent_timestep = state.get_intermediate("latent_timestep")
603+
604+
# if dtype is None and prompt_embeds is not None:
605+
# dtype = prompt_embeds.dtype
606+
# elif dtype is None:
607+
# dtype = pipeline.vae.dtype
608+
609+
# if device is None:
610+
# device = pipeline._execution_device
611+
612+
# image = pipeline.image_processor.preprocess(image)
613+
# add_noise = True if denoising_start is None else False
614+
# if latents is None:
615+
# latents = pipeline.prepare_latents_img2img(
616+
# image,
617+
# latent_timestep,
618+
# batch_size,
619+
# num_images_per_prompt,
620+
# dtype,
621+
# device,
622+
# generator,
623+
# add_noise,
624+
# )
625+
626+
# state.add_intermediate("latents", latents)
627+
628+
# return pipeline, state
621629

622630

623631
class StableDiffusionXLImg2ImgPrepareLatentsStep(PipelineBlock):

0 commit comments

Comments
 (0)