Skip to content

Commit 2cff104

Browse files
yrougyclaude
andcommitted
llama : align KV cache tensor-split granularity to cache block size
When a quantized KV cache is used with SPLIT_MODE_TENSOR the per-device slice size must be a multiple of the cache type's block size (e.g. 32 for Q4_0). Previously the granularity was derived solely from the weight tensor's block size (1 for F16), which happened to satisfy the constraint only when n_embd_head_k was already a multiple of the KV block size. Make the alignment explicit by taking the lcm of granularity_kv and ggml_blck_size(tensor->type) for cache tensors. For F16/BF16 cache (blck_size=1) the behaviour is unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f0d252b commit 2cff104

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/llama-model.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,17 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
592592

593593
const int64_t granularity_kv = granularity_q / n_gqa;
594594
if (std::regex_match(tensor_name, pattern_kv_weight) ||
595-
std::regex_match(tensor_name, pattern_kv_bias) ||
596-
std::regex_match(tensor_name, pattern_kv_cache)) {
595+
std::regex_match(tensor_name, pattern_kv_bias)) {
597596
GGML_ASSERT(segments.size() == 1);
598597
return {granularity_kv};
599598
}
599+
if (std::regex_match(tensor_name, pattern_kv_cache)) {
600+
GGML_ASSERT(segments.size() == 1);
601+
// For quantized KV cache the split boundary must also align to
602+
// the cache type's block size (e.g. 32 for Q4_0).
603+
const int64_t kv_blck_size = ggml_blck_size(tensor->type);
604+
return {std::lcm(granularity_kv, kv_blck_size)};
605+
}
600606
if (std::regex_match(tensor_name, pattern_qkv_weight) || std::regex_match(tensor_name, pattern_qkv_bias)) {
601607
GGML_ASSERT(segments.size() == 3);
602608
return {granularity_q, granularity_kv, granularity_kv};

0 commit comments

Comments
 (0)