Skip to content
Open
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
3 changes: 3 additions & 0 deletions docs/backend/OPENVINO.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ cmake -B build/ReleaseOV -G Ninja -DCMAKE_BUILD_TYPE=Release -DGGML_OPENVINO=ON
cmake --build build/ReleaseOV --parallel
```

> [!NOTE]
> To translate with the OpenVINO GGUF frontend instead of the default in-tree translator, add `-DGGML_OPENVINO_GGUF_FRONTEND=ON`; see [ggml/src/ggml-openvino/frontend-gguf/README.md](../../ggml/src/ggml-openvino/frontend-gguf/README.md).

```cmd
REM Windows: x64 Native Tools Command Prompt for VS, so that the MSVC toolchain is on PATH
git clone https://github.com/ggml-org/llama.cpp
Expand Down
27 changes: 27 additions & 0 deletions ggml/src/ggml-openvino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,42 @@ find_package(OpenCL REQUIRED)

message(STATUS "Found OpenVINO: ${OpenVINO_DIR} (found version \"${OpenVINO_VERSION}\")")

# The backend is the same for every frontend; frontend.h is the seam to the OpenVINO frontend that converts ggml graphs,
# and exactly one implementation is built: the in-tree translator in frontend-ggml/ (default), or the OpenVINO GGUF
# frontend in frontend-gguf/ with GGML_OPENVINO_GGUF_FRONTEND=ON.
option(GGML_OPENVINO_GGUF_FRONTEND "ggml: OPENVINO backend translates graphs with the OpenVINO GGUF frontend (needs openvino::frontend::gguf) instead of the in-tree legacy translator" OFF)
if (GGML_OPENVINO_GGUF_FRONTEND)
if (NOT TARGET openvino::frontend::gguf)
message(FATAL_ERROR "GGML_OPENVINO_GGUF_FRONTEND=ON requires the OpenVINO GGUF frontend target "
"openvino::frontend::gguf, which this OpenVINO (${OpenVINO_DIR}) does not provide. "
"Use an OpenVINO build that includes the GGUF frontend, or configure with "
"-DGGML_OPENVINO_GGUF_FRONTEND=OFF to use the in-tree translator.")
endif()
set(GGML_OPENVINO_FRONTEND_DIR frontend-gguf)
else()
set(GGML_OPENVINO_FRONTEND_DIR frontend-ggml)
endif()
message(STATUS "OpenVINO backend: converting graphs with ${GGML_OPENVINO_FRONTEND_DIR}/")

file(GLOB_RECURSE GGML_HEADERS_OPENVINO "*.h" "*.hpp")
file(GLOB_RECURSE GGML_SOURCES_OPENVINO "*.cpp")
# only the selected frontend
list(FILTER GGML_HEADERS_OPENVINO EXCLUDE REGEX "/frontend-(ggml|gguf)/")
list(FILTER GGML_SOURCES_OPENVINO EXCLUDE REGEX "/frontend-(ggml|gguf)/")
file(GLOB_RECURSE GGML_HEADERS_OPENVINO_FRONTEND "${GGML_OPENVINO_FRONTEND_DIR}/*.h" "${GGML_OPENVINO_FRONTEND_DIR}/*.hpp")
file(GLOB_RECURSE GGML_SOURCES_OPENVINO_FRONTEND "${GGML_OPENVINO_FRONTEND_DIR}/*.cpp")

ggml_add_backend_library(ggml-openvino
${GGML_SOURCES_OPENVINO}
${GGML_HEADERS_OPENVINO}
${GGML_SOURCES_OPENVINO_FRONTEND}
${GGML_HEADERS_OPENVINO_FRONTEND}
)

target_link_libraries(ggml-openvino PRIVATE openvino::runtime openvino::threading OpenCL::OpenCL)
if (GGML_OPENVINO_GGUF_FRONTEND)
target_link_libraries(ggml-openvino PRIVATE openvino::frontend::gguf)
endif()

# the OpenVINO RTTI macros take one argument and leave __VA_ARGS__ empty, which -Wpedantic reports
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
Expand Down
19 changes: 14 additions & 5 deletions ggml/src/ggml-openvino/dev_doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ When a `ggml_cgraph` is dispatched to the OpenVINO backend, it:

## Source layout

The backend is shared by two OpenVINO frontends, of which one is compiled in ([`frontend.h`](../frontend.h) is the seam):

- `ggml-openvino.cpp`, `utils.*`, `ggml-decoder.*`, `supports-op.cpp`, `ggml-quants.*`, `model-cache.*`, `ggml-openvino-extra.*`, `device/`: buffers, graph analysis (`GgmlOvDecoder`), compilation, caching and inference, independent of the frontend. `supports-op.cpp` holds every `supports_op` rule (devices, numerics, graph patterns), so both frontends accept the same cases.
- `openvino/`: post-conversion passes (`pass/`, `pass_registry/`), custom OpenVINO ops (`op/*.hpp`) and runtime info, shared by both frontends.
- `frontend-ggml/`: the in-tree GGML-to-OpenVINO translator (default), its decoder adapter `GgmlOvLegacyDecoder` and its `supports_op` hook (the ops of its op table and the variants it does not translate).
- `frontend-gguf/`: the OpenVINO GGUF frontend path (`GGML_OPENVINO_GGUF_FRONTEND=ON`), its decoder adapter `GgmlOvGgufDecoder` and its `supports_op` hook (its op list and the variants it does not translate). See [its README](../frontend-gguf/README.md).

Backend files, relative to `ggml/src/ggml-openvino/`, and OpenVINO GPU plugin parts:

| File | Content |
|---|---|
| `ggml-openvino.cpp` | Backend, device and buffer interfaces; op support checks, including the replay op checks |
| `ggml-openvino.cpp` | Backend, device and buffer interfaces |
| `supports-op.cpp` | Op support checks, including the replay op checks |
| `ggml-openvino-extra.cpp` | Registry of the environment variables, value checks and device selection |
| `ggml-decoder.cpp` | Dynamic dimensions, control inputs, output names and the `cgraph_ov.txt` dump |
| `ggml-decoder.cpp` | Dynamic dimensions, graph inputs and outputs, and the `cgraph_ov.txt` dump |
| `frontend-ggml/legacy-decoder.cpp` | Control inputs, output names and op cases of the in-tree translator |
| `utils.cpp` | Compute-branch filtering, compiled reuse, input/output binding, and replay writeback through `query_state()` |
| `model-cache.cpp` | Compiled model cache on disk: cache key (`cache_settings[]`) and source file identity |
| `ggml-quants.cpp` | Extraction and requantization of the GGUF weights |
| `device/npu_config.cpp`, `device/npu_quant_policy.cpp` | NPU plugin properties and NPU weight layouts |
| `openvino/translate_session.cpp` | Runtime sampling aliases, model construction and replay `Assign` sinks |
| `openvino/recurrent_replay_fusion.cpp` | GGML group recognition and standard-op replay translation |
| `openvino/op/` | One translator for each GGML op. `recurrent_replay_graph.hpp` emits the replay pattern. |
| `frontend-ggml/translate_session.cpp` | Runtime sampling aliases, model construction and replay `Assign` sinks |
| `frontend-ggml/recurrent_replay_fusion.cpp` | GGML group recognition and standard-op replay translation |
| `frontend-ggml/op/` | One translator for each GGML op. `openvino/op/recurrent_replay_graph.hpp` emits the replay pattern. |
| `openvino/pass/` | OpenVINO graph passes, for example `keep_cache_results.cpp` for the KV-only prefill |
| OV `recurrent_replay_fusion.cpp` and `recurrent_cache_replay` | Structural matching, writable-input validation, source staging and GPU computation |

Expand Down
7 changes: 5 additions & 2 deletions ggml/src/ggml-openvino/device/npu_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ ov::AnyMap ggml_openvino_npu_without_npuw(const ov::AnyMap & config) {

ov::AnyMap ggml_openvino_npu_prefill_config(const ov::AnyMap & base, const GgmlOvDecoder & decoder) {
ov::AnyMap prefill_config = base;
if (ggml_openvino_npu_hfa_prefill_enabled()) {
prefill_config["NPUW_ATTN_HISTORY_SIZE_INPUT"] = decoder.get_hfa_history_input_name();
// HFA needs a history-size input, which only some frontends create (see GgmlOvDecoder::get_hfa_history_input_name)
const std::string hfa_history_input =
ggml_openvino_npu_hfa_prefill_enabled() ? decoder.get_hfa_history_input_name() : std::string();
if (!hfa_history_input.empty()) {
prefill_config["NPUW_ATTN_HISTORY_SIZE_INPUT"] = hfa_history_input;
prefill_config["NPUW_ONLINE_PIPELINE"] = "REP";
// keep the tags of a MoE graph
const auto isolate = prefill_config.find("NPUW_ONLINE_ISOLATE");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "../ggml-decoder.h"

#include <cstdint>
#include <map>
#include <openvino/core/node.hpp>
Expand All @@ -13,22 +15,9 @@ namespace ov {
namespace frontend {
namespace ggml {

struct ModelInputInfo {
element::Type type;
PartialShape shape;
};

struct ModelExtraInputInfo {
element::Type type;
Shape shape;
int64_t value;
bool is_parameter;
// when set, the value is read from dimension shape_axis of this model input instead of a Parameter
std::string shape_source;
int64_t shape_axis = -1;
// Optional model shape for runtime controls whose allocation changes per compute.
PartialShape parameter_shape = PartialShape::dynamic();
};
// Model input descriptions are shared with the frontend-independent graph analysis (../ggml-decoder.h).
using ::ModelExtraInputInfo;
using ::ModelInputInfo;

class GgmlDecoder : public DecoderBase {
public:
Expand Down
Loading