A very specific and technical question!
It appears you're describing a optimization technique used in kobold.cpp, a C++ implementation of a conversational AI model. The technique is called "Context Shift" or "Cache Shift". I'll try to explain how it works.
Background
In a multi-turn conversation, the model needs to process the entire conversation history to generate a response. As the conversation grows, the input sequence length increases, which can lead to performance issues. To mitigate this, kobold.cpp uses a technique called "token shaving" or "context truncation", where older tokens are removed from the conversation history to make room for new messages.
However, as you mentioned, simply removing tokens from the start of the conversation can cause problems due to causal attention. Causal attention is a mechanism used in transformer-based models, where the attention weights are computed based on the causal relationships between tokens. When tokens are removed from the start of the conversation, the attention weights for all subsequent tokens change, requiring the entire conversation history to be reprocessed.
Context Shift
Context Shift is an optimization technique that allows the model to reuse the KV (key-value) cache for the majority of the conversation history, even when tokens are removed from the start of the conversation.
Here's a high-level overview of how it works:
- Cache preparation: Before processing a new message, the model prepares a cache of KV values for the current conversation history.
- Token removal: When the context limit is reached, kobold.cpp removes a certain number of tokens from the start of the conversation history.
- Cache shifting: Instead of recomputing the KV cache for the entire conversation history, the model shifts the existing KV cache to align with the new conversation history. This is done by "sliding" the KV cache, effectively removing the KV values corresponding to the removed tokens from the start of the conversation.
- New token processing: The model then processes the new message, computing the KV values only for the new tokens. These new KV values are appended to the shifted KV cache.
By reusing the KV cache for the majority of the conversation history and only recomputing the KV values for the new tokens, Context Shift significantly reduces the computational cost of processing new messages.
Logs showing only a small number of tokens processed
When Context Shift is enabled, the logs show only a small number of tokens processed because the model is only computing the KV values for the new tokens, rather than reprocessing the entire conversation history. This is why the number of tokens processed seems to match or be around the amount of tokens in the user's new message.
I hope this explanation helps! Let me know if you have further questions.