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

Text2ImagePipeline heterogenous compile #1768

Merged

Conversation

RyanMetcalfeInt8
Copy link
Contributor

@RyanMetcalfeInt8 RyanMetcalfeInt8 commented Feb 19, 2025

To simplify creation of a heterogenous stable diffusion txt2image pipeline, this adds a new API to Text2ImagePipeline class:

/**
 * Compiles image generation pipeline for given devices for text encoding, denoising, and vae decoding.
 * @param text_encode_device A device to compile text encoder(s) with
 * @param denoise_device A device to compile denoiser (e.g. UNet, SD3 Transformer, etc.) with
 * @param vae_decode_device A device to compile VAE decoder(s) with
 * @param properties A map of properties which affect models compilation
 * @note If pipeline was compiled before, an exception is thrown.
 */
void compile(const std::string& text_encode_device,
             const std::string& denoise_device,
             const std::string& vae_decode_device,
             const ov::AnyMap& properties = {});

(Need some feedback here.. especially on if we technically need 3 sets of properties.. one per device?)

This API greatly simplifies heterogenous pipeline setup to this:

ov::genai::Text2ImagePipeline pipe(models_path);
pipe.reshape(1, width, height, pipe.get_generation_config().guidance_scale);
pipe.compile(text_encoder_device, unet_device, vae_decoder_device);

And so now with these changes, heterogenous stable diffusion sample can support all variants of stable diffusion (SD1.5, LCM, XL, SD3, etc.) with the same code. With the old method (creating sub-components and assembling pipeline object), it would have been difficult to achieve this.

With that said, this PR is tested and working with the following pipelines (with NPU running denoise):

  • SD1.5 / LCM
  • SDXL

TODO:

  • Add python bindings for the new API
  • Update python heterogenous sample
    FUTURE WORK (outside the scope of this PR):

…ompile flow

This makes the following flow work for Stable Diffusion XL:
ov::genai::Text2ImagePipeline pipe(models_path);
pipe.reshape(1, width, height, pipe.get_generation_config().guidance_scale);
pipe.compile(text_encoder_device, unet_device, vae_decoder_device);

This commit fixes a couple errors that were present.
1. The original StableDiffusionXLPipeline constructor used in this case
   called StableDiffusionPipeline(pipeline_type, root_dir), which itself
   made a call to initialize_generation_config(). I think the idea here
   is that StableDiffusionXLPipeline's implementation should be called,
   but since at this point the vtables haven't been completed,
   StableDiffusionPipeline's implementation is called, and an exception
   was thrown since it doesn't support the "StableDiffusionXLPipeline"
   class_name. This is kind of deeper problem which probably requires
   more in-depth refactor.. but for now I resolved this issue by
   avoiding that specific construction flow.

2. StableDiffusionXLPipeline's reshape implementation originally used
   batch_size_multiplier (typically set to 2) to reshape the text
   encoders. But in fact these should be fixes at 1.
@RyanMetcalfeInt8 RyanMetcalfeInt8 marked this pull request as draft February 19, 2025 14:35
@github-actions github-actions bot added category: text to image Text 2 image pipeline category: samples GenAI samples category: GenAI C++ API Changes in GenAI C++ public headers labels Feb 19, 2025
@github-actions github-actions bot added the category: Python API Python API for GenAI label Feb 19, 2025
@RyanMetcalfeInt8 RyanMetcalfeInt8 marked this pull request as ready for review February 20, 2025 19:11
@ilya-lavrenov ilya-lavrenov added this to the 2025.1 milestone Feb 21, 2025
@RyanMetcalfeInt8
Copy link
Contributor Author

@ilya-lavrenov -- Thanks for the review! I pushed changes to address your comments.

@ilya-lavrenov
Copy link
Contributor

Please, also ensure that .pyi files are up to date:
{7E2496C8-45C4-4EF4-A084-19BB01BE6393}

Once you built Python API, pyi files in source tree are automatically updated and you can commit those changes.

@RyanMetcalfeInt8
Copy link
Contributor Author

RyanMetcalfeInt8 commented Feb 26, 2025

Once you built Python API, pyi files in source tree are automatically updated and you can commit those changes.

I'm trying to figure out how this pyi generation works... In my case, I don't see the .pyi getting generated. Looking at the CMakeLists.txt, it seems like it only enables this if OpenVINODeveloperPackage_FOUND.

Seems like OpenVINODeveloperPackage is not found in my case (I am setting up OV env. by sourcing nightly package setupvars.bat). Do I need to build OpenVINO from source to get this to work? BTW, I'm on Windows also...

@ilya-lavrenov
Copy link
Contributor

build_jenkins

@ilya-lavrenov ilya-lavrenov merged commit fae9029 into openvinotoolkit:master Feb 26, 2025
62 of 63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: GenAI C++ API Changes in GenAI C++ public headers category: Python API Python API for GenAI category: samples GenAI samples category: text to image Text 2 image pipeline
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants