Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/models/ltx2.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ to validate syntax and types without loading weights. See the
[shared run instructions](../dgl/README.md#run-an-example) for build paths
and the server's file-access rules.

To continue a clip from existing frames and sound in a custom script, pack the
video and audio masks in the same layout as the latents. Give all features of
each audio token the same mask value. Pass the clean reference as `base` and
use `denoise_mask` with `mask_input = true` in one `sample` call. Use `0` for
the region to preserve and `1` for the region to generate.

#### Latent upscaling

`load_latent_upscaler` loads the spatial or temporal checkpoint as an
Expand Down
3 changes: 2 additions & 1 deletion src/dgl/models/dit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ namespace sd::dgl::models {
present(condition.c_ref_audios), present(condition.c_reference_blocks),
options.audio_length, options.video_sigma_shift, options.audio_sigma_shift, step.target_sigma};
} else if (sd_version_is_ltxav(version())) {
params.extra = LTXAVDiffusionExtra{nullptr, present(audio_time), options.audio_length, options.fps, present(options.video_positions), first(condition.extra_c_crossattns)};
params.extra = LTXAVDiffusionExtra{nullptr, present(audio_time), options.audio_length, options.fps,
present(options.video_positions), first(condition.extra_c_crossattns), present(step.mask)};
} else if (sd_version_is_minit2i(version())) {
params.extra = MiniT2IDiffusionExtra{params.y};
} else if (sd_version_is_sensenova_u1(version())) {
Expand Down
88 changes: 70 additions & 18 deletions src/model/diffusion/ltxv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ namespace LTXV {
ggml_tensor* ax,
ggml_tensor* timestep,
ggml_tensor* audio_timestep,
ggml_tensor* global_timestep,
ggml_tensor* context,
ggml_tensor* v_pe,
ggml_tensor* a_pe,
Expand Down Expand Up @@ -945,29 +946,34 @@ namespace LTXV {
auto a_timestep_mod = a_pair.first;
auto a_embedded_time = a_pair.second;

auto prompt_timestep_scaled = global_timestep != nullptr
? ggml_ext_scale(ctx, global_timestep, config.timestep_scale_multiplier)
: a_timestep_scaled;
ggml_tensor* v_prompt_timestep_mod = nullptr;
ggml_tensor* a_prompt_timestep_mod = nullptr;
if (config.cross_attention_adaln) {
auto prompt_adaln_single = std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["prompt_adaln_single"]);
auto audio_prompt_adaln_single = std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["audio_prompt_adaln_single"]);
v_prompt_timestep_mod = prompt_adaln_single->forward(ctx, a_timestep_scaled).first;
a_prompt_timestep_mod = audio_prompt_adaln_single->forward(ctx, a_timestep_scaled).first;
v_prompt_timestep_mod = prompt_adaln_single->forward(ctx, prompt_timestep_scaled).first;
a_prompt_timestep_mod = audio_prompt_adaln_single->forward(ctx, prompt_timestep_scaled).first;
}

auto av_ca_video_timestep = repeat_scalar_timestep_like(ctx, effective_audio_timestep, timestep);
auto av_ca_audio_timestep = effective_audio_timestep;
auto av_ca_factor = config.av_ca_timestep_scale_multiplier / config.timestep_scale_multiplier;
// With audio conditioning, scale/shift follows each stream's tokens. Both gates use the shared sampling timestep.
auto av_ca_video_timestep = global_timestep != nullptr ? v_timestep_scaled : repeat_scalar_timestep_like(ctx, effective_audio_timestep, timestep);
auto av_ca_audio_timestep = global_timestep != nullptr ? a_timestep_scaled : effective_audio_timestep;
auto av_ca_factor = config.av_ca_timestep_scale_multiplier / config.timestep_scale_multiplier;
auto av_ca_global_timestep = global_timestep != nullptr ? ggml_ext_scale(ctx, prompt_timestep_scaled, av_ca_factor) : nullptr;
auto av_ca_video_scale_shift_timestep =
std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["av_ca_video_scale_shift_adaln_single"])->forward(ctx, av_ca_video_timestep).first;
auto av_ca_a2v_gate_noise_timestep =
std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["av_ca_a2v_gate_adaln_single"])
->forward(ctx, ggml_ext_scale(ctx, av_ca_video_timestep, av_ca_factor))
->forward(ctx, av_ca_global_timestep != nullptr ? av_ca_global_timestep : ggml_ext_scale(ctx, av_ca_video_timestep, av_ca_factor))
.first;
auto av_ca_audio_scale_shift_timestep =
std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["av_ca_audio_scale_shift_adaln_single"])->forward(ctx, av_ca_audio_timestep).first;
auto av_ca_v2a_gate_noise_timestep =
std::dynamic_pointer_cast<AdaLayerNormSingle>(blocks["av_ca_v2a_gate_adaln_single"])
->forward(ctx, ggml_ext_scale(ctx, av_ca_audio_timestep, av_ca_factor))
->forward(ctx, av_ca_global_timestep != nullptr ? av_ca_global_timestep : ggml_ext_scale(ctx, av_ca_audio_timestep, av_ca_factor))
.first;

auto mark_prelude = [](ggml_tensor* tensor, const char* name) {
Expand Down Expand Up @@ -1125,6 +1131,41 @@ namespace LTXV {
return {vx, ax};
}

sd::Tensor<float> audio_timesteps_from_mask(const sd::Tensor<float>& packed,
const sd::Tensor<float>& mask,
int audio_length,
float timestep) const {
const auto& shape = packed.shape();
const auto& mask_shape = mask.shape();
if (audio_length <= 0 || shape.size() < 4 || mask_shape.size() < 4 || mask_shape[3] <= config.in_channels) {
return {};
}

const int64_t spatial_size = shape[0] * shape[1] * shape[2];
const int64_t audio_offset = spatial_size * config.in_channels;
const int64_t audio_values = static_cast<int64_t>(audio_length) * config.num_audio_channels * config.audio_frequency_bins;
if (audio_offset + audio_values > packed.numel()) {
return {};
}

std::vector<float> times(audio_length);
bool has_audio_conditioning = false;
for (int64_t token = 0; token < audio_length; ++token) {
// The packed tail stores [frequency, time, channel]. Read the first feature of each time token.
auto coord = sd::tensor_unravel_index(audio_offset + token * config.audio_frequency_bins, shape);
coord.resize(mask_shape.size(), 0);
for (size_t dim = 0; dim < mask_shape.size(); ++dim) {
if (mask_shape[dim] == 1) {
coord[dim] = 0;
}
}
const float weight = mask.index(coord);
times[token] = weight * timestep;
has_audio_conditioning |= weight != 1.f;
}
return has_audio_conditioning ? sd::Tensor<float>::from_vector(std::move(times)) : sd::Tensor<float>{};
}

ggml_tensor* merge_av_latents(GGMLRunnerContext* runner_ctx, ggml_tensor* vx, ggml_tensor* ax) const {
auto ctx = runner_ctx->ggml_ctx;
if (ax == nullptr || ggml_nelements(ax) == 0 || ax->ne[1] == 0) {
Expand Down Expand Up @@ -1173,7 +1214,8 @@ namespace LTXV {
int audio_length = 0,
float frame_rate = 24.f,
const sd::Tensor<float>& video_positions_tensor = {},
const sd::Tensor<float>& audio_context_tensor = {}) {
const sd::Tensor<float>& audio_context_tensor = {},
const sd::Tensor<float>& global_timestep_tensor = {}) {
auto split_inputs = split_av_latents(x_tensor, audio_length);
vx_input_cache = split_inputs.first;
if (!audio_x_tensor.empty()) {
Expand All @@ -1192,12 +1234,13 @@ namespace LTXV {
throw std::runtime_error("LTX audio diffusion requires the prepared audio condition in extra_crossattn");
}

ggml_tensor* vx = make_input(vx_input_cache);
ggml_tensor* ax = make_optional_input(ax_input_cache);
ggml_tensor* timesteps = make_input(timesteps_tensor);
ggml_tensor* a_timestep = make_optional_input(audio_timesteps_tensor);
ggml_tensor* context = make_optional_input(context_tensor);
ggml_tensor* audio_context = make_optional_input(audio_context_tensor);
ggml_tensor* vx = make_input(vx_input_cache);
ggml_tensor* ax = make_optional_input(ax_input_cache);
ggml_tensor* timesteps = make_input(timesteps_tensor);
ggml_tensor* a_timestep = make_optional_input(audio_timesteps_tensor);
ggml_tensor* global_timestep = make_optional_input(global_timestep_tensor);
ggml_tensor* context = make_optional_input(context_tensor);
ggml_tensor* audio_context = make_optional_input(audio_context_tensor);

ggml_cgraph* gf = new_graph_custom(LTXAV_GRAPH_SIZE);

Expand Down Expand Up @@ -1322,6 +1365,7 @@ namespace LTXV {
ax,
timesteps,
a_timestep,
global_timestep,
context,
video_pe,
audio_pe,
Expand All @@ -1342,9 +1386,16 @@ namespace LTXV {
int audio_length = 0,
float frame_rate = 24.f,
const sd::Tensor<float>& video_positions = {},
const sd::Tensor<float>& audio_context = {}) {
auto get_graph = [&]() -> ggml_cgraph* {
return build_graph(x, timesteps, context, audio_x, audio_timesteps, audio_length, frame_rate, video_positions, audio_context);
const sd::Tensor<float>& audio_context = {},
const sd::Tensor<float>& denoise_mask = {}) {
const sd::Tensor<float> empty;
const auto masked_audio_timesteps = !denoise_mask.empty() && audio_timesteps.numel() == 1
? audio_timesteps_from_mask(x, denoise_mask, audio_length, audio_timesteps[0])
: sd::Tensor<float>{};
const auto& audio_token_timesteps = masked_audio_timesteps.empty() ? audio_timesteps : masked_audio_timesteps;
const auto& global_timestep = masked_audio_timesteps.empty() ? empty : audio_timesteps;
auto get_graph = [&]() -> ggml_cgraph* {
return build_graph(x, timesteps, context, audio_x, audio_token_timesteps, audio_length, frame_rate, video_positions, audio_context, global_timestep);
};
auto out = restore_trailing_singleton_dims(GGMLRunner::compute(get_graph, n_threads, false), x.dim());
if (out.empty() && device_pe_key_ && last_compute_status() == GGML_STATUS_ALLOC_FAILED) {
Expand All @@ -1370,7 +1421,8 @@ namespace LTXV {
extra->audio_length,
extra->frame_rate,
tensor_or_empty(extra->video_positions),
tensor_or_empty(extra->audio_context));
tensor_or_empty(extra->audio_context),
tensor_or_empty(extra->denoise_mask));
}

void test(const std::string& x_path,
Expand Down
1 change: 1 addition & 0 deletions src/model/diffusion/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct LTXAVDiffusionExtra {
float frame_rate = 24.f;
const sd::Tensor<float>* video_positions = nullptr;
const sd::Tensor<float>* audio_context = nullptr;
const sd::Tensor<float>* denoise_mask = nullptr;
};

struct MiniMaxH3DiffusionExtra {
Expand Down
Loading