#include "ggml-openvino.h"
#include "ggml-backend-impl.h"
#include "ggml-backend.h"
#include "ggml-impl.h"
#include "ggml-openvino-extra.h"
#include "ggml-openvino/openvino/op_table.h"
#include "ggml-openvino/utils.h"
#include "ggml-quants.h"
#include "ggml.h"
#include "model-cache.h"
#include <algorithm>
#include <atomic>
#include <cerrno>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
#include <openvino/core/type/element_type.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/parameter.hpp>
#include <openvino/openvino.hpp>
#include <openvino/runtime/allocator.hpp>
#include <openvino/runtime/intel_gpu/ocl/ocl.hpp>
#include <openvino/runtime/intel_npu/level_zero/level_zero.hpp>
#include <openvino/runtime/properties.hpp>
#include <openvino/runtime/tensor.hpp>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# include <psapi.h>
#else
# include <sys/mman.h>
# include <unistd.h>
#endif
namespace {
struct ggml_backend_openvino_buffer_context {
int device;
std::string name;
size_t id;
void * data;
size_t size;
bool is_remote;
void * spill_mapping = nullptr;
size_t spill_size = 0;
bool external_memory = false;
bool kv_on_demand = false;
std::shared_ptr<ov::Tensor> ov_buffer;
std::map<ggml_tensor *, ggml_openvino_extra_base *> tensor_extras;
std::map<const void *, uint64_t> weight_fingerprints;
std::vector<ggml_openvino_source_mapping> source_mappings;
void * data_prev = nullptr;
bool weight_registered = false;
static size_t next_buffer_id() {
static std::atomic<size_t> next_id{1};
return next_id.fetch_add(1);
}
ggml_backend_openvino_buffer_context(int device, void * external_data, size_t size) :
device(device),
name(std::string(GGML_OPENVINO_NAME) + std::to_string(device)),
id(next_buffer_id()),
data(external_data),
size(size),
is_remote(false),
external_memory(true) {
ov_buffer = std::make_shared<ov::Tensor>(ov::element::u8, ov::Shape{size}, data);
}
ggml_backend_openvino_buffer_context(int device, size_t size, bool is_remote = false, bool lazy = false) :
device(device),
name(std::string(GGML_OPENVINO_NAME) + std::to_string(device)),
id(next_buffer_id()),
data(nullptr),
size(size),
is_remote(is_remote) {
if (size == 0) {
return;
}
const auto & device_name = ggml_openvino_get_available_devices()[device];
if (is_remote) {
GGML_ASSERT(ggml_openvino_is_gpu());
auto remote_context = ggml_openvino_get_remote_context(device_name);
auto gpu_context = remote_context->as<ov::intel_gpu::ocl::ClContext>();
ov::intel_gpu::ocl::USMTensor usm_tensor =
gpu_context.create_usm_device_tensor(ov::element::u8, ov::Shape{size});
data = usm_tensor.get();
ov_buffer = std::make_shared<ov::intel_gpu::ocl::USMTensor>(std::move(usm_tensor));
} else {
const bool strict_mmap = device_name == "NPU" &&
ggml_openvino_npu_raw_weight_cache_enabled() &&
ggml_openvino_getenv_int("GGML_OPENVINO_SELF_CONTAINED_MMAP") &&
ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR");
const size_t lazy_host_threshold =
(size_t) ggml_openvino_getenv_int("GGML_OPENVINO_LAZY_HOST_THRESHOLD_MB", 2048) * 1024 * 1024;
const bool lazy_host_alloc = lazy || (strict_mmap && size >= lazy_host_threshold);
if (!lazy_host_alloc && ggml_openvino_is_npu() && ggml_openvino_npu_l0_host_tensors_enabled() &&
!ggml_openvino_model_cache_only()) {
try {
auto npu_context = ov_singleton_core().get_default_context("NPU");
auto host_tensor = npu_context.create_host_tensor(ov::element::u8, ov::Shape{size});
data = host_tensor.data();
memset(data, 0, size);
ov_buffer = std::make_shared<ov::Tensor>(std::move(host_tensor));
} catch (const std::exception & e) {
GGML_LOG_WARN("%s: L0 host tensor allocation of %zu bytes failed (%s); using anonymous memory\n",
__func__, size, e.what());
data = nullptr;
}
}
if (data != nullptr) {
} else
#ifndef _WIN32
if (const char * spill_dir =
ggml_openvino_model_cache_only() ? nullptr : ggml_openvino_getenv_str("GGML_OPENVINO_SPILL_DIR")) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/ggml-ov-weights-%d-XXXXXX", spill_dir, (int) getpid());
int fd = mkstemp(path);
if (fd < 0) {
GGML_LOG_ERROR("%s: mkstemp(%s) failed: %s\n", __func__, path, strerror(errno));
return;
}
unlink(path); if (ftruncate(fd, (off_t) size) != 0) {
GGML_LOG_ERROR("%s: ftruncate(%zu) failed: %s\n", __func__, size, strerror(errno));
close(fd);
return;
}
void * m = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); if (m == MAP_FAILED) {
GGML_LOG_ERROR("%s: mmap(%zu) failed: %s\n", __func__, size, strerror(errno));
return;
}
data = m;
spill_mapping = m;
spill_size = size;
GGML_LOG_INFO("%s: weight buffer spilled to %s (%zu MB, file-backed)\n", __func__, spill_dir,
size / 1024 / 1024);
ov_buffer = std::make_shared<ov::Tensor>(ov::element::u8, ov::Shape{size}, data);
} else
#endif
{
#ifdef _WIN32
if (!ggml_openvino_model_cache_only() && ggml_openvino_getenv_str("GGML_OPENVINO_SPILL_DIR")) {
GGML_LOG_WARN("%s: GGML_OPENVINO_SPILL_DIR is not supported on Windows, ignoring\n", __func__);
}
#endif
#ifdef _WIN32
data = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (data == nullptr) {
return;
}
#else
void * m = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m == MAP_FAILED) {
return;
}
data = m;
#endif
spill_mapping = data;
spill_size = size;
ov_buffer = std::make_shared<ov::Tensor>(ov::element::u8, ov::Shape{size}, data);
}
}
if (data == nullptr) {
GGML_LOG_ERROR("%s: failed to allocate %zu bytes\n", __func__, size);
return;
}
if (reinterpret_cast<uintptr_t>(data) % TENSOR_ALIGNMENT != 0) {
GGML_LOG_ERROR("%s: %s buffer is not aligned to %d bytes\n", __func__, device_name.c_str(),
TENSOR_ALIGNMENT);
GGML_ABORT("fatal error");
}
}
~ggml_backend_openvino_buffer_context() {
for (auto & pair : tensor_extras) {
delete pair.second;
}
tensor_extras.clear();
if (weight_registered) {
ggml_openvino_unregister_weight_buffer(data);
}
if (kv_on_demand) {
ggml_openvino_kv_storage_unregister(data, size);
}
if (spill_mapping != nullptr) {
#ifdef _WIN32
VirtualFree(spill_mapping, 0, MEM_RELEASE);
#else
munmap(spill_mapping, spill_size);
#endif
}
}
};
struct ggml_backend_openvino_buffer_type_context {
int device;
std::string name;
};
}
namespace {
struct ov_weight_buffer_registry {
std::mutex mutex;
std::vector<std::pair<void *, size_t>> buffers;
bool released = false;
};
ov_weight_buffer_registry & ov_weight_registry() {
static ov_weight_buffer_registry reg;
return reg;
}
}
void ggml_openvino_register_weight_buffer(void * data, size_t size) {
if (data == nullptr || size == 0) {
return;
}
auto & reg = ov_weight_registry();
std::lock_guard<std::mutex> lock(reg.mutex);
for (const auto & b : reg.buffers) {
if (b.first == data) {
return; }
}
reg.buffers.emplace_back(data, size);
}
void ggml_openvino_unregister_weight_buffer(const void * data) {
if (data == nullptr) {
return;
}
auto & reg = ov_weight_registry();
std::lock_guard<std::mutex> lock(reg.mutex);
reg.buffers.erase(std::remove_if(reg.buffers.begin(), reg.buffers.end(),
[data](const std::pair<void *, size_t> & b) { return b.first == data; }),
reg.buffers.end());
}
bool ggml_openvino_weight_buffers_released() {
auto & reg = ov_weight_registry();
std::lock_guard<std::mutex> lock(reg.mutex);
return reg.released;
}
void ggml_openvino_release_weight_buffers() {
auto & reg = ov_weight_registry();
std::lock_guard<std::mutex> lock(reg.mutex);
if (reg.released) {
return;
}
size_t total = 0;
#if !defined(_WIN32)
for (const auto & b : reg.buffers) {
const size_t page = (size_t) sysconf(_SC_PAGESIZE);
const uintptr_t ustart = reinterpret_cast<uintptr_t>(b.first);
const size_t offset_to_page = (page - (ustart & (page - 1))) & (page - 1);
if (b.second > offset_to_page) {
const size_t aligned_len = (b.second - offset_to_page) & ~(page - 1);
if (aligned_len > 0) {
char * astart = static_cast<char *>(b.first) + offset_to_page;
if (madvise(astart, aligned_len, MADV_DONTNEED) == 0) {
total += aligned_len;
}
}
}
}
#endif
reg.released = true;
GGML_LOG_INFO("%s: released %zu MB of host weight buffers (%zu buffers)\n", __func__, total / 1024 / 1024,
reg.buffers.size());
}
static void ggml_backend_openvino_buffer_free_buffer(ggml_backend_buffer_t buffer) {
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
delete ctx;
}
static void * ggml_backend_openvino_buffer_get_base(ggml_backend_buffer_t buffer) {
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
return ctx->data;
}
static bool is_stateful_enabled() {
return ggml_openvino_getenv_int("GGML_OPENVINO_STATEFUL_EXECUTION") != 0;
}
static bool is_self_contained_mmap_enabled() {
return ggml_openvino_is_npu() && ggml_openvino_npu_raw_weight_cache_enabled() &&
ggml_openvino_getenv_int("GGML_OPENVINO_SELF_CONTAINED_MMAP") &&
ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR");
}
static void ggml_openvino_set_extra(ggml_backend_openvino_buffer_context * ctx,
ggml_tensor * tensor,
ggml_openvino_extra_base * extra) {
auto it = ctx->tensor_extras.find(tensor);
if (it != ctx->tensor_extras.end() && it->second != extra) {
delete it->second;
}
ctx->tensor_extras[tensor] = extra;
tensor->extra = extra;
}
static bool ggml_openvino_swap_buffer_context(ggml_backend_buffer_t buffer,
ggml_backend_openvino_buffer_context *& ctx,
ggml_tensor * tensor,
bool is_remote,
bool lazy) {
const int device = ctx->device;
const size_t size = ctx->size;
const size_t offset = (char *) tensor->data - (char *) ctx->data;
delete ctx;
ctx = new ggml_backend_openvino_buffer_context(device, size, is_remote, lazy);
buffer->context = ctx;
if (ctx->data == nullptr) {
return false;
}
tensor->data = (char *) ctx->data + offset;
return true;
}
static enum ggml_status ggml_backend_openvino_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) {
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
const bool is_device_state = strncmp(tensor->name, "cache_", 6) == 0 ||
strcmp(tensor->name, "layer_inp_dev") == 0 || strcmp(tensor->name, "h_nextn_dev") == 0;
if (is_device_state && !ctx->is_remote && ggml_openvino_is_gpu() && !is_stateful_enabled()) {
GGML_ASSERT(ctx->tensor_extras.empty());
if (!ggml_openvino_swap_buffer_context(buffer, ctx, tensor, true, false)) {
return GGML_STATUS_ALLOC_FAILED;
}
}
const bool is_kv_tensor = strncmp(tensor->name, "cache_k_l", 9) == 0 || strncmp(tensor->name, "cache_v_l", 9) == 0;
if (is_kv_tensor && tensor->view_src == nullptr && !ctx->is_remote && ggml_openvino_npu_kv_on_demand_enabled() &&
tensor->ne[2] == 1 && tensor->ne[3] == 1 && tensor->nb[1] == ggml_row_size(tensor->type, tensor->ne[0])) {
if (!ctx->kv_on_demand && ctx->tensor_extras.empty() && tensor->data == ctx->data) {
if (!ggml_openvino_swap_buffer_context(buffer, ctx, tensor, false, true)) {
return GGML_STATUS_ALLOC_FAILED;
}
ctx->kv_on_demand = true;
}
if (ctx->kv_on_demand) {
ggml_openvino_kv_storage_register(tensor->data, tensor->nb[1], tensor->ne[1]);
}
}
if (tensor->view_src != nullptr) {
GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
if (tensor->view_src->extra != nullptr) {
if (!ggml_are_same_shape(tensor, tensor->view_src) && ggml_is_contiguous(tensor) &&
!ggml_is_quantized(tensor->type) && tensor->data != nullptr && ggml_nbytes(tensor) > 0) {
if (ggml_openvino_tensor_extra * extra =
ggml_openvino_create_tensor_extra(tensor, ctx->is_remote,
ggml_openvino_get_available_devices()[ctx->device])) {
ggml_openvino_set_extra(ctx, tensor, extra);
return GGML_STATUS_SUCCESS;
}
}
tensor->extra = tensor->view_src->extra;
}
return GGML_STATUS_SUCCESS;
}
ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
if (tensor->data != nullptr && !ggml_is_quantized(tensor->type) && ggml_nbytes(tensor) > 0) {
ggml_openvino_tensor_extra * extra = ggml_openvino_create_tensor_extra(
tensor, ctx->is_remote, ggml_openvino_get_available_devices()[ctx->device]);
if (extra != nullptr) {
ggml_openvino_set_extra(ctx, tensor, extra);
}
}
return GGML_STATUS_SUCCESS;
}
static void ggml_backend_openvino_buffer_memset_tensor(ggml_backend_buffer_t buffer,
ggml_tensor * tensor,
uint8_t value,
size_t offset,
size_t size) {
GGML_ASSERT(tensor != nullptr && tensor->data != nullptr);
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue(ggml_openvino_get_available_devices()[ctx->device]);
auto mem_fill_fn = ggml_openvino_get_clEnqueueMemFillINTEL();
if (mem_fill_fn != nullptr) {
uint8_t pattern = value;
cl_int err = mem_fill_fn(queue, (char *) tensor->data + offset, &pattern,
sizeof(pattern), size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemFillINTEL failed with error %d\n", __func__, err);
}
clFinish(queue);
} else {
GGML_LOG_ERROR("%s: clEnqueueMemFillINTEL not available for GPU buffer\n", __func__);
}
} else {
if (!ctx->kv_on_demand ||
!ggml_openvino_kv_storage_copy((char *) tensor->data + offset, &value, size, GGML_OPENVINO_KV_FILL)) {
memset((char *) tensor->data + offset, value, size);
}
}
}
static bool ggml_openvino_extra_unchanged(const ggml_backend_openvino_buffer_context * ctx,
const ggml_tensor * tensor) {
auto it = ctx->tensor_extras.find(const_cast<ggml_tensor *>(tensor));
if (it == ctx->tensor_extras.end() || it->second == nullptr || tensor->extra != it->second ||
it->second->type != ggml_openvino_extra_base::Type::TENSOR) {
return false;
}
const auto * extra = static_cast<const ggml_openvino_tensor_extra *>(it->second);
const auto & t = extra->tensor;
if (extra->data != tensor->data || t == nullptr || t->get_element_type() != GgmlOvDecoder::get_ov_type(tensor)) {
return false;
}
const auto & shape = t->get_shape();
if (shape.size() != GGML_MAX_DIMS) {
return false;
}
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
if (shape[GGML_MAX_DIMS - 1 - i] != static_cast<size_t>(tensor->ne[i])) {
return false;
}
}
return true;
}
#ifdef _WIN32
static bool ggml_openvino_range_has_locked_pages(uintptr_t first, uintptr_t last, uintptr_t page) {
std::vector<PSAPI_WORKING_SET_EX_INFORMATION> pages(std::min<size_t>((last - first) / page, 4096));
for (uintptr_t addr = first; addr < last;) {
const size_t n = std::min<size_t>(pages.size(), (last - addr) / page);
for (size_t i = 0; i < n; i++) {
pages[i].VirtualAddress = reinterpret_cast<void *>(addr + i * page);
}
if (!QueryWorkingSetEx(GetCurrentProcess(), pages.data(), DWORD(n * sizeof(pages[0])))) {
return true; }
for (size_t i = 0; i < n; i++) {
if (pages[i].VirtualAttributes.Valid && pages[i].VirtualAttributes.Locked) {
return true;
}
}
addr += n * page;
}
return false;
}
#endif
static void ggml_openvino_release_source_pages(ggml_backend_openvino_buffer_context * ctx, const void * data, size_t size) {
const ggml_openvino_source_mapping * mapping = ggml_openvino_find_source_mapping(data, size, ctx->source_mappings);
if (mapping == nullptr || !mapping->read_only) {
return;
}
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo(&info);
const uintptr_t page = info.dwPageSize;
#else
const uintptr_t page = (uintptr_t) sysconf(_SC_PAGESIZE);
#endif
const uintptr_t first = ((uintptr_t) data + page - 1) & ~(page - 1);
const uintptr_t last = ((uintptr_t) data + size) & ~(page - 1);
if (last <= first) {
return;
}
#ifdef _WIN32
if (!ggml_openvino_range_has_locked_pages(first, last, page)) {
VirtualUnlock(reinterpret_cast<void *>(first), last - first);
}
#elif defined(__linux__)
madvise(reinterpret_cast<void *>(first), last - first, MADV_DONTNEED);
#endif
}
static void ggml_backend_openvino_buffer_set_tensor(ggml_backend_buffer_t buffer,
ggml_tensor * tensor,
const void * data,
size_t offset,
size_t size) {
GGML_ASSERT(tensor != nullptr && tensor->data != nullptr);
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
bool is_weight_buffer = (buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
bool is_full_tensor_set = (offset == 0 && size == ggml_nbytes(tensor) && tensor->view_src == nullptr);
if (is_weight_buffer && ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR")) {
if (is_full_tensor_set) {
ctx->weight_fingerprints[tensor->data] = ggml_openvino_source_fingerprint(data, size, ctx->source_mappings);
}
if (ggml_openvino_model_cache_only()) {
if (!is_full_tensor_set) {
GGML_ABORT("ggml-openvino: cache-only mode requires whole mmap weight uploads");
}
return;
}
}
bool is_2d = (tensor->ne[2] == 1 && tensor->ne[3] == 1);
bool is_supported_weight_shape = is_2d || (tensor->ne[3] == 1 && ggml_is_quantized(tensor->type));
if (is_weight_buffer && is_full_tensor_set && is_supported_weight_shape) {
if (ggml_openvino_is_npu() && ggml_openvino_npu_raw_weight_cache_enabled() &&
ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR")) {
memcpy(tensor->data, data, size);
return;
}
try {
auto result = process_weight_tensor(tensor, data, tensor->data);
result.weight_node->set_friendly_name(tensor->name);
ggml_openvino_extra_base * extra;
if (result.is_quantized()) {
extra = new ggml_openvino_quantized_weight_extra(std::move(result.weights), std::move(result.scales),
std::move(result.zp), result.weight_node);
} else {
extra = new ggml_openvino_weight_extra(std::move(result.weights), result.weight_node);
}
ggml_openvino_set_extra(ctx, tensor, extra);
if (!ctx->is_remote) {
if (ggml_openvino_weight_buffers_released()) {
GGML_ABORT(
"ggml-openvino: loading a new model while GGML_OPENVINO_RELEASE_WEIGHTS pinned a previous "
"model's compiled graph. This mode supports a single model per process; unset it for "
"multi-model runs.");
}
ggml_openvino_register_weight_buffer(ctx->data, ctx->size);
ctx->weight_registered = true;
}
if (!ctx->external_memory) {
ggml_openvino_release_source_pages(ctx, data, size);
}
} catch (const std::exception & e) {
GGML_LOG_ERROR("%s: failed to process weight tensor for %s: %s\n", __func__, tensor->name, e.what());
memcpy((char *) tensor->data + offset, data, size);
}
} else {
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue(ggml_openvino_get_available_devices()[ctx->device]);
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (mem_cpy_fn != nullptr) {
cl_int err = mem_cpy_fn(queue, CL_TRUE, (char *) tensor->data + offset, data, size,
0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL failed with error %d\n", __func__, err);
}
} else {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
}
} else if (!ctx->kv_on_demand || !ggml_openvino_kv_storage_copy((char *) tensor->data + offset,
const_cast<void *>(data), size,
GGML_OPENVINO_KV_WRITE)) {
memcpy((char *) tensor->data + offset, data, size);
}
if (ggml_openvino_extra_unchanged(ctx, tensor)) {
return;
}
ggml_openvino_tensor_extra * extra = ggml_openvino_create_tensor_extra(
tensor, ctx->is_remote, ggml_openvino_get_available_devices()[ctx->device]);
if (extra == nullptr) {
return;
}
ggml_openvino_set_extra(ctx, tensor, extra);
}
}
static ov::Tensor ggml_openvino_eval_weight_node(const ov::Output<ov::Node> & output) {
const auto node = output.get_node_shared_ptr();
if (const auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node)) {
return constant->get_tensor_view();
}
ov::TensorVector inputs;
for (const auto & input : node->input_values()) {
inputs.push_back(ggml_openvino_eval_weight_node(input));
}
ov::TensorVector outputs;
for (size_t i = 0; i < node->get_output_size(); i++) {
outputs.emplace_back(node->get_output_element_type(i), node->get_output_shape(i));
}
if (!node->evaluate(outputs, inputs)) {
OPENVINO_THROW("cannot evaluate ", node->get_type_name());
}
return outputs[output.get_index()];
}
static bool ggml_openvino_weight_to_ggml(const ggml_tensor * tensor, std::vector<uint8_t> & out) {
const auto * extra = static_cast<const ggml_openvino_extra_base *>(tensor->extra);
std::shared_ptr<ov::Node> weight_node;
if (extra->type == ggml_openvino_extra_base::Type::QUANTIZED_WEIGHT) {
weight_node = static_cast<const ggml_openvino_quantized_weight_extra *>(extra)->weight_node;
} else if (extra->type == ggml_openvino_extra_base::Type::WEIGHT) {
weight_node = static_cast<const ggml_openvino_weight_extra *>(extra)->weight_node;
}
if (weight_node == nullptr || ggml_quantize_requires_imatrix(tensor->type)) {
return false;
}
try {
ov::Tensor values = ggml_openvino_eval_weight_node(weight_node->output(0));
if (values.get_size() != (size_t) ggml_nelements(tensor)) {
return false;
}
if (values.get_element_type() != ov::element::f32) {
auto param = std::make_shared<ov::op::v0::Parameter>(values.get_element_type(), values.get_shape());
auto convert = std::make_shared<ov::op::v0::Convert>(param, ov::element::f32);
ov::TensorVector f32{ov::Tensor(ov::element::f32, values.get_shape())};
if (!convert->evaluate(f32, {values})) {
return false;
}
values = f32[0];
}
out.resize(ggml_nbytes(tensor));
ggml_quantize_chunk(tensor->type, values.data<float>(), out.data(), 0, ggml_nrows(tensor), tensor->ne[0],
nullptr);
return true;
} catch (const std::exception & e) {
GGML_LOG_WARN("%s: cannot rebuild %s: %s\n", __func__, tensor->name, e.what());
return false;
}
}
static void ggml_backend_openvino_buffer_get_tensor(ggml_backend_buffer_t buffer,
const ggml_tensor * tensor,
void * data,
size_t offset,
size_t size) {
GGML_ASSERT(tensor != nullptr && tensor->data != nullptr);
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
if (ggml_openvino_model_cache_only() && buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
GGML_ABORT("ggml-openvino: cannot read unloaded weights in cache-only mode");
}
if (ggml_is_quantized(tensor->type) && tensor->view_src == nullptr && tensor->extra != nullptr &&
static_cast<const ggml_openvino_extra_base *>(tensor->extra)->type != ggml_openvino_extra_base::Type::TENSOR) {
std::vector<uint8_t> rebuilt;
if (ggml_openvino_weight_to_ggml(tensor, rebuilt)) {
memcpy(data, rebuilt.data() + offset, size);
return;
}
}
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue(ggml_openvino_get_available_devices()[ctx->device]);
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (mem_cpy_fn != nullptr) {
cl_int err = mem_cpy_fn(queue, CL_TRUE, data, (const char *) tensor->data + offset,
size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL failed with error %d\n", __func__, err);
}
} else {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
}
} else if (!ctx->kv_on_demand ||
!ggml_openvino_kv_storage_copy((const char *) tensor->data + offset, data, size, GGML_OPENVINO_KV_READ)) {
memcpy(data, (const char *) tensor->data + offset, size);
}
}
static bool ggml_backend_openvino_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
const ggml_tensor * src,
ggml_tensor * dst) {
GGML_ASSERT(src != nullptr && dst != nullptr);
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue(ggml_openvino_get_available_devices()[ctx->device]);
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (mem_cpy_fn == nullptr) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
return false;
}
if (ggml_backend_buffer_is_host(src->buffer)) {
cl_int err = mem_cpy_fn(queue, CL_TRUE, dst->data,
src->data, ggml_nbytes(src), 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL (host-to-device) failed with error %d\n", __func__, err);
return false;
}
return true;
}
if (ggml_backend_buffer_is_openvino(src->buffer)) {
ggml_backend_openvino_buffer_context * src_ctx =
(ggml_backend_openvino_buffer_context *) src->buffer->context;
if (src_ctx->is_remote && src_ctx->device == ctx->device) {
cl_int err = mem_cpy_fn(queue, CL_TRUE, dst->data,
src->data, ggml_nbytes(src),
0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL (device-to-device) failed with error %d\n", __func__, err);
return false;
}
return true;
}
}
return false;
}
if (ggml_backend_buffer_is_host(src->buffer)) {
const size_t n = ggml_nbytes(src);
std::vector<uint8_t> staged;
const void * from = src->data;
if (ggml_openvino_kv_storage_copy(src->data, nullptr, 0, GGML_OPENVINO_KV_READ)) {
staged.resize(n);
ggml_openvino_kv_storage_copy(src->data, staged.data(), n, GGML_OPENVINO_KV_READ);
from = staged.data();
}
if (!ggml_openvino_kv_storage_copy(dst->data, const_cast<void *>(from), n, GGML_OPENVINO_KV_WRITE)) {
memcpy(dst->data, from, n);
}
return true;
}
return false;
}
static void ggml_backend_openvino_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
GGML_ASSERT(ctx->data != nullptr);
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue(ggml_openvino_get_available_devices()[ctx->device]);
auto mem_fill_fn = ggml_openvino_get_clEnqueueMemFillINTEL();
if (mem_fill_fn != nullptr) {
uint8_t pattern = value;
cl_int err = mem_fill_fn(queue, ctx->data, &pattern, sizeof(pattern), ctx->size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_WARN("%s: clEnqueueMemFillINTEL failed with error %d\n", __func__, err);
}
clFinish(queue);
} else {
GGML_LOG_WARN("%s: clEnqueueMemFillINTEL not available for GPU buffer clear\n", __func__);
}
} else if (ctx->kv_on_demand) {
ggml_openvino_kv_storage_clear(ctx->data, ctx->size, value);
} else {
memset(ctx->data, value, ctx->size);
}
}
static const ggml_backend_buffer_i ggml_backend_openvino_buffer_interface = {
ggml_backend_openvino_buffer_free_buffer,
ggml_backend_openvino_buffer_get_base,
ggml_backend_openvino_buffer_init_tensor,
ggml_backend_openvino_buffer_memset_tensor,
ggml_backend_openvino_buffer_set_tensor,
ggml_backend_openvino_buffer_get_tensor,
NULL,
NULL,
ggml_backend_openvino_buffer_cpy_tensor,
ggml_backend_openvino_buffer_clear,
NULL,
NULL,
NULL,
NULL,
};
static const char * ggml_backend_openvino_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
ggml_backend_openvino_buffer_type_context * ctx = (ggml_backend_openvino_buffer_type_context *) buft->context;
return ctx->name.c_str();
}
static ggml_backend_buffer_t ggml_backend_openvino_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
size_t size) {
ggml_backend_openvino_buffer_type_context * buft_ctx = (ggml_backend_openvino_buffer_type_context *) buft->context;
ggml_backend_openvino_buffer_context * ctx = new ggml_backend_openvino_buffer_context(buft_ctx->device, size);
if (ctx->data == nullptr && size > 0) {
GGML_LOG_ERROR("%s: failed to allocate buffer of size %zu\n", __func__, size);
delete ctx;
return nullptr;
}
return ggml_backend_buffer_init(buft, ggml_backend_openvino_buffer_interface, ctx, size);
}
static size_t ggml_backend_openvino_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
GGML_UNUSED(buft);
return TENSOR_ALIGNMENT;
}
static size_t ggml_backend_openvino_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
ggml_backend_openvino_buffer_type_context * buft_ctx = (ggml_backend_openvino_buffer_type_context *) buft->context;
return ggml_openvino_max_alloc_size(ggml_openvino_get_available_devices()[buft_ctx->device]);
}
static size_t ggml_backend_openvino_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft,
const ggml_tensor * tensor) {
GGML_UNUSED(buft);
if (is_self_contained_mmap_enabled()) {
return ggml_nbytes(tensor);
}
if (!ggml_openvino_model_cache_only() && ggml_is_quantized(tensor->type) && tensor->ne[3] == 1) {
ggml_openvino_extracted_layout layout = ggml_openvino_get_extracted_layout(tensor);
if (layout.total_size > 0) {
return layout.total_size;
}
}
return ggml_nbytes(tensor);
}
static const ggml_backend_buffer_type_i ggml_backend_openvino_buffer_type_interface = {
ggml_backend_openvino_buffer_type_get_name,
ggml_backend_openvino_buffer_type_alloc_buffer,
NULL,
ggml_backend_openvino_buffer_type_get_alignment,
ggml_backend_openvino_buffer_type_get_max_size,
ggml_backend_openvino_buffer_type_get_alloc_size,
NULL,
nullptr,
};
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_openvino_buffer_type(int device) {
GGML_ASSERT(device >= 0 && device < ggml_backend_openvino_get_device_count());
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::vector<ggml_backend_buffer_type> buffer_types;
static std::vector<ggml_backend_openvino_buffer_type_context> buffer_type_contexts;
if (buffer_types.empty()) {
int device_count = ggml_backend_openvino_get_device_count();
buffer_types.resize(device_count);
buffer_type_contexts.resize(device_count);
for (int i = 0; i < device_count; i++) {
buffer_type_contexts[i].device = i;
buffer_type_contexts[i].name = std::string(GGML_OPENVINO_NAME) + std::to_string(i);
buffer_types[i] = ggml_backend_buffer_type{
ggml_backend_openvino_buffer_type_interface,
ggml_backend_reg_dev_get(ggml_backend_openvino_reg(), i),
&buffer_type_contexts[i],
};
}
}
return &buffer_types[device];
}
static const char * ggml_backend_openvino_host_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
ggml_backend_openvino_buffer_type_context * ctx = (ggml_backend_openvino_buffer_type_context *) buft->context;
return ctx->name.c_str();
}
static bool ggml_backend_openvino_host_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
GGML_UNUSED(buft);
return true;
}
static const ggml_backend_buffer_type_i ggml_backend_openvino_host_buffer_type_interface = {
ggml_backend_openvino_host_buffer_type_get_name,
ggml_backend_openvino_buffer_type_alloc_buffer,
NULL,
ggml_backend_openvino_buffer_type_get_alignment,
ggml_backend_openvino_buffer_type_get_max_size,
ggml_backend_openvino_buffer_type_get_alloc_size,
NULL,
ggml_backend_openvino_host_buffer_type_is_host,
};
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_openvino_host_buffer_type(int device) {
GGML_ASSERT(device >= 0 && device < ggml_backend_openvino_get_device_count());
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
static std::vector<ggml_backend_buffer_type> buffer_types;
static std::vector<ggml_backend_openvino_buffer_type_context> buffer_type_contexts;
if (buffer_types.empty()) {
int device_count = ggml_backend_openvino_get_device_count();
buffer_types.resize(device_count);
buffer_type_contexts.resize(device_count);
for (int i = 0; i < device_count; i++) {
buffer_type_contexts[i].device = i;
buffer_type_contexts[i].name = std::string(GGML_OPENVINO_NAME) + std::to_string(i) + "_HOST";
buffer_types[i] = ggml_backend_buffer_type{
ggml_backend_openvino_host_buffer_type_interface,
ggml_backend_reg_dev_get(ggml_backend_openvino_reg(), i),
&buffer_type_contexts[i],
};
}
}
return &buffer_types[device];
}
bool ggml_backend_buffer_is_openvino(ggml_backend_buffer_t buffer) {
return buffer->iface.free_buffer == ggml_backend_openvino_buffer_free_buffer;
}
size_t ggml_backend_openvino_buffer_get_ctx_id(ggml_backend_buffer_t buffer) {
if (!ggml_backend_buffer_is_openvino(buffer)) {
return 0;
}
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
return ctx->id;
}
bool ggml_openvino_buffer_is_remote(const ggml_tensor * tensor) {
if (tensor == nullptr || tensor->buffer == nullptr) {
return false;
}
if (!ggml_backend_buffer_is_openvino(tensor->buffer)) {
return false;
}
auto * ctx = static_cast<ggml_backend_openvino_buffer_context *>(tensor->buffer->context);
return ctx->is_remote;
}
void ggml_openvino_buffer_register_extra(ggml_tensor * tensor, ggml_openvino_extra_base * extra) {
GGML_ASSERT(tensor != nullptr);
GGML_ASSERT(tensor->buffer != nullptr);
GGML_ASSERT(ggml_backend_buffer_is_openvino(tensor->buffer));
ggml_openvino_set_extra(static_cast<ggml_backend_openvino_buffer_context *>(tensor->buffer->context), tensor,
extra);
}
bool ggml_backend_buft_is_openvino(ggml_backend_buffer_type_t buft) {
return buft->iface.get_name == ggml_backend_openvino_buffer_type_get_name;
}
bool ggml_backend_buft_is_openvino_host(ggml_backend_buffer_type_t buft) {
return buft->iface.get_name == ggml_backend_openvino_host_buffer_type_get_name;
}
static std::mutex g_fallback_fingerprints_mutex;
static std::map<std::tuple<const void *, size_t, std::string>, uint64_t> g_fallback_fingerprints;
uint64_t ggml_backend_openvino_weight_fingerprint(const ggml_tensor * tensor) {
if (ggml_backend_buffer_is_openvino(tensor->buffer)) {
auto * ctx = static_cast<ggml_backend_openvino_buffer_context *>(tensor->buffer->context);
auto it = ctx->weight_fingerprints.find(tensor->data);
if (it != ctx->weight_fingerprints.end()) {
return it->second;
}
if (ctx->external_memory) {
const uint64_t fp = ggml_openvino_source_fingerprint(tensor->data, ggml_nbytes(tensor), ctx->source_mappings);
ctx->weight_fingerprints[tensor->data] = fp;
return fp;
}
GGML_ABORT("ggml-openvino: missing source identity for weight %s", tensor->name);
}
const auto key =
std::make_tuple(static_cast<const void *>(tensor->data), ggml_nbytes(tensor), std::string(tensor->name));
std::lock_guard<std::mutex> lock(g_fallback_fingerprints_mutex);
auto it = g_fallback_fingerprints.find(key);
if (it != g_fallback_fingerprints.end()) {
return it->second;
}
std::vector<ggml_openvino_source_mapping> mappings;
return g_fallback_fingerprints[key] = ggml_openvino_source_fingerprint(tensor->data, ggml_nbytes(tensor), mappings);
}
static void ggml_backend_openvino_free(ggml_backend_t backend) {
ggml_backend_openvino_context * ctx = (ggml_backend_openvino_context *) backend->context;
if (ctx->runtime_context) {
auto r_ctx = std::static_pointer_cast<ov_runtime_context>(ctx->runtime_context);
auto cache = r_ctx->compiled_cache;
r_ctx->clear_caches();
std::lock_guard<std::mutex> cache_lock(cache->mutex);
if (--cache->backend_count == 0) {
if (!ggml_openvino_weight_buffers_released()) {
cache->graphs.clear();
ov_clear_imported_blobs();
std::lock_guard<std::mutex> fingerprints_lock(g_fallback_fingerprints_mutex);
g_fallback_fingerprints.clear();
}
}
}
delete ctx;
delete backend;
}
static const char * ggml_backend_openvino_get_name(ggml_backend_t backend) {
return GGML_OPENVINO_NAME;
GGML_UNUSED(backend);
}
static enum ggml_status ggml_backend_openvino_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
return ov_graph_compute(cgraph, backend);
GGML_UNUSED(backend);
}
static const ggml_backend_i ggml_backend_openvino_interface = {
ggml_backend_openvino_get_name,
ggml_backend_openvino_free,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
ggml_backend_openvino_graph_compute,
NULL,
NULL,
NULL,
};
int ggml_backend_openvino_get_device_count() {
return (int) ggml_openvino_get_available_devices().size();
}
static ggml_guid_t ggml_backend_openvino_guid(void) {
static ggml_guid guid = {0x12, 0xa8, 0xae, 0xf4, 0xc0, 0x1e, 0x61, 0x97,
0x8f, 0xeb, 0x33, 0x04, 0xa1, 0x33, 0x51, 0x2d};
return &guid;
}
static std::shared_ptr<ov_runtime_context> get_ov_runtime_context_ptr(int device) {
static auto cache = std::make_shared<ov_compiled_model_cache>();
auto r_ctx = std::make_shared<ov_runtime_context>();
r_ctx->device = ggml_openvino_get_available_devices()[device];
r_ctx->stateful = is_stateful_enabled() && !ggml_openvino_is_npu();
r_ctx->compiled_cache = cache;
std::lock_guard<std::mutex> cache_lock(cache->mutex);
++cache->backend_count;
return r_ctx;
}
GGML_BACKEND_API ggml_backend_t ggml_backend_openvino_init(int device) {
if (device < 0 || device >= ggml_backend_openvino_get_device_count()) {
GGML_LOG_ERROR("%s: invalid device %d\n", __func__, device);
return nullptr;
}
ggml_backend_openvino_context * ctx = new ggml_backend_openvino_context;
if (ctx == nullptr) {
GGML_LOG_ERROR("%s: failed to allocate context\n", __func__);
return nullptr;
}
ctx->runtime_context = get_ov_runtime_context_ptr(device);
if (ctx->runtime_context == nullptr) {
GGML_LOG_ERROR("%s: failed to allocate runtime context\n", __func__);
delete ctx;
return nullptr;
}
ggml_backend_t openvino_backend = new ggml_backend{
ggml_backend_openvino_guid(),
ggml_backend_openvino_interface,
ggml_backend_reg_dev_get(ggml_backend_openvino_reg(), device),
ctx,
};
return openvino_backend;
}
GGML_BACKEND_API bool ggml_backend_is_openvino(ggml_backend_t backend) {
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_openvino_guid());
}
namespace {
struct ggml_backend_openvino_device_context {
int device;
std::string name;
std::string ov_name; std::string description;
size_t total_memory;
};
}
static bool ov_device_has_prefix(const std::string & s, const std::string & prefix) {
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
}
static bool ov_try_get_size_t_property(const std::string & device, const std::string & property, size_t & out) {
try {
const ov::Any value = ov_singleton_core().get_property(device, property);
if (value.is<size_t>()) {
out = value.as<size_t>();
return true;
}
if (value.is<uint64_t>()) {
out = (size_t) value.as<uint64_t>();
return true;
}
if (value.is<unsigned long long>()) {
out = (size_t) value.as<unsigned long long>();
return true;
}
if (value.is<int64_t>()) {
const int64_t v = value.as<int64_t>();
if (v >= 0) {
out = (size_t) v;
return true;
}
}
} catch (...) {
}
return false;
}
static size_t ov_system_available_memory() {
#ifdef _WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (GlobalMemoryStatusEx(&status)) {
return (size_t) status.ullAvailPhys;
}
#else
if (FILE * f = fopen("/proc/meminfo", "r")) {
char line[256];
unsigned long long kb = 0;
bool found = false;
while (!found && fgets(line, sizeof(line), f)) {
found = sscanf(line, "MemAvailable: %llu kB", &kb) == 1;
}
fclose(f);
if (found) {
return (size_t) std::min<unsigned long long>(kb * 1024, SIZE_MAX);
}
}
#endif
return SIZE_MAX;
}
static bool ov_device_shares_system_memory(const std::string & device) {
if (ov_device_has_prefix(device, "NPU")) {
return true;
}
if (!ov_device_has_prefix(device, "GPU")) {
return false;
}
try {
return ov_singleton_core().get_property(device, ov::device::type) == ov::device::Type::INTEGRATED;
} catch (...) {
return false;
}
}
static bool ov_gpu_stat_is_host_memory(const std::string & key) {
return key == "usm_host" || key == "usm_shared";
}
static bool ov_try_get_gpu_used_memory(const std::string & device, size_t & out) {
out = 0;
try {
const ov::Any stats_any = ov_singleton_core().get_property(device, "GPU_MEMORY_STATISTICS");
if (stats_any.is<std::map<std::string, uint64_t>>()) {
const auto stats = stats_any.as<std::map<std::string, uint64_t>>();
for (const auto & kv : stats) {
if (!ov_gpu_stat_is_host_memory(kv.first)) {
out += (size_t) kv.second;
}
}
return true;
}
if (stats_any.is<ov::AnyMap>()) {
const auto stats = stats_any.as<ov::AnyMap>();
for (const auto & kv : stats) {
if (ov_gpu_stat_is_host_memory(kv.first)) {
continue;
}
if (kv.second.is<size_t>()) {
out += kv.second.as<size_t>();
} else if (kv.second.is<uint64_t>()) {
out += (size_t) kv.second.as<uint64_t>();
} else if (kv.second.is<unsigned long long>()) {
out += (size_t) kv.second.as<unsigned long long>();
}
}
return true;
}
} catch (...) {
}
return false;
}
static const char * ggml_backend_openvino_device_get_name(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ctx->name.c_str();
}
static const char * ggml_backend_openvino_device_get_description(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ctx->description.c_str();
}
static void ggml_backend_openvino_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
size_t used = 0;
const bool known = ctx->total_memory > 0 &&
(ov_device_has_prefix(ctx->ov_name, "GPU") ?
ov_try_get_gpu_used_memory(ctx->ov_name, used) :
ov_try_get_size_t_property(ctx->ov_name, "NPU_DEVICE_ALLOC_MEM_SIZE", used));
if (known) {
*total = ctx->total_memory;
*free = (used >= *total) ? 0 : (*total - used);
} else {
#ifdef _WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
*total = status.ullTotalPhys;
*free = status.ullAvailPhys;
#else
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
*total = pages * page_size;
*free = *total;
#endif }
if (ov_device_shares_system_memory(ctx->ov_name)) {
*free = std::min(*free, ov_system_available_memory());
}
}
static enum ggml_backend_dev_type ggml_backend_openvino_device_get_type(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ggml_openvino_is_selected_device(ctx->ov_name) ? GGML_BACKEND_DEVICE_TYPE_GPU : GGML_BACKEND_DEVICE_TYPE_IGPU;
}
static void ggml_backend_openvino_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
props->name = ggml_backend_openvino_device_get_name(dev);
props->description = ggml_backend_openvino_device_get_description(dev);
props->type = ggml_backend_openvino_device_get_type(dev);
ggml_backend_openvino_device_get_memory(dev, &props->memory_free, &props->memory_total);
props->caps = {
false,
false,
is_self_contained_mmap_enabled(),
false,
true,
false,
};
}
static ggml_backend_t ggml_backend_openvino_device_init(ggml_backend_dev_t dev, const char * params) {
GGML_UNUSED(params);
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
if (!ggml_openvino_is_selected_device(ctx->ov_name)) {
GGML_LOG_WARN("%s: %s (OpenVINO %s) is not the selected device, no ops will run on it; "
"set GGML_OPENVINO_DEVICE=%s to use it\n",
__func__, ctx->name.c_str(), ctx->ov_name.c_str(), ctx->ov_name.c_str());
}
return ggml_backend_openvino_init(ctx->device);
}
static ggml_backend_buffer_type_t ggml_backend_openvino_device_get_buffer_type(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ggml_backend_openvino_buffer_type(ctx->device);
}
static ggml_backend_buffer_type_t ggml_backend_openvino_device_get_host_buffer_type(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ggml_backend_openvino_host_buffer_type(ctx->device);
}
static ggml_backend_buffer_t ggml_backend_openvino_device_buffer_from_host_ptr(ggml_backend_dev_t dev,
void * ptr,
size_t size,
size_t max_tensor_size) {
auto * dev_ctx = static_cast<ggml_backend_openvino_device_context *>(dev->context);
auto * ctx = new ggml_backend_openvino_buffer_context(dev_ctx->device, ptr, size);
GGML_UNUSED(max_tensor_size);
return ggml_backend_buffer_init(ggml_backend_openvino_buffer_type(dev_ctx->device),
ggml_backend_openvino_buffer_interface, ctx, size);
}
static bool has_view_op_input(const ggml_tensor * op) {
for (int i = 0; i < GGML_MAX_SRC; i++) {
if (op->src[i] == nullptr) {
break;
}
if (op->src[i]->op == GGML_OP_VIEW) {
return true;
}
}
return false;
}
static bool has_strides_on_element_grid(const ggml_tensor * t) {
if (ggml_nelements(t) == 0) {
return true;
}
std::vector<size_t> strides;
for (int i = 0; i < GGML_MAX_DIMS; i++) {
if (t->ne[i] > 1) {
strides.push_back(t->nb[i]);
}
}
std::sort(strides.begin(), strides.end());
for (size_t i = 1; i < strides.size(); i++) {
if (strides[i - 1] == 0 || strides[i] % strides[i - 1] != 0) {
return false;
}
}
return true;
}
static bool is_row_span_view(const ggml_tensor * t) {
if (ggml_openvino_is_static() || is_stateful_enabled()) {
return false;
}
const ggml_tensor * src = t->op == GGML_OP_VIEW ? t->src[0] : nullptr;
if (src == nullptr || src->op == GGML_OP_VIEW || !ggml_is_contiguous(src) || t->type != src->type) {
return false;
}
const size_t es = ggml_type_size(t->type);
int d = 0;
size_t span = 1;
while (d < GGML_MAX_DIMS && t->nb[d] == span * es) {
span *= t->ne[d];
d++;
}
if (d < 2) {
return false;
}
for (int s = 1; d < GGML_MAX_DIMS; ++d, ++s) {
if (t->ne[d] != src->ne[s] || t->nb[d] != src->nb[s]) {
return false;
}
}
return t->view_offs % es == 0 && t->view_offs / es + span <= (size_t) src->ne[0];
}
static bool has_non_contiguous_view_input(const ggml_tensor * op) {
for (int i = 0; i < GGML_MAX_SRC; i++) {
if (op->src[i] == nullptr) {
break;
}
if (op->src[i]->op == GGML_OP_VIEW && !ggml_is_contiguous(op->src[i])) {
return true;
}
}
return false;
}
static bool is_supported_flash_attn_pattern(const ggml_tensor * op) {
for (int i = 0; i < 3; i++) {
const ggml_tensor * src = op->src[i];
if (src->op == GGML_OP_PERMUTE) {
if (src->src[0] == nullptr) {
return false;
}
if (i == 0 && src->src[0]->op == GGML_OP_CONCAT) {
continue;
}
if (i == 2 && src->src[0]->op == GGML_OP_VIEW && src->src[0]->src[0] != nullptr &&
src->src[0]->src[0]->op == GGML_OP_VIEW && src->src[0]->src[0]->src[0] != nullptr &&
src->src[0]->src[0]->src[0]->view_src == nullptr) {
continue;
}
if (src->src[0]->op != GGML_OP_VIEW && src->src[0]->op != GGML_OP_RESHAPE) {
return false;
}
const ggml_tensor * base = src->src[0]->src[0];
if (base != nullptr && src->src[0]->op == GGML_OP_VIEW && base->op == GGML_OP_RESHAPE) {
base = base->src[0];
}
if (base == nullptr || base->view_src != nullptr) {
return false;
}
} else if (src->op == GGML_OP_VIEW) {
if (src->src[0] == nullptr || src->src[0]->view_src != nullptr) {
return false;
}
} else if (src->op == GGML_OP_CPY) {
if (src->src[0] == nullptr || src->src[0]->op != GGML_OP_PERMUTE || src->src[0]->src[0] == nullptr) {
return false;
}
} else {
return false;
}
}
return true;
}
static bool is_gemma3n_flash_attn_pattern(const ggml_tensor * op) {
if (!is_supported_flash_attn_pattern(op)) {
return false;
}
const ggml_tensor * q_base =
op->src[0] != nullptr && op->src[0]->src[0] != nullptr ? op->src[0]->src[0]->src[0] : nullptr;
const ggml_tensor * k_base =
op->src[1] != nullptr && op->src[1]->src[0] != nullptr ? op->src[1]->src[0]->src[0] : nullptr;
const ggml_tensor * v_base =
op->src[2] != nullptr && op->src[2]->src[0] != nullptr ? op->src[2]->src[0]->src[0] : nullptr;
if (q_base == nullptr || q_base->op != GGML_OP_ROPE) {
return false;
}
const bool is_qkv_direct =
k_base != nullptr && v_base != nullptr && k_base->op == GGML_OP_ROPE && v_base->op == GGML_OP_RMS_NORM;
return is_qkv_direct;
}
static bool checked_mul_size(size_t a, size_t b, size_t & out) {
if (a == 0 || b == 0) {
out = 0;
return true;
}
if (a > SIZE_MAX / b) {
return false;
}
out = a * b;
return true;
}
static bool tensor_view_fits_src_buffer(const ggml_tensor * tensor) {
if (tensor->view_src == nullptr) {
return true;
}
const size_t src_nbytes = ggml_nbytes(tensor->view_src);
if (tensor->view_offs > src_nbytes) {
return false;
}
const size_t tensor_nbytes = ggml_nbytes(tensor);
return tensor_nbytes <= src_nbytes - tensor->view_offs;
}
static bool cpy_output_view_is_supported(const ggml_tensor * op) {
if (op->view_src == nullptr) {
return true;
}
if (!tensor_view_fits_src_buffer(op)) {
return false;
}
return ggml_nbytes(op) == 0 || ggml_is_contiguous(op) || GgmlOvDecoder::is_conv_state_writeback(op) ||
GgmlOvDecoder::is_gdn_snapshot_writeback(op) || GgmlOvDecoder::is_ssm_state_writeback(op);
}
static bool mul_mat_id_requires_large_tmp(const ggml_tensor * op) {
const ggml_tensor * as = op->src[0];
const ggml_tensor * ids = op->src[2];
if (as == nullptr || ids == nullptr) {
return true;
}
size_t tmp_elems = 1;
if (!checked_mul_size(tmp_elems, static_cast<size_t>(ids->ne[1]), tmp_elems) ||
!checked_mul_size(tmp_elems, static_cast<size_t>(ids->ne[0]), tmp_elems) ||
!checked_mul_size(tmp_elems, static_cast<size_t>(as->ne[1]), tmp_elems) ||
!checked_mul_size(tmp_elems, static_cast<size_t>(as->ne[0]), tmp_elems)) {
return true;
}
size_t tmp_bytes = 0;
if (!checked_mul_size(tmp_elems, sizeof(float), tmp_bytes)) {
return true;
}
static constexpr size_t mul_mat_id_tmp_limit = 1ULL << 30; return tmp_bytes > mul_mat_id_tmp_limit;
}
static bool tensor_name_starts_with(const ggml_tensor * tensor, const char * prefix) {
return tensor != nullptr && strncmp(tensor->name, prefix, strlen(prefix)) == 0;
}
static bool is_msa_block_mask_expansion(const ggml_tensor * op) {
if (tensor_name_starts_with(op, "msa_")) {
return true;
}
const ggml_tensor * src = op->src[0];
while (src != nullptr && (src->op == GGML_OP_RESHAPE || src->op == GGML_OP_REPEAT)) {
if (tensor_name_starts_with(src, "msa_block_mask")) {
return true;
}
src = src->src[0];
}
return tensor_name_starts_with(src, "msa_block_mask");
}
namespace {
struct ggml_openvino_op_support {
bool is_supported = true;
std::string reason;
operator bool() const {
return is_supported;
}
};
}
static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
if (is_msa_block_mask_expansion(op)) {
return {false, "MSA block mask expansion is not supported"};
}
switch (op->op) {
case GGML_OP_TOP_K: {
if (op->src[0]->type != GGML_TYPE_F32 || op->type != GGML_TYPE_I32 || ggml_openvino_is_npu()) {
return {false, "TOP_K requires F32 input and I32 output on CPU or GPU"};
}
break;
}
case GGML_OP_CONCAT: {
if (op->type == GGML_TYPE_I64) {
return {false, "CONCAT with I64 type is not supported"};
}
if (ggml_is_quantized(op->type)) {
return {false, "CONCAT with quantized type is not supported"};
}
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_BF16 && has_view_op_input(op)) {
return {false, "CONCAT with BF16 type and VIEW input is not supported on GPU"};
}
break;
}
case GGML_OP_SET: {
const auto nb1 = static_cast<size_t>(op->op_params[0]);
const auto nb2 = static_cast<size_t>(op->op_params[1]);
const auto nb3 = static_cast<size_t>(op->op_params[2]);
if (op->src[0] == nullptr || nb1 != op->src[0]->nb[1] || nb2 != op->src[0]->nb[2] || nb3 != op->src[0]->nb[3]) {
return {false, "SET op with dst nb1=" + std::to_string(nb1) + ", nb2=" + std::to_string(nb2) + ", nb3=" + std::to_string(nb3) +
" that does not match src0 strides nb[1]=" + (op->src[0] != nullptr ? std::to_string(op->src[0]->nb[1]) : "null") +
", nb[2]=" + (op->src[0] != nullptr ? std::to_string(op->src[0]->nb[2]) : "null") +
", nb[3]=" + (op->src[0] != nullptr ? std::to_string(op->src[0]->nb[3]) : "null")};
}
break;
}
case GGML_OP_GET_ROWS:
case GGML_OP_SET_ROWS: {
if (GgmlOvDecoder::is_conv_state_snapshot_writeback(op) && ggml_openvino_is_static()) {
return {false, "convolution snapshots require dynamic execution"};
}
if (op->ne[3] != 1 && !GgmlOvDecoder::is_get_rows_batched_4d(op) &&
!GgmlOvDecoder::is_conv_state_snapshot_writeback(op)) {
return {false, "GET_ROWS/SET_ROWS with ne[3] != 1 (ne[3]=" + std::to_string(op->ne[3]) + ") is not supported"};
}
if (op->op == GGML_OP_SET_ROWS && op->src[0]->type == GGML_TYPE_BF16) {
return {false, "SET_ROWS with BF16 src0 is not supported"};
}
if (op->ne[0] == 256 && (op->src[0]->type == GGML_TYPE_Q4_K || op->src[0]->type == GGML_TYPE_Q5_K ||
op->src[0]->type == GGML_TYPE_Q4_1 || op->src[0]->type == GGML_TYPE_Q5_1)) {
return {false, "GET_ROWS/SET_ROWS with ne[0] == 256 and type " + std::string(ggml_type_name(op->src[0]->type)) +
" rejected due to f16-arithmetic dequant rounding errors that intermittently exceed 1e-7 NMSE threshold"};
}
break;
}
case GGML_OP_RESHAPE: {
if (strncmp(op->name, "ffn_norm_exps", sizeof("ffn_norm_exps") - 1) == 0) {
return {false, "RESHAPE for ffn_norm_exps is not supported"};
}
break;
}
case GGML_OP_ADD:
case GGML_OP_MUL:
case GGML_OP_SUB: {
if (op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "ADD/MUL/SUB with PERMUTE src1 is not supported"};
}
if (op->src[0]->type != op->src[1]->type &&
(op->src[0]->type == GGML_TYPE_BF16 || op->src[1]->type == GGML_TYPE_BF16)) {
return {false, "ADD/MUL/SUB with BF16 and a different src1 type is not supported"};
}
if (op->op == GGML_OP_ADD && is_moe_expert_sum_add(op) && op->src[1]->src[0]->ne[1] > 8) {
return {false, "MoE expert-plane sum with more than 8 experts is not supported"};
}
for (int i = 0; i < 4; i++) {
if (op->src[0]->ne[i] != op->src[1]->ne[i] && (op->src[0]->ne[i] != 1 && op->src[1]->ne[i] != 1)) {
return {false, "ADD/MUL/SUB with incompatible broadcast shapes: src0->ne[" + std::to_string(i) + "]=" +
std::to_string(op->src[0]->ne[i]) + ", src1->ne[" + std::to_string(i) + "]=" +
std::to_string(op->src[1]->ne[i])};
}
}
break;
}
case GGML_OP_SCALE: {
if (op->type == GGML_TYPE_BF16) {
return {false, "SCALE with BF16 type is not supported"};
}
if (op->view_src != nullptr && !ggml_is_contiguous(op)) {
return {false, "SCALE in place on a non-contiguous view is not supported"};
}
break;
}
case GGML_OP_RMS_NORM: {
if (op->type == GGML_TYPE_BF16) {
return {false, "RMS_NORM with BF16 type is not supported"};
}
break;
}
case GGML_OP_ADD_ID: {
if (op->type != GGML_TYPE_F32 || op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type != GGML_TYPE_F32 ||
op->src[2]->type != GGML_TYPE_I32) {
return {false, "ADD_ID only supports F32 inputs/output and I32 ids"};
}
break;
}
case GGML_OP_DIV: {
if (ggml_openvino_is_gpu() && op->src[1]->ne[0] == op->ne[0] &&
op->src[1]->ne[1] == 1 && op->src[1]->ne[2] == 1 && op->src[1]->ne[3] == 1 &&
op->src[1]->buffer != nullptr && op->src[1]->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
return {false, "DIV per-channel scale broadcast is not supported on GPU"};
}
break;
}
case GGML_OP_POOL_1D: {
const ggml_op_pool pool_mode = static_cast<ggml_op_pool>(op->op_params[0]);
if (pool_mode != GGML_OP_POOL_MAX && pool_mode != GGML_OP_POOL_AVG) {
return {false, "POOL_1D mode " + std::to_string(pool_mode) + " is not supported"};
}
if (op->op_params[3] >= op->op_params[1]) {
return {false, "POOL_1D with padding >= kernel size is not supported"};
}
break;
}
case GGML_OP_POOL_2D: {
if (ggml_openvino_is_gpu()) {
const int32_t * params = op->op_params;
const int k0 = params[1];
const int k1 = params[2];
const int p0 = params[5];
const int p1 = params[6];
if ((p0 > 0 || p1 > 0) && (k0 < 3 || k1 < 3)) {
return {false, "POOL_2D with padding and kernel size < 3 is not supported on " + ggml_openvino_get_device_name()};
}
}
break;
}
case GGML_OP_SUM: {
if (op->src[0]->op == GGML_OP_PERMUTE) {
return {false, "SUM with PERMUTE input is not supported"};
}
break;
}
case GGML_OP_MEAN: {
if (op->src[0]->op == GGML_OP_PERMUTE && op->src[0]->src[0] != nullptr && op->src[0]->src[0]->op == GGML_OP_VIEW) {
return {false, "MEAN with PERMUTE of VIEW input is not supported"};
}
break;
}
case GGML_OP_SUM_ROWS: {
if (op->src[0]->op == GGML_OP_PERMUTE) {
return {false, "SUM_ROWS with PERMUTE input is not supported"};
}
break;
}
case GGML_OP_FLASH_ATTN_EXT: {
float scale = 1.0f;
float max_bias = 0.0f;
float logit_softcap = 0.0f;
const auto * op_params = op->op_params;
memcpy(&scale, (const float *) op_params + 0, sizeof(float));
memcpy(&max_bias, (const float *) op_params + 1, sizeof(float));
memcpy(&logit_softcap, (const float *) op_params + 2, sizeof(float));
if (fabsf(scale - 1.0f) < 1e-6f && is_gemma3n_flash_attn_pattern(op)) {
return {false, "FLASH_ATTN_EXT gemma3n pattern is not supported"};
}
if (ggml_flash_attn_ext_has_indexed_bias(op)) {
return {false, "FLASH_ATTN_EXT with indexed bias is not supported"};
}
if (op->src[5] != nullptr) {
return {false, "FLASH_ATTN_EXT with K/V row lists is not supported"};
}
if (op->src[4] != nullptr && op->src[3] == nullptr) {
return {false, "FLASH_ATTN_EXT with sinks but no mask is not supported"};
}
if (!is_supported_flash_attn_pattern(op)) {
return {false, "FLASH_ATTN_EXT unsupported attention pattern"};
}
if (max_bias > 0) {
return {false, "FLASH_ATTN_EXT with max_bias > 0 (max_bias=" + std::to_string(max_bias) + ") is not supported"};
}
if (logit_softcap != 0 && (op->src[1]->ne[0] != op->src[0]->ne[0] || op->src[2]->ne[0] != op->src[0]->ne[0])) {
return {false, "FLASH_ATTN_EXT with logit_softcap != 0 needs one head size for Q, K and V"};
}
if (logit_softcap != 0 && op->src[4] != nullptr) {
return {false, "FLASH_ATTN_EXT with sinks and logit_softcap != 0 is not supported"};
}
break;
}
case GGML_OP_PERMUTE: {
if (op->type == GGML_TYPE_BF16 && ggml_openvino_is_gpu()) {
return {false, "PERMUTE with BF16 type is not supported on GPU"};
}
break;
}
case GGML_OP_DUP: {
if (op->type != op->src[0]->type || !ggml_are_same_shape(op, op->src[0]) || !ggml_is_contiguous(op->src[0])) {
return {false, "DUP with type conversion or non-contiguous src is not supported"};
}
break;
}
case GGML_OP_CPY: {
if (op->src[0]->type != GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_BF16) {
return {false, "CPY with BF16 src[1] type is not supported"};
}
if (ggml_openvino_is_npu() && (op->src[0]->type == GGML_TYPE_BF16 || op->src[1]->type == GGML_TYPE_BF16)) {
return {false, "CPY with BF16 is not supported on NPU"};
}
if (ggml_is_quantized(op->type)) {
return {false, "CPY to quantized destination (e.g. f32 -> q4_0) is numerically unstable"};
}
if (ggml_nelements(op->src[0]) != ggml_nelements(op->src[1])) {
return {false, "CPY with mismatched element counts is not supported: src0=" + std::to_string(ggml_nelements(op->src[0])) +
" != src1=" + std::to_string(ggml_nelements(op->src[1]))};
}
if ((op->ne[0] == 3 && op->ne[1] == 4 && op->ne[2] == 3 && op->ne[3] == 2) ||
(op->ne[0] == 1 && op->ne[1] == 4 && op->ne[2] == 3 && op->ne[3] == 2) ||
(op->ne[0] == 2 && op->ne[1] == 4 && op->ne[2] == 3 && op->ne[3] == 2)) {
return {false, "CPY with non-contiguous shape [" + std::to_string(op->ne[0]) + ", " +
std::to_string(op->ne[1]) + ", " + std::to_string(op->ne[2]) + ", " +
std::to_string(op->ne[3]) + "] is not supported"};
}
if (!cpy_output_view_is_supported(op)) {
return {false, "CPY with non-contiguous output view is not supported"};
}
break;
}
case GGML_OP_MUL_MAT: {
if (ggml_prec_get_src(op, 1) == GGML_PREC_BF16 || ggml_prec_get_src(op, 1) == GGML_PREC_F32) {
return {false, "MUL_MAT with a BF16 or F32 src1 precision is not supported"};
}
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[1] != nullptr &&
ggml_is_quantized(op->src[0]->type) && strcmp(op->src[0]->name, "a") == 0 &&
strcmp(op->src[1]->name, "b") == 0 && op->src[0]->ne[1] == 1 && op->src[1]->ne[1] == 64 &&
op->src[0]->ne[0] == 256 && op->src[1]->ne[0] == 256) {
return {false, "MUL_MAT quantized benchmark test case on GPU is not supported"};
}
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_F32 && op->ne[0] == 1 && op->ne[1] == 1 &&
(op->src[0]->buffer == nullptr || op->src[0]->buffer->usage != GGML_BACKEND_BUFFER_USAGE_WEIGHTS)) {
return {false, "MUL_MAT scalar dot product with non-weight src[0] on GPU is not supported"};
}
if (ggml_openvino_is_gpu() && (op->src[0]->type == GGML_TYPE_Q4_1 || op->src[0]->type == GGML_TYPE_Q4_K) &&
op->src[0]->buffer == nullptr) {
return {false, "MUL_MAT with unbound Q4_1/Q4_K src[0] on GPU is not supported"};
}
if (ggml_openvino_is_gpu() && ggml_is_quantized(op->src[0]->type) && op->src[0]->ne[2] > 1 &&
(op->src[0]->buffer == nullptr || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC"))) {
return {false, "MUL_MAT with batched quantized src[0] and static shapes on GPU is not supported"};
}
if (op->src[0]->ne[3] != op->src[1]->ne[3] && op->src[0]->ne[3] != 1 && op->src[1]->ne[3] != 1) {
return {false, "MUL_MAT with incompatible broadcast on ne[3]: src0->ne[3]=" + std::to_string(op->src[0]->ne[3]) +
", src1->ne[3]=" + std::to_string(op->src[1]->ne[3])};
}
if (op->src[0]->op == GGML_OP_VIEW && op->src[1]->op == GGML_OP_VIEW) {
return {false, "MUL_MAT with both inputs as VIEW is not supported"};
}
break;
}
case GGML_OP_MUL_MAT_ID: {
if (op->src[0] != nullptr && op->src[0]->ne[2] <= 1) {
return {false, "MUL_MAT_ID with single-expert or empty ne[2] <= 1 (ne[2]=" +
std::to_string(op->src[0]->ne[2]) + ") is not supported"};
}
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[0]->buffer == nullptr) {
return {false, "MUL_MAT_ID with unbound expert tensors on GPU is not supported"};
}
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[0]->type == GGML_TYPE_MXFP4 &&
mul_mat_id_requires_large_tmp(op)) {
return {false, "MUL_MAT_ID with MXFP4 weights requires large temporary on GPU"};
}
break;
}
case GGML_OP_ROPE: {
if (op->view_src != nullptr && !ggml_is_contiguous(op->src[0])) {
return {false, "ROPE on VIEW / non-contiguous input is not supported"};
}
if (op->type == GGML_TYPE_BF16) {
return {false, "ROPE with BF16 type is not supported"};
}
break;
}
case GGML_OP_TRANSPOSE: {
if (op->type == GGML_TYPE_BF16) {
return {false, "TRANSPOSE with BF16 type is not supported"};
}
break;
}
case GGML_OP_REPEAT: {
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_BF16) {
return {false, "REPEAT with BF16 type is not supported on GPU"};
}
break;
}
case GGML_OP_GATED_DELTA_NET: {
const char * reason = nullptr;
const ggml_tensor * v_src = op->src[2]->op == GGML_OP_VIEW ? op->src[2]->src[0] : op->src[2];
if (v_src->op == GGML_OP_PERMUTE) {
reason = "GATED_DELTA_NET with PERMUTE src2 is not supported";
}
else if (op->src[3]->ne[0] != 1 && (op->op_params[0] > 1 || ggml_openvino_gdn_native_state_enabled()) &&
!ov::frontend::ggml::op::gdn_per_key_gate_supported()) {
reason = "GATED_DELTA_NET with a per-key gate needs an OpenVINO GatedDeltaNet with a rank-4 gate for state "
"snapshots or the native state layout";
}
else if (op->op_params[0] > 1) {
if (ggml_openvino_is_static()) {
reason = "GATED_DELTA_NET snapshots require dynamic execution";
}
}
if (reason != nullptr) {
if (ggml_openvino_gdn_native_state_enabled()) {
GGML_ABORT("ggml-openvino: GGML_OPENVINO_GDN_NATIVE_STATE is set, but %s", reason);
}
return {false, reason};
}
if (((const int32_t *) op->op_params)[1] != 0) {
return {false, "GATED_DELTA_NET with replay of pending tokens is not supported"};
}
break;
}
case GGML_OP_SSM_SCAN: {
if (op->src[3]->ne[0] != 1) {
return {false, "SSM_SCAN with Mamba-1 state-wise decay is not supported"};
}
if (op->op_params[0] > 1) {
return {false, "SSM_SCAN with rollback snapshots (K > 1) is not supported"};
}
if (ggml_openvino_is_static()) {
return {false, "SSM_SCAN requires dynamic execution"};
}
break;
}
case GGML_OP_VIEW: {
if (strcmp(op->name, "selected_experts") == 0) {
return {false, "VIEW for selected_experts (argsort_top_k) is not supported"};
}
break;
}
case GGML_OP_CONV_2D:
case GGML_OP_CONV_2D_DW: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "CONV_2D kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_2D with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_2D with non-contiguous view input is not supported"};
}
const int32_t * params = op->op_params;
const int p0 = params[2];
const int p1 = params[3];
const int d0 = params[4];
const int d1 = params[5];
const int64_t dilated_kw = (int64_t) d0 * (op->src[0]->ne[0] - 1) + 1;
const int64_t dilated_kh = (int64_t) d1 * (op->src[0]->ne[1] - 1) + 1;
const int64_t padded_w = op->src[1]->ne[0] + 2 * p0;
const int64_t padded_h = op->src[1]->ne[1] + 2 * p1;
if (padded_w < dilated_kw || padded_h < dilated_kh) {
return {false, "CONV_2D padded input is smaller than kernel"};
}
break;
}
case GGML_OP_CONV_3D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0 || op->src[0]->ne[2] <= 0) {
return {false, "CONV_3D kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_3D with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_3D with non-contiguous view input is not supported"};
}
const int32_t * params = op->op_params;
const int p0 = params[3];
const int p1 = params[4];
const int p2 = params[5];
const int d0 = params[6];
const int d1 = params[7];
const int d2 = params[8];
const int64_t dilated_kw = (int64_t) d0 * (op->src[0]->ne[0] - 1) + 1;
const int64_t dilated_kh = (int64_t) d1 * (op->src[0]->ne[1] - 1) + 1;
const int64_t dilated_kd = (int64_t) d2 * (op->src[0]->ne[2] - 1) + 1;
const int64_t padded_w = op->src[1]->ne[0] + 2 * p0;
const int64_t padded_h = op->src[1]->ne[1] + 2 * p1;
const int64_t padded_d = op->src[1]->ne[2] + 2 * p2;
if (padded_w < dilated_kw || padded_h < dilated_kh || padded_d < dilated_kd) {
return {false, "CONV_3D padded input is smaller than kernel"};
}
break;
}
case GGML_OP_CONV_TRANSPOSE_1D:
case GGML_OP_CONV_TRANSPOSE_2D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "CONV_TRANSPOSE kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_TRANSPOSE with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_TRANSPOSE with non-contiguous view input is not supported"};
}
break;
}
case GGML_OP_IM2COL: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "IM2COL kernel size must be positive"};
}
break;
}
case GGML_OP_IM2COL_3D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0 || op->src[0]->ne[2] <= 0) {
return {false, "IM2COL_3D kernel size must be positive"};
}
break;
}
default:
break;
}
return {true, ""};
}
static ggml_openvino_op_support ggml_backend_openvino_device_supports_op_impl(ggml_backend_dev_t dev, const ggml_tensor * op) {
GGML_ASSERT(dev->reg != nullptr);
ggml_backend_openvino_device_context * dev_ctx = (ggml_backend_openvino_device_context *) dev->context;
if (!ggml_openvino_is_selected_device(dev_ctx->ov_name)) {
for (int i = -1; i < GGML_MAX_SRC; i++) {
const ggml_tensor * t = i < 0 ? op : op->src[i];
ggml_backend_buffer_t buf = t == nullptr ? nullptr : (t->view_src ? t->view_src->buffer : t->buffer);
if (buf != nullptr &&
(ggml_backend_buft_is_openvino(buf->buft) || ggml_backend_buft_is_openvino_host(buf->buft)) &&
((ggml_backend_openvino_buffer_type_context *) buf->buft->context)->device == dev_ctx->device) {
GGML_ABORT("%s is not the selected OpenVINO device (%s). The OpenVINO device is chosen with the "
"GGML_OPENVINO_DEVICE environment variable, not -dev: set GGML_OPENVINO_DEVICE=%s",
dev_ctx->name.c_str(), ggml_openvino_get_device_name().c_str(), dev_ctx->ov_name.c_str());
}
}
return {false, "device is not the selected OpenVINO device"};
}
if (GgmlOvDecoder::is_gdn_snapshot_writeback(op)) {
const auto gdn_support = ggml_backend_openvino_device_supports_op_impl(dev, op->src[0]->src[0]);
if (!gdn_support.is_supported) {
GGML_ABORT(
"ggml-openvino: %s. Recurrent rollback (n_rs_seq > 0, used by MTP, EAGLE3, DFlash and DSpark "
"speculative decoding) is not supported for this model on this device",
gdn_support.reason.c_str());
}
}
static std::unordered_set<ggml_type> supported_types{
GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_I64, GGML_TYPE_I32, GGML_TYPE_Q4_0,
GGML_TYPE_Q4_1, GGML_TYPE_Q4_K, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_K, GGML_TYPE_Q8_0, GGML_TYPE_Q6_K,
GGML_TYPE_MXFP4};
static const auto build_supported_sets = [] {
const auto & table = ov::frontend::ggml::get_supported_ops();
std::unordered_set<ggml_op> ops;
std::unordered_set<ggml_unary_op> unary_ops;
std::unordered_set<ggml_glu_op> glu_ops;
ops.insert(GGML_OP_NONE);
for (int i = 0; i < GGML_OP_COUNT; ++i) {
const std::string key = std::string("GGML_OP_") + ggml_op_name(static_cast<ggml_op>(i));
if (table.count(key)) {
ops.insert(static_cast<ggml_op>(i));
}
}
for (int i = 0; i < GGML_UNARY_OP_COUNT; ++i) {
const std::string key = std::string("GGML_UNARY_OP_") + ggml_unary_op_name(static_cast<ggml_unary_op>(i));
if (table.count(key)) {
unary_ops.insert(static_cast<ggml_unary_op>(i));
}
}
for (int i = 0; i < GGML_GLU_OP_COUNT; ++i) {
const std::string key = std::string("GGML_GLU_OP_") + ggml_glu_op_name(static_cast<ggml_glu_op>(i));
if (table.count(key)) {
glu_ops.insert(static_cast<ggml_glu_op>(i));
}
}
return std::make_tuple(ops, unary_ops, glu_ops);
};
static const auto supported_sets = build_supported_sets();
static const auto & supported_ops = std::get<0>(supported_sets);
static const auto & supported_unary_ops = std::get<1>(supported_sets);
static const auto & supported_glu_ops = std::get<2>(supported_sets);
switch (op->op) {
case GGML_OP_UNARY: {
auto supported = supported_unary_ops.find(ggml_get_unary_op(op)) != supported_unary_ops.end();
if (!supported) {
return {false, "unary op " + std::string(ggml_unary_op_name(ggml_get_unary_op(op))) + " has no op translator"};
}
if (op->type == GGML_TYPE_F32 && ggml_get_unary_op(op) == GGML_UNARY_OP_EXP && !ggml_openvino_is_gpu()) {
return {false, "UNARY_EXP with F32 type is not supported on " + ggml_openvino_get_device_name()};
}
if (op->type == GGML_TYPE_F32 && ggml_get_unary_op(op) == GGML_UNARY_OP_EXPM1) {
return {false, "UNARY_EXPM1 with F32 type is not supported"};
}
break;
}
case GGML_OP_GLU: {
auto supported = supported_glu_ops.find(ggml_get_glu_op(op)) != supported_glu_ops.end();
if (!supported) {
return {false, "GLU op " + std::string(ggml_glu_op_name(ggml_get_glu_op(op))) + " has no op translator"};
}
if (op->src[1] == nullptr && op->src[0]->ne[0] % 2 != 0) {
return {false, "GLU op with odd src0 ne[0] and null src1 is not supported"};
}
break;
}
default: {
auto supported = supported_ops.find(op->op) != supported_ops.end();
if (!supported) {
return {false, "op " + std::string(ggml_op_name(op->op)) + " has no op translator"};
}
static std::set<ggml_op> ops_not_support_view_input{};
if (ops_not_support_view_input.find(op->op) != ops_not_support_view_input.end() && has_view_op_input(op)) {
return {false, "op " + std::string(ggml_op_name(op->op)) + " with VIEW input is not supported"};
}
}
}
if (supported_types.find(op->type) == supported_types.end()) {
return {false, "tensor type " + std::string(ggml_type_name(op->type)) + " is not supported"};
}
for (int i = 0; i < GGML_MAX_SRC; i++) {
auto * src = op->src[i];
if (src == nullptr) {
break;
}
if (supported_types.find(src->type) == supported_types.end()) {
return {false, "src[" + std::to_string(i) + "] type " + std::string(ggml_type_name(src->type)) + " is not supported"};
}
if (!has_strides_on_element_grid(src) && !is_row_span_view(src) &&
!(i == 1 && GgmlOvDecoder::is_gdn_snapshot_writeback(op)) &&
!(i == 2 && GgmlOvDecoder::is_conv_state_snapshot_writeback(op))) {
return {false, "src[" + std::to_string(i) + "] strides are not multiples of each other"};
}
const bool is_supported_3d_moe_expert =
op->op == GGML_OP_MUL_MAT_ID && i == 0 && (src->type == GGML_TYPE_MXFP4 || src->ne[3] == 1);
const bool is_supported_3d_batched_weight = op->op == GGML_OP_MUL_MAT && i == 0 && src->ne[3] == 1 &&
op->src[1]->ne[2] == src->ne[2] && op->src[1]->ne[3] == 1;
if (ggml_is_quantized(src->type) && src->ne[2] != 1 && !is_supported_3d_moe_expert &&
!is_supported_3d_batched_weight) {
return {false, "3D quantized tensor for src[" + std::to_string(i) + "] is not supported"};
}
}
auto op_support_case = is_op_supported_case(op);
if (!op_support_case.is_supported) {
return op_support_case;
}
return {true, ""};
}
namespace {
struct supports_op_tally {
std::mutex mutex;
std::map<std::string, size_t> counts;
void arm_summary() {
static std::once_flag armed;
std::call_once(armed, [] {
std::atexit([] {
auto & self = instance();
std::lock_guard<std::mutex> lock(self.mutex);
size_t n_accept_kinds = 0;
size_t n_reject_kinds = 0;
GGML_LOG_WARN("ov-supports-op: ---- summary (distinct op kinds, with call counts) ----\n");
for (const auto & [key, count] : self.counts) {
const bool ok = key.size() > 3 && key.compare(key.size() - 3, 3, ":ok") == 0;
ok ? ++n_accept_kinds : ++n_reject_kinds;
GGML_LOG_WARN("ov-supports-op: %-8s %-24s %zu call(s)\n", ok ? "ACCEPT" : "REJECT",
key.substr(0, key.size() - 3).c_str(), count);
}
if (n_reject_kinds == 0) {
GGML_LOG_WARN("ov-supports-op: %zu op kind(s) accepted, 0 rejected"
" -- every op was supported, so no split originated here\n",
n_accept_kinds);
} else {
GGML_LOG_WARN("ov-supports-op: %zu op kind(s) accepted, %zu REJECTED"
" -- each rejected kind forces a graph split\n",
n_accept_kinds, n_reject_kinds);
}
});
});
}
static supports_op_tally & instance() {
static supports_op_tally tally;
return tally;
}
};
}
static bool ggml_backend_openvino_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
auto res = ggml_backend_openvino_device_supports_op_impl(dev, op);
const bool supported = res.is_supported;
if (!supported) {
static const bool log_unsupported = ggml_openvino_getenv_int("GGML_OPENVINO_LOG_UNSUPPORTED_OPS") != 0;
if (log_unsupported) {
GGML_LOG_WARN("OpenVINO op unsupported: op '%s' (%s), type %s: %s\n",
op->name, ggml_op_name(op->op), ggml_type_name(op->type), res.reason.c_str());
}
}
static const int log_level = ggml_openvino_getenv_int("GGML_OPENVINO_LOG_SUPPORTS_OP");
if (log_level <= 0) {
return supported;
}
std::string op_desc = ggml_op_name(op->op);
if (op->op == GGML_OP_UNARY) {
op_desc += std::string("/") + ggml_unary_op_name(ggml_get_unary_op(op));
} else if (op->op == GGML_OP_GLU) {
op_desc += std::string("/") + ggml_glu_op_name(ggml_get_glu_op(op));
}
auto & tally = supports_op_tally::instance();
tally.arm_summary();
size_t count;
{
std::lock_guard<std::mutex> lock(tally.mutex);
count = ++tally.counts[op_desc + (supported ? ":ok" : ":no")];
}
if (log_level >= 2) {
GGML_LOG_WARN("ov-supports-op: %-6s %-22s name='%s' type=%s ne=[%ld,%ld,%ld,%ld] (call %zu)\n",
supported ? "ACCEPT" : "REJECT", op_desc.c_str(), op->name, ggml_type_name(op->type),
(long) op->ne[0], (long) op->ne[1], (long) op->ne[2], (long) op->ne[3], count);
}
return supported;
}
static bool ggml_backend_openvino_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
if (ggml_backend_buft_is_openvino(buft)) {
ggml_backend_openvino_device_context * dev_ctx = (ggml_backend_openvino_device_context *) dev->context;
ggml_backend_openvino_buffer_type_context * buft_ctx =
(ggml_backend_openvino_buffer_type_context *) buft->context;
return buft_ctx->device == dev_ctx->device;
}
return ggml_backend_buft_is_host(buft);
}
static const struct ggml_backend_device_i ggml_backend_openvino_device_interface = {
ggml_backend_openvino_device_get_name,
ggml_backend_openvino_device_get_description,
ggml_backend_openvino_device_get_memory,
ggml_backend_openvino_device_get_type,
ggml_backend_openvino_device_get_props,
ggml_backend_openvino_device_init,
ggml_backend_openvino_device_get_buffer_type,
ggml_backend_openvino_device_get_host_buffer_type,
ggml_backend_openvino_device_buffer_from_host_ptr,
ggml_backend_openvino_device_supports_op,
ggml_backend_openvino_device_supports_buft,
NULL,
NULL,
NULL,
NULL,
};
namespace {
struct ggml_backend_openvino_reg_context {
std::vector<ggml_backend_dev_t> devices;
};
}
static const char * ggml_backend_openvino_reg_get_name(ggml_backend_reg_t reg) {
return GGML_OPENVINO_NAME;
GGML_UNUSED(reg);
}
static size_t ggml_backend_openvino_reg_get_device_count(ggml_backend_reg_t reg) {
GGML_UNUSED(reg);
return (size_t) ggml_backend_openvino_get_device_count();
}
static ggml_backend_dev_t ggml_backend_openvino_reg_get_device(ggml_backend_reg_t reg, size_t index) {
ggml_backend_openvino_reg_context * ctx = (ggml_backend_openvino_reg_context *) reg->context;
GGML_ASSERT(index < ctx->devices.size());
return ctx->devices[index];
}
static const struct ggml_backend_reg_i ggml_backend_openvino_reg_interface = {
ggml_backend_openvino_reg_get_name,
ggml_backend_openvino_reg_get_device_count,
ggml_backend_openvino_reg_get_device,
NULL,
};
static void ggml_openvino_init() {
ggml_openvino_init_device_config();
GGML_LOG_INFO("OpenVINO: using device %s\n", ggml_openvino_get_device_name().c_str());
}
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void) {
static ggml_backend_reg reg;
static bool initialized = false;
{
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
if (!initialized) {
ggml_openvino_init();
const std::vector<std::string> openvino_devices = ggml_openvino_get_available_devices();
ggml_backend_openvino_reg_context * ctx = new ggml_backend_openvino_reg_context;
for (int i = 0; i < ggml_backend_openvino_get_device_count(); i++) {
ggml_backend_openvino_device_context * dev_ctx = new ggml_backend_openvino_device_context;
dev_ctx->device = i;
dev_ctx->name = GGML_OPENVINO_NAME + std::to_string(i);
dev_ctx->ov_name = openvino_devices[i];
dev_ctx->description = "GGML_OPENVINO_DEVICE=" + dev_ctx->ov_name +
(ggml_openvino_is_selected_device(dev_ctx->ov_name) ? " (selected)" : "") +
" - " + ggml_openvino_get_device_description(dev_ctx->ov_name);
dev_ctx->total_memory = 0;
if (ov_device_has_prefix(dev_ctx->ov_name, "GPU")) {
ov_try_get_size_t_property(dev_ctx->ov_name, "GPU_DEVICE_TOTAL_MEM_SIZE", dev_ctx->total_memory);
} else if (ov_device_has_prefix(dev_ctx->ov_name, "NPU")) {
ov_try_get_size_t_property(dev_ctx->ov_name, "NPU_DEVICE_TOTAL_MEM_SIZE", dev_ctx->total_memory);
}
ggml_backend_dev_t dev =
new ggml_backend_device{ ggml_backend_openvino_device_interface,
®,
dev_ctx};
ctx->devices.push_back(dev);
}
reg = ggml_backend_reg{ GGML_BACKEND_API_VERSION,
ggml_backend_openvino_reg_interface,
ctx};
}
initialized = true;
}
return ®
}
GGML_BACKEND_DL_IMPL(ggml_backend_openvino_reg)