Skip to content

Conversation

davidberenstein1957
Copy link
Contributor

This update enhances support for Pruna AI, providing users with tailored code snippets for model integrations with Transformers and Diffusers.

  • Introduced a new library entry for Pruna AI in model-libraries.
  • Added main entry point and specific snippet generation functions for diffusers and transformers models.
  • Cleaned up whitespace inconsistencies in existing snippets.

TLDR: Pruna API normally mimics the Transformers and Diffusers API, so we can use PrunaModel.from_pretrained on top of pipelines or specific models. We re-use the underlying snippets for both the library and do some greedy replacements of certain part of the code snippets.

example

import torch
from diffusers import FluxFillPipeline
from diffusers.utils import load_image

image = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup.png")
mask = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup_mask.png")

pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
image = pipe(
    prompt="a white paper cup",
    image=image,
    mask_image=mask,
    height=1632,
    width=1232,
    guidance_scale=30,
    num_inference_steps=50,
    max_sequence_length=512,
    generator=torch.Generator("cpu").manual_seed(0)
).images[0]
image.save(f"flux-fill-dev.png")

becomes

import torch
from pruna import PrunaModel
from diffusers.utils import load_image

image = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup.png")
mask = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup_mask.png")

pipe = PrunaModel.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
image = pipe(
    prompt="a white paper cup",
    image=image,
    mask_image=mask,
    height=1632,
    width=1232,
    guidance_scale=30,
    num_inference_steps=50,
    max_sequence_length=512,
    generator=torch.Generator("cpu").manual_seed(0)
).images[0]
image.save(f"flux-fill-dev.png")

- Introduced `pruna` function to generate code snippets based on model library type.
- Added support for `diffusers` and `transformers` libraries with specific snippet transformations.
- Included a default snippet for models not matching the specified libraries.
- Updated `MODEL_LIBRARIES_UI_ELEMENTS` to include Pruna AI with relevant repository and documentation links.
- Added a utility function to prepend the PrunaModel import to snippets if not already present.
- Updated the `pruna` function to apply this import check to generated snippets.
- Removed redundant import checks from `pruna_diffusers` and `pruna_transformers` functions for cleaner code.
@davidberenstein1957 davidberenstein1957 changed the title Add Pruna AI library snippets (no formatting) Add Pruna AI library snippets (no formatting changes) Sep 7, 2025
Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

I did not check the diffusers/transformers snippets themselves but the PR looks good. I left a few comments to address but that's all :)

And thanks for opening a separate PR without reformatting!

- Refactored the `pruna` function to use model tags instead of library names for identifying model types.
- Reintroduced the Pruna AI entry in `MODEL_LIBRARIES_UI_ELEMENTS` after its removal, ensuring it includes relevant repository and documentation links.
- Improved code clarity by aligning snippet generation with updated model identification criteria.
Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

Looks good! I'll merge once lint CI is green :)

@Wauplin Wauplin merged commit 4a22043 into huggingface:main Sep 9, 2025
2 of 4 checks passed
@Wauplin
Copy link
Contributor

Wauplin commented Sep 9, 2025

(expect 2-3 days before getting it live on the Hub)

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.

3 participants