Skip to content

Created more generic pipeline for text-to-image task #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/sagemaker_huggingface_inference_toolkit/diffusers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,22 @@ def is_diffusers_available():
if is_diffusers_available():
import torch

from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler, StableDiffusionPipeline
from diffusers import DiffusionPipeline


class SMAutoPipelineForText2Image:
class DiffusionPipelineForText2Image:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to change the class name? Looks like it is only used here in this file, so should be safe.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did so as it was related to the import of the class AutoPipelineForText2Image in diffusers not used anymore. Maybe it would make more sense to name it SMDiffusionPipelineForText2Image


def __init__(self, model_dir: str, device: str = None): # needs "cuda" for GPU
self.pipeline = None
dtype = torch.float32
if device == "cuda":
dtype = torch.bfloat16 if is_torch_bf16_gpu_available() else torch.float16
device_map = "auto" if device == "cuda" else None
if torch.cuda.device_count() > 1:
device_map = "balanced"
self.pipeline = DiffusionPipeline.from_pretrained(model_dir, torch_dtype=dtype, device_map=device_map)

self.pipeline = AutoPipelineForText2Image.from_pretrained(model_dir, torch_dtype=dtype, device_map=device_map)
# try to use DPMSolverMultistepScheduler
if isinstance(self.pipeline, StableDiffusionPipeline):
try:
self.pipeline.scheduler = DPMSolverMultistepScheduler.from_config(self.pipeline.scheduler.config)
except Exception:
pass
self.pipeline.to(device)
if not self.pipeline:
self.pipeline = DiffusionPipeline.from_pretrained(model_dir, torch_dtype=dtype).to(device)

def __call__(
self,
Expand All @@ -64,7 +62,7 @@ def __call__(


DIFFUSERS_TASKS = {
"text-to-image": SMAutoPipelineForText2Image,
"text-to-image": DiffusionPipelineForText2Image,
}


Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_diffusers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from transformers.testing_utils import require_torch, slow

from PIL import Image
from sagemaker_huggingface_inference_toolkit.diffusers_utils import SMAutoPipelineForText2Image
from sagemaker_huggingface_inference_toolkit.diffusers_utils import DiffusionPipelineForText2Image
from sagemaker_huggingface_inference_toolkit.transformers_utils import _load_model_from_hub, get_pipeline


Expand All @@ -28,7 +28,7 @@ def test_get_diffusers_pipeline():
tmpdirname,
)
pipe = get_pipeline("text-to-image", -1, storage_dir)
assert isinstance(pipe, SMAutoPipelineForText2Image)
assert isinstance(pipe, DiffusionPipelineForText2Image)


@slow
Expand Down