Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ggml/src/ggml-openvino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ On the static path (NPU, or any device with `GGML_OPENVINO_FORCE_STATIC=1`), mod
# NPU host gather

`GGML_OPENVINO_NPU_HOST_GATHER=1` (NPU, opt-in) gathers the rows of large host-resident embedding tables (at least 64 MiB, raw GGUF data) on the host. The static graph gets the gathered rows through an `ggml_host_rows_<node>` input instead of an OpenVINO Gather over the table. NPUW otherwise lifts the table to a function input, and gathering from an input that large costs about as much as streaming the whole table every call (21.6 ms of Gemma 4 E2B decode). A table used only for such lookups (the Gemma 3n/4 per-layer embedding table) is not converted or bound to the NPU at all.

## Multimodal

Qwen3.5 and Gemma4 E2B image input work on OpenVINO CPU and GPU with their matching
projectors. Four-slot serving was validated with unified and split KV.
Qwen3.5 reuses its vision graph across image resolutions; Gemma4 uses
resolution-specific encoder graphs.

Gemma4 audio is not enabled: preprocessing and Conformer still need dynamic length/block handling, runtime padding and view/state offsets, and cross-length compiled-model reuse. The small weightless preprocessing graph currently runs uncached because of the naive path's node-count heuristic.

Static/NPU multimodal execution is untested.
170 changes: 168 additions & 2 deletions ggml/src/ggml-openvino/ggml-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ggml-quants.h"
#include "ggml.h"
#include "utils.h"
#include "openvino/utils.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -33,6 +34,7 @@
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

static std::set<std::string> collect_graph_output_names(const ggml_cgraph * cgraph) {
Expand Down Expand Up @@ -302,12 +304,24 @@ static std::string get_tensor_ov_name(const ggml_cgraph * cgraph, const ggml_ten
return GgmlOvDecoder::get_tensor_name(cgraph, tensor);
}

std::string GgmlOvDecoder::get_position_input_ov_name(const ggml_tensor * tensor) const {
// Keep independent spatial position tensors on independent ports.
for (int i = 0; i < m_cgraph->n_nodes; ++i) {
const auto * node = m_cgraph->nodes[i];
if (node->op == GGML_OP_ROPE) {
if (node->src[1] != tensor) { return get_tensor_name(m_cgraph, tensor); }
break;
}
}
return "inp_pos";
}

static std::string get_tensor_graph_input_ov_name(const GgmlOvDecoder * decoder,
const ggml_cgraph * cgraph,
const ggml_tensor * tensor,
const ggml_tensor * op) {
if (GgmlOvDecoder::is_inp_pos(tensor, op)) {
return "inp_pos";
return decoder->get_position_input_ov_name(tensor);
}
if (GgmlOvDecoder::is_inp_emb(tensor, op)) {
return "embd";
Expand Down Expand Up @@ -435,6 +449,29 @@ bool GgmlOvDecoder::is_gdn_out_prescaled(const ggml_tensor * gdn) const {
return has_norm;
}

static bool is_gdn_output_gate_view(const ggml_tensor * node, const ggml_cgraph * graph) {
if (node->op != GGML_OP_VIEW || !ggml_is_contiguous(node) || node->view_offs != 0 ||
!node->src[0] || node->src[0]->op != GGML_OP_MUL_MAT ||
ggml_nelements(node) != ggml_nelements(node->src[0])) {
return false;
}
for (int i = 0; i < graph->n_nodes; ++i) {
const auto * consumer = graph->nodes[i];
if (consumer->op != GGML_OP_MUL || std::memcmp(node->ne, consumer->ne, sizeof(node->ne)) != 0) { continue; }
for (int j = 0; j < 2; ++j) {
const auto * gate = consumer->src[j];
const auto * norm = consumer->src[1 - j];
if (gate->op != GGML_OP_UNARY || gate->src[0] != node) { continue; }
if (norm->op == GGML_OP_MUL) { norm = norm->src[0]; }
if (norm->op == GGML_OP_RMS_NORM && norm->src[0]->op == GGML_OP_VIEW &&
norm->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET) {
return true;
}
}
}
return false;
}

int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
int op_case = 0;
switch (node->op) {
Expand Down Expand Up @@ -702,6 +739,9 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
break;
}
case GGML_OP_VIEW: {
if (!m_naive && !m_is_static && is_gdn_output_gate_view(node, m_cgraph)) {
return 5;
}
if (is_sampling_view(node)) {
return 3;
}
Expand Down Expand Up @@ -950,9 +990,114 @@ static const ggml_tensor * softmax_scores_source(const ggml_tensor * scores) {
return scores;
}

std::pair<ModelParams, ComputeParams> GgmlOvDecoder::compute_llm_params(ggml_cgraph * cgraph, bool is_static) {
std::vector<int64_t> GgmlOvDecoder::vision_reshape_pattern(const ggml_tensor * node) {
const auto * src = node->src[0];
if (node->op == GGML_OP_CONT && std::memcmp(node->ne, src->ne, sizeof(node->ne)) == 0) {
// No reshape is emitted; all dimensions come directly from the input.
return {-2, -2, -2, -2};
}
if (node->ne[2] == 1 && node->ne[3] == 1) {
return {1, 1, -1, node->ne[0]};
}
if (node->op == GGML_OP_RESHAPE && src->op == GGML_OP_MUL_MAT && src->src[0] &&
src->src[0]->op == GGML_OP_RESHAPE && src->src[0]->src[0]->op == GGML_OP_IM2COL &&
node->ne[2] == 1 && node->ne[3] == src->ne[1]) {
// Unflatten patch convolution: [channels, 1, patch_height, patch_width].
return {node->ne[3], 1, -2, -2};
}
if (node->op == GGML_OP_RESHAPE && src->op == GGML_OP_CONT && src->ne[3] == 1 &&
node->ne[0] == src->ne[0] && node->ne[1] == src->ne[1] && node->ne[2] * node->ne[3] == src->ne[2]) {
// Split rows into fixed-size merge groups while retaining both grid axes.
return {-2, node->ne[2], -2, node->ne[0]};
}
if (node->op == GGML_OP_CONT && src->ne[3] == 1 && node->ne[3] == 1 &&
node->ne[2] == src->ne[2] && node->ne[0] * node->ne[1] == src->ne[0] * src->ne[1]) {
// Merge adjacent patch features into each column; the feature width is fixed.
return {1, -2, -2, node->ne[0]};
}
return {-2, -2, -2, -2};
}

bool GgmlOvDecoder::is_dynamic_vision_graph(const ggml_cgraph * cgraph) {
bool image_patches = false, vision_rope = false, attention = false;
// Recognize the operation patterns covered by the dynamic patch-encoder path.
// Reject language graphs before allocating the set used to check external sources.
for (int i = 0; i < cgraph->n_nodes; ++i) {
const auto * node = cgraph->nodes[i];
switch (node->op) {
case GGML_OP_NONE: case GGML_OP_RESHAPE: case GGML_OP_PERMUTE: case GGML_OP_CONT:
case GGML_OP_VIEW: case GGML_OP_MUL_MAT: case GGML_OP_ADD: case GGML_OP_MUL:
case GGML_OP_NORM: case GGML_OP_RMS_NORM: case GGML_OP_UNARY: case GGML_OP_CPY:
break;
case GGML_OP_IM2COL:
if (node->op_params[6] != 1) { return false; }
image_patches |= node->src[1] && node->src[1]->op == GGML_OP_NONE &&
(node->src[1]->flags & GGML_TENSOR_FLAG_INPUT);
break;
case GGML_OP_UPSCALE:
// Spatial position-grid interpolation; channel/batch resampling stays static.
if (node->ne[2] != node->src[0]->ne[2] || node->ne[3] != node->src[0]->ne[3]) { return false; }
break;
case GGML_OP_ROPE:
if (node->op_params[2] != GGML_ROPE_TYPE_VISION) { return false; }
vision_rope = true;
break;
case GGML_OP_FLASH_ATTN_EXT:
if (node->src[3] || node->src[4]) { return false; }
attention = true;
break;
default:
return false;
}
}
if (!image_patches || !vision_rope || !attention) {
return false;
}
std::unordered_set<const ggml_tensor *> nodes;
nodes.reserve(cgraph->n_nodes);
for (int i = 0; i < cgraph->n_nodes; ++i) {
nodes.insert(cgraph->nodes[i]);
}
// Require declared inputs or weights at graph boundaries before enabling spatial reuse.
for (int i = 0; i < cgraph->n_nodes; ++i) {
for (const auto * src : cgraph->nodes[i]->src) {
if (!src || nodes.count(src)) { continue; }
const auto * root = src->view_src ? src->view_src : src;
if (!(root->flags & GGML_TENSOR_FLAG_INPUT) &&
!(root->buffer && root->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS)) { return false; }
}
}
return true;
}

std::pair<ModelParams, ComputeParams> GgmlOvDecoder::compute_llm_params(ggml_cgraph * cgraph,
bool is_static,
bool dynamic_vision) {
ModelParams model_params;
ComputeParams compute_params;
model_params.is_dynamic_vision = !is_static && dynamic_vision;
if (model_params.is_dynamic_vision) {
for (int i = 0; i < cgraph->n_nodes; ++i) {
const auto * node = cgraph->nodes[i];
const auto name = get_tensor_ov_name(cgraph, node);
const auto * root = node->view_src ? node->view_src : node;
const bool weight = root->buffer && root->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS;
if ((node->op == GGML_OP_RESHAPE || node->op == GGML_OP_CONT) && !weight) {
const auto shape = get_shape(node);
compute_params.vision_shapes["vision_shape_" + name] = {shape.begin(), shape.end()};
}
if (node->op == GGML_OP_UPSCALE) {
compute_params.vision_shapes["vision_size_" + name] = {node->ne[1], node->ne[0]};
if ((node->op_params[0] & 0xff) == GGML_SCALE_MODE_BILINEAR &&
(node->op_params[0] & GGML_SCALE_FLAG_ANTIALIAS)) {
compute_params.vision_projections["vision_y_" + name] =
ov::frontend::ggml::bilinear_antialias_projection(node->src[0]->ne[1], node->ne[1], false);
compute_params.vision_projections["vision_x_" + name] =
ov::frontend::ggml::bilinear_antialias_projection(node->src[0]->ne[0], node->ne[0], true);
}
}
}
}
auto get_attention_pattern_case = [](const ggml_tensor * node) -> int {
if (node == nullptr) {
return -1;
Expand Down Expand Up @@ -1492,6 +1637,11 @@ ov::PartialShape GgmlOvDecoder::get_graph_input_shape(const ggml_tensor * op,
ov::PartialShape input_shape;
bool kv_layout_4d = false;

if (m_model_params.is_dynamic_vision && input && op->op == GGML_OP_IM2COL &&
input == op->src[1]) {
return ov::PartialShape{input->ne[3], input->ne[2], -1, -1};
}

if (is_inp_tok(input, op) || is_inp_pos(input, op)) {
// tokens or positions
int len = m_is_static ? (m_is_prefill ? m_prefill_chunk_size : 1) : -1;
Expand Down Expand Up @@ -1706,6 +1856,22 @@ void GgmlOvDecoder::add_extra_inputs() {
m_model_extra_inputs[name] = {ov::element::i64, ov::Shape{1}, value, force_parameter || !m_is_static, {}, -1};
};

for (const auto & [name, shape] : m_compute_params.vision_shapes) {
m_model_extra_inputs[name] = {ov::element::i64, ov::Shape{shape.size()}, 0, true, {}, -1};
}
for (int i = 0; i < m_cgraph->n_nodes && !m_compute_params.vision_projections.empty(); ++i) {
const auto * node = m_cgraph->nodes[i];
if (node->op != GGML_OP_UPSCALE) { continue; }
const auto name = get_tensor_ov_name(m_cgraph, node);
if (m_compute_params.vision_projections.count("vision_y_" + name) == 0) { continue; }
m_model_extra_inputs["vision_y_" + name] = {ov::element::f32,
ov::Shape{size_t(node->ne[1]), size_t(node->src[0]->ne[1])}, 0, true, {}, -1,
ov::PartialShape{-1, node->src[0]->ne[1]}};
m_model_extra_inputs["vision_x_" + name] = {ov::element::f32,
ov::Shape{size_t(node->src[0]->ne[0]), size_t(node->ne[0])}, 0, true, {}, -1,
ov::PartialShape{node->src[0]->ne[0], -1}};
}

if (m_compute_params.attention_size != -1) {
create_1d_input("attention_size", m_compute_params.attention_size);
}
Expand Down
27 changes: 24 additions & 3 deletions ggml/src/ggml-openvino/ggml-decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct ModelParams {
bool has_rs_rollback = false;
bool mixed_rope_params = false;
bool is_cacheless_attn = false;
bool is_dynamic_vision = false;
// the graph writes a transposed V cache (`-fa off`: one value element per row, tokens along the row)
bool v_trans = false;
// the graph has FLASH_ATTN_EXT with logit soft-capping (gemma-2)
Expand Down Expand Up @@ -140,6 +141,9 @@ struct ComputeParams {
std::map<std::string, int64_t> sampling_rows;
std::map<std::string, int64_t> gdn_attention_rows;
std::map<std::string, std::vector<int64_t>> recurrent_controls;
// GGML layout metadata that cannot be recovered after flattening a vision grid.
std::map<std::string, std::vector<int64_t>> vision_shapes;
std::map<std::string, std::vector<float>> vision_projections;
};

// defined below; declared here because GgmlOvDecoder uses it inline
Expand All @@ -150,6 +154,12 @@ bool is_moe_expert_sum_add(const ggml_tensor * node);

class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
public:
static bool is_dynamic_vision_graph(const ggml_cgraph * cgraph);
// OV order: -2 reads the runtime target; -1 infers one extent; nonnegative values are invariant.
static std::vector<int64_t> vision_reshape_pattern(const ggml_tensor * node);
std::vector<int64_t> get_vision_reshape_pattern(int node_idx) const override {
return vision_reshape_pattern(m_node_info_list[node_idx].node);
}
static std::string get_tensor_name(const ggml_cgraph * cgraph, const ggml_tensor * tensor);
struct NodeInfo {
ggml_tensor * node;
Expand Down Expand Up @@ -447,7 +457,10 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {

void clear_model_weights() { m_model_weights.clear(); }

static std::pair<ModelParams, ComputeParams> compute_llm_params(ggml_cgraph * cgraph, bool is_static);
// The execution path supplies the structural vision classification once per compute.
static std::pair<ModelParams, ComputeParams> compute_llm_params(ggml_cgraph * cgraph,
bool is_static,
bool dynamic_vision = false);

const ModelParams & get_model_params() const { return m_model_params; }

Expand Down Expand Up @@ -526,7 +539,10 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
tensor->ne[3] != 1 || (tensor->buffer != nullptr && tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS)) {
return false;
}
return (op->op == GGML_OP_RMS_NORM || op->op == GGML_OP_L2_NORM) && op->src[0] == tensor &&
// Gemma applies per-token scaling to supplied image embeddings.
const bool scaled_input = op->op == GGML_OP_MUL && (tensor->flags & GGML_TENSOR_FLAG_INPUT) &&
is_inp_scale_rows(op->src[1], op);
return (op->op == GGML_OP_RMS_NORM || op->op == GGML_OP_L2_NORM || scaled_input) && op->src[0] == tensor &&
(tensor->op == GGML_OP_NONE || tensor->op == GGML_OP_CPY);
}

Expand All @@ -553,7 +569,10 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {

// per-token embedding scale [1, n_tokens] (inp->scale_rows in llama-graph.cpp)
static bool is_inp_scale_rows(const ggml_tensor * tensor, const ggml_tensor * op) {
return op->op == GGML_OP_MUL && tensor == op->src[1] && strcmp(tensor->name, "inp_scale_rows") == 0;
return tensor && op->op == GGML_OP_MUL && tensor == op->src[1] &&
(tensor->flags & GGML_TENSOR_FLAG_INPUT) && tensor->type == GGML_TYPE_F32 &&
tensor->ne[0] == 1 && tensor->ne[2] == 1 && tensor->ne[3] == 1 &&
op->src[0]->ne[1] == tensor->ne[1] && op->src[0]->ne[2] == 1 && op->src[0]->ne[3] == 1;
}

static bool is_rope_freqs_weight(const ggml_tensor * tensor, const ggml_tensor * op) {
Expand Down Expand Up @@ -745,6 +764,8 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
op->src[0]->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY;
}

std::string get_position_input_ov_name(const ggml_tensor * tensor) const;

private:
void set_input_output();
int compute_op_case(const ggml_tensor * node) const;
Expand Down
22 changes: 20 additions & 2 deletions ggml/src/ggml-openvino/ggml-openvino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,12 +1542,20 @@ static bool is_supported_flash_attn_pattern(const ggml_tensor * op) {
// PERMUTE -> RESHAPE -> base (view_src==nullptr) (whisper Q)
// VIEW -> base (view_src==nullptr) (whisper K/V from kv_pad)
// PERMUTE -> VIEW -> RESHAPE -> base (Q of a layer without RoPE, e.g. SmolLM3)
// PERMUTE -> ROPE (view_src==nullptr) (cacheless vision Q/K)
// PERMUTE -> RMS_NORM (view_src==nullptr) (Gemma4 vision V)
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 (src->src[0]->op == GGML_OP_ROPE && src->src[0]->view_src == nullptr) {
continue;
}
if (i == 2 && src->src[0]->op == GGML_OP_RMS_NORM && src->src[0]->view_src == nullptr) {
continue;
}
// MLA: Q = PERMUTE -> CONCAT(q_nope_absorbed, q_pe); V = PERMUTE -> VIEW -> VIEW -> K cache
// (V is the first kv_lora_rank values of each K cache row)
if (i == 0 && src->src[0]->op == GGML_OP_CONCAT) {
Expand Down Expand Up @@ -1588,6 +1596,13 @@ static bool is_gemma3n_flash_attn_pattern(const ggml_tensor * op) {
return false;
}

// Gemma4 vision rotates the two head halves with consecutive spatial RoPEs.
const auto * q = op->src[0];
if (q->op == GGML_OP_PERMUTE && q->src[0]->op == GGML_OP_ROPE &&
q->src[0]->src[0] && q->src[0]->src[0]->op == GGML_OP_ROPE) {
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 =
Expand Down Expand Up @@ -1992,8 +2007,11 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
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_prec_get_src(op, 1) == GGML_PREC_BF16) {
return {false, "MUL_MAT with a BF16 src1 precision is not supported"};
}
if (ggml_prec_get_src(op, 1) == GGML_PREC_F32 && ggml_openvino_is_npu()) {
return {false, "MUL_MAT with F32 src1 precision requires CPU or GPU"};
}
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 &&
Expand Down
3 changes: 2 additions & 1 deletion ggml/src/ggml-openvino/model-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
// Version 9 hashes GGML_OPENVINO_ROPE_F32_ANGLE, which became opt-in.
// Version 10 hashes only whether GGML_OPENVINO_PROFILING is 2 or more (it then compiles with enable_profiling),
// and no longer GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS, which only changes the output bindings.
h = fnv1a_u64(h, 10);
// Version 11 honors explicit F32 MatMul input precision with node-level guards.
h = fnv1a_u64(h, 11);
h = fnv1a(h, graph_signature.data(), graph_signature.size());
for (const char * name : cache_settings) {
const char * value = ggml_openvino_getenv_str(name, "");
Expand Down
Loading
Loading