From 724ebba103cbcd182140a8c427e481ec7def5df0 Mon Sep 17 00:00:00 2001 From: SamuelOliveirads Date: Mon, 13 Jul 2026 22:28:55 -0300 Subject: [PATCH] dsv4: document dependency ordering and reset state --- src/graphs/build_deepseek4.cpp | 9 ++++----- src/llama-dsv4.cpp | 2 +- src/llama-load-tensors.cpp | 2 +- src/llama.cpp | 1 + 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/graphs/build_deepseek4.cpp b/src/graphs/build_deepseek4.cpp index a5c8d975a..2b5eda9e9 100644 --- a/src/graphs/build_deepseek4.cpp +++ b/src/graphs/build_deepseek4.cpp @@ -103,6 +103,10 @@ static ggml_tensor * dsv4_append_zero_row(ggml_context * ctx, ggml_tensor * t, b } static ggml_tensor * dsv4_with_zero_dep(ggml_context * ctx, ggml_tensor * t, ggml_tensor * dep) { + // Consumers of t must depend on dep so compression/current-cache work completes + // before state persistence or dependent cache reads, graph expansion order alone + // is not a scheduler data dependency. Keep the typed scalar/add1 form because + // the F16 raw-K path requires it. if (dep == nullptr) { return t; } @@ -557,7 +561,6 @@ static ggml_tensor * build_hc_pre( ggml_tensor ** post, ggml_tensor ** comb) { const int64_t hc = hparams.dsv4_hc_mult; - const int64_t hc_mix_dim = (2 + hc)*hc; const int64_t nt = x->ne[2]; ggml_tensor * mixes = llm.build_mhc_pre_projection(x, hc_fn, nullptr, @@ -600,7 +603,6 @@ static ggml_tensor * build_hc_head( ggml_tensor * hc_scale, ggml_tensor * hc_base) { const int64_t hc = hparams.dsv4_hc_mult; - const int64_t nt = x->ne[2]; ggml_tensor * mixes = llm.build_mhc_pre_projection(x, hc_fn, nullptr, n_embd, hc, norm_rms_eps, false); @@ -1167,14 +1169,12 @@ ggml_cgraph * llm_build_context::build_deepseek4() { raw_mask = dsv4_pad_raw_mask_to(ctx0, raw_mask, raw_attn_n_kv, n_tokens); cb(raw_mask, "dsv4_raw_mask_padded", il); ggml_tensor * attn = nullptr; - const char * attn_path = "raw"; if (ratio == llama_context::dsv4_runtime::CSA_RATIO && lctx.dsv4.inputs.csa.kq_mask != nullptr && lctx.dsv4.csa_plan.n_kv > 0 && lctx.dsv4.lid_plan.n_kv > 0 && !cparams.k_cache_hadamard) { - attn_path = "csa"; ggml_tensor * csa_k = dsv4_comp_get_k(ctx0, lctx.dsv4.cache.csa_k[(size_t) il], lctx.dsv4.csa_ctx, @@ -1215,7 +1215,6 @@ ggml_cgraph * llm_build_context::build_deepseek4() { std::any_of(lctx.dsv4.hca_plan.n_visible.begin(), lctx.dsv4.hca_plan.n_visible.end(), [](int32_t n_visible) { return n_visible > 0; }) && !cparams.k_cache_hadamard) { - attn_path = "hca"; ggml_tensor * hca_k = dsv4_comp_get_k(ctx0, lctx.dsv4.cache.hca_k[(size_t) il], lctx.dsv4.hca_ctx, diff --git a/src/llama-dsv4.cpp b/src/llama-dsv4.cpp index c610facc0..befb991be 100644 --- a/src/llama-dsv4.cpp +++ b/src/llama-dsv4.cpp @@ -325,7 +325,7 @@ static bool dsv4_build_raw_context( } const llama_kv_cache & kv = lctx.kv_self; - if (kv.head < 0 || kv.head + batch.n_tokens > (int32_t) kv.size) { + if (kv.head + batch.n_tokens > (int32_t) kv.size) { LLAMA_LOG_ERROR("%s: DSV4 raw write slots [%d, %d) are outside kv cache size %u\n", __func__, kv.head, kv.head + batch.n_tokens, kv.size); return false; diff --git a/src/llama-load-tensors.cpp b/src/llama-load-tensors.cpp index 6b9da0240..6dd0500f5 100644 --- a/src/llama-load-tensors.cpp +++ b/src/llama-load-tensors.cpp @@ -2734,7 +2734,7 @@ bool create_tensors_helper::create_deepseek2_tensors(const LLM_TN & tn) { return use_mmap_buffer; } -bool create_tensors_helper::create_deepseek4_tensors(const LLM_TN & tn) { +bool create_tensors_helper::create_deepseek4_tensors(const LLM_TN &) { LOADING_PRELUDE auto create_tensor_from_meta = [&](ggml_context * ctx, const std::string & name, int flags = 0) -> ggml_tensor * { diff --git a/src/llama.cpp b/src/llama.cpp index f7ef6d7a3..3812d1504 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -8713,6 +8713,7 @@ int32_t llama_get_kv_cache_used_cells(const struct llama_context * ctx) { void llama_kv_cache_clear(struct llama_context * ctx) { llama_kv_cache_clear(ctx->kv_self); + llama_reset_dsv4_state(ctx); } // Unified speculative-checkpoint