Skip to content

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

18.0 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// Utilities shared by the GGUF frontend per-op test suite (test_ops.cpp).
//
// The GGUF frontend translation layer operates purely on the abstract GgufDecoder
// interface; it does not read .gguf files.  For tests we therefore provide a tiny
// in-memory decoder (SingleOpDecoder) that describes a single ggml op plus its
// input/output tensors, and drive ov::frontend::gguf::FrontEnd::convert on it.  This
// keeps the tests free of any llama.cpp / ggml dependency.
//
// Provides:
//   - SingleOpDecoder / SingleOpBuilder — describe one op and build an ov::Model.
//   - run_on_cpu  — compile an ov::Model on CPU and run one inference.
//   - expect_near — element-wise |actual-expected| <= atol check via GTest.

#pragma once

#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include "cnpy.h"
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
#include "gtest/gtest.h"
#include "op_table.hpp"
#include "openvino/core/model.hpp"
#include "openvino/core/partial_shape.hpp"
#include "openvino/core/type/bfloat16.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/core/type/float16.hpp"
#include "openvino/core/visibility.hpp"
#include "openvino/frontend/gguf/frontend.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/pass/serialize.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/runtime/tensor.hpp"
#include "openvino/util/file_util.hpp"
#include "quant/gguf.hpp"

// TEST_DATA_DIR is injected by CMakeLists.txt as an absolute path to the source-tree
// test_data/ directory; used as a fallback when running from the build tree.
#ifndef TEST_DATA_DIR
#    error "TEST_DATA_DIR must be defined by CMake (add_compile_definitions)"
#endif

namespace ov_gguf_test {

using namespace ov::frontend::gguf;

// Set of ggml op types that some test in this binary has actually converted.  Every
// SingleOpDecoder construction records its op type here, so the record is a by-product of the tests
// running rather than a hand-maintained list that can drift.  Checked against op_table.cpp by the
// coverage gate in test_op_coverage.cpp, which therefore fails when a new op is registered without
// a test.  Populated at run time, so the gate has to run last -- see that file for how.
inline std::set<std::string>& converted_op_types() {
    static std::set<std::string> ops;
    return ops;
}

// Description of one tensor (graph input or op output) in the single-op model.
struct TensorDesc {
    std::string name;
    ov::element::Type type;
    ov::PartialShape shape;
};

// A minimal GgufDecoder that exposes exactly one ggml op.  The op has `inputs`
// (all of which become graph Parameters) and a single output.  Operation parameters
// are exposed through the typed node-scoped get_attribute(name) accessor, matching the
// decoder API the op translators consume; a real decoder (e.g. the llama.cpp cgraph
// decoder) populates the same attributes from ggml's op_params.
class SingleOpDecoder : public GgufDecoder, public std::enable_shared_from_this<SingleOpDecoder> {
public:
    SingleOpDecoder(std::string op_type,
                    std::vector<TensorDesc> inputs,
                    std::vector<TensorDesc> extra_inputs,
                    TensorDesc output,
                    std::map<std::string, ov::Any> attributes)
        : m_op_type(std::move(op_type)),
          m_inputs(std::move(inputs)),
          m_output(std::move(output)),
          m_attributes(std::move(attributes)) {
        converted_op_types().insert(m_op_type);
        for (const auto& in : m_inputs) {
            m_input_names.push_back(in.name);
            auto p = std::make_shared<ov::op::v0::Parameter>(in.type, in.shape);
            p->set_friendly_name(in.name);
            p->output(0).set_names({in.name});
            m_model_inputs[in.name] = p;
        }
        for (const auto& in : extra_inputs) {
            auto p = std::make_shared<ov::op::v0::Parameter>(in.type, in.shape);
            p->set_friendly_name(in.name);
            p->output(0).set_names({in.name});
            m_model_extra_inputs[in.name] = p;
        }
    }

    // ── typed node-scoped attribute access (we hold a single op) ────────────────
    ov::Any get_attribute(const std::string& name) const override {
        auto it = m_attributes.find(name);
        return it == m_attributes.end() ? ov::Any{} : it->second;
    }

    // ── per-node metadata ───────────────────────────────────────────────────────
    int64_t get_input_view_element_offset(const std::string&) const override {
        auto it = m_attributes.find("view_offset");
        return it == m_attributes.end() ? 0 : it->second.as<int64_t>();
    }
    ov::PartialShape get_input_shape(const std::string& name) const override {
        return find_input(name).shape;
    }
    size_t get_input_size() const override {
        return m_inputs.size();
    }

    std::vector<std::string> get_input_names() const override {
        return m_input_names;
    }

    ov::PartialShape get_output_shape() const override {
        return m_output.shape;
    }

    std::vector<std::string> get_output_names() const override {
        return {m_output.name};
    }

    const std::string& get_op_type() const override {
        return m_op_type;
    }
    const std::string& get_op_name() const override {
        return m_output.name;
    }

    void visit_subgraph(std::function<void(std::shared_ptr<GgufDecoder>)> node_visitor) const override {
        node_visitor(std::const_pointer_cast<SingleOpDecoder>(shared_from_this()));
    }

    const std::map<std::string, std::shared_ptr<ov::Node>>& get_model_inputs() const override {
        return m_model_inputs;
    }
    const std::map<std::string, std::shared_ptr<ov::Node>>& get_model_extra_inputs() const override {
        return m_model_extra_inputs;
    }
    std::vector<std::string> get_model_output_names() const override {
        return {m_output.name};
    }

    // The optional model-scope accessors (get_model_extra_inputs, get_tokenizer_config) both
    // default to empty on GgufDecoder, which is exactly right for a single-op test decoder: no
    // auxiliary inputs and no tokenizer metadata. So neither is overridden here.

private:
    const TensorDesc& find_input(const std::string& name) const {
        for (const auto& in : m_inputs) {
            if (in.name == name) {
                return in;
            }
        }
        throw std::runtime_error("SingleOpDecoder: unknown input '" + name + "'");
    }

    std::string m_op_type;
    std::vector<TensorDesc> m_inputs;
    TensorDesc m_output;
    std::map<std::string, ov::Any> m_attributes;
    std::vector<std::string> m_input_names;
    std::map<std::string, std::shared_ptr<ov::Node>> m_model_inputs;
    std::map<std::string, std::shared_ptr<ov::Node>> m_model_extra_inputs;
};

// Fluent builder: describe a single op and convert it to an ov::Model.
class SingleOpBuilder {
public:
    SingleOpBuilder& op(const std::string& op_type) {
        m_op_type = op_type;
        return *this;
    }
    SingleOpBuilder& input(const std::string& name, ov::element::Type type, const ov::PartialShape& shape) {
        m_inputs.push_back({name, type, shape});
        return *this;
    }
    SingleOpBuilder& extra_input(const std::string& name, ov::element::Type type, const ov::PartialShape& shape) {
        m_extra_inputs.push_back({name, type, shape});
        return *this;
    }
    SingleOpBuilder& output(const std::string& name, ov::element::Type type, const ov::PartialShape& shape) {
        m_output = {name, type, shape};
        return *this;
    }
    // Set a typed operation attribute (e.g. "scale", "eps", "swapped"), the way a real
    // decoder exposes ggml op_params to the translators.
    template <typename T>
    SingleOpBuilder& attr(const std::string& name, const T& value) {
        m_attributes[name] = ov::Any(value);
        return *this;
    }
    SingleOpBuilder& op_case(int c) {
        m_attributes["op_case"] = ov::Any(c);
        return *this;
    }

    // Build the single-op decoder. A SingleOpDecoder exposes no "rope_config" attribute, so the
    // frontend's InputModel::get_rope_config returns a default (no shared rope table) and the LLM
    // preprocess step no-ops -- exactly what a single-op test wants, with no naive flag.
    std::shared_ptr<GgufDecoder> decoder() const {
        auto attrs = m_attributes;
        attrs.emplace("output_type", ov::Any(m_output.type));
        return std::make_shared<SingleOpDecoder>(m_op_type, m_inputs, m_extra_inputs, m_output, attrs);
    }

    std::shared_ptr<ov::Model> build() const {
        FrontEnd fe;
        return fe.convert(fe.load(decoder()));
    }

    // Convert through a FrontEnd that has the given extensions registered first.
    std::shared_ptr<ov::Model> build_with_extensions(
        const std::vector<std::shared_ptr<ov::Extension>>& extensions) const {
        FrontEnd fe;
        for (const auto& ext : extensions) {
            fe.add_extension(ext);
        }
        return fe.convert(fe.load(decoder()));
    }

private:
    std::string m_op_type;
    std::vector<TensorDesc> m_inputs;
    std::vector<TensorDesc> m_extra_inputs;
    TensorDesc m_output;
    std::map<std::string, ov::Any> m_attributes;
};

// ── inference / comparison helpers ──────────────────────────────────────────────

inline ov::Tensor make_f32_tensor(const ov::Shape& shape, const std::vector<float>& data) {
    ov::Tensor t(ov::element::f32, shape);
    std::copy(data.begin(), data.end(), t.data<float>());
    return t;
}

inline ov::Tensor make_f16_tensor(const ov::Shape& shape, const std::vector<float>& data) {
    ov::Tensor tensor(ov::element::f16, shape);
    std::transform(data.begin(), data.end(), tensor.data<ov::float16>(), [](float value) {
        return ov::float16(value);
    });
    return tensor;
}

inline ov::Tensor make_i64_tensor(const ov::Shape& shape, const std::vector<int64_t>& data) {
    ov::Tensor tensor(ov::element::i64, shape);
    std::copy(data.begin(), data.end(), tensor.data<int64_t>());
    return tensor;
}

// Compile on CPU and run one inference with the given named inputs; return the single output.
//
// Inference precision is requested as f32: these tests validate the converted graph against an fp32
// reference, so request fp32 regardless of the plugin's performance-mode default
// (e.g. bf16 on avx512_core_bf16 hosts or fp16 on ARM).
inline ov::Tensor run_on_cpu(const std::shared_ptr<ov::Model>& model, const std::map<std::string, ov::Tensor>& inputs) {
    ov::Core core;
    auto compiled = core.compile_model(model, "CPU", ov::hint::inference_precision(ov::element::f32));
    auto req = compiled.create_infer_request();
    for (const auto& kv : inputs) {
        req.set_tensor(kv.first, kv.second);
    }
    req.infer();
    return req.get_output_tensor(0);
}

// Default relative tolerance accounts for differences in platform-specific FP32 math kernels.
// ARM approximations can accumulate error through op chains such as rope.
#if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
#    define OV_GGUF_TEST_DEFAULT_RTOL 1e-2f
#else
#    define OV_GGUF_TEST_DEFAULT_RTOL 1e-5f
#endif

// Compare against an fp32 reference with a combined absolute + relative tolerance:
//   |actual - expected| <= atol + rtol * |expected|
// The relative term accounts for error that grows with the magnitude of the value.
inline void expect_near(const ov::Tensor& actual,
                        const std::vector<float>& expected,
                        float atol = 1e-4f,
                        float rtol = OV_GGUF_TEST_DEFAULT_RTOL) {
    ASSERT_EQ(actual.get_element_type(), ov::element::f32);
    ASSERT_EQ(actual.get_size(), expected.size());
    const float* a = actual.data<float>();
    for (size_t i = 0; i < expected.size(); ++i) {
        const float tol = atol + rtol * std::fabs(expected[i]);
        EXPECT_NEAR(a[i], expected[i], tol) << "mismatch at index " << i;
    }
}

// ── npy helpers (used by the dequant tests) ─────────────────────────────────────

// Locate the test_data directory.  When installed (CI), the data lives in a "test_data"
// folder next to the test executable; when running from the build tree it is in the
// source tree at the compile-time TEST_DATA_DIR path.
inline std::string test_data_dir() {
    const std::string installed =
        ov::util::path_join({ov::test::utils::getExecutableDirectory(), "test_data"}).string();
    if (ov::util::directory_exists(installed)) {
        return installed;
    }
    return TEST_DATA_DIR;
}

inline std::string test_data_path(const std::string& stem) {
    return ov::util::path_join({test_data_dir(), stem + ".npy"}).string();
}

// Load a .npy array as a flat vector of T.
template <typename T>
std::vector<T> load_npy(const std::string& stem) {
    cnpy::NpyArray arr = cnpy::npy_load(test_data_path(stem));
    const T* begin = arr.data<T>();
    return std::vector<T>(begin, begin + arr.num_vals);
}

// cnpy::npz_t is a vector of pairs, so look the entry up by name.
inline const cnpy::NpyArray& npz_array(const cnpy::npz_t& arrays, const std::string& name) {
    const auto it = std::find_if(arrays.begin(), arrays.end(), [&](const auto& entry) {
        return entry.first == name;
    });
    OPENVINO_ASSERT(it != arrays.end(), "Missing reference array ", name);
    return it->second;
}

inline void quantize_weight(GgufWeightTable& weights, const std::string& name, GgufTensorType type) {
    const auto source = weights.at(name).data;
    OPENVINO_ASSERT(source.get_element_type() == ov::element::f32);
    const auto* values = source.data<const float>();
    GgufWeight result;
    result.shape = source.get_shape();
    result.qtype = type;
    result.source_name = name;
    if (type == GGUF_TYPE_BF16) {
        ov::Tensor converted(ov::element::bf16, source.get_shape());
        std::transform(values, values + source.get_size(), converted.data<ov::bfloat16>(), [](float value) {
            return ov::bfloat16(value);
        });
        result.data = std::move(converted);
    } else {
        OPENVINO_ASSERT(type == GGUF_TYPE_Q8_0 && !source.get_shape().empty() && source.get_shape().back() % 32 == 0);
        ov::Tensor packed(ov::element::u8, {source.get_size() / 32 * 34});
        for (size_t group = 0; group < source.get_size() / 32; ++group) {
            std::array<int8_t, 32> codes;
            ov::float16 scale;
            quantize_row_q8_0_c(values + group * 32, codes.size(), codes.data(), scale);
            const auto bits = scale.to_bits();
            auto* block = packed.data<uint8_t>() + group * 34;
            std::memcpy(block, &bits, sizeof(bits));
            std::memcpy(block + sizeof(bits), codes.data(), codes.size());
        }
        result.data = std::move(packed);
    }
    weights.at(name) = std::move(result);
}

// Normalized MSE, the acceptance metric for the accuracy fixtures: sum((a-e)^2) / sum(e^2).
class Nmse {
public:
    void add(double actual, double expected) {
        m_error += (actual - expected) * (actual - expected);
        m_norm += expected * expected;
        m_finite = m_finite && std::isfinite(actual);
    }
    bool all_finite() const {
        return m_finite;
    }
    // Reference energy; a near-zero value means the fixture itself carries no signal.
    double reference_norm() const {
        return m_norm;
    }
    double value() const {
        return m_error / m_norm;
    }

private:
    double m_error = 0, m_norm = 0;
    bool m_finite = true;
};

inline Nmse nmse(const float* actual, const float* expected, size_t count) {
    Nmse result;
    for (size_t i = 0; i < count; ++i)
        result.add(actual[i], expected[i]);
    return result;
}

inline void expect_nmse_below(const Nmse& metric, double limit, const std::string& context = {}) {
    ASSERT_TRUE(metric.all_finite()) << context;
    ASSERT_GT(metric.reference_norm(), 1e-12) << context;
    EXPECT_LT(metric.value(), limit) << context;
}

// Round-trips a model through in-memory IR serialization.
inline std::shared_ptr<ov::Model> reload_ir_model(const std::shared_ptr<ov::Model>& model) {
    std::stringstream xml, weights;
    ov::pass::Serialize(xml, weights).run_on_model(model);
    const auto bytes = weights.str();
    ov::Tensor constants(ov::element::u8, {bytes.size()});
    if (!bytes.empty())
        std::memcpy(constants.data(), bytes.data(), bytes.size());
    return ov::Core{}.read_model(xml.str(), constants);
}

// GGUFMakeStateful keeps extra leading convolution taps; returns the trailing `taps` per channel.
inline ov::Tensor trailing_conv_taps(const ov::Tensor& state, size_t channels, size_t taps) {
    const auto stride = state.get_size() / channels;
    ov::Tensor logical(ov::element::f32, {channels, taps});
    for (size_t channel = 0; channel < channels; ++channel)
        std::copy_n(state.data<const float>() + channel * stride + stride - taps,
                    taps,
                    logical.data<float>() + channel * taps);
    return logical;
}

// The GGUF bytes a reference npz stores as "model", written to a temporary file for its lifetime.
struct TemporaryGguf {
    std::string path =
        (std::filesystem::temp_directory_path() / (ov::test::utils::generateTestFilePrefix() + ".gguf")).string();
    explicit TemporaryGguf(const cnpy::NpyArray& bytes) {
        std::ofstream file(path, std::ios::binary);
        file.write(bytes.data<char>(), bytes.num_vals);
        OPENVINO_ASSERT(file, "Cannot write ", path);
    }
    TemporaryGguf(const TemporaryGguf&) = delete;
    TemporaryGguf& operator=(const TemporaryGguf&) = delete;
    ~TemporaryGguf() {
        std::filesystem::remove(path);
    }
};

}  // namespace ov_gguf_test