Repository navigation
Description
- Project: leejet/stable-diffusion.cpp
- Version tested:
master-1106-532d2c1(commit532d2c1, right after the opaque-options refactor refactor(api): adopt opaque options, tensors, and results #2365). Baseline comparison against the previously packagedmaster-1104-5dcfa79. - Hardware: 4 × RTX 5070 Ti (16303 MiB each), PCIe, DDR5 ≥ 96 GB
- Model: MiniMax-H3 AV (DiT video):
minimax_h3_ref2va_pruned-Q8_0.gguf(20.5 GB weights, 53 transformer blocks),minimax_h3_video_vae_fp16.safetensors, Sakura TE (Q4_K_M, ~14 GB) - Reported: 2026-11-17, during development of our own project on top of stable-diffusion.cpp
- Status: all four issues diagnosed with root cause; item 1 and item 2 already have working local patches (validated below; the working patch is provided in the first comment of this issue), item 3/item 4 are design gaps with suggested direction.
1. MAIN BUG — layer-split sessions OOM mid-run: weight staging copies are never released (lazy eviction + pin-order trap)
Symptom
With the model fitting via --offload-to-cpu and a split backend
(diffusion=cuda0&cuda1 --split-mode layer), short generations succeed (24f,
672×384). At HD 124f the run dies at the same place every time,
mid-sampling:
[E] ggml: ggml_backend_cuda_buffer_type_alloc_buffer: allocating 764.86 MiB on device 1: cudaMalloc failed: out of memory
[E] minimax_h3 compute failed: GGML status: error (failed to allocate memory) --- execution_engine.cpp:479
[E] minimax_h3 segment 37/54 (minimax_h3.blocks.33) failed during execution
Nothing about the failing block is special — it is simply the point where the
second card runs out. Per-segment capacity logs (free bytes at segment start,
device 1, HD 124f, blocks .30 → .33):
free 2708 MB → 2314 MB → 1918 MB → 1524 MB (−394 MB per block executed, monotonic)
resident(tracked weights) 3150 → 3544 → 3938 → 4332 MB (+394 MB per block, never freed)
394 MB is exactly one Q8_0 block. The device accumulates every block it has
ever executed, plus the prefetched next block, until real allocations
overflow. The per-segment capacity check keeps "passing" because its estimate
(≈9.1 GB pending vs ≈10.1 GB free) still fits on paper; the actual
cudaMalloc during execution then fails with ~1 GB of phantom headroom.
Root cause
Two mechanisms combine:
-
Eviction is pressure-only. Device-side staging copies of weights
(ModelManager::compute_staging_blocks_) are released from
release_compute_staging_blocks(false)/evict_compute_backend_block()—
which are only reached from themake_enough_memory()failure path
(execution_engine.cpp~925) and from unregister/teardown. Normal segment
completion never releases them. On the home card this is masked (the buffer
cache reuses per-device staging buffers and the pressure loop caps growth at
~4.9 GB in our single-card traces); on a secondary split device every block
copy just accumulates. -
The pin-order trap.
prepare_params()pins the segment's tensors
(state->pin_count++,model_manager.cpp~1615). The pins are released by
SegmentWeightPipeline::segment_end()→
release_compute_backend_params(), which is called from the next
segment'ssegment_start()(activate()→segment_end(),
segment_weight_pipeline.cpp~46/~106). So at every moment when eviction is
attempted under pressure — i.e. during the next segment's preparation —
the just-executed block (the ideal eviction candidate) is still pinned, and
thefully_evictablechecks in
evict_compute_backend_params()/release_compute_staging_blocks()reject
the block. The manager therefore cannot evict precisely the thing it needs
to evict; "under pressure" arrives too late and the allocator — not the
estimator — takes the hit.
Local fix (validated)
Eagerly evict the previous segment's staging copies at the point where their
pins are guaranteed to be gone — right after weights.segment_start(index) in
GraphExecution::run() (execution_engine.cpp):
if (!weights.segment_start(index, ensure_capacity)) {
return fail_segment("weight preparation");
}
if (segmented && index > 0 && manager != nullptr) {
// segment_start() has just released the previous segment's weight pins;
// its device staging copies are now the cheapest memory on the card.
// Release them before this segment allocates its workspace. The
// prefetched segment lives in separate staging blocks and is pinned
// out of eviction.
manager->evict_compute_backend_params(weights.params(index - 1));
}evict_compute_backend_params() is already strict (it frees only staging
blocks that are fully unpinned and intersect the target tensors), so this
cannot evict the current block or the prefetch window.
Validation (patched build, all runs --vae-tiling --sage-attn --offload-to-cpu --rng cpu)
| Run | Config | Result |
|---|---|---|
| HD 124f, 1 GPU | minimax_h3_mlp_chunk_budget_gib=1 |
done, 2m38s / 2 steps |
| HD 124f, split 2 | same + --split-mode layer |
done, 3m10s / 2 steps |
| HD 124f, split 3 | same | done, 3m14s / 2 steps |
| HD 124f hires (draft 672×384 + HD refine), 1 vs split 2 | same | both done; SSIM = 1.000000 |
| 24f 672×384, split 2/3/4 vs baseline | unpatched chunking | SSIM = 1.000000 |
| Regression: qwen-image / krea2 / moss t2i on patched binary | defaults | all done, no behavior change |
Cost: staged block weights are re-uploaded from RAM per sampling step
(~394 MB/block at Q8_0). Observed overhead for split vs single card is
+10…20%, which is the price of capacity, not of the eviction fix itself.
A refinement worth considering upstream: keep an LRU window (e.g. 2–4 blocks)
instead of evicting at distance 1, to cut re-uploads across steps.
2. The seq-chunking machinery is a no-op on CUDA (ggml_backend_buft_get_max_size() = SIZE_MAX), so block intermediates are monolithic
minimax_h3.hpp (and the same idiom elsewhere) sizes sequence chunking from
the backend buffer-type maximum allocation size:
size_t chunk_budget = ggml_backend_buft_get_max_size(buft); // CUDA: SIZE_MAX
chunk_budget -= chunk_budget % alignment;
...
chunk_size = min(seq_len, chunk_budget / token_bytes); // => never chunksOn CUDA the buft max size is effectively unbounded, so both
FeedForward::forward() and Attention::project_qkv() fall back to
monolithic intermediates even though the chunked code path exists and works
(it was clearly written for backends like Vulkan/Metal with allocation caps).
Measured on HD 264f (seq ≈ 82.7k tokens, f32 graph):
attn.qkv_projMUL_MAT output: 6780 MB in one buffer, followed by a
6860 MB CONCAT (text-cond merge) — ~13.6 GB of live intermediates for one
projection before attention even starts;mlp.fc1output: a multi-GB monolith until budgeted.
Two knobs were needed to make this configurable (local patch, --model-args):
minimax_h3_mlp_chunk_budget_gib— capschunk_budgetfor both
FeedForward::forward()andAttention::project_qkv()(shared block-level
token-chunk budget);- the existing
minimax_h3_attn_chunk_budget(head grouping) was already
budgeted — the projection path simply wasn't.
Worth noting for whoever tunes estimates: capping FFN alone changed the
scheduler's pending estimate by exactly 0 bytes (22248.99 MB before and
after, byte-identical) — the ggml sched-reserve simulation already reuses FFN
scratch perfectly. The estimate only moved when qkv projection chunking
was capped (22.2 → 21.0 GB). The monolithic qkv+concat, not the MLP, is the
real wall on CUDA.
Suggested upstream direction: give the chunk budget a first-class config
surface (per-model model-args at minimum) instead of deriving it purely from
buft_get_max_size(), and default it to something sane on CUDA for long-seq
DiTs (H3, Wan, LTX-class).
3. Activations are hardwired to f32; no bf16 knob — the per-block workspace floor blocks whole canvas classes
The H3 runner builds every intermediate in f32. There is no
--model-args/CLI knob for activation dtype (--type and
--tensor-type-rules are weight-only). Consequence — measured per-block
workspace floor (segment blocks.0, single device, chunk budgets already at
0.25 GiB, i.e. the floor is the seq-proportional q/k/v totals + residual
streams, not scratch):
| Canvas | seq tokens | pending / real reserve | fits 16 GB? |
|---|---|---|---|
| HD 124f (1344×768) | ~41 k | ~9.5 GB | yes (with item 2's knobs) |
| HD 264f | ~83 k | 18.5 GB real (ggml_gallocr_reserve_n_impl single-arena attempt) |
no |
| FHD 124f (1920×1088) | ~84 k | ~22 GB | no |
| FHD 264f | ~165 k | ~40 GB | no |
bf16 activations would halve this floor (HD 264f → ~9–10 GB, FHD 124f →
~11–12 GB) and make these classes feasible on 16 GB cards. This is not exotic:
neighboring runners in the same tree already use f16 opportunistically
(qwen_image_2_1.hpp f16 prefix KV cache under flash-attn, wan.hpp f16
loader defaults), and the vendor's own H3 artifacts are fp16/bf16. The H3
runner is simply still all-f32. A minimax_h3_act_dtype=bf16 (or a global
activation-dtype policy with per-op f32 accumulation guarantees) is the
suggested shape.
Note this also bounds what multi-GPU layer split can do at all: activation
workspace is per-block and per-token — it does not divide across cards. Layer
split fixes weight capacity; item 3 (or sequence parallelism) is what fixes
long-sequence capacity. The current need 22760.99 MB ... available 15127.11 MB wall on HD 264f is identical on master-1104 and master-1106
with 1 or 4 cards — not a regression, just a hard floor.
4. Row split mode is unusable on CUDA: "backend has no split buffer type"
--split-mode row fails during session setup on CUDA backends (the split
buffer type lookup has no CUDA path), leaving layer as the only usable mode
for diffusion=cuda0&cuda1. Layer split, per item 3, cannot split activations —
so today there is no way at all to scale a too-long sequence across cards. Not
blocking our envelope, but it removes the obvious answer to item 3's floor.
5. Minor observations (DX / estimator honesty)
- Failure phase mislabeling. The capacity gate that trips first is
reached throughweights.segment_start(index, ensure_capacity), but it is
reported asfailed during weight preparation— the message suggests weight
I/O, while the actual reject is the workspace capacity estimate. The real
phase only becomes visible after bypassing it ("workspace capacity check").
Suggest distinct phase names (ensure_capacityvsweights load). - Check vs allocator mismatch. The check's
pending(sched-reserve
estimate) and the real single-arenagallocrreserve differ materially
(22.25 GB estimate vs 18.55 GB actual for the same HD 264f segment); and
the check passes while the actual allocator dies later with ~1 GB of
apparent headroom (cublas workspace / fragmentation are excluded by design:
"Backend scratch buffers and pipelines are not included in graph
measurements",model_manager.cpp~1792). A per-backend dynamic scratch
allowance would make the gate predictive. - Hardcoded 512 MB safety margin (
model_manager.cpp~1793) is a big
fraction of a 16 GB card; a CLI knob (or auto fromtotal/free) would help
small-VRAM users. log_detailsincheck_capacity()almost never fires (only on final
failure), so tuning conversations start blind; a
--log-level verbose-gated per-segment capacity line (pending/free/resident
per backend) was invaluable for this diagnosis and is cheap to keep.
Suggested default for 16 GB cards (H3), after these fixes
--offload-to-cpu --vae-tiling --sage-attn \
--model-args minimax_h3_mlp_chunk_budget_gib=1,minimax_h3_attn_chunk_budget=0.5
HD 124f runs on one card; diffusion=cuda0&cuda1 --split-mode layer adds
weight headroom bit-identically (SSIM 1.000000 across 2/3-card splits, direct
and hires paths). HD 264f+ / FHD need item 3 (bf16 activations) or item 4 (row/seq
parallelism) to exist on 16 GB.
Filed by Qwen3.8-Next-Flash 180B (AI assistant) on behalf of the user. This investigation was conducted while building our own project on top
of stable-diffusion.cpp; full per-segment logs are available and we are glad
to test any upstream fix against this workload.
Activity
Koreprobro commentedon Nov 17, 2026
Working patch against
532d2c1(validated: HD 124f direct + layer-split 2/3 cards + hires path, SSIM 1.000000; qwen-image / krea2 / moss regression clean):