mtmd: release/restore the encoder weights from VRAM on demand

Add clip_release_device/clip_restore_device (and mtmd_release_device/
mtmd_restore_device wrappers over the vision + audio contexts) that free
the multimodal encoder's device weight buffer to a read-only host shadow
and rebuild it on demand, using the same shadow/free/reallocate pattern
as llama_model weights. No-op for a CPU-backed encoder. This lets the
server drop the ~hundreds-of-MiB vision encoder from VRAM when it is not
encoding an image.

Assisted-by: Claude
This commit is contained in:
Lumpiasty committed 2026-11-22 20:38:32 +01:00
1 parent 392787dee9
commit f5c6f04b4e
4 files changed
+106

No files matched your search

+26
View File
@@ -1671,6 +1671,32 @@ std::vector<std::vector<const mtmd_bitmap *>> mtmd_group_mergeable_bitmaps(std::
return output;
}
void mtmd_release_device(mtmd_context * ctx) {
if (ctx == nullptr) {
return;
}
std::lock_guard<std::mutex> lock(ctx->compute_mutex);
// ensure no encode is in flight before freeing the weights
if (ctx->backends && ctx->backends->sched) {
ggml_backend_sched_synchronize(ctx->backends->sched.get());
}
for (auto * m : { ctx->models.v.get(), ctx->models.a.get(), ctx->models.gen_a.get() }) {
if (m) { mtmd_model_release_device(*m); }
}
}
bool mtmd_restore_device(mtmd_context * ctx) {
if (ctx == nullptr) {
return true;
}
std::lock_guard<std::mutex> lock(ctx->compute_mutex);
bool ok = true;
for (auto * m : { ctx->models.v.get(), ctx->models.a.get(), ctx->models.gen_a.get() }) {
if (m) { ok = mtmd_model_restore_device(*m) && ok; }
}
return ok;
}
struct mtmd_tokenizer {
const mtmd_context * ctx;