|
| 1 | +// Copyright (C) 2023-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include "imwrite.hpp" |
| 5 | +#include "openvino/genai/image_generation/text2image_pipeline.hpp" |
| 6 | + |
| 7 | +int32_t main(int32_t argc, char* argv[]) try { |
| 8 | + OPENVINO_ASSERT(argc >= 3 && argc <= 6, |
| 9 | + "Usage: ", |
| 10 | + argv[0], |
| 11 | + " <MODEL_DIR> '<PROMPT>' [ <TXT_ENCODE_DEVICE> <UNET_DEVICE> <VAE_DEVICE> ]"); |
| 12 | + |
| 13 | + const std::string models_path = argv[1], prompt = argv[2]; |
| 14 | + |
| 15 | + std::filesystem::path root_dir = models_path; |
| 16 | + |
| 17 | + const int width = 512; |
| 18 | + const int height = 512; |
| 19 | + const float guidance_scale = 7.5f; |
| 20 | + const int number_of_images_to_generate = 1; |
| 21 | + const int number_of_inference_steps_per_image = 20; |
| 22 | + |
| 23 | + // Set devices to command-line args if specified, otherwise default to CPU. |
| 24 | + // Note that these can be set to CPU, GPU, or NPU. |
| 25 | + const std::string text_encoder_device = (argc > 3) ? argv[3] : "CPU"; |
| 26 | + const std::string unet_device = (argc > 4) ? argv[4] : "CPU"; |
| 27 | + const std::string vae_decoder_device = (argc > 5) ? argv[5] : "CPU"; |
| 28 | + |
| 29 | + std::cout << "text_encoder_device: " << text_encoder_device << std::endl; |
| 30 | + std::cout << "unet_device: " << unet_device << std::endl; |
| 31 | + std::cout << "vae_decoder_device: " << vae_decoder_device << std::endl; |
| 32 | + |
| 33 | + // this is the path to where compiled models will get cached |
| 34 | + // (so that the 'compile' method run much faster 2nd+ time) |
| 35 | + std::string ov_cache_dir = "./cache"; |
| 36 | + |
| 37 | + // |
| 38 | + // Step 1: Prepare each Text2Image subcomponent (scheduler, text encoder, unet, vae) separately. |
| 39 | + // |
| 40 | + |
| 41 | + // Create the scheduler from the details listed in the json. |
| 42 | + auto scheduler = ov::genai::Scheduler::from_config(root_dir / "scheduler/scheduler_config.json"); |
| 43 | + |
| 44 | + // Note that we could have created the scheduler by specifying specific type (for example EULER_DISCRETE), like |
| 45 | + // this: auto scheduler = ov::genai::Scheduler::from_config(root_dir / "scheduler/scheduler_config.json", |
| 46 | + // ov::genai::Scheduler::Type::EULER_DISCRETE); |
| 47 | + |
| 48 | + // Create unet object |
| 49 | + auto unet = ov::genai::UNet2DConditionModel(root_dir / "unet"); |
| 50 | + |
| 51 | + // Given the guidance scale, etc., calculate the batch size. |
| 52 | + int unet_batch_size = 1; |
| 53 | + if (guidance_scale > 1.0f && unet.get_config().time_cond_proj_dim < 0) { |
| 54 | + unet_batch_size = 2; |
| 55 | + } |
| 56 | + |
| 57 | + // Create, reshape, and compile the text encoder. |
| 58 | + auto text_encoder = ov::genai::CLIPTextModel(root_dir / "text_encoder"); |
| 59 | + text_encoder.reshape(unet_batch_size); |
| 60 | + text_encoder.compile(text_encoder_device, ov::cache_dir(ov_cache_dir)); |
| 61 | + |
| 62 | + // The max_postiion_embeddings config from text encoder will be used as a parameter to unet reshape. |
| 63 | + int max_position_embeddings = text_encoder.get_config().max_position_embeddings; |
| 64 | + |
| 65 | + // Reshape unet to a static shape, and compile it. |
| 66 | + unet.reshape(unet_batch_size, height, width, max_position_embeddings); |
| 67 | + unet.compile(unet_device, ov::cache_dir(ov_cache_dir)); |
| 68 | + |
| 69 | + // Create, reshape, and compile the vae decoder. |
| 70 | + auto vae = ov::genai::AutoencoderKL(root_dir / "vae_decoder"); |
| 71 | + vae.reshape(1, height, width); // We set batch-size to '1' here, as we're configuring our pipeline to return 1 |
| 72 | + // image per 'generate' call. |
| 73 | + vae.compile(vae_decoder_device, ov::cache_dir(ov_cache_dir)); |
| 74 | + |
| 75 | + // |
| 76 | + // Step 2: Create a Text2ImagePipeline from the individual subcomponents |
| 77 | + // |
| 78 | + auto pipe = ov::genai::Text2ImagePipeline::stable_diffusion(scheduler, text_encoder, unet, vae); |
| 79 | + |
| 80 | + // |
| 81 | + // Step 3: Use the Text2ImagePipeline to generate 'number_of_images_to_generate' images. |
| 82 | + // |
| 83 | + for (int imagei = 0; imagei < number_of_images_to_generate; imagei++) { |
| 84 | + std::cout << "Generating image " << imagei << std::endl; |
| 85 | + |
| 86 | + ov::Tensor image = pipe.generate(prompt, |
| 87 | + ov::genai::width(width), |
| 88 | + ov::genai::height(height), |
| 89 | + ov::genai::guidance_scale(guidance_scale), |
| 90 | + ov::genai::num_inference_steps(number_of_inference_steps_per_image)); |
| 91 | + |
| 92 | + imwrite("image_" + std::to_string(imagei) + ".bmp", image, true); |
| 93 | + } |
| 94 | + |
| 95 | + return EXIT_SUCCESS; |
| 96 | +} catch (const std::exception& error) { |
| 97 | + try { |
| 98 | + std::cerr << error.what() << '\n'; |
| 99 | + } catch (const std::ios_base::failure&) { |
| 100 | + } |
| 101 | + return EXIT_FAILURE; |
| 102 | +} catch (...) { |
| 103 | + try { |
| 104 | + std::cerr << "Non-exception object thrown\n"; |
| 105 | + } catch (const std::ios_base::failure&) { |
| 106 | + } |
| 107 | + return EXIT_FAILURE; |
| 108 | +} |
0 commit comments