Skip to content

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

8.8 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/node_builders/moe_builders.hpp"
#include "common_test_utils/ov_plugin_cache.hpp"
#include "common_test_utils/ov_tensor_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
#include "functional_test_utils/skip_tests_config.hpp"
#include "openvino/core/preprocess/pre_post_process.hpp"
#include "openvino/pass/serialize.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/runtime/intel_gpu/properties.hpp"
#include "shared_test_classes/base/ov_subgraph.hpp"

namespace {

// Regression test for openvinotoolkit/model_server#4580: a MoE model read from an IR file must compile and run with
// OFFLOAD_RATIO set, whether or not the file was read with ENABLE_MMAP, and without the caller passing WEIGHTS_PATH.
// The parameter is the ENABLE_MMAP value passed to Core::read_model (or to compile_model for the from-file case).
// Skips live in skip_tests_config.cpp: the whole suite on GPUs without HW_MATMUL (MoE fusion, and with it the offload
// path, is gated on supports_immad), and the mmap=false inference case while #38584 is open.
class MoEOffloadFromIRFile : public ::testing::Test, public ::testing::WithParamInterface<bool> {
public:
    static std::string get_test_case_name(::testing::TestParamInfo<bool> obj) {
        return std::string("mmap=") + (obj.param ? "true" : "false");
    }

protected:
    // 4 experts, top-2: with OFFLOAD_RATIO 50 the plugin keeps 2 expert slots resident, so a few inferences with
    // distinct inputs route to experts that are not resident and the .bin file is actually read (the offload reader
    // opens it on the first expert miss, not at compile time).
    static constexpr size_t num_experts = 4;
    static constexpr size_t topk = 2;
    static constexpr size_t hidden_size = 128;
    static constexpr size_t intermediate_size = 256;
    static constexpr size_t offload_ratio_under_test = 50;
    static constexpr int inference_count = 4;

    std::shared_ptr<ov::Core> core;
    std::string xml_path;
    std::string bin_path;

    void SetUp() override {
        SKIP_IF_CURRENT_TEST_IS_DISABLED();
        core = ov::test::utils::PluginCache::get().core();

        const auto prefix = ov::test::utils::generateTestFilePrefix();
        xml_path = prefix + ".xml";
        bin_path = prefix + ".bin";

        // Same compressed 3-GEMM pattern as smoke_MoE3GemmCompressedFusion in subgraph_tests/dynamic/moe.cpp.
        const ov::test::MoePatternParams moe_params{{-1, -1, static_cast<int64_t>(hidden_size)}, topk, num_experts, intermediate_size};
        auto model = ov::test::initMoE3GeMMSubgraph(moe_params,
                                                    ov::element::f32,  // data_precision
                                                    ov::element::u8,   // weights_precision
                                                    true,              // use_weight_decompression
                                                    ov::element::f16,  // decompression_precision
                                                    ov::element::f16,  // scale_precision
                                                    ov::test::utils::DecompressionType::full,
                                                    ov::test::utils::DecompressionType::full,
                                                    true,  // reshape_on_decompression
                                                    128);  // group_size
        // f16 tensors at the boundary, as MoECompressedFusionTest sets through inType/outType; without them the
        // expert block is not fused and OFFLOAD_RATIO has nothing to act on.
        ov::preprocess::PrePostProcessor ppp(model);
        ppp.input(0).tensor().set_element_type(ov::element::f16);
        ppp.output(0).tensor().set_element_type(ov::element::f16);
        model = ppp.build();
        ov::pass::Serialize(xml_path, bin_path).run_on_model(model);
    }

    void TearDown() override {
        // Every compiled model and infer request is a local of its test body, so they are released before this
        // runs; on Windows the offload reader keeps the .bin open without FILE_SHARE_DELETE until then.
        ov::test::utils::removeIRFiles(xml_path, bin_path);
    }

    std::shared_ptr<ov::Model> read_model() const {
        return core->read_model(xml_path, std::string{}, ov::AnyMap{ov::enable_mmap(GetParam())});
    }

    static void check_compiled(const ov::CompiledModel& compiled_model, size_t expected_offload_ratio) {
        ASSERT_TRUE(compiled_model);
        // The model must have taken the fused MoE path, otherwise OFFLOAD_RATIO has nothing to act on.
        ov::test::CheckNumberOfNodesWithType(compiled_model, "moe_3gemm_fused_compressed", 1);
        EXPECT_EQ(compiled_model.get_property(ov::intel_gpu::offload_ratio), expected_offload_ratio);
    }

    // Runs the same inputs through the offloaded and the fully resident compiled model and compares the outputs:
    // equal outputs show that the experts read back from the .bin file are the ones the model was built with.
    // Not bit-exact: a 16-token input with top-2 routing can need more experts than the resident slots (2 of 4 at
    // OFFLOAD_RATIO 50), and then exec_batched_gemv in moe_3gemm_swiglu_opt.cpp falls back to the per-expert oneDNN
    // loop (on_before_batched_gemv sets needs_fallback -> exec_prefill_onednn) while the resident model runs the batched
    // GEMV kernels, with a different accumulation order. On Arc 140V the largest absolute difference measured was 1.2e-4,
    // on outputs below 0.25 in magnitude (the 4 seeds below, OFFLOAD_RATIO 25 and 50); single-token inputs, which fit
    // the slots, are bit-exact.
    static void check_inference_matches_resident(ov::CompiledModel& offloaded, ov::CompiledModel& resident) {
        auto offloaded_request = offloaded.create_infer_request();
        auto resident_request = resident.create_infer_request();
        for (int seed = 1; seed <= inference_count; ++seed) {
            const auto input =
                ov::test::utils::create_and_fill_tensor(ov::element::f16, ov::Shape{1, 16, hidden_size}, ov::test::utils::InputGenerateData(-1, 2, 100, seed));
            offloaded_request.set_input_tensor(input);
            offloaded_request.infer();
            resident_request.set_input_tensor(input);
            resident_request.infer();
            ov::test::utils::compare(resident_request.get_output_tensor(), offloaded_request.get_output_tensor(), ov::element::f16, 1e-3, 1e-3);
        }
    }
};

TEST_P(MoEOffloadFromIRFile, CompileModelWithOffloadRatio) {
    const auto model = read_model();
    ov::CompiledModel compiled_model;
    OV_ASSERT_NO_THROW(compiled_model = core->compile_model(model, ov::test::utils::DEVICE_GPU, {ov::intel_gpu::offload_ratio(offload_ratio_under_test)}));
    check_compiled(compiled_model, offload_ratio_under_test);
}

TEST_P(MoEOffloadFromIRFile, InferWithOffloadRatioMatchesResident) {
    const auto model = read_model();
    ov::CompiledModel offloaded;
    ov::CompiledModel resident;
    OV_ASSERT_NO_THROW(offloaded = core->compile_model(model, ov::test::utils::DEVICE_GPU, {ov::intel_gpu::offload_ratio(offload_ratio_under_test)}));
    OV_ASSERT_NO_THROW(resident = core->compile_model(model, ov::test::utils::DEVICE_GPU, {ov::intel_gpu::offload_ratio(0)}));
    check_compiled(offloaded, offload_ratio_under_test);
    check_compiled(resident, 0);
    check_inference_matches_resident(offloaded, resident);
}

// The compile_model(path) entry point: the plugin reads the file itself, with ENABLE_MMAP taken from the compile
// properties (IPlugin::compile_model(path) -> ICore::read_model), and the caller never holds the ov::Model.
TEST_P(MoEOffloadFromIRFile, CompileModelFromFileWithOffloadRatio) {
    ov::CompiledModel compiled_model;
    OV_ASSERT_NO_THROW(
        compiled_model =
            core->compile_model(xml_path, ov::test::utils::DEVICE_GPU, {ov::intel_gpu::offload_ratio(offload_ratio_under_test), ov::enable_mmap(GetParam())}));
    check_compiled(compiled_model, offload_ratio_under_test);
}

// Control baseline for the three tests above: with WEIGHTS_PATH passed explicitly the plugin never needs the path
// from the model, so this passes with and without the frontend change.
TEST_P(MoEOffloadFromIRFile, CompileModelWithOffloadRatioAndWeightsPath) {
    const auto model = read_model();
    ov::CompiledModel compiled_model;
    OV_ASSERT_NO_THROW(
        compiled_model =
            core->compile_model(model, ov::test::utils::DEVICE_GPU, {ov::intel_gpu::offload_ratio(offload_ratio_under_test), ov::weights_path(bin_path)}));
    check_compiled(compiled_model, offload_ratio_under_test);
}

INSTANTIATE_TEST_SUITE_P(smoke_MoEOffload, MoEOffloadFromIRFile, ::testing::Bool(), MoEOffloadFromIRFile::get_test_case_name);

}  // namespace