Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

14.9 KB
Raw Blame

Extending the GGUF frontend

Extensions add architecture or projector builders, operation converters or normalization passes to one GGUF frontend instance, as C++ objects or from a shared library, without rebuilding OpenVINO. The builder headers in dev_api/openvino/frontend/gguf ship with the frontend development package (openvino::frontend::gguf) and have no compatibility guarantee across releases: build against the release that will load the extension (C++17).

Choose an extension

What you need Extension Register before
New architecture with the existing decoder topology ov::frontend::gguf::ArchitectureExtension with a decoder definition load()
Different topology or model family ArchitectureExtension with a ModelBuilder factory load()
Add or override one mmproj encoder/projector branch ov::frontend::gguf::ProjectorExtension load()
Add or override an operation converter ov::frontend::ConversionExtension convert()
Prepare a stateful decoder with GenAI IO ov::frontend::gguf::adapt_to_genai() After convert()
Change state handling or the model interface ov::frontend::DecoderTransformationExtension convert()

Architecture and projector extensions apply only to native .gguf files; converters and passes apply to both input paths. None of them add quantization formats, tokenization, preprocessing or a device backend. Extensions attached to an ov::BaseOpExtension and shared-library wrappers are unwrapped; a bare custom operation supplies no GGML converter. TelemetryExtension is stored but never invoked; other types are ignored.

GenAI integration in PR #4683 accepts extension objects or library paths through ov::genai::extensions(...) in C++, or library paths through extensions=[...] in Python. For LLMPipeline and ContinuousBatchingPipeline, also select GGUF_READER="FRONTEND". Extensions are registered before loading on every GGUF frontend, including both language and projector frontends in VLMPipeline.

Lifecycle

Use one frontend instance for registration, loading and conversion:

#include "openvino/frontend/gguf/extension/architecture.hpp"
#include "openvino/frontend/manager.hpp"

ov::frontend::FrontEndManager manager;
auto frontend = manager.load_by_framework("gguf");
frontend->add_extension(std::make_shared<ov::frontend::gguf::ArchitectureExtension>(
    "my-decoder", ov::frontend::gguf::RopeMode::Neox));
auto model = frontend->convert(frontend->load("my-decoder.gguf"));
  1. load() parses the file, resolves the architecture handler and saves it with a snapshot of the projector registry. Later registrations affect only later loads.
  2. convert() merges built-in and registered converters, builds a fresh graph through the selected ModelBuilder, and resolves each mmproj modality against the saved projector registry. GgufGraphContext::node() invokes converters immediately, so builders see inferred shapes.
  3. Normalization runs extension passes in registration order, after marking compressed constants and before LowerSetRowsStateless and the remaining cleanup.

Registrations on another frontend, or on a consumer that does not forward them, have no effect.

Architecture extensions

The short constructor takes the literal general.architecture and its RoPE mode. When tensors and metadata cannot determine a choice, pass a definition with options:

using namespace ov::frontend::gguf;
auto definition = make_decoder_architecture(
    "my-decoder", RopeMode::Neox,
    [](const GgufMetadata&) {
        DecoderOptions options;
        options.geglu = true;
        return options;
    });
frontend->add_extension(std::make_shared<ArchitectureExtension>(definition));

ArchitectureExtension(ArchitectureDefinition) also accepts a custom ModelBuilder factory with no RoPE mode. See architectures.md for options, builders, porting and validation.

A callback returning DecoderParameters gives full control over decoder dimensions, normalization, scales, layer/RoPE/SWA settings, routing and model contracts. It bypasses the convenience helper's built-in architecture defaults; see complete parameters. A custom whole-model factory can replace topology using generic graph operations or the shared blocks through configure_decoder(parameters).

Matching and replacement

A handler matches on general.architecture, then its optional predicate. Handlers with different ids may share an architecture only with disjoint predicates; if two claim one file, loading fails regardless of order. For mmproj branches use ProjectorExtension, not another clip handler.

RegistrationMode::Add (default) rejects a duplicate id. To replace a handler, including a built-in, keep its id and register with RegistrationMode::Replace:

frontend->add_extension(std::make_shared<ArchitectureExtension>(definition, RegistrationMode::Replace));

Replacing an unknown id fails; a new id for a claimed architecture causes an ambiguous match. Replacement affects only this frontend's registry, and ArchRegistry::supported_archs() reflects it.

Extend mmproj with a projector component

ProjectorExtension derives from ArchitectureExtension and is packaged the same way, but registers in a separate projector registry. It builds one vision or audio branch; the clip coordinator selects branches as described in mmproj.md, names outputs vision.embeddings / audio.embeddings and finishes the model.

Decoder builders declare GenAI adaptation through parameters.model_contract. The optional supports_genai_adaptation takes precedence over genai.enabled; genai also specifies multimodal positions, causal or context-prefix attention, preserved inputs and auxiliary output mappings. Logits are first by default; genai.logits_first can preserve legacy ordering. Extensions may override these choices without changing AdaptToGenAI. Models saved without the contract use the builder-owned legacy resolver.

A ProjectorDefinition has id, architecture, modality (vision or audio), projector_type, build and optional match, genai_layout and genai_adapter. build appends to the shared GgufGraphContext and returns ProjectorResult: the output and optional string configuration under the modality prefix. It must not call finish() or register the primary output; extra inputs need unique names. A predicate may distinguish variants of one resolved type; two matching handlers are an error.

Reuse a built-in topology for a new projector type when the metadata and weights are compatible:

#include "openvino/frontend/gguf/extension/projector.hpp"

using namespace ov::frontend::gguf;
ProjectorDefinition definition{
    "clip.vision.my-gemma3", "clip", "vision", "my-gemma3",
    [](GgufGraphContext& graph) {
        return build_builtin_projector(graph, "vision", "gemma3");
    },
    {}};
frontend->add_extension(std::make_shared<ProjectorExtension>(definition));

A new topology instead builds its branch from graph.metadata(), graph.tensors() and graph.node():

ProjectorDefinition audio{
    "clip.audio.example", "clip", "audio", "example-audio",
    [](GgufGraphContext& graph) {
        auto input = graph.add_input("audio.example_input", ov::element::f32, {1, 1, -1, 8});
        auto output = graph.node("GGML_OP_SCALE", {input}, 0, {{"scale", 2.0f}, {"bias", 0.0f}});
        return ProjectorResult{output, {{"audio.merge", "1"}}};
    },
    {}};

To replace one built-in branch, reuse its id (for example clip.vision.gemma3) with RegistrationMode::Replace; other branches and the clip.mmproj coordinator stay active. The component list comes from ArchRegistry::projectors().supported_projectors(). An architecture other than clip would also need its own whole-model coordinator.

The builder selects GenAI layout explicitly with genai_layout. Recipes describe input semantics (Image, PaddedPatches, WindowedPatches, SplitMerger, PackedPatches, TemporalPackedPatches) and are serialized with the model. build_builtin_projector() returns the recipe matching its graph. A result's explicit recipe overrides the definition. A new or replacement projector declaring neither gets Unsupported; it does not inherit a recipe from its projector name. Only legacy models without vision.genai_layout use a built-in recipe selected by vision.projector.

Audio builders declare audio_layout separately. The Gemma 4 audio layouts select the matching GenAI input preprocessing and are serialized as audio.genai_layout; custom aliases can return the layout from build_builtin_projector(). A replacement that declares no audio layout remains unchanged, even if it reuses a built-in projector id. Only legacy models without audio.genai_layout use the built-in audio projector name.

A projector may declare its components with configure_model_contract(). For lfm3vl, lfm3a and lfm3a_gen models the coordinator reads that declaration through model_contract() and adds built-in component groups only where the projector declared none; declared recurrent batching, outputs and state prefixes are preserved. build_builtin_projector() also builds lfm3vl (vision) and lfm3a (audio).

A projector can declare equally sized feature blocks through string entries in ProjectorResult.config: <modality>.embedding_output_count gives the positive output count, and <modality>.embedding_output.<index> gives each nonempty, unique output name. These entries survive IR serialization and control generic output splitting. Built-in builders declare the standard feature names; the builder-owned resolver also handles legacy auxiliary-count metadata, or a single feature output if no count is present.

A custom genai_adapter runs for both EMBEDDINGS and VISION_ENCODERS after generic extraction of the selected modality. It receives an independent model clone and returns ModelAdapterResult: the primary graph in model and optional named graphs in models. It can replace the entire input graph. EMBEDDINGS supports vision and audio and permits an empty map; when a map is returned, it must contain the primary graph. VISION_ENCODERS requires vision modality and a nonempty map containing the primary graph. Every map entry needs a nonempty name and a nonnull graph. All returned graphs are validated before the pass commits the result, so callback failures leave the supplied graph unchanged. A result callback overrides the definition callback; an explicitly empty result callback selects generic adaptation and, for VISION_ENCODERS, the recipe.

Its implementation belongs to the owning builder or external extension. Callback lifetime is retained with the extension library, and callback attributes are removed from committed graphs. Callbacks are excluded from IR serialization; use a serializable recipe when adaptation must work after reloading an unadapted IR.

Add or override an operation converter

Register an ov::frontend::ConversionExtension whose converter takes const NodeContext& and returns an indexed ov::OutputVector; named-output overloads are unused. Match the exact emitted string:

#include "openvino/frontend/extension/conversion.hpp"
#include "openvino/op/negative.hpp"

frontend->add_extension(std::make_shared<ov::frontend::ConversionExtension>(
    "EXAMPLE_NEGATE",
    [](const ov::frontend::NodeContext& context) -> ov::OutputVector {
        OPENVINO_ASSERT(context.get_input_size() == 1, "EXAMPLE_NEGATE requires one input");
        auto result = std::make_shared<ov::op::v0::Negative>(context.get_input(0));
        result->set_friendly_name(context.get_name());
        return {result};
    }));

A builder can then call graph.node("EXAMPLE_NEGATE", {value}). Use the base NodeContext interface (get_attribute<int>("op_case", 0) for the case); GGUF's concrete one is internal. A same-named extension overrides the built-in converter and the last registration wins. Preserve the operation's semantics, types and layout. For a built-in converter see how_to_add_op.md.

Register normalization passes

DecoderTransformationExtension wraps a pass or a std::shared_ptr<ov::Model> function and runs during normalization, before built-in lowerings. For a GenAI-ready decoder, call adapt_to_genai() on the converted model instead: it owns stateful conversion and adaptation. Do not also register GGUFMakeStateful or AdaptToGenAI on that frontend. The deprecated GenAIExtension performs the same call during convert(). See runtime.md.

Build and load a shared library

Export extensions with OPENVINO_CREATE_EXTENSIONS, as in extension.cpp; one list may mix all types. The example README builds the decoder, whole-model projection and mmproj plugins and runs them. Load a library through the base frontend interface:

frontend->add_extension("/tmp/gguf-extensions/libgguf_projector_extension.so");
auto model = frontend->convert(frontend->load("projector.gguf"));

Loaded input models keep the libraries their builder needs alive.

Validate and troubleshoot

Load the actual library, convert a matching file and compare with the reference as described in architectures.md. Existing coverage lives in test_architecture_extension.cpp, test_builder_api.cpp, test_extensions.cpp, test_mmproj.cpp and ov_gguf_architecture_library_tests.

Symptom Check
GGUF absent from automatic frontend selection Use load_by_framework("gguf"); the frontend is hidden from read_model()
Unsupported architecture Register before load(); check the literal string and predicate
Duplicate id or unknown replacement Add for new ids, Replace only for existing ids
Two handlers claim a file Make predicates disjoint or replace the existing handler
Unsupported mmproj projector Register a ProjectorExtension before load(); check modality and resolved type
Missing operation converter Register the exact string on the same frontend before convert()
Cache remains stateless Call adapt_to_genai() for GenAI IO, or register GGUFMakeStateful for GGUF IO before convert()
Outputs differ from the reference Follow debugging_accuracy.md

GenAI C++ callers can forward extension objects or shared-library paths using ov::genai::extensions(...); Python uses extensions=[...] for library paths. Select GGUF_READER="FRONTEND" when using the language-model pipelines. These extensions are registered on each GGUF frontend before loading.