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] U8 KV cache corruption when batch size grows (#37381)
Piotr Krzemiński0f570cb5ec62
diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cppindex 69dccaa680a..4eac33eea3c 100644--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp@@ -2775,13 +2775,13 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M// Grow the norm buffer if needed; preserve old content for L0 tokens so subsequent// decodes see the correct norms (this path grows in place, no beam reorder).auto grow_meta_data = [&](PlainTensor& meta_data) {- if (meta_data && meta_data.size(2) >= L0 + L1) {+ if (meta_data && meta_data.size(0) == B && meta_data.size(1) == H && meta_data.size(2) >= L0 + L1) {return;}PlainTensor old = meta_data;meta_data = PlainTensor{};meta_data.resize<float>({B, H, L0 + L1, 1});- if (old && L0 > 0 && !is_reset) {+ if (old && L0 > 0 && !is_reset && old.size(0) == B && old.size(1) == H) {for (size_t b = 0; b < B; ++b) {for (size_t h = 0; h < H; ++h) {std::memcpy(meta_data.ptr<float>(b, h, 0), old.ptr<float>(b, h, 0), sizeof(float) * L0);@@ -2899,17 +2899,29 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M};internal_mem_k->redefineDesc(reset_desc(k_kvcache_precision, S_cache));internal_mem_v->redefineDesc(reset_desc(v_kvcache_precision, SV_cache));- if ((is_quantized_cache(k_kvcache_precision) || is_quantized_cache(v_kvcache_precision)) && !is_k_turboq &&- !is_v_turboq) {- auto& old_scale_zp_k = m_k_state->get_scale_zp();- auto& old_scale_zp_v = m_v_state->get_scale_zp();- // only dim0, dim1 need change- // LBHS- old_scale_zp_k.m_strides[0] = m_key_spec.by_channel ? H * B * S : H * B * S / m_key_spec.group_size * 2;- old_scale_zp_k.m_strides[1] = m_key_spec.by_channel ? H * S : H * S / m_key_spec.group_size * 2;- old_scale_zp_v.m_strides[0] =- m_value_spec.by_channel ? H * B * SV : H * B * SV / m_value_spec.group_size * 2;- old_scale_zp_v.m_strides[1] = m_value_spec.by_channel ? H * SV : H * SV / m_value_spec.group_size * 2;++ const bool need_k_szp = is_quantized_cache(k_kvcache_precision) && !is_k_turboq;+ const bool need_v_szp = is_quantized_cache(v_kvcache_precision) && !is_v_turboq;+ if (need_k_szp || need_v_szp) {+ auto reset_scale_zp =+ [&](const ov::Extensions::Cpu::CacheSpec& quant_param, size_t hidden_states, size_t cache_max_size) {+ const size_t capacity_L = cache_max_size / (B * H * hidden_states);+ PlainTensor scale_zp;+ scale_zp.resize<float>(compute_scale_zp_shape(quant_param,+ hidden_states,+ B,+ H,+ std::max(capacity_L, (L0 + L1) * 2),+ order,+ real_order));+ return scale_zp;+ };+ if (need_k_szp) {+ m_k_state->set_scale_zp(reset_scale_zp(m_key_spec, S, m_k_state->internal_state_max_size()));+ }+ if (need_v_szp) {+ m_v_state->set_scale_zp(reset_scale_zp(m_value_spec, SV, m_v_state->internal_state_max_size()));+ }}}if (need_redefine) {diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cppindex fa4c49d741f..0105df5d81f 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp@@ -230,7 +230,8 @@ void ConcatSDPTest::generate_inputs(const std::vector<ov::Shape>& targetInputStakv_shape[1] = static_cast<size_t>(m_headNumKV);auto past_shape = targetInputStaticShapes[1];past_shape[1] = static_cast<size_t>(m_headNumKV);- past_shape[2] = (idx == 0) ? 0 : m_accum_L_q;+ const bool starts_conversation = idx == 0 || m_resetBefore.count(static_cast<size_t>(idx)) != 0;+ past_shape[2] = starts_conversation ? 0 : m_accum_L_q;m_accum_L_q = past_shape[2] + q_shape[2];inputs.insert({params[0], fill_gaussian(params[0], q_shape, idx + 1)});@@ -253,6 +254,12 @@ ConcatSDPTest::run_test(const std::shared_ptr<ov::Model>& model, const ov::AnyMam_accum_L_q = 0;std::vector<std::vector<ov::Tensor>> all;for (const auto& shapes : targetStaticShapes) {+ const auto iter = static_cast<size_t>(m_iter);+ if (iter != 0 && m_resetBefore.count(iter) != 0) {+ for (auto&& state : req.query_state()) {+ state.reset();+ }+ }generate_inputs(shapes);for (const auto& port : compiledModel.inputs()) {const auto& name = port.get_node()->get_friendly_name();@@ -310,5 +317,28 @@ TEST_P(ConcatSDPTest, CompareWithRefs) {}}+std::string ConcatSDPResetStateTest::getTestCaseName(const testing::TestParamInfo<ConcatSDPTestParams>& obj) {+ return "Reset_" + ConcatSDPTest::getTestCaseName(obj);+}++void ConcatSDPResetStateTest::SetUp() {+ ConcatSDPTest::SetUp();+ // A declared past length of 0 marks the first prompt of a new conversation, so the+ // states are reset right before that iteration.+ const auto& inputShapes = std::get<1>(this->GetParam());+ const auto& pastShapes = inputShapes[1].second;+ for (size_t i = 1; i < pastShapes.size(); ++i) {+ if (pastShapes[i][2] == 0) {+ m_resetBefore.insert(i);+ }+ }+}++TEST_P(ConcatSDPResetStateTest, CompareWithRefs) {+ SKIP_IF_CURRENT_TEST_IS_DISABLED();+ ASSERT_FALSE(m_resetBefore.empty()) << "test shapes must contain at least one reset point";+ run();+}+} // namespace test} // namespace ovdiff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hppindex 5a43cdacc1f..4e089ee083c 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp@@ -3,6 +3,8 @@//#pragma once+#include <set>+#include "shared_test_classes/base/ov_subgraph.hpp"#include "utils/cpu_test_utils.hpp"@@ -58,6 +60,19 @@ protected:int64_t m_headNumKV = 8;int m_iter = 0;size_t m_accum_L_q = 0;+ // Iteration indices at which the KV-cache states are reset before the infer, i.e. the+ // first prompt of a new conversation. Iteration 0 is implicitly a conversation start+ std::set<size_t> m_resetBefore;+};++// Same subgraph as ConcatSDPTest, but the iteration list is split into several conversations+// by `reset_state` calls, with a different batch in each conversation+class ConcatSDPResetStateTest : public ConcatSDPTest {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<ConcatSDPTestParams>& obj);++protected:+ void SetUp() override;};} // namespace testdiff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cppindex 0a2c7cc8238..d8ac1b628a5 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp@@ -41,6 +41,32 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,::testing::Values<int64_t>(8)),ConcatSDPTest::getTestCaseName);+const std::vector<std::vector<InputShape>> resetStateInputShapes = {+ // A: batch 1, 40 tokens -> B: batch 4, 15 + 9 tokens -> C: batch 2, 10 + 1 tokens+ {+ {{-1, 8, -1, 64},+ {{1, 8, 40, 64},+ {4, 8, 15, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64},+ {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64},+ {2, 8, 10, 64}, {2, 8, 1, 64}}},+ {{-1, 8, -1, 64},+ {{1, 8, 0, 64},+ {4, 8, 0, 64}, {4, 8, 15, 64}, {4, 8, 16, 64}, {4, 8, 17, 64}, {4, 8, 18, 64},+ {4, 8, 19, 64}, {4, 8, 20, 64}, {4, 8, 21, 64}, {4, 8, 22, 64}, {4, 8, 23, 64},+ {2, 8, 0, 64}, {2, 8, 10, 64}}},+ },+};++INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPResetStateTest,+ ConcatSDPResetStateTest,+ ::testing::Combine(::testing::Values(ElementType::f32),+ ::testing::ValuesIn(resetStateInputShapes),+ ::testing::Values(cfg_none, cfg_u8_sym),+ ::testing::Values(false),+ ::testing::Values<int64_t>(8),+ ::testing::Values<int64_t>(8)),+ ConcatSDPResetStateTest::getTestCaseName);+} // namespace} // namespace test} // namespace ovdiff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cppindex 2aaceee9884..68546794593 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp@@ -61,6 +61,32 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,::testing::Values<int64_t>(8, 2, 1)),ConcatSDPTest::getTestCaseName);+const std::vector<std::vector<InputShape>> resetStateInputShapes = {+ // A: batch 1, 40 tokens -> B: batch 4, 15 + 9 tokens -> C: batch 2, 10 + 1 tokens+ {+ {{-1, 8, -1, 64},+ {{1, 8, 40, 64},+ {4, 8, 15, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64},+ {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64},+ {2, 8, 10, 64}, {2, 8, 1, 64}}},+ {{-1, 8, -1, 64},+ {{1, 8, 0, 64},+ {4, 8, 0, 64}, {4, 8, 15, 64}, {4, 8, 16, 64}, {4, 8, 17, 64}, {4, 8, 18, 64},+ {4, 8, 19, 64}, {4, 8, 20, 64}, {4, 8, 21, 64}, {4, 8, 22, 64}, {4, 8, 23, 64},+ {2, 8, 0, 64}, {2, 8, 10, 64}}},+ },+};++INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPResetStateTest_Codecs,+ ConcatSDPResetStateTest,+ ::testing::Combine(::testing::Values(ElementType::f32),+ ::testing::ValuesIn(resetStateInputShapes),+ ::testing::ValuesIn(all_kv_cfgs()),+ ::testing::Values(false),+ ::testing::Values<int64_t>(8),+ ::testing::Values<int64_t>(8)),+ ConcatSDPResetStateTest::getTestCaseName);+} // namespace} // namespace test} // namespace ov