-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Hybrid recurrent cache #13979
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
Hybrid recurrent cache #13979
Conversation
src/llama-graph.cpp
Outdated
const llama_kv_cache_unified_state * llm_graph_context::get_state_unified() const { | ||
const auto * umstate = dynamic_cast<const llama_kv_cache_unified_state *>(mstate); | ||
if (!umstate) { | ||
const auto hmstate = dynamic_cast<const llama_kv_cache_hybrid_recurrent_state *>(mstate); | ||
if (hmstate) { | ||
umstate = hmstate->get_state_attn(); | ||
} | ||
} | ||
GGML_ASSERT(umstate); | ||
return umstate; | ||
} | ||
|
||
const llama_kv_cache_recurrent_state * llm_graph_context::get_state_recurrent() const { | ||
const auto * rmstate = dynamic_cast<const llama_kv_cache_recurrent_state *>(mstate); | ||
if (!rmstate) { | ||
const auto hmstate = dynamic_cast<const llama_kv_cache_hybrid_recurrent_state *>(mstate); | ||
if (hmstate) { | ||
rmstate = hmstate->get_state_recurrent(); | ||
} | ||
} | ||
GGML_ASSERT(rmstate); | ||
return rmstate; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These dynamic casts should not be necessary. Instead you need a new llm_graph_context::build_attn_inp_kv_hybrid_recurrent()
method, similar to llm_graph_context::build_attn_inp_kv_unified_iswa()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm working through this now and a couple of questions are coming up:
- Would it be best to combine
build_inp_s_copy
withbuild_attn_inp_kv
for hybrid so that models call just one "build inputs" function, or keep them separate for simplicity? - For the
build_attn
methods, each has a correspondingllm_graph_input_attn_*
class. Thebuild_inp_s_*
methods don't have this pattern which would make this a bit harder to have code reuse. Are there plans to refactor that further @compilade? - In the
mamba2
branch,s_mask
seems to be totally removed. I'd prefer not to do all of the boilerplate for duplicatingbuild_inp_s_mask
for the hybrid recurrent case if that's definitely going to be going away. Is there any reason that might stick around past the merge ofmamba2
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answering out of order, but it should still make sense:
2. For the
build_attn
methods, each has a correspondingllm_graph_input_attn_*
class. Thebuild_inp_s_*
methods don't have this pattern
They do follow this pattern, see
Line 190 in 7e00e60
class llm_graph_input_s_copy : public llm_graph_input_i { |
(this is on the current master
)
I think you might mean the build_attn_*
methods also return instances of llm_graph_input_attn_*
?
That seems to be directly related to llm_graph_context::build_attn()
which has multiple implementations which differ by the type of the first argument (e.g. for llm_graph_input_attn_kv_unified
, llm_graph_input_attn_no_cache
, etc.)
Are there plans to refactor that further @compilade?
Not really, outside of removing s_mask
(and related functions and classes) as part of #13834.
- Would it be best to combine
build_inp_s_copy
withbuild_attn_inp_kv
for hybrid so that models call just one "build inputs" function, or keep them separate for simplicity?
Personally, I think it would be simpler to keep them separate, because they are fundamentally different (one is intended to be used by build_copy_mask_state
(renamed to build_recurrent_state
in #13834), while the other is used by build_attn
), and they are pretty much independent, even in hybrid models (at least for Jamba, the recurrent and self-attention layers are mostly independent on that front).
I don't see how build_attn
would ever need s_copy
.
build_inp_s_copy
and build_inp_attn_kv_*
are called once at the beginning of the graph, while build_attn
and build_recurrent_state
are called once per layer (where applicable, and so usually different layers for both).
3. Is there any reason [
s_mask
] might stick around past the merge ofmamba2
?
No reason to keep it, s_mask
will be removed. Its functionality is redundant with s_copy
, and otherwise prevents minimizing unnecessary state copies. It was used to clear the states, but the same can be done through inp_s_copy
and clearing by copying a zero-ed state (which is the rs_z
'th state in the mamba2
branch (and #13834)).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's super helpful! I was missing the distinction between build_attn_inp
and build_attn
which makes perfect sense.
Personally, I think it would be simpler to keep them separate
I agree on my personal gut feeling, so I'll go with this and see how it feels once complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think this feels a lot cleaner now. For build_inp_s_copy
, I opted to add an optional parameter so that the caller can take ownership of casting the cache state rather than duplicating the function into build_inp_s_copy_hybrid
. That felt a little cleaner w.r.t. code reuse, but I'm happy to do a separate method if that's preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there's one more place that will need changing in build_copy_mask_state
(renamed to build_recurrent_state
on mamba2
). Similar to build_inp_s_copy
, I think the cleanest way to do this for code reuse is to add an optional parameter that, if unset, will use the current logic of casting mstate
.
// TODO: will the recurrent cache be in an undefined state at this point? | ||
LLAMA_LOG_ERROR("%s: failed to prepare recurrent ubatches\n", __func__); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but that will be fixed in #13834
(Noting here in case this gets merged first so that I don't forget to update the comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I strip out this TODO at this point?
ab918bb
to
60ca3ba
Compare
@ggerganov I've noticed that the
|
This is an attempt to handle race conditions between /health returning OK and the other endpoints not returning successfully. ggml-org#13979 (comment) Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
I've tried adding retry logic for all requests in 39a93b3 to work around the race between |
The changes to the server tests should not be needed. Let's revert the commit for now and I'll investigate. |
39a93b3
to
60ca3ba
Compare
@ggerganov Thanks, it looks like those changes didn't fix the failures anyway, so definitely not the right fix. I've reset them out and can open an issue with details of what I see locally |
Issue for follow up on |
7958d84
to
3669876
Compare
I've rebased on #13834. Drafting for now until it's merged |
3669876
to
8c59841
Compare
That was quick! Undrafting now that #13834 is merged |
95b6698
to
faf4119
Compare
I pushed some recommendations in gabe-l-hart#2. After merging gabe-l-hart#2 into this branch, we can proceed to merge into However, note that before we proceed with the rest of the changes that build on top of this, such as Mamba 2, Granite and Falcon, we have to fix the naming of the classes and the variables as I have described in the TODOs in gabe-l-hart#2. We also have to make the recurrent cache to not maintain any compute-related state similar to how the state of the unified cache (i.e. The main thing about the naming is that we have to avoid associating the recurrent state/cache with the "KV cache". For example |
I suggest But it mostly represents the number of modified recurrent states (which may be larger than |
Thanks @ggerganov! I'm looking over those changes now and will merge them shortly once my head is fully wrapped around them. I'm definitely open to tackling the rename from |
recurrent : rework graph inputs + add TODOs
Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
@ggerganov @compilade With Georgei's PR, I think the last open question is whether to tackle the rename now. Thoughts / preferences? I think it would touch a bunch more files, but might be nice to get it all done at once. |
If we want to tackle the rename now, I think it would look like:
I'll take a whack at it locally and see how invasive it feels |
In the rename, one thing this would open up is renaming |
…e kv cache This removes the notion of "kv" from the interface names for these memory types. There are still many references to kv in the implementation of the recurrent memory which will need further adjustment. Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
Another one as I'm working through it: in |
Or maybe |
Ah, interesting. I was basing off of how the mamba models use |
Technically The "unified" suffix in |
Ok, understood on both counts. I'm working through the |
Anywhere that "kv_<state|cell|size|etc>" is used, I've used the more generic "mem_" prefix. The specifics of "k" (key) translate to "r" (recurrent state) and "v" (value) translate to "s" (state-space embedding states). Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
Ok, renaming complete! I think it's a lot cleaner now, but would definitely love the extra eyes on anywhere I might have missed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good to merge. Thanks for the patience and catching up with all the changes on master
👍
Thank you! I learned a ton along the way. I'll make the suggested change from |
It just _happens_ to have the same number of letters as _attn! Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
Ok, renames/refactors done. 🤞 on Ci! |
Successfully rebased Granite 4 and verified that things are working as implemented here, including the refactor of the |
Co-authored-by: Georgi Gerganov <[email protected]>
// TODO: move this implementation to llama_memory_recurrent. | ||
// this is analogous to llama_kv_cache_unified::cpy_k / cpy_v | ||
// when moving, avoid passing `ggml_cgraph` - only pass `ggml_context`. would likely need to split the | ||
// implementation in 2 separate methods. the goal is to avoid calling `ggml_build_forward_expand` in | ||
// `llama_memory_recurrent` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the goal is to avoid calling
ggml_build_forward_expand
I'm not sure that would be convenient; build_rs
when used with avoid_copies
pretty much has to have a way to order the writes.
Splitting the function in two would require handling states_extra
at call sites, while for now it's transparently handled within build_rs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. It's ok to pass the ggml_cgraph
as argument in that case.
This is a re-opened version of #13904 after #13746 was merged
Description
This PR introduces the
llama_kv_cache_hybrid_recurrent
cache implementation. It follows the pattern ofllama_kv_cache_unified_iswa
by holding two child cache instances and implementing the interface logic such that it manages both correctly for the appropriate layers.Changes
The main change in this PR is the addition of
llama_kv_cache_hybrid_recurrent
inllama-kv-cache-hybrid-recurrent.*
. In addition to this, the PR does the following:llama_model_is_hybrid_recurrent
public API (akin tollama_model_is_recurrent
)LLM_KV_ATTENTION_LAYER_INDICES
as an hparam to hold the indices of the layers that should use attention (versus recurrent)iswa
, but that mechanism also isn't particularly extensible. It might be more appropriate to have a generic mechanism for indicating the type of caching to use for each layer, but that would start to approach the generic hybrid implementation that I originally attempted which ended up being too abstract (feat: Hybrid unified/recurrent cache #13276).llm_graph_context
that need a specific type of cache to use getters (get_state_unified
/get_state_recurrent
) that will properly handlellama_kv_cache_hybrid_recurrent
n_embd_k_s
/n_embd_v_s
layer-dependent and use layer indices when calling them in the existing cache implementationsllama_kv_cache_recurrent
llama_model::create_memory
to usellm_arch_is_recurrent
andllm_arch_is_hybrid_recurrent
rather than relying on adding models to theswitch
statement which was redundant with the implementation of these functionsedits by @ggerganov below:
Notes so I don't forget