forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_video_generation_models.cpp
More file actions
233 lines (222 loc) · 11.2 KB
/
py_video_generation_models.cpp
File metadata and controls
233 lines (222 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2025-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl/filesystem.h>
#include <filesystem>
#include "openvino/genai/video_generation/autoencoder_kl_ltx_video.hpp"
#include "openvino/genai/video_generation/ltx_video_transformer_3d_model.hpp"
#include "py_utils.hpp"
namespace py = pybind11;
namespace pyutils = ov::genai::pybind::utils;
void init_ltx_video_transformer_3d_model(py::module_& m) {
auto ltx_transformer =
py::class_<ov::genai::LTXVideoTransformer3DModel>(m,
"LTXVideoTransformer3DModel",
"LTXVideoTransformer3DModel class for LTX-Video denoising.")
.def(py::init([](const std::filesystem::path& root_dir) {
return std::make_unique<ov::genai::LTXVideoTransformer3DModel>(root_dir);
}),
py::arg("root_dir"),
R"(
LTXVideoTransformer3DModel class constructor.
root_dir (os.PathLike): Model root directory.
)")
.def(py::init(
[](const std::filesystem::path& root_dir, const std::string& device, const py::kwargs& kwargs) {
return std::make_unique<ov::genai::LTXVideoTransformer3DModel>(
root_dir,
device,
pyutils::kwargs_to_any_map(kwargs));
}),
py::arg("root_dir"),
py::arg("device"),
R"(
LTXVideoTransformer3DModel class constructor.
root_dir (os.PathLike): Model root directory.
device (str): Device on which inference will be done.
kwargs: Device properties.
)")
.def(py::init([](const ov::genai::LTXVideoTransformer3DModel& model) {
return std::make_unique<ov::genai::LTXVideoTransformer3DModel>(model);
}),
py::arg("model"),
R"(
LTXVideoTransformer3DModel copy constructor.
model (LTXVideoTransformer3DModel): Model to copy.
)");
py::class_<ov::genai::LTXVideoTransformer3DModel::Config>(ltx_transformer,
"Config",
"Configuration for LTXVideoTransformer3DModel.")
.def(py::init([](const std::filesystem::path& config_path) {
return std::make_unique<ov::genai::LTXVideoTransformer3DModel::Config>(config_path);
}),
py::arg("config_path"))
.def_readonly("in_channels", &ov::genai::LTXVideoTransformer3DModel::Config::in_channels)
.def_readonly("patch_size", &ov::genai::LTXVideoTransformer3DModel::Config::patch_size)
.def_readonly("patch_size_t", &ov::genai::LTXVideoTransformer3DModel::Config::patch_size_t);
ltx_transformer.def("get_config", &ov::genai::LTXVideoTransformer3DModel::get_config)
.def(
"compile",
[](ov::genai::LTXVideoTransformer3DModel& self, const std::string& device, const py::kwargs& kwargs) {
auto properties = pyutils::kwargs_to_any_map(kwargs);
py::gil_scoped_release rel;
return self.compile(device, properties);
},
py::arg("device"),
R"(
Compiles the model.
device (str): Device to run the model on (e.g., CPU, GPU).
kwargs: Device properties.
)")
.def("reshape",
&ov::genai::LTXVideoTransformer3DModel::reshape,
py::arg("batch_size"),
py::arg("num_frames"),
py::arg("height"),
py::arg("width"),
py::arg("tokenizer_model_max_length"),
R"(
Reshapes the model for specific input dimensions.
batch_size (int): Batch size.
num_frames (int): Number of video frames.
height (int): Video height.
width (int): Video width.
tokenizer_model_max_length (int): Maximum sequence length for tokenizer.
)")
.def("set_hidden_states",
&ov::genai::LTXVideoTransformer3DModel::set_hidden_states,
py::arg("tensor_name"),
py::arg("encoder_hidden_states"),
R"(
Sets encoder hidden states tensor.
tensor_name (str): Name of the tensor input.
encoder_hidden_states (ov.Tensor): Hidden states from text encoder.
)")
.def("set_adapters",
&ov::genai::LTXVideoTransformer3DModel::set_adapters,
py::arg("adapters"))
.def("infer",
&ov::genai::LTXVideoTransformer3DModel::infer,
py::call_guard<py::gil_scoped_release>(),
py::arg("latent"),
py::arg("timestep"),
R"(
Performs inference.
latent (ov.Tensor): Latent video tensor.
timestep (ov.Tensor): Current timestep tensor.
Returns: Denoised latent tensor.
)");
}
void init_autoencoder_kl_ltx_video(py::module_& m) {
auto vae =
py::class_<ov::genai::AutoencoderKLLTXVideo>(m,
"AutoencoderKLLTXVideo",
"AutoencoderKLLTXVideo class for LTX-Video VAE decoding.")
.def(py::init([](const std::filesystem::path& vae_decoder_path) {
return std::make_unique<ov::genai::AutoencoderKLLTXVideo>(vae_decoder_path);
}),
py::arg("vae_decoder_path"),
R"(
AutoencoderKLLTXVideo class constructor with decoder only.
vae_decoder_path (os.PathLike): VAE decoder directory.
)")
.def(py::init(
[](const std::filesystem::path& vae_encoder_path, const std::filesystem::path& vae_decoder_path) {
return std::make_unique<ov::genai::AutoencoderKLLTXVideo>(vae_encoder_path, vae_decoder_path);
}),
py::arg("vae_encoder_path"),
py::arg("vae_decoder_path"),
R"(
AutoencoderKLLTXVideo class constructor with encoder and decoder.
vae_encoder_path (os.PathLike): VAE encoder directory.
vae_decoder_path (os.PathLike): VAE decoder directory.
)")
.def(py::init([](const std::filesystem::path& vae_decoder_path,
const std::string& device,
const py::kwargs& kwargs) {
return std::make_unique<ov::genai::AutoencoderKLLTXVideo>(vae_decoder_path,
device,
pyutils::kwargs_to_any_map(kwargs));
}),
py::arg("vae_decoder_path"),
py::arg("device"),
R"(
AutoencoderKLLTXVideo class constructor with decoder only.
vae_decoder_path (os.PathLike): VAE decoder directory.
device (str): Device on which inference will be done.
kwargs: Device properties.
)")
.def(py::init([](const std::filesystem::path& vae_encoder_path,
const std::filesystem::path& vae_decoder_path,
const std::string& device,
const py::kwargs& kwargs) {
return std::make_unique<ov::genai::AutoencoderKLLTXVideo>(vae_encoder_path,
vae_decoder_path,
device,
pyutils::kwargs_to_any_map(kwargs));
}),
py::arg("vae_encoder_path"),
py::arg("vae_decoder_path"),
py::arg("device"),
R"(
AutoencoderKLLTXVideo class constructor with encoder and decoder.
vae_encoder_path (os.PathLike): VAE encoder directory.
vae_decoder_path (os.PathLike): VAE decoder directory.
device (str): Device on which inference will be done.
kwargs: Device properties.
)");
py::class_<ov::genai::AutoencoderKLLTXVideo::Config>(vae, "Config", "Configuration for AutoencoderKLLTXVideo.")
.def(py::init([](const std::filesystem::path& config_path) {
return std::make_unique<ov::genai::AutoencoderKLLTXVideo::Config>(config_path);
}),
py::arg("config_path"))
.def_readonly("in_channels", &ov::genai::AutoencoderKLLTXVideo::Config::in_channels)
.def_readonly("latent_channels", &ov::genai::AutoencoderKLLTXVideo::Config::latent_channels)
.def_readonly("out_channels", &ov::genai::AutoencoderKLLTXVideo::Config::out_channels)
.def_readonly("scaling_factor", &ov::genai::AutoencoderKLLTXVideo::Config::scaling_factor)
.def_readonly("block_out_channels", &ov::genai::AutoencoderKLLTXVideo::Config::block_out_channels)
.def_readonly("patch_size", &ov::genai::AutoencoderKLLTXVideo::Config::patch_size)
.def_readonly("patch_size_t", &ov::genai::AutoencoderKLLTXVideo::Config::patch_size_t);
vae.def("get_config", &ov::genai::AutoencoderKLLTXVideo::get_config)
.def("get_vae_scale_factor", &ov::genai::AutoencoderKLLTXVideo::get_vae_scale_factor)
.def(
"compile",
[](ov::genai::AutoencoderKLLTXVideo& self, const std::string& device, const py::kwargs& kwargs) {
auto properties = pyutils::kwargs_to_any_map(kwargs);
py::gil_scoped_release rel;
return self.compile(device, properties);
},
py::arg("device"),
R"(
Compiles the model.
device (str): Device to run the model on (e.g., CPU, GPU).
kwargs: Device properties.
)")
.def("reshape",
&ov::genai::AutoencoderKLLTXVideo::reshape,
py::arg("batch_size"),
py::arg("num_frames"),
py::arg("height"),
py::arg("width"),
R"(
Reshapes the model for specific input dimensions.
batch_size (int): Batch size.
num_frames (int): Number of video frames.
height (int): Video height.
width (int): Video width.
)")
.def("decode",
&ov::genai::AutoencoderKLLTXVideo::decode,
py::call_guard<py::gil_scoped_release>(),
py::arg("latent"),
R"(
Decodes latent video to pixel space.
latent (ov.Tensor): Latent video tensor.
Returns: Decoded video tensor.
)");
}
void init_video_generation_models(py::module_& m) {
init_ltx_video_transformer_3d_model(m);
init_autoencoder_kl_ltx_video(m);
}