Skip to content

Commit 387c728

Browse files
authored
[Port 24.1] Move from image_generation python conversion scripts to optimum-cli (LCM Dreamshaper v7 model) (#402)
1 parent a3d4153 commit 387c728

12 files changed

+221
-138
lines changed
+90-52
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,118 @@
11
name: lcm_dreamshaper
2+
23
on:
34
pull_request:
45
paths:
56
- image_generation/lcm_dreamshaper_v7/cpp/**
67
- image_generation/common/**
78
- .github/workflows/lcm_dreamshaper_cpp.yml
89
- thirdparty/openvino_tokenizers
10+
11+
env:
12+
working_directory: "./image_generation/lcm_dreamshaper_v7/cpp/"
13+
914
concurrency:
1015
group: ${{ github.workflow }}-${{ github.ref }}
1116
cancel-in-progress: true
17+
1218
jobs:
1319
lcm_dreamshaper_v7_cpp-linux:
1420
runs-on: ubuntu-20.04
21+
defaults:
22+
run:
23+
# Do not ignore bash profile files. From:
24+
# https://github.com/marketplace/actions/setup-miniconda#important
25+
shell: bash -l {0}
1526
steps:
1627
- uses: actions/checkout@v4
1728
with:
1829
submodules: recursive
19-
- uses: actions/setup-python@v4
30+
31+
- name: Setup conda
32+
uses: conda-incubator/setup-miniconda@v3
2033
with:
21-
python-version: 3.8
22-
- name: Initialize OpenVINO
34+
miniconda-version: "latest"
35+
activate-environment: openvino_lcm_cpp
36+
python-version: "3.10"
37+
38+
- name: Install OpenVINO and other conda dependencies
2339
run: |
24-
mkdir openvino
25-
curl https://storage.openvinotoolkit.org/repositories/openvino/packages/2024.1/linux/l_openvino_toolkit_ubuntu20_2024.1.0.15008.f4afc983258_x86_64.tgz | tar --directory ./openvino/ --strip-components 1 -xz
26-
sudo ./openvino/install_dependencies/install_openvino_dependencies.sh
27-
- name: Download / convert a model / tokenizer
40+
conda activate openvino_lcm_cpp
41+
conda update -c conda-forge --all
42+
conda install -c conda-forge openvino=2024.1.0 c-compiler cxx-compiler make cmake
43+
conda env config vars set LD_LIBRARY_PATH=$CONDA_PREFIX/lib:$LD_LIBRARY_PATH
44+
45+
- name: Install python dependencies
46+
working-directory: ${{ env.working_directory }}
2847
run: |
29-
source ./openvino/setupvars.sh
30-
cd ./image_generation/lcm_dreamshaper_v7/cpp/scripts/
31-
python -m pip install -U pip
32-
python -m pip install -r ./requirements.txt
33-
python -m pip install ../../../../thirdparty/openvino_tokenizers/
34-
python convert_model.py -lcm "SimianLuo/LCM_Dreamshaper_v7" -t "FP16"
48+
conda activate openvino_lcm_cpp
49+
python -m pip install -r requirements.txt
50+
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
51+
52+
- name: Download and convert model and tokenizer
53+
working-directory: ${{ env.working_directory }}
54+
run: |
55+
conda activate openvino_lcm_cpp
56+
export MODEL_PATH="models/lcm_dreamshaper_v7/FP16"
57+
optimum-cli export openvino --model SimianLuo/LCM_Dreamshaper_v7 --weight-format fp16 $MODEL_PATH
58+
convert_tokenizer $MODEL_PATH/tokenizer/ --tokenizer-output-type i32 -o $MODEL_PATH/tokenizer/
59+
3560
- name: Build app
61+
working-directory: ${{ env.working_directory }}
3662
run: |
37-
source ./openvino/setupvars.sh
38-
cd ./image_generation/lcm_dreamshaper_v7/cpp/
63+
conda activate openvino_lcm_cpp
3964
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build/
4065
cmake --build ./build/ --config Release --parallel
66+
4167
- name: Run app
68+
working-directory: ${{ env.working_directory }}
4269
run: |
43-
source ./openvino/setupvars.sh
44-
cd ./image_generation/lcm_dreamshaper_v7/cpp/build/
45-
./lcm_dreamshaper
70+
./build/lcm_dreamshaper
71+
4672
lcm_dreamshaper_v7_cpp-windows:
47-
runs-on: windows-latest
48-
steps:
49-
- uses: actions/checkout@v4
50-
with:
51-
submodules: recursive
52-
- uses: actions/setup-python@v4
53-
with:
54-
python-version: 3.8
55-
- name: Initialize OpenVINO
56-
shell: cmd
57-
run: |
58-
curl --output ov.zip https://storage.openvinotoolkit.org/repositories/openvino/packages/2024.1/windows/w_openvino_toolkit_windows_2024.1.0.15008.f4afc983258_x86_64.zip
59-
unzip ov.zip
60-
- name: Download / convert a model / tokenizer
61-
shell: cmd
62-
run: |
63-
call w_openvino_toolkit_windows_2024.1.0.15008.f4afc983258_x86_64/setupvars.bat
64-
cd ./image_generation/lcm_dreamshaper_v7/cpp/scripts/
65-
python -m pip install -r ./requirements.txt
66-
python -m pip install ../../../../thirdparty/openvino_tokenizers/
67-
python convert_model.py -lcm "SimianLuo/LCM_Dreamshaper_v7" -t "FP16"
68-
- name: Build app
69-
shell: cmd
70-
run: |
71-
call w_openvino_toolkit_windows_2024.1.0.15008.f4afc983258_x86_64/setupvars.bat
72-
cd ./image_generation/lcm_dreamshaper_v7/cpp/
73-
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build/
74-
cmake --build ./build/ --config Release --parallel
75-
- name: Run app
76-
shell: cmd
77-
run: |
78-
call w_openvino_toolkit_windows_2024.1.0.15008.f4afc983258_x86_64/setupvars.bat
79-
cd ./image_generation/lcm_dreamshaper_v7/cpp/build/
80-
call "./Release/lcm_dreamshaper.exe"
73+
runs-on: windows-latest
74+
steps:
75+
- uses: actions/checkout@v4
76+
with:
77+
submodules: recursive
78+
79+
- name: Setup conda
80+
uses: conda-incubator/setup-miniconda@v3
81+
with:
82+
miniconda-version: "latest"
83+
activate-environment: openvino_lcm_cpp
84+
python-version: "3.10"
85+
86+
- name: Install OpenVINO and other conda dependencies
87+
run: |
88+
conda activate openvino_lcm_cpp
89+
conda update -c conda-forge --all
90+
conda install -c conda-forge openvino=2024.1.0 c-compiler cxx-compiler make cmake
91+
conda env config vars set LD_LIBRARY_PATH=$CONDA_PREFIX/lib:$LD_LIBRARY_PATH
92+
93+
- name: Install python dependencies
94+
working-directory: ${{ env.working_directory }}
95+
run: |
96+
conda activate openvino_lcm_cpp
97+
python -m pip install -r requirements.txt
98+
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
99+
100+
- name: Download and convert model and tokenizer
101+
working-directory: ${{ env.working_directory }}
102+
run: |
103+
conda activate openvino_lcm_cpp
104+
$env:MODEL_PATH='models/lcm_dreamshaper_v7/FP16'
105+
optimum-cli export openvino --model SimianLuo/LCM_Dreamshaper_v7 --weight-format fp16 $env:MODEL_PATH
106+
convert_tokenizer $env:MODEL_PATH/tokenizer/ --tokenizer-output-type i32 -o $env:MODEL_PATH/tokenizer/
107+
108+
- name: Build app
109+
working-directory: ${{ env.working_directory }}
110+
run: |
111+
conda activate openvino_lcm_cpp
112+
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build/
113+
cmake --build ./build/ --config Release --parallel
114+
115+
- name: Run app
116+
working-directory: ${{ env.working_directory }}
117+
run: |
118+
& "./build/Release/lcm_dreamshaper.exe" -r --dynamic

.github/workflows/stable_diffusion_1_5_cpp.yml

-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ jobs:
9292
run: |
9393
conda activate openvino_sd_cpp
9494
python -m pip install -r requirements.txt
95-
$env:SPM_PROTOBUF_PROVIDER = 'internal'
96-
$env:SPM_ABSL_PROVIDER = 'internal'
9795
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
9896
9997
- name: Download and convert model and tokenizer

image_generation/common/diffusers/src/scheduler_lcm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ std::map<std::string, ov::Tensor> LCMScheduler::step(ov::Tensor noise_pred, ov::
192192
if (inference_step != num_inference_steps - 1) {
193193
std::vector<float> noise;
194194
if (read_torch_noise) {
195-
std::string noise_file = "../scripts/torch_noise_step_" + std::to_string(inference_step) + ".txt";
195+
std::string noise_file = "./latents/torch_noise_step_" + std::to_string(inference_step) + ".txt";
196196
noise = read_vector_from_txt(noise_file);
197197
} else {
198198
noise = randn_function(noise_pred.get_size(), seed);

image_generation/lcm_dreamshaper_v7/cpp/README.md

+28-22
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
The pure C++ text-to-image pipeline, driven by the OpenVINO native API for SD v1.5 Latent Consistency Model with LCM Scheduler. It includes advanced features like LoRA integration with safetensors and [OpenVINO Tokenizers](https://github.com/openvinotoolkit/openvino_tokenizers). Loading `openvino_tokenizers` to `ov::Core` enables tokenization. [The common folder](../../common/) contains schedulers for image generation and `imwrite()` for saving `bmp` images. This demo has been tested for Linux platform only. There is also a Jupyter [notebook](https://github.com/openvinotoolkit/openvino_notebooks/blob/main/notebooks/263-latent-consistency-models-image-generation/263-lcm-lora-controlnet.ipynb) which provides an example of image generaztion in Python.
33

44
> [!NOTE]
5-
>This tutorial assumes that the current working directory is `<openvino.genai repo>/image_generation/lcm_dreamshaper_v7/cpp/` and all paths are relative to this folder.
5+
> This tutorial assumes that the current working directory is `<openvino.genai repo>/image_generation/lcm_dreamshaper_v7/cpp/` and all paths are relative to this folder.
66
77
## Step 1: Prepare build environment
88

9+
Prerequisites:
10+
- Conda ([installation guide](https://conda.io/projects/conda/en/latest/user-guide/install/index.html))
11+
912
C++ Packages:
1013
* [CMake](https://cmake.org/download/): Cross-platform build tool
11-
* [OpenVINO](https://docs.openvino.ai/2023.2/openvino_docs_install_guides_overview.html): Model inference
14+
* [OpenVINO](https://docs.openvino.ai/2024/get-started/install-openvino.html): Model inference
1215

1316
Prepare a python environment and install dependencies:
1417
```shell
1518
conda create -n openvino_lcm_cpp python==3.10
1619
conda activate openvino_lcm_cpp
17-
conda install -c conda-forge openvino c-compiler cxx-compiler make
20+
conda update -c conda-forge --all
21+
conda install -c conda-forge openvino=2024.1.0 c-compiler cxx-compiler make cmake
22+
# Ensure that Conda standard libraries are used
23+
conda env config vars set LD_LIBRARY_PATH=$CONDA_PREFIX/lib:$LD_LIBRARY_PATH
1824
```
1925

2026
## Step 2: Latent Consistency Model and Tokenizer models
@@ -26,20 +32,19 @@ conda install -c conda-forge openvino c-compiler cxx-compiler make
2632
```shell
2733
git submodule update --init
2834
conda activate openvino_lcm_cpp
29-
python -m pip install -r scripts/requirements.txt
35+
python -m pip install -r requirements.txt
3036
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
3137
```
3238

33-
2. Run model conversion script to download and convert PyTorch model to OpenVINO IR via [optimum-intel](https://github.com/huggingface/optimum-intel). Please, use the script `scripts/convert_model.py` to convert the model:
34-
39+
2. Download the model from Huggingface and convert it to OpenVINO IR via [optimum-intel CLI](https://github.com/huggingface/optimum-intel). Example command for downloading and exporting FP16 model:
3540
```shell
36-
cd scripts
37-
python convert_model.py -lcm "SimianLuo/LCM_Dreamshaper_v7" -t FP16
41+
export MODEL_PATH="models/lcm_dreamshaper_v7/FP16"
42+
# Using optimum-cli for exporting model to OpenVINO format
43+
optimum-cli export openvino --model SimianLuo/LCM_Dreamshaper_v7 --weight-format fp16 $MODEL_PATH
44+
# Converting tokenizer
45+
convert_tokenizer $MODEL_PATH/tokenizer/ --tokenizer-output-type i32 -o $MODEL_PATH/tokenizer/
3846
```
3947

40-
> [!NOTE]
41-
>Only static model is currently supported for this sample.
42-
4348
### LoRA enabling with safetensors
4449

4550
Refer to [python pipeline blog](https://blog.openvino.ai/blog-posts/enable-lora-weights-with-stable-diffusion-controlnet-pipeline).
@@ -65,19 +70,20 @@ Usage:
6570
lcm_dreamshaper [OPTION...]
6671
```
6772

68-
* `-p, --posPrompt arg` Initial positive prompt for SD (default: cyberpunk cityscape like Tokyo New York with tall buildings at dusk golden hour cinematic lighting)
73+
* `-p, --posPrompt arg` Initial positive prompt for LCM (default: a beautiful pink unicorn)
6974
* `-d, --device arg` AUTO, CPU, or GPU. Doesn't apply to Tokenizer model, OpenVINO Tokenizers can be inferred on a CPU device only (default: CPU)
70-
* `--step arg` Number of diffusion step ( default: 20)
75+
* `--step arg` Number of diffusion step (default: 4)
7176
* `-s, --seed arg` Number of random seed to generate latent (default: 42)
72-
* `--num arg` Number of image output(default: 1)
77+
* `--num arg` Number of image output (default: 1)
7378
* `--height arg` Height of output image (default: 512)
7479
* `--width arg` Width of output image (default: 512)
7580
* `-c, --useCache` Use model caching
76-
* `-r, --readNPLatent` Read numpy generated latents from file
77-
* `-m, --modelPath arg` Specify path of SD model IR (default: ../scripts/SimianLuo/LCM_Dreamshaper_v7)
78-
* `-t, --type arg` Specify the type of SD model IR (FP16_static or FP16_dyn) (default: FP16_static)
79-
* `-l, --loraPath arg` Specify path of lora file. (*.safetensors). (default: )
80-
* `-a, --alpha arg` alpha for lora (default: 0.75)
81+
* `-r, --readNPLatent` Read numpy generated latents from file, only supported for one output image
82+
* `-m, --modelPath arg` Specify path to LCM model IRs (default: ./models/lcm_dreamshaper_v7)
83+
* `-t, --type arg` Specify the type of LCM model IRs (e.g., FP32, FP16 or INT8) (default: FP16)
84+
* `--dynamic` Specify the model input shape to use dynamic shape
85+
* `-l, --loraPath arg` Specify path to LoRA file (*.safetensors) (default: )
86+
* `-a, --alpha arg` Specify alpha for LoRA (default: 0.75)
8187
* `-h, --help` Print usage
8288
8389
> [!NOTE]
@@ -89,15 +95,15 @@ Positive prompt: a beautiful pink unicorn
8995
9096
Read the numpy latent input and noise for scheduler instead of C++ std lib for the alignment with Python pipeline.
9197
92-
* Generate image with random data generated by Python `./build/lcm_dreamshaper -r`
98+
* Generate image with random data generated by Python: `./build/lcm_dreamshaper -r`
9399
94100
![image](./python_random.bmp)
95101
96-
* Generate image with C++ lib generated latent and noise : `./build/lcm_dreamshaper`
102+
* Generate image with C++ lib generated latent and noise: `./build/lcm_dreamshaper`
97103
98104
![image](./cpp_random.bmp)
99105
100-
* Generate image with soulcard lora and C++ generated latent and noise `./stable_diffusion -r -l path/to/soulcard.safetensors`
106+
* Generate image with soulcard lora and C++ generated latent and noise: `./stable_diffusion -r -l path/to/soulcard.safetensors`
101107
102108
![image](./lora_cpp_random.bmp)
103109

image_generation/lcm_dreamshaper_v7/cpp/scripts/convert_model.py

-41
This file was deleted.

0 commit comments

Comments
 (0)