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] Fix MoE offload slot-id buffer freed while kernels still read it (#38545)
Blair DuCray-Oppat, AIGP74331408dd41
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 858a004d4f0..f0b3bcff3e3 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@@ -741,6 +741,9 @@ public:int _gate_up_group_size;int _down_group_size;std::shared_ptr<IExpertWeightProvider> _weight_provider;+ // OTD slot ids read by kernels that are still in flight when execute() returns,+ // so the buffer must outlive the per-call scratch_buffers.+ memory::ptr _expert_index_buffer;// --- OTD helper methods (used when _weight_provider->is_offloaded()) ---@@ -839,15 +842,16 @@ public:// Write remapped slot IDs to GPU bufferauto& engine = instance.get_network().get_engine();const size_t topk_bytes = topk_count * sizeof(uint32_t);- if (!scratch._expert_index_buffer || scratch._expert_index_buffer->size() < topk_bytes) {+ if (!_expert_index_buffer || _expert_index_buffer->size() < topk_bytes) {auto layout = cldnn::layout({1, 1, 1, static_cast<ov::Dimension::value_type>(topk_bytes)}, ov::element::i8, cldnn::format::bfyx);- scratch._expert_index_buffer = engine.allocate_memory(layout, allocation_type::usm_host, false);+ _expert_index_buffer = engine.allocate_memory(layout, allocation_type::usm_host, false);}std::vector<uint32_t> slots_u32(lease->size());for (size_t i = 0; i < lease->size(); i++) {slots_u32[i] = static_cast<uint32_t>((*lease)[i]);}- scratch._expert_index_buffer->copy_from(stream, slots_u32.data(), 0, 0, topk_bytes, true);+ _expert_index_buffer->copy_from(stream, slots_u32.data(), 0, 0, topk_bytes, true);+ scratch._expert_index_buffer = _expert_index_buffer;set_otd_weight_pointers(instance, scratch);needs_fallback = false;@@ -879,16 +883,16 @@ public:// Write remapped slot IDs to GPU bufferauto& engine = instance.get_network().get_engine();const size_t topk_bytes = topk_count * sizeof(uint32_t);- if (!scratch._expert_index_buffer || scratch._expert_index_buffer->size() < topk_bytes) {+ if (!_expert_index_buffer || _expert_index_buffer->size() < topk_bytes) {auto layout = cldnn::layout({1, 1, 1, static_cast<ov::Dimension::value_type>(topk_bytes)}, ov::element::i8, cldnn::format::bfyx);- scratch._expert_index_buffer = engine.allocate_memory(layout, allocation_type::usm_host, false);+ _expert_index_buffer = engine.allocate_memory(layout, allocation_type::usm_host, false);}std::vector<uint32_t> slots_u32(lease->size());for (size_t i = 0; i < lease->size(); i++) {slots_u32[i] = static_cast<uint32_t>((*lease)[i]);}- scratch._expert_index_buffer->copy_from(stream, slots_u32.data(), 0, 0, topk_bytes, true);- batch_mem_ptr = scratch._expert_index_buffer;+ _expert_index_buffer->copy_from(stream, slots_u32.data(), 0, 0, topk_bytes, true);+ batch_mem_ptr = _expert_index_buffer;needs_fallback = false;}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cppindex 69502801a9d..027cedc642c 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp@@ -19,6 +19,7 @@#include "../../common/random_generator.hpp"#include "../test_utils/test_utils.h"+#include "common_test_utils/common_utils.hpp"#include "moe_3gemm_fused_inst.h"#include "moe_3gemm_test_data.h"#include "common_test_utils/common_utils.hpp"@@ -581,6 +582,10 @@ struct Moe3GemmTestParams {size_t top_k;size_t group_size;bool is_signed = false;+ size_t lru_expert_num = 0; // > 0: expert weights are offloaded to disk (OFFLOAD_RATIO path)+ bool micro_gemm_prefill = false; // true: prefill uses the micro-GEMM path (grouped GEMM off)+ size_t routed_experts = 0; // > 0: tokens are routed only to the first routed_experts experts+ size_t executions = 1; // > 1: execute() again on new inputs of the same shape};class moe_3gemm_compressed_gpu_random : public ::testing::TestWithParam<std::tuple<cldnn::MoERouterFused::RoutingType, Moe3GemmTestParams>> {};@@ -591,6 +596,9 @@ TEST_P(moe_3gemm_compressed_gpu_random, moe_accuracy_test_random) {if (!engine.get_device_info().supports_immad) {GTEST_SKIP() << "No immad support";}+ if (param.micro_gemm_prefill && engine.get_device_info().arch < gpu_arch::xe2) {+ GTEST_SKIP() << "Micro-GEMM prefill needs Xe2 or newer";+ }tests::random_generator rg(GET_SUITE_NAME);Moe3GemmConfig config;@@ -608,6 +616,19 @@ TEST_P(moe_3gemm_compressed_gpu_random, moe_accuracy_test_random) {// Generate random dataauto hidden_states = rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.hidden_size, -1.0f, 1.0f, 1000);auto routing_weights = rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.num_experts, 0.0f, 1.0f, 1000);+ // Experts after the first routed_experts get a logit below every random one, so top-k never selects them.+ const size_t routed_experts = param.routed_experts;+ auto restrict_routing = [&](std::vector<ov::float16>& logits) {+ if (routed_experts == 0) {+ return;+ }+ for (size_t t = 0; t < config.batch_size * config.seq_len; ++t) {+ for (size_t e = routed_experts; e < config.num_experts; ++e) {+ logits[t * config.num_experts + e] = ov::float16(-1.0f);+ }+ }+ };+ restrict_routing(routing_weights);auto w0_data = rg.generate_random_1d<float>(config.num_experts * config.hidden_size * config.inter_size, -1.0f, 0.0f, 1000);auto w1_data = rg.generate_random_1d<float>(config.num_experts * config.hidden_size * config.inter_size, -1.0f, 0.0f, 1000);@@ -750,28 +771,109 @@ TEST_P(moe_3gemm_compressed_gpu_random, moe_accuracy_test_random) {input_info("w2_scale"),input_info("w2_zp")};- topology.add(moe_3gemm_fused_compressed("moe_3gemm_fused_compressed", moe_inputs, moe_config));+ // Offload: resident slots are refilled from a weights file holding the same tensors in plugin order+ // (weights, scales, zero points for gate/up/down). One quantization group per row keeps the file+ // layout identical to the device layout.+ std::vector<size_t> weight_bin_offsets;+ // One file per test process: parallel test runners execute these cases at the same time.+ auto weights_path = std::filesystem::temp_directory_path() / (ov::test::utils::generateTestFilePrefix() + "_moe_3gemm_otd_weights.bin");+ // Declared before the network so it runs after the network has closed the file.+ struct remove_file_on_exit {+ std::filesystem::path path;+ ~remove_file_on_exit() {+ std::error_code ec;+ std::filesystem::remove(path, ec);+ }+ } weights_file_cleanup{param.lru_expert_num > 0 ? weights_path : std::filesystem::path{}};+ if (param.lru_expert_num > 0) {+ ASSERT_TRUE(group_num == 1 && group_num2 == 1);+ std::ofstream ofs(weights_path, std::ios::binary | std::ios::trunc);+ ASSERT_TRUE(ofs.is_open()) << "Cannot create temp file: " << weights_path;+ auto append = [&](const void* data, size_t bytes) {+ weight_bin_offsets.push_back(static_cast<size_t>(ofs.tellp()));+ ofs.write(static_cast<const char*>(data), static_cast<std::streamsize>(bytes));+ };+ append(w0_q_packed.data(), w0_q_packed.size());+ append(w1_q_packed.data(), w1_q_packed.size());+ append(w2_q_packed.data(), w2_q_packed.size());+ append(w0_scale.data(), w0_scale.size() * sizeof(ov::float16));+ append(w1_scale.data(), w1_scale.size() * sizeof(ov::float16));+ append(w2_scale.data(), w2_scale.size() * sizeof(ov::float16));+ append(w0_zp_packed.data(), w0_zp_packed.size());+ append(w1_zp_packed.data(), w1_zp_packed.size());+ append(w2_zp_packed.data(), w2_zp_packed.size());+ }++ topology.add(moe_3gemm_fused_compressed("moe_3gemm_fused_compressed",+ moe_inputs,+ moe_config,+ weight_bin_offsets,+ param.lru_expert_num > 0 ? weights_path : std::filesystem::path{},+ param.lru_expert_num));auto net_config = get_test_default_config(engine);net_config.set_property(ov::intel_gpu::allow_new_shape_infer(true));+ if (param.micro_gemm_prefill) {+ net_config.set_property(ov::intel_gpu::moe_use_micro_gemm_prefill(true));+ net_config.set_property(ov::intel_gpu::moe_use_grouped_gemm_prefill(false));+ }network network(engine, topology, net_config);network.set_input_data("hidden_states", hidden_states_mem);network.set_input_data("routing_weights", routing_weights_mem);- auto outputs = network.execute();- auto output_prim = outputs.begin()->second.get_memory();- get_test_stream().flush();- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output_prim, get_test_stream());-- auto ref_output = routing_type == cldnn::MoERouterFused::RoutingType::SIGMOID_BIAS- ? ref.run_reference_sigmoid(hidden_states, routing_weights, routing_bias_data, routing_eps_val, w0_qdq, w1_qdq, w2_qdq)- : ref.run_reference_softmax(hidden_states, routing_weights, w0_qdq, w1_qdq, w2_qdq);+ // When all routed experts fit in the resident slots, the batched GEMV kernels (seq_len <= 32) and the+ // micro-GEMM prefill kernels read remapped slot ids from a host buffer. They may still be running when+ // execute() returns, so the buffer must stay alive. A later call of the same size reuses it.+ const size_t slot_id_bytes = config.seq_len * config.top_k * sizeof(uint32_t);+ const bool fits_resident_slots =+ config.seq_len * config.top_k <= param.lru_expert_num || (param.routed_experts > 0 && param.routed_experts <= param.lru_expert_num);+ const bool reads_slot_ids = config.seq_len <= 32 || param.micro_gemm_prefill;+ const bool checks_slot_ids = param.lru_expert_num > 0 && fits_resident_slots && reads_slot_ids;// SigmoidBias routing performs all routing math (sigmoid, bias, normalization) in f16 on the kernel side,// while the reference uses f32, leading to slightly larger numerical divergence than Softmax.const float base_tolerance = routing_type == cldnn::MoERouterFused::RoutingType::SIGMOID_BIAS ? 0.3f : 0.15f;const float tolerance = base_tolerance * (config.hidden_size / 128);- for (size_t i = 0; i < ref_output.size(); ++i) {- ASSERT_NEAR(static_cast<float>(output_ptr[i]), static_cast<float>(ref_output[i]), tolerance);++ // Inputs of every call are allocated before the host USM baseline: on an iGPU they are host USM themselves.+ std::vector<std::vector<ov::float16>> run_hidden{hidden_states};+ std::vector<std::vector<ov::float16>> run_routing{routing_weights};+ std::vector<memory::ptr> run_inputs;+ for (size_t run = 1; run < param.executions; ++run) {+ run_hidden.push_back(rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.hidden_size, -1.0f, 1.0f, 1000));+ run_routing.push_back(rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.num_experts, 0.0f, 1.0f, 1000));+ restrict_routing(run_routing.back());+ run_inputs.push_back(create_f16_tensor_3d(run_hidden.back(), config.batch_size, config.seq_len, config.hidden_size));+ run_inputs.push_back(create_f16_tensor_3d(run_routing.back(), config.batch_size, config.seq_len, config.num_experts));+ }++ const auto usm_host_before = engine.get_used_device_memory(allocation_type::usm_host);+ uint64_t usm_host_after_first = 0;+ for (size_t run = 0; run < param.executions; ++run) {+ if (run > 0) {+ network.set_input_data("hidden_states", run_inputs[2 * (run - 1)]);+ network.set_input_data("routing_weights", run_inputs[2 * (run - 1) + 1]);+ }+ auto outputs = network.execute();+ auto output_prim = outputs.begin()->second.get_memory();+ get_test_stream().flush();+ cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output_prim, get_test_stream());++ if (checks_slot_ids) {+ const auto usm_host_after = engine.get_used_device_memory(allocation_type::usm_host);+ ASSERT_GE(usm_host_after, usm_host_before + slot_id_bytes) << "execute() #" << run + 1;+ if (run == 0) {+ usm_host_after_first = usm_host_after;+ } else {+ ASSERT_EQ(usm_host_after, usm_host_after_first) << "execute() #" << run + 1 << " grew host USM";+ }+ }++ auto ref_output = routing_type == cldnn::MoERouterFused::RoutingType::SIGMOID_BIAS+ ? ref.run_reference_sigmoid(run_hidden[run], run_routing[run], routing_bias_data, routing_eps_val, w0_qdq, w1_qdq, w2_qdq)+ : ref.run_reference_softmax(run_hidden[run], run_routing[run], w0_qdq, w1_qdq, w2_qdq);+ for (size_t i = 0; i < ref_output.size(); ++i) {+ ASSERT_NEAR(static_cast<float>(output_ptr[i]), static_cast<float>(ref_output[i]), tolerance) << "execute() #" << run + 1;+ }}}@@ -834,6 +936,29 @@ INSTANTIATE_TEST_SUITE_P(smoke_batched_gemv_hidden_not_multiple_of_1024,::testing::Values(Moe3GemmTestParams{1, true, 2816, 512, 4, 2, 128},Moe3GemmTestParams{2, true, 2816, 512, 4, 2, 128})));+// Expert weights offloaded to disk (OFFLOAD_RATIO) with 4 of 8 experts resident. Two tokens with top-2 routing+// always fit the resident slots, so the batched GEMV kernels run on remapped slot ids; eight tokens can exceed+// them and fall back to the per-expert loop. The fourth case calls execute() twice with new inputs of the same size,+// so the second call's kernels read the slot-id buffer the first call allocated.+INSTANTIATE_TEST_SUITE_P(smoke_batched_gemv_offload,+ moe_3gemm_compressed_gpu_random,+ ::testing::Combine(::testing::Values(cldnn::MoERouterFused::RoutingType::SOFTMAX),+ ::testing::Values(Moe3GemmTestParams{2, false, 256, 256, 8, 2, 256, false, 4},+ Moe3GemmTestParams{2, true, 256, 256, 8, 2, 256, false, 4},+ Moe3GemmTestParams{8, false, 256, 256, 8, 2, 256, false, 4},+ Moe3GemmTestParams{2, true, 256, 256, 8, 2, 256, false, 4, false, 0, 2})));++// Offloaded expert weights on the micro-GEMM prefill path: 40 tokens is above the batched GEMV threshold (32), and+// micro-GEMM needs Xe2 and 4-bit weights. Routing restricted to 4 experts fits the 4 resident slots, so the prefill+// kernels run on remapped slot ids; unrestricted routing can exceed them and fall back to the per-expert loop.+// The third case calls execute() twice with new inputs of the same size, so the second call reuses the slot-id buffer.+INSTANTIATE_TEST_SUITE_P(smoke_prefill_micro_gemm_offload,+ moe_3gemm_compressed_gpu_random,+ ::testing::Combine(::testing::Values(cldnn::MoERouterFused::RoutingType::SOFTMAX),+ ::testing::Values(Moe3GemmTestParams{40, true, 256, 256, 8, 2, 256, false, 4, true, 4},+ Moe3GemmTestParams{40, true, 256, 256, 8, 2, 256, false, 4, true, 0},+ Moe3GemmTestParams{40, true, 256, 256, 8, 2, 256, false, 4, true, 4, 2})));+class moe_3gemm_compressed_gpu_u4 : public ::testing::TestWithParam<cldnn::MoERouterFused::RoutingType> {};class moe_3gemm_compressed_gpu_shared_random : public ::testing::TestWithParam<Moe3GemmTestParams> {};