From 1a6fee568c56fa8d3f784d0667608e88166de157 Mon Sep 17 00:00:00 2001 From: wOvAN Date: Wed, 5 Aug 2026 15:30:29 +0500 Subject: [PATCH] llama : add backends of the other model to the context For models that share tensors with another model through ctx_other (e.g. Gemma4Assistant, Eagle3, DFlash, DSpark), add the other model's device backends to the context scheduler so that pre-allocated shared tensors (tok_embd, output) can be scheduled. Fixes an abort when the speculative draft model uses a device list that does not cover the GPU holding the target model's output projection. Assisted-by: opencode --- src/llama-context.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 0de3a68d1cb0..3253af7fcaab 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -337,6 +337,32 @@ llama_context::llama_context( backends.emplace_back(backend); } + // add backends for the devices of the other model (if any) + // some models (e.g. Gemma4Assistant, Eagle3, DFlash, DSpark) share tensors + // with the target model through ctx_other, and those tensors may be + // pre-allocated on devices that are not part of this model's device list + if (cparams.ctx_other != nullptr) { + const llama_model * model_other = llama_get_model(cparams.ctx_other); + for (const auto & dev : model_other->devices) { + bool found = false; + for (const auto & dev_self : model.devices) { + if (dev_self.dev == dev.dev) { + found = true; + break; + } + } + if (!found) { + ggml_backend_t backend = ggml_backend_dev_init(dev.dev, nullptr); + if (backend == nullptr) { + throw std::runtime_error(format("failed to initialize %s backend", ggml_backend_dev_name(dev.dev))); + } + LLAMA_LOG_INFO("%s: adding backend for device %s: shared tensors with the other model (e.g. tok_embd, output)\n", + __func__, ggml_backend_dev_name(dev.dev)); + backends.emplace_back(backend); + } + } + } + // add ACCEL backends (such as BLAS) for (size_t i = 0; i < ggml_backend_dev_count(); ++i) { ggml_backend_dev_t dev = ggml_backend_dev_get(i);