mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-08-10 06:07:58 +00:00
Compare commits
No commits in common. "ea7f0c87cfe4c673263b4c201c596c7f1cbe2528" and "db99efdd6d2a43c7937fd55b3359206c680a75b0" have entirely different histories.
ea7f0c87cf
...
db99efdd6d
@ -11,11 +11,10 @@ endif()
|
|||||||
if (MSVC)
|
if (MSVC)
|
||||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||||
add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
|
add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
|
||||||
# /MP is MSVC-only: icx rejects it outright once offloading is enabled.
|
|
||||||
add_compile_options(
|
add_compile_options(
|
||||||
$<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:MSVC>>:/MP>
|
$<$<COMPILE_LANGUAGE:C>:/MP>
|
||||||
$<$<COMPILE_LANGUAGE:C>:/utf-8>
|
$<$<COMPILE_LANGUAGE:C>:/utf-8>
|
||||||
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/MP>
|
$<$<COMPILE_LANGUAGE:CXX>:/MP>
|
||||||
$<$<COMPILE_LANGUAGE:CXX>:/utf-8>
|
$<$<COMPILE_LANGUAGE:CXX>:/utf-8>
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@ -15,7 +15,6 @@ API and command-line option may change frequently.***
|
|||||||
|
|
||||||
## 🔥Important News
|
## 🔥Important News
|
||||||
|
|
||||||
* **2026/08/04** 🚀 stable-diffusion.cpp adds **Day-1 support for MiniMax-H3**
|
|
||||||
* **2026/06/25** 🚀 stable-diffusion.cpp now supports **Krea2**
|
* **2026/06/25** 🚀 stable-diffusion.cpp now supports **Krea2**
|
||||||
* **2026/06/04** 🚀 stable-diffusion.cpp now supports **Ideogram4**
|
* **2026/06/04** 🚀 stable-diffusion.cpp now supports **Ideogram4**
|
||||||
* **2026/05/31** 🚀 stable-diffusion.cpp now supports **PiD**
|
* **2026/05/31** 🚀 stable-diffusion.cpp now supports **PiD**
|
||||||
@ -67,7 +66,6 @@ API and command-line option may change frequently.***
|
|||||||
- [Mage-Flow-Edit](./docs/mage_flow.md#image-editing)
|
- [Mage-Flow-Edit](./docs/mage_flow.md#image-editing)
|
||||||
- Video Models
|
- Video Models
|
||||||
- [Wan2.1/Wan2.2](./docs/wan.md)
|
- [Wan2.1/Wan2.2](./docs/wan.md)
|
||||||
- [MiniMax-H3](./docs/minimax_h3.md)
|
|
||||||
- [LTX-2.3](./docs/ltx2.md)
|
- [LTX-2.3](./docs/ltx2.md)
|
||||||
- [HunyuanVideo 1.5](./docs/hunyuan_video.md)
|
- [HunyuanVideo 1.5](./docs/hunyuan_video.md)
|
||||||
- [LingBot-Video](./docs/lingbot_video.md)
|
- [LingBot-Video](./docs/lingbot_video.md)
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,96 +0,0 @@
|
|||||||
# MiniMax-H3
|
|
||||||
|
|
||||||
MiniMax-H3 jointly generates video and stereo audio with a packed diffusion
|
|
||||||
transformer. The implementation supports text-to-audio-video (T2VA), optional
|
|
||||||
first-frame conditioning (I2VA), first/last-frame conditioning (FL2VA), and
|
|
||||||
image/video/audio reference conditioning (Ref2VA).
|
|
||||||
|
|
||||||
## Model files
|
|
||||||
|
|
||||||
Pass the four MiniMax-H3 components separately:
|
|
||||||
|
|
||||||
- `--diffusion-model`: MiniMax-H3 diffusion transformer
|
|
||||||
- `--vae`: MiniMax-H3 video VAE
|
|
||||||
- `--audio-vae`: MiniMax-H3 audio VAE
|
|
||||||
- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder checkpoint
|
|
||||||
|
|
||||||
The text encoder must be the MiniMax-H3 variant: Qwen3-VL-32B truncated to 50
|
|
||||||
language layers and exported without the final language-model normalization.
|
|
||||||
Its Qwen3-VL vision tower, including the three DeepStack mergers, must also be
|
|
||||||
present. If the vision tower is stored separately, pass it with `--llm_vision`.
|
|
||||||
|
|
||||||
Both the original time-embedder DiT and the smaller AdaLN curve-table variant
|
|
||||||
are detected from their weights.
|
|
||||||
|
|
||||||
### Download weights
|
|
||||||
|
|
||||||
- Download minimax_h3_fl2va/minimax_h3_ref2va
|
|
||||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/diffusion_models
|
|
||||||
- gguf: https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main
|
|
||||||
- Download qwen3vl_32b_minimax_h3
|
|
||||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/text_encoders
|
|
||||||
- gguf: https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main
|
|
||||||
- Download vae
|
|
||||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae
|
|
||||||
- Download audio vae
|
|
||||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae
|
|
||||||
|
|
||||||
## Text-to-audio-video
|
|
||||||
|
|
||||||
```sh
|
|
||||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_fl2va-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "A cute American Shorthair silver tabby kitten surfs on a tropical ocean wave, riding a white surfboard with the clear text 'sd.cpp' on it. Cinematic tracking shot, realistic water, bright sunlight, smooth motion, and consistent character appearance. Add upbeat tropical surf-rock background music with cheerful drums and guitar, synchronized with the kitten’s energetic surfing." --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
|
||||||
```
|
|
||||||
|
|
||||||
<video src=../assets/minimax-h3/t2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
|
||||||
|
|
||||||
Omitting `--audio-vae` still runs the joint diffusion model but produces video without a
|
|
||||||
decoded audio track.
|
|
||||||
|
|
||||||
## First/last-frame conditioning
|
|
||||||
|
|
||||||
Add `--init-img` for I2VA, or both `--init-img` and `--end-img` for FL2VA:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_fl2va-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "a lovely cat" -i ..\assets\ernie_image\turbo_example.png --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
|
||||||
```
|
|
||||||
|
|
||||||
<video src=../assets/minimax-h3/i2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
|
||||||
|
|
||||||
## Reference-to-audio-video conditioning
|
|
||||||
|
|
||||||
Ref2VA accepts any combination of reference images, reference videos, paired
|
|
||||||
video soundtracks, and standalone audio references:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_ref2va_pruned-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "Use the cat from <Picture 1> as the main character. Keep the cat’s appearance, fur color, facial features, and identity consistent with the reference image. Create a 2-second cinematic video: start with an extreme close-up shot of the cat’s face, focusing on its cute expression and detailed fur texture. The camera slowly rotates around the cat’s head, creating a dynamic reveal. Then smoothly pull back and zoom out to reveal the full scene: the cat is standing confidently on a surfboard, riding ocean waves. Water splashes around the board, sea breeze gently moves the cat’s fur, and the cat maintains a cute and fearless expression while surfing. Smooth camera movement, cinematic orbit shot, seamless zoom-out transition, low-angle wide shot, realistic ocean environment, golden sunlight, dynamic waves, high-quality realistic style, natural motion, no distortion, keep the cat’s identity unchanged." -r ..\assets\ernie_image\turbo_example.png --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
|
||||||
```
|
|
||||||
|
|
||||||
<video src=../assets/minimax-h3/r2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
|
||||||
|
|
||||||
`--ref-image`, `--ref-video`, and `--ref-audio` can each be repeated. A
|
|
||||||
reference video is a directory of image frames sorted lexicographically and is
|
|
||||||
treated as 24 fps. Repeated `--ref-video-audio` WAV files are paired by index
|
|
||||||
with repeated `--ref-video` inputs. WAV PCM (8/16/24/32-bit) and 32/64-bit
|
|
||||||
floating-point samples are accepted; audio is converted to stereo 32 kHz by the
|
|
||||||
pipeline.
|
|
||||||
|
|
||||||
Reference inputs are presented to Qwen3-VL in image, video, then audio order.
|
|
||||||
Videos are sampled at 2 fps for the Qwen presentation while their full 24 fps
|
|
||||||
latents condition the diffusion transformer. Paired video and audio references
|
|
||||||
share the same timeline. Ref2VA cannot be combined with `--init-img` or
|
|
||||||
`--end-img` in one request.
|
|
||||||
|
|
||||||
Reference images keep their aspect ratio and are only downscaled when their
|
|
||||||
pixel area exceeds the requested generation canvas.
|
|
||||||
|
|
||||||
The C API exposes the same inputs through `ref_images`, `ref_videos`, and
|
|
||||||
`ref_audios` in `sd_vid_gen_params_t`. Each `sd_ref_video_t` supplies its own
|
|
||||||
frame rate and optional soundtrack; non-24-fps inputs are resampled internally.
|
|
||||||
|
|
||||||
## Shape and runtime notes
|
|
||||||
|
|
||||||
- Width and height are aligned upward to a multiple of 32.
|
|
||||||
- Frame count is aligned upward to the `17k + 5` grid, with a minimum of 5.
|
|
||||||
- MiniMax-H3 runs at 24 fps; another requested value is overridden.
|
|
||||||
- The default video flow shift is 12. The audio stream is mapped internally to
|
|
||||||
its shift of 3, so the regular samplers can operate on the packed AV latent.
|
|
||||||
@ -754,18 +754,6 @@ int main(int argc, const char* argv[]) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto load_audio = [&](const std::string& path, SDAudioOwner& audio) -> bool {
|
|
||||||
std::vector<float> samples;
|
|
||||||
uint32_t sample_rate = 0;
|
|
||||||
uint32_t channels = 0;
|
|
||||||
if (!load_wav_from_file(path, samples, sample_rate, channels)) {
|
|
||||||
LOG_ERROR("load WAV audio from '%s' failed", path.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
audio.reset(std::move(samples), sample_rate, channels);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (gen_params.init_image_path.size() > 0) {
|
if (gen_params.init_image_path.size() > 0) {
|
||||||
if (!load_image_and_update_size(gen_params.init_image_path, gen_params.init_image)) {
|
if (!load_image_and_update_size(gen_params.init_image_path, gen_params.init_image)) {
|
||||||
return 1;
|
return 1;
|
||||||
@ -789,37 +777,6 @@ int main(int argc, const char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!gen_params.ref_video_paths.empty()) {
|
|
||||||
gen_params.ref_videos.clear();
|
|
||||||
gen_params.ref_videos.reserve(gen_params.ref_video_paths.size());
|
|
||||||
for (const auto& path : gen_params.ref_video_paths) {
|
|
||||||
std::vector<SDImageOwner> frames;
|
|
||||||
if (!load_images_from_dir(path, frames, 0, 0, 0, cli_params.verbose) || frames.empty()) {
|
|
||||||
LOG_ERROR("load reference video frames from '%s' failed", path.c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
gen_params.ref_videos.push_back(std::move(frames));
|
|
||||||
}
|
|
||||||
|
|
||||||
gen_params.ref_video_audios.clear();
|
|
||||||
gen_params.ref_video_audios.resize(gen_params.ref_videos.size());
|
|
||||||
for (size_t i = 0; i < gen_params.ref_video_audio_paths.size(); ++i) {
|
|
||||||
if (!load_audio(gen_params.ref_video_audio_paths[i], gen_params.ref_video_audios[i])) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!gen_params.ref_audio_paths.empty()) {
|
|
||||||
gen_params.ref_audios.clear();
|
|
||||||
gen_params.ref_audios.resize(gen_params.ref_audio_paths.size());
|
|
||||||
for (size_t i = 0; i < gen_params.ref_audio_paths.size(); ++i) {
|
|
||||||
if (!load_audio(gen_params.ref_audio_paths[i], gen_params.ref_audios[i])) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gen_params.mask_image_path.size() > 0) {
|
if (gen_params.mask_image_path.size() > 0) {
|
||||||
if (!load_sd_image_from_file(gen_params.mask_image.put(),
|
if (!load_sd_image_from_file(gen_params.mask_image.put(),
|
||||||
gen_params.mask_image_path.c_str(),
|
gen_params.mask_image_path.c_str(),
|
||||||
|
|||||||
@ -1404,30 +1404,6 @@ ArgOptions SDGenerationParams::get_options() {
|
|||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto on_ref_video_arg = [&](int argc, const char** argv, int index) {
|
|
||||||
if (++index >= argc) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ref_video_paths.push_back(argv[index]);
|
|
||||||
return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto on_ref_video_audio_arg = [&](int argc, const char** argv, int index) {
|
|
||||||
if (++index >= argc) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ref_video_audio_paths.push_back(argv[index]);
|
|
||||||
return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto on_ref_audio_arg = [&](int argc, const char** argv, int index) {
|
|
||||||
if (++index >= argc) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ref_audio_paths.push_back(argv[index]);
|
|
||||||
return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto on_cache_mode_arg = [&](int argc, const char** argv, int index) {
|
auto on_cache_mode_arg = [&](int argc, const char** argv, int index) {
|
||||||
if (++index >= argc) {
|
if (++index >= argc) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -1592,20 +1568,8 @@ ArgOptions SDGenerationParams::get_options() {
|
|||||||
on_high_noise_skip_layers_arg},
|
on_high_noise_skip_layers_arg},
|
||||||
{"-r",
|
{"-r",
|
||||||
"--ref-image",
|
"--ref-image",
|
||||||
"reference image for Flux Kontext or MiniMax-H3 Ref2VA (can be used multiple times)",
|
"reference image for Flux Kontext models (can be used multiple times)",
|
||||||
on_ref_image_arg},
|
on_ref_image_arg},
|
||||||
{"",
|
|
||||||
"--ref-video",
|
|
||||||
"MiniMax-H3 Ref2VA reference video frame directory at 24 fps (can be used multiple times)",
|
|
||||||
on_ref_video_arg},
|
|
||||||
{"",
|
|
||||||
"--ref-video-audio",
|
|
||||||
"WAV soundtrack paired by index with --ref-video (can be used multiple times)",
|
|
||||||
on_ref_video_audio_arg},
|
|
||||||
{"",
|
|
||||||
"--ref-audio",
|
|
||||||
"standalone WAV reference for MiniMax-H3 Ref2VA (can be used multiple times)",
|
|
||||||
on_ref_audio_arg},
|
|
||||||
{"",
|
{"",
|
||||||
"--cache-mode",
|
"--cache-mode",
|
||||||
"caching method: 'easycache' (DiT), 'ucache' (UNET), 'dbcache'/'taylorseer'/'cache-dit' (DiT block-level), 'spectrum' (UNET/DiT Chebyshev+Taylor forecasting)",
|
"caching method: 'easycache' (DiT), 'ucache' (UNET), 'dbcache'/'taylorseer'/'cache-dit' (DiT block-level), 'spectrum' (UNET/DiT Chebyshev+Taylor forecasting)",
|
||||||
@ -2402,16 +2366,6 @@ bool SDGenerationParams::validate(SDMode mode) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ref_video_audio_paths.size() > ref_video_paths.size()) {
|
|
||||||
LOG_ERROR("error: each --ref-video-audio needs a corresponding --ref-video");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode != VID_GEN && (!ref_video_paths.empty() || !ref_video_audio_paths.empty() || !ref_audio_paths.empty())) {
|
|
||||||
LOG_ERROR("error: reference video and audio inputs require vid_gen mode");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sample_params.shifted_timestep < 0 || sample_params.shifted_timestep > 1000) {
|
if (sample_params.shifted_timestep < 0 || sample_params.shifted_timestep > 1000) {
|
||||||
LOG_ERROR("error: shifted_timestep must be in range [0, 1000]");
|
LOG_ERROR("error: shifted_timestep must be in range [0, 1000]");
|
||||||
return false;
|
return false;
|
||||||
@ -2606,35 +2560,6 @@ sd_vid_gen_params_t SDGenerationParams::to_sd_vid_gen_params_t() {
|
|||||||
control_frame_views.push_back(frame.get());
|
control_frame_views.push_back(frame.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
ref_image_views.clear();
|
|
||||||
ref_image_views.reserve(ref_images.size());
|
|
||||||
for (auto& image : ref_images) {
|
|
||||||
ref_image_views.push_back(image.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
ref_video_frame_views.clear();
|
|
||||||
ref_video_frame_views.resize(ref_videos.size());
|
|
||||||
ref_video_views.clear();
|
|
||||||
ref_video_views.reserve(ref_videos.size());
|
|
||||||
for (size_t i = 0; i < ref_videos.size(); ++i) {
|
|
||||||
auto& frame_views = ref_video_frame_views[i];
|
|
||||||
frame_views.reserve(ref_videos[i].size());
|
|
||||||
for (auto& frame : ref_videos[i]) {
|
|
||||||
frame_views.push_back(frame.get());
|
|
||||||
}
|
|
||||||
sd_audio_t audio = i < ref_video_audios.size() ? ref_video_audios[i].get() : sd_audio_t{};
|
|
||||||
ref_video_views.push_back({frame_views.empty() ? nullptr : frame_views.data(),
|
|
||||||
static_cast<int>(frame_views.size()),
|
|
||||||
24,
|
|
||||||
audio});
|
|
||||||
}
|
|
||||||
|
|
||||||
ref_audio_views.clear();
|
|
||||||
ref_audio_views.reserve(ref_audios.size());
|
|
||||||
for (auto& audio : ref_audios) {
|
|
||||||
ref_audio_views.push_back(audio.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
sample_params.guidance.slg.layers = skip_layers.empty() ? nullptr : skip_layers.data();
|
sample_params.guidance.slg.layers = skip_layers.empty() ? nullptr : skip_layers.data();
|
||||||
sample_params.guidance.slg.layer_count = skip_layers.size();
|
sample_params.guidance.slg.layer_count = skip_layers.size();
|
||||||
high_noise_sample_params.guidance.slg.layers = high_noise_skip_layers.empty() ? nullptr : high_noise_skip_layers.data();
|
high_noise_sample_params.guidance.slg.layers = high_noise_skip_layers.empty() ? nullptr : high_noise_skip_layers.data();
|
||||||
@ -2653,12 +2578,6 @@ sd_vid_gen_params_t SDGenerationParams::to_sd_vid_gen_params_t() {
|
|||||||
params.clip_skip = clip_skip;
|
params.clip_skip = clip_skip;
|
||||||
params.init_image = init_image.get();
|
params.init_image = init_image.get();
|
||||||
params.end_image = end_image.get();
|
params.end_image = end_image.get();
|
||||||
params.ref_images = ref_image_views.empty() ? nullptr : ref_image_views.data();
|
|
||||||
params.ref_images_count = static_cast<int>(ref_image_views.size());
|
|
||||||
params.ref_videos = ref_video_views.empty() ? nullptr : ref_video_views.data();
|
|
||||||
params.ref_videos_count = static_cast<int>(ref_video_views.size());
|
|
||||||
params.ref_audios = ref_audio_views.empty() ? nullptr : ref_audio_views.data();
|
|
||||||
params.ref_audios_count = static_cast<int>(ref_audio_views.size());
|
|
||||||
params.control_frames = control_frame_views.empty() ? nullptr : control_frame_views.data();
|
params.control_frames = control_frame_views.empty() ? nullptr : control_frame_views.data();
|
||||||
params.control_frames_size = static_cast<int>(control_frame_views.size());
|
params.control_frames_size = static_cast<int>(control_frame_views.size());
|
||||||
params.width = get_resolved_width();
|
params.width = get_resolved_width();
|
||||||
@ -2738,9 +2657,6 @@ std::string SDGenerationParams::to_string() const {
|
|||||||
<< " mask_image_path: \"" << mask_image_path << "\",\n"
|
<< " mask_image_path: \"" << mask_image_path << "\",\n"
|
||||||
<< " control_image_path: \"" << control_image_path << "\",\n"
|
<< " control_image_path: \"" << control_image_path << "\",\n"
|
||||||
<< " ref_image_paths: " << vec_str_to_string(ref_image_paths) << ",\n"
|
<< " ref_image_paths: " << vec_str_to_string(ref_image_paths) << ",\n"
|
||||||
<< " ref_video_paths: " << vec_str_to_string(ref_video_paths) << ",\n"
|
|
||||||
<< " ref_video_audio_paths: " << vec_str_to_string(ref_video_audio_paths) << ",\n"
|
|
||||||
<< " ref_audio_paths: " << vec_str_to_string(ref_audio_paths) << ",\n"
|
|
||||||
<< " control_video_path: \"" << control_video_path << "\",\n"
|
<< " control_video_path: \"" << control_video_path << "\",\n"
|
||||||
<< " auto_resize_ref_image: " << (auto_resize_ref_image ? "true" : "false") << ",\n"
|
<< " auto_resize_ref_image: " << (auto_resize_ref_image ? "true" : "false") << ",\n"
|
||||||
<< " increase_ref_index: " << (increase_ref_index ? "true" : "false") << ",\n"
|
<< " increase_ref_index: " << (increase_ref_index ? "true" : "false") << ",\n"
|
||||||
|
|||||||
@ -212,9 +212,6 @@ struct SDGenerationParams {
|
|||||||
std::string control_image_path;
|
std::string control_image_path;
|
||||||
std::string ip_adapter_image_path;
|
std::string ip_adapter_image_path;
|
||||||
std::vector<std::string> ref_image_paths;
|
std::vector<std::string> ref_image_paths;
|
||||||
std::vector<std::string> ref_video_paths;
|
|
||||||
std::vector<std::string> ref_video_audio_paths;
|
|
||||||
std::vector<std::string> ref_audio_paths;
|
|
||||||
std::string control_video_path;
|
std::string control_video_path;
|
||||||
|
|
||||||
sd_sample_params_t sample_params;
|
sd_sample_params_t sample_params;
|
||||||
@ -278,9 +275,6 @@ struct SDGenerationParams {
|
|||||||
SDImageOwner init_image;
|
SDImageOwner init_image;
|
||||||
SDImageOwner end_image;
|
SDImageOwner end_image;
|
||||||
std::vector<SDImageOwner> ref_images;
|
std::vector<SDImageOwner> ref_images;
|
||||||
std::vector<std::vector<SDImageOwner>> ref_videos;
|
|
||||||
std::vector<SDAudioOwner> ref_video_audios;
|
|
||||||
std::vector<SDAudioOwner> ref_audios;
|
|
||||||
SDImageOwner mask_image;
|
SDImageOwner mask_image;
|
||||||
SDImageOwner control_image;
|
SDImageOwner control_image;
|
||||||
SDImageOwner ip_adapter_image;
|
SDImageOwner ip_adapter_image;
|
||||||
@ -289,9 +283,6 @@ struct SDGenerationParams {
|
|||||||
|
|
||||||
// Backing storage for sd_img_gen_params_t view fields.
|
// Backing storage for sd_img_gen_params_t view fields.
|
||||||
std::vector<sd_image_t> ref_image_views;
|
std::vector<sd_image_t> ref_image_views;
|
||||||
std::vector<std::vector<sd_image_t>> ref_video_frame_views;
|
|
||||||
std::vector<sd_ref_video_t> ref_video_views;
|
|
||||||
std::vector<sd_audio_t> ref_audio_views;
|
|
||||||
std::vector<sd_image_t> pm_id_image_views;
|
std::vector<sd_image_t> pm_id_image_views;
|
||||||
std::vector<sd_image_t> control_frame_views;
|
std::vector<sd_image_t> control_frame_views;
|
||||||
|
|
||||||
|
|||||||
@ -1374,132 +1374,3 @@ bool write_wav_to_file(const std::string& path,
|
|||||||
file.write(reinterpret_cast<const char*>(pcm.data()), static_cast<std::streamsize>(pcm.size() * sizeof(int16_t)));
|
file.write(reinterpret_cast<const char*>(pcm.data()), static_cast<std::streamsize>(pcm.size() * sizeof(int16_t)));
|
||||||
return file.good();
|
return file.good();
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t read_le16(const uint8_t* data) {
|
|
||||||
return static_cast<uint16_t>(data[0]) |
|
|
||||||
(static_cast<uint16_t>(data[1]) << 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t read_le32(const uint8_t* data) {
|
|
||||||
return static_cast<uint32_t>(data[0]) |
|
|
||||||
(static_cast<uint32_t>(data[1]) << 8) |
|
|
||||||
(static_cast<uint32_t>(data[2]) << 16) |
|
|
||||||
(static_cast<uint32_t>(data[3]) << 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool load_wav_from_file(const std::string& path,
|
|
||||||
std::vector<float>& interleaved_samples,
|
|
||||||
uint32_t& sample_rate,
|
|
||||||
uint32_t& channels) {
|
|
||||||
interleaved_samples.clear();
|
|
||||||
sample_rate = 0;
|
|
||||||
channels = 0;
|
|
||||||
|
|
||||||
std::ifstream file(path, std::ios::binary);
|
|
||||||
uint8_t riff_header[12];
|
|
||||||
if (!file.read(reinterpret_cast<char*>(riff_header), sizeof(riff_header)) ||
|
|
||||||
std::memcmp(riff_header, "RIFF", 4) != 0 ||
|
|
||||||
std::memcmp(riff_header + 8, "WAVE", 4) != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t audio_format = 0;
|
|
||||||
uint16_t bits_per_sample = 0;
|
|
||||||
uint16_t block_align = 0;
|
|
||||||
std::streampos data_pos = std::streampos(-1);
|
|
||||||
uint32_t data_size = 0;
|
|
||||||
|
|
||||||
while (file.good()) {
|
|
||||||
uint8_t chunk_header[8];
|
|
||||||
if (!file.read(reinterpret_cast<char*>(chunk_header), sizeof(chunk_header))) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
uint32_t chunk_size = read_le32(chunk_header + 4);
|
|
||||||
std::streampos chunk_data_pos = file.tellg();
|
|
||||||
|
|
||||||
if (std::memcmp(chunk_header, "fmt ", 4) == 0) {
|
|
||||||
if (chunk_size < 16) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::vector<uint8_t> fmt(chunk_size);
|
|
||||||
if (!file.read(reinterpret_cast<char*>(fmt.data()), chunk_size)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
audio_format = read_le16(fmt.data());
|
|
||||||
channels = read_le16(fmt.data() + 2);
|
|
||||||
sample_rate = read_le32(fmt.data() + 4);
|
|
||||||
block_align = read_le16(fmt.data() + 12);
|
|
||||||
bits_per_sample = read_le16(fmt.data() + 14);
|
|
||||||
if (audio_format == 0xfffe && chunk_size >= 40) {
|
|
||||||
audio_format = read_le16(fmt.data() + 24);
|
|
||||||
}
|
|
||||||
} else if (std::memcmp(chunk_header, "data", 4) == 0) {
|
|
||||||
data_pos = chunk_data_pos;
|
|
||||||
data_size = chunk_size;
|
|
||||||
file.seekg(chunk_size, std::ios::cur);
|
|
||||||
} else {
|
|
||||||
file.seekg(chunk_size, std::ios::cur);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file.good()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if ((chunk_size & 1) != 0) {
|
|
||||||
file.seekg(1, std::ios::cur);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const uint32_t bytes_per_sample = (bits_per_sample + 7) / 8;
|
|
||||||
if (data_pos == std::streampos(-1) || data_size == 0 || channels == 0 || sample_rate == 0 ||
|
|
||||||
block_align == 0 || bytes_per_sample == 0 || block_align < channels * bytes_per_sample ||
|
|
||||||
(audio_format != 1 && audio_format != 3)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const uint64_t frame_count = data_size / block_align;
|
|
||||||
if (frame_count == 0 || frame_count > SIZE_MAX / channels) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::vector<uint8_t> pcm(data_size);
|
|
||||||
file.clear();
|
|
||||||
file.seekg(data_pos);
|
|
||||||
if (!file.read(reinterpret_cast<char*>(pcm.data()), data_size)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
interleaved_samples.resize(static_cast<size_t>(frame_count * channels));
|
|
||||||
for (uint64_t frame = 0; frame < frame_count; ++frame) {
|
|
||||||
const uint8_t* frame_data = pcm.data() + frame * block_align;
|
|
||||||
for (uint32_t channel = 0; channel < channels; ++channel) {
|
|
||||||
const uint8_t* sample_data = frame_data + channel * bytes_per_sample;
|
|
||||||
float sample = 0.0f;
|
|
||||||
if (audio_format == 3 && bits_per_sample == 32) {
|
|
||||||
std::memcpy(&sample, sample_data, sizeof(sample));
|
|
||||||
} else if (audio_format == 3 && bits_per_sample == 64) {
|
|
||||||
double value;
|
|
||||||
std::memcpy(&value, sample_data, sizeof(value));
|
|
||||||
sample = static_cast<float>(value);
|
|
||||||
} else if (audio_format == 1 && bits_per_sample == 8) {
|
|
||||||
sample = (static_cast<int>(sample_data[0]) - 128) / 128.0f;
|
|
||||||
} else if (audio_format == 1 && bits_per_sample == 16) {
|
|
||||||
sample = static_cast<int16_t>(read_le16(sample_data)) / 32768.0f;
|
|
||||||
} else if (audio_format == 1 && bits_per_sample == 24) {
|
|
||||||
int32_t value = static_cast<int32_t>(sample_data[0]) |
|
|
||||||
(static_cast<int32_t>(sample_data[1]) << 8) |
|
|
||||||
(static_cast<int32_t>(sample_data[2]) << 16);
|
|
||||||
if ((value & 0x800000) != 0) {
|
|
||||||
value |= ~0xffffff;
|
|
||||||
}
|
|
||||||
sample = value / 8388608.0f;
|
|
||||||
} else if (audio_format == 1 && bits_per_sample == 32) {
|
|
||||||
int32_t value = static_cast<int32_t>(read_le32(sample_data));
|
|
||||||
sample = value / 2147483648.0f;
|
|
||||||
} else {
|
|
||||||
interleaved_samples.clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
interleaved_samples[static_cast<size_t>(frame * channels + channel)] = sample;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -110,9 +110,4 @@ bool write_wav_to_file(const std::string& path,
|
|||||||
uint32_t channels,
|
uint32_t channels,
|
||||||
uint32_t sample_rate);
|
uint32_t sample_rate);
|
||||||
|
|
||||||
bool load_wav_from_file(const std::string& path,
|
|
||||||
std::vector<float>& interleaved_samples,
|
|
||||||
uint32_t& sample_rate,
|
|
||||||
uint32_t& channels);
|
|
||||||
|
|
||||||
#endif // __MEDIA_IO_H__
|
#endif // __MEDIA_IO_H__
|
||||||
|
|||||||
@ -141,37 +141,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SDAudioOwner {
|
|
||||||
private:
|
|
||||||
uint32_t sample_rate_ = 0;
|
|
||||||
uint32_t channels_ = 0;
|
|
||||||
std::vector<float> samples_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SDAudioOwner() = default;
|
|
||||||
|
|
||||||
void reset(std::vector<float> samples = {}, uint32_t sample_rate = 0, uint32_t channels = 0) {
|
|
||||||
samples_ = std::move(samples);
|
|
||||||
sample_rate_ = sample_rate;
|
|
||||||
channels_ = channels;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const {
|
|
||||||
return samples_.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
sd_audio_t get() {
|
|
||||||
return {sample_rate_,
|
|
||||||
channels_,
|
|
||||||
channels_ == 0 ? 0 : static_cast<uint64_t>(samples_.size() / channels_),
|
|
||||||
samples_.empty() ? nullptr : samples_.data()};
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<float>& samples() const {
|
|
||||||
return samples_;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class SDImageVec {
|
class SDImageVec {
|
||||||
private:
|
private:
|
||||||
std::vector<sd_image_t> images_;
|
std::vector<sd_image_t> images_;
|
||||||
|
|||||||
@ -247,13 +247,6 @@ typedef struct {
|
|||||||
uint8_t* data;
|
uint8_t* data;
|
||||||
} sd_image_t;
|
} sd_image_t;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
sd_image_t* frames;
|
|
||||||
int frame_count;
|
|
||||||
int fps;
|
|
||||||
sd_audio_t audio;
|
|
||||||
} sd_ref_video_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int* layers;
|
int* layers;
|
||||||
size_t layer_count;
|
size_t layer_count;
|
||||||
@ -404,12 +397,6 @@ typedef struct {
|
|||||||
int clip_skip;
|
int clip_skip;
|
||||||
sd_image_t init_image;
|
sd_image_t init_image;
|
||||||
sd_image_t end_image;
|
sd_image_t end_image;
|
||||||
sd_image_t* ref_images;
|
|
||||||
int ref_images_count;
|
|
||||||
sd_ref_video_t* ref_videos;
|
|
||||||
int ref_videos_count;
|
|
||||||
sd_audio_t* ref_audios;
|
|
||||||
int ref_audios_count;
|
|
||||||
sd_image_t* control_frames;
|
sd_image_t* control_frames;
|
||||||
int control_frames_size;
|
int control_frames_size;
|
||||||
int width;
|
int width;
|
||||||
|
|||||||
@ -1,335 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""Merge selected tensors from multiple safetensors files without loading weights.
|
|
||||||
|
|
||||||
Edit ``OUTPUT_PATH`` and ``SOURCE_RULES`` below, then run:
|
|
||||||
|
|
||||||
python scripts/merge_safetensors.py
|
|
||||||
|
|
||||||
Each source rule uses regular expressions against complete tensor names.
|
|
||||||
``include`` is required and matches when any expression succeeds. ``exclude``
|
|
||||||
wins over ``include``. Expressions are evaluated with ``re.search``.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import struct
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import BinaryIO
|
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
# Configuration
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
OUTPUT_PATH = Path(r"..\models\diffusion_models\minimax_h3_ref2va_pruned_bf16.safetensors")
|
|
||||||
|
|
||||||
SOURCE_RULES = [
|
|
||||||
{
|
|
||||||
"path": Path(r"..\models\diffusion_models\minimax_h3_ref2va_bf16.safetensors"),
|
|
||||||
"include": [r".*"],
|
|
||||||
"exclude": [r".*adaln_proj\.linear.*", r"time_embedder.*"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": Path(r"..\models\diffusion_models\minimax_h3_ref2va_pruned_int8_convrot.safetensors"),
|
|
||||||
"include": [r"^.*adaln_proj\.linear.*", "adaln_t_table"],
|
|
||||||
"exclude": [],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
# Safetensors metadata is optional. Set this to a dict[str, str] if needed.
|
|
||||||
OUTPUT_METADATA = None
|
|
||||||
|
|
||||||
# Refuse to replace an existing output unless explicitly enabled.
|
|
||||||
OVERWRITE_OUTPUT = False
|
|
||||||
|
|
||||||
# Only tensor headers and this fixed-size buffer are held in memory.
|
|
||||||
COPY_BUFFER_SIZE = 8 * 1024 * 1024
|
|
||||||
PROGRESS_INTERVAL = 1024 * 1024 * 1024
|
|
||||||
MAX_HEADER_SIZE = 256 * 1024 * 1024
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class TensorEntry:
|
|
||||||
name: str
|
|
||||||
source_path: Path
|
|
||||||
source_data_offset: int
|
|
||||||
source_start: int
|
|
||||||
source_end: int
|
|
||||||
dtype: str
|
|
||||||
shape: list[int]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def size(self) -> int:
|
|
||||||
return self.source_end - self.source_start
|
|
||||||
|
|
||||||
|
|
||||||
def format_bytes(size: int) -> str:
|
|
||||||
value = float(size)
|
|
||||||
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
|
|
||||||
if value < 1024.0 or unit == "TiB":
|
|
||||||
return f"{value:.2f} {unit}"
|
|
||||||
value /= 1024.0
|
|
||||||
raise AssertionError("unreachable")
|
|
||||||
|
|
||||||
|
|
||||||
def read_exact(file: BinaryIO, size: int, description: str) -> bytes:
|
|
||||||
data = file.read(size)
|
|
||||||
if len(data) != size:
|
|
||||||
raise ValueError(f"truncated {description}: expected {size} bytes, got {len(data)}")
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def read_safetensors_header(path: Path) -> tuple[dict, int, int]:
|
|
||||||
file_size = path.stat().st_size
|
|
||||||
with path.open("rb") as file:
|
|
||||||
header_size = struct.unpack("<Q", read_exact(file, 8, f"header size in {path}"))[0]
|
|
||||||
if header_size == 0 or header_size > MAX_HEADER_SIZE:
|
|
||||||
raise ValueError(
|
|
||||||
f"invalid header size in {path}: {header_size} "
|
|
||||||
f"(limit: {MAX_HEADER_SIZE})"
|
|
||||||
)
|
|
||||||
header_bytes = read_exact(file, header_size, f"header in {path}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
header = json.loads(header_bytes)
|
|
||||||
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
|
||||||
raise ValueError(f"invalid safetensors JSON header in {path}: {error}") from error
|
|
||||||
if not isinstance(header, dict):
|
|
||||||
raise ValueError(f"safetensors header in {path} is not an object")
|
|
||||||
|
|
||||||
data_offset = 8 + header_size
|
|
||||||
if data_offset > file_size:
|
|
||||||
raise ValueError(f"safetensors data offset is past end of file: {path}")
|
|
||||||
return header, data_offset, file_size
|
|
||||||
|
|
||||||
|
|
||||||
def parse_tensor_entry(
|
|
||||||
name: str,
|
|
||||||
info: object,
|
|
||||||
source_path: Path,
|
|
||||||
source_data_offset: int,
|
|
||||||
source_file_size: int,
|
|
||||||
) -> TensorEntry:
|
|
||||||
if not isinstance(info, dict):
|
|
||||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid header entry")
|
|
||||||
|
|
||||||
dtype = info.get("dtype")
|
|
||||||
shape = info.get("shape")
|
|
||||||
offsets = info.get("data_offsets")
|
|
||||||
if not isinstance(dtype, str):
|
|
||||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid dtype")
|
|
||||||
if not isinstance(shape, list) or not all(
|
|
||||||
isinstance(dimension, int) and dimension >= 0 for dimension in shape
|
|
||||||
):
|
|
||||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid shape")
|
|
||||||
if (
|
|
||||||
not isinstance(offsets, list)
|
|
||||||
or len(offsets) != 2
|
|
||||||
or not all(isinstance(offset, int) for offset in offsets)
|
|
||||||
):
|
|
||||||
raise ValueError(f"{source_path}: tensor {name!r} has invalid data offsets")
|
|
||||||
|
|
||||||
start, end = offsets
|
|
||||||
if start < 0 or end < start or source_data_offset + end > source_file_size:
|
|
||||||
raise ValueError(
|
|
||||||
f"{source_path}: tensor {name!r} byte range [{start}, {end}) "
|
|
||||||
"is outside the file"
|
|
||||||
)
|
|
||||||
|
|
||||||
return TensorEntry(
|
|
||||||
name=name,
|
|
||||||
source_path=source_path,
|
|
||||||
source_data_offset=source_data_offset,
|
|
||||||
source_start=start,
|
|
||||||
source_end=end,
|
|
||||||
dtype=dtype,
|
|
||||||
shape=list(shape),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def compile_patterns(rule_index: int, field: str, values: object) -> list[re.Pattern[str]]:
|
|
||||||
if not isinstance(values, list) or not all(isinstance(value, str) for value in values):
|
|
||||||
raise TypeError(f"SOURCE_RULES[{rule_index}][{field!r}] must be a list of strings")
|
|
||||||
try:
|
|
||||||
return [re.compile(value) for value in values]
|
|
||||||
except re.error as error:
|
|
||||||
raise ValueError(
|
|
||||||
f"invalid regex in SOURCE_RULES[{rule_index}][{field!r}]: {error}"
|
|
||||||
) from error
|
|
||||||
|
|
||||||
|
|
||||||
def collect_entries() -> list[TensorEntry]:
|
|
||||||
if not SOURCE_RULES:
|
|
||||||
raise ValueError("SOURCE_RULES must contain at least one source")
|
|
||||||
|
|
||||||
entries: list[TensorEntry] = []
|
|
||||||
selected_by_name: dict[str, TensorEntry] = {}
|
|
||||||
header_cache: dict[Path, tuple[dict, int, int]] = {}
|
|
||||||
|
|
||||||
for rule_index, rule in enumerate(SOURCE_RULES):
|
|
||||||
if not isinstance(rule, dict) or "path" not in rule or "include" not in rule:
|
|
||||||
raise TypeError(
|
|
||||||
f"SOURCE_RULES[{rule_index}] must contain 'path' and 'include'"
|
|
||||||
)
|
|
||||||
|
|
||||||
source_path = Path(rule["path"])
|
|
||||||
if not source_path.is_file():
|
|
||||||
raise FileNotFoundError(f"source file does not exist: {source_path}")
|
|
||||||
source_path = source_path.resolve()
|
|
||||||
|
|
||||||
include = compile_patterns(rule_index, "include", rule["include"])
|
|
||||||
exclude = compile_patterns(rule_index, "exclude", rule.get("exclude", []))
|
|
||||||
if not include:
|
|
||||||
raise ValueError(f"SOURCE_RULES[{rule_index}]['include'] must not be empty")
|
|
||||||
|
|
||||||
if source_path not in header_cache:
|
|
||||||
header_cache[source_path] = read_safetensors_header(source_path)
|
|
||||||
header, data_offset, file_size = header_cache[source_path]
|
|
||||||
|
|
||||||
matched = 0
|
|
||||||
for name, info in header.items():
|
|
||||||
if name == "__metadata__":
|
|
||||||
continue
|
|
||||||
if not any(pattern.search(name) for pattern in include):
|
|
||||||
continue
|
|
||||||
if any(pattern.search(name) for pattern in exclude):
|
|
||||||
continue
|
|
||||||
|
|
||||||
entry = parse_tensor_entry(name, info, source_path, data_offset, file_size)
|
|
||||||
previous = selected_by_name.get(name)
|
|
||||||
if previous is not None:
|
|
||||||
raise ValueError(
|
|
||||||
f"tensor {name!r} was selected more than once:\n"
|
|
||||||
f" first: {previous.source_path}\n"
|
|
||||||
f" second: {source_path}"
|
|
||||||
)
|
|
||||||
selected_by_name[name] = entry
|
|
||||||
print(f"entry {entry}")
|
|
||||||
entries.append(entry)
|
|
||||||
matched += 1
|
|
||||||
|
|
||||||
print(f"Rule {rule_index}: selected {matched} tensors from {source_path}")
|
|
||||||
if matched == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"SOURCE_RULES[{rule_index}] did not select any tensors; check its regexes"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not entries:
|
|
||||||
raise ValueError("no tensors were selected")
|
|
||||||
return entries
|
|
||||||
|
|
||||||
|
|
||||||
def build_output_header(entries: list[TensorEntry]) -> tuple[bytes, int]:
|
|
||||||
header: dict[str, object] = {}
|
|
||||||
if OUTPUT_METADATA is not None:
|
|
||||||
if not isinstance(OUTPUT_METADATA, dict) or not all(
|
|
||||||
isinstance(key, str) and isinstance(value, str)
|
|
||||||
for key, value in OUTPUT_METADATA.items()
|
|
||||||
):
|
|
||||||
raise TypeError("OUTPUT_METADATA must be None or a dict[str, str]")
|
|
||||||
header["__metadata__"] = OUTPUT_METADATA
|
|
||||||
|
|
||||||
output_offset = 0
|
|
||||||
for entry in entries:
|
|
||||||
header[entry.name] = {
|
|
||||||
"dtype": entry.dtype,
|
|
||||||
"shape": entry.shape,
|
|
||||||
"data_offsets": [output_offset, output_offset + entry.size],
|
|
||||||
}
|
|
||||||
output_offset += entry.size
|
|
||||||
|
|
||||||
header_bytes = json.dumps(header, separators=(",", ":"), ensure_ascii=False).encode(
|
|
||||||
"utf-8"
|
|
||||||
)
|
|
||||||
header_bytes += b" " * (-len(header_bytes) % 8)
|
|
||||||
return header_bytes, output_offset
|
|
||||||
|
|
||||||
|
|
||||||
def copy_tensor(source: BinaryIO, output: BinaryIO, entry: TensorEntry) -> None:
|
|
||||||
source.seek(entry.source_data_offset + entry.source_start)
|
|
||||||
remaining = entry.size
|
|
||||||
while remaining:
|
|
||||||
chunk = source.read(min(COPY_BUFFER_SIZE, remaining))
|
|
||||||
if not chunk:
|
|
||||||
raise OSError(
|
|
||||||
f"unexpected end of file while copying {entry.name!r} "
|
|
||||||
f"from {entry.source_path}"
|
|
||||||
)
|
|
||||||
output.write(chunk)
|
|
||||||
remaining -= len(chunk)
|
|
||||||
|
|
||||||
|
|
||||||
def write_output(entries: list[TensorEntry]) -> None:
|
|
||||||
if COPY_BUFFER_SIZE <= 0:
|
|
||||||
raise ValueError("COPY_BUFFER_SIZE must be positive")
|
|
||||||
|
|
||||||
output_path = OUTPUT_PATH.resolve()
|
|
||||||
source_paths = {entry.source_path.resolve() for entry in entries}
|
|
||||||
if output_path in source_paths:
|
|
||||||
raise ValueError("OUTPUT_PATH must not be one of the source files")
|
|
||||||
if output_path.exists() and not OVERWRITE_OUTPUT:
|
|
||||||
raise FileExistsError(
|
|
||||||
f"output already exists: {output_path}; set OVERWRITE_OUTPUT = True to replace it"
|
|
||||||
)
|
|
||||||
|
|
||||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
partial_path = output_path.with_name(output_path.name + ".partial")
|
|
||||||
if partial_path.exists():
|
|
||||||
raise FileExistsError(
|
|
||||||
f"partial output already exists: {partial_path}; remove it before retrying"
|
|
||||||
)
|
|
||||||
|
|
||||||
header_bytes, tensor_bytes = build_output_header(entries)
|
|
||||||
print(
|
|
||||||
f"Writing {len(entries)} tensors ({format_bytes(tensor_bytes)}) to {output_path}"
|
|
||||||
)
|
|
||||||
|
|
||||||
current_source_path: Path | None = None
|
|
||||||
current_source: BinaryIO | None = None
|
|
||||||
copied = 0
|
|
||||||
next_progress = PROGRESS_INTERVAL
|
|
||||||
try:
|
|
||||||
with partial_path.open("xb") as output:
|
|
||||||
output.write(struct.pack("<Q", len(header_bytes)))
|
|
||||||
output.write(header_bytes)
|
|
||||||
|
|
||||||
try:
|
|
||||||
for entry in entries:
|
|
||||||
if entry.source_path != current_source_path:
|
|
||||||
if current_source is not None:
|
|
||||||
current_source.close()
|
|
||||||
current_source = entry.source_path.open("rb")
|
|
||||||
current_source_path = entry.source_path
|
|
||||||
|
|
||||||
copy_tensor(current_source, output, entry)
|
|
||||||
copied += entry.size
|
|
||||||
if PROGRESS_INTERVAL > 0 and copied >= next_progress:
|
|
||||||
print(
|
|
||||||
f" copied {format_bytes(copied)} / "
|
|
||||||
f"{format_bytes(tensor_bytes)}"
|
|
||||||
)
|
|
||||||
while next_progress <= copied:
|
|
||||||
next_progress += PROGRESS_INTERVAL
|
|
||||||
finally:
|
|
||||||
if current_source is not None:
|
|
||||||
current_source.close()
|
|
||||||
|
|
||||||
if copied != tensor_bytes:
|
|
||||||
raise OSError(f"copied {copied} tensor bytes, expected {tensor_bytes}")
|
|
||||||
os.replace(partial_path, output_path)
|
|
||||||
except BaseException:
|
|
||||||
partial_path.unlink(missing_ok=True)
|
|
||||||
raise
|
|
||||||
|
|
||||||
print(f"Done: {output_path} ({format_bytes(output_path.stat().st_size)})")
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
entries = collect_entries()
|
|
||||||
write_output(entries)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@ -2,10 +2,8 @@
|
|||||||
#define __SD_CONDITIONING_CONDITIONER_HPP__
|
#define __SD_CONDITIONING_CONDITIONER_HPP__
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iomanip>
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "core/tensor_ggml.hpp"
|
#include "core/tensor_ggml.hpp"
|
||||||
#include "core/util.h"
|
#include "core/util.h"
|
||||||
@ -27,8 +25,6 @@ struct SDCondition {
|
|||||||
sd::Tensor<int32_t> c_vinput_mask;
|
sd::Tensor<int32_t> c_vinput_mask;
|
||||||
std::vector<std::pair<int, sd::Tensor<float>>> c_image_embeds;
|
std::vector<std::pair<int, sd::Tensor<float>>> c_image_embeds;
|
||||||
std::vector<sd::Tensor<float>> c_ref_images;
|
std::vector<sd::Tensor<float>> c_ref_images;
|
||||||
std::vector<sd::Tensor<float>> c_ref_audios;
|
|
||||||
std::vector<MiniMaxH3ReferenceBlock> c_reference_blocks;
|
|
||||||
|
|
||||||
std::vector<sd::Tensor<float>> extra_c_crossattns;
|
std::vector<sd::Tensor<float>> extra_c_crossattns;
|
||||||
|
|
||||||
@ -59,12 +55,6 @@ struct SDCondition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& tensor : c_ref_audios) {
|
|
||||||
if (!tensor.empty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& tensor : extra_c_crossattns) {
|
for (const auto& tensor : extra_c_crossattns) {
|
||||||
if (!tensor.empty()) {
|
if (!tensor.empty()) {
|
||||||
return false;
|
return false;
|
||||||
@ -75,18 +65,6 @@ struct SDCondition {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class MiniMaxH3PresentationKind {
|
|
||||||
IMAGE,
|
|
||||||
VIDEO,
|
|
||||||
AUDIO,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniMaxH3PresentationItem {
|
|
||||||
MiniMaxH3PresentationKind kind = MiniMaxH3PresentationKind::IMAGE;
|
|
||||||
std::vector<sd::Tensor<float>> frames;
|
|
||||||
std::vector<float> timestamps;
|
|
||||||
};
|
|
||||||
|
|
||||||
static inline sd::Tensor<float> apply_token_weights(sd::Tensor<float> hidden_states,
|
static inline sd::Tensor<float> apply_token_weights(sd::Tensor<float> hidden_states,
|
||||||
const std::vector<float>& weights) {
|
const std::vector<float>& weights) {
|
||||||
if (hidden_states.empty()) {
|
if (hidden_states.empty()) {
|
||||||
@ -129,7 +107,6 @@ struct ConditionerParams {
|
|||||||
int height = -1;
|
int height = -1;
|
||||||
bool zero_out_masked = false;
|
bool zero_out_masked = false;
|
||||||
const std::vector<sd::Tensor<float>>* ref_images = nullptr; // for qwen image edit
|
const std::vector<sd::Tensor<float>>* ref_images = nullptr; // for qwen image edit
|
||||||
const std::vector<MiniMaxH3PresentationItem>* minimax_h3_references = nullptr;
|
|
||||||
RefImageParams ref_image_params;
|
RefImageParams ref_image_params;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1833,7 +1810,6 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
sd_version_is_boogu_image(version) ||
|
sd_version_is_boogu_image(version) ||
|
||||||
sd_version_is_sefi_image(version) ||
|
sd_version_is_sefi_image(version) ||
|
||||||
sd_version_is_krea2(version) ||
|
sd_version_is_krea2(version) ||
|
||||||
sd_version_is_minimax_h3(version) ||
|
|
||||||
sd_version_is_mage_flow(version)) {
|
sd_version_is_mage_flow(version)) {
|
||||||
arch = LLM::LLMArch::QWEN3_VL;
|
arch = LLM::LLMArch::QWEN3_VL;
|
||||||
} else if (sd_version_is_z_image(version) || version == VERSION_OVIS_IMAGE || version == VERSION_FLUX2_KLEIN) {
|
} else if (sd_version_is_z_image(version) || version == VERSION_OVIS_IMAGE || version == VERSION_FLUX2_KLEIN) {
|
||||||
@ -2017,9 +1993,7 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
const std::set<int>& out_layers,
|
const std::set<int>& out_layers,
|
||||||
int prompt_template_encode_start_idx,
|
int prompt_template_encode_start_idx,
|
||||||
bool spell_quotes = false,
|
bool spell_quotes = false,
|
||||||
int max_length = 100000000,
|
int max_length = 100000000) {
|
||||||
const LLM::DeepStackImageEmbeds& deepstack_image_embeds = {},
|
|
||||||
const std::vector<LLM::ImageGrid>& image_grids = {}) {
|
|
||||||
auto tokens_weights_mask = tokenize(prompt, prompt_attn_range, min_length, max_length, spell_quotes);
|
auto tokens_weights_mask = tokenize(prompt, prompt_attn_range, min_length, max_length, spell_quotes);
|
||||||
auto& tokens = std::get<0>(tokens_weights_mask);
|
auto& tokens = std::get<0>(tokens_weights_mask);
|
||||||
auto& weights = std::get<1>(tokens_weights_mask);
|
auto& weights = std::get<1>(tokens_weights_mask);
|
||||||
@ -2052,9 +2026,7 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
true,
|
true);
|
||||||
deepstack_image_embeds,
|
|
||||||
image_grids);
|
|
||||||
GGML_ASSERT(!hidden_states.empty());
|
GGML_ASSERT(!hidden_states.empty());
|
||||||
hidden_states = apply_token_weights(std::move(hidden_states), weights);
|
hidden_states = apply_token_weights(std::move(hidden_states), weights);
|
||||||
GGML_ASSERT(hidden_states.shape()[1] > prompt_template_encode_start_idx);
|
GGML_ASSERT(hidden_states.shape()[1] > prompt_template_encode_start_idx);
|
||||||
@ -2136,8 +2108,6 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
std::vector<std::string> extra_prompts;
|
std::vector<std::string> extra_prompts;
|
||||||
std::vector<std::pair<int, int>> extra_prompts_attn_range;
|
std::vector<std::pair<int, int>> extra_prompts_attn_range;
|
||||||
std::vector<std::pair<int, sd::Tensor<float>>> image_embeds;
|
std::vector<std::pair<int, sd::Tensor<float>>> image_embeds;
|
||||||
LLM::DeepStackImageEmbeds deepstack_image_embeds;
|
|
||||||
std::vector<LLM::ImageGrid> image_grids;
|
|
||||||
int prompt_template_encode_start_idx = 34;
|
int prompt_template_encode_start_idx = 34;
|
||||||
int min_length = 0; // pad tokens
|
int min_length = 0; // pad tokens
|
||||||
int max_length = 100000000;
|
int max_length = 100000000;
|
||||||
@ -2148,131 +2118,7 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
int64_t t0 = ggml_time_ms();
|
int64_t t0 = ggml_time_ms();
|
||||||
RefImageResizeMode resize_mode = conditioner_params.ref_image_params.vlm_resize_mode;
|
RefImageResizeMode resize_mode = conditioner_params.ref_image_params.vlm_resize_mode;
|
||||||
|
|
||||||
if (sd_version_is_minimax_h3(version)) {
|
if (sd_version_is_hunyuan_video(version)) {
|
||||||
prompt_template_encode_start_idx = 0;
|
|
||||||
out_layers = {50};
|
|
||||||
prompt_attn_range = {0, 0};
|
|
||||||
|
|
||||||
if (llm->enable_vision) {
|
|
||||||
const std::string placeholder = "<|image_pad|>";
|
|
||||||
const int patch_size = llm->config.vision.patch_size;
|
|
||||||
const int factor = patch_size * llm->config.vision.spatial_merge_size;
|
|
||||||
|
|
||||||
auto resize_for_vision = [&](const sd::Tensor<float>& image) {
|
|
||||||
int height = static_cast<int>(image.shape()[1]);
|
|
||||||
int width = static_cast<int>(image.shape()[0]);
|
|
||||||
int h_bar = std::max(factor, static_cast<int>(std::round(static_cast<double>(height) / factor)) * factor);
|
|
||||||
int w_bar = std::max(factor, static_cast<int>(std::round(static_cast<double>(width) / factor)) * factor);
|
|
||||||
resize_image_dims(height,
|
|
||||||
width,
|
|
||||||
h_bar,
|
|
||||||
w_bar,
|
|
||||||
factor,
|
|
||||||
3136,
|
|
||||||
12845056,
|
|
||||||
RefImageResizeMode::AREA);
|
|
||||||
auto resized = sd::ops::interpolate(
|
|
||||||
image,
|
|
||||||
std::vector<int64_t>{w_bar, h_bar, image.shape()[2], image.shape()[3]});
|
|
||||||
for (int64_t i = 0; i < resized.numel(); ++i) {
|
|
||||||
resized[i] = std::clamp(resized[i], 0.f, 1.f) * 2.f - 1.f;
|
|
||||||
}
|
|
||||||
return resized;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto add_vision_outputs = [&](std::vector<sd::Tensor<float>> image_outputs,
|
|
||||||
int grid_h,
|
|
||||||
int grid_w) {
|
|
||||||
GGML_ASSERT(image_outputs.size() == 4);
|
|
||||||
auto image_embed = std::move(image_outputs[0]);
|
|
||||||
prompt += "<|vision_start|>";
|
|
||||||
int image_embed_idx = static_cast<int>(tokenizer->encode(prompt, nullptr).size());
|
|
||||||
image_embeds.emplace_back(image_embed_idx, image_embed);
|
|
||||||
if (deepstack_image_embeds.empty()) {
|
|
||||||
deepstack_image_embeds.resize(image_outputs.size() - 1);
|
|
||||||
}
|
|
||||||
for (size_t layer = 0; layer < deepstack_image_embeds.size(); ++layer) {
|
|
||||||
deepstack_image_embeds[layer].emplace_back(image_embed_idx, std::move(image_outputs[layer + 1]));
|
|
||||||
}
|
|
||||||
image_grids.push_back({image_embed_idx,
|
|
||||||
static_cast<int>(image_embed.shape()[1]),
|
|
||||||
grid_h,
|
|
||||||
grid_w});
|
|
||||||
for (int64_t i = 0; i < image_embed.shape()[1]; ++i) {
|
|
||||||
prompt += placeholder;
|
|
||||||
}
|
|
||||||
prompt += "<|vision_end|>";
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto* references = conditioner_params.minimax_h3_references;
|
|
||||||
if (references != nullptr && !references->empty()) {
|
|
||||||
int picture_index = 0;
|
|
||||||
int video_index = 0;
|
|
||||||
int audio_index = 0;
|
|
||||||
for (const auto& item : *references) {
|
|
||||||
if (item.kind == MiniMaxH3PresentationKind::AUDIO) {
|
|
||||||
prompt += "<Audio " + std::to_string(++audio_index) + ">: ";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (item.kind == MiniMaxH3PresentationKind::IMAGE) {
|
|
||||||
GGML_ASSERT(item.frames.size() == 1);
|
|
||||||
auto resized = resize_for_vision(item.frames[0]);
|
|
||||||
prompt += "<Picture " + std::to_string(++picture_index) + ">: ";
|
|
||||||
add_vision_outputs(llm->encode_image_outputs(n_threads,
|
|
||||||
resized,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
true),
|
|
||||||
static_cast<int>(resized.shape()[1]) / patch_size,
|
|
||||||
static_cast<int>(resized.shape()[0]) / patch_size);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
GGML_ASSERT(!item.frames.empty());
|
|
||||||
prompt += "<Video " + std::to_string(++video_index) + ">: ";
|
|
||||||
for (size_t frame = 0; frame < item.frames.size(); frame += 2) {
|
|
||||||
size_t next = std::min(frame + 1, item.frames.size() - 1);
|
|
||||||
float t0 = frame < item.timestamps.size() ? item.timestamps[frame] : frame / 2.f;
|
|
||||||
float t1 = next < item.timestamps.size() ? item.timestamps[next] : next / 2.f;
|
|
||||||
std::ostringstream timestamp;
|
|
||||||
timestamp << '<' << std::fixed << std::setprecision(1) << (t0 + t1) * 0.5f << " seconds>";
|
|
||||||
prompt += timestamp.str();
|
|
||||||
|
|
||||||
auto first = resize_for_vision(item.frames[frame]);
|
|
||||||
auto second = resize_for_vision(item.frames[next]);
|
|
||||||
if (first.shape()[0] != second.shape()[0] || first.shape()[1] != second.shape()[1]) {
|
|
||||||
second = sd::ops::interpolate(second,
|
|
||||||
std::vector<int64_t>{first.shape()[0],
|
|
||||||
first.shape()[1],
|
|
||||||
second.shape()[2],
|
|
||||||
second.shape()[3]});
|
|
||||||
}
|
|
||||||
auto pair = sd::ops::concat(first.unsqueeze(2), second.unsqueeze(2), 2);
|
|
||||||
add_vision_outputs(llm->encode_video_block_outputs(n_threads,
|
|
||||||
pair,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
true),
|
|
||||||
static_cast<int>(first.shape()[1]) / patch_size,
|
|
||||||
static_cast<int>(first.shape()[0]) / patch_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (conditioner_params.ref_images != nullptr) {
|
|
||||||
for (size_t i = 0; i < conditioner_params.ref_images->size(); ++i) {
|
|
||||||
auto resized = resize_for_vision((*conditioner_params.ref_images)[i]);
|
|
||||||
prompt += "<Picture " + std::to_string(i + 1) + ">: ";
|
|
||||||
add_vision_outputs(llm->encode_image_outputs(n_threads,
|
|
||||||
resized,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
true),
|
|
||||||
static_cast<int>(resized.shape()[1]) / patch_size,
|
|
||||||
static_cast<int>(resized.shape()[0]) / patch_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prompt += conditioner_params.text;
|
|
||||||
} else if (sd_version_is_hunyuan_video(version)) {
|
|
||||||
prompt_template_encode_start_idx = 98;
|
prompt_template_encode_start_idx = 98;
|
||||||
out_layers = {26};
|
out_layers = {26};
|
||||||
|
|
||||||
@ -2820,9 +2666,7 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
out_layers,
|
out_layers,
|
||||||
prompt_template_encode_start_idx,
|
prompt_template_encode_start_idx,
|
||||||
spell_quotes,
|
spell_quotes,
|
||||||
max_length,
|
max_length);
|
||||||
deepstack_image_embeds,
|
|
||||||
image_grids);
|
|
||||||
std::vector<sd::Tensor<float>> extra_hidden_states_vec;
|
std::vector<sd::Tensor<float>> extra_hidden_states_vec;
|
||||||
if (sd_version_is_hunyuan_video(version) && byt5) {
|
if (sd_version_is_hunyuan_video(version) && byt5) {
|
||||||
std::vector<std::string> quoted_texts;
|
std::vector<std::string> quoted_texts;
|
||||||
@ -2883,17 +2727,6 @@ struct LLMEmbedder : public Conditioner {
|
|||||||
SDCondition result;
|
SDCondition result;
|
||||||
result.c_crossattn = std::move(hidden_states);
|
result.c_crossattn = std::move(hidden_states);
|
||||||
result.extra_c_crossattns = std::move(extra_hidden_states_vec);
|
result.extra_c_crossattns = std::move(extra_hidden_states_vec);
|
||||||
if (sd_version_is_minimax_h3(version)) {
|
|
||||||
std::vector<int32_t> tags(static_cast<size_t>(result.c_crossattn.shape()[1]), 1);
|
|
||||||
for (const auto& [index, image_embed] : image_embeds) {
|
|
||||||
int64_t begin = std::max<int64_t>(0, index - 1);
|
|
||||||
int64_t end = std::min<int64_t>(static_cast<int64_t>(tags.size()),
|
|
||||||
index + image_embed.shape()[1] + 1);
|
|
||||||
std::fill(tags.begin() + begin, tags.begin() + end, 0);
|
|
||||||
}
|
|
||||||
int64_t tag_count = static_cast<int64_t>(tags.size());
|
|
||||||
result.c_token_types = sd::Tensor<int32_t>({tag_count}, std::move(tags));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -43,7 +43,6 @@ enum SDVersion {
|
|||||||
VERSION_FLUX2,
|
VERSION_FLUX2,
|
||||||
VERSION_FLUX2_KLEIN,
|
VERSION_FLUX2_KLEIN,
|
||||||
VERSION_LTXAV,
|
VERSION_LTXAV,
|
||||||
VERSION_MINIMAX_H3,
|
|
||||||
VERSION_HIDREAM_O1,
|
VERSION_HIDREAM_O1,
|
||||||
VERSION_Z_IMAGE,
|
VERSION_Z_IMAGE,
|
||||||
VERSION_BOOGU_IMAGE,
|
VERSION_BOOGU_IMAGE,
|
||||||
@ -124,10 +123,6 @@ static inline bool sd_version_is_ltxav(SDVersion version) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool sd_version_is_minimax_h3(SDVersion version) {
|
|
||||||
return version == VERSION_MINIMAX_H3;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool sd_version_is_wan(SDVersion version) {
|
static inline bool sd_version_is_wan(SDVersion version) {
|
||||||
if (version == VERSION_WAN2 || version == VERSION_WAN2_2_I2V || version == VERSION_WAN2_2_TI2V) {
|
if (version == VERSION_WAN2 || version == VERSION_WAN2_2_I2V || version == VERSION_WAN2_2_TI2V) {
|
||||||
return true;
|
return true;
|
||||||
@ -277,7 +272,6 @@ static inline bool sd_version_is_dit(SDVersion version) {
|
|||||||
if (sd_version_is_flux(version) ||
|
if (sd_version_is_flux(version) ||
|
||||||
sd_version_is_flux2(version) ||
|
sd_version_is_flux2(version) ||
|
||||||
sd_version_is_ltxav(version) ||
|
sd_version_is_ltxav(version) ||
|
||||||
sd_version_is_minimax_h3(version) ||
|
|
||||||
sd_version_is_sd3(version) ||
|
sd_version_is_sd3(version) ||
|
||||||
sd_version_is_wan(version) ||
|
sd_version_is_wan(version) ||
|
||||||
sd_version_is_lingbot_video(version) ||
|
sd_version_is_lingbot_video(version) ||
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -87,29 +87,6 @@ struct LTXAVDiffusionExtra {
|
|||||||
const sd::Tensor<float>* video_positions = nullptr;
|
const sd::Tensor<float>* video_positions = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class MiniMaxH3ReferenceKind : int32_t {
|
|
||||||
IMAGE,
|
|
||||||
VIDEO,
|
|
||||||
AUDIO,
|
|
||||||
VIDEO_AUDIO,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniMaxH3ReferenceBlock {
|
|
||||||
MiniMaxH3ReferenceKind kind = MiniMaxH3ReferenceKind::IMAGE;
|
|
||||||
int32_t video_index = -1;
|
|
||||||
int32_t audio_index = -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniMaxH3DiffusionExtra {
|
|
||||||
const sd::Tensor<int32_t>* text_token_tags = nullptr;
|
|
||||||
const sd::Tensor<int32_t>* keyframe_indices = nullptr;
|
|
||||||
const std::vector<sd::Tensor<float>>* reference_audio_latents = nullptr;
|
|
||||||
const std::vector<MiniMaxH3ReferenceBlock>* reference_blocks = nullptr;
|
|
||||||
int audio_length = 0;
|
|
||||||
float video_sigma_shift = 12.f;
|
|
||||||
float audio_sigma_shift = 3.f;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniT2IDiffusionExtra {
|
struct MiniT2IDiffusionExtra {
|
||||||
const sd::Tensor<float>* mask = nullptr;
|
const sd::Tensor<float>* mask = nullptr;
|
||||||
};
|
};
|
||||||
@ -129,7 +106,6 @@ using DiffusionExtraParams = std::variant<std::monostate,
|
|||||||
WanDiffusionExtra,
|
WanDiffusionExtra,
|
||||||
HiDreamO1DiffusionExtra,
|
HiDreamO1DiffusionExtra,
|
||||||
LTXAVDiffusionExtra,
|
LTXAVDiffusionExtra,
|
||||||
MiniMaxH3DiffusionExtra,
|
|
||||||
MiniT2IDiffusionExtra,
|
MiniT2IDiffusionExtra,
|
||||||
HunyuanVideoDiffusionExtra>;
|
HunyuanVideoDiffusionExtra>;
|
||||||
|
|
||||||
|
|||||||
@ -79,20 +79,9 @@ namespace LLM {
|
|||||||
int window_size = 112;
|
int window_size = 112;
|
||||||
int num_position_embeddings = 0;
|
int num_position_embeddings = 0;
|
||||||
std::set<int> fullatt_block_indexes = {7, 15, 23, 31};
|
std::set<int> fullatt_block_indexes = {7, 15, 23, 31};
|
||||||
std::vector<int> deepstack_visual_indexes;
|
|
||||||
bool split_patch_embed = false;
|
bool split_patch_embed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ImageGrid {
|
|
||||||
int index = 0;
|
|
||||||
int size = 0;
|
|
||||||
int grid_h = 0;
|
|
||||||
int grid_w = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
using ImageEmbeds = std::vector<std::pair<int, sd::Tensor<float>>>;
|
|
||||||
using DeepStackImageEmbeds = std::vector<ImageEmbeds>;
|
|
||||||
|
|
||||||
struct LLMConfig {
|
struct LLMConfig {
|
||||||
LLMArch arch = LLMArch::QWEN2_5_VL;
|
LLMArch arch = LLMArch::QWEN2_5_VL;
|
||||||
int64_t num_layers = 28;
|
int64_t num_layers = 28;
|
||||||
@ -104,7 +93,6 @@ namespace LLM {
|
|||||||
bool qkv_bias = true;
|
bool qkv_bias = true;
|
||||||
bool attention_out_bias = false;
|
bool attention_out_bias = false;
|
||||||
bool qk_norm = false;
|
bool qk_norm = false;
|
||||||
bool final_norm = true;
|
|
||||||
bool rms_norm_add = false;
|
bool rms_norm_add = false;
|
||||||
bool normalize_input = false;
|
bool normalize_input = false;
|
||||||
int64_t vocab_size = 152064;
|
int64_t vocab_size = 152064;
|
||||||
@ -269,20 +257,9 @@ namespace LLM {
|
|||||||
if ((arch == LLMArch::QWEN3 || arch == LLMArch::QWEN3_VL) && config.num_layers == 28) {
|
if ((arch == LLMArch::QWEN3 || arch == LLMArch::QWEN3_VL) && config.num_layers == 28) {
|
||||||
config.num_heads = 16;
|
config.num_heads = 16;
|
||||||
}
|
}
|
||||||
if (arch == LLMArch::QWEN3_VL && config.num_layers == 50 && config.hidden_size == 5120) {
|
|
||||||
config.num_heads = 64;
|
|
||||||
config.final_norm = false;
|
|
||||||
}
|
|
||||||
if (detected_vision_layers > 0) {
|
if (detected_vision_layers > 0) {
|
||||||
config.vision.num_layers = detected_vision_layers;
|
config.vision.num_layers = detected_vision_layers;
|
||||||
}
|
}
|
||||||
if (arch == LLMArch::QWEN3_VL) {
|
|
||||||
if (config.vision.num_layers == 24) {
|
|
||||||
config.vision.deepstack_visual_indexes = {5, 11, 17};
|
|
||||||
} else if (config.vision.num_layers == 27) {
|
|
||||||
config.vision.deepstack_visual_indexes = {8, 16, 24};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LOG_DEBUG("llm: num_layers = %" PRId64 ", vocab_size = %" PRId64 ", hidden_size = %" PRId64 ", intermediate_size = %" PRId64,
|
LOG_DEBUG("llm: num_layers = %" PRId64 ", vocab_size = %" PRId64 ", hidden_size = %" PRId64 ", intermediate_size = %" PRId64,
|
||||||
config.num_layers,
|
config.num_layers,
|
||||||
config.vocab_size,
|
config.vocab_size,
|
||||||
@ -564,37 +541,6 @@ namespace LLM {
|
|||||||
return input_embed;
|
return input_embed;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ggml_tensor* add_deepstack_image_embeds(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* x,
|
|
||||||
const std::vector<std::pair<int, ggml_tensor*>>& image_embeds) {
|
|
||||||
if (image_embeds.empty()) {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
GGML_ASSERT(x->ne[2] == 1);
|
|
||||||
auto raw_x = ggml_cast(ctx->ggml_ctx, x, image_embeds[0].second->type);
|
|
||||||
int64_t token_start = 0;
|
|
||||||
ggml_tensor* output = nullptr;
|
|
||||||
for (const auto& [index, image_embed] : image_embeds) {
|
|
||||||
GGML_ASSERT(index >= token_start);
|
|
||||||
GGML_ASSERT(index + image_embed->ne[1] <= raw_x->ne[1]);
|
|
||||||
if (index > token_start) {
|
|
||||||
auto text_embed = ggml_ext_slice(ctx->ggml_ctx, raw_x, 1, token_start, index);
|
|
||||||
output = output == nullptr ? text_embed : ggml_concat(ctx->ggml_ctx, output, text_embed, 1);
|
|
||||||
}
|
|
||||||
auto visual_embed = ggml_ext_slice(ctx->ggml_ctx, raw_x, 1, index, index + image_embed->ne[1]);
|
|
||||||
visual_embed = ggml_add(ctx->ggml_ctx, visual_embed, image_embed);
|
|
||||||
output = output == nullptr ? visual_embed : ggml_concat(ctx->ggml_ctx, output, visual_embed, 1);
|
|
||||||
token_start = index + image_embed->ne[1];
|
|
||||||
}
|
|
||||||
if (token_start < raw_x->ne[1]) {
|
|
||||||
auto text_embed = ggml_ext_slice(ctx->ggml_ctx, raw_x, 1, token_start, raw_x->ne[1]);
|
|
||||||
output = output == nullptr ? text_embed : ggml_concat(ctx->ggml_ctx, output, text_embed, 1);
|
|
||||||
}
|
|
||||||
GGML_ASSERT(output != nullptr && output->ne[1] == raw_x->ne[1]);
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VisionMLP : public GGMLBlock {
|
struct VisionMLP : public GGMLBlock {
|
||||||
protected:
|
protected:
|
||||||
LLMVisionArch arch_;
|
LLMVisionArch arch_;
|
||||||
@ -777,33 +723,6 @@ namespace LLM {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Qwen3VLDeepStackMerger : public GGMLBlock {
|
|
||||||
protected:
|
|
||||||
int64_t merge_dim;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Qwen3VLDeepStackMerger(int64_t dim,
|
|
||||||
int64_t context_dim,
|
|
||||||
int64_t spatial_merge_size)
|
|
||||||
: merge_dim(context_dim * spatial_merge_size * spatial_merge_size) {
|
|
||||||
blocks["norm"] = std::make_shared<LayerNorm>(merge_dim, 1e-6f);
|
|
||||||
blocks["linear_fc1"] = std::make_shared<Linear>(merge_dim, merge_dim, true);
|
|
||||||
blocks["linear_fc2"] = std::make_shared<Linear>(merge_dim, dim, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto norm = std::dynamic_pointer_cast<LayerNorm>(blocks["norm"]);
|
|
||||||
auto linear_fc1 = std::dynamic_pointer_cast<Linear>(blocks["linear_fc1"]);
|
|
||||||
auto linear_fc2 = std::dynamic_pointer_cast<Linear>(blocks["linear_fc2"]);
|
|
||||||
|
|
||||||
x = ggml_reshape_2d(ctx->ggml_ctx, x, merge_dim, ggml_nelements(x) / merge_dim);
|
|
||||||
x = norm->forward(ctx, x);
|
|
||||||
x = linear_fc1->forward(ctx, x);
|
|
||||||
x = ggml_gelu_erf(ctx->ggml_ctx, x);
|
|
||||||
return linear_fc2->forward(ctx, x);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VisionAttention : public GGMLBlock {
|
struct VisionAttention : public GGMLBlock {
|
||||||
protected:
|
protected:
|
||||||
bool llama_cpp_style;
|
bool llama_cpp_style;
|
||||||
@ -925,7 +844,6 @@ namespace LLM {
|
|||||||
int spatial_merge_size;
|
int spatial_merge_size;
|
||||||
int num_grid_per_side;
|
int num_grid_per_side;
|
||||||
std::set<int> fullatt_block_indexes;
|
std::set<int> fullatt_block_indexes;
|
||||||
std::vector<int> deepstack_visual_indexes;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VisionModel(bool llama_cpp_style,
|
VisionModel(bool llama_cpp_style,
|
||||||
@ -935,8 +853,7 @@ namespace LLM {
|
|||||||
num_layers(vision_params.num_layers),
|
num_layers(vision_params.num_layers),
|
||||||
spatial_merge_size(vision_params.spatial_merge_size),
|
spatial_merge_size(vision_params.spatial_merge_size),
|
||||||
num_grid_per_side(vision_params.num_position_embeddings > 0 ? static_cast<int>(std::sqrt(vision_params.num_position_embeddings)) : 0),
|
num_grid_per_side(vision_params.num_position_embeddings > 0 ? static_cast<int>(std::sqrt(vision_params.num_position_embeddings)) : 0),
|
||||||
fullatt_block_indexes(vision_params.fullatt_block_indexes),
|
fullatt_block_indexes(vision_params.fullatt_block_indexes) {
|
||||||
deepstack_visual_indexes(vision_params.deepstack_visual_indexes) {
|
|
||||||
blocks["patch_embed"] = std::shared_ptr<GGMLBlock>(new VisionPatchEmbed(vision_params.split_patch_embed,
|
blocks["patch_embed"] = std::shared_ptr<GGMLBlock>(new VisionPatchEmbed(vision_params.split_patch_embed,
|
||||||
arch_,
|
arch_,
|
||||||
vision_params.patch_size,
|
vision_params.patch_size,
|
||||||
@ -958,11 +875,6 @@ namespace LLM {
|
|||||||
vision_params.out_hidden_size,
|
vision_params.out_hidden_size,
|
||||||
vision_params.hidden_size,
|
vision_params.hidden_size,
|
||||||
spatial_merge_size));
|
spatial_merge_size));
|
||||||
for (size_t i = 0; i < deepstack_visual_indexes.size(); ++i) {
|
|
||||||
blocks["deepstack_merger_list." + std::to_string(i)] = std::make_shared<Qwen3VLDeepStackMerger>(vision_params.out_hidden_size,
|
|
||||||
vision_params.hidden_size,
|
|
||||||
spatial_merge_size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Embedding> pos_embedder() {
|
std::shared_ptr<Embedding> pos_embedder() {
|
||||||
@ -981,7 +893,7 @@ namespace LLM {
|
|||||||
return spatial_merge_size;
|
return spatial_merge_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ggml_tensor*> forward_outputs(GGMLRunnerContext* ctx,
|
ggml_tensor* forward(GGMLRunnerContext* ctx,
|
||||||
ggml_tensor* pixel_values,
|
ggml_tensor* pixel_values,
|
||||||
ggml_tensor* pe,
|
ggml_tensor* pe,
|
||||||
ggml_tensor* window_index,
|
ggml_tensor* window_index,
|
||||||
@ -1007,7 +919,6 @@ namespace LLM {
|
|||||||
x = ggml_reshape_4d(ctx->ggml_ctx, x, x->ne[0] / spatial_merge_size / spatial_merge_size, x->ne[1] * spatial_merge_size * spatial_merge_size, x->ne[2], x->ne[3]);
|
x = ggml_reshape_4d(ctx->ggml_ctx, x, x->ne[0] / spatial_merge_size / spatial_merge_size, x->ne[1] * spatial_merge_size * spatial_merge_size, x->ne[2], x->ne[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ggml_tensor*> deepstack_outputs;
|
|
||||||
for (int i = 0; i < num_layers; i++) {
|
for (int i = 0; i < num_layers; i++) {
|
||||||
auto block = std::dynamic_pointer_cast<VisionBlock>(blocks["blocks." + std::to_string(i)]);
|
auto block = std::dynamic_pointer_cast<VisionBlock>(blocks["blocks." + std::to_string(i)]);
|
||||||
|
|
||||||
@ -1016,11 +927,7 @@ namespace LLM {
|
|||||||
mask = nullptr;
|
mask = nullptr;
|
||||||
}
|
}
|
||||||
x = block->forward(ctx, x, pe, mask);
|
x = block->forward(ctx, x, pe, mask);
|
||||||
auto deepstack_it = std::find(deepstack_visual_indexes.begin(), deepstack_visual_indexes.end(), i);
|
if (i == 0) {
|
||||||
if (deepstack_it != deepstack_visual_indexes.end()) {
|
|
||||||
size_t deepstack_index = static_cast<size_t>(std::distance(deepstack_visual_indexes.begin(), deepstack_it));
|
|
||||||
auto deepstack_merger = std::dynamic_pointer_cast<Qwen3VLDeepStackMerger>(blocks["deepstack_merger_list." + std::to_string(deepstack_index)]);
|
|
||||||
deepstack_outputs.push_back(deepstack_merger->forward(ctx, x));
|
|
||||||
}
|
}
|
||||||
sd::ggml_graph_cut::mark_graph_cut(x, "llm.vision.blocks." + std::to_string(i), "x");
|
sd::ggml_graph_cut::mark_graph_cut(x, "llm.vision.blocks." + std::to_string(i), "x");
|
||||||
}
|
}
|
||||||
@ -1032,19 +939,7 @@ namespace LLM {
|
|||||||
x = ggml_get_rows(ctx->ggml_ctx, x, window_inverse_index);
|
x = ggml_get_rows(ctx->ggml_ctx, x, window_inverse_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ggml_tensor*> outputs = {x};
|
return x;
|
||||||
outputs.insert(outputs.end(), deepstack_outputs.begin(), deepstack_outputs.end());
|
|
||||||
return outputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* pixel_values,
|
|
||||||
ggml_tensor* pe,
|
|
||||||
ggml_tensor* window_index,
|
|
||||||
ggml_tensor* window_inverse_index,
|
|
||||||
ggml_tensor* window_mask,
|
|
||||||
ggml_tensor* pos_embeds = nullptr) {
|
|
||||||
return forward_outputs(ctx, pixel_values, pe, window_index, window_inverse_index, window_mask, pos_embeds)[0];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1368,10 +1263,8 @@ namespace LLM {
|
|||||||
for (int i = 0; i < num_layers; i++) {
|
for (int i = 0; i < num_layers; i++) {
|
||||||
blocks["layers." + std::to_string(i)] = std::shared_ptr<GGMLBlock>(new TransformerBlock(config, i));
|
blocks["layers." + std::to_string(i)] = std::shared_ptr<GGMLBlock>(new TransformerBlock(config, i));
|
||||||
}
|
}
|
||||||
if (config.final_norm) {
|
|
||||||
blocks["norm"] = std::shared_ptr<GGMLBlock>(new LLMRMSNorm(config.hidden_size, config.rms_norm_eps, config.rms_norm_add));
|
blocks["norm"] = std::shared_ptr<GGMLBlock>(new LLMRMSNorm(config.hidden_size, config.rms_norm_eps, config.rms_norm_add));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* embed(GGMLRunnerContext* ctx,
|
ggml_tensor* embed(GGMLRunnerContext* ctx,
|
||||||
ggml_tensor* input_ids) {
|
ggml_tensor* input_ids) {
|
||||||
@ -1385,11 +1278,9 @@ namespace LLM {
|
|||||||
ggml_tensor* input_pos,
|
ggml_tensor* input_pos,
|
||||||
ggml_tensor* attention_mask,
|
ggml_tensor* attention_mask,
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
const std::vector<std::vector<std::pair<int, ggml_tensor*>>>& deepstack_image_embeds = {},
|
|
||||||
ggml_tensor* sliding_attention_mask = nullptr,
|
ggml_tensor* sliding_attention_mask = nullptr,
|
||||||
bool return_all_hidden_states = false) {
|
bool return_all_hidden_states = false) {
|
||||||
auto norm = config.final_norm ? std::dynamic_pointer_cast<LLMRMSNorm>(blocks["norm"])
|
auto norm = std::dynamic_pointer_cast<LLMRMSNorm>(blocks["norm"]);
|
||||||
: nullptr;
|
|
||||||
std::vector<ggml_tensor*> intermediate_outputs;
|
std::vector<ggml_tensor*> intermediate_outputs;
|
||||||
|
|
||||||
if (config.normalize_input) {
|
if (config.normalize_input) {
|
||||||
@ -1404,9 +1295,6 @@ namespace LLM {
|
|||||||
auto block = std::dynamic_pointer_cast<TransformerBlock>(blocks["layers." + std::to_string(i)]);
|
auto block = std::dynamic_pointer_cast<TransformerBlock>(blocks["layers." + std::to_string(i)]);
|
||||||
|
|
||||||
x = block->forward(ctx, x, input_pos, attention_mask, sliding_attention_mask);
|
x = block->forward(ctx, x, input_pos, attention_mask, sliding_attention_mask);
|
||||||
if (i < static_cast<int>(deepstack_image_embeds.size())) {
|
|
||||||
x = add_deepstack_image_embeds(ctx, x, deepstack_image_embeds[static_cast<size_t>(i)]);
|
|
||||||
}
|
|
||||||
if (return_all_hidden_states || out_layers.size() > 1) {
|
if (return_all_hidden_states || out_layers.size() > 1) {
|
||||||
x = ggml_cont(ctx->ggml_ctx, x);
|
x = ggml_cont(ctx->ggml_ctx, x);
|
||||||
}
|
}
|
||||||
@ -1420,7 +1308,7 @@ namespace LLM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto normed_x = norm == nullptr ? x : norm->forward(ctx, x);
|
auto normed_x = norm->forward(ctx, x);
|
||||||
if (return_all_hidden_states) {
|
if (return_all_hidden_states) {
|
||||||
intermediate_outputs.push_back(normed_x);
|
intermediate_outputs.push_back(normed_x);
|
||||||
x = intermediate_outputs[0];
|
x = intermediate_outputs[0];
|
||||||
@ -1448,7 +1336,6 @@ namespace LLM {
|
|||||||
ggml_tensor* attention_mask,
|
ggml_tensor* attention_mask,
|
||||||
ggml_tensor* sliding_attention_mask,
|
ggml_tensor* sliding_attention_mask,
|
||||||
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
||||||
const std::vector<std::vector<std::pair<int, ggml_tensor*>>>& deepstack_image_embeds,
|
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
bool return_all_hidden_states = false) {
|
bool return_all_hidden_states = false) {
|
||||||
// input_ids: [N, n_token]
|
// input_ids: [N, n_token]
|
||||||
@ -1460,7 +1347,6 @@ namespace LLM {
|
|||||||
input_pos,
|
input_pos,
|
||||||
attention_mask,
|
attention_mask,
|
||||||
std::move(out_layers),
|
std::move(out_layers),
|
||||||
deepstack_image_embeds,
|
|
||||||
sliding_attention_mask,
|
sliding_attention_mask,
|
||||||
return_all_hidden_states);
|
return_all_hidden_states);
|
||||||
}
|
}
|
||||||
@ -1486,7 +1372,6 @@ namespace LLM {
|
|||||||
ggml_tensor* attention_mask,
|
ggml_tensor* attention_mask,
|
||||||
ggml_tensor* sliding_attention_mask,
|
ggml_tensor* sliding_attention_mask,
|
||||||
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
||||||
const std::vector<std::vector<std::pair<int, ggml_tensor*>>>& deepstack_image_embeds,
|
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
bool return_all_hidden_states = false) {
|
bool return_all_hidden_states = false) {
|
||||||
// input_ids: [N, n_token]
|
// input_ids: [N, n_token]
|
||||||
@ -1498,7 +1383,6 @@ namespace LLM {
|
|||||||
attention_mask,
|
attention_mask,
|
||||||
sliding_attention_mask,
|
sliding_attention_mask,
|
||||||
image_embeds,
|
image_embeds,
|
||||||
deepstack_image_embeds,
|
|
||||||
out_layers,
|
out_layers,
|
||||||
return_all_hidden_states);
|
return_all_hidden_states);
|
||||||
return x;
|
return x;
|
||||||
@ -1640,8 +1524,7 @@ namespace LLM {
|
|||||||
std::vector<float>& window_mask_vec,
|
std::vector<float>& window_mask_vec,
|
||||||
std::vector<float>& pe_vec,
|
std::vector<float>& pe_vec,
|
||||||
std::array<std::vector<int32_t>, 4>& pos_embed_idx_data,
|
std::array<std::vector<int32_t>, 4>& pos_embed_idx_data,
|
||||||
std::array<std::vector<float>, 4>& pos_embed_weight_data,
|
std::array<std::vector<float>, 4>& pos_embed_weight_data) {
|
||||||
std::vector<ggml_tensor*>* output_tensors = nullptr) {
|
|
||||||
GGML_ASSERT(image->ne[1] % (vision_params.patch_size * vision_params.spatial_merge_size) == 0);
|
GGML_ASSERT(image->ne[1] % (vision_params.patch_size * vision_params.spatial_merge_size) == 0);
|
||||||
GGML_ASSERT(image->ne[0] % (vision_params.patch_size * vision_params.spatial_merge_size) == 0);
|
GGML_ASSERT(image->ne[0] % (vision_params.patch_size * vision_params.spatial_merge_size) == 0);
|
||||||
|
|
||||||
@ -1673,11 +1556,7 @@ namespace LLM {
|
|||||||
int pos_len = static_cast<int>(pe_vec.size() / head_dim / 2);
|
int pos_len = static_cast<int>(pe_vec.size() / head_dim / 2);
|
||||||
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
|
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
|
||||||
runner->set_backend_tensor_data(pe, pe_vec.data());
|
runner->set_backend_tensor_data(pe, pe_vec.data());
|
||||||
auto outputs = vision_model->forward_outputs(runner_ctx, pixel_values, pe, nullptr, nullptr, nullptr, pos_embeds);
|
return vision_model->forward(runner_ctx, pixel_values, pe, nullptr, nullptr, nullptr, pos_embeds);
|
||||||
if (output_tensors != nullptr) {
|
|
||||||
*output_tensors = outputs;
|
|
||||||
}
|
|
||||||
return outputs[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int llm_grid_h = grid_h / vision_params.spatial_merge_size;
|
int llm_grid_h = grid_h / vision_params.spatial_merge_size;
|
||||||
@ -1743,11 +1622,7 @@ namespace LLM {
|
|||||||
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
|
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
|
||||||
runner->set_backend_tensor_data(pe, pe_vec.data());
|
runner->set_backend_tensor_data(pe, pe_vec.data());
|
||||||
|
|
||||||
auto output = vision_model->forward(runner_ctx, pixel_values, pe, window_index, window_inverse_index, window_mask);
|
return vision_model->forward(runner_ctx, pixel_values, pe, window_index, window_inverse_index, window_mask);
|
||||||
if (output_tensors != nullptr) {
|
|
||||||
*output_tensors = {output};
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -1792,7 +1667,6 @@ namespace LLM {
|
|||||||
ggml_tensor* attention_mask,
|
ggml_tensor* attention_mask,
|
||||||
ggml_tensor* sliding_attention_mask,
|
ggml_tensor* sliding_attention_mask,
|
||||||
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
std::vector<std::pair<int, ggml_tensor*>> image_embeds,
|
||||||
const std::vector<std::vector<std::pair<int, ggml_tensor*>>>& deepstack_image_embeds,
|
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
bool return_all_hidden_states = false) {
|
bool return_all_hidden_states = false) {
|
||||||
auto hidden_states = model.forward(ctx,
|
auto hidden_states = model.forward(ctx,
|
||||||
@ -1801,7 +1675,6 @@ namespace LLM {
|
|||||||
attention_mask,
|
attention_mask,
|
||||||
sliding_attention_mask,
|
sliding_attention_mask,
|
||||||
image_embeds,
|
image_embeds,
|
||||||
deepstack_image_embeds,
|
|
||||||
out_layers,
|
out_layers,
|
||||||
return_all_hidden_states); // [N, n_token, hidden_size]
|
return_all_hidden_states); // [N, n_token, hidden_size]
|
||||||
return hidden_states;
|
return hidden_states;
|
||||||
@ -1820,9 +1693,7 @@ namespace LLM {
|
|||||||
|
|
||||||
ggml_cgraph* build_graph(const sd::Tensor<int32_t>& input_ids_tensor,
|
ggml_cgraph* build_graph(const sd::Tensor<int32_t>& input_ids_tensor,
|
||||||
const sd::Tensor<float>& attention_mask_tensor,
|
const sd::Tensor<float>& attention_mask_tensor,
|
||||||
const ImageEmbeds& image_embeds_tensor,
|
const std::vector<std::pair<int, sd::Tensor<float>>>& image_embeds_tensor,
|
||||||
const DeepStackImageEmbeds& deepstack_image_embeds_tensor,
|
|
||||||
const std::vector<ImageGrid>& image_grids,
|
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
bool return_all_hidden_states = false) {
|
bool return_all_hidden_states = false) {
|
||||||
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
||||||
@ -1833,13 +1704,6 @@ namespace LLM {
|
|||||||
ggml_tensor* embed = make_input(embed_tensor);
|
ggml_tensor* embed = make_input(embed_tensor);
|
||||||
image_embeds.emplace_back(idx, embed);
|
image_embeds.emplace_back(idx, embed);
|
||||||
}
|
}
|
||||||
std::vector<std::vector<std::pair<int, ggml_tensor*>>> deepstack_image_embeds(deepstack_image_embeds_tensor.size());
|
|
||||||
for (size_t layer = 0; layer < deepstack_image_embeds_tensor.size(); ++layer) {
|
|
||||||
deepstack_image_embeds[layer].reserve(deepstack_image_embeds_tensor[layer].size());
|
|
||||||
for (const auto& [idx, embed_tensor] : deepstack_image_embeds_tensor[layer]) {
|
|
||||||
deepstack_image_embeds[layer].emplace_back(idx, make_input(embed_tensor));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t n_tokens = input_ids->ne[0];
|
int64_t n_tokens = input_ids->ne[0];
|
||||||
if (config.arch == LLMArch::MISTRAL_SMALL_3_2 ||
|
if (config.arch == LLMArch::MISTRAL_SMALL_3_2 ||
|
||||||
@ -1860,30 +1724,6 @@ namespace LLM {
|
|||||||
input_pos_vec[2 * n_tokens + i] = i;
|
input_pos_vec[2 * n_tokens + i] = i;
|
||||||
input_pos_vec[3 * n_tokens + i] = 0;
|
input_pos_vec[3 * n_tokens + i] = 0;
|
||||||
}
|
}
|
||||||
if (config.arch == LLMArch::QWEN3_VL && !image_grids.empty()) {
|
|
||||||
int offset = 0;
|
|
||||||
for (const auto& grid : image_grids) {
|
|
||||||
int end = grid.index + grid.size;
|
|
||||||
int grid_h = grid.grid_h / config.vision.spatial_merge_size;
|
|
||||||
int grid_w = grid.grid_w / config.vision.spatial_merge_size;
|
|
||||||
int len_max = std::max(grid_h, grid_w);
|
|
||||||
int next_pos = grid.index + len_max + offset;
|
|
||||||
GGML_ASSERT(grid.index >= 0 && end <= n_tokens);
|
|
||||||
GGML_ASSERT(grid_h > 0 && grid_w > 0 && grid.size == grid_h * grid_w);
|
|
||||||
for (int token = end; token < n_tokens; ++token) {
|
|
||||||
int pos = next_pos + token - end;
|
|
||||||
input_pos_vec[token] = pos;
|
|
||||||
input_pos_vec[n_tokens + token] = pos;
|
|
||||||
input_pos_vec[2 * n_tokens + token] = pos;
|
|
||||||
}
|
|
||||||
for (int token = 0; token < grid.size; ++token) {
|
|
||||||
input_pos_vec[grid.index + token] = grid.index + offset;
|
|
||||||
input_pos_vec[n_tokens + grid.index + token] = grid.index + offset + token / grid_w;
|
|
||||||
input_pos_vec[2 * n_tokens + grid.index + token] = grid.index + offset + token % grid_w;
|
|
||||||
}
|
|
||||||
offset += len_max - grid.size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto input_pos = ggml_new_tensor_1d(compute_ctx,
|
auto input_pos = ggml_new_tensor_1d(compute_ctx,
|
||||||
@ -1941,7 +1781,6 @@ namespace LLM {
|
|||||||
attention_mask,
|
attention_mask,
|
||||||
sliding_attention_mask,
|
sliding_attention_mask,
|
||||||
image_embeds,
|
image_embeds,
|
||||||
deepstack_image_embeds,
|
|
||||||
out_layers,
|
out_layers,
|
||||||
return_all_hidden_states);
|
return_all_hidden_states);
|
||||||
|
|
||||||
@ -1953,20 +1792,16 @@ namespace LLM {
|
|||||||
sd::Tensor<float> compute(const int n_threads,
|
sd::Tensor<float> compute(const int n_threads,
|
||||||
const sd::Tensor<int32_t>& input_ids,
|
const sd::Tensor<int32_t>& input_ids,
|
||||||
const sd::Tensor<float>& attention_mask,
|
const sd::Tensor<float>& attention_mask,
|
||||||
const ImageEmbeds& image_embeds,
|
const std::vector<std::pair<int, sd::Tensor<float>>>& image_embeds,
|
||||||
std::set<int> out_layers,
|
std::set<int> out_layers,
|
||||||
bool return_all_hidden_states = false,
|
bool return_all_hidden_states = false,
|
||||||
bool auto_free = true,
|
bool auto_free = true,
|
||||||
bool free_compute_buffer = true,
|
bool free_compute_buffer = true,
|
||||||
bool free_compute_params = true,
|
bool free_compute_params = true) {
|
||||||
const DeepStackImageEmbeds& deepstack_image_embeds = {},
|
|
||||||
const std::vector<ImageGrid>& image_grids = {}) {
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
auto get_graph = [&]() -> ggml_cgraph* {
|
||||||
return build_graph(input_ids,
|
return build_graph(input_ids,
|
||||||
attention_mask,
|
attention_mask,
|
||||||
image_embeds,
|
image_embeds,
|
||||||
deepstack_image_embeds,
|
|
||||||
image_grids,
|
|
||||||
out_layers,
|
out_layers,
|
||||||
return_all_hidden_states);
|
return_all_hidden_states);
|
||||||
};
|
};
|
||||||
@ -2016,24 +1851,6 @@ namespace LLM {
|
|||||||
pos_embed_weight_data_);
|
pos_embed_weight_data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ggml_tensor*> encode_image_outputs(GGMLRunnerContext* runner_ctx, ggml_tensor* image) {
|
|
||||||
std::vector<ggml_tensor*> outputs;
|
|
||||||
encode_image_common(this,
|
|
||||||
compute_ctx,
|
|
||||||
runner_ctx,
|
|
||||||
image,
|
|
||||||
config.vision,
|
|
||||||
model.vision_model(),
|
|
||||||
window_index_vec,
|
|
||||||
window_inverse_index_vec,
|
|
||||||
window_mask_vec,
|
|
||||||
pe_vec,
|
|
||||||
pos_embed_idx_data_,
|
|
||||||
pos_embed_weight_data_,
|
|
||||||
&outputs);
|
|
||||||
return outputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_cgraph* build_encode_image_graph(const sd::Tensor<float>& image_tensor) {
|
ggml_cgraph* build_encode_image_graph(const sd::Tensor<float>& image_tensor) {
|
||||||
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
||||||
ggml_tensor* image = make_input(image_tensor);
|
ggml_tensor* image = make_input(image_tensor);
|
||||||
@ -2058,166 +1875,6 @@ namespace LLM {
|
|||||||
};
|
};
|
||||||
return take_or_empty(GGMLRunner::compute<float>(get_graph, n_threads, auto_free, free_compute_buffer, free_compute_params));
|
return take_or_empty(GGMLRunner::compute<float>(get_graph, n_threads, auto_free, free_compute_buffer, free_compute_params));
|
||||||
}
|
}
|
||||||
|
|
||||||
ggml_cgraph* build_encode_image_outputs_graph(const sd::Tensor<float>& image_tensor) {
|
|
||||||
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
|
||||||
ggml_tensor* image = make_input(image_tensor);
|
|
||||||
|
|
||||||
auto runner_ctx = get_context();
|
|
||||||
auto outputs = encode_image_outputs(&runner_ctx, image);
|
|
||||||
GGML_ASSERT(!outputs.empty());
|
|
||||||
auto combined = outputs[0];
|
|
||||||
for (size_t i = 1; i < outputs.size(); ++i) {
|
|
||||||
combined = ggml_concat(compute_ctx, combined, outputs[i], 0);
|
|
||||||
}
|
|
||||||
ggml_build_forward_expand(gf, combined);
|
|
||||||
return gf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> process_video_block_tensor(const sd::Tensor<float>& frames,
|
|
||||||
const LLMVisionConfig& vision_params) {
|
|
||||||
GGML_ASSERT(frames.dim() == 5);
|
|
||||||
GGML_ASSERT(frames.shape()[2] == vision_params.temporal_patch_size);
|
|
||||||
GGML_ASSERT(frames.shape()[3] == vision_params.in_channels);
|
|
||||||
GGML_ASSERT(frames.shape()[4] == 1);
|
|
||||||
|
|
||||||
int64_t width = frames.shape()[0];
|
|
||||||
int64_t height = frames.shape()[1];
|
|
||||||
int64_t temporal = frames.shape()[2];
|
|
||||||
int64_t channels = frames.shape()[3];
|
|
||||||
int64_t patch = vision_params.patch_size;
|
|
||||||
int64_t merge = vision_params.spatial_merge_size;
|
|
||||||
int64_t grid_w = width / patch;
|
|
||||||
int64_t grid_h = height / patch;
|
|
||||||
int64_t feature = channels * temporal * patch * patch;
|
|
||||||
int64_t token_count = grid_h * grid_w;
|
|
||||||
sd::Tensor<float> output({feature, token_count});
|
|
||||||
|
|
||||||
int64_t token = 0;
|
|
||||||
for (int64_t block_h = 0; block_h < grid_h / merge; ++block_h) {
|
|
||||||
for (int64_t block_w = 0; block_w < grid_w / merge; ++block_w) {
|
|
||||||
for (int64_t inner_h = 0; inner_h < merge; ++inner_h) {
|
|
||||||
for (int64_t inner_w = 0; inner_w < merge; ++inner_w) {
|
|
||||||
int64_t patch_h = block_h * merge + inner_h;
|
|
||||||
int64_t patch_w = block_w * merge + inner_w;
|
|
||||||
int64_t offset = 0;
|
|
||||||
for (int64_t c = 0; c < channels; ++c) {
|
|
||||||
for (int64_t t = 0; t < temporal; ++t) {
|
|
||||||
for (int64_t y = 0; y < patch; ++y) {
|
|
||||||
for (int64_t x = 0; x < patch; ++x) {
|
|
||||||
output.index(offset++, token) =
|
|
||||||
frames.index(patch_w * patch + x,
|
|
||||||
patch_h * patch + y,
|
|
||||||
t,
|
|
||||||
c,
|
|
||||||
0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
++token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_cgraph* build_encode_video_block_outputs_graph(const sd::Tensor<float>& pixel_values_tensor,
|
|
||||||
int grid_h,
|
|
||||||
int grid_w) {
|
|
||||||
ggml_cgraph* gf = new_graph_custom(LLM_GRAPH_SIZE);
|
|
||||||
auto pixel_values = make_input(pixel_values_tensor);
|
|
||||||
auto runner_ctx = get_context();
|
|
||||||
auto vision = model.vision_model();
|
|
||||||
int head_dim = static_cast<int>(config.vision.hidden_size / config.vision.num_heads);
|
|
||||||
auto pos_embeds = build_patch_pos_embeds(&runner_ctx, vision, grid_h, grid_w);
|
|
||||||
window_index_vec.resize(static_cast<size_t>((grid_h / config.vision.spatial_merge_size) *
|
|
||||||
(grid_w / config.vision.spatial_merge_size)));
|
|
||||||
for (int i = 0; i < static_cast<int>(window_index_vec.size()); ++i) {
|
|
||||||
window_index_vec[static_cast<size_t>(i)] = i;
|
|
||||||
}
|
|
||||||
pe_vec = Rope::gen_qwen2vl_pe(grid_h,
|
|
||||||
grid_w,
|
|
||||||
config.vision.spatial_merge_size,
|
|
||||||
window_index_vec,
|
|
||||||
10000,
|
|
||||||
{head_dim / 2, head_dim / 2});
|
|
||||||
int pos_len = static_cast<int>(pe_vec.size() / head_dim / 2);
|
|
||||||
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
|
|
||||||
set_backend_tensor_data(pe, pe_vec.data());
|
|
||||||
auto outputs = vision->forward_outputs(&runner_ctx,
|
|
||||||
pixel_values,
|
|
||||||
pe,
|
|
||||||
nullptr,
|
|
||||||
nullptr,
|
|
||||||
nullptr,
|
|
||||||
pos_embeds);
|
|
||||||
GGML_ASSERT(!outputs.empty());
|
|
||||||
auto combined = outputs[0];
|
|
||||||
for (size_t i = 1; i < outputs.size(); ++i) {
|
|
||||||
combined = ggml_concat(compute_ctx, combined, outputs[i], 0);
|
|
||||||
}
|
|
||||||
ggml_build_forward_expand(gf, combined);
|
|
||||||
return gf;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<sd::Tensor<float>> encode_image_outputs(const int n_threads,
|
|
||||||
const sd::Tensor<float>& image,
|
|
||||||
bool auto_free = false,
|
|
||||||
bool free_compute_buffer = false,
|
|
||||||
bool free_compute_params = false) {
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
|
||||||
return build_encode_image_outputs_graph(image);
|
|
||||||
};
|
|
||||||
auto combined = take_or_empty(GGMLRunner::compute<float>(get_graph, n_threads, auto_free, free_compute_buffer, free_compute_params));
|
|
||||||
if (combined.empty()) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
size_t output_count = config.vision.deepstack_visual_indexes.size() + 1;
|
|
||||||
GGML_ASSERT(combined.shape()[0] == config.hidden_size * static_cast<int64_t>(output_count));
|
|
||||||
std::vector<sd::Tensor<float>> outputs;
|
|
||||||
outputs.reserve(output_count);
|
|
||||||
for (size_t i = 0; i < output_count; ++i) {
|
|
||||||
outputs.push_back(sd::ops::slice(combined,
|
|
||||||
0,
|
|
||||||
static_cast<int64_t>(i) * config.hidden_size,
|
|
||||||
static_cast<int64_t>(i + 1) * config.hidden_size));
|
|
||||||
}
|
|
||||||
return outputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<sd::Tensor<float>> encode_video_block_outputs(const int n_threads,
|
|
||||||
const sd::Tensor<float>& frames,
|
|
||||||
bool auto_free = false,
|
|
||||||
bool free_compute_buffer = false,
|
|
||||||
bool free_compute_params = false) {
|
|
||||||
int grid_h = static_cast<int>(frames.shape()[1] / config.vision.patch_size);
|
|
||||||
int grid_w = static_cast<int>(frames.shape()[0] / config.vision.patch_size);
|
|
||||||
auto pixel_values = process_video_block_tensor(frames, config.vision);
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
|
||||||
return build_encode_video_block_outputs_graph(pixel_values, grid_h, grid_w);
|
|
||||||
};
|
|
||||||
auto combined = take_or_empty(GGMLRunner::compute<float>(get_graph,
|
|
||||||
n_threads,
|
|
||||||
auto_free,
|
|
||||||
free_compute_buffer,
|
|
||||||
free_compute_params));
|
|
||||||
if (combined.empty()) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
size_t output_count = config.vision.deepstack_visual_indexes.size() + 1;
|
|
||||||
GGML_ASSERT(combined.shape()[0] == config.hidden_size * static_cast<int64_t>(output_count));
|
|
||||||
std::vector<sd::Tensor<float>> outputs;
|
|
||||||
outputs.reserve(output_count);
|
|
||||||
for (size_t i = 0; i < output_count; ++i) {
|
|
||||||
outputs.push_back(sd::ops::slice(combined,
|
|
||||||
0,
|
|
||||||
static_cast<int64_t>(i) * config.hidden_size,
|
|
||||||
static_cast<int64_t>(i + 1) * config.hidden_size));
|
|
||||||
}
|
|
||||||
return outputs;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LLMEmbedder {
|
struct LLMEmbedder {
|
||||||
|
|||||||
@ -1,28 +0,0 @@
|
|||||||
#ifndef __SD_MODEL_VAE_AUDIO_VAE_HPP__
|
|
||||||
#define __SD_MODEL_VAE_AUDIO_VAE_HPP__
|
|
||||||
|
|
||||||
#include "core/ggml_extend.hpp"
|
|
||||||
|
|
||||||
struct AudioVAERunner : public GGMLRunner {
|
|
||||||
AudioVAERunner(ggml_backend_t backend,
|
|
||||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
|
||||||
: GGMLRunner(backend, weight_manager) {}
|
|
||||||
|
|
||||||
virtual void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) = 0;
|
|
||||||
virtual size_t get_params_mem_size() = 0;
|
|
||||||
virtual std::string get_desc() = 0;
|
|
||||||
virtual sd::Tensor<float> encode(int n_threads,
|
|
||||||
const sd::Tensor<float>& waveform) {
|
|
||||||
SD_UNUSED(n_threads);
|
|
||||||
SD_UNUSED(waveform);
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
virtual sd::Tensor<float> decode(int n_threads,
|
|
||||||
const sd::Tensor<float>& latent_tensor) = 0;
|
|
||||||
virtual int input_sample_rate() const {
|
|
||||||
return output_sample_rate();
|
|
||||||
}
|
|
||||||
virtual int output_sample_rate() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // __SD_MODEL_VAE_AUDIO_VAE_HPP__
|
|
||||||
@ -8,7 +8,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "core/ggml_extend.hpp"
|
#include "core/ggml_extend.hpp"
|
||||||
#include "model/vae/audio_vae.hpp"
|
|
||||||
#include "model_loader.h"
|
#include "model_loader.h"
|
||||||
#include "model_manager.h"
|
#include "model_manager.h"
|
||||||
|
|
||||||
@ -997,7 +996,7 @@ namespace LTXV {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LTXAudioVAERunner : public AudioVAERunner {
|
struct LTXAudioVAERunner : public GGMLRunner {
|
||||||
LTXAudioVAEConfig config;
|
LTXAudioVAEConfig config;
|
||||||
LTXAudioVAE model;
|
LTXAudioVAE model;
|
||||||
std::string weight_prefix;
|
std::string weight_prefix;
|
||||||
@ -1007,7 +1006,7 @@ namespace LTXV {
|
|||||||
const String2TensorStorage& tensor_storage_map,
|
const String2TensorStorage& tensor_storage_map,
|
||||||
const std::string& prefix = "",
|
const std::string& prefix = "",
|
||||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
||||||
: AudioVAERunner(backend, weight_manager),
|
: GGMLRunner(backend, weight_manager),
|
||||||
weight_prefix(prefix),
|
weight_prefix(prefix),
|
||||||
config(LTXAudioVAEConfig::detect_from_weights(tensor_storage_map)),
|
config(LTXAudioVAEConfig::detect_from_weights(tensor_storage_map)),
|
||||||
model(config) {
|
model(config) {
|
||||||
@ -1018,20 +1017,20 @@ namespace LTXV {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) override {
|
void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) {
|
||||||
model.get_param_tensors(tensors, weight_prefix);
|
model.get_param_tensors(tensors, weight_prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t get_params_mem_size() override {
|
size_t get_params_mem_size() {
|
||||||
return model.get_params_mem_size();
|
return model.get_params_mem_size();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_desc() override {
|
std::string get_desc() {
|
||||||
return "ltx_audio_vae";
|
return "ltx_audio_vae";
|
||||||
}
|
}
|
||||||
|
|
||||||
sd::Tensor<float> decode(int n_threads,
|
sd::Tensor<float> decode(int n_threads,
|
||||||
const sd::Tensor<float>& latent_tensor) override {
|
const sd::Tensor<float>& latent_tensor) {
|
||||||
int64_t t0 = ggml_time_ms();
|
int64_t t0 = ggml_time_ms();
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
auto get_graph = [&]() -> ggml_cgraph* {
|
||||||
auto latent = make_input(latent_tensor);
|
auto latent = make_input(latent_tensor);
|
||||||
@ -1048,10 +1047,6 @@ namespace LTXV {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int output_sample_rate() const override {
|
|
||||||
return config.output_sample_rate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void test(const std::string& input_path) {
|
void test(const std::string& input_path) {
|
||||||
auto z = sd::load_tensor_from_file_as_tensor<float>(input_path);
|
auto z = sd::load_tensor_from_file_as_tensor<float>(input_path);
|
||||||
GGML_ASSERT(!z.empty());
|
GGML_ASSERT(!z.empty());
|
||||||
|
|||||||
@ -1,497 +0,0 @@
|
|||||||
#ifndef __SD_MODEL_VAE_MINIMAX_H3_AUDIO_VAE_HPP__
|
|
||||||
#define __SD_MODEL_VAE_MINIMAX_H3_AUDIO_VAE_HPP__
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "model/vae/audio_vae.hpp"
|
|
||||||
#include "model/vae/ltx_audio_vae.hpp"
|
|
||||||
|
|
||||||
namespace MiniMaxH3 {
|
|
||||||
|
|
||||||
struct AudioSnake1D : public UnaryBlock {
|
|
||||||
int64_t channels;
|
|
||||||
|
|
||||||
explicit AudioSnake1D(int64_t channels)
|
|
||||||
: channels(channels) {}
|
|
||||||
|
|
||||||
void init_params(ggml_context* ctx,
|
|
||||||
const String2TensorStorage& tensor_storage_map = {},
|
|
||||||
const std::string prefix = "") override {
|
|
||||||
SD_UNUSED(tensor_storage_map);
|
|
||||||
SD_UNUSED(prefix);
|
|
||||||
params["alpha"] = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 1, channels, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) override {
|
|
||||||
auto alpha = params["alpha"];
|
|
||||||
auto oscillation = ggml_sin(ctx->ggml_ctx, ggml_mul(ctx->ggml_ctx, x, alpha));
|
|
||||||
oscillation = ggml_mul(ctx->ggml_ctx, oscillation, oscillation);
|
|
||||||
auto eps = ggml_ext_scale(ctx->ggml_ctx, ggml_ext_ones(ctx->ggml_ctx, 1, 1, 1, 1), 1e-9f);
|
|
||||||
return ggml_add(ctx->ggml_ctx,
|
|
||||||
x,
|
|
||||||
ggml_div(ctx->ggml_ctx, oscillation, ggml_add(ctx->ggml_ctx, alpha, eps)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioEncoderResidualUnit : public GGMLBlock {
|
|
||||||
int64_t channels;
|
|
||||||
|
|
||||||
AudioEncoderResidualUnit(int64_t channels, int dilation)
|
|
||||||
: channels(channels) {
|
|
||||||
blocks["block.0"] = std::make_shared<AudioSnake1D>(channels);
|
|
||||||
blocks["block.1"] = std::make_shared<LTXV::Conv1D>(channels,
|
|
||||||
channels,
|
|
||||||
7,
|
|
||||||
1,
|
|
||||||
3 * dilation,
|
|
||||||
dilation);
|
|
||||||
blocks["block.2"] = std::make_shared<AudioSnake1D>(channels);
|
|
||||||
blocks["block.3"] = std::make_shared<LTXV::Conv1D>(channels, channels, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto act1 = std::dynamic_pointer_cast<AudioSnake1D>(blocks["block.0"]);
|
|
||||||
auto conv1 = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["block.1"]);
|
|
||||||
auto act2 = std::dynamic_pointer_cast<AudioSnake1D>(blocks["block.2"]);
|
|
||||||
auto conv2 = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["block.3"]);
|
|
||||||
auto h = conv2->forward(ctx, act2->forward(ctx, conv1->forward(ctx, act1->forward(ctx, x))));
|
|
||||||
if (x->ne[0] != h->ne[0]) {
|
|
||||||
int64_t pad = (x->ne[0] - h->ne[0]) / 2;
|
|
||||||
x = ggml_ext_slice(ctx->ggml_ctx, x, 0, pad, x->ne[0] - pad);
|
|
||||||
}
|
|
||||||
return ggml_add(ctx->ggml_ctx, x, h);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioEncoderBlock : public GGMLBlock {
|
|
||||||
int64_t out_channels;
|
|
||||||
|
|
||||||
AudioEncoderBlock(int64_t out_channels, int stride)
|
|
||||||
: out_channels(out_channels) {
|
|
||||||
int64_t in_channels = out_channels / 2;
|
|
||||||
blocks["block.0"] = std::make_shared<AudioEncoderResidualUnit>(in_channels, 1);
|
|
||||||
blocks["block.1"] = std::make_shared<AudioEncoderResidualUnit>(in_channels, 3);
|
|
||||||
blocks["block.2"] = std::make_shared<AudioEncoderResidualUnit>(in_channels, 9);
|
|
||||||
blocks["block.3"] = std::make_shared<AudioSnake1D>(in_channels);
|
|
||||||
blocks["block.4"] = std::make_shared<LTXV::Conv1D>(in_channels,
|
|
||||||
out_channels,
|
|
||||||
2 * stride,
|
|
||||||
stride,
|
|
||||||
static_cast<int>(std::ceil(stride / 2.f)));
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
|
||||||
auto unit = std::dynamic_pointer_cast<AudioEncoderResidualUnit>(blocks["block." + std::to_string(i)]);
|
|
||||||
x = unit->forward(ctx, x);
|
|
||||||
}
|
|
||||||
auto act = std::dynamic_pointer_cast<AudioSnake1D>(blocks["block.3"]);
|
|
||||||
auto conv = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["block.4"]);
|
|
||||||
return conv->forward(ctx, act->forward(ctx, x));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioEncoder : public GGMLBlock {
|
|
||||||
static constexpr std::array<int, 5> strides = {2, 4, 4, 5, 5};
|
|
||||||
|
|
||||||
AudioEncoder() {
|
|
||||||
int64_t channels = 64;
|
|
||||||
blocks["block.0"] = std::make_shared<LTXV::Conv1D>(1, channels, 7, 1, 3);
|
|
||||||
for (size_t i = 0; i < strides.size(); ++i) {
|
|
||||||
channels *= 2;
|
|
||||||
blocks["block." + std::to_string(i + 1)] = std::make_shared<AudioEncoderBlock>(channels, strides[i]);
|
|
||||||
}
|
|
||||||
blocks["block.6"] = std::make_shared<AudioSnake1D>(channels);
|
|
||||||
blocks["block.7"] = std::make_shared<LTXV::Conv1D>(channels, 2048, 3, 1, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto input = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["block.0"]);
|
|
||||||
x = input->forward(ctx, x);
|
|
||||||
for (size_t i = 0; i < strides.size(); ++i) {
|
|
||||||
auto block = std::dynamic_pointer_cast<AudioEncoderBlock>(blocks["block." + std::to_string(i + 1)]);
|
|
||||||
x = block->forward(ctx, x);
|
|
||||||
}
|
|
||||||
auto act = std::dynamic_pointer_cast<AudioSnake1D>(blocks["block.6"]);
|
|
||||||
auto out = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["block.7"]);
|
|
||||||
return out->forward(ctx, act->forward(ctx, x));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioGeGLUMLP : public GGMLBlock {
|
|
||||||
AudioGeGLUMLP(int64_t hidden_size, int64_t intermediate_size) {
|
|
||||||
blocks["norm"] = std::make_shared<LayerNorm>(hidden_size);
|
|
||||||
blocks["w0"] = std::make_shared<Linear>(hidden_size, intermediate_size, true);
|
|
||||||
blocks["w1"] = std::make_shared<Linear>(hidden_size, intermediate_size, true);
|
|
||||||
blocks["w2"] = std::make_shared<Linear>(intermediate_size, hidden_size, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto norm = std::dynamic_pointer_cast<LayerNorm>(blocks["norm"]);
|
|
||||||
auto w0 = std::dynamic_pointer_cast<Linear>(blocks["w0"]);
|
|
||||||
auto w1 = std::dynamic_pointer_cast<Linear>(blocks["w1"]);
|
|
||||||
auto w2 = std::dynamic_pointer_cast<Linear>(blocks["w2"]);
|
|
||||||
x = norm->forward(ctx, x);
|
|
||||||
auto gate = ggml_ext_gelu(ctx->ggml_ctx, w0->forward(ctx, x), true);
|
|
||||||
return w2->forward(ctx, ggml_mul(ctx->ggml_ctx, gate, w1->forward(ctx, x)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioCausalAttention : public GGMLBlock {
|
|
||||||
static constexpr int64_t in_channels = 2048;
|
|
||||||
static constexpr int64_t out_channels = 32;
|
|
||||||
static constexpr int64_t num_head = 8;
|
|
||||||
static constexpr int64_t head_dim = in_channels / num_head;
|
|
||||||
|
|
||||||
AudioCausalAttention() {
|
|
||||||
blocks["qkv"] = std::make_shared<Linear>(in_channels, in_channels * 3, false);
|
|
||||||
blocks["proj"] = std::make_shared<Linear>(out_channels, out_channels, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_params(ggml_context* ctx,
|
|
||||||
const String2TensorStorage& tensor_storage_map = {},
|
|
||||||
const std::string prefix = "") override {
|
|
||||||
GGMLBlock::init_params(ctx, tensor_storage_map, prefix);
|
|
||||||
params["q_bias"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, in_channels);
|
|
||||||
params["v_bias"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, in_channels);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto qkv_layer = std::dynamic_pointer_cast<Linear>(blocks["qkv"]);
|
|
||||||
auto proj = std::dynamic_pointer_cast<Linear>(blocks["proj"]);
|
|
||||||
auto qkv = ggml_ext_chunk(ctx->ggml_ctx, qkv_layer->forward(ctx, x), 3, 0);
|
|
||||||
auto bias_shape = [&](ggml_tensor* bias) {
|
|
||||||
return ggml_reshape_4d(ctx->ggml_ctx, bias, bias->ne[0], 1, 1, 1);
|
|
||||||
};
|
|
||||||
auto q = ggml_add(ctx->ggml_ctx, qkv[0], bias_shape(params["q_bias"]));
|
|
||||||
auto k = qkv[1];
|
|
||||||
auto v = ggml_add(ctx->ggml_ctx, qkv[2], bias_shape(params["v_bias"]));
|
|
||||||
|
|
||||||
int64_t sequence = x->ne[1];
|
|
||||||
auto mask = ggml_diag_mask_inf(ctx->ggml_ctx,
|
|
||||||
ggml_ext_zeros(ctx->ggml_ctx, sequence, sequence, 1, 1),
|
|
||||||
0);
|
|
||||||
auto attn_out = ggml_ext_attention_ext(ctx->ggml_ctx,
|
|
||||||
ctx->backend,
|
|
||||||
q,
|
|
||||||
k,
|
|
||||||
v,
|
|
||||||
num_head,
|
|
||||||
mask,
|
|
||||||
false,
|
|
||||||
ctx->flash_attn_enabled);
|
|
||||||
int64_t batch = attn_out->ne[2] * attn_out->ne[3];
|
|
||||||
attn_out = ggml_reshape_4d(ctx->ggml_ctx, attn_out, head_dim, num_head, sequence, batch);
|
|
||||||
attn_out = ggml_cont(ctx->ggml_ctx, ggml_permute(ctx->ggml_ctx, attn_out, 1, 0, 2, 3));
|
|
||||||
attn_out = ggml_mean(ctx->ggml_ctx, attn_out);
|
|
||||||
attn_out = ggml_reshape_3d(ctx->ggml_ctx, attn_out, head_dim, sequence, batch);
|
|
||||||
|
|
||||||
constexpr int64_t pool = head_dim / out_channels;
|
|
||||||
attn_out = ggml_reshape_4d(ctx->ggml_ctx, attn_out, pool, out_channels, sequence, batch);
|
|
||||||
attn_out = ggml_mean(ctx->ggml_ctx, attn_out);
|
|
||||||
attn_out = ggml_reshape_3d(ctx->ggml_ctx, attn_out, out_channels, sequence, batch);
|
|
||||||
return proj->forward(ctx, attn_out);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioAttentionProjection : public GGMLBlock {
|
|
||||||
AudioAttentionProjection() {
|
|
||||||
blocks["norm1"] = std::make_shared<LayerNorm>(2048);
|
|
||||||
blocks["attn"] = std::make_shared<AudioCausalAttention>();
|
|
||||||
blocks["proj"] = std::make_shared<Linear>(2048, 32, true);
|
|
||||||
blocks["norm3"] = std::make_shared<LayerNorm>(2048);
|
|
||||||
blocks["norm2"] = std::make_shared<LayerNorm>(32);
|
|
||||||
blocks["mlp"] = std::make_shared<AudioGeGLUMLP>(32, 64);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto norm1 = std::dynamic_pointer_cast<LayerNorm>(blocks["norm1"]);
|
|
||||||
auto attn = std::dynamic_pointer_cast<AudioCausalAttention>(blocks["attn"]);
|
|
||||||
auto proj = std::dynamic_pointer_cast<Linear>(blocks["proj"]);
|
|
||||||
auto norm3 = std::dynamic_pointer_cast<LayerNorm>(blocks["norm3"]);
|
|
||||||
auto norm2 = std::dynamic_pointer_cast<LayerNorm>(blocks["norm2"]);
|
|
||||||
auto mlp = std::dynamic_pointer_cast<AudioGeGLUMLP>(blocks["mlp"]);
|
|
||||||
x = ggml_add(ctx->ggml_ctx,
|
|
||||||
proj->forward(ctx, norm3->forward(ctx, x)),
|
|
||||||
attn->forward(ctx, norm1->forward(ctx, x)));
|
|
||||||
return ggml_add(ctx->ggml_ctx, x, mlp->forward(ctx, norm2->forward(ctx, x)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioAMPBlock : public GGMLBlock {
|
|
||||||
int channels;
|
|
||||||
|
|
||||||
AudioAMPBlock(int channels,
|
|
||||||
int kernel_size,
|
|
||||||
const std::array<int, 3>& dilations)
|
|
||||||
: channels(channels) {
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
|
||||||
blocks["activations." + std::to_string(i * 2)] =
|
|
||||||
std::make_shared<LTXV::Activation1D>(channels);
|
|
||||||
blocks["activations." + std::to_string(i * 2 + 1)] =
|
|
||||||
std::make_shared<LTXV::Activation1D>(channels);
|
|
||||||
blocks["convs1." + std::to_string(i)] =
|
|
||||||
std::make_shared<LTXV::Conv1D>(channels,
|
|
||||||
channels,
|
|
||||||
kernel_size,
|
|
||||||
1,
|
|
||||||
(kernel_size * dilations[i] - dilations[i]) / 2,
|
|
||||||
dilations[i]);
|
|
||||||
blocks["convs2." + std::to_string(i)] =
|
|
||||||
std::make_shared<LTXV::Conv1D>(channels,
|
|
||||||
channels,
|
|
||||||
kernel_size,
|
|
||||||
1,
|
|
||||||
kernel_size / 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
|
||||||
auto act1 = std::dynamic_pointer_cast<LTXV::Activation1D>(
|
|
||||||
blocks["activations." + std::to_string(i * 2)]);
|
|
||||||
auto act2 = std::dynamic_pointer_cast<LTXV::Activation1D>(
|
|
||||||
blocks["activations." + std::to_string(i * 2 + 1)]);
|
|
||||||
auto conv1 = std::dynamic_pointer_cast<LTXV::Conv1D>(
|
|
||||||
blocks["convs1." + std::to_string(i)]);
|
|
||||||
auto conv2 = std::dynamic_pointer_cast<LTXV::Conv1D>(
|
|
||||||
blocks["convs2." + std::to_string(i)]);
|
|
||||||
|
|
||||||
auto h = conv1->forward(ctx, act1->forward(ctx, x));
|
|
||||||
h = conv2->forward(ctx, act2->forward(ctx, h));
|
|
||||||
x = ggml_add(ctx->ggml_ctx, x, h);
|
|
||||||
}
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct BigVGAN : public GGMLBlock {
|
|
||||||
static constexpr int initial_channels = 1024;
|
|
||||||
static constexpr int num_kernels = 3;
|
|
||||||
static constexpr int num_upsamples = 7;
|
|
||||||
static constexpr std::array<int, num_upsamples> rates = {5, 5, 2, 2, 2, 2, 2};
|
|
||||||
static constexpr std::array<int, num_upsamples> kernels = {9, 9, 4, 4, 4, 4, 4};
|
|
||||||
static constexpr std::array<int, num_kernels> res_kernels = {3, 7, 11};
|
|
||||||
|
|
||||||
BigVGAN() {
|
|
||||||
blocks["conv_pre"] = std::make_shared<LTXV::Conv1D>(2048,
|
|
||||||
initial_channels,
|
|
||||||
7,
|
|
||||||
1,
|
|
||||||
3);
|
|
||||||
int channels = initial_channels;
|
|
||||||
for (int i = 0; i < num_upsamples; ++i) {
|
|
||||||
int next_channels = initial_channels / (1 << (i + 1));
|
|
||||||
blocks["ups." + std::to_string(i) + ".0"] =
|
|
||||||
std::make_shared<LTXV::ConvTranspose1D>(channels,
|
|
||||||
next_channels,
|
|
||||||
kernels[i],
|
|
||||||
rates[i],
|
|
||||||
(kernels[i] - rates[i]) / 2);
|
|
||||||
for (int j = 0; j < num_kernels; ++j) {
|
|
||||||
blocks["resblocks." + std::to_string(i * num_kernels + j)] =
|
|
||||||
std::make_shared<AudioAMPBlock>(next_channels,
|
|
||||||
res_kernels[j],
|
|
||||||
std::array<int, 3>{1, 3, 5});
|
|
||||||
}
|
|
||||||
channels = next_channels;
|
|
||||||
}
|
|
||||||
blocks["activation_post"] = std::make_shared<LTXV::Activation1D>(channels);
|
|
||||||
blocks["conv_post"] = std::make_shared<LTXV::Conv1D>(channels,
|
|
||||||
1,
|
|
||||||
7,
|
|
||||||
1,
|
|
||||||
3,
|
|
||||||
1,
|
|
||||||
false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto conv_pre = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["conv_pre"]);
|
|
||||||
x = conv_pre->forward(ctx, x);
|
|
||||||
for (int i = 0; i < num_upsamples; ++i) {
|
|
||||||
auto up = std::dynamic_pointer_cast<LTXV::ConvTranspose1D>(
|
|
||||||
blocks["ups." + std::to_string(i) + ".0"]);
|
|
||||||
x = up->forward(ctx, x);
|
|
||||||
|
|
||||||
ggml_tensor* sum = nullptr;
|
|
||||||
for (int j = 0; j < num_kernels; ++j) {
|
|
||||||
auto block = std::dynamic_pointer_cast<AudioAMPBlock>(
|
|
||||||
blocks["resblocks." + std::to_string(i * num_kernels + j)]);
|
|
||||||
auto value = block->forward(ctx, x);
|
|
||||||
sum = sum == nullptr ? value : ggml_add(ctx->ggml_ctx, sum, value);
|
|
||||||
}
|
|
||||||
x = ggml_ext_scale(ctx->ggml_ctx, sum, 1.f / num_kernels);
|
|
||||||
}
|
|
||||||
auto activation = std::dynamic_pointer_cast<LTXV::Activation1D>(blocks["activation_post"]);
|
|
||||||
auto conv_post = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["conv_post"]);
|
|
||||||
return ggml_clamp(ctx->ggml_ctx,
|
|
||||||
conv_post->forward(ctx, activation->forward(ctx, x)),
|
|
||||||
-1.f,
|
|
||||||
1.f);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioVAE : public GGMLBlock {
|
|
||||||
static constexpr int kLatentChannels = 32;
|
|
||||||
|
|
||||||
AudioVAE() {
|
|
||||||
blocks["encoder"] = std::make_shared<AudioEncoder>();
|
|
||||||
blocks["pre_block"] = std::make_shared<AudioAttentionProjection>();
|
|
||||||
blocks["mean_proj"] = std::make_shared<LTXV::Conv1D>(kLatentChannels, kLatentChannels, 1);
|
|
||||||
blocks["logs_proj"] = std::make_shared<LTXV::Conv1D>(kLatentChannels, kLatentChannels, 1);
|
|
||||||
blocks["dec_in_proj"] = std::make_shared<LTXV::Conv1D>(kLatentChannels,
|
|
||||||
2048,
|
|
||||||
1);
|
|
||||||
blocks["decoder"] = std::make_shared<BigVGAN>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_params(ggml_context* ctx,
|
|
||||||
const String2TensorStorage& tensor_storage_map = {},
|
|
||||||
const std::string prefix = "") override {
|
|
||||||
SD_UNUSED(tensor_storage_map);
|
|
||||||
SD_UNUSED(prefix);
|
|
||||||
params["latents_mean"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, kLatentChannels);
|
|
||||||
params["latents_std"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, kLatentChannels);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* encode(GGMLRunnerContext* ctx, ggml_tensor* waveform) {
|
|
||||||
GGML_ASSERT(waveform->ne[1] == 2);
|
|
||||||
auto encoder = std::dynamic_pointer_cast<AudioEncoder>(blocks["encoder"]);
|
|
||||||
auto pre = std::dynamic_pointer_cast<AudioAttentionProjection>(blocks["pre_block"]);
|
|
||||||
auto mean_proj = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["mean_proj"]);
|
|
||||||
|
|
||||||
waveform = ggml_reshape_3d(ctx->ggml_ctx, waveform, waveform->ne[0], 1, waveform->ne[1]);
|
|
||||||
auto x = encoder->forward(ctx, waveform); // [B*S, 2048, T]
|
|
||||||
x = ggml_cont(ctx->ggml_ctx, ggml_permute(ctx->ggml_ctx, x, 1, 0, 2, 3));
|
|
||||||
x = pre->forward(ctx, x);
|
|
||||||
x = ggml_cont(ctx->ggml_ctx, ggml_permute(ctx->ggml_ctx, x, 1, 0, 2, 3));
|
|
||||||
auto z = mean_proj->forward(ctx, x);
|
|
||||||
|
|
||||||
auto mean = ggml_reshape_4d(ctx->ggml_ctx, params["latents_mean"], 1, kLatentChannels, 1, 1);
|
|
||||||
auto std = ggml_reshape_4d(ctx->ggml_ctx, params["latents_std"], 1, kLatentChannels, 1, 1);
|
|
||||||
z = ggml_div(ctx->ggml_ctx, ggml_sub(ctx->ggml_ctx, z, mean), std);
|
|
||||||
return ggml_cont(ctx->ggml_ctx, ggml_permute(ctx->ggml_ctx, z, 0, 2, 1, 3));
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* decode(GGMLRunnerContext* ctx, ggml_tensor* latent) {
|
|
||||||
GGML_ASSERT(latent->ne[1] == 2 && latent->ne[2] == kLatentChannels);
|
|
||||||
latent = ggml_cont(ctx->ggml_ctx,
|
|
||||||
ggml_permute(ctx->ggml_ctx, latent, 0, 2, 1, 3));
|
|
||||||
|
|
||||||
auto mean = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
params["latents_mean"],
|
|
||||||
1,
|
|
||||||
kLatentChannels,
|
|
||||||
1,
|
|
||||||
1);
|
|
||||||
auto std = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
params["latents_std"],
|
|
||||||
1,
|
|
||||||
kLatentChannels,
|
|
||||||
1,
|
|
||||||
1);
|
|
||||||
latent = ggml_add(ctx->ggml_ctx,
|
|
||||||
ggml_mul(ctx->ggml_ctx, latent, std),
|
|
||||||
mean);
|
|
||||||
|
|
||||||
auto dec_in = std::dynamic_pointer_cast<LTXV::Conv1D>(blocks["dec_in_proj"]);
|
|
||||||
auto decoder = std::dynamic_pointer_cast<BigVGAN>(blocks["decoder"]);
|
|
||||||
int64_t streams = latent->ne[2] * latent->ne[3];
|
|
||||||
latent = ggml_reshape_3d(ctx->ggml_ctx,
|
|
||||||
latent,
|
|
||||||
latent->ne[0],
|
|
||||||
latent->ne[1],
|
|
||||||
streams);
|
|
||||||
ggml_tensor* waveform = nullptr;
|
|
||||||
for (int64_t stream = 0; stream < streams; ++stream) {
|
|
||||||
auto stream_latent = ggml_ext_slice(ctx->ggml_ctx, latent, 2, stream, stream + 1);
|
|
||||||
auto stream_waveform = decoder->forward(ctx, dec_in->forward(ctx, stream_latent));
|
|
||||||
waveform = waveform == nullptr
|
|
||||||
? stream_waveform
|
|
||||||
: ggml_concat(ctx->ggml_ctx, waveform, stream_waveform, 2);
|
|
||||||
}
|
|
||||||
return ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
waveform,
|
|
||||||
waveform->ne[0],
|
|
||||||
streams,
|
|
||||||
1,
|
|
||||||
1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AudioVAERunner : public ::AudioVAERunner {
|
|
||||||
AudioVAE model;
|
|
||||||
std::string weight_prefix;
|
|
||||||
|
|
||||||
AudioVAERunner(ggml_backend_t backend,
|
|
||||||
const String2TensorStorage& tensor_storage_map,
|
|
||||||
const std::string& prefix = "",
|
|
||||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
|
||||||
: ::AudioVAERunner(backend, weight_manager),
|
|
||||||
weight_prefix(prefix) {
|
|
||||||
model.init(params_ctx, tensor_storage_map, prefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) override {
|
|
||||||
model.get_param_tensors(tensors, weight_prefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t get_params_mem_size() override {
|
|
||||||
return model.get_params_mem_size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_desc() override {
|
|
||||||
return "minimax_h3_audio_vae";
|
|
||||||
}
|
|
||||||
|
|
||||||
int output_sample_rate() const override {
|
|
||||||
return 32000;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> encode(int n_threads,
|
|
||||||
const sd::Tensor<float>& waveform) override {
|
|
||||||
int64_t t0 = ggml_time_ms();
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
|
||||||
auto input = make_input(waveform);
|
|
||||||
auto runner_ctx = get_context();
|
|
||||||
auto latent = model.encode(&runner_ctx, input);
|
|
||||||
auto graph = new_graph_custom(655360);
|
|
||||||
ggml_build_forward_expand(graph, latent);
|
|
||||||
return graph;
|
|
||||||
};
|
|
||||||
auto result = restore_trailing_singleton_dims(
|
|
||||||
GGMLRunner::compute<float>(get_graph, n_threads, false, false, false),
|
|
||||||
4);
|
|
||||||
int64_t t1 = ggml_time_ms();
|
|
||||||
LOG_INFO("MiniMax-H3 audio VAE encode completed, taking %.2fs",
|
|
||||||
(t1 - t0) / 1000.f);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> decode(int n_threads,
|
|
||||||
const sd::Tensor<float>& latent_tensor) override {
|
|
||||||
int64_t t0 = ggml_time_ms();
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
|
||||||
auto latent = make_input(latent_tensor);
|
|
||||||
auto runner_ctx = get_context();
|
|
||||||
auto waveform = model.decode(&runner_ctx, latent);
|
|
||||||
auto graph = new_graph_custom(655360);
|
|
||||||
ggml_build_forward_expand(graph, waveform);
|
|
||||||
return graph;
|
|
||||||
};
|
|
||||||
auto result = restore_trailing_singleton_dims(
|
|
||||||
GGMLRunner::compute<float>(get_graph, n_threads, false, false, false),
|
|
||||||
4);
|
|
||||||
int64_t t1 = ggml_time_ms();
|
|
||||||
LOG_INFO("MiniMax-H3 audio VAE decode completed, taking %.2fs",
|
|
||||||
(t1 - t0) / 1000.f);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace MiniMaxH3
|
|
||||||
|
|
||||||
#endif // __SD_MODEL_VAE_MINIMAX_H3_AUDIO_VAE_HPP__
|
|
||||||
@ -1,805 +0,0 @@
|
|||||||
#ifndef __SD_MODEL_VAE_MINIMAX_H3_VAE_HPP__
|
|
||||||
#define __SD_MODEL_VAE_MINIMAX_H3_VAE_HPP__
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <array>
|
|
||||||
#include <cmath>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <tuple>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "model/common/rope.hpp"
|
|
||||||
#include "model/diffusion/dit.hpp"
|
|
||||||
#include "model/vae/vae.hpp"
|
|
||||||
|
|
||||||
namespace MiniMaxH3VAE {
|
|
||||||
|
|
||||||
constexpr int H3_VIDEO_VAE_GRAPH_SIZE = 262144;
|
|
||||||
|
|
||||||
struct CausalConv3d : public Conv3d {
|
|
||||||
std::tuple<int, int, int> temporal_padding;
|
|
||||||
|
|
||||||
CausalConv3d(int64_t in_channels,
|
|
||||||
int64_t out_channels,
|
|
||||||
std::tuple<int, int, int> kernel_size,
|
|
||||||
std::tuple<int, int, int> stride = {1, 1, 1},
|
|
||||||
std::tuple<int, int, int> padding = {0, 0, 0})
|
|
||||||
: Conv3d(in_channels,
|
|
||||||
out_channels,
|
|
||||||
kernel_size,
|
|
||||||
stride,
|
|
||||||
{0, 0, 0}),
|
|
||||||
temporal_padding(padding) {}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) override {
|
|
||||||
auto reflect_pad = [&](ggml_tensor* value, int dim, int amount) {
|
|
||||||
for (int i = 0; i < amount; ++i) {
|
|
||||||
GGML_ASSERT(value->ne[dim] > 1);
|
|
||||||
auto left = ggml_ext_slice(ctx->ggml_ctx, value, dim, 1, 2);
|
|
||||||
auto right = ggml_ext_slice(ctx->ggml_ctx,
|
|
||||||
value,
|
|
||||||
dim,
|
|
||||||
value->ne[dim] - 2,
|
|
||||||
value->ne[dim] - 1);
|
|
||||||
value = ggml_concat(ctx->ggml_ctx, left, value, dim);
|
|
||||||
value = ggml_concat(ctx->ggml_ctx, value, right, dim);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
};
|
|
||||||
|
|
||||||
x = reflect_pad(x, 0, std::get<2>(temporal_padding));
|
|
||||||
x = reflect_pad(x, 1, std::get<1>(temporal_padding));
|
|
||||||
int temporal_pad = std::get<0>(temporal_padding) * 2;
|
|
||||||
if (temporal_pad > 0) {
|
|
||||||
x = ggml_ext_pad_ext(ctx->ggml_ctx,
|
|
||||||
ctx->backend,
|
|
||||||
x,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
temporal_pad,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0);
|
|
||||||
}
|
|
||||||
return Conv3d::forward(ctx, x);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TemporalGroupNorm : public GroupNorm {
|
|
||||||
explicit TemporalGroupNorm(int64_t channels)
|
|
||||||
: GroupNorm(32, channels, 1e-6f, true) {}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
ggml_tensor* result = nullptr;
|
|
||||||
for (int64_t t = 0; t < x->ne[2]; ++t) {
|
|
||||||
auto frame = ggml_ext_slice(ctx->ggml_ctx, x, 2, t, t + 1);
|
|
||||||
GGML_ASSERT(frame->ne[3] % num_channels == 0);
|
|
||||||
int64_t batch_size = frame->ne[3] / num_channels;
|
|
||||||
frame = ggml_cont(ctx->ggml_ctx, frame);
|
|
||||||
frame = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
frame,
|
|
||||||
frame->ne[0],
|
|
||||||
frame->ne[1],
|
|
||||||
num_channels,
|
|
||||||
batch_size);
|
|
||||||
frame = GroupNorm::forward(ctx, frame);
|
|
||||||
frame = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
frame,
|
|
||||||
frame->ne[0],
|
|
||||||
frame->ne[1],
|
|
||||||
1,
|
|
||||||
num_channels * batch_size);
|
|
||||||
result = result == nullptr ? frame : ggml_concat(ctx->ggml_ctx, result, frame, 2);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Downsample3D : public GGMLBlock {
|
|
||||||
int spatial_stride;
|
|
||||||
|
|
||||||
Downsample3D(int64_t in_channels,
|
|
||||||
int64_t out_channels,
|
|
||||||
int temporal_stride,
|
|
||||||
int spatial_stride)
|
|
||||||
: spatial_stride(spatial_stride) {
|
|
||||||
blocks["conv"] = std::make_shared<CausalConv3d>(in_channels,
|
|
||||||
out_channels,
|
|
||||||
std::tuple{3, 3, 3},
|
|
||||||
std::tuple{temporal_stride, spatial_stride, spatial_stride},
|
|
||||||
std::tuple{1, 0, 0});
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
if (spatial_stride == 2) {
|
|
||||||
GGML_ASSERT(x->ne[0] > 1 && x->ne[1] > 1);
|
|
||||||
auto right = ggml_ext_slice(ctx->ggml_ctx, x, 0, x->ne[0] - 2, x->ne[0] - 1);
|
|
||||||
x = ggml_concat(ctx->ggml_ctx, x, right, 0);
|
|
||||||
auto bottom = ggml_ext_slice(ctx->ggml_ctx, x, 1, x->ne[1] - 2, x->ne[1] - 1);
|
|
||||||
x = ggml_concat(ctx->ggml_ctx, x, bottom, 1);
|
|
||||||
}
|
|
||||||
return std::dynamic_pointer_cast<CausalConv3d>(blocks["conv"])->forward(ctx, x);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ResnetBlock3D : public GGMLBlock {
|
|
||||||
int64_t in_channels;
|
|
||||||
int64_t out_channels;
|
|
||||||
|
|
||||||
ResnetBlock3D(int64_t in_channels,
|
|
||||||
int64_t out_channels)
|
|
||||||
: in_channels(in_channels), out_channels(out_channels) {
|
|
||||||
blocks["norm1"] = std::make_shared<TemporalGroupNorm>(in_channels);
|
|
||||||
blocks["norm2"] = std::make_shared<TemporalGroupNorm>(out_channels);
|
|
||||||
blocks["conv1"] = std::make_shared<CausalConv3d>(in_channels,
|
|
||||||
out_channels,
|
|
||||||
std::tuple{3, 3, 3},
|
|
||||||
std::tuple{1, 1, 1},
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
blocks["conv2"] = std::make_shared<CausalConv3d>(out_channels,
|
|
||||||
out_channels,
|
|
||||||
std::tuple{3, 3, 3},
|
|
||||||
std::tuple{1, 1, 1},
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
if (in_channels != out_channels) {
|
|
||||||
blocks["nin_shortcut"] = std::make_shared<CausalConv3d>(in_channels,
|
|
||||||
out_channels,
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto norm1 = std::dynamic_pointer_cast<TemporalGroupNorm>(blocks["norm1"]);
|
|
||||||
auto norm2 = std::dynamic_pointer_cast<TemporalGroupNorm>(blocks["norm2"]);
|
|
||||||
auto conv1 = std::dynamic_pointer_cast<CausalConv3d>(blocks["conv1"]);
|
|
||||||
auto conv2 = std::dynamic_pointer_cast<CausalConv3d>(blocks["conv2"]);
|
|
||||||
auto h = conv1->forward(ctx, ggml_silu(ctx->ggml_ctx, norm1->forward(ctx, x)));
|
|
||||||
h = conv2->forward(ctx, ggml_silu(ctx->ggml_ctx, norm2->forward(ctx, h)));
|
|
||||||
if (in_channels != out_channels) {
|
|
||||||
x = std::dynamic_pointer_cast<CausalConv3d>(blocks["nin_shortcut"])->forward(ctx, x);
|
|
||||||
}
|
|
||||||
return ggml_add(ctx->ggml_ctx, x, h);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Encoder : public GGMLBlock {
|
|
||||||
static constexpr int levels = 6;
|
|
||||||
static constexpr std::array<int, levels> multipliers = {1, 2, 2, 4, 4, 8};
|
|
||||||
static constexpr std::array<int, levels> spatial_down = {2, 2, 2, 2, 1, 1};
|
|
||||||
static constexpr std::array<int, levels> temporal_down = {1, 2, 2, 1, 1, 1};
|
|
||||||
|
|
||||||
Encoder() {
|
|
||||||
constexpr int ch = 128;
|
|
||||||
blocks["conv_in"] = std::make_shared<CausalConv3d>(3,
|
|
||||||
ch,
|
|
||||||
std::tuple{3, 3, 3},
|
|
||||||
std::tuple{1, 1, 1},
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
int64_t previous = ch;
|
|
||||||
for (int level = 0; level < levels; ++level) {
|
|
||||||
int64_t current = ch * multipliers[level];
|
|
||||||
for (int block = 0; block < 2; ++block) {
|
|
||||||
blocks["down." + std::to_string(level) + ".block." + std::to_string(block)] =
|
|
||||||
std::make_shared<ResnetBlock3D>(block == 0 ? previous : current,
|
|
||||||
current);
|
|
||||||
}
|
|
||||||
if (spatial_down[level] * temporal_down[level] > 1) {
|
|
||||||
blocks["down." + std::to_string(level) + ".downsample"] =
|
|
||||||
std::make_shared<Downsample3D>(current,
|
|
||||||
current,
|
|
||||||
temporal_down[level],
|
|
||||||
spatial_down[level]);
|
|
||||||
}
|
|
||||||
previous = current;
|
|
||||||
}
|
|
||||||
blocks["norm_out"] = std::make_shared<TemporalGroupNorm>(previous);
|
|
||||||
blocks["conv_out"] = std::make_shared<CausalConv3d>(previous,
|
|
||||||
48,
|
|
||||||
std::tuple{3, 3, 3},
|
|
||||||
std::tuple{1, 1, 1},
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
x = std::dynamic_pointer_cast<CausalConv3d>(blocks["conv_in"])->forward(ctx, x);
|
|
||||||
for (int level = 0; level < levels; ++level) {
|
|
||||||
for (int block = 0; block < 2; ++block) {
|
|
||||||
x = std::dynamic_pointer_cast<ResnetBlock3D>(
|
|
||||||
blocks["down." + std::to_string(level) + ".block." + std::to_string(block)])
|
|
||||||
->forward(ctx, x);
|
|
||||||
}
|
|
||||||
auto downsample = blocks.find("down." + std::to_string(level) + ".downsample");
|
|
||||||
if (downsample != blocks.end()) {
|
|
||||||
x = std::dynamic_pointer_cast<Downsample3D>(downsample->second)->forward(ctx, x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
auto norm = std::dynamic_pointer_cast<TemporalGroupNorm>(blocks["norm_out"]);
|
|
||||||
auto conv = std::dynamic_pointer_cast<CausalConv3d>(blocks["conv_out"]);
|
|
||||||
return conv->forward(ctx, ggml_silu(ctx->ggml_ctx, norm->forward(ctx, x)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static ggml_tensor* attention_layout(ggml_context* ctx, ggml_tensor* x) {
|
|
||||||
x = ggml_cont(ctx, ggml_permute(ctx, x, 0, 2, 1, 3));
|
|
||||||
return ggml_reshape_3d(ctx, x, x->ne[0], x->ne[1], x->ne[2] * x->ne[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ggml_tensor* apply_partial_rope(ggml_context* ctx,
|
|
||||||
ggml_tensor* x,
|
|
||||||
ggml_tensor* pe) {
|
|
||||||
int64_t rot_dim = pe->ne[2] * 2;
|
|
||||||
auto rotated = Rope::apply_rope(ctx,
|
|
||||||
ggml_ext_slice(ctx, x, 0, 0, rot_dim),
|
|
||||||
pe,
|
|
||||||
false);
|
|
||||||
if (rot_dim == x->ne[0]) {
|
|
||||||
return rotated;
|
|
||||||
}
|
|
||||||
auto tail = attention_layout(ctx,
|
|
||||||
ggml_ext_slice(ctx, x, 0, rot_dim, x->ne[0]));
|
|
||||||
return ggml_concat(ctx, rotated, tail, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DecoderAttention : public GGMLBlock {
|
|
||||||
static constexpr int num_head = 32;
|
|
||||||
static constexpr int head_dim = 64;
|
|
||||||
static constexpr int dim = num_head * head_dim;
|
|
||||||
|
|
||||||
DecoderAttention() {
|
|
||||||
blocks["to_qkv"] = std::make_shared<Linear>(dim, dim * 3, true);
|
|
||||||
blocks["to_out"] = std::make_shared<Linear>(dim, dim, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* x,
|
|
||||||
ggml_tensor* pe) {
|
|
||||||
auto to_qkv = std::dynamic_pointer_cast<Linear>(blocks["to_qkv"]);
|
|
||||||
auto to_out = std::dynamic_pointer_cast<Linear>(blocks["to_out"]);
|
|
||||||
auto qkv_projection = to_qkv->forward(ctx, x);
|
|
||||||
int64_t sequence = x->ne[1];
|
|
||||||
int64_t batch_size = x->ne[2] * x->ne[3];
|
|
||||||
qkv_projection = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
qkv_projection,
|
|
||||||
3 * head_dim,
|
|
||||||
num_head,
|
|
||||||
sequence,
|
|
||||||
batch_size);
|
|
||||||
auto qkv = ggml_ext_chunk(ctx->ggml_ctx, qkv_projection, 3, 0);
|
|
||||||
auto q = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
qkv[0],
|
|
||||||
head_dim,
|
|
||||||
num_head,
|
|
||||||
sequence,
|
|
||||||
batch_size);
|
|
||||||
auto k = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
qkv[1],
|
|
||||||
head_dim,
|
|
||||||
num_head,
|
|
||||||
sequence,
|
|
||||||
batch_size);
|
|
||||||
auto v = ggml_reshape_4d(ctx->ggml_ctx,
|
|
||||||
qkv[2],
|
|
||||||
head_dim,
|
|
||||||
num_head,
|
|
||||||
sequence,
|
|
||||||
batch_size);
|
|
||||||
q = ggml_rms_norm(ctx->ggml_ctx, q, 1e-5f);
|
|
||||||
k = ggml_rms_norm(ctx->ggml_ctx, k, 1e-5f);
|
|
||||||
q = apply_partial_rope(ctx->ggml_ctx, q, pe);
|
|
||||||
k = apply_partial_rope(ctx->ggml_ctx, k, pe);
|
|
||||||
auto out = ggml_ext_attention_ext(ctx->ggml_ctx,
|
|
||||||
ctx->backend,
|
|
||||||
q,
|
|
||||||
k,
|
|
||||||
v,
|
|
||||||
num_head,
|
|
||||||
nullptr,
|
|
||||||
true,
|
|
||||||
ctx->flash_attn_enabled);
|
|
||||||
return to_out->forward(ctx, out);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DecoderFeedForward : public GGMLBlock {
|
|
||||||
static constexpr int dim = 2048;
|
|
||||||
static constexpr int kInnerDim = dim * 4;
|
|
||||||
|
|
||||||
DecoderFeedForward() {
|
|
||||||
blocks["w1"] = std::make_shared<Linear>(dim, kInnerDim * 2, true);
|
|
||||||
blocks["w2"] = std::make_shared<Linear>(kInnerDim, dim, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
|
||||||
auto w1 = std::dynamic_pointer_cast<Linear>(blocks["w1"]);
|
|
||||||
auto w2 = std::dynamic_pointer_cast<Linear>(blocks["w2"]);
|
|
||||||
auto gate = ggml_ext_chunk(ctx->ggml_ctx, w1->forward(ctx, x), 2, 0);
|
|
||||||
return w2->forward(ctx,
|
|
||||||
ggml_mul(ctx->ggml_ctx,
|
|
||||||
ggml_silu(ctx->ggml_ctx, gate[0]),
|
|
||||||
gate[1]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DecoderBlock : public GGMLBlock {
|
|
||||||
static constexpr int dim = 2048;
|
|
||||||
|
|
||||||
DecoderBlock() {
|
|
||||||
blocks["norm1"] = std::make_shared<RMSNorm>(dim, 1e-5f);
|
|
||||||
blocks["attn"] = std::make_shared<DecoderAttention>();
|
|
||||||
blocks["norm2"] = std::make_shared<RMSNorm>(dim, 1e-5f);
|
|
||||||
blocks["ff"] = std::make_shared<DecoderFeedForward>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_params(ggml_context* ctx,
|
|
||||||
const String2TensorStorage& tensor_storage_map = {},
|
|
||||||
const std::string prefix = "") override {
|
|
||||||
SD_UNUSED(tensor_storage_map);
|
|
||||||
SD_UNUSED(prefix);
|
|
||||||
params["scale1"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
|
|
||||||
params["scale2"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* x,
|
|
||||||
ggml_tensor* pe) {
|
|
||||||
auto norm1 = std::dynamic_pointer_cast<RMSNorm>(blocks["norm1"]);
|
|
||||||
auto attn = std::dynamic_pointer_cast<DecoderAttention>(blocks["attn"]);
|
|
||||||
auto norm2 = std::dynamic_pointer_cast<RMSNorm>(blocks["norm2"]);
|
|
||||||
auto ff = std::dynamic_pointer_cast<DecoderFeedForward>(blocks["ff"]);
|
|
||||||
x = ggml_add(ctx->ggml_ctx,
|
|
||||||
x,
|
|
||||||
ggml_mul(ctx->ggml_ctx,
|
|
||||||
attn->forward(ctx, norm1->forward(ctx, x), pe),
|
|
||||||
params["scale1"]));
|
|
||||||
return ggml_add(ctx->ggml_ctx,
|
|
||||||
x,
|
|
||||||
ggml_mul(ctx->ggml_ctx,
|
|
||||||
ff->forward(ctx, norm2->forward(ctx, x)),
|
|
||||||
params["scale2"]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Decoder : public GGMLBlock {
|
|
||||||
static constexpr int dim = 2048;
|
|
||||||
static constexpr int num_layers = 36;
|
|
||||||
static constexpr int num_register_tokens = 4;
|
|
||||||
static constexpr int patch_size = 16;
|
|
||||||
static constexpr int patch_size_t = 4;
|
|
||||||
|
|
||||||
Decoder() {
|
|
||||||
blocks["x_embedder"] = std::make_shared<Linear>(24, dim, true);
|
|
||||||
for (int i = 0; i < num_layers; ++i) {
|
|
||||||
blocks["transformer_blocks." + std::to_string(i)] =
|
|
||||||
std::make_shared<DecoderBlock>();
|
|
||||||
}
|
|
||||||
blocks["norm_out"] = std::make_shared<LayerNorm>(dim, 1e-5f, true, true);
|
|
||||||
blocks["proj_out"] = std::make_shared<Linear>(dim,
|
|
||||||
3 * patch_size_t * patch_size * patch_size,
|
|
||||||
true,
|
|
||||||
true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_params(ggml_context* ctx,
|
|
||||||
const String2TensorStorage& tensor_storage_map = {},
|
|
||||||
const std::string prefix = "") override {
|
|
||||||
SD_UNUSED(tensor_storage_map);
|
|
||||||
SD_UNUSED(prefix);
|
|
||||||
params["register_tokens"] = ggml_new_tensor_2d(ctx,
|
|
||||||
GGML_TYPE_F32,
|
|
||||||
dim,
|
|
||||||
num_register_tokens);
|
|
||||||
params["mask_token"] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, dim);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* forward(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* z,
|
|
||||||
ggml_tensor* pe) {
|
|
||||||
int64_t width = z->ne[0];
|
|
||||||
int64_t height = z->ne[1];
|
|
||||||
int64_t num_frames = z->ne[2];
|
|
||||||
int64_t batch_size = z->ne[3] / 24;
|
|
||||||
GGML_ASSERT(batch_size == 1);
|
|
||||||
|
|
||||||
z = ggml_cont(ctx->ggml_ctx,
|
|
||||||
ggml_ext_torch_permute(ctx->ggml_ctx, z, 3, 0, 1, 2));
|
|
||||||
z = ggml_reshape_3d(ctx->ggml_ctx,
|
|
||||||
z,
|
|
||||||
24,
|
|
||||||
width * height * num_frames,
|
|
||||||
batch_size);
|
|
||||||
auto x_embedder = std::dynamic_pointer_cast<Linear>(blocks["x_embedder"]);
|
|
||||||
auto h = x_embedder->forward(ctx, z);
|
|
||||||
int64_t num_patches = h->ne[1];
|
|
||||||
h = ggml_concat(ctx->ggml_ctx, h, params["register_tokens"], 1);
|
|
||||||
auto zero = ggml_ext_scale(ctx->ggml_ctx,
|
|
||||||
ggml_ext_slice(ctx->ggml_ctx, h, 1, 0, 1),
|
|
||||||
0.f);
|
|
||||||
h = ggml_concat(ctx->ggml_ctx, h, zero, 1);
|
|
||||||
|
|
||||||
for (int i = 0; i < num_layers; ++i) {
|
|
||||||
auto block = std::dynamic_pointer_cast<DecoderBlock>(
|
|
||||||
blocks["transformer_blocks." + std::to_string(i)]);
|
|
||||||
h = block->forward(ctx, h, pe);
|
|
||||||
sd::ggml_graph_cut::mark_graph_cut(h,
|
|
||||||
"minimax_h3_vae.decoder.blocks." + std::to_string(i),
|
|
||||||
"hidden_states");
|
|
||||||
}
|
|
||||||
|
|
||||||
auto norm_out = std::dynamic_pointer_cast<LayerNorm>(blocks["norm_out"]);
|
|
||||||
auto proj_out = std::dynamic_pointer_cast<Linear>(blocks["proj_out"]);
|
|
||||||
h = proj_out->forward(ctx, norm_out->forward(ctx, h));
|
|
||||||
h = ggml_ext_slice(ctx->ggml_ctx, h, 1, 0, num_patches);
|
|
||||||
return DiT::unpatchify_3d(ctx->ggml_ctx,
|
|
||||||
h,
|
|
||||||
num_frames,
|
|
||||||
height,
|
|
||||||
width,
|
|
||||||
patch_size_t,
|
|
||||||
patch_size,
|
|
||||||
patch_size,
|
|
||||||
true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniMaxH3VideoVAE : public GGMLBlock {
|
|
||||||
MiniMaxH3VideoVAE() {
|
|
||||||
blocks["encoder"] = std::make_shared<Encoder>();
|
|
||||||
blocks["quant_conv"] = std::make_shared<Conv3d>(48,
|
|
||||||
48,
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
blocks["post_quant_conv"] = std::make_shared<Conv3d>(24,
|
|
||||||
24,
|
|
||||||
std::tuple{1, 1, 1});
|
|
||||||
blocks["decoder"] = std::make_shared<Decoder>();
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* encode(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* pixels,
|
|
||||||
ggml_tensor* pixel_mean,
|
|
||||||
ggml_tensor* pixel_std) {
|
|
||||||
pixels = ggml_div(ctx->ggml_ctx,
|
|
||||||
ggml_sub(ctx->ggml_ctx, pixels, pixel_mean),
|
|
||||||
pixel_std);
|
|
||||||
auto encoder = std::dynamic_pointer_cast<Encoder>(blocks["encoder"]);
|
|
||||||
auto quant = std::dynamic_pointer_cast<Conv3d>(blocks["quant_conv"]);
|
|
||||||
auto moments = quant->forward(ctx, encoder->forward(ctx, pixels));
|
|
||||||
return ggml_ext_slice(ctx->ggml_ctx, moments, 3, 0, 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_tensor* decode(GGMLRunnerContext* ctx,
|
|
||||||
ggml_tensor* latent,
|
|
||||||
ggml_tensor* pe,
|
|
||||||
ggml_tensor* pixel_mean,
|
|
||||||
ggml_tensor* pixel_std) {
|
|
||||||
auto post_quant = std::dynamic_pointer_cast<Conv3d>(blocks["post_quant_conv"]);
|
|
||||||
auto decoder = std::dynamic_pointer_cast<Decoder>(blocks["decoder"]);
|
|
||||||
auto pixels = decoder->forward(ctx, post_quant->forward(ctx, latent), pe);
|
|
||||||
pixels = ggml_add(ctx->ggml_ctx,
|
|
||||||
ggml_mul(ctx->ggml_ctx, pixels, pixel_std),
|
|
||||||
pixel_mean);
|
|
||||||
return ggml_clamp(ctx->ggml_ctx, pixels, 0.f, 1.f);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MiniMaxH3VideoVAERunner : public VAE {
|
|
||||||
MiniMaxH3VideoVAE model;
|
|
||||||
sd::Tensor<float> pixel_mean;
|
|
||||||
sd::Tensor<float> pixel_std;
|
|
||||||
sd::Tensor<float> latents_mean;
|
|
||||||
sd::Tensor<float> latents_std;
|
|
||||||
sd::Tensor<float> rope_cache;
|
|
||||||
|
|
||||||
MiniMaxH3VideoVAERunner(ggml_backend_t backend,
|
|
||||||
const String2TensorStorage& tensor_storage_map,
|
|
||||||
const std::string& prefix = "first_stage_model",
|
|
||||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
|
||||||
: VAE(VERSION_MINIMAX_H3, backend, prefix, weight_manager),
|
|
||||||
pixel_mean({1, 1, 1, 3}, {0.485f, 0.456f, 0.406f}),
|
|
||||||
pixel_std({1, 1, 1, 3}, {0.229f, 0.224f, 0.225f}),
|
|
||||||
latents_mean({1, 1, 1, 24},
|
|
||||||
{0.858090341091156f, -0.960659146308899f, 1.066164016723633f, -0.509032547473907f,
|
|
||||||
-0.272758185863495f, -1.367541432380676f, -0.255325496196747f, -0.269075542688370f,
|
|
||||||
-0.537684082984924f, -0.046409729868174f, 0.665737032890320f, 0.196901276707649f,
|
|
||||||
-0.546060800552368f, -0.403534203767776f, -0.236830249428749f, 0.259284526109695f,
|
|
||||||
-0.301339447498322f, 0.211341992020607f, -1.120684862136841f, 0.358193337917328f,
|
|
||||||
-0.042251437902451f, 0.260482996702194f, 0.228640928864479f, 0.705603182315826f}),
|
|
||||||
latents_std({1, 1, 1, 24},
|
|
||||||
{1.222377419471741f, 1.276726365089417f, 1.683177471160889f, 1.754945516586304f,
|
|
||||||
1.563621640205383f, 2.194143533706665f, 0.965313792228699f, 1.056988596916199f,
|
|
||||||
0.841948926448822f, 0.772995293140411f, 1.895593762397766f, 0.946841835975647f,
|
|
||||||
0.799680948257446f, 0.449889004230499f, 0.719739973545075f, 0.693629324436188f,
|
|
||||||
2.961095094680786f, 2.769419908523560f, 3.049618482589722f, 2.108805418014527f,
|
|
||||||
3.276226282119751f, 3.162735700607300f, 2.281681299209595f, 2.612784385681153f}) {
|
|
||||||
scale_input = false;
|
|
||||||
model.init(params_ctx, tensor_storage_map, prefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_desc() override {
|
|
||||||
return "minimax_h3_video_vae";
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_encoder_output_channels(int input_channels) override {
|
|
||||||
SD_UNUSED(input_channels);
|
|
||||||
return 24;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) override {
|
|
||||||
model.get_param_tensors(tensors, weight_prefix);
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> vae_output_to_latents(const sd::Tensor<float>& vae_output,
|
|
||||||
std::shared_ptr<RNG> rng) override {
|
|
||||||
SD_UNUSED(rng);
|
|
||||||
return vae_output;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> diffusion_to_vae_latents(const sd::Tensor<float>& latents) override {
|
|
||||||
return latents * latents_std + latents_mean;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> vae_to_diffusion_latents(const sd::Tensor<float>& latents) override {
|
|
||||||
return (latents - latents_mean) / latents_std;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> ensure_video_shape(const sd::Tensor<float>& tensor) {
|
|
||||||
if (tensor.dim() == 5) {
|
|
||||||
return tensor;
|
|
||||||
}
|
|
||||||
GGML_ASSERT(tensor.dim() == 4);
|
|
||||||
return tensor.reshape({tensor.shape()[0],
|
|
||||||
tensor.shape()[1],
|
|
||||||
1,
|
|
||||||
tensor.shape()[2],
|
|
||||||
tensor.shape()[3]});
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd_tiling_params_t h3_tiling(sd_tiling_params_t params) {
|
|
||||||
params.enabled = true;
|
|
||||||
params.tile_size_x = 16;
|
|
||||||
params.tile_size_y = 16;
|
|
||||||
params.target_overlap = 0.25f;
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> repeat_last_frame(const sd::Tensor<float>& input,
|
|
||||||
int64_t count) {
|
|
||||||
auto result = input;
|
|
||||||
auto last = sd::ops::slice(input, 2, input.shape()[2] - 1, input.shape()[2]);
|
|
||||||
for (int64_t i = 0; i < count; ++i) {
|
|
||||||
result = sd::ops::concat(result, last, 2);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> blend_temporal(const sd::Tensor<float>& previous,
|
|
||||||
const sd::Tensor<float>& current,
|
|
||||||
int64_t extent) {
|
|
||||||
auto output = current;
|
|
||||||
extent = std::min({extent, previous.shape()[2], current.shape()[2]});
|
|
||||||
int64_t previous_start = previous.shape()[2] - extent;
|
|
||||||
for (int64_t b = 0; b < current.shape()[4]; ++b) {
|
|
||||||
for (int64_t c = 0; c < current.shape()[3]; ++c) {
|
|
||||||
for (int64_t t = 0; t < extent; ++t) {
|
|
||||||
float wb = static_cast<float>(t) / extent;
|
|
||||||
float wa = 1.f - wb;
|
|
||||||
for (int64_t h = 0; h < current.shape()[1]; ++h) {
|
|
||||||
for (int64_t w = 0; w < current.shape()[0]; ++w) {
|
|
||||||
output.index(w, h, t, c, b) =
|
|
||||||
previous.index(w, h, previous_start + t, c, b) * wa +
|
|
||||||
current.index(w, h, t, c, b) * wb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> encode(int n_threads,
|
|
||||||
const sd::Tensor<float>& x,
|
|
||||||
sd_tiling_params_t tiling_params,
|
|
||||||
bool circular_x = false,
|
|
||||||
bool circular_y = false) override {
|
|
||||||
auto input = ensure_video_shape(x);
|
|
||||||
auto tiling = h3_tiling(tiling_params);
|
|
||||||
if (input.shape()[2] == 1) {
|
|
||||||
auto encoded = VAE::encode(n_threads, input, tiling, circular_x, circular_y);
|
|
||||||
if (!encoded.empty() && encoded.shape()[2] > 1) {
|
|
||||||
encoded = sd::ops::slice(encoded,
|
|
||||||
2,
|
|
||||||
encoded.shape()[2] - 1,
|
|
||||||
encoded.shape()[2]);
|
|
||||||
}
|
|
||||||
return encoded;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t pad = (-input.shape()[2]) % 17;
|
|
||||||
if (pad < 0) {
|
|
||||||
pad += 17;
|
|
||||||
}
|
|
||||||
if (pad > 0) {
|
|
||||||
input = repeat_last_frame(input, pad);
|
|
||||||
}
|
|
||||||
sd::Tensor<float> result;
|
|
||||||
for (int64_t start = 0; start < input.shape()[2]; start += 17) {
|
|
||||||
auto chunk = sd::ops::slice(input, 2, start, start + 17);
|
|
||||||
auto encoded = VAE::encode(n_threads, chunk, tiling, circular_x, circular_y);
|
|
||||||
if (encoded.empty()) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
result = result.empty() ? std::move(encoded)
|
|
||||||
: sd::ops::concat(result, encoded, 2);
|
|
||||||
}
|
|
||||||
if (result.shape()[2] > 3) {
|
|
||||||
result = sd::ops::slice(result, 2, 0, result.shape()[2] - 3);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> decode(int n_threads,
|
|
||||||
const sd::Tensor<float>& x,
|
|
||||||
sd_tiling_params_t tiling_params,
|
|
||||||
bool decode_video = false,
|
|
||||||
bool circular_x = false,
|
|
||||||
bool circular_y = false,
|
|
||||||
bool silent = false) override {
|
|
||||||
auto input = ensure_video_shape(x);
|
|
||||||
auto tiling = h3_tiling(tiling_params);
|
|
||||||
if (input.shape()[2] == 1) {
|
|
||||||
auto decoded = VAE::decode(n_threads,
|
|
||||||
input,
|
|
||||||
tiling,
|
|
||||||
decode_video,
|
|
||||||
circular_x,
|
|
||||||
circular_y,
|
|
||||||
silent);
|
|
||||||
if (!decoded.empty() && decoded.shape()[2] > 1) {
|
|
||||||
decoded = sd::ops::slice(decoded,
|
|
||||||
2,
|
|
||||||
decoded.shape()[2] - 1,
|
|
||||||
decoded.shape()[2]);
|
|
||||||
}
|
|
||||||
return decoded;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr int64_t tokens_per_chunk = 5;
|
|
||||||
constexpr int64_t token_drop = 3;
|
|
||||||
constexpr int64_t token_overlap = 2;
|
|
||||||
constexpr int64_t frames_per_chunk = 20;
|
|
||||||
constexpr int64_t frame_pre_padding = 3;
|
|
||||||
constexpr int64_t frame_overlap = 5;
|
|
||||||
|
|
||||||
int64_t pseudo_tokens = input.shape()[2] + token_drop;
|
|
||||||
int64_t pad_tokens = (tokens_per_chunk - pseudo_tokens % tokens_per_chunk) % tokens_per_chunk;
|
|
||||||
pseudo_tokens += pad_tokens;
|
|
||||||
int64_t num_chunks = pseudo_tokens / tokens_per_chunk - 1;
|
|
||||||
if (num_chunks < 1) {
|
|
||||||
pad_tokens += tokens_per_chunk;
|
|
||||||
num_chunks += 1;
|
|
||||||
}
|
|
||||||
if (pad_tokens > 0) {
|
|
||||||
input = repeat_last_frame(input, pad_tokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> result;
|
|
||||||
sd::Tensor<float> overlap;
|
|
||||||
for (int64_t i = 0; i < num_chunks; ++i) {
|
|
||||||
int64_t start = i * tokens_per_chunk;
|
|
||||||
int64_t end = std::min(start + tokens_per_chunk + token_overlap,
|
|
||||||
input.shape()[2]);
|
|
||||||
auto chunk = sd::ops::slice(input, 2, start, end);
|
|
||||||
auto decoded = VAE::decode(n_threads,
|
|
||||||
chunk,
|
|
||||||
tiling,
|
|
||||||
true,
|
|
||||||
circular_x,
|
|
||||||
circular_y,
|
|
||||||
silent);
|
|
||||||
if (decoded.empty()) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t first_end = std::min<int64_t>(frames_per_chunk, decoded.shape()[2]);
|
|
||||||
auto first = sd::ops::slice(decoded,
|
|
||||||
2,
|
|
||||||
std::min<int64_t>(frame_pre_padding, first_end),
|
|
||||||
first_end);
|
|
||||||
if (!overlap.empty()) {
|
|
||||||
first = blend_temporal(overlap, first, frame_overlap);
|
|
||||||
overlap = {};
|
|
||||||
}
|
|
||||||
result = result.empty() ? std::move(first)
|
|
||||||
: sd::ops::concat(result, first, 2);
|
|
||||||
|
|
||||||
if (decoded.shape()[2] > frames_per_chunk + frame_pre_padding) {
|
|
||||||
overlap = sd::ops::slice(decoded,
|
|
||||||
2,
|
|
||||||
frames_per_chunk + frame_pre_padding,
|
|
||||||
decoded.shape()[2]);
|
|
||||||
}
|
|
||||||
if (i == num_chunks - 1 && !overlap.empty()) {
|
|
||||||
result = sd::ops::concat(result, overlap, 2);
|
|
||||||
overlap = {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t expected_frames = input.shape()[2] <= 1 ? 1 : ((x.shape()[2] - 2) / 5) * 17 + 5;
|
|
||||||
expected_frames = std::max<int64_t>(1, expected_frames);
|
|
||||||
if (result.shape()[2] > expected_frames) {
|
|
||||||
result = sd::ops::slice(result, 2, 0, expected_frames);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> build_rope(int64_t width,
|
|
||||||
int64_t height,
|
|
||||||
int64_t num_frames) {
|
|
||||||
std::vector<std::vector<float>> ids;
|
|
||||||
ids.reserve(static_cast<size_t>(width * height * num_frames + 5));
|
|
||||||
constexpr float two_pi = 6.28318530717958647692f;
|
|
||||||
for (int64_t t = 0; t < num_frames; ++t) {
|
|
||||||
float pt = (2.f * ((t + 0.5f) / num_frames) - 1.f) * two_pi;
|
|
||||||
for (int64_t h = 0; h < height; ++h) {
|
|
||||||
float ph = (2.f * ((h + 0.5f) / height) - 1.f) * two_pi;
|
|
||||||
for (int64_t w = 0; w < width; ++w) {
|
|
||||||
float pw = (2.f * ((w + 0.5f) / width) - 1.f) * two_pi;
|
|
||||||
ids.push_back({pt, ph, pw});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 5; ++i) {
|
|
||||||
ids.push_back({0.f, 0.f, 0.f});
|
|
||||||
}
|
|
||||||
auto values = Rope::embed_nd(ids,
|
|
||||||
1,
|
|
||||||
100.f,
|
|
||||||
std::vector<int>{16, 16, 16});
|
|
||||||
return sd::Tensor<float>({2,
|
|
||||||
2,
|
|
||||||
24,
|
|
||||||
static_cast<int64_t>(ids.size())},
|
|
||||||
std::move(values));
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> _compute(const int n_threads,
|
|
||||||
const sd::Tensor<float>& z,
|
|
||||||
bool decode_graph) override {
|
|
||||||
auto input = ensure_video_shape(z);
|
|
||||||
if (decode_graph) {
|
|
||||||
rope_cache = build_rope(input.shape()[0],
|
|
||||||
input.shape()[1],
|
|
||||||
input.shape()[2]);
|
|
||||||
}
|
|
||||||
auto get_graph = [&]() -> ggml_cgraph* {
|
|
||||||
auto value = make_input(input);
|
|
||||||
auto mean = make_input(pixel_mean);
|
|
||||||
auto std = make_input(pixel_std);
|
|
||||||
auto runner_ctx = get_context();
|
|
||||||
ggml_tensor* out = nullptr;
|
|
||||||
if (decode_graph) {
|
|
||||||
auto pe = make_input(rope_cache);
|
|
||||||
out = model.decode(&runner_ctx, value, pe, mean, std);
|
|
||||||
} else {
|
|
||||||
out = model.encode(&runner_ctx, value, mean, std);
|
|
||||||
}
|
|
||||||
auto graph = new_graph_custom(H3_VIDEO_VAE_GRAPH_SIZE);
|
|
||||||
ggml_build_forward_expand(graph, out);
|
|
||||||
return graph;
|
|
||||||
};
|
|
||||||
return restore_trailing_singleton_dims(
|
|
||||||
GGMLRunner::compute<float>(get_graph,
|
|
||||||
n_threads,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false),
|
|
||||||
5);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace MiniMaxH3VAE
|
|
||||||
|
|
||||||
#endif // __SD_MODEL_VAE_MINIMAX_H3_VAE_HPP__
|
|
||||||
@ -74,7 +74,7 @@ public:
|
|||||||
int scale_factor = 8;
|
int scale_factor = 8;
|
||||||
if (version == VERSION_LTXAV) {
|
if (version == VERSION_LTXAV) {
|
||||||
scale_factor = 32;
|
scale_factor = 32;
|
||||||
} else if (version == VERSION_WAN2_2_TI2V || sd_version_is_hunyuan_video(version) || sd_version_is_mage_flow(version) || sd_version_is_minimax_h3(version)) {
|
} else if (version == VERSION_WAN2_2_TI2V || sd_version_is_hunyuan_video(version) || sd_version_is_mage_flow(version)) {
|
||||||
scale_factor = 16;
|
scale_factor = 16;
|
||||||
} else if (sd_version_uses_flux2_vae(version)) {
|
} else if (sd_version_uses_flux2_vae(version)) {
|
||||||
scale_factor = 16;
|
scale_factor = 16;
|
||||||
@ -115,7 +115,7 @@ public:
|
|||||||
tile_size_y = get_tile_size(params.tile_size_y, params.rel_size_y, latent_y);
|
tile_size_y = get_tile_size(params.tile_size_y, params.rel_size_y, latent_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual sd::Tensor<float> encode(int n_threads,
|
sd::Tensor<float> encode(int n_threads,
|
||||||
const sd::Tensor<float>& x,
|
const sd::Tensor<float>& x,
|
||||||
sd_tiling_params_t tiling_params,
|
sd_tiling_params_t tiling_params,
|
||||||
bool circular_x = false,
|
bool circular_x = false,
|
||||||
@ -136,8 +136,7 @@ public:
|
|||||||
// Image VAE encode is more sensitive to tile boundary context than decode.
|
// Image VAE encode is more sensitive to tile boundary context than decode.
|
||||||
// Keep the smaller legacy factor for video VAEs, but default image encode
|
// Keep the smaller legacy factor for video VAEs, but default image encode
|
||||||
// tiles to 64 latent pixels so a 512px SD image is encoded as one tile.
|
// tiles to 64 latent pixels so a 512px SD image is encoded as one tile.
|
||||||
const float encode_tile_factor = sd_version_is_minimax_h3(version) ? 1.f : (sd_version_is_wan(version) || sd_version_is_hunyuan_video(version) || sd_version_is_ltxav(version)) ? 1.30539f
|
const float encode_tile_factor = (sd_version_is_wan(version) || sd_version_is_hunyuan_video(version) || sd_version_is_ltxav(version)) ? 1.30539f : 2.0f;
|
||||||
: 2.0f;
|
|
||||||
get_tile_sizes(tile_size_x, tile_size_y, tile_overlap, tiling_params, W, H, encode_tile_factor);
|
get_tile_sizes(tile_size_x, tile_size_y, tile_overlap, tiling_params, W, H, encode_tile_factor);
|
||||||
LOG_DEBUG("VAE Tile size: %dx%d", tile_size_x, tile_size_y);
|
LOG_DEBUG("VAE Tile size: %dx%d", tile_size_x, tile_size_y);
|
||||||
output = tiled_compute(input,
|
output = tiled_compute(input,
|
||||||
@ -167,7 +166,7 @@ public:
|
|||||||
return std::move(output);
|
return std::move(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual sd::Tensor<float> decode(int n_threads,
|
sd::Tensor<float> decode(int n_threads,
|
||||||
const sd::Tensor<float>& x,
|
const sd::Tensor<float>& x,
|
||||||
sd_tiling_params_t tiling_params,
|
sd_tiling_params_t tiling_params,
|
||||||
bool decode_video = false,
|
bool decode_video = false,
|
||||||
|
|||||||
@ -537,10 +537,6 @@ SDVersion ModelLoader::get_sd_version() {
|
|||||||
if (tensor_storage.name.find("model.diffusion_model.adaln_single.emb.timestep_embedder.linear_1.bias") != std::string::npos) {
|
if (tensor_storage.name.find("model.diffusion_model.adaln_single.emb.timestep_embedder.linear_1.bias") != std::string::npos) {
|
||||||
return VERSION_LTXAV;
|
return VERSION_LTXAV;
|
||||||
}
|
}
|
||||||
if (tensor_storage.name.find("model.diffusion_model.video_patch_proj.weight") != std::string::npos &&
|
|
||||||
tensor_storage_map.find("model.diffusion_model.audio_patch_proj.weight") != tensor_storage_map.end()) {
|
|
||||||
return VERSION_MINIMAX_H3;
|
|
||||||
}
|
|
||||||
if (tensor_storage.name.find("model.diffusion_model.blocks.0.cross_attn.norm_k.weight") != std::string::npos) {
|
if (tensor_storage.name.find("model.diffusion_model.blocks.0.cross_attn.norm_k.weight") != std::string::npos) {
|
||||||
is_wan = true;
|
is_wan = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1048,7 +1048,7 @@ std::string convert_diffusers_to_original_wan_vae(std::string name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string convert_first_stage_model_name(std::string name, std::string prefix, SDVersion version) {
|
std::string convert_first_stage_model_name(std::string name, std::string prefix, SDVersion version) {
|
||||||
if (sd_version_is_hunyuan_video(version) || sd_version_is_mage_flow(version) || sd_version_is_minimax_h3(version)) {
|
if (sd_version_is_hunyuan_video(version) || sd_version_is_mage_flow(version)) {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
if (sd_version_uses_wan_vae(version)) {
|
if (sd_version_uses_wan_vae(version)) {
|
||||||
@ -1468,7 +1468,7 @@ std::string convert_tensor_name(std::string name, SDVersion version) {
|
|||||||
|
|
||||||
replace_with_prefix_map(name, prefix_map);
|
replace_with_prefix_map(name, prefix_map);
|
||||||
|
|
||||||
if (sd_version_is_boogu_image(version) || sd_version_is_krea2(version) || sd_version_is_mage_flow(version) || sd_version_is_minimax_h3(version)) {
|
if (sd_version_is_boogu_image(version) || sd_version_is_krea2(version) || sd_version_is_mage_flow(version)) {
|
||||||
const std::string hf_vision_prefix = "text_encoders.llm.model.visual.";
|
const std::string hf_vision_prefix = "text_encoders.llm.model.visual.";
|
||||||
if (starts_with(name, hf_vision_prefix)) {
|
if (starts_with(name, hf_vision_prefix)) {
|
||||||
name = "text_encoders.llm.visual." + name.substr(hf_vision_prefix.size());
|
name = "text_encoders.llm.visual." + name.substr(hf_vision_prefix.size());
|
||||||
|
|||||||
@ -38,7 +38,6 @@
|
|||||||
#include "model/diffusion/lingbot_video.hpp"
|
#include "model/diffusion/lingbot_video.hpp"
|
||||||
#include "model/diffusion/ltxv.hpp"
|
#include "model/diffusion/ltxv.hpp"
|
||||||
#include "model/diffusion/mage_flow.hpp"
|
#include "model/diffusion/mage_flow.hpp"
|
||||||
#include "model/diffusion/minimax_h3.hpp"
|
|
||||||
#include "model/diffusion/minit2i.hpp"
|
#include "model/diffusion/minit2i.hpp"
|
||||||
#include "model/diffusion/mmdit.hpp"
|
#include "model/diffusion/mmdit.hpp"
|
||||||
#include "model/diffusion/model.hpp"
|
#include "model/diffusion/model.hpp"
|
||||||
@ -54,8 +53,6 @@
|
|||||||
#include "model/vae/ltx_audio_vae.hpp"
|
#include "model/vae/ltx_audio_vae.hpp"
|
||||||
#include "model/vae/ltx_vae.hpp"
|
#include "model/vae/ltx_vae.hpp"
|
||||||
#include "model/vae/mage_vae.hpp"
|
#include "model/vae/mage_vae.hpp"
|
||||||
#include "model/vae/minimax_h3_audio_vae.hpp"
|
|
||||||
#include "model/vae/minimax_h3_vae.hpp"
|
|
||||||
#include "model/vae/tae.hpp"
|
#include "model/vae/tae.hpp"
|
||||||
#include "model/vae/vae.hpp"
|
#include "model/vae/vae.hpp"
|
||||||
#include "model/vae/wan_vae.hpp"
|
#include "model/vae/wan_vae.hpp"
|
||||||
@ -109,7 +106,6 @@ const char* model_version_to_str[] = {
|
|||||||
"Flux.2",
|
"Flux.2",
|
||||||
"Flux.2 klein",
|
"Flux.2 klein",
|
||||||
"LTXAV",
|
"LTXAV",
|
||||||
"MiniMax-H3",
|
|
||||||
"HiDream O1",
|
"HiDream O1",
|
||||||
"Z-Image",
|
"Z-Image",
|
||||||
"Boogu Image",
|
"Boogu Image",
|
||||||
@ -220,7 +216,6 @@ public:
|
|||||||
std::shared_ptr<RNG> sampler_rng = nullptr;
|
std::shared_ptr<RNG> sampler_rng = nullptr;
|
||||||
int n_threads = -1;
|
int n_threads = -1;
|
||||||
float default_flow_shift = INFINITY;
|
float default_flow_shift = INFINITY;
|
||||||
float active_flow_shift = INFINITY;
|
|
||||||
|
|
||||||
std::shared_ptr<Conditioner> cond_stage_model;
|
std::shared_ptr<Conditioner> cond_stage_model;
|
||||||
std::shared_ptr<FrozenCLIPVisionEmbedder> clip_vision; // for svd or wan2.1 i2v
|
std::shared_ptr<FrozenCLIPVisionEmbedder> clip_vision; // for svd or wan2.1 i2v
|
||||||
@ -228,7 +223,7 @@ public:
|
|||||||
std::shared_ptr<DiffusionModelRunner> high_noise_diffusion_model;
|
std::shared_ptr<DiffusionModelRunner> high_noise_diffusion_model;
|
||||||
std::shared_ptr<VAE> first_stage_model;
|
std::shared_ptr<VAE> first_stage_model;
|
||||||
std::shared_ptr<VAE> preview_vae;
|
std::shared_ptr<VAE> preview_vae;
|
||||||
std::shared_ptr<AudioVAERunner> audio_vae_model;
|
std::shared_ptr<LTXV::LTXAudioVAERunner> audio_vae_model;
|
||||||
std::shared_ptr<ControlNet> control_net;
|
std::shared_ptr<ControlNet> control_net;
|
||||||
std::shared_ptr<IPAdapter::IPAdapterRunner> ip_adapter;
|
std::shared_ptr<IPAdapter::IPAdapterRunner> ip_adapter;
|
||||||
sd::Tensor<float> ip_adapter_tokens;
|
sd::Tensor<float> ip_adapter_tokens;
|
||||||
@ -809,9 +804,9 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(SAFE_STR(sd_ctx_params->audio_vae_path)) > 0) {
|
if (strlen(SAFE_STR(sd_ctx_params->audio_vae_path)) > 0) {
|
||||||
LOG_INFO("loading audio VAE from '%s'", sd_ctx_params->audio_vae_path);
|
LOG_INFO("loading LTX audio VAE from '%s'", sd_ctx_params->audio_vae_path);
|
||||||
if (!model_loader.init_from_file(sd_ctx_params->audio_vae_path)) {
|
if (!model_loader.init_from_file(sd_ctx_params->audio_vae_path)) {
|
||||||
LOG_WARN("loading audio VAE weights from '%s' failed", sd_ctx_params->audio_vae_path);
|
LOG_WARN("loading LTX audio VAE weights from '%s' failed", sd_ctx_params->audio_vae_path);
|
||||||
} else {
|
} else {
|
||||||
use_audio_vae = true;
|
use_audio_vae = true;
|
||||||
}
|
}
|
||||||
@ -1008,11 +1003,6 @@ public:
|
|||||||
tae_preview_only = false;
|
tae_preview_only = false;
|
||||||
use_tae = true;
|
use_tae = true;
|
||||||
}
|
}
|
||||||
if (sd_version_is_minimax_h3(version) && use_tae) {
|
|
||||||
LOG_WARN("MiniMax-H3 does not have a compatible TAE; ignoring --taesd");
|
|
||||||
tae_preview_only = false;
|
|
||||||
use_tae = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& tensor_storage_map = model_loader.get_tensor_storage_map();
|
auto& tensor_storage_map = model_loader.get_tensor_storage_map();
|
||||||
|
|
||||||
@ -1121,17 +1111,6 @@ public:
|
|||||||
tensor_storage_map,
|
tensor_storage_map,
|
||||||
"model.diffusion_model",
|
"model.diffusion_model",
|
||||||
model_manager);
|
model_manager);
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
cond_stage_model = std::make_shared<LLMEmbedder>(backend_for(SDBackendModule::TE),
|
|
||||||
tensor_storage_map,
|
|
||||||
version,
|
|
||||||
"",
|
|
||||||
true,
|
|
||||||
model_manager);
|
|
||||||
diffusion_model = std::make_shared<MiniMaxH3::MiniMaxH3Runner>(backend_for(SDBackendModule::DIFFUSION),
|
|
||||||
tensor_storage_map,
|
|
||||||
"model.diffusion_model",
|
|
||||||
model_manager);
|
|
||||||
} else if (sd_version_is_hunyuan_video(version)) {
|
} else if (sd_version_is_hunyuan_video(version)) {
|
||||||
cond_stage_model = std::make_shared<LLMEmbedder>(backend_for(SDBackendModule::TE),
|
cond_stage_model = std::make_shared<LLMEmbedder>(backend_for(SDBackendModule::TE),
|
||||||
tensor_storage_map,
|
tensor_storage_map,
|
||||||
@ -1424,11 +1403,6 @@ public:
|
|||||||
false,
|
false,
|
||||||
version,
|
version,
|
||||||
model_manager);
|
model_manager);
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
return std::make_shared<MiniMaxH3VAE::MiniMaxH3VideoVAERunner>(backend_for(SDBackendModule::VAE),
|
|
||||||
tensor_storage_map,
|
|
||||||
"first_stage_model",
|
|
||||||
model_manager);
|
|
||||||
} else if (sd_version_is_mage_flow(vae_version)) {
|
} else if (sd_version_is_mage_flow(vae_version)) {
|
||||||
return std::make_shared<MageVAE::MageVAERunner>(backend_for(SDBackendModule::VAE),
|
return std::make_shared<MageVAE::MageVAERunner>(backend_for(SDBackendModule::VAE),
|
||||||
tensor_storage_map,
|
tensor_storage_map,
|
||||||
@ -1514,18 +1488,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (use_audio_vae) {
|
if (use_audio_vae) {
|
||||||
if (sd_version_is_minimax_h3(version)) {
|
|
||||||
audio_vae_model = std::make_shared<MiniMaxH3::AudioVAERunner>(backend_for(SDBackendModule::VAE),
|
|
||||||
tensor_storage_map,
|
|
||||||
"",
|
|
||||||
model_manager);
|
|
||||||
} else {
|
|
||||||
audio_vae_model = std::make_shared<LTXV::LTXAudioVAERunner>(backend_for(SDBackendModule::VAE),
|
audio_vae_model = std::make_shared<LTXV::LTXAudioVAERunner>(backend_for(SDBackendModule::VAE),
|
||||||
tensor_storage_map,
|
tensor_storage_map,
|
||||||
"",
|
"",
|
||||||
model_manager);
|
model_manager);
|
||||||
}
|
if (!register_runner_params("LTX audio VAE",
|
||||||
if (!register_runner_params("audio VAE",
|
|
||||||
audio_vae_model,
|
audio_vae_model,
|
||||||
SDBackendModule::VAE,
|
SDBackendModule::VAE,
|
||||||
&vae_params_mem_size)) {
|
&vae_params_mem_size)) {
|
||||||
@ -1638,10 +1605,8 @@ public:
|
|||||||
ignore_tensors.insert("model.diffusion_model.__index_timestep_zero__");
|
ignore_tensors.insert("model.diffusion_model.__index_timestep_zero__");
|
||||||
|
|
||||||
if (audio_vae_model) {
|
if (audio_vae_model) {
|
||||||
if (!sd_version_is_minimax_h3(version)) {
|
|
||||||
ignore_tensors.insert("audio_vae.encoder");
|
ignore_tensors.insert("audio_vae.encoder");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (version == VERSION_OVIS_IMAGE) {
|
if (version == VERSION_OVIS_IMAGE) {
|
||||||
ignore_tensors.insert("text_encoders.llm.vision_model.");
|
ignore_tensors.insert("text_encoders.llm.vision_model.");
|
||||||
ignore_tensors.insert("text_encoders.llm.visual_tokenizer.");
|
ignore_tensors.insert("text_encoders.llm.visual_tokenizer.");
|
||||||
@ -1763,7 +1728,6 @@ public:
|
|||||||
sd_version_is_wan(version) ||
|
sd_version_is_wan(version) ||
|
||||||
sd_version_is_hunyuan_video(version) ||
|
sd_version_is_hunyuan_video(version) ||
|
||||||
sd_version_is_lingbot_video(version) ||
|
sd_version_is_lingbot_video(version) ||
|
||||||
sd_version_is_minimax_h3(version) ||
|
|
||||||
sd_version_is_qwen_image(version) ||
|
sd_version_is_qwen_image(version) ||
|
||||||
sd_version_is_mage_flow(version) ||
|
sd_version_is_mage_flow(version) ||
|
||||||
version == VERSION_HIDREAM_O1 ||
|
version == VERSION_HIDREAM_O1 ||
|
||||||
@ -1778,8 +1742,6 @@ public:
|
|||||||
default_flow_shift = 5.f;
|
default_flow_shift = 5.f;
|
||||||
} else if (sd_version_is_hunyuan_video(version)) {
|
} else if (sd_version_is_hunyuan_video(version)) {
|
||||||
default_flow_shift = 7.f;
|
default_flow_shift = 7.f;
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
default_flow_shift = 12.f;
|
|
||||||
} else if (sd_version_is_ernie_image(version)) {
|
} else if (sd_version_is_ernie_image(version)) {
|
||||||
default_flow_shift = 4.f;
|
default_flow_shift = 4.f;
|
||||||
} else if (sd_version_is_pid(version)) {
|
} else if (sd_version_is_pid(version)) {
|
||||||
@ -2727,15 +2689,6 @@ public:
|
|||||||
condition.c_token_types.empty() ? nullptr : &condition.c_token_types,
|
condition.c_token_types.empty() ? nullptr : &condition.c_token_types,
|
||||||
condition.c_vinput_mask.empty() ? nullptr : &condition.c_vinput_mask,
|
condition.c_vinput_mask.empty() ? nullptr : &condition.c_vinput_mask,
|
||||||
condition.c_image_embeds.empty() ? nullptr : &condition.c_image_embeds};
|
condition.c_image_embeds.empty() ? nullptr : &condition.c_image_embeds};
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
diffusion_params.extra = MiniMaxH3DiffusionExtra{
|
|
||||||
condition.c_token_types.empty() ? nullptr : &condition.c_token_types,
|
|
||||||
condition.c_position_ids.empty() ? nullptr : &condition.c_position_ids,
|
|
||||||
condition.c_ref_audios.empty() ? nullptr : &condition.c_ref_audios,
|
|
||||||
condition.c_reference_blocks.empty() ? nullptr : &condition.c_reference_blocks,
|
|
||||||
audio_length,
|
|
||||||
std::isfinite(active_flow_shift) ? active_flow_shift : 12.f,
|
|
||||||
3.f};
|
|
||||||
} else if (sd_version_is_ltxav(version)) {
|
} else if (sd_version_is_ltxav(version)) {
|
||||||
diffusion_params.extra = LTXAVDiffusionExtra{
|
diffusion_params.extra = LTXAVDiffusionExtra{
|
||||||
nullptr,
|
nullptr,
|
||||||
@ -2909,7 +2862,7 @@ public:
|
|||||||
int get_diffusion_model_down_factor() {
|
int get_diffusion_model_down_factor() {
|
||||||
int down_factor = 8; // unet
|
int down_factor = 8; // unet
|
||||||
if (sd_version_is_dit(version)) {
|
if (sd_version_is_dit(version)) {
|
||||||
if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version) || sd_version_is_minimax_h3(version)) {
|
if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version)) {
|
||||||
down_factor = 2;
|
down_factor = 2;
|
||||||
} else {
|
} else {
|
||||||
down_factor = 1;
|
down_factor = 1;
|
||||||
@ -2923,8 +2876,6 @@ public:
|
|||||||
if (sd_version_is_dit(version)) {
|
if (sd_version_is_dit(version)) {
|
||||||
if (sd_version_is_ltxav(version)) {
|
if (sd_version_is_ltxav(version)) {
|
||||||
latent_channel = 128;
|
latent_channel = 128;
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
latent_channel = 24;
|
|
||||||
} else if (version == VERSION_WAN2_2_TI2V) {
|
} else if (version == VERSION_WAN2_2_TI2V) {
|
||||||
latent_channel = 48;
|
latent_channel = 48;
|
||||||
} else if (sd_version_is_hunyuan_video(version)) {
|
} else if (sd_version_is_hunyuan_video(version)) {
|
||||||
@ -2978,8 +2929,6 @@ public:
|
|||||||
int latent_frames = frames;
|
int latent_frames = frames;
|
||||||
if (sd_version_is_ltxav(version)) {
|
if (sd_version_is_ltxav(version)) {
|
||||||
latent_frames = ((frames - 1) / 8) + 1;
|
latent_frames = ((frames - 1) / 8) + 1;
|
||||||
} else if (sd_version_is_minimax_h3(version)) {
|
|
||||||
latent_frames = frames <= 5 ? 2 : ((frames - 5) / 17) * 5 + 2;
|
|
||||||
} else if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version) || sd_version_is_hunyuan_video(version)) {
|
} else if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version) || sd_version_is_hunyuan_video(version)) {
|
||||||
latent_frames = ((frames - 1) / 4) + 1;
|
latent_frames = ((frames - 1) / 4) + 1;
|
||||||
}
|
}
|
||||||
@ -2993,9 +2942,6 @@ public:
|
|||||||
if (sd_version_is_ltxav(version)) {
|
if (sd_version_is_ltxav(version)) {
|
||||||
return (latent_frames - 1) * 8 + 1;
|
return (latent_frames - 1) * 8 + 1;
|
||||||
}
|
}
|
||||||
if (sd_version_is_minimax_h3(version)) {
|
|
||||||
return latent_frames <= 2 ? 5 : ((latent_frames - 2) / 5) * 17 + 5;
|
|
||||||
}
|
|
||||||
if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version) || sd_version_is_hunyuan_video(version)) {
|
if (sd_version_is_wan(version) || sd_version_is_lingbot_video(version) || sd_version_is_hunyuan_video(version)) {
|
||||||
return (latent_frames - 1) * 4 + 1;
|
return (latent_frames - 1) * 4 + 1;
|
||||||
}
|
}
|
||||||
@ -3003,13 +2949,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int align_video_frames(int frames) {
|
int align_video_frames(int frames) {
|
||||||
if (sd_version_is_minimax_h3(version)) {
|
|
||||||
frames = std::max(frames, 5);
|
|
||||||
while (frames % 17 != 5) {
|
|
||||||
++frames;
|
|
||||||
}
|
|
||||||
return frames;
|
|
||||||
}
|
|
||||||
return latent_frames_to_video_frames(video_frames_to_latent_frames(frames));
|
return latent_frames_to_video_frames(video_frames_to_latent_frames(frames));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3084,7 +3023,6 @@ public:
|
|||||||
flow_shift = default_flow_shift;
|
flow_shift = default_flow_shift;
|
||||||
}
|
}
|
||||||
flow_denoiser->set_shift(flow_shift);
|
flow_denoiser->set_shift(flow_shift);
|
||||||
active_flow_shift = flow_shift;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3805,7 +3743,7 @@ struct sd_ctx_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static bool sd_version_supports_video_generation(SDVersion version) {
|
static bool sd_version_supports_video_generation(SDVersion version) {
|
||||||
return version == VERSION_SVD || sd_version_is_wan(version) || sd_version_is_hunyuan_video(version) || sd_version_is_lingbot_video(version) || sd_version_is_ltxav(version) || sd_version_is_minimax_h3(version);
|
return version == VERSION_SVD || sd_version_is_wan(version) || sd_version_is_hunyuan_video(version) || sd_version_is_lingbot_video(version) || sd_version_is_ltxav(version);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool sd_version_supports_image_generation(SDVersion version) {
|
static bool sd_version_supports_image_generation(SDVersion version) {
|
||||||
@ -3867,7 +3805,7 @@ static sd_audio_t* waveform_to_sd_audio(const StableDiffusionGGML* sd,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
audio->sample_rate = static_cast<uint32_t>(sd->audio_vae_model != nullptr ? sd->audio_vae_model->output_sample_rate() : 0);
|
audio->sample_rate = static_cast<uint32_t>(sd->audio_vae_model != nullptr ? sd->audio_vae_model->config.output_sample_rate() : 0);
|
||||||
audio->channels = static_cast<uint32_t>(channels);
|
audio->channels = static_cast<uint32_t>(channels);
|
||||||
audio->sample_count = static_cast<uint64_t>(sample_count);
|
audio->sample_count = static_cast<uint64_t>(sample_count);
|
||||||
size_t sample_bytes = waveform.numel() * sizeof(float);
|
size_t sample_bytes = waveform.numel() * sizeof(float);
|
||||||
@ -4080,10 +4018,6 @@ struct GenerationRequest {
|
|||||||
frames = sd_ctx->sd->align_video_frames(requested_frames);
|
frames = sd_ctx->sd->align_video_frames(requested_frames);
|
||||||
clip_skip = sd_vid_gen_params->clip_skip;
|
clip_skip = sd_vid_gen_params->clip_skip;
|
||||||
fps = std::max(1, sd_vid_gen_params->fps);
|
fps = std::max(1, sd_vid_gen_params->fps);
|
||||||
if (sd_version_is_minimax_h3(sd_ctx->sd->version) && fps != 24) {
|
|
||||||
LOG_WARN("MiniMax-H3 uses 24 fps; overriding requested fps %d", fps);
|
|
||||||
fps = 24;
|
|
||||||
}
|
|
||||||
vae_scale_factor = sd_ctx->sd->get_vae_scale_factor();
|
vae_scale_factor = sd_ctx->sd->get_vae_scale_factor();
|
||||||
diffusion_model_down_factor = sd_ctx->sd->get_diffusion_model_down_factor();
|
diffusion_model_down_factor = sd_ctx->sd->get_diffusion_model_down_factor();
|
||||||
seed = sd_vid_gen_params->seed;
|
seed = sd_vid_gen_params->seed;
|
||||||
@ -4323,8 +4257,6 @@ struct SamplePlan {
|
|||||||
if (sd_version_is_ltxav(sd_ctx->sd->version) && request->frames > 0) {
|
if (sd_version_is_ltxav(sd_ctx->sd->version) && request->frames > 0) {
|
||||||
int latent_frames = ((request->frames - 1) / 8) + 1;
|
int latent_frames = ((request->frames - 1) / 8) + 1;
|
||||||
sample_seq_len *= latent_frames;
|
sample_seq_len *= latent_frames;
|
||||||
} else if (sd_version_is_minimax_h3(sd_ctx->sd->version) && request->frames > 0) {
|
|
||||||
sample_seq_len *= sd_ctx->sd->video_frames_to_latent_frames(request->frames);
|
|
||||||
}
|
}
|
||||||
sigmas = sd_ctx->sd->denoiser->get_sigmas(total_steps,
|
sigmas = sd_ctx->sd->denoiser->get_sigmas(total_steps,
|
||||||
sample_seq_len,
|
sample_seq_len,
|
||||||
@ -4364,10 +4296,6 @@ struct ImageGenerationLatents {
|
|||||||
sd::Tensor<float> control_image;
|
sd::Tensor<float> control_image;
|
||||||
std::vector<sd::Tensor<float>> ref_images;
|
std::vector<sd::Tensor<float>> ref_images;
|
||||||
std::vector<sd::Tensor<float>> ref_latents;
|
std::vector<sd::Tensor<float>> ref_latents;
|
||||||
std::vector<sd::Tensor<float>> reference_audio_latents;
|
|
||||||
std::vector<MiniMaxH3ReferenceBlock> minimax_reference_blocks;
|
|
||||||
std::vector<MiniMaxH3PresentationItem> minimax_presentation_refs;
|
|
||||||
std::vector<int32_t> keyframe_indices;
|
|
||||||
sd::Tensor<float> denoise_mask;
|
sd::Tensor<float> denoise_mask;
|
||||||
sd::Tensor<float> clip_vision_output;
|
sd::Tensor<float> clip_vision_output;
|
||||||
sd::Tensor<float> vace_context;
|
sd::Tensor<float> vace_context;
|
||||||
@ -4682,67 +4610,6 @@ static int get_ltxav_num_audio_latents(int frames, int fps) {
|
|||||||
return static_cast<int>(std::ceil((static_cast<float>(frames) / static_cast<float>(fps)) * kLatentsPerSecond));
|
return static_cast<int>(std::ceil((static_cast<float>(frames) / static_cast<float>(fps)) * kLatentsPerSecond));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_minimax_h3_num_audio_latents(int frames, int fps) {
|
|
||||||
GGML_ASSERT(frames > 0 && fps > 0);
|
|
||||||
return std::max(1,
|
|
||||||
static_cast<int>(std::lround(
|
|
||||||
static_cast<double>(frames) * 40.0 / fps)));
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> make_minimax_h3_empty_audio_latent(int audio_length) {
|
|
||||||
if (audio_length <= 0) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
return sd::zeros<float>({audio_length, 2, 32, 1});
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> prepare_minimax_h3_reference_waveform(const sd_audio_t& audio,
|
|
||||||
int target_sample_rate = 32000) {
|
|
||||||
if (audio.data == nullptr || audio.sample_count == 0 || audio.channels == 0 || audio.sample_rate == 0) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
uint64_t output_samples = static_cast<uint64_t>(std::llround(
|
|
||||||
static_cast<long double>(audio.sample_count) * target_sample_rate / audio.sample_rate));
|
|
||||||
output_samples = std::max<uint64_t>(1, output_samples);
|
|
||||||
uint64_t padded_samples = (output_samples + 799) / 800 * 800;
|
|
||||||
sd::Tensor<float> waveform({static_cast<int64_t>(padded_samples), 2, 1, 1});
|
|
||||||
|
|
||||||
for (uint64_t i = 0; i < output_samples; ++i) {
|
|
||||||
long double source_pos = static_cast<long double>(i) * audio.sample_rate / target_sample_rate;
|
|
||||||
uint64_t source0 = std::min<uint64_t>(static_cast<uint64_t>(source_pos), audio.sample_count - 1);
|
|
||||||
uint64_t source1 = std::min<uint64_t>(source0 + 1, audio.sample_count - 1);
|
|
||||||
float fraction = static_cast<float>(source_pos - source0);
|
|
||||||
for (uint32_t channel = 0; channel < 2; ++channel) {
|
|
||||||
uint32_t source_channel = audio.channels == 1 ? 0 : std::min<uint32_t>(channel, audio.channels - 1);
|
|
||||||
float a = audio.data[source0 * audio.channels + source_channel];
|
|
||||||
float b = audio.data[source1 * audio.channels + source_channel];
|
|
||||||
waveform.index(static_cast<int64_t>(i), channel, 0, 0) =
|
|
||||||
std::clamp(a + (b - a) * fraction, -1.f, 1.f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return waveform;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sd::Tensor<float> unpack_minimax_h3_audio_latent(const sd::Tensor<float>& packed_latent,
|
|
||||||
int audio_length,
|
|
||||||
int video_channels) {
|
|
||||||
if (packed_latent.empty() || audio_length <= 0) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
GGML_ASSERT(packed_latent.dim() == 4 || packed_latent.dim() == 5);
|
|
||||||
int64_t spatial_size = packed_latent.shape()[0] * packed_latent.shape()[1] * packed_latent.shape()[2];
|
|
||||||
int64_t required = static_cast<int64_t>(audio_length) * 2 * 32;
|
|
||||||
int64_t available = (packed_latent.shape()[3] - video_channels) * spatial_size;
|
|
||||||
if (available < required) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
sd::Tensor<float> audio({audio_length, 2, 32, 1});
|
|
||||||
const float* source = packed_latent.data() +
|
|
||||||
static_cast<size_t>(video_channels) * static_cast<size_t>(spatial_size);
|
|
||||||
std::copy_n(source, static_cast<size_t>(required), audio.data());
|
|
||||||
return audio;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ImageGenerationEmbeds {
|
struct ImageGenerationEmbeds {
|
||||||
SDCondition cond;
|
SDCondition cond;
|
||||||
SDCondition uncond;
|
SDCondition uncond;
|
||||||
@ -5843,248 +5710,6 @@ static std::optional<ImageGenerationLatents> prepare_video_generation_latents(sd
|
|||||||
end_image = sd_image_to_tensor(sd_vid_gen_params->end_image, request->width, request->height);
|
end_image = sd_image_to_tensor(sd_vid_gen_params->end_image, request->width, request->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sd_version_is_minimax_h3(sd_ctx->sd->version)) {
|
|
||||||
if (sd_vid_gen_params->ref_images_count < 0 || sd_vid_gen_params->ref_videos_count < 0 ||
|
|
||||||
sd_vid_gen_params->ref_audios_count < 0 ||
|
|
||||||
(sd_vid_gen_params->ref_images_count > 0 && sd_vid_gen_params->ref_images == nullptr) ||
|
|
||||||
(sd_vid_gen_params->ref_videos_count > 0 && sd_vid_gen_params->ref_videos == nullptr) ||
|
|
||||||
(sd_vid_gen_params->ref_audios_count > 0 && sd_vid_gen_params->ref_audios == nullptr)) {
|
|
||||||
LOG_ERROR("invalid MiniMax-H3 Ref2VA input arrays");
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
latents.audio_length = get_minimax_h3_num_audio_latents(request->frames,
|
|
||||||
request->fps);
|
|
||||||
latents.audio_latent = make_minimax_h3_empty_audio_latent(latents.audio_length);
|
|
||||||
|
|
||||||
bool has_references = sd_vid_gen_params->ref_images_count > 0 ||
|
|
||||||
sd_vid_gen_params->ref_videos_count > 0 ||
|
|
||||||
sd_vid_gen_params->ref_audios_count > 0;
|
|
||||||
if (has_references && (!start_image.empty() || !end_image.empty())) {
|
|
||||||
LOG_ERROR("MiniMax-H3 keyframes and Ref2VA references cannot be used together");
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sd_vid_gen_params->control_frames_size > 0) {
|
|
||||||
LOG_ERROR("MiniMax-H3 control_frames are not implemented");
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto add_visual_noise = [&](sd::Tensor<float> latent) {
|
|
||||||
auto condition_rng = std::make_shared<PhiloxRNG>();
|
|
||||||
condition_rng->manual_seed(static_cast<uint64_t>(request->seed));
|
|
||||||
return latent * MiniMaxH3::VISUAL_COND_TIMESTEP +
|
|
||||||
sd::Tensor<float>::randn_like(latent, condition_rng) *
|
|
||||||
(1.f - MiniMaxH3::VISUAL_COND_TIMESTEP);
|
|
||||||
};
|
|
||||||
|
|
||||||
auto add_keyframe = [&](const sd::Tensor<float>& image,
|
|
||||||
int32_t frame_index,
|
|
||||||
const char* name) -> bool {
|
|
||||||
if (image.empty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
auto video_image = image.reshape({image.shape()[0],
|
|
||||||
image.shape()[1],
|
|
||||||
1,
|
|
||||||
image.shape()[2],
|
|
||||||
image.shape()[3]});
|
|
||||||
auto latent = sd_ctx->sd->encode_first_stage(video_image);
|
|
||||||
if (latent.empty()) {
|
|
||||||
LOG_ERROR("failed to encode MiniMax-H3 %s keyframe", name);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
latents.ref_images.push_back(image);
|
|
||||||
latents.ref_latents.push_back(add_visual_noise(std::move(latent)));
|
|
||||||
latents.keyframe_indices.push_back(frame_index);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto resize_reference = [&](const sd::Tensor<float>& image,
|
|
||||||
int width,
|
|
||||||
int height) {
|
|
||||||
return sd::ops::interpolate(
|
|
||||||
image,
|
|
||||||
std::vector<int64_t>{width, height, image.shape()[2], image.shape()[3]});
|
|
||||||
};
|
|
||||||
|
|
||||||
auto encode_reference_audio = [&](const sd_audio_t& audio,
|
|
||||||
int32_t* audio_index) -> bool {
|
|
||||||
if (sd_ctx->sd->audio_vae_model == nullptr) {
|
|
||||||
LOG_ERROR("MiniMax-H3 Ref2VA audio requires --audio-vae with encoder weights");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto waveform = prepare_minimax_h3_reference_waveform(
|
|
||||||
audio,
|
|
||||||
sd_ctx->sd->audio_vae_model->input_sample_rate());
|
|
||||||
if (waveform.empty()) {
|
|
||||||
LOG_ERROR("invalid MiniMax-H3 reference audio");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto encoded = sd_ctx->sd->audio_vae_model->encode(sd_ctx->sd->n_threads, waveform);
|
|
||||||
if (encoded.empty()) {
|
|
||||||
LOG_ERROR("failed to encode MiniMax-H3 reference audio");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
*audio_index = static_cast<int32_t>(latents.reference_audio_latents.size());
|
|
||||||
latents.reference_audio_latents.push_back(std::move(encoded));
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (has_references) {
|
|
||||||
LOG_INFO("MiniMax-H3 Ref2VA: %d image(s), %d video(s), %d audio clip(s)",
|
|
||||||
sd_vid_gen_params->ref_images_count,
|
|
||||||
sd_vid_gen_params->ref_videos_count,
|
|
||||||
sd_vid_gen_params->ref_audios_count);
|
|
||||||
|
|
||||||
for (int i = 0; i < sd_vid_gen_params->ref_images_count; ++i) {
|
|
||||||
auto image = ensure_image_tensor_channels(
|
|
||||||
sd_image_to_tensor(sd_vid_gen_params->ref_images[i]),
|
|
||||||
3);
|
|
||||||
if (image.empty()) {
|
|
||||||
LOG_ERROR("failed to load MiniMax-H3 reference image %d", i + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
int source_w = static_cast<int>(image.shape()[0]);
|
|
||||||
int source_h = static_cast<int>(image.shape()[1]);
|
|
||||||
double source_area = static_cast<double>(source_w) * source_h;
|
|
||||||
double target_area = static_cast<double>(request->width) * request->height;
|
|
||||||
double scale = std::min(1.0, std::sqrt(target_area / source_area));
|
|
||||||
int width = std::max(32, static_cast<int>(std::round(source_w * scale / 32.f)) * 32);
|
|
||||||
int height = std::max(32, static_cast<int>(std::round(source_h * scale / 32.f)) * 32);
|
|
||||||
image = resize_reference(image, width, height);
|
|
||||||
auto latent = sd_ctx->sd->encode_first_stage(image);
|
|
||||||
if (latent.empty()) {
|
|
||||||
LOG_ERROR("failed to encode MiniMax-H3 reference image %d", i + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
int32_t video_index = static_cast<int32_t>(latents.ref_latents.size());
|
|
||||||
latents.ref_latents.push_back(add_visual_noise(std::move(latent)));
|
|
||||||
latents.minimax_reference_blocks.push_back({MiniMaxH3ReferenceKind::IMAGE,
|
|
||||||
video_index,
|
|
||||||
-1});
|
|
||||||
MiniMaxH3PresentationItem item;
|
|
||||||
item.kind = MiniMaxH3PresentationKind::IMAGE;
|
|
||||||
item.frames.push_back(std::move(image));
|
|
||||||
latents.minimax_presentation_refs.push_back(std::move(item));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int video_idx = 0; video_idx < sd_vid_gen_params->ref_videos_count; ++video_idx) {
|
|
||||||
const auto& reference = sd_vid_gen_params->ref_videos[video_idx];
|
|
||||||
if (reference.frames == nullptr || reference.frame_count < 1) {
|
|
||||||
LOG_ERROR("invalid MiniMax-H3 reference video %d", video_idx + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
int source_fps = reference.fps > 0 ? reference.fps : 24;
|
|
||||||
int normalized_frames = static_cast<int>(std::lround(
|
|
||||||
static_cast<double>(reference.frame_count) * 24.0 / source_fps));
|
|
||||||
normalized_frames = std::min(normalized_frames, request->frames);
|
|
||||||
if (normalized_frames < 5) {
|
|
||||||
LOG_ERROR("MiniMax-H3 reference video %d needs at least 5 frames at 24 fps",
|
|
||||||
video_idx + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
while (normalized_frames % 17 != 5) {
|
|
||||||
--normalized_frames;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto first = ensure_image_tensor_channels(sd_image_to_tensor(reference.frames[0]), 3);
|
|
||||||
if (first.empty()) {
|
|
||||||
LOG_ERROR("invalid first frame in MiniMax-H3 reference video %d", video_idx + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
int source_w = static_cast<int>(first.shape()[0]);
|
|
||||||
int source_h = static_cast<int>(first.shape()[1]);
|
|
||||||
double ratio = static_cast<double>(source_w) / source_h;
|
|
||||||
double nominal_w = ratio >= 1.0 ? 768.0 * ratio : 768.0;
|
|
||||||
double nominal_h = ratio >= 1.0 ? 768.0 : 768.0 / ratio;
|
|
||||||
if (nominal_w * nominal_h > 768.0 * 1344.0) {
|
|
||||||
double scale = std::sqrt((768.0 * 1344.0) / (nominal_w * nominal_h));
|
|
||||||
nominal_w *= scale;
|
|
||||||
nominal_h *= scale;
|
|
||||||
}
|
|
||||||
int width = std::max(32, static_cast<int>(std::round(nominal_w / 32.0)) * 32);
|
|
||||||
int height = std::max(32, static_cast<int>(std::round(nominal_h / 32.0)) * 32);
|
|
||||||
if (source_w * source_h < width * height) {
|
|
||||||
width = std::max(32, static_cast<int>(std::round(source_w / 32.0)) * 32);
|
|
||||||
height = std::max(32, static_cast<int>(std::round(source_h / 32.0)) * 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
sd::Tensor<float> video({width, height, normalized_frames, 3, 1});
|
|
||||||
for (int frame = 0; frame < normalized_frames; ++frame) {
|
|
||||||
int source_index = std::min(reference.frame_count - 1,
|
|
||||||
static_cast<int>(std::floor(frame * source_fps / 24.0)));
|
|
||||||
auto source = ensure_image_tensor_channels(
|
|
||||||
sd_image_to_tensor(reference.frames[source_index]),
|
|
||||||
3);
|
|
||||||
if (source.empty()) {
|
|
||||||
LOG_ERROR("invalid frame %d in MiniMax-H3 reference video %d",
|
|
||||||
source_index + 1,
|
|
||||||
video_idx + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
source = resize_reference(source, width, height);
|
|
||||||
sd::ops::slice_assign(&video, 2, frame, frame + 1, source.unsqueeze(2));
|
|
||||||
}
|
|
||||||
auto video_latent = sd_ctx->sd->encode_first_stage(video);
|
|
||||||
if (video_latent.empty()) {
|
|
||||||
LOG_ERROR("failed to encode MiniMax-H3 reference video %d", video_idx + 1);
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
int32_t audio_index = -1;
|
|
||||||
bool has_audio = reference.audio.data != nullptr && reference.audio.sample_count > 0;
|
|
||||||
if (has_audio) {
|
|
||||||
if (!encode_reference_audio(reference.audio, &audio_index)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
MiniMaxH3PresentationItem audio_item;
|
|
||||||
audio_item.kind = MiniMaxH3PresentationKind::AUDIO;
|
|
||||||
latents.minimax_presentation_refs.push_back(std::move(audio_item));
|
|
||||||
}
|
|
||||||
|
|
||||||
MiniMaxH3PresentationItem video_item;
|
|
||||||
video_item.kind = MiniMaxH3PresentationKind::VIDEO;
|
|
||||||
for (int frame = 0; frame < normalized_frames; frame += 12) {
|
|
||||||
auto sampled = sd::ops::slice(video, 2, frame, frame + 1)
|
|
||||||
.reshape({width, height, 3, 1});
|
|
||||||
video_item.frames.push_back(std::move(sampled));
|
|
||||||
video_item.timestamps.push_back(frame / 24.f);
|
|
||||||
}
|
|
||||||
latents.minimax_presentation_refs.push_back(std::move(video_item));
|
|
||||||
|
|
||||||
int32_t video_index = static_cast<int32_t>(latents.ref_latents.size());
|
|
||||||
latents.ref_latents.push_back(add_visual_noise(std::move(video_latent)));
|
|
||||||
latents.minimax_reference_blocks.push_back({has_audio ? MiniMaxH3ReferenceKind::VIDEO_AUDIO
|
|
||||||
: MiniMaxH3ReferenceKind::VIDEO,
|
|
||||||
video_index,
|
|
||||||
audio_index});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int audio_idx = 0; audio_idx < sd_vid_gen_params->ref_audios_count; ++audio_idx) {
|
|
||||||
int32_t encoded_index = -1;
|
|
||||||
if (!encode_reference_audio(sd_vid_gen_params->ref_audios[audio_idx], &encoded_index)) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
MiniMaxH3PresentationItem item;
|
|
||||||
item.kind = MiniMaxH3PresentationKind::AUDIO;
|
|
||||||
latents.minimax_presentation_refs.push_back(std::move(item));
|
|
||||||
latents.minimax_reference_blocks.push_back({MiniMaxH3ReferenceKind::AUDIO,
|
|
||||||
-1,
|
|
||||||
encoded_index});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!has_references && (!start_image.empty() || !end_image.empty())) {
|
|
||||||
LOG_INFO(!start_image.empty() && !end_image.empty() ? "MiniMax-H3 FL2VA" : !start_image.empty() ? "MiniMax-H3 I2VA"
|
|
||||||
: "MiniMax-H3 end-frame conditioning");
|
|
||||||
}
|
|
||||||
if (!has_references &&
|
|
||||||
(!add_keyframe(start_image, 0, "start") ||
|
|
||||||
!add_keyframe(end_image, request->frames - 1, "end"))) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sd_version_is_ltxav(sd_ctx->sd->version)) {
|
if (sd_version_is_ltxav(sd_ctx->sd->version)) {
|
||||||
latents.audio_length = get_ltxav_num_audio_latents(request->frames, request->fps);
|
latents.audio_length = get_ltxav_num_audio_latents(request->frames, request->fps);
|
||||||
latents.audio_latent = make_ltxav_empty_audio_latent(latents.audio_length);
|
latents.audio_latent = make_ltxav_empty_audio_latent(latents.audio_length);
|
||||||
@ -6445,8 +6070,7 @@ static std::optional<ImageGenerationLatents> prepare_video_generation_latents(sd
|
|||||||
latents.init_latent = sd_ctx->sd->generate_init_latent(request->width, request->height, request->frames, true);
|
latents.init_latent = sd_ctx->sd->generate_init_latent(request->width, request->height, request->frames, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((sd_version_is_ltxav(sd_ctx->sd->version) || sd_version_is_minimax_h3(sd_ctx->sd->version)) &&
|
if (sd_version_is_ltxav(sd_ctx->sd->version) && !latents.audio_latent.empty()) {
|
||||||
!latents.audio_latent.empty()) {
|
|
||||||
if (!latents.denoise_mask.empty()) {
|
if (!latents.denoise_mask.empty()) {
|
||||||
latents.denoise_mask = pack_ltxav_audio_and_video_denoise_mask(latents.denoise_mask,
|
latents.denoise_mask = pack_ltxav_audio_and_video_denoise_mask(latents.denoise_mask,
|
||||||
latents.init_latent,
|
latents.init_latent,
|
||||||
@ -6470,8 +6094,7 @@ static ImageGenerationEmbeds prepare_video_generation_embeds(sd_ctx_t* sd_ctx,
|
|||||||
condition_params.text = request.prompt;
|
condition_params.text = request.prompt;
|
||||||
condition_params.zero_out_masked = true;
|
condition_params.zero_out_masked = true;
|
||||||
condition_params.ref_images = &latents.ref_images;
|
condition_params.ref_images = &latents.ref_images;
|
||||||
condition_params.minimax_h3_references = &latents.minimax_presentation_refs;
|
if (sd_version_is_lingbot_video(sd_ctx->sd->version)) {
|
||||||
if (sd_version_is_lingbot_video(sd_ctx->sd->version) || sd_version_is_minimax_h3(sd_ctx->sd->version)) {
|
|
||||||
condition_params.ref_image_params.vlm_resize_mode = RefImageResizeMode::AREA;
|
condition_params.ref_image_params.vlm_resize_mode = RefImageResizeMode::AREA;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -6480,28 +6103,12 @@ static ImageGenerationEmbeds prepare_video_generation_embeds(sd_ctx_t* sd_ctx,
|
|||||||
condition_params);
|
condition_params);
|
||||||
embeds.cond.c_concat = latents.concat_latent;
|
embeds.cond.c_concat = latents.concat_latent;
|
||||||
embeds.cond.c_vector = latents.clip_vision_output;
|
embeds.cond.c_vector = latents.clip_vision_output;
|
||||||
if (sd_version_is_minimax_h3(sd_ctx->sd->version)) {
|
|
||||||
embeds.cond.c_ref_images = latents.ref_latents;
|
|
||||||
embeds.cond.c_ref_audios = latents.reference_audio_latents;
|
|
||||||
embeds.cond.c_reference_blocks = latents.minimax_reference_blocks;
|
|
||||||
if (!latents.keyframe_indices.empty()) {
|
|
||||||
embeds.cond.c_position_ids = sd::Tensor<int32_t>(
|
|
||||||
{static_cast<int64_t>(latents.keyframe_indices.size())},
|
|
||||||
latents.keyframe_indices);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (request.use_uncond) {
|
if (request.use_uncond) {
|
||||||
condition_params.text = request.negative_prompt;
|
condition_params.text = request.negative_prompt;
|
||||||
embeds.uncond = sd_ctx->sd->cond_stage_model->get_learned_condition(sd_ctx->sd->n_threads,
|
embeds.uncond = sd_ctx->sd->cond_stage_model->get_learned_condition(sd_ctx->sd->n_threads,
|
||||||
condition_params);
|
condition_params);
|
||||||
embeds.uncond.c_concat = latents.concat_latent;
|
embeds.uncond.c_concat = latents.concat_latent;
|
||||||
embeds.uncond.c_vector = latents.clip_vision_output;
|
embeds.uncond.c_vector = latents.clip_vision_output;
|
||||||
if (sd_version_is_minimax_h3(sd_ctx->sd->version)) {
|
|
||||||
embeds.uncond.c_ref_images = latents.ref_latents;
|
|
||||||
embeds.uncond.c_ref_audios = latents.reference_audio_latents;
|
|
||||||
embeds.uncond.c_reference_blocks = latents.minimax_reference_blocks;
|
|
||||||
embeds.uncond.c_position_ids = embeds.cond.c_position_ids;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t t1 = ggml_time_ms();
|
int64_t t1 = ggml_time_ms();
|
||||||
@ -6523,7 +6130,7 @@ static sd_image_t* decode_video_outputs(sd_ctx_t* sd_ctx,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
sd::Tensor<float> video_latent = final_latent;
|
sd::Tensor<float> video_latent = final_latent;
|
||||||
if ((sd_version_is_ltxav(sd_ctx->sd->version) || sd_version_is_minimax_h3(sd_ctx->sd->version)) &&
|
if (sd_version_is_ltxav(sd_ctx->sd->version) &&
|
||||||
video_latent.shape()[3] > sd_ctx->sd->get_latent_channel()) {
|
video_latent.shape()[3] > sd_ctx->sd->get_latent_channel()) {
|
||||||
video_latent = sd::ops::slice(video_latent, 3, 0, sd_ctx->sd->get_latent_channel());
|
video_latent = sd::ops::slice(video_latent, 3, 0, sd_ctx->sd->get_latent_channel());
|
||||||
}
|
}
|
||||||
@ -7111,7 +6718,7 @@ SD_API bool generate_video(sd_ctx_t* sd_ctx,
|
|||||||
LOG_INFO("generating latent video completed, taking %.2fs", (latent_end - latent_start) * 1.0f / 1000);
|
LOG_INFO("generating latent video completed, taking %.2fs", (latent_end - latent_start) * 1.0f / 1000);
|
||||||
|
|
||||||
sd_audio_t* generated_audio = nullptr;
|
sd_audio_t* generated_audio = nullptr;
|
||||||
if ((sd_version_is_ltxav(sd_ctx->sd->version) || sd_version_is_minimax_h3(sd_ctx->sd->version)) &&
|
if (sd_version_is_ltxav(sd_ctx->sd->version) &&
|
||||||
latents.audio_length > 0 &&
|
latents.audio_length > 0 &&
|
||||||
sd_ctx->sd->audio_vae_model != nullptr) {
|
sd_ctx->sd->audio_vae_model != nullptr) {
|
||||||
if (sd_ctx->sd->get_cancel_flag() == SD_CANCEL_ALL) {
|
if (sd_ctx->sd->get_cancel_flag() == SD_CANCEL_ALL) {
|
||||||
@ -7120,11 +6727,7 @@ SD_API bool generate_video(sd_ctx_t* sd_ctx,
|
|||||||
}
|
}
|
||||||
int64_t audio_latent_decode_start = ggml_time_ms();
|
int64_t audio_latent_decode_start = ggml_time_ms();
|
||||||
|
|
||||||
auto audio_latent = sd_version_is_minimax_h3(sd_ctx->sd->version)
|
auto audio_latent = unpack_ltxav_audio_latent(final_latent,
|
||||||
? unpack_minimax_h3_audio_latent(final_latent,
|
|
||||||
latents.audio_length,
|
|
||||||
sd_ctx->sd->get_latent_channel())
|
|
||||||
: unpack_ltxav_audio_latent(final_latent,
|
|
||||||
latents.audio_length,
|
latents.audio_length,
|
||||||
sd_ctx->sd->get_latent_channel());
|
sd_ctx->sd->get_latent_channel());
|
||||||
if (!audio_latent.empty()) {
|
if (!audio_latent.empty()) {
|
||||||
@ -7137,7 +6740,7 @@ SD_API bool generate_video(sd_ctx_t* sd_ctx,
|
|||||||
if (!waveform.empty()) {
|
if (!waveform.empty()) {
|
||||||
generated_audio = waveform_to_sd_audio(sd_ctx->sd, waveform);
|
generated_audio = waveform_to_sd_audio(sd_ctx->sd, waveform);
|
||||||
} else {
|
} else {
|
||||||
LOG_WARN("audio latent decode failed; continuing with silent video output");
|
LOG_WARN("LTX audio latent decode failed; continuing with silent video output");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int64_t audio_latent_decode_end = ggml_time_ms();
|
int64_t audio_latent_decode_end = ggml_time_ms();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user