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()) +
").");
35 Logger::error(
"[KV_BATCH_UPDATE L" + std::to_string(
layer_idx) +
"] k_batch_for_layer size mismatch. Expected " +
41 Logger::error(
"[KV_BATCH_UPDATE L" + std::to_string(
layer_idx) +
"] v_batch_for_layer size mismatch. Expected " +
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.");
63 ") is out of bounds for total cache size. Skipping update for this token.");
117#if defined(__EMSCRIPTEN_PTHREADS__)
120#pragma omp parallel for schedule(dynamic)
137 ": history_len is " + std::to_string(
history_len) +
". Skipping score calculation for this head.");
155 ", history_pos " + std::to_string(
t_hist) +
158 scores[
t_hist] = -std::numeric_limits<float>::infinity();
163 for (
int d = 0; d <
head_dim; ++d) {
181 for (
int i = 0;
i < std::min(3, (
int)scores.size());
i++) {
189 if (scores[
t_hist] == -std::numeric_limits<float>::infinity() || scores[
t_hist] == 0.0f)
continue;
195 ", history_pos " + std::to_string(
t_hist) +
201 for (
int d = 0; d <
head_dim; ++d) {
208#if defined(__EMSCRIPTEN_PTHREADS__)
248 Logger::error(
"[ATTN_WINDOW_CPU] KV cache holds fewer than " +
257#if defined(__EMSCRIPTEN_PTHREADS__)
260#pragma omp parallel for schedule(dynamic)
271 std::vector<float> scores(
visible.size());
285 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
291#if defined(__EMSCRIPTEN_PTHREADS__)
301 const std::vector<std::vector<int>>&
rows,
311 Logger::error(
"[ATTN_ROWS_CPU] q or row lists do not match the batch");
316#if defined(__EMSCRIPTEN_PTHREADS__)
319#pragma omp parallel for schedule(dynamic)
324 std::vector<float> scores(
visible.size());
343 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
350#if defined(__EMSCRIPTEN_PTHREADS__)
373 const std::vector<std::vector<int>>&
rows,
374 const std::vector<int>*
key_pos,
381 const std::vector<int>&
buckets,
388 Logger::error(
"[ATTN_BIAS_CPU] q, row lists, positions or the bias table do not match the batch");
392 const int D =
static_cast<int>(
buckets.size());
393#if defined(__EMSCRIPTEN_PTHREADS__)
396#pragma omp parallel for schedule(dynamic)
402 std::vector<float> scores(
visible.size());
414 const int n = std::min(std::max(
p -
j, 0),
D - 1);
432 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
439#if defined(__EMSCRIPTEN_PTHREADS__)
449 const std::vector<std::vector<int>>&
rows,
450 const std::vector<int>*
key_pos,
469 Logger::error(
"[ATTN_RELKV_CPU] q, row lists, positions or the relative tables do not match the batch");
475#if defined(__EMSCRIPTEN_PTHREADS__)
478#pragma omp parallel for schedule(dynamic)
484 std::vector<float> scores(
visible.size());
507 float dot = 0.0f,
rel = 0.0f;
516 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
524 if (scores[
i] == -std::numeric_limits<float>::infinity() || scores[
i] == 0.0f)
continue;
533#if defined(__EMSCRIPTEN_PTHREADS__)
550 Logger::error(
"update_kv_cache_batch_cpu_sequence_aware: KVCache is null.");
555 " is out of bounds for KVCache layers (size " + std::to_string(
kv_cache->layers.size()) +
").");
573 ") is out of bounds for total cache size. Skipping update for this token.");
631 scores[
t_hist] = -std::numeric_limits<float>::infinity();
636 for (
int d = 0; d <
head_dim; ++d) {
647 if (scores[
t_hist] == -std::numeric_limits<float>::infinity() || scores[
t_hist] == 0.0f)
continue;
654 for (
int d = 0; d <
head_dim; ++d) {
static void warning(const std::string &message)
static void info(const std::string &message)
static void error(const std::string &message)
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)
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)
std::vector< std::vector< int > > attention_visible_rows(int num_tokens_in_batch, int start_pos_in_sequence, int window, int sinks)
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)
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)
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_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)
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 swa_visible_ranges(int pos, int window, int sinks, int &sink_hi, int &win_lo)
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)
float softcap_score(float s, float c)
const float * p[kIrDepthSourcesMax]
Logging utilities for the TinyLlama implementation.
void parallel_for(int64_t n, F &&fn)
Key-Value cache for a single transformer layer.
Complete Key-Value cache for all transformer layers.
void softmax_vector_cpu(const std::vector< float > &x, std::vector< float > &out)