forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix lora video support #21
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
Closed
Closed
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cc1f3d5
Add LoRA adapter support for video generation (#3306)
Eshaan-byte 41b96b0
Fix CI: Add set_adapters Python binding and safety check
goyaladitya05 7c1d319
Address PR feedback: refine ASSERT message and move CFG tests
goyaladitya05 6830c2e
Address PR feedback for LoRA video support
goyaladitya05 6977cec
fix CI build and lint failures
goyaladitya05 476a48d
Regenerate pyi stubs for set_adapters
goyaladitya05 c95a549
Fix CI: Remove manual docstring from LTXVideoTransformer3DModel.set_a…
goyaladitya05 990d550
Fix CI: Restore set_adapters docstring with correct pybind11-stubgen …
goyaladitya05 f8e2907
Fix CI: Correct set_adapters docstring indentation to match pybind11-…
goyaladitya05 5f6079e
Fix CI: Remove set_adapters docstring from C++ binding to produce det…
goyaladitya05 f48aa80
Fix WWB tests and Windows VLM bug
goyaladitya05 c097433
Merge remote-tracking branch 'upstream/master' into fix-lora-video-su…
goyaladitya05 cef3507
Merge branch 'master' into fix-lora-video-support
goyaladitya05 e89f419
undo changes to requirements.txt
goyaladitya05 39af1aa
Merge branch 'fix-lora-video-support' of https://github.com/goyaladit…
goyaladitya05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| // Copyright (C) 2025-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <random> | ||
| #include <filesystem> | ||
|
|
||
| #include "progress_bar.hpp" | ||
| #include "imwrite_video.hpp" | ||
|
|
||
| #include <openvino/genai/video_generation/text2video_pipeline.hpp> | ||
|
|
||
| int main(int32_t argc, char* argv[]) try { | ||
| OPENVINO_ASSERT(argc >= 3 && (argc - 3) % 2 == 0, "Usage: ", argv[0], " <MODEL_DIR> '<PROMPT>' [<LORA_SAFETENSORS> <ALPHA> ...]]"); | ||
|
|
||
| std::filesystem::path models_dir = argv[1]; | ||
| std::string prompt = argv[2]; | ||
|
|
||
| const std::string device = "CPU"; // GPU can be used as well | ||
| float frame_rate = 25.0f; | ||
|
|
||
| ov::genai::AdapterConfig adapter_config; | ||
| // 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); | ||
goyaladitya05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // LoRA adapters passed to the constructor will be activated by default in next generates | ||
| ov::genai::Text2VideoPipeline pipe(models_dir, device, ov::genai::adapters(adapter_config)); | ||
|
|
||
| std::cout << "Generating video with LoRA adapters applied, resulting video will be in lora_video.avi\n"; | ||
| auto output = pipe.generate( | ||
| prompt, | ||
| ov::genai::negative_prompt("worst quality, inconsistent motion, blurry, jittery, distorted"), | ||
| ov::genai::height(480), | ||
| ov::genai::width(704), | ||
| ov::genai::num_frames(161), | ||
| ov::genai::num_inference_steps(25), | ||
| ov::genai::num_videos_per_prompt(1), | ||
| ov::genai::callback(progress_bar), | ||
| ov::genai::frame_rate(frame_rate), | ||
| ov::genai::guidance_scale(3) | ||
| ); | ||
|
|
||
| save_video("lora_video.avi", output.video, frame_rate); | ||
|
|
||
| std::cout << "Generating video without LoRA adapters applied, resulting video will be in baseline_video.avi\n"; | ||
| output = pipe.generate( | ||
| prompt, | ||
| ov::genai::adapters(), // passing adapters in generate overrides adapters set in the constructor; adapters() means no adapters | ||
| ov::genai::negative_prompt("worst quality, inconsistent motion, blurry, jittery, distorted"), | ||
| ov::genai::height(480), | ||
| ov::genai::width(704), | ||
| ov::genai::num_frames(161), | ||
| ov::genai::num_inference_steps(25), | ||
| ov::genai::num_videos_per_prompt(1), | ||
| ov::genai::callback(progress_bar), | ||
| ov::genai::frame_rate(frame_rate), | ||
| ov::genai::guidance_scale(3) | ||
| ); | ||
|
|
||
| save_video("baseline_video.avi", output.video, frame_rate); | ||
|
|
||
| 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 hidden or 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 hidden or 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,77 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (C) 2025-2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import argparse | ||
| import cv2 | ||
| import openvino_genai | ||
|
|
||
|
|
||
| def save_video(filename: str, video_tensor, fps: int = 25): | ||
| batch_size, num_frames, height, width, _ = video_tensor.shape | ||
| video_data = video_tensor.data | ||
|
|
||
| for b in range(batch_size): | ||
| if batch_size == 1: | ||
| output_path = filename | ||
| else: | ||
| base, ext = filename.rsplit(".", 1) if "." in filename else (filename, "avi") | ||
| output_path = f"{base}_b{b}.{ext}" | ||
|
|
||
| fourcc = cv2.VideoWriter_fourcc(*"MJPG") | ||
| writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) | ||
|
|
||
goyaladitya05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for f in range(num_frames): | ||
| frame_bgr = cv2.cvtColor(video_data[b, f], cv2.COLOR_RGB2BGR) | ||
| writer.write(frame_bgr) | ||
|
|
||
| writer.release() | ||
| print(f"Wrote {output_path} ({num_frames} frames, {width}x{height} @ {fps} fps)") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Generate video from text prompt using OpenVINO GenAI with LoRA adapters" | ||
| ) | ||
| parser.add_argument("model_dir", help="Path to the model directory") | ||
| parser.add_argument("prompt", help="Text prompt for video generation") | ||
| parser.add_argument("lora_adapter", help="Path to the LoRA adapter file (.safetensors)") | ||
| args = parser.parse_args() | ||
|
|
||
| # Load adapter | ||
| adapter_config = openvino_genai.AdapterConfig() | ||
| adapter_config.add(openvino_genai.Adapter(args.lora_adapter)) | ||
|
|
||
| pipe = openvino_genai.Text2VideoPipeline(args.model_dir, "CPU", adapters=adapter_config) # GPU can be used as well | ||
|
|
||
| frame_rate = 25 | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| print(f"Generation step {step + 1} / {num_steps}") | ||
| return False | ||
|
|
||
| output = pipe.generate( | ||
| args.prompt, | ||
| negative_prompt="worst quality, inconsistent motion, blurry, jittery, distorted", | ||
| height=480, | ||
| width=704, | ||
| num_frames=161, | ||
| num_inference_steps=25, | ||
| num_videos_per_prompt=1, | ||
| callback=callback, | ||
| frame_rate=frame_rate, | ||
| guidance_scale=3, | ||
| adapters=adapter_config, | ||
| ) | ||
|
|
||
| save_video("genai_video.avi", output.video, frame_rate) | ||
|
|
||
| print(f"\nPerformance metrics:") | ||
goyaladitya05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print(f" Load time: {output.perf_metrics.get_load_time():.2f} ms") | ||
| print(f" Generate duration: {output.perf_metrics.get_generate_duration():.2f} ms") | ||
| print(f" Transformer duration: {output.perf_metrics.get_transformer_infer_duration().mean:.2f} ms") | ||
| print(f" VAE decoder duration: {output.perf_metrics.get_vae_decoder_infer_duration():.2f} ms") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.