Skip to content
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

Refactor diffusers tasks #1947

Merged
merged 23 commits into from
Jul 16, 2024
Merged

Refactor diffusers tasks #1947

merged 23 commits into from
Jul 16, 2024

Conversation

IlyasMoutawwakil
Copy link
Member

@IlyasMoutawwakil IlyasMoutawwakil commented Jul 8, 2024

What does this PR do?

This PR refactors "stable-diffusion" and "stable-diffusion-xl" into "text-to-image", "image-to-image" and "inpainting" tasks.
"stable-diffusion" and "stable-diffusion-xl" become model types in this case, with extensibility to other model types like "lcm" (latent consistency, control net, etc).
This PR also adds new class variables to the TasksManager mostly to create a mapping from tasks to model types to model classes. Which is used to infer things like task and model type from model or model class (e.g. text-to-image and stable-diffusion from StableDiffusionPipeline).

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Who can review?

… into "text-to-image", "image-to-image" and "inpainting"
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment on lines +224 to +229
if task in ["stable-diffusion", "stable-diffusion-xl"]:
logger.warning(
f"The task `{task}` is deprecated and will be removed in a future release of Optimum. "
"Please use one of the following tasks instead: `text-to-image`, `image-to-image`, `inpainting`."
)

Copy link
Member Author

Choose a reason for hiding this comment

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

We warn and then it gets replaced with text-to-image by default. This is the same as the previous behavior for stable-diffusion but not stable-diffusion-xl as the model loader in that case used to be an image-to-image pipeline.

Copy link
Collaborator

Choose a reason for hiding this comment

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

_optional_components was introduced in diffusers v0.22.0 https://github.com/huggingface/diffusers/blob/v0.22.0/src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py#L144 for StableDiffusionXLPipeline (not the case for StableDiffusionXLImg2ImgPipeline). I think we might need to upgrade DIFFUSERS_MINIMUM_VERSION as well to avoid any potential issue when loading a SDXL model with missing optional components

@@ -260,6 +280,9 @@ class TasksManager:
"vision2seq-lm": "image-to-text",
"zero-shot-classification": "text-classification",
"image-feature-extraction": "feature-extraction",
# for backward compatibility
"stable-diffusion": "text-to-image",
"stable-diffusion-xl": "text-to-image",
Copy link
Member Author

Choose a reason for hiding this comment

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

should this be image-to-image for stable-diffusion-xl ? I don't think it matters

Comment on lines 161 to 166
# Torch model mappings
_TRANSFORMERS_TASKS_TO_MODEL_MAPPINGS = {}
_DIFFUSERS_TASKS_TO_PIPELINE_MAPPINGS = {}

# TF model mappings
_TRANSFORMERS_TASKS_TO_TF_MODEL_MAPPINGS = {}
Copy link
Member Author

Choose a reason for hiding this comment

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

uses the mappings from diffusers and transformers to construct a task->model_type->model_class

Comment on lines 296 to 300
for task_name in _DIFFUSERS_TASKS_TO_PIPELINE_MAPPINGS:
pipeline_mapping = _DIFFUSERS_TASKS_TO_PIPELINE_MAPPINGS[task_name]
_DIFFUSERS_TASKS_TO_PIPELINE_MAPPINGS[task_name] = {
pipeline_name: pipeline_class.__name__ for pipeline_name, pipeline_class in pipeline_mapping.items()
}
Copy link
Member Author

Choose a reason for hiding this comment

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

transforming classes into class names to conform with transformers mappings

Comment on lines +95 to +103
is_stable_diffusion = isinstance(
pipeline, (StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline)
)
is_stable_diffusion_xl = isinstance(
pipeline, (StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline, StableDiffusionXLPipeline)
)
is_latent_consistency_model = isinstance(
pipeline, (LatentConsistencyModelPipeline, LatentConsistencyModelImg2ImgPipeline)
)
Copy link
Member Author

Choose a reason for hiding this comment

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

could be simplified using an _infer_model_type_from_model_or_model_class method and _DIFFUSERS_TASKS_TO_PIPELINE_MAPPINGS

Copy link
Contributor

@fxmarty fxmarty left a comment

Choose a reason for hiding this comment

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

I left a few comments, great work!

optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
optimum/exporters/tasks.py Show resolved Hide resolved
optimum/exporters/tasks.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@echarlaix echarlaix left a comment

Choose a reason for hiding this comment

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

Thanks a lot @IlyasMoutawwakil

Comment on lines +224 to +229
if task in ["stable-diffusion", "stable-diffusion-xl"]:
logger.warning(
f"The task `{task}` is deprecated and will be removed in a future release of Optimum. "
"Please use one of the following tasks instead: `text-to-image`, `image-to-image`, `inpainting`."
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

_optional_components was introduced in diffusers v0.22.0 https://github.com/huggingface/diffusers/blob/v0.22.0/src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py#L144 for StableDiffusionXLPipeline (not the case for StableDiffusionXLImg2ImgPipeline). I think we might need to upgrade DIFFUSERS_MINIMUM_VERSION as well to avoid any potential issue when loading a SDXL model with missing optional components

optimum/exporters/tasks.py Show resolved Hide resolved
optimum/exporters/tasks.py Show resolved Hide resolved
@echarlaix echarlaix merged commit ab4341b into main Jul 16, 2024
60 of 64 checks passed
@echarlaix echarlaix deleted the auto-diffusion branch July 16, 2024 08:52
@IlyasMoutawwakil IlyasMoutawwakil restored the auto-diffusion branch August 26, 2024 09:26
@echarlaix echarlaix deleted the auto-diffusion branch August 29, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants