Skip to content

Commit fb30e52

Browse files
committed
fix: keep MiniMax-H3 VAE weights resident across temporal chunks
Each temporal chunk of the MiniMax-H3 video VAE decode and encode went through VAE::decode/VAE::encode, whose trailing runner_end() evicts the ~5GB weights from the compute backend, so the next chunk re-staged them (under auto-fit even re-reading the safetensors from disk). Keep the runner alive across the temporal chunk loop and end it once when the loop finishes. VAE::decode/VAE::encode gain a non-virtual overload with an auto_end flag; the virtual signatures are unchanged so existing overrides are unaffected.
1 parent 3f8527a commit fb30e52

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎src/model/vae/minimax_h3_vae.hpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,11 @@ namespace MiniMaxH3VAE {
633633
auto plan = make_vae_temporal_tile_plan(input.shape()[2], {17, 0});
634634
auto result = process_vae_temporal_tiles(input, plan, [&](const sd::Tensor<float>& chunk, const VAETemporalTile& tile) {
635635
SD_UNUSED(tile);
636-
return VAE::encode(n_threads, chunk, tiling, circular_x, circular_y);
636+
// keep the runner alive across chunks; ending it here would
637+
// evict and reload the encoder weights every chunk
638+
return VAE::encode(n_threads, chunk, tiling, circular_x, circular_y, false);
637639
});
640+
runner_end();
638641
if (result.empty()) {
639642
return {};
640643
}
@@ -695,13 +698,16 @@ namespace MiniMaxH3VAE {
695698
{static_cast<int>(tokens_per_chunk + token_overlap), static_cast<int>(token_overlap)});
696699
GGML_ASSERT(plan.tiles.size() == static_cast<size_t>(num_chunks));
697700
auto result = process_vae_temporal_tiles(input, plan, [&](const sd::Tensor<float>& chunk, const VAETemporalTile& tile) {
701+
// keep the runner alive across chunks; ending it here would
702+
// evict and reload the decoder weights every chunk
698703
auto decoded = VAE::decode(n_threads,
699704
chunk,
700705
tiling,
701706
true,
702707
circular_x,
703708
circular_y,
704-
silent);
709+
silent,
710+
false);
705711
if (decoded.empty()) {
706712
return sd::Tensor<float>();
707713
}
@@ -728,6 +734,7 @@ namespace MiniMaxH3VAE {
728734
}
729735
return first;
730736
});
737+
runner_end();
731738
if (result.empty()) {
732739
return {};
733740
}

‎src/model/vae/vae.hpp‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ struct VAE : public GGMLRunner {
241241
sd_tiling_params_t tiling_params,
242242
bool circular_x = false,
243243
bool circular_y = false) {
244+
return encode(n_threads, x, tiling_params, circular_x, circular_y, true);
245+
}
246+
247+
sd::Tensor<float> encode(int n_threads,
248+
const sd::Tensor<float>& x,
249+
sd_tiling_params_t tiling_params,
250+
bool circular_x,
251+
bool circular_y,
252+
bool auto_end) {
244253
int64_t t0 = ggml_time_ms();
245254
tiling_params = resolve_tiling_params(tiling_params);
246255
sd::Tensor<float> input = x;
@@ -280,7 +289,9 @@ struct VAE : public GGMLRunner {
280289
tiling_params);
281290
}
282291

283-
runner_end();
292+
if (auto_end) {
293+
runner_end();
294+
}
284295

285296
if (output.empty()) {
286297
LOG_ERROR("vae encode compute failed");
@@ -298,6 +309,17 @@ struct VAE : public GGMLRunner {
298309
bool circular_x = false,
299310
bool circular_y = false,
300311
bool silent = false) {
312+
return decode(n_threads, x, tiling_params, decode_video, circular_x, circular_y, silent, true);
313+
}
314+
315+
sd::Tensor<float> decode(int n_threads,
316+
const sd::Tensor<float>& x,
317+
sd_tiling_params_t tiling_params,
318+
bool decode_video,
319+
bool circular_x,
320+
bool circular_y,
321+
bool silent,
322+
bool auto_end) {
301323
int64_t t0 = ggml_time_ms();
302324
tiling_params = resolve_tiling_params(tiling_params);
303325
sd::Tensor<float> input = x;
@@ -338,7 +360,9 @@ struct VAE : public GGMLRunner {
338360
tiling_params);
339361
}
340362

341-
runner_end();
363+
if (auto_end) {
364+
runner_end();
365+
}
342366

343367
if (output.empty()) {
344368
LOG_ERROR("vae decode compute failed");

0 commit comments

Comments
 (0)