Skip to content

Update the llama.cpp to utilize OV frontend op translations - #342

Open
preetha-intel wants to merge 6 commits into
ravi9:dev_backend_openvinofrom
preetha-intel:ov-gguf-frontend
Open

preetha-intel wants to merge 6 commits into
ravi9:dev_backend_openvinofrom
preetha-intel:ov-gguf-frontend

Conversation

@preetha-intel

@preetha-intel preetha-intel commented Oct 12, 2026 •

Copy link
Copy Markdown

Add GGML decoder to support OV GGUF Frontend.

Overview

This PR adds an opt-in way for the OpenVINO backend to translate graphs with the OpenVINO GGUF frontend instead of the in-tree translator.

A new CMake option, GGML_OPENVINO_GGUF_FRONTEND, is OFF by default. When it is OFF, the build is exactly what dev_backend_openvino builds today. The in-tree translator sources, model-cache.cpp and ggml-openvino-extra.h are not modified, so open PRs against the in-tree translator are unaffected.

When it is ON, the backend is built from a new directory, ggml/src/ggml-openvino-ggml-frontend/. There, GgmlOvDecoder passes ggml graphs to the OpenVINO GGUF frontend (openvino::frontend::gguf), which owns the op translations. Configuration fails with an explicit error if the OpenVINO build does not provide that target.

Changes

New directory: ggml/src/ggml-openvino-ggml-frontend/

  • GgmlOvDecoder, the backend entry points and the graph-execution utilities for the frontend path. supports_op answers from a fixed op list that is kept in step with the frontend's op table.
  • Frontend-side model transformations: eltwise rank alignment (a workaround for a GPU-plugin issue), applying a constant scale before the MatMul on GPU/NPU so f16 does not overflow (Granite), KV-state sequence-axis handling and the stateful mask rewrite.
  • CMakeLists.cmake: the build rules for this mode, included from ggml-openvino/CMakeLists.txt.
  • The forwarding headers ggml-quants.h, model-cache.h and ggml-openvino-extra.h point to the copies in ggml-openvino/. Those files are compiled once, in both modes, so a fix to quantization, the model cache or device selection applies to both paths.
  • Frontend-only settings stay in this directory. The helper for GGML_OPENVINO_DISABLE_SCALE_REORDER is defined here, and the setting is hashed into this path's own compiled-model cache key.
  • README.md: build instructions and the frontend-only environment variables.

CMake changes

  • ggml-openvino now links openvino::frontend::gguf.
  • Configuration fails with a clear error if that target isn't found.
  • The source globs are no longer recursive, since the subdirectories are gone.

Openvino Frontend changes PR : openvinotoolkit/openvino#38769

@preetha-intel
preetha-intel marked this pull request as draft October 12, 2026 10:17
@mostafafaheem
mostafafaheem changed the base branch from master to dev_backend_openvino October 12, 2026 12:58
@mostafafaheem
mostafafaheem changed the base branch from dev_backend_openvino to master October 12, 2026 12:59
@mostafafaheem

Copy link
Copy Markdown
Collaborator

Thanks for contributing! Development happens on dev_backend_openvino branch. Please rebase accordingly

@ravi9
ravi9 changed the base branch from master to dev_backend_openvino October 16, 2026 16:16
@ravi9
ravi9 marked this pull request as ready for review October 16, 2026 16:46
@cavusmustafa

Copy link
Copy Markdown
Collaborator

Could we stage this migration behind an opt-in CMake flag, keeping the existing translations as the default for now?

This change replaces a substantial translation and fusion layer. Retaining the legacy path temporarily would let us compare correctness, model/device coverage, and performance, while providing an easy rollback. The GGUF frontend dependency should only be required when the new path is enabled.

Once validation gives us confidence, we can switch the default and remove the legacy implementation in a follow-up PR.

@ravi9, what do you think about taking this incremental approach?

@ravi9

ravi9 commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Sure, an incremental approach with the opt-in CMake flag sounds good.

@ravi9
ravi9 force-pushed the dev_backend_openvino branch from 61a6258 to df21ad8 Compare October 20, 2026 18:50
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Oct 23, 2026
@ravi9
ravi9 force-pushed the dev_backend_openvino branch 4 times, most recently from be796d8 to 25e3135 Compare October 29, 2026 17:37
@shahkarnav115-beep

Copy link
Copy Markdown

hey @ravi9 and @cavusmustafa!!
Waiting for these PR to get merged in dev_backend_openvino for future PR regarding GGUFReaderV2. I haven't opened it yet due to large refactors going on as you mentioned earlier. These PR will really helpfull for GGUFReaderV2.

Thank you!!

@ravi9
ravi9 force-pushed the dev_backend_openvino branch 2 times, most recently from ba80126 to 45e6c13 Compare November 23, 2026 05:36
@preetha-intel

Copy link
Copy Markdown
Author

@cavusmustafa @ravi9 I have all my changes moved to a new directory and added the cmake option. Can you review and merge this.

@cavusmustafa

cavusmustafa commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

@cavusmustafa @ravi9 I have all my changes moved to a new directory and added the cmake option. Can you review and merge this.

Thanks for the update @preetha-intel. Looking into the changes, I see two main folders for both legacy frontend and gguf frontend. This could lead to have duplicate copies of same code structures and also I don't think this would be approved to be merged into llama.cpp upstream repo.

image

Instead, is it possible to convert this into a structure that they still share the same directory but differs in a few places below?

  1. Decoder adapter. GgmlOvDecoder keeps the shared graph analysis (inputs/outputs, model/compute params, KV/recurrent detection, dynamic dims) with no OV frontend base class. Two thin adapters sit on top of it:
    • GgmlOvLegacyDecoder : GgmlOvDecoder, ov::frontend::ggml::GgmlDecoder: index-based get_view_input_*, op_params, op_case
    • GgmlOvGgufDecoder : GgmlOvDecoder, ov::frontend::gguf::GgufDecoder: get_attribute, describe_view, describe_kv_view, node-scoped visit_subgraph
  2. Conversion. The FrontEnd::convert call sites in utils.cpp go through one function.
  3. supports_op rules. The op_table-derived rules for legacy, the static list for GGUF.
ggml/src/ggml-openvino/
  CMakeLists.txt            single file; GGML_OPENVINO_GGUF_FRONTEND only picks the frontend subdir (+ links openvino::frontend::gguf)
  ggml-openvino.cpp, utils.*, ggml-decoder.* (core), ggml-quants.*, model-cache.*, ggml-openvino-extra.*, device/*   shared
  openvino/pass/, openvino/pass_registry/      shared post-conversion passes (one copy of align_eltwise_ranks / kv_state_seq_axis)
  frontend.h                the seam, see below
  frontend-ggml/            existing translator (op/, translate_session, op_table, ...) + legacy adapter + its supports_op
  frontend-gguf/            GGUF adapter + gguf::FrontEnd convert/extensions + static op list + rewrite_stateful_masks
// frontend.h: exactly one implementation is compiled in
std::shared_ptr<GgmlOvDecoder> ov_frontend_make_decoder(...);
std::shared_ptr<ov::Model>     ov_frontend_convert(const std::shared_ptr<GgmlOvDecoder> & decoder, bool naive);
ggml_openvino_op_support       ov_frontend_supports_op(const ggml_tensor * op);
const char *                   ov_frontend_name();

@ravi9
ravi9 force-pushed the dev_backend_openvino branch from fcf6088 to 2371fe4 Compare November 26, 2026 03:53
@preetha-intel

preetha-intel commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

@cavusmustafa @ravi9 I have all my changes moved to a new directory and added the cmake option. Can you review and merge this.

Thanks for the update @preetha-intel. Looking into the changes, I see two main folders for both legacy frontend and gguf frontend. This could lead to have duplicate copies of same code structures and also I don't think this would be approved to be merged into llama.cpp upstream repo.

image Instead, is it possible to convert this into a structure that they still share the same directory but differs in a few places below?
  1. Decoder adapter. GgmlOvDecoder keeps the shared graph analysis (inputs/outputs, model/compute params, KV/recurrent detection, dynamic dims) with no OV frontend base class. Two thin adapters sit on top of it:

    • GgmlOvLegacyDecoder : GgmlOvDecoder, ov::frontend::ggml::GgmlDecoder: index-based get_view_input_*, op_params, op_case
    • GgmlOvGgufDecoder : GgmlOvDecoder, ov::frontend::gguf::GgufDecoder: get_attribute, describe_view, describe_kv_view, node-scoped visit_subgraph
  2. Conversion. The FrontEnd::convert call sites in utils.cpp go through one function.

  3. supports_op rules. The op_table-derived rules for legacy, the static list for GGUF.

ggml/src/ggml-openvino/
  CMakeLists.txt            single file; GGML_OPENVINO_GGUF_FRONTEND only picks the frontend subdir (+ links openvino::frontend::gguf)
  ggml-openvino.cpp, utils.*, ggml-decoder.* (core), ggml-quants.*, model-cache.*, ggml-openvino-extra.*, device/*   shared
  openvino/pass/, openvino/pass_registry/      shared post-conversion passes (one copy of align_eltwise_ranks / kv_state_seq_axis)
  frontend.h                the seam, see below
  frontend-ggml/            existing translator (op/, translate_session, op_table, ...) + legacy adapter + its supports_op
  frontend-gguf/            GGUF adapter + gguf::FrontEnd convert/extensions + static op list + rewrite_stateful_masks
// frontend.h: exactly one implementation is compiled in
std::shared_ptr<GgmlOvDecoder> ov_frontend_make_decoder(...);
std::shared_ptr<ov::Model>     ov_frontend_convert(const std::shared_ptr<GgmlOvDecoder> & decoder, bool naive);
ggml_openvino_op_support       ov_frontend_supports_op(const ggml_tensor * op);
const char *                   ov_frontend_name();

Agreed on the single directory. The requested changes is still a work in progress. But I am in need of few clarifications.

I refactored the current PR with a state that the legacy path will be replaced once GGUF frontend validation is done, so removing it later should be a simple rename and not another restructure.

** Regarding Index accessors**

The GGUF frontend doesn't need the index-based accessors. Indices only exist in the legacy path because its translator reads the whole graph through one decoder and addresses nodes by position. The GGUF frontend gets one node-scoped decoder per node from visit_subgraph(), so its accessors take no node_idx. Views and KV reads go through typed attributes (describe_view, describe_kv_view) instead of get_view_input_*.

As a result, the index lives only in GgmlOvLegacyDecoder. The shared GgmlOvDecoder core keeps the per-node info list, and each adapter decides how to expose it, so neither frontend base class leaks into the core.

Impact on other open PRs with the suggested structure

  • Renames: most of the openvino/ → frontend-ggml/ moves are pure renames (57 of ~65 are 100%, the rest 69–99%), so a normal merge or rebase should follow them.
  • New op translators: these need to go under frontend-ggml/op/ and be registered in the relocated op_table.cpp.
  • Patches and cherry-picks: these won't follow the renames reliably.

@ravi9
ravi9 force-pushed the dev_backend_openvino branch 3 times, most recently from 4c0d07b to 12b1ca5 Compare December 1, 2026 16:55
@cavusmustafa

Copy link
Copy Markdown
Collaborator

@cavusmustafa @ravi9 I have all my changes moved to a new directory and added the cmake option. Can you review and merge this.

Thanks for the update @preetha-intel. Looking into the changes, I see two main folders for both legacy frontend and gguf frontend. This could lead to have duplicate copies of same code structures and also I don't think this would be approved to be merged into llama.cpp upstream repo.
image
Instead, is it possible to convert this into a structure that they still share the same directory but differs in a few places below?

  1. Decoder adapter. GgmlOvDecoder keeps the shared graph analysis (inputs/outputs, model/compute params, KV/recurrent detection, dynamic dims) with no OV frontend base class. Two thin adapters sit on top of it:

    • GgmlOvLegacyDecoder : GgmlOvDecoder, ov::frontend::ggml::GgmlDecoder: index-based get_view_input_*, op_params, op_case
    • GgmlOvGgufDecoder : GgmlOvDecoder, ov::frontend::gguf::GgufDecoder: get_attribute, describe_view, describe_kv_view, node-scoped visit_subgraph
  2. Conversion. The FrontEnd::convert call sites in utils.cpp go through one function.

  3. supports_op rules. The op_table-derived rules for legacy, the static list for GGUF.

ggml/src/ggml-openvino/
  CMakeLists.txt            single file; GGML_OPENVINO_GGUF_FRONTEND only picks the frontend subdir (+ links openvino::frontend::gguf)
  ggml-openvino.cpp, utils.*, ggml-decoder.* (core), ggml-quants.*, model-cache.*, ggml-openvino-extra.*, device/*   shared
  openvino/pass/, openvino/pass_registry/      shared post-conversion passes (one copy of align_eltwise_ranks / kv_state_seq_axis)
  frontend.h                the seam, see below
  frontend-ggml/            existing translator (op/, translate_session, op_table, ...) + legacy adapter + its supports_op
  frontend-gguf/            GGUF adapter + gguf::FrontEnd convert/extensions + static op list + rewrite_stateful_masks
// frontend.h: exactly one implementation is compiled in
std::shared_ptr<GgmlOvDecoder> ov_frontend_make_decoder(...);
std::shared_ptr<ov::Model>     ov_frontend_convert(const std::shared_ptr<GgmlOvDecoder> & decoder, bool naive);
ggml_openvino_op_support       ov_frontend_supports_op(const ggml_tensor * op);
const char *                   ov_frontend_name();

Agreed on the single directory. The requested changes is still a work in progress. But I am in need of few clarifications.

I refactored the current PR with a state that the legacy path will be replaced once GGUF frontend validation is done, so removing it later should be a simple rename and not another restructure.

** Regarding Index accessors**

The GGUF frontend doesn't need the index-based accessors. Indices only exist in the legacy path because its translator reads the whole graph through one decoder and addresses nodes by position. The GGUF frontend gets one node-scoped decoder per node from visit_subgraph(), so its accessors take no node_idx. Views and KV reads go through typed attributes (describe_view, describe_kv_view) instead of get_view_input_*.

As a result, the index lives only in GgmlOvLegacyDecoder. The shared GgmlOvDecoder core keeps the per-node info list, and each adapter decides how to expose it, so neither frontend base class leaks into the core.

Impact on other open PRs with the suggested structure

  • Renames: most of the openvino/ → frontend-ggml/ moves are pure renames (57 of ~65 are 100%, the rest 69–99%), so a normal merge or rebase should follow them.
  • New op translators: these need to go under frontend-ggml/op/ and be registered in the relocated op_table.cpp.
  • Patches and cherry-picks: these won't follow the renames reliably.

Hi @preetha-intel, for 'node_idx', the proposed solution sounds good. Is there any blockers for it?

For rename, that's right it could impact all other PRs as the folder name change. Maybe we can start with keeping the legacy folder name as "openvino" as a first step? Still this PR will be a big change and we cannot fully avoid impacting other PRs but this could minimize it.

@ravi9
ravi9 force-pushed the dev_backend_openvino branch 2 times, most recently from a546d03 to c3358cb Compare December 2, 2026 04:05
@preetha-intel

Copy link
Copy Markdown
Author

I have updated the file structure as per the earlier comment to maintain the common code for both llama.cpp and ov frontend. But its highly conflicting with any pushes to the master as it touches the core llama.cpp files.
Can we merge this change first or I need to revert to my earlier status of having the gguf-frontend support standalone

@ravi9
ravi9 force-pushed the dev_backend_openvino branch from 27fb980 to fe2c7b6 Compare December 2, 2026 19:31
@ravi9

ravi9 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

@preetha-intel: I've updated the contributing guide. Review it, and you could also share it with your AI agent so it helps with the PRs.

Following are a few updates and comments based on AI agent review:

Rebased Notes:

  • Conflicts:
    • CMakeLists.txt: dev removed the GGML_OPENVINO_HAS_CACHE_UPDATE check; kept dev.
    • docs: kept the dev rows; added the GGUF note and the GGML_OPENVINO_DISABLE_SCALE_REORDER row.
    • e16eb88: the dev changes merged since the branch's last merge are ported to the new layout. Q2_0 (c3727fd) in supports-op.cpp and ggml-openvino.cpp. Nemotron/LFM3 (e20c453): the Mamba check and the GPU MUL op case in frontend-ggml/legacy-decoder.cpp, the GATED_DELTA_NET check in supports-op.cpp, openvino/op/mul.cpp moved to frontend-ggml/op/mul.cpp. Please check these ports.
  • After the rebase, the only file outside ggml/src/ggml-openvino was ggml/CMakeLists.txt; 5728d35 removes it, so the branch no longer touches core llama.cpp files.

Fixed in 5728d35

  • The GGML_OPENVINO_GGUF_FRONTEND option moved from ggml/CMakeLists.txt to ggml/src/ggml-openvino/CMakeLists.txt (fork PRs change only the OpenVINO paths).
  • frontend-gguf/ov-frontend.cpp: removed the duplicated comment before ov_frontend_convert.
  • frontend-ggml/legacy-decoder.cpp: removed the reference to recurrent_cache_fusion.cpp, which dev removed.

Results (default OFF build, OpenVINO 2026.5, Panther Lake)

Check dev PR
Build, -DLLAMA_FATAL_WARNINGS=ON 0 warnings 0 warnings
ctest, CPU and GPU 21/21 21/21
test-backend-ops, 7 ops, CPU and GPU 3 MUL_MAT mxfp4 cases fail on CPU same
Greedy llama-cli, 5 models x CPU/GPU/NPU reference identical output in 15/15 cells
llama-embedding, 5 models x CPU/GPU/NPU 8 pass identical output in 15/15 cells
Build with -DGGML_OPENVINO_GGUF_FRONTEND=ON n/a fails (item 1)

Remaining issues

  1. The ON build fails with OpenVINO 2026.5. The 2026.5 package already exports openvino::frontend::gguf, so the target check in CMakeLists.txt passes and its error never shows. The build stops at frontend-gguf/ov-frontend.cpp:23: openvino/frontend/gguf/push_scale_before_matmul.hpp: No such file or directory. The configure step needs to check for that header, and frontend-gguf/README.md needs to name the OpenVINO build that has it (Update OV gguf Frontend  openvinotoolkit/openvino#38769).
  2. Open translator PRs break with the frontend-ggml/ rename. CMakeLists.txt still compiles every openvino/**/*.cpp, while the translator headers moved to frontend-ggml/. A new file under openvino/op/ fails to build; this happened to dev's mul.cpp in this rebase. Edits to compute_op_case and supports_op also conflict. Keeping the in-tree translator in openvino/, as suggested on 2026-12-01, removes most of this; the single-directory structure itself can stay, and the standalone layout is not needed.
  3. The default build changes in shared code, although the PR body says it is the dev build:
    • utils.cpp:726: a view whose shape differs from its root is bound from its own data.
    • utils.cpp:1405 and :1797: the stateful KV size grows by get_inp_pos_n_tokens() instead of inp_pos->ne[3].
    • utils.cpp:2626: NPU graphs without inp_pos (bge-small, bge-m3) now pass the "inp_pos not found" error and fail later in the NPU compiler.
      These look like fixes; they belong in a separate PR, or in the PR body with their own tests.
  4. The PR body and the messages of 3cd9def and b695667 are stale: they name ggml/src/ggml-openvino-ggml-frontend/, CMakeLists.cmake and forwarding headers, say the globs are not recursive, and say ggml-openvino-extra.h is unchanged.
  5. Minor: clang-format (backend .clang-format) changes about 575 lines of frontend-gguf/gguf-decoder.cpp, 76 of ov-frontend.cpp and 19 of frontend.h; frontend-gguf/supports-op.cpp:23 is 150 characters; commit subjects use "ggml-openvino: " or no prefix, and the fork now uses "openvino: ".

Next steps

  1. Fetch ov-gguf-frontend and reset to it before adding commits.
  2. Add the fixes for items 1-5 as commits on top;
  3. ON mode is not verified: it needs a run with an OpenVINO build that has the GGUF frontend changes.

@ravi9
ravi9 force-pushed the dev_backend_openvino branch 4 times, most recently from 5b4342a to a274a39 Compare December 4, 2026 16:14
preetha-intel and others added 6 commits December 4, 2026 21:46
Add GGML_OPENVINO_GGUF_FRONTEND (default OFF). When ON, the backend is built
from ggml/src/ggml-openvino-ggml-frontend/, where GgmlOvDecoder is a bridge to
the OpenVINO GGUF frontend (openvino::frontend::gguf), which owns the op
translations; the supports_op answer is a static op list kept in step with the
frontend's op table. The in-tree translator in ggml/src/ggml-openvino/ stays
untouched and remains the default, so changes to it keep applying.

The frontend variant carries the earlier decoder/utils work: eltwise rank
alignment, scale-before-MatMul on GPU/NPU to keep f16 in range, SWA/KV-state
handling and the stateful mask rewrite.

Also wire the new directory into CODEOWNERS, the labeler, the OpenVINO CI path
filters and docs/backend/OPENVINO.md.

Squashes the previous branch commits (legacy bridge removal, eltwise rank
alignment, scale-before-MatMul, opt-in CMake option) after the rebase onto
the rewritten dev_backend_openvino: their net effect on ggml-openvino/ was the
CMake switch only.
The frontend directory duplicated ggml-quants, ggml-openvino-extra and
model-cache from the in-tree backend. Keep a single copy in ggml-openvino/,
compile it in both build modes, and leave forwarding headers in
ggml-openvino-ggml-frontend/ so a fix to quantization, the model cache or
device selection lands once.

Add ggml_openvino_manual_gqa_attn_enabled() and
ggml_openvino_scale_reorder_enabled() (GGML_OPENVINO_DISABLE_SCALE_REORDER) to
the shared device config, and include the new switch in the model-cache key.
…dapters

Fold ggml-openvino-ggml-frontend/ into ggml-openvino/. The backend is shared;
frontend.h is the seam to the frontend that converts ggml graphs, and exactly
one implementation is built: frontend-ggml/ (default) or frontend-gguf/
(GGML_OPENVINO_GGUF_FRONTEND=ON).

GgmlOvDecoder keeps the shared graph analysis with no OV frontend base class.
GgmlOvLegacyDecoder adds the index-based accessors, op_params and op_case for
the in-tree translator; GgmlOvGgufDecoder adds get_attribute, describe_view,
describe_kv_view and node-scoped visit_subgraph for the GGUF frontend.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml OpenVINO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants