KV cache
×state · kept between calls
The rebuilt keys and the values of every head and position so far, kept between forward calls.
- shape
- k [B batch, 128 heads, 1001 cached positions, 192 content + rotary] v [B batch, 128 heads, 1001 cached positions, 128 value]
This call rebuilds the new token’s keys and values and hands them to the cache, which returns all 1,001 positions for the scores. It holds 128 heads of 192 key and 128 value features per position, not the 512-feature latent they are rebuilt from: 40,960 values per position against 576. It has no fixed size; the slider stops at the configuration’s 163,840 positions (max_position_embeddings), which the code does not enforce.
Sources
In codepast_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
Contextual evidence
With a cache, the cached length is added to this call’s, the rebuilt and rotated keys and the values are handed to the cache object, which returns every cached key and value so far, and the scores cover all of them.
modeling_deepseek.py 836–870 ↗ · deepseek-ai/DeepSeek-V2 @ 4461458f186c
Excerpt
kv_seq_len = value_states.shape[-2]
if past_key_value is not None:
if self.layer_idx is None:
raise ValueError(
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
"with a layer index."
)
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)… lines 846–855 omitted …
if past_key_value is not None:
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
key_states, value_states = past_key_value.update(
key_states, value_states, self.layer_idx, cache_kwargs
)
attn_weights = (
torch.matmul(query_states, key_states.transpose(2, 3)) * self.softmax_scale
)
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
raise ValueError(
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
f" {attn_weights.size()}"
)The joint down-projection yields the key–value latent and one rotary key shared by all heads; the latent is normalized and up-projected into per-head content keys and values.
modeling_deepseek.py 822–835 ↗ · deepseek-ai/DeepSeek-V2 @ 4461458f186c
Excerpt
compressed_kv = self.kv_a_proj_with_mqa(hidden_states)
compressed_kv, k_pe = torch.split(
compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
)
k_pe = k_pe.view(bsz, q_len, 1, self.qk_rope_head_dim).transpose(1, 2)
kv = (
self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
.view(bsz, q_len, self.num_heads, self.qk_nope_head_dim + self.v_head_dim)
.transpose(1, 2)
)
k_nope, value_states = torch.split(
kv, [self.qk_nope_head_dim, self.v_head_dim], dim=-1
)The released configuration: width, layers, one dense layer, heads, routed and shared experts, routing (softmax scores, group-limited greedy selection of the best 3 of 8 expert groups, top-6 of 160, weights not renormalized and scaled by 16), MLA ranks and head widths, vocabulary, YaRN RoPE, bfloat16.
config.json 1–59 ↗ · deepseek-ai/DeepSeek-V2 @ 4461458f186c
Excerpt
{
"architectures": [
"DeepseekV2ForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"auto_map": {
"AutoConfig": "configuration_deepseek.DeepseekV2Config",
"AutoModel": "modeling_deepseek.DeepseekV2Model",
"AutoModelForCausalLM": "modeling_deepseek.DeepseekV2ForCausalLM"
},
"aux_loss_alpha": 0.001,
"bos_token_id": 100000,
"eos_token_id": 100001,
"first_k_dense_replace": 1,
"hidden_act": "silu",
"hidden_size": 5120,
"initializer_range": 0.02,
"intermediate_size": 12288,
"kv_lora_rank": 512,
"max_position_embeddings": 163840,
"model_type": "deepseek_v2",
"moe_intermediate_size": 1536,
"moe_layer_freq": 1,
"n_group": 8,
"n_routed_experts": 160,
"n_shared_experts": 2,
"norm_topk_prob": false,
"num_attention_heads": 128,
"num_experts_per_tok": 6,
"num_hidden_layers": 60,
"num_key_value_heads": 128,
"pretraining_tp": 1,
"q_lora_rank": 1536,
"qk_nope_head_dim": 128,
"qk_rope_head_dim": 64,
"rms_norm_eps": 1e-06,
"rope_scaling": {
"beta_fast": 32,
"beta_slow": 1,
"factor": 40,
"mscale": 0.707,
"mscale_all_dim": 0.707,
"original_max_position_embeddings": 4096,
"type": "yarn"
},
"rope_theta": 10000,
"routed_scaling_factor": 16.0,
"scoring_func": "softmax",
"seq_aux": true,
"tie_word_embeddings": false,
"topk_group": 3,
"topk_method": "group_limited_greedy",
"torch_dtype": "bfloat16",
"transformers_version": "4.39.3",
"use_cache": true,
"v_head_dim": 128,
"vocab_size": 102400
}