From b867b67e7e47efbf2952887440b5b0d641060740 Mon Sep 17 00:00:00 2001 From: Concedo <39025047+LostRuins@users.noreply.github.com> Date: Fri, 5 Dec 2025 16:43:37 +0800 Subject: [PATCH] added mechanics for a full clear if fast forward is not used, this should help recover from bad states --- gpttype_adapter.cpp | 13 ++++++++++--- model_adapter.cpp | 9 ++++++++- model_adapter.h | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/gpttype_adapter.cpp b/gpttype_adapter.cpp index b662b6148..010674b62 100644 --- a/gpttype_adapter.cpp +++ b/gpttype_adapter.cpp @@ -3782,7 +3782,7 @@ generation_outputs gpttype_generate(const generation_inputs inputs) { if(kcpp_data->use_fastforward) { - ContextFastForward(current_context_tokens, embd_inp, n_past, last_n_tokens, nctx, smartcontext, false, true); + ContextFastForward(current_context_tokens, embd_inp, n_past, last_n_tokens, nctx, smartcontext, false, true, 0); } } if(is_recurrent) @@ -3830,12 +3830,19 @@ generation_outputs gpttype_generate(const generation_inputs inputs) } if(kcpp_data->use_fastforward) { - ContextFastForward(current_context_tokens, embd_inp, n_past, last_n_tokens, nctx, smartcontext, triggersc, false); + ContextFastForward(current_context_tokens, embd_inp, n_past, last_n_tokens, nctx, smartcontext, triggersc, false, 4); } } if(file_format == FileFormat::GGUF_GENERIC) { - llama_memory_seq_rm(llama_get_memory(llama_ctx_v4), 0, n_past, -1); + if(n_past==0) //force full clear + { + llama_memory_clear(llama_get_memory(llama_ctx_v4),true); + } + else + { + llama_memory_seq_rm(llama_get_memory(llama_ctx_v4), 0, n_past, -1); + } if(draft_ctx) { llama_memory_seq_rm(llama_get_memory(draft_ctx), 0, n_past, -1); diff --git a/model_adapter.cpp b/model_adapter.cpp index 61382cbaf..8a0260f8e 100644 --- a/model_adapter.cpp +++ b/model_adapter.cpp @@ -513,7 +513,7 @@ std::string gguf_get_model_arch(const std::string & gguf_filename) void ContextFastForward(std::vector ¤t_context_tokens, std::vector &embd_inp, int &n_past, std::vector &last_n_tokens, const int nctx, std::vector &smartcontext, - bool useSmartContext, const bool requireFullSubset) + bool useSmartContext, const bool requireFullSubset, const int minimum_to_proceed) { const int SCCtxLenThreshold = nctx * 0.8; //how much context length must be reach to trigger smartcontext const int SCInpLenThreshold = nctx * 0.6; //how big must the input array be to trigger smartcontext @@ -568,6 +568,13 @@ std::string gguf_get_model_arch(const std::string & gguf_filename) } } + if(n_past < minimum_to_proceed) //too few tokens to fast forward, so lets start fresh + { + last_n_tokens.erase(last_n_tokens.end() - n_past, last_n_tokens.end()); + n_past = 0; + fastforwardok = false; + } + if(fastforwardok) { last_n_tokens.erase(last_n_tokens.begin(), last_n_tokens.begin() + n_past); diff --git a/model_adapter.h b/model_adapter.h index bd3c9b81c..1f639e5a4 100644 --- a/model_adapter.h +++ b/model_adapter.h @@ -129,7 +129,7 @@ int ArrFindIndexOf(const std::vector targetArray, const std::vector se FileFormat check_file_format(const std::string & fname, FileFormatExtraMeta * fileformatmeta); void ContextFastForward(std::vector ¤t_context_tokens, std::vector &embd_inp, int &n_past, std::vector &last_n_tokens, const int nctx, std::vector &smartcontext, - const bool useSmartContext, const bool requireFullSubset); + const bool useSmartContext, const bool requireFullSubset, const int minimum_to_proceed); bool gguf_tensor_exists(const std::string & filename, std::string tensor_name, bool exactmatch); std::string gguf_get_model_arch(const std::string & filename);