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
[CPU][PA]Fix block_num calculation for vLLM (#31207)
Zhang Yi328b88954750
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cppindex db8f2baf03c..c2c3e2355c1 100644--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp@@ -509,14 +509,16 @@ static void quantize_block_by_channel(const ov::intel_cpu::PlainTensor& src,// Quantized cache is either u8/u4, the plain memory is both uint8,// Here we use stride_bytes instead of stride which consider divide sub_byte_multiplier automatically.if (past_len == 0) {- const auto total_blocks =- block_indices_begins.ptr<int32_t>()[sub_seq_id + 1] - block_indices_begins.ptr<int32_t>()[sub_seq_id];+ // (block_indices_begins[sub_seq_id + 1] - 1) is not a reliable upper bound for sub_seq,+ // (block_indices_begins[sub_seq_id + 1] - 1) may not exist in current batch under vLLM case,+ // For example prompt_size=391, max-num-batched-tokens=256, vLLM allocates 13 blocks [0, 13)+ // but in first iteration, vLLM only feeds 256 tokens, [0,8).+ const auto total_blocks = ov::intel_cpu::div_up(q_len, block_size);parallel_for(total_blocks, [&](int32_t block_count) {const auto block_id = block_number_start + block_count;const auto block_number = block_indices.ptr<int32_t>()[block_id];- const auto token_num = (block_id == (block_indices_begins.ptr<int32_t>()[sub_seq_id + 1] - 1))- ? (q_len - block_count * block_size)- : block_size;+ const auto token_num =+ (block_count == (total_blocks - 1)) ? (q_len - block_count * block_size) : block_size;const size_t b_in_tokens = subsequence_begins.ptr<int32_t>()[sub_seq_id] + block_count * block_size;auto base = dst.ptr<uint8_t, DST_PREC>(block_number, h, 0, 0);auto* p_scales = reinterpret_cast<float*>(base);@@ -534,7 +536,7 @@ static void quantize_block_by_channel(const ov::intel_cpu::PlainTensor& src,} else {const auto prev_nums = past_len % block_size;const size_t block_offset = block_number_start + past_len / block_size;- const auto total_blocks = block_indices_begins.ptr<int32_t>()[sub_seq_id + 1] - block_offset;+ const auto total_blocks = ov::intel_cpu::div_up(prev_nums + q_len, block_size);parallel_for(total_blocks, [&](size_t block_id) {const bool is_first_block = block_id == 0;size_t offset = 0;diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/paged_attn.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/paged_attn.cppindex 2b7b468a3b9..486bd664338 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/paged_attn.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/paged_attn.cpp@@ -38,7 +38,7 @@ using namespace std;namespace ov {namespace test {using InputShapes = std::vector<InputShape>;-using PagedAttnTestParams = std::tuple<ElementType, InputShapes>;+using PagedAttnTestParams = std::tuple<ElementType, InputShapes, bool>;class PagedAttnTestBase : public testing::WithParamInterface<PagedAttnTestParams>,virtual public ov::test::SubgraphBaseTest,@@ -47,7 +47,8 @@ public:static std::string getTestCaseName(const testing::TestParamInfo<PagedAttnTestParams>& obj) {ElementType inType;InputShapes inputShapes;- std::tie(inType, inputShapes) = obj.param;+ bool extendBlockIndices;+ std::tie(inType, inputShapes, extendBlockIndices) = obj.param;std::ostringstream result;result << "IS=";for (const auto& shape : inputShapes) {@@ -63,7 +64,8 @@ public:}result << ")_";}- result << "Prc=" << inType;+ result << "Prc=" << inType << "_";+ result << "ExtendBlockIndices=" << extendBlockIndices << std::endl;return result.str();}@@ -210,7 +212,8 @@ public:void SetUp() override {ElementType inType;InputShapes inputShapes;- std::tie(inType, inputShapes) = this->GetParam();+ bool extendBlockIndices;+ std::tie(inType, inputShapes, extendBlockIndices) = this->GetParam();targetDevice = ov::test::utils::DEVICE_CPU;rel_threshold = 1e-2f;configuration[ov::hint::inference_precision.name()] = ov::element::f32;@@ -241,7 +244,10 @@ public:value += stride;}}- virtual void generate(int idx, const bool isPagedAttn, const std::vector<ov::Shape>& targetInputStaticShapes) {+ virtual void generate(int idx,+ const bool isPagedAttn,+ const std::vector<ov::Shape>& targetInputStaticShapes,+ bool extendBlockIndices) {inputs.clear();auto create_input = [this](std::shared_ptr<ov::op::v0::Parameter> param, ov::Shape shape, float val) {if (param->get_element_type() == ov::element::i32) {@@ -300,7 +306,12 @@ public:subsequence_begins_data[0] = 0;subsequence_begins_data[1] = targetInputStaticShapes[0][0];block_indices_begins_data[0] = 0;- block_indices_begins_data[1] = 1;+ // test case here only has 1 block for prefill, but we allocate 2 blocks to simulate the vLLM case.+ // To test whether we have overflow in kernels.+ if (extendBlockIndices)+ block_indices_begins_data[1] = 2;+ else+ block_indices_begins_data[1] = 1;block_indices_data[0] = 0;} else {past_lens_data[0] = past_len_count;@@ -349,7 +360,7 @@ public:class PagedAttnVSSDPATest : public PagedAttnTestBase {public:- std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model) {+ std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model, bool extendBlockIndices) {function = model;prepare();for (const auto& input : compiledModel.inputs()) {@@ -373,7 +384,7 @@ public:std::vector<ov::Tensor> outputs;int idx = 0;for (auto&& shapes : targetStaticShapes) {- generate(idx++, true, shapes);+ generate(idx++, true, shapes, extendBlockIndices);for (const auto& input : inputs) {inferRequest.set_tensor(input.first, input.second);}@@ -392,7 +403,7 @@ public:std::vector<ov::Tensor> outputs;int idx = 0;for (auto&& shapes : targetStaticShapes) {- generate(idx++, false, shapes);+ generate(idx++, false, shapes, false);for (const auto& input : inputs) {inferRequest.set_tensor(input.first, input.second);}@@ -410,11 +421,12 @@ TEST_P(PagedAttnVSSDPATest, CompareWithRefs) {SKIP_IF_CURRENT_TEST_IS_DISABLED();ElementType inType;InputShapes inputShapes;- std::tie(inType, inputShapes) = this->GetParam();+ bool extendBlockIndices;+ std::tie(inType, inputShapes, extendBlockIndices) = this->GetParam();if (inType == ElementType::bf16 && !ov::with_cpu_x86_bfloat16())GTEST_SKIP();// compare the logits from paged attn and sdpa- auto actualOutputs = run_test(function);+ auto actualOutputs = run_test(function, extendBlockIndices);auto expectedOutputs = run_ref_test(functionRefs);for (size_t i = 0; i < actualOutputs.size(); i++) {ov::test::utils::compare(expectedOutputs[i], actualOutputs[i], abs_threshold, rel_threshold);@@ -433,7 +445,8 @@ const std::vector<InputShapes> inputShapeAndReorders = { // greedy searchINSTANTIATE_TEST_SUITE_P(smoke_PagedAttnVSSDPATest,PagedAttnVSSDPATest,::testing::Combine(::testing::Values(ElementType::f32, ElementType::bf16),- ::testing::ValuesIn(inputShapeAndReorders)),+ ::testing::ValuesIn(inputShapeAndReorders),+ ::testing::Values(true, false)),PagedAttnTestBase::getTestCaseName);} // namespace@@ -563,7 +576,7 @@ public:return model;}- std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model) {+ std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model, bool extendBlockIndices) {configuration[ov::hint::kv_cache_precision.name()] = ov::element::f16;function = model;prepare();@@ -588,7 +601,7 @@ public:std::vector<ov::Tensor> outputs;int idx = 0;for (auto&& shapes : targetStaticShapes) {- generate(idx++, true, shapes);+ generate(idx++, true, shapes, extendBlockIndices);for (const auto& input : inputs) {inferRequest.set_tensor(input.first, input.second);}@@ -607,7 +620,7 @@ public:std::vector<ov::Tensor> outputs;int idx = 0;for (auto&& shapes : targetStaticShapes) {- generate(idx++, false, shapes);+ generate(idx++, false, shapes, false);for (const auto& input : inputs) {inferRequest.set_tensor(input.first, input.second);}@@ -626,11 +639,12 @@ TEST_P(PagedAttnVSMatmulTest, CompareWithRefs) {SKIP_IF_CURRENT_TEST_IS_DISABLED();ElementType inType;InputShapes inputShapes;- std::tie(inType, inputShapes) = this->GetParam();+ bool extendBlockIndices;+ std::tie(inType, inputShapes, extendBlockIndices) = this->GetParam();if (inType == ElementType::bf16 && !ov::with_cpu_x86_bfloat16())GTEST_SKIP();// compare the logits from paged attn and sdpa- auto actualOutputs = run_test(function);+ auto actualOutputs = run_test(function, extendBlockIndices);auto expectedOutputs = run_ref_test(functionRefs);for (size_t i = 0; i < actualOutputs.size(); i++) {ov::test::utils::compare(expectedOutputs[i], actualOutputs[i], abs_threshold, rel_threshold);@@ -650,7 +664,8 @@ const std::vector<InputShapes> inputShapes = { // greedy searchINSTANTIATE_TEST_SUITE_P(smoke_PagedAttnVSMatmulTest,PagedAttnVSMatmulTest,::testing::Combine(::testing::Values(ElementType::f32, ElementType::f16),- ::testing::ValuesIn(inputShapes)),+ ::testing::ValuesIn(inputShapes),+ ::testing::Values(true, false)),PagedAttnTestBase::getTestCaseName);} // namespace