OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Validate and enforce weightless caching for MoE offload_ratio (#38976)
Xiong Yuan32776832f8d8
diff --git a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.rst b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.rstindex ec2663b110e..975aaf84a5e 100644--- a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.rst+++ b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.rst@@ -428,11 +428,18 @@ All parameters must be set before calling ``ov::Core::compile_model()`` in order- ``ov::hint::performance_mode``- ``ov::intel_gpu::disable_winograd_convolution``- ``ov::intel_gpu::enable_loop_unrolling``+- ``ov::intel_gpu::offload_ratio``- ``ov::intel_gpu::hint::enable_sdpa_optimization``- ``ov::intel_gpu::hint::host_task_priority``- ``ov::intel_gpu::hint::queue_priority``- ``ov::intel_gpu::hint::queue_throttle``+Setting ``ov::intel_gpu::offload_ratio`` above zero automatically enables weightless+caching and selects ``ov::CacheMode::OPTIMIZE_SIZE``. Explicitly disabling weightless+caching or selecting ``ov::CacheMode::OPTIMIZE_SPEED`` is not supported with offloading,+including when importing a compiled model or loading it from cache.+MoE offloading requires a valid weights file and the corresponding constant offsets.+Read-only Properties+++++++++++++++++++++++++++++++++++++++diff --git a/src/inference/include/openvino/runtime/intel_gpu/properties.hpp b/src/inference/include/openvino/runtime/intel_gpu/properties.hppindex 939f3d60d0c..f37be734fbc 100644--- a/src/inference/include/openvino/runtime/intel_gpu/properties.hpp+++ b/src/inference/include/openvino/runtime/intel_gpu/properties.hpp@@ -180,7 +180,10 @@ static constexpr Property<bool> enable_large_allocations{"GPU_ENABLE_LARGE_ALLOC* When set to 0 (default), all weights are resident on GPU. A value of N means* N% of eligible weights are streamed from host memory on demand via an LRU cache,* reducing GPU memory usage at the cost of inference latency.- * Currently supported for MoE expert weights only.+ * Currently supported for MoE expert weights only. This property requires weightless+ * caching: it automatically enables @ref ov::enable_weightless unless the user explicitly+ * disables it. Setting this property together with @ref ov::enable_weightless(false) or+ * @ref ov::cache_mode(ov::CacheMode::OPTIMIZE_SPEED) throws an exception.* @ingroup ov_runtime_ocl_gpu_prop_cpp_api*/static constexpr Property<size_t, PropertyMutability::RW> offload_ratio{"OFFLOAD_RATIO"};diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/execution_config.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/execution_config.hppindex 0cc0f14776e..815ed588be2 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/execution_config.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/execution_config.hpp@@ -34,6 +34,7 @@ struct ExecutionConfig : public ov::PluginConfig {using ov::PluginConfig::finalize;const ov::AnyMap& get_user_properties() const { return m_user_properties; }+ void apply_weightless_options();protected:void finalize_impl(const IRemoteContext* context) override;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cppindex 4037dfdddec..4a8f7eac072 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp@@ -125,6 +125,9 @@ protected:[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {auto jit = KernelGenerator::get_jit_constants(params);auto desc = params.typed_desc<moe_3gemm_fused_compressed>();+ for (const auto* name : {"INPUT0_TYPE", "OUTPUT_TYPE", "OPTIONAL_SHAPE_INFO_ARG"}) {+ jit.remove(name);+ }jit.make("INPUT0_TYPE", "int"); // topk_idjit.make("OUTPUT_TYPE", "int"); // tokens_per_expertjit.make("OUTPUT1_TYPE", "int"); // experts_info_start_idx@@ -223,6 +226,10 @@ protected:[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {auto jit = KernelGenerator::get_jit_constants(params);+ // These stages consume scratch buffers, not the primitive's input/output tensors.+ for (const auto* name : {"INPUT0_TYPE", "INPUT1_TYPE", "OUTPUT_TYPE", "OPTIONAL_SHAPE_INFO_ARG"}) {+ jit.remove(name);+ }auto desc = params.typed_desc<moe_3gemm_fused_compressed>();// auto& engine = params.prog->get_engine();// const auto& info = engine.get_device_info();@@ -310,6 +317,17 @@ public:protected:[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {auto jit = KernelGenerator::get_jit_constants(params);+ for (const auto* name : {"INPUT0_TYPE",+ "INPUT1_TYPE",+ "INPUT2_TYPE",+ "INPUT3_TYPE",+ "INPUT4_TYPE",+ "INPUT5_TYPE",+ "INPUT6_TYPE",+ "OUTPUT_TYPE",+ "OPTIONAL_SHAPE_INFO_ARG"}) {+ jit.remove(name);+ }auto desc = params.typed_desc<moe_3gemm_fused_compressed>();// auto& engine = params.prog->get_engine();// const auto& info = engine.get_device_info();@@ -355,6 +373,9 @@ public:protected:[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {auto jit = KernelGenerator::get_jit_constants(params);+ for (const auto* name : {"INPUT0_TYPE", "INPUT1_TYPE", "INPUT2_TYPE", "OUTPUT_TYPE", "OPTIONAL_SHAPE_INFO_ARG"}) {+ jit.remove(name);+ }auto desc = params.typed_desc<moe_3gemm_fused_compressed>();auto hidden_size = desc->_config.hidden_size;diff --git a/src/plugins/intel_gpu/src/plugin/plugin.cpp b/src/plugins/intel_gpu/src/plugin/plugin.cppindex b41ac5c709d..46e443b6dba 100644--- a/src/plugins/intel_gpu/src/plugin/plugin.cpp+++ b/src/plugins/intel_gpu/src/plugin/plugin.cpp@@ -41,7 +41,6 @@#include "openvino/runtime/plugin_config.hpp"#include "openvino/runtime/properties.hpp"#include "openvino/runtime/shared_buffer.hpp"-#include "openvino/runtime/weightless_properties_utils.hpp"#include "openvino/util/env_util.hpp"#include "openvino/util/file_util.hpp"#include "openvino/util/mmap_object.hpp"@@ -209,7 +208,7 @@ std::shared_ptr<ov::Model> Plugin::clone_and_transform_model(const std::shared_p// Set weightless cache attribute only for non IR (e.g. onnxruntime) models// This is a temporary solution. A common way of handling weightless caching will be defined later.if (config_copy.get_enable_weightless()) {- const auto weight_path = ov::util::make_path(config.get_weights_path());+ const auto weight_path = ov::util::make_path(config_copy.get_weights_path());if (weight_path.extension() != ".bin" && !is_weightless_cache_attributes_set(cloned_model)) {set_weightless_cache_attributes(cloned_model);@@ -484,6 +483,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model,ExecutionConfig config = m_configs_map.at(device_id);config.set_user_property(_orig_config, OptionVisibility::RELEASE);+ config.apply_weightless_options();ov::CacheMode cache_mode = config.get_cache_mode();ov::EncryptionCallbacks encryption_callbacks = config.get_cache_encryption_callbacks();@@ -501,7 +501,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model,return nullptr;}- if (ov::util::is_weightless_enabled(config.get_user_properties()).value_or(false)) {+ if (config.get_enable_weightless()) {const auto& weights_path = ov::util::make_path(config.get_weights_path());if (weights_path.extension() != ".bin") {diff --git a/src/plugins/intel_gpu/src/runtime/execution_config.cpp b/src/plugins/intel_gpu/src/runtime/execution_config.cppindex e289d290e04..a154bb08a5c 100644--- a/src/plugins/intel_gpu/src/runtime/execution_config.cpp+++ b/src/plugins/intel_gpu/src/runtime/execution_config.cpp@@ -173,6 +173,19 @@ void ExecutionConfig::finalize(cldnn::engine& engine) {PluginConfig::finalize(ctx.get(), nullptr);}+void ExecutionConfig::apply_weightless_options() {+ const auto enable_weightless = ov::util::is_weightless_enabled(get_user_properties());+ if (get_offload_ratio() > 0) {+ OPENVINO_ASSERT(enable_weightless.value_or(true) &&+ (!is_set_by_user(ov::cache_mode) || get_cache_mode() != ov::CacheMode::OPTIMIZE_SPEED),+ "OFFLOAD_RATIO requires weightless cache. Do not set ENABLE_WEIGHTLESS=false or "+ "CACHE_MODE=OPTIMIZE_SPEED when using OFFLOAD_RATIO.");+ set_property({ov::enable_weightless(true), ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE)});+ } else if (enable_weightless) {+ set_property(ov::enable_weightless(*enable_weightless));+ }+}+void ExecutionConfig::apply_rt_info(const IRemoteContext* context, const ov::RTMap& rt_info, bool is_llm, bool is_paged_attention_model, bool has_lora) {const auto* remote_context = dynamic_cast<const RemoteContextImpl*>(context);OPENVINO_ASSERT(remote_context != nullptr, "Expected GPU RemoteContextImpl in ExecutionConfig::apply_rt_info");@@ -190,10 +203,8 @@ void ExecutionConfig::apply_rt_info(const IRemoteContext* context, const ov::RTM// WEIGHTS_PATH is used for the weightless cache mechanism which is used only as defined by// ov::util::is_weightless_enabled. Not setting WEIGHTS_PATH will result in not- // using that mechanism. OTD (MoE offload) also requires the .bin path.- if (const auto enable_weightless = ov::util::is_weightless_enabled(get_user_properties()); enable_weightless) {- set_property({ov::enable_weightless(*enable_weightless)});- }+ // using that mechanism. OTD (MoE offload) also requires the .bin path.+ apply_weightless_options();if (get_enable_weightless() || get_offload_ratio() > 0) {apply_rt_info_property(ov::weights_path, rt_info);}@@ -342,6 +353,8 @@ void ExecutionConfig::apply_model_specific_options(const IRemoteContext* context}void ExecutionConfig::finalize_impl(const IRemoteContext* context) {+ apply_weightless_options();+GPU_DEBUG_IF(get_help()) {print_help();exit(-1);diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/execution_config_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/execution_config_test.cppindex 4a68b5c3e59..08994dfe803 100644--- a/src/plugins/intel_gpu/tests/unit/module_tests/execution_config_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/module_tests/execution_config_test.cpp@@ -15,6 +15,7 @@#include "openvino/op/parameter.hpp"#include "openvino/op/paged_attention.hpp"#include "openvino/op/subtract.hpp"+#include "ov_ops/moe_compressed.hpp"using namespace cldnn;using namespace ov::intel_gpu;@@ -152,6 +153,74 @@ static std::shared_ptr<ov::Model> make_pa_matmul_model(ov::element::Type weight_pa_token_type_ids, pa_qq_bias, pa_qq_bias_begins});}+// Build a minimal model containing an internal MOECompressed operation.+static std::shared_ptr<ov::Model> make_moe_model() {+ const size_t num_experts = 4;+ const size_t hidden_size = 128;+ const size_t inter_size = 256;+ const size_t group_size = 128;+ const size_t top_k = 2;++ ov::op::internal::MOECompressed::Config config{};+ config.hidden_size = hidden_size;+ config.inter_size = inter_size;+ config.num_expert = num_experts;+ config.num_shared_expert = 1;+ config.top_k = top_k;+ config.group_size = group_size;+ config.has_zp = true;+ config.out_type = ov::element::f16;++ size_t group_num = hidden_size / group_size;+ size_t group_num2 = inter_size / group_size;++ ov::OutputVector inputs;++ // 0: hidden_states [1, 1, hidden_size]+ auto hidden = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 1, hidden_size});+ inputs.push_back(hidden->output(0));++ // 1: routing_weights [1, 1, top_k]+ auto routing = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 1, top_k});+ inputs.push_back(routing->output(0));++ // 2: topk_indices [1, 1, top_k]+ auto topk_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{1, 1, top_k});+ inputs.push_back(topk_idx->output(0));++ auto make_const = [](ov::element::Type type, const ov::Shape& shape) {+ return std::make_shared<ov::op::v0::Constant>(type, shape, 0);+ };++ // Routed experts (9 constants: gate/up/down x weight/scale/zp)+ inputs.push_back(make_const(ov::element::u8, {num_experts, inter_size, group_size, group_num})); // 3: w0_weight+ inputs.push_back(make_const(ov::element::f16, {num_experts, inter_size, group_num, 1})); // 4: w0_scale+ inputs.push_back(make_const(ov::element::u8, {num_experts, inter_size, group_num, 1})); // 5: w0_zp+ inputs.push_back(make_const(ov::element::u8, {num_experts, inter_size, group_size, group_num})); // 6: w1_weight+ inputs.push_back(make_const(ov::element::f16, {num_experts, inter_size, group_num, 1})); // 7: w1_scale+ inputs.push_back(make_const(ov::element::u8, {num_experts, inter_size, group_num, 1})); // 8: w1_zp+ inputs.push_back(make_const(ov::element::u8, {num_experts, hidden_size, group_size, group_num2})); // 9: w2_weight+ inputs.push_back(make_const(ov::element::f16, {num_experts, hidden_size, group_num2, 1})); // 10: w2_scale+ inputs.push_back(make_const(ov::element::u8, {num_experts, hidden_size, group_num2, 1})); // 11: w2_zp++ // Shared experts (10 constants)+ inputs.push_back(make_const(ov::element::u8, {1, inter_size, group_size, group_num})); // 12: shared_gate_weight+ inputs.push_back(make_const(ov::element::f16, {1, inter_size, group_num, 1})); // 13: shared_gate_scale+ inputs.push_back(make_const(ov::element::u8, {1, inter_size, group_num, 1})); // 14: shared_gate_zp+ inputs.push_back(make_const(ov::element::u8, {1, inter_size, group_size, group_num})); // 15: shared_up_weight+ inputs.push_back(make_const(ov::element::f16, {1, inter_size, group_num, 1})); // 16: shared_up_scale+ inputs.push_back(make_const(ov::element::u8, {1, inter_size, group_num, 1})); // 17: shared_up_zp+ inputs.push_back(make_const(ov::element::u8, {1, hidden_size, group_size, group_num2})); // 18: shared_down_weight+ inputs.push_back(make_const(ov::element::f16, {1, hidden_size, group_num2, 1})); // 19: shared_down_scale+ inputs.push_back(make_const(ov::element::u8, {1, hidden_size, group_num2, 1})); // 20: shared_down_zp+ inputs.push_back(make_const(ov::element::f16, {1, hidden_size})); // 21: shared_gate_gate_weight++ auto moe_node = std::make_shared<ov::op::internal::MOECompressed>(inputs, config);++ return std::make_shared<ov::Model>(ov::OutputVector{moe_node->output(0)},+ ov::ParameterVector{hidden, routing, topk_idx});+}+TEST(execution_config, kv_cache_u4_weights_auto_detect_u4) {auto& engine = get_test_engine();auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});@@ -294,3 +363,97 @@ TEST(execution_config, finish_after_enqueue_default_off) {GTEST_FAIL() << "finish_after_enqueue must default to OFF when OV_GPU_FINISH_AFTER_ENQUEUE is unset";}}++TEST(execution_config, moe_offload_enables_weightless_by_default) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});+ auto model = make_moe_model();++ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.finalize(ctx.get(), model.get());++ ASSERT_TRUE(config.get_enable_weightless());+}++TEST(execution_config, moe_offload_rejects_explicitly_disabled_weightless) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});+ auto model = make_moe_model();++ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.set_user_property(ov::enable_weightless(false));++ ASSERT_ANY_THROW(config.finalize(ctx.get(), model.get()));+}++TEST(execution_config, moe_offload_rejects_optimize_speed_cache_mode) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});+ auto model = make_moe_model();++ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.set_user_property(ov::cache_mode(ov::CacheMode::OPTIMIZE_SPEED));++ ASSERT_ANY_THROW(config.finalize(ctx.get(), model.get()));+}++TEST(execution_config, moe_offload_accepts_optimize_size_cache_mode) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});+ auto model = make_moe_model();++ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.set_user_property(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));+ config.finalize(ctx.get(), model.get());++ ASSERT_TRUE(config.get_enable_weightless());+}++TEST(execution_config, moe_offload_model_less_config_defaults_to_optimize_size) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});++ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.finalize(ctx.get(), nullptr);++ EXPECT_TRUE(config.get_enable_weightless());+ EXPECT_EQ(config.get_cache_mode(), ov::CacheMode::OPTIMIZE_SIZE);+}++TEST(execution_config, moe_offload_model_less_config_rejects_incompatible_properties) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});++ const std::vector<ov::AnyMap> incompatible_properties = {+ {ov::enable_weightless(false)},+ {ov::cache_mode(ov::CacheMode::OPTIMIZE_SPEED)},+ {ov::enable_weightless(true), ov::cache_mode(ov::CacheMode::OPTIMIZE_SPEED)},+ {ov::enable_weightless(false), ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE)},+ };+ for (size_t i = 0; i < incompatible_properties.size(); ++i) {+ SCOPED_TRACE(i);+ ExecutionConfig config;+ config.set_user_property(ov::intel_gpu::offload_ratio(20));+ config.set_user_property(incompatible_properties[i]);+ EXPECT_THROW(config.finalize(ctx.get(), nullptr), ov::AssertFailure);+ }+}++TEST(execution_config, moe_offload_zero_preserves_default_cache_options) {+ auto& engine = get_test_engine();+ auto ctx = std::make_shared<RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});++ ExecutionConfig config;+ config.set_user_property({ov::intel_gpu::offload_ratio(0),+ ov::enable_weightless(false),+ ov::cache_mode(ov::CacheMode::OPTIMIZE_SPEED)});+ config.finalize(ctx.get(), nullptr);++ EXPECT_FALSE(config.get_enable_weightless());+ EXPECT_EQ(config.get_cache_mode(), ov::CacheMode::OPTIMIZE_SPEED);+}