Skip to content

Conversation

@SahilCarterr
Copy link
Contributor

What does this PR do?

Fixes #10707 Added Self in from_pretrained method so inference will correctly recognize pipeline

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. @yiyixuxu

Before Fix:

Before this fix pyright inferred the return type as Any, leading to incorrect type checking:

import torch
from diffusers.pipelines.flux.pipeline_flux import FluxPipeline
from diffusers.utils.loading_utils import load_image

pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16,
)
reveal_type(pipe)

Output

/content/my_script.py
  /content/my_script.py:13:13 - information: Type of "pipe" is "DiffusionPipeline | Any | Unknown"
0 errors, 0 warnings, 1 information 

After Fix

/content/my_script.py
  /content/my_script.py:13:13 - information: Type of "pipe" is "FluxPipeline"
0 errors, 0 warnings, 1 information 

Added Self in from_pretrained method so  inference will correctly recognize pipeline
@SahilCarterr
Copy link
Contributor Author

@hlky

@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.

Copy link
Contributor

@hlky hlky left a comment

Choose a reason for hiding this comment

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

Thanks @SahilCarterr. I've edited this to use typing-extensions as Self was only added to typing in 3.11. This type annotation is certainly useful as it allows code editors to pick up on a pipeline's functions etc. However I'd like to mention that type correctness is not strictly enforced, we do encourage type hints and welcome PRs that add type hints, but we currently do not expect mypy or similar to pass

@hlky hlky requested review from sayakpaul and yiyixuxu February 4, 2025 08:47
@hlky
Copy link
Contributor

hlky commented Feb 4, 2025

@yiyixuxu @sayakpaul We get typing-extensions from torch and probably some other packages, should be ok to not explicitly add to our own dependencies, WDYT?

@sayakpaul
Copy link
Member

We get typing-extensions from torch and probably some other packages, should be ok to not explicitly add to our own dependencies, WDYT?

We already get it from PyTorch but I am not too sure how our dependency tests will act like with this.

@hlky
Copy link
Contributor

hlky commented Feb 4, 2025

Dependency tests are passing.

It looks like numpy has typing-extensions as a dependency and we require numpy, also flax requires typing-extensions, so this should be covered for all backends and minimal install.

@yiyixuxu yiyixuxu merged commit 5b1dcd1 into huggingface:main Feb 4, 2025
12 checks passed
@SahilCarterr SahilCarterr deleted the fix_from_pret branch February 5, 2025 06:33
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.

Annotate return type of DiffusionPipeline.from_pretrained as Self | None

5 participants