Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cache shifting for Llama models #341

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mistralrs-core/src/models/llama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ impl CausalSelfAttention {
.contiguous()?;
}

let (k, v) =
crate::pipeline::Cache::update_kv_cache(&mut kv_cache[block_idx], k, v, false)?;
let (k, v) = crate::pipeline::Cache::update_kv_cache_cache_shifting(
&mut kv_cache[block_idx],
k,
v,
false,
self.max_seq_len,
)?;

let k = repeat_kv(k, self.num_attention_heads / self.num_key_value_heads)?.contiguous()?;
let v = repeat_kv(v, self.num_attention_heads / self.num_key_value_heads)?.contiguous()?;
Expand Down
37 changes: 36 additions & 1 deletion mistralrs-core/src/pipeline/cache_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,42 @@ impl Cache {
Ok((k, v))
}

/// Update the KV cache and return (k,v,attn_mask)
/// Update the KV cache taking into account token shifting and return (k,v)
pub(crate) fn update_kv_cache_cache_shifting(
cache: &mut Option<(Tensor, Tensor)>,
k: Tensor,
v: Tensor,
slow_cat: bool,
max_len: usize,
) -> Result<(Tensor, Tensor), candle_core::Error> {
let (k, v) = match &*cache {
None => (k, v),
Some((k_cache, v_cache)) => {
let (k, v) = if !slow_cat {
let k = candle_nn::ops::kvconcat(k_cache, &k, 2)?.contiguous()?;
let v = candle_nn::ops::kvconcat(v_cache, &v, 2)?.contiguous()?;
(k, v)
} else {
let k = Tensor::cat(&[k_cache, &k], 2)?.contiguous()?;
let v = Tensor::cat(&[v_cache, &v], 2)?.contiguous()?;
(k, v)
};
let cache_seq_len = k.dims()[2];
if cache_seq_len > max_len {
(
k.narrow(2, cache_seq_len - max_len, max_len)?,
v.narrow(2, cache_seq_len - max_len, max_len)?,
)
} else {
(k, v)
}
}
};
*cache = Some((k.clone(), v.clone()));
Ok((k, v))
}

/// Update the KV cache taking into account sliding window and return (k,v,attn_mask)
pub(crate) fn update_kv_cache_sliding_window(
cache: &mut Option<(Tensor, Tensor)>,
k: Tensor,
Expand Down
9 changes: 7 additions & 2 deletions mistralrs-core/src/xlora_models/llama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ impl CausalSelfAttention {
.contiguous()?;
}

let (k, v) =
crate::pipeline::Cache::update_kv_cache(&mut kv_cache[block_idx], k, v, false)?;
let (k, v) = crate::pipeline::Cache::update_kv_cache_cache_shifting(
&mut kv_cache[block_idx],
k,
v,
false,
self.max_seq_len,
)?;

let k = repeat_kv(k, self.num_attention_heads / self.num_key_value_heads)?.contiguous()?;
let v = repeat_kv(v, self.num_attention_heads / self.num_key_value_heads)?.contiguous()?;
Expand Down
Loading