#include <vector>
#include <cmath>
#include "kv_cache.h"
Go to the source code of this file.
|
| float | softcap_score (float s, float c) |
| |
| void | update_kv_cache_batch_cpu (KVCache *kv_cache, int layer_idx, const std::vector< float > &k_batch_for_layer, const std::vector< float > &v_batch_for_layer, int num_tokens_in_batch, int start_pos_in_sequence, int num_kv_heads, int head_dim) |
| |
| void | attention_batch_cpu (const std::vector< float > &q_batch_roped, KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, int start_pos_in_sequence, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, float softcap=0.0f) |
| |
| void | swa_visible_ranges (int pos, int window, int sinks, int &sink_hi, int &win_lo) |
| |
| void | attention_window_batch_cpu (const std::vector< float > &q_batch_roped, KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, int start_pos_in_sequence, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, int window, int sinks, float softcap=0.0f) |
| |
| void | attention_rows_batch_cpu (const std::vector< float > &q_batch_roped, const KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, const std::vector< std::vector< int > > &rows, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, float softcap=0.0f) |
| |
| std::vector< std::vector< int > > | attention_visible_rows (int num_tokens_in_batch, int start_pos_in_sequence, int window, int sinks) |
| |
| void | attention_bias_batch_cpu (const std::vector< float > &q_batch_roped, const KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, const std::vector< std::vector< int > > &rows, const std::vector< int > *key_pos, const std::vector< int > &query_pos, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, const float *bias, const std::vector< int > &buckets, float softcap=0.0f) |
| |
| void | attention_relkv_batch_cpu (const std::vector< float > &q_batch_roped, const KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, const std::vector< std::vector< int > > &rows, const std::vector< int > *key_pos, const std::vector< int > &query_pos, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, const float *rk, int rk_width, const float *rv, int rv_width, int max_distance, float softcap=0.0f) |
| |
| void | update_kv_cache_batch_cpu_sequence_aware (KVCache *kv_cache, int layer_idx, const std::vector< float > &k_batch_for_layer, const std::vector< float > &v_batch_for_layer, int num_tokens_in_batch, const std::vector< int > &sequence_indices, const std::vector< int > &position_in_sequence, int num_kv_heads, int head_dim) |
| |
| void | attention_batch_cpu_sequence_aware (const std::vector< float > &q_batch_roped, KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, const std::vector< int > &sequence_indices, const std::vector< int > &position_in_sequence, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, int max_seq_len_per_sequence) |
| |
◆ attention_batch_cpu()
Definition at line 92 of file cpu_attention.cpp.
103 {
109 return;
110 }
114
115
116
117#if defined(__EMSCRIPTEN_PTHREADS__)
119#else
120#pragma omp parallel for schedule(dynamic)
122#endif
126
130
132
133
137 ": history_len is " + std::to_string(
history_len) +
". Skipping score calculation for this head.");
138 continue;
139 }
141
151 }
155 ", history_pos " + std::to_string(
t_hist) +
158 scores[
t_hist] = -std::numeric_limits<float>::infinity();
159 continue;
160 }
161
163 for (
int d = 0; d <
head_dim; ++d) {
165 }
173 }
175
176 }
177
181 for (
int i = 0;
i < std::min(3, (
int)scores.size());
i++) {
183 }
185 }
187
189 if (scores[
t_hist] == -std::numeric_limits<float>::infinity() || scores[
t_hist] == 0.0f)
continue;
190
195 ", history_pos " + std::to_string(
t_hist) +
198 continue;
199 }
200
201 for (
int d = 0; d <
head_dim; ++d) {
204 }
205 }
206 }
207 }
208#if defined(__EMSCRIPTEN_PTHREADS__)
209 );
210#endif
211}
static void warning(const std::string &message)
static void info(const std::string &message)
static void error(const std::string &message)
float softcap_score(float s, float c)
void parallel_for(int64_t n, F &&fn)
void softmax_vector_cpu(const std::vector< float > &x, std::vector< float > &out)
References Logger::error(), Logger::info(), j, ember::parallel_for(), softcap_score(), softmax_vector_cpu(), and Logger::warning().
Referenced by attention_window_batch_cpu(), CPUBatchProcessor::forward_cpu_batch(), and ember_ir::Interpreter::run_nodes().
◆ attention_batch_cpu_sequence_aware()
| void attention_batch_cpu_sequence_aware |
( |
const std::vector< float > & |
q_batch_roped, |
|
|
KVCacheLayer & |
current_layer_kv_cache, |
|
|
std::vector< float > & |
batch_attn_output, |
|
|
int |
num_tokens_in_batch, |
|
|
const std::vector< int > & |
sequence_indices, |
|
|
const std::vector< int > & |
position_in_sequence, |
|
|
int |
num_q_heads, |
|
|
int |
num_kv_heads, |
|
|
int |
head_dim, |
|
|
float |
attention_scale, |
|
|
int |
max_seq_len_per_sequence |
|
) |
| |
◆ attention_bias_batch_cpu()
| void attention_bias_batch_cpu |
( |
const std::vector< float > & |
q_batch_roped, |
|
|
const KVCacheLayer & |
current_layer_kv_cache, |
|
|
std::vector< float > & |
batch_attn_output, |
|
|
int |
num_tokens_in_batch, |
|
|
const std::vector< std::vector< int > > & |
rows, |
|
|
const std::vector< int > * |
key_pos, |
|
|
const std::vector< int > & |
query_pos, |
|
|
int |
num_q_heads, |
|
|
int |
num_kv_heads, |
|
|
int |
head_dim, |
|
|
float |
attention_scale, |
|
|
const float * |
bias, |
|
|
const std::vector< int > & |
buckets, |
|
|
float |
softcap = 0.0f |
|
) |
| |
Definition at line 368 of file cpu_attention.cpp.
383 {
388 Logger::error(
"[ATTN_BIAS_CPU] q, row lists, positions or the bias table do not match the batch");
389 return;
390 }
392 const int D =
static_cast<int>(
buckets.size());
393#if defined(__EMSCRIPTEN_PTHREADS__)
395#else
396#pragma omp parallel for schedule(dynamic)
398#endif
402 std::vector<float> scores(
visible.size());
411 break;
412 }
414 const int n = std::min(std::max(
p -
j, 0),
D - 1);
416 }
425
428 }
432 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
435 }
436 }
437 }
438 }
439#if defined(__EMSCRIPTEN_PTHREADS__)
440 );
441#endif
442}
const float * p[kIrDepthSourcesMax]
References Logger::error(), j, n, p, ember::parallel_for(), softcap_score(), and softmax_vector_cpu().
Referenced by ember_ir::Interpreter::run_nodes().
◆ attention_relkv_batch_cpu()
| void attention_relkv_batch_cpu |
( |
const std::vector< float > & |
q_batch_roped, |
|
|
const KVCacheLayer & |
current_layer_kv_cache, |
|
|
std::vector< float > & |
batch_attn_output, |
|
|
int |
num_tokens_in_batch, |
|
|
const std::vector< std::vector< int > > & |
rows, |
|
|
const std::vector< int > * |
key_pos, |
|
|
const std::vector< int > & |
query_pos, |
|
|
int |
num_q_heads, |
|
|
int |
num_kv_heads, |
|
|
int |
head_dim, |
|
|
float |
attention_scale, |
|
|
const float * |
rk, |
|
|
int |
rk_width, |
|
|
const float * |
rv, |
|
|
int |
rv_width, |
|
|
int |
max_distance, |
|
|
float |
softcap = 0.0f |
|
) |
| |
Definition at line 444 of file cpu_attention.cpp.
462 {
469 Logger::error(
"[ATTN_RELKV_CPU] q, row lists, positions or the relative tables do not match the batch");
470 return;
471 }
473
475#if defined(__EMSCRIPTEN_PTHREADS__)
477#else
478#pragma omp parallel for schedule(dynamic)
480#endif
484 std::vector<float> scores(
visible.size());
494 break;
495 }
498 }
507 float dot = 0.0f,
rel = 0.0f;
512 }
516 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
519 }
524 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
527 }
529 }
530 }
531 }
532 }
533#if defined(__EMSCRIPTEN_PTHREADS__)
534 );
535#endif
536}
References Logger::error(), j, p, ember::parallel_for(), softcap_score(), and softmax_vector_cpu().
Referenced by ember_ir::Interpreter::run_nodes().
◆ attention_rows_batch_cpu()
◆ attention_visible_rows()
◆ attention_window_batch_cpu()
| void attention_window_batch_cpu |
( |
const std::vector< float > & |
q_batch_roped, |
|
|
KVCacheLayer & |
current_layer_kv_cache, |
|
|
std::vector< float > & |
batch_attn_output, |
|
|
int |
num_tokens_in_batch, |
|
|
int |
start_pos_in_sequence, |
|
|
int |
num_q_heads, |
|
|
int |
num_kv_heads, |
|
|
int |
head_dim, |
|
|
float |
attention_scale, |
|
|
int |
window, |
|
|
int |
sinks, |
|
|
float |
softcap = 0.0f |
|
) |
| |
Definition at line 219 of file cpu_attention.cpp.
232 {
233
237 return;
238 }
244 return;
245 }
248 Logger::error(
"[ATTN_WINDOW_CPU] KV cache holds fewer than " +
250 return;
251 }
252
253
254
255
256
257#if defined(__EMSCRIPTEN_PTHREADS__)
259#else
260#pragma omp parallel for schedule(dynamic)
262#endif
271 std::vector<float> scores(
visible.size());
272
281 }
285 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
288 }
289 }
290 }
291#if defined(__EMSCRIPTEN_PTHREADS__)
292 );
293#endif
294}
void attention_batch_cpu(const std::vector< float > &q_batch_roped, KVCacheLayer ¤t_layer_kv_cache, std::vector< float > &batch_attn_output, int num_tokens_in_batch, int start_pos_in_sequence, int num_q_heads, int num_kv_heads, int head_dim, float attention_scale, float softcap)
References attention_batch_cpu(), Logger::error(), j, ember::parallel_for(), pos, softcap_score(), softmax_vector_cpu(), and swa_visible_ranges().
Referenced by ember_ir::Interpreter::run_nodes().
◆ softcap_score()
◆ swa_visible_ranges()
◆ update_kv_cache_batch_cpu()
Definition at line 8 of file cpu_attention.cpp.
17 {
20 return;
21 }
23 Logger::error(
"update_kv_cache_batch_cpu: layer_idx " + std::to_string(
layer_idx) +
" is out of bounds for KVCache layers (size " + std::to_string(
kv_cache->layers.size()) +
").");
24 return;
25 }
33
35 Logger::error(
"[KV_BATCH_UPDATE L" + std::to_string(
layer_idx) +
"] k_batch_for_layer size mismatch. Expected " +
37 return;
38 }
39
41 Logger::error(
"[KV_BATCH_UPDATE L" + std::to_string(
layer_idx) +
"] v_batch_for_layer size mismatch. Expected " +
43 return;
44 }
45
49 "] Precondition failed: Layer cache not sized to max_seq_len_config. K size: " + std::to_string(
layer_cache.k.size()) +
50 ", V size: " + std::to_string(
layer_cache.v.size()) +
52 ". Check KVCache::initialize.");
53 return;
54 }
57
59
63 ") is out of bounds for total cache size. Skipping update for this token.");
64 continue;
65 }
66
72
73
77
78
83
87
88 }
89
90}
Key-Value cache for a single transformer layer.
References Logger::error(), Logger::info(), and j.
Referenced by CPUBatchProcessor::forward_cpu_batch().
◆ update_kv_cache_batch_cpu_sequence_aware()
Definition at line 538 of file cpu_attention.cpp.
548 {
550 Logger::error(
"update_kv_cache_batch_cpu_sequence_aware: KVCache is null.");
551 return;
552 }
555 " is out of bounds for KVCache layers (size " + std::to_string(
kv_cache->layers.size()) +
").");
556 return;
557 }
558
561
564
567
573 ") is out of bounds for total cache size. Skipping update for this token.");
574 continue;
575 }
576
578
582
586 }
587}
References Logger::error(), and j.
Referenced by CPUBatchProcessor::forward_cpu_batch().