-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LoRA fuse sample for Text2Image pipeline. (#1170)
Sample demonstrates how to maximize performance of LoRA adapters with Text2Image pipeline.
- Loading branch information
Showing
9 changed files
with
180 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (C) 2023-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "openvino/genai/image_generation/text2image_pipeline.hpp" | ||
|
||
#include "imwrite.hpp" | ||
|
||
int32_t main(int32_t argc, char* argv[]) try { | ||
OPENVINO_ASSERT(argc >= 3 && (argc - 3) % 2 == 0, "Usage: ", argv[0], " <MODEL_DIR> '<PROMPT>' [<LORA_SAFETENSORS> <ALPHA> ...]]"); | ||
|
||
const std::string models_path = argv[1], prompt = argv[2]; | ||
const std::string device = "CPU"; // GPU can be used as well | ||
|
||
// MODE_FUSE instructs pipeline to fuse adapter tensors into original model weights loaded into memory | ||
// giving the same performance level for inference as for the original model. After doing it you cannot | ||
// change adapter dynamically without re-initializing the pipeline from scratch. | ||
ov::genai::AdapterConfig adapter_config(ov::genai::AdapterConfig::MODE_FUSE); | ||
|
||
// Multiple LoRA adapters applied simultaneously are supported, parse them all and corresponding alphas from cmd parameters: | ||
for(size_t i = 0; i < (argc - 3)/2; ++i) { | ||
ov::genai::Adapter adapter(argv[3 + 2*i]); | ||
float alpha = std::atof(argv[3 + 2*i + 1]); | ||
adapter_config.add(adapter, alpha); | ||
} | ||
|
||
// LoRA adapters passed to the constructor will be activated by default in next generates | ||
ov::genai::Text2ImagePipeline pipe(models_path, device, ov::genai::adapters(adapter_config)); | ||
|
||
std::cout << "Generating image with LoRA adapters fused into original weights, resulting image will be in lora.bmp\n"; | ||
ov::Tensor image = pipe.generate(prompt, | ||
ov::genai::generator(std::make_shared<ov::genai::CppStdGenerator>(42)), | ||
ov::genai::width(512), | ||
ov::genai::height(896), | ||
ov::genai::num_inference_steps(20)); | ||
imwrite("lora.bmp", image, true); | ||
|
||
return EXIT_SUCCESS; | ||
} catch (const std::exception& error) { | ||
try { | ||
std::cerr << error.what() << '\n'; | ||
} catch (const std::ios_base::failure&) {} | ||
return EXIT_FAILURE; | ||
} catch (...) { | ||
try { | ||
std::cerr << "Non-exception object thrown\n"; | ||
} catch (const std::ios_base::failure&) {} | ||
return EXIT_FAILURE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import argparse | ||
|
||
import openvino as ov | ||
import openvino_genai | ||
import numpy as np | ||
import sys | ||
|
||
|
||
class Generator(openvino_genai.Generator): | ||
def __init__(self, seed, mu=0.0, sigma=1.0): | ||
openvino_genai.Generator.__init__(self) | ||
np.random.seed(seed) | ||
self.mu = mu | ||
self.sigma = sigma | ||
|
||
def next(self): | ||
return np.random.normal(self.mu, self.sigma) | ||
|
||
|
||
def image_write(path: str, image_tensor: ov.Tensor): | ||
from PIL import Image | ||
image = Image.fromarray(image_tensor.data[0]) | ||
image.save(path) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('models_path') | ||
parser.add_argument('prompt') | ||
args, adapters = parser.parse_known_args() | ||
|
||
prompt = args.prompt | ||
|
||
device = "CPU" # GPU can be used as well | ||
|
||
# MODE_FUSE instructs pipeline to fuse adapter tensors into original model weights loaded into memory | ||
# giving the same performance level for inference as for the original model. After doing it you cannot | ||
# change adapter dynamically without re-initializing the pipeline from scratch. | ||
adapter_config = openvino_genai.AdapterConfig(openvino_genai.AdapterConfig.Mode.MODE_FUSE) | ||
|
||
# Multiple LoRA adapters applied simultaneously are supported, parse them all and corresponding alphas from cmd parameters: | ||
for i in range(int(len(adapters) / 2)): | ||
adapter = openvino_genai.Adapter(adapters[2 * i]) | ||
alpha = float(adapters[2 * i + 1]) | ||
adapter_config.add(adapter, alpha) | ||
|
||
# LoRA adapters passed to the constructor will be activated by default in next generates | ||
pipe = openvino_genai.Text2ImagePipeline(args.models_path, device, adapters=adapter_config) | ||
print("Generating image with LoRA adapters applied, resulting image will be in lora.bmp") | ||
image = pipe.generate(prompt, | ||
generator=Generator(42), | ||
width=512, | ||
height=896, | ||
num_inference_steps=20) | ||
|
||
image_write("lora.bmp", image) | ||
|
||
|
||
if '__main__' == __name__: | ||
main() |