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] reduce temporary weight memory of PA and SDPA (#29850)
Yuan Hu0e60910102e9
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cppindex b33b44062e2..1ffd07875d3 100644--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp@@ -1435,6 +1435,7 @@ struct MHAHelper {size_t _key_group_size = 0;size_t _value_group_size = 0;bool _quant_key_bychannel = 0;+ size_t _new_score_stride = 0;PlainTensor _weight; // [nthr, H, 32, rnd_up(kv_len, block_size)], shared by first and second loop along bhPlainTensor _output; // [nthr, 32, H, S], shared by first and second loop along bh@@ -1475,6 +1476,11 @@ struct MHAHelper {_weight.resize<float>({size_t{1}, size_t{1}, size_t{1}, size_t{1}});}+ void resize_temporary_weight_buffer(const size_t& h) {+ // resize temporary buffers, weight.size(3) will be aligned to block_size+ _weight.resize<float>({static_cast<size_t>(_nthr), h, _block_size, _new_score_stride});+ }+void init(size_t H,size_t S,size_t SV,@@ -1506,11 +1512,9 @@ struct MHAHelper {_sliding_window = sliding_window;_d_scale = d_scale;- auto prev_score_stride = _weight.stride(2);+ auto prev_score_stride = _new_score_stride;auto want_score_stride = rnd_up(kv_len, _block_size);- auto new_score_stride = std::max(prev_score_stride, want_score_stride);- // resize temporary buffers, weight.size(3) will be aligned to block_size- _weight.resize<float>({static_cast<size_t>(_nthr), H, _block_size, new_score_stride});+ _new_score_stride = std::max(prev_score_stride, want_score_stride);// std::max(S, SV) here is to ensure by_channel quantize has enough buffer to useif (_quant_key_bychannel)_output.resize<float>({static_cast<size_t>(_nthr), _block_size, H, std::max(S, SV)});@@ -1518,7 +1522,7 @@ struct MHAHelper {_output.resize<float>({static_cast<size_t>(_nthr), _block_size, H, SV});// TODO: kernel supports stride- if (_qk_gemm.empty() || prev_score_stride < new_score_stride) {+ if (_qk_gemm.empty() || prev_score_stride < _new_score_stride) {_qk_gemm.resize(_block_size);_wv_gemm.resize(_block_size);_wv_gemm_acc.resize(_block_size);@@ -1528,7 +1532,7 @@ struct MHAHelper {S,H * S,_block_size,- _weight.stride(2),+ _new_score_stride,false,in_type);_wv_gemm[i] =@@ -1536,7 +1540,7 @@ struct MHAHelper {SV,_block_size,// if it's bf16, the stride needs double due to reuse float buffer- (in_type == ov::element::Type_t::f32 ? 1 : 2) * _weight.stride(2),+ (in_type == ov::element::Type_t::f32 ? 1 : 2) * _new_score_stride,SV,_output.stride(1),false,@@ -1546,7 +1550,7 @@ struct MHAHelper {SV,_block_size,// if it's bf16, the stride needs double due to reuse float buffer- (in_type == ov::element::Type_t::f32 ? 1 : 2) * _weight.stride(2),+ (in_type == ov::element::Type_t::f32 ? 1 : 2) * _new_score_stride,SV,_output.stride(1),false,@@ -1651,7 +1655,7 @@ struct MHAHelper {auto cur_kv_len_blocks = div_up(cur_kv_len, _block_size);for (size_t h = hq_beg; h < hq_end; h++) {auto* q_ptr = query.ptr<DATA_TYPE>(h, q_start, 0);- auto* c_ptr = _weight.ptr<float>(ithr, h, 0, 0);+ auto* c_ptr = _weight.ptr<float>(ithr, h - hq_beg, 0, 0);// for each query block, loop through all key block// for blocks:// 1 0 0 0 ...@@ -1671,7 +1675,7 @@ struct MHAHelper {for (size_t m = q_start; m < q_end; m++) {// apply attention mask & sofmaxauto ncausal = (cur_kv_len - q_cnt + (m - q_start) + 1);- auto score = _weight.ptr<float>(ithr, h, m - q_start);+ auto score = _weight.ptr<float>(ithr, h - hq_beg, m - q_start);if (_sliding_window) {size_t start_idx = 0;auto new_causal = ncausal;@@ -1725,7 +1729,7 @@ struct MHAHelper {}// reuse float buffer, need to use float to compute offset- auto* w_ptr = reinterpret_cast<DATA_TYPE*>(_weight.ptr<float>(ithr, h, 0, 0));+ auto* w_ptr = reinterpret_cast<DATA_TYPE*>(_weight.ptr<float>(ithr, h - hq_beg, 0, 0));float* fp32_out_ptr =q_is_xf16 ? _output.ptr<float>(ithr, 0, h, 0) : output_emb.ptr<float>(q_start, h * SV);@@ -1796,7 +1800,7 @@ struct MHAHelper {for (size_t h = hq_beg; h < hq_end; h++) {(*_gemv)(query.ptr<DATA_TYPE>(h, pq),present_key.ptr<KEY_CACHE_TYPE>(block_number, hk),- _weight.ptr<float>(ithr, h, pq) + pk);+ _weight.ptr<float>(ithr, h - hq_beg, pq) + pk);}}}@@ -1810,13 +1814,13 @@ struct MHAHelper {if (precision_of<KEY_CACHE_TYPE>::value == ov::element::u8 && _quant_key_bychannel) {dot_product_block_by_channel(query.ptr<DATA_TYPE>(h, pq),present_key.ptr<uint8_t>(block_number, hk),- _weight.ptr<float>(ithr, h, pq) + pk,+ _weight.ptr<float>(ithr, h - hq_beg, pq) + pk,S,std::min(_block_size, cur_kv_len - pk));} else {dot_product_block(query.ptr<DATA_TYPE>(h, pq),present_key.ptr<KEY_CACHE_TYPE>(block_number, hk),- _weight.ptr<float>(ithr, h, pq) + pk,+ _weight.ptr<float>(ithr, h - hq_beg, pq) + pk,S,std::min(_block_size, cur_kv_len - pk),_key_group_size);@@ -1837,8 +1841,8 @@ struct MHAHelper {alibi_slope = alibi_slopes.ptr<float>()[h];alibi_lookup = _alibi_lookup.ptr<float>() + _alibi_lookup.m_dims[0] - cur_kv_len;}- attn_softmax_kernel<float>(_weight.ptr<float>(ithr, h, pq),- _weight.ptr<float>(ithr, h, pq),+ attn_softmax_kernel<float>(_weight.ptr<float>(ithr, h - hq_beg, pq),+ _weight.ptr<float>(ithr, h - hq_beg, pq),_d_scale,alibi_lookup,nullptr,@@ -1851,7 +1855,7 @@ struct MHAHelper {alibi_slope);if (score_output) {memcpy(score_output + h * rnd_up(cur_kv_len, 16),- _weight.ptr<float>(ithr, h, pq),+ _weight.ptr<float>(ithr, h - hq_beg, pq),cur_kv_len * sizeof(float));}}@@ -1869,7 +1873,7 @@ struct MHAHelper {present_value.m_ptr.get() + v_stride);attn_acc_value_block<typename element_type_traits<VALUE_PREC>::value_type, VALUE_PREC>(_output.ptr<float>(ithr, pq, h),- _weight.ptr<float>(ithr, h, pq) + pv,+ _weight.ptr<float>(ithr, h - hq_beg, pq) + pv,v_ptr,SV,std::min(_block_size, cur_kv_len - pv),@@ -2270,6 +2274,8 @@ struct MHA {attn_work_count * Hk <= 2 * _helper._nthr? false: true; // or less than 2 work items per thread, loop H+ auto weight_h = loop_hk ? _helper.H / Hk : 1;+ _helper.resize_temporary_weight_buffer(weight_h);parallel_for2d_dynamic(attn_work_count, loop_hk ? Hk : _helper.H, [&](size_t w, size_t hx) {size_t hk, hq_beg, hq_end;diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cppindex f97dc8d0999..49f376f1106 100644--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp@@ -342,7 +342,8 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {qk_scratch_b.resize<T>({B, Hk, qk_gemm_ptr->get_scratch_b_size() / data_size});wv_scratch_b.resize<T>({B, Hk, wv_gemm_ptr->get_scratch_b_size() / data_size});const size_t m_block_size = qk_gemm_ptr->get_mblk_size();- weight_score.resize<float>({m_threads_num, H, m_block_size, kv_len});++ weight_score.resize<float>({m_threads_num, 1, m_block_size, kv_len});if (has_out_transpose) {fp32_out.resize<float>({B, q_len, H, head_size_v});} else {@@ -387,7 +388,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {auto m_cnt = m_end - m_start;size_t tid = parallel_get_thread_num();T* q_ptr = &query.at<T>({b, h, m_start, 0});- auto* c_ptr = weight_score.ptr<float>(ithr, h, 0, 0);+ auto* c_ptr = weight_score.ptr<float>(ithr, 0, 0, 0);T* k_ptr = &qk_scratch_b.at<T>({b, h / h_each_group_len, 0});qk_gemm_ptr->executeGemm(m_cnt < m_block_size,q_ptr,@@ -423,7 +424,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {for (size_t m = m_start; m < m_end; m++) {// apply attention mask & sofmaxauto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;- auto score = weight_score.ptr<float>(ithr, h, m - m_start);+ auto score = weight_score.ptr<float>(ithr, 0, m - m_start);attn_softmax(reinterpret_cast<void*>(score),reinterpret_cast<T*>(score),d_scale,@@ -437,7 +438,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {precision_of<T>::value,precision_of<T>::value);}- auto* w_ptr = reinterpret_cast<T*>(weight_score.ptr<float>(ithr, h, 0, 0));+ auto* w_ptr = reinterpret_cast<T*>(weight_score.ptr<float>(ithr, 0, 0, 0));float* fp32_out_ptr;if (is_xf16) {fp32_out_ptr = has_out_transpose ? &fp32_out.at<float>({b, m_start, h, 0})