Repository navigation
Description
Feature Summary
Free the source weights once their compute copy is staged for an immediate LoRA merge, so the source storage and the staged copies do not stay resident together. The source is still in the model file and can be read again if the staged copy is evicted.
Detailed Description
Since #2403 prepare_params stages a compute copy of each LoRA target and its weight_scale (stage_tensors_to_compute_backend) and then merges the LoRA into that copy (apply_loras_to_params):
stable-diffusion.cpp/src/model_manager.cpp
Lines 2224 to 2245 in d68f288
| if (!load_tensors_to_params_backend(required_states)) { | |
| return false; | |
| } | |
| if (!stage_tensors_to_compute_backend(required_states) || | |
| (retain_weights_ && !ensure_host_capacity(0, required_states))) { | |
| const std::unordered_set<TensorState*> failed(required_states.begin(), required_states.end()); | |
| release_compute_staging_blocks(false, &failed); | |
| release_params_storage_blocks(false, &failed); | |
| return false; | |
| } | |
| // LoRA execution may reclaim other residency blocks while these weights are in use. | |
| const uint64_t use_epoch = ++residency_epoch_; | |
| for (TensorState* state : required_states) { | |
| if (state != nullptr) { | |
| state->pin_count++; | |
| state->last_use_epoch = use_epoch; | |
| } | |
| } | |
| if (!apply_loras_to_params(required_states)) { |
evictable_compute_backend_blocks keeps a storage block resident while any of its tensors has a staged copy. base_model and apply_lora(base_model, ...) are two resources that share the source storage (model manager reused 732 resident weight tensors), so the source also stays referenced by a resource that never runs. The source storage and the staged copies are therefore resident together: both on the device when the params live there, or the source in host memory and the copies on the device with params_backend = "cpu".
Reproduction
d68f2883c9, CUDA 12.8, RTX 5090 (32 GB), no other process on the GPU.
sd-cli --mode dgl --script repro.dgl --run-once off --log-level debug
repro.dgl:
fn pack_av(video, audio, audio_scale) {
shape = tensor::shape(video)
count = tensor::numel(audio)
tail_channels = math::ceil_div(count, shape[0] * shape[1] * shape[2])
tail_shape = [shape[0], shape[1], shape[2], tail_channels, 1]
flat = tensor::reshape(tensor::mul(audio, audio_scale), [count])
padded = tensor::pad(flat, [0, math::product(tail_shape) - count])
return tensor::concat([video, tensor::reshape(padded, tail_shape)], axis = 3)
}
fn main() {
base_model = load_dit("minimax_h3_ref2va_pruned_int8_convrot.safetensors", backend = "cuda0", family = "minimax_h3")
text = load_text_encoder("qwen3vl_32b_minimax_h3-Q4_K_M.gguf", backend = "cuda0", family = "minimax_h3")
vae = load_vae("minimax_h3_video_vae_int8_convrot.safetensors", backend = "cuda0", family = "minimax_h3")
audio_vae = load_audio_vae("minimax_h3_audio_vae_fp32.safetensors", backend = "cuda0", family = "minimax_h3")
adapter = load_lora("minimax_h3_ref2v_turbo_4step_v0.1_comfyui_bf16.safetensors")
model = apply_lora(base_model, adapter, weight = 1.0, mode = "immediately")
rng = make_rng("cuda", seed = 42)
video_base = tensor::zeros(latent_shape(vae, width = 1248, height = 704, frames = 124))
audio_shape = audio_latent_shape(audio_vae, frames = 124, fps = 24)
base = pack_av(video_base, tensor::zeros(audio_shape), audio_scale = 4.0)
positive = encode_prompt(text, "a woman walks through a rainy street at night", width = 1248, height = 704, clip_skip = 0)
pos = predictor(model, positive, guidance = 3.5, prediction = "auto", audio_length = audio_shape[0], fps = 24, video_sigma_shift = 12.0)
guided = cfg(pos, pos, scale = 1.0)
sigmas = make_sigmas(guided, latent = base, steps = 4, scheduler = "simple")
start = add_noise(guided, base, tensor::randn_like(base, rng = rng), sigma = sigmas[0])
latent = sample(start, sigmas, predict = guided, method = "euler", rng = rng, eta = 0.0)
return {latent = latent}
}
sample fails during weight preparation. The DiT source (20083 MiB) and the compute copies staged so far (9965 MiB) are resident together, and the next staging block does not fit:
[V] load_dit: 20083.17 MiB params -> CUDA0
[V] model manager prepared params backend buffers (20083.17 MB, 732 tensors, 22 blocks, VRAM) on CUDA0
[V] model manager reused 732 resident weight tensors
[W] model manager memory on CUDA0: reported free 779.00 MiB / total 32579.44 MiB, tracked weights 30047.79 MiB / other runtime 0.00 MiB / current runtime 0.00 MiB / idle buffer pool 0.00 MiB
[W] execution engine cannot make enough memory available on CUDA0: need 1431.32 MB device / 919.32 MB budget, available 779.00 MB device / unlimited budget
[D] model manager released compute params (9964.62 MB, 194 tensors, 11 blocks) from CUDA0
[E] minimax_h3 segment 1/1 (graph) failed during weight preparation
[E] diffusion compute failed
Through a persistent context
Measured at 52491bd9f1 with one sd_dgl_ctx_t kept across sd_dgl_run calls, the same models, a full Ref2VA job (one reference image, VAE decode), 4 steps:
| DiT params | first run | second run (new prompt) | host RSS |
|---|---|---|---|
| CUDA0 | fails as above | 94.2 s, 54 segments, LoRA applied 154 times | 1.7 GB |
params_backend = "cpu" |
86.8 s, 1 segment | 62.2 s, 1 segment | 24.5 GB |
With params_backend = "cpu" the job runs as one segment, and the source stays in pinned host memory for the life of the context.
With the source released after staging
A patch that stages every tensor of a resource with LoRA targets and frees each source storage block as soon as all of its tensors are staged, on d68f2883c9 with repro.dgl: sampling completes in 53.3 s as one segment, with the LoRA applied once.
At 52491bd9f1 through a persistent context, params on CUDA0:
| run | wall time | segments | host RSS |
|---|---|---|---|
| LoRA, first | 76.8 s | 1 | 1.8 GB |
| LoRA, new prompt | 61.3 s | 1 | 2.1 GB |
| no LoRA | 68.8 s | 1 | 1.2 GB |
| LoRA again | 69.9 s | 1 | 1.0 GB |
The frames are bit-identical to the params_backend = "cpu" runs.
Alternatives you considered
params_backend = "cpu"for the DiT: works, at the cost of a second copy of the params in pinned host memory.params_backend = "disk": the run fails with the same capacity error.mode = "at_runtime": the DiT is cut into 54 segments (the case feat: support immediate LoRA merges for INT8 weights #2403 addressed).- Baking the LoRA into the INT8 file offline: needs one file per LoRA combination.
Additional context
No response
Activity