Skip to content

Commit

Permalink
Merge branch 'master' into wenyi5608-greedy-sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyi5608 authored Apr 17, 2024
2 parents 5017097 + 534963a commit bc746d9
Show file tree
Hide file tree
Showing 45 changed files with 2,760 additions and 1,637 deletions.
28 changes: 28 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BasedOnStyle: Google
IndentWidth: 4
UseTab: Never
ColumnLimit: 120

Language: Cpp
Standard: Cpp11

AccessModifierOffset: -4
AlignConsecutiveMacros: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
CommentPragmas: '^#'
DerivePointerAlignment: false
FixNamespaceComments: true
IndentCaseLabels: false
IndentPPDirectives: AfterHash
ForEachMacros:
- foreach
- FOREACH_CHILD
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/good_first_issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ body:
value: |
- [Contribution guide - start here!](https://github.com/openvinotoolkit/openvino/blob/master/CONTRIBUTING.md)
- [Intel DevHub Discord channel](https://discord.gg/7pVRxUwdWG) - engage in discussions, ask questions and talk to OpenVINO developers
- [How to link your Pull Request to an issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#manually-linking-a-pull-request-to-an-issue-using-the-pull-request-sidebar)
validations:
required: true

Expand Down
267 changes: 264 additions & 3 deletions .github/workflows/causal_lm_cpp.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/llm_bench-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
python-version: 3.8
- name: Test stateful
run: |
python -m pip install -r llm_bench/python/requirements.txt optimum
python -m pip install -r llm_bench/python/requirements.txt
python -m pip uninstall --yes openvino
python -m pip install openvino-nightly
python llm_bench/python/convert.py --model_id TinyLlama/TinyLlama-1.1B-Chat-v1.0 --output_dir . --stateful
Expand Down
115 changes: 73 additions & 42 deletions .github/workflows/stable_diffusion_1_5_cpp.yml
Original file line number Diff line number Diff line change
@@ -1,84 +1,115 @@
name: stable_diffusion_1_5_cpp

on:
pull_request:
paths:
- image_generation/stable_diffusion_1_5/cpp/**
- image_generation/common/**
- .github/workflows/stable_diffusion_1_5_cpp.yml
- thirdparty/openvino_tokenizers

env:
working_directory: "./image_generation/stable_diffusion_1_5/cpp/"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
stable_diffusion_1_5_cpp-linux:
runs-on: ubuntu-20.04-8-cores
defaults:
run:
# Do not ignore bash profile files. From:
# https://github.com/marketplace/actions/setup-miniconda#important
shell: bash -l {0}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-python@v4

- name: Setup conda
uses: conda-incubator/setup-miniconda@v3
with:
python-version: 3.8
- name: Install OpenVINO
miniconda-version: "latest"
activate-environment: openvino_sd_cpp
python-version: "3.10"

- name: Install OpenVINO and other conda dependencies
run: |
set -e
mkdir openvino
curl https://storage.openvinotoolkit.org/repositories/openvino/packages/nightly/2024.1.0-14645-e6dc0865128/l_openvino_toolkit_ubuntu20_2024.1.0.dev20240304_x86_64.tgz | tar --directory ./openvino/ --strip-components 1 -xz
sudo ./openvino/install_dependencies/install_openvino_dependencies.sh
- name: Download / convert models
conda activate openvino_sd_cpp
conda install -c conda-forge openvino c-compiler cxx-compiler make cmake
conda env config vars set LD_LIBRARY_PATH=$CONDA_PREFIX/lib:$LD_LIBRARY_PATH
- name: Install python dependencies
working-directory: ${{ env.working_directory }}
run: |
set -e
source ./openvino/setupvars.sh
cd ./image_generation/stable_diffusion_1_5/cpp/scripts/
python -m pip install -U pip
python -m pip install -r ./requirements.txt
python -m pip install ../../../../thirdparty/openvino_tokenizers/
python convert_model.py -sd runwayml/stable-diffusion-v1-5 -b 1 -t FP16 -dyn True
conda activate openvino_sd_cpp
python -m pip install -r requirements.txt
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
- name: Download and convert model and tokenizer
working-directory: ${{ env.working_directory }}
run: |
conda activate openvino_sd_cpp
export MODEL_PATH="models/stable_diffusion_v1_5_ov/FP16"
optimum-cli export openvino --model runwayml/stable-diffusion-v1-5 --task stable-diffusion --convert-tokenizer --weight-format fp16 $MODEL_PATH
convert_tokenizer $MODEL_PATH/tokenizer/ --tokenizer-output-type i32 -o $MODEL_PATH/tokenizer/
- name: Build app
working-directory: ${{ env.working_directory }}
run: |
set -e
source ./openvino/setupvars.sh
cd ./image_generation/stable_diffusion_1_5/cpp/
conda activate openvino_sd_cpp
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build/
cmake --build ./build/ --config Release --parallel
- name: Run app
working-directory: ${{ env.working_directory }}
run: |
set -e
source ./openvino/setupvars.sh
cd ./image_generation/stable_diffusion_1_5/cpp/build
./stable_diffusion -m ../scripts/runwayml/stable-diffusion-v1-5 -t FP16_dyn
./build/stable_diffusion -m ./models/stable_diffusion_v1_5_ov -t FP16
stable_diffusion_1_5_cpp-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-python@v4

- name: Setup conda
uses: conda-incubator/setup-miniconda@v3
with:
python-version: 3.8
- name: Initialize OpenVINO
shell: cmd
miniconda-version: "latest"
activate-environment: openvino_sd_cpp
python-version: "3.10"

- name: Install OpenVINO and other conda dependencies
run: |
conda activate openvino_sd_cpp
conda install -c conda-forge openvino c-compiler cxx-compiler make cmake
- name: Install python dependencies
working-directory: ${{ env.working_directory }}
run: |
curl --output ov.zip https://storage.openvinotoolkit.org/repositories/openvino/packages/nightly/2024.1.0-14645-e6dc0865128/w_openvino_toolkit_windows_2024.1.0.dev20240304_x86_64.zip
unzip ov.zip
- name: Download / convert a model / tokenizer
shell: cmd
conda activate openvino_sd_cpp
python -m pip install -r requirements.txt
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
- name: Download and convert model and tokenizer
working-directory: ${{ env.working_directory }}
run: |
call w_openvino_toolkit_windows_2024.1.0.dev20240304_x86_64/setupvars.bat
cd ./image_generation/stable_diffusion_1_5/cpp/scripts/
python -m pip install -r ./requirements.txt
python -m pip install ../../../../thirdparty/openvino_tokenizers/
python convert_model.py -sd runwayml/stable-diffusion-v1-5 -b 1 -t FP16 -dyn True
conda activate openvino_sd_cpp
$env:MODEL_PATH='models/stable_diffusion_v1_5_ov/FP16'
optimum-cli export openvino --model runwayml/stable-diffusion-v1-5 --task stable-diffusion --convert-tokenizer --weight-format fp16 $env:MODEL_PATH
convert_tokenizer $env:MODEL_PATH/tokenizer/ --tokenizer-output-type i32 -o $env:MODEL_PATH/tokenizer/
- name: Build app
shell: cmd
working-directory: ${{ env.working_directory }}
run: |
call w_openvino_toolkit_windows_2024.1.0.dev20240304_x86_64/setupvars.bat
cd ./image_generation/stable_diffusion_1_5/cpp/
conda activate openvino_sd_cpp
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build/
cmake --build ./build/ --config Release --parallel
- name: Run app
shell: cmd
working-directory: ${{ env.working_directory }}
run: |
call w_openvino_toolkit_windows_2024.1.0.dev20240304_x86_64/setupvars.bat
cd ./image_generation/stable_diffusion_1_5/cpp/build/
call "./Release/stable_diffusion.exe" -m ../scripts/runwayml/stable-diffusion-v1-5 -t FP16_dyn
& "./build/Release/stable_diffusion.exe" -m ./models/stable_diffusion_v1_5_ov -t FP16 --dynamic
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# build/artifact dirs
_*
[Bb]uild*/
cmake-build*

# but ensure we don't skip __init__.py and __main__.py
!__init__.py
!__main__.py

# developer tools
*.idea
.vscode
.vs/
.vsconan/
.DS_Store
**/tags
compile_commands.json
bin/
.local_vimrc
.gdb_history
.vimspector.json
doc/
docs/build_documentation/work_dir/
temp/
.repo/
CMakeLists.txt.user
docs/IE_PLUGIN_DG/html/
CMakeUserPresets.json

*.project
*.cproject
*.pydevproject
*.settings
*/gen/
*.swp
/config.xml

# Python-specific
*.?env*
*.pyc
__pycache__
7 changes: 6 additions & 1 deletion image_generation/common/diffusers/include/scheduler_lcm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class LCMScheduler : public Scheduler {
float clip_sample_range = 1.0f,
float dynamic_thresholding_ratio = 0.995f,
float sample_max_value = 1.0f,
bool read_torch_noise = false);
bool read_torch_noise = false,
uint32_t seed = 42);

void set_timesteps(size_t num_inference_steps) override;

Expand Down Expand Up @@ -47,6 +48,10 @@ class LCMScheduler : public Scheduler {
float dynamic_thresholding_ratio;
float sample_max_value;
bool read_torch_noise;
std::mt19937 gen;
std::normal_distribution<float> normal;
uint32_t seed;

std::vector<float> threshold_sample(const std::vector<float>& flat_sample);
std::vector<float> randn_function(uint32_t size, uint32_t seed);
};
33 changes: 17 additions & 16 deletions image_generation/common/diffusers/src/scheduler_lcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ std::vector<T> linspace(U start, U end, size_t num, bool endpoint = false) {
return indices;
}

// for torch.randn()
std::vector<float> randn_function(uint32_t size, uint32_t seed = 42) {
std::vector<float> noise(size);
{
std::mt19937 gen{static_cast<unsigned long>(seed)};
std::normal_distribution<float> normal{0.0f, 1.0f};
std::for_each(noise.begin(), noise.end(), [&](float& x) {
x = normal(gen);
});
}
return noise;
}

std::vector<float> read_vector_from_txt(std::string& file_name) {
std::ifstream input_data(file_name, std::ifstream::in);
std::istream_iterator<float> start(input_data), end;
Expand All @@ -64,7 +51,8 @@ LCMScheduler::LCMScheduler(size_t num_train_timesteps,
float clip_sample_range,
float dynamic_thresholding_ratio,
float sample_max_value,
bool read_torch_noise):
bool read_torch_noise,
uint32_t seed):
prediction_type_config(prediction_type),
num_train_timesteps_config(num_train_timesteps),
original_inference_steps_config(original_inference_steps),
Expand All @@ -74,7 +62,10 @@ LCMScheduler::LCMScheduler(size_t num_train_timesteps,
clip_sample_range(clip_sample_range),
dynamic_thresholding_ratio(dynamic_thresholding_ratio),
sample_max_value(sample_max_value),
read_torch_noise(read_torch_noise) {
read_torch_noise(read_torch_noise),
seed(seed),
gen(seed),
normal(0.0f, 1.0f) {

sigma_data = 0.5f; // Default: 0.5

Expand Down Expand Up @@ -204,7 +195,7 @@ std::map<std::string, ov::Tensor> LCMScheduler::step(ov::Tensor noise_pred, ov::
std::string noise_file = "../scripts/torch_noise_step_" + std::to_string(inference_step) + ".txt";
noise = read_vector_from_txt(noise_file);
} else {
noise = randn_function(noise_pred.get_size());
noise = randn_function(noise_pred.get_size(), seed);
}

for (std::size_t i = 0; i < noise_pred.get_size(); ++i) {
Expand Down Expand Up @@ -261,3 +252,13 @@ std::vector<float> LCMScheduler::threshold_sample(const std::vector<float>& flat

return thresholded_sample;
}

std::vector<float> LCMScheduler::randn_function(uint32_t size, uint32_t seed = 42) {
std::vector<float> noise(size);
{
std::for_each(noise.begin(), noise.end(), [&](float& x) {
x = normal(gen);
});
}
return noise;
}
1 change: 1 addition & 0 deletions image_generation/lcm_dreamshaper_v7/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ conda install -c conda-forge openvino c-compiler cxx-compiler make
1. Install dependencies to import models from HuggingFace:

```shell
git submodule update --init
conda activate openvino_lcm_cpp
python -m pip install -r scripts/requirements.txt
python -m pip install ../../../thirdparty/openvino_tokenizers/[transformers]
Expand Down
Binary file modified image_generation/lcm_dreamshaper_v7/cpp/cpp_random.bmp
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--extra-index-url https://download.pytorch.org/whl/cpu
torch==2.2.0+cpu
diffusers==0.26.3
optimum-intel[nncf,openvino]==1.15.2
torch==2.2.2+cpu
diffusers==0.27.2
optimum-intel[nncf,openvino]==1.16.0
2 changes: 1 addition & 1 deletion image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ int32_t main(int32_t argc, char* argv[]) try {
std::shared_ptr<Scheduler> scheduler = std::make_shared<LCMScheduler>(LCMScheduler(
1000, 0.00085f, 0.012f, BetaSchedule::SCALED_LINEAR,
PredictionType::EPSILON, {}, 50, true, 10.0f, false,
false, 1.0f, 0.995f, 1.0f, read_np_latent));
false, 1.0f, 0.995f, 1.0f, read_np_latent, user_seed));
scheduler->set_timesteps(num_inference_steps);
std::vector<std::int64_t> timesteps = scheduler->get_timesteps();

Expand Down
3 changes: 3 additions & 0 deletions image_generation/stable_diffusion_1_5/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
images
models
Loading

0 comments on commit bc746d9

Please sign in to comment.