diff --git a/src/llama-hparams.cpp b/src/llama-hparams.cpp index 14b0047c..33721a55 100644 --- a/src/llama-hparams.cpp +++ b/src/llama-hparams.cpp @@ -1891,7 +1891,50 @@ void llm_load_hparams( case LLM_ARCH_DEEPSEEK4: case LLM_ARCH_GLM_DSA: { - const bool is_dsv4 = model.arch == LLM_ARCH_DFLASH || model.arch == LLM_ARCH_DEEPSEEK4; + if (model.arch == LLM_ARCH_DFLASH) { + const bool has_dense_signature = + ml.get_tensor_meta("blk.0.attn_q.weight") != nullptr; + const bool has_dsv4_signature = + ml.get_tensor_meta("blk.0.attn_q_a.weight") != nullptr || + ml.get_tensor_meta("blk.0.hc_attn_base.weight") != nullptr; + if (has_dense_signature && has_dsv4_signature) { + throw std::runtime_error("dflash: ambiguous dense and DSV4 tensor signatures"); + } + if (!has_dense_signature && !has_dsv4_signature) { + throw std::runtime_error("dflash: unrecognized or incomplete tensor signature"); + } + hparams.dflash_dsv4 = has_dsv4_signature; + } + const bool dflash_dense = model.arch == LLM_ARCH_DFLASH && !hparams.dflash_dsv4; + if (dflash_dense) { + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); + ml.get_key("dflash.block_size", hparams.dflash_block_size); + ml.get_key(LLM_KV_TOKENIZER_MASK_ID, hparams.dflash_mask_token_id); + ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); + ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.swa_layers, hparams.n_layer, false); + load_dflash_target_layer_ids(ml, "dflash.target_layers", hparams, true); + + const ggml_tensor * fc = ml.get_tensor_meta("fc.weight"); + if (fc == nullptr || fc->ne[0] <= 0 || fc->ne[1] != hparams.n_embd) { + throw std::runtime_error("dflash: fc.weight must have shape [n_target_features, embedding_length]"); + } + hparams.dflash_n_target_features = (uint32_t) fc->ne[0]; + hparams.dflash_backbone_rotary_base = hparams.rope_freq_base_train; + hparams.dflash_laguna = false; + + for (uint32_t i = 0; i < hparams.dflash_n_target_layers; ++i) { + if (hparams.dflash_target_layer_ids[i] == 0) { + throw std::runtime_error("dflash: target_layers must use one-based IDs"); + } + --hparams.dflash_target_layer_ids[i]; + } + validate_dflash_hparams(hparams, model.arch); + + hparams.n_layer_kv_from_start = hparams.n_layer; + model.type = e_model::MODEL_UNKNOWN; + break; + } + const bool is_dsv4 = model.arch == LLM_ARCH_DEEPSEEK4 || hparams.dflash_dsv4; ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); if (model.arch == LLM_ARCH_DEEPSEEK4 && hparams.n_layer == 43 && hparams.nextn_predict_layers > 0) { LLAMA_LOG_WARN("===============================================================================================\n"); diff --git a/src/llama-load-tensors.cpp b/src/llama-load-tensors.cpp index 3a613e4f..447411bf 100644 --- a/src/llama-load-tensors.cpp +++ b/src/llama-load-tensors.cpp @@ -5561,7 +5561,9 @@ bool create_tensors_helper::create_tensors() { case LLM_ARCH_GEMMA4_ASSISTANT: use_mmap_buffer = create_gemma4_mtp_tensors(tn); break; case LLM_ARCH_DFLASH: - use_mmap_buffer = create_dflash_dsv4_tensors(tn); break; + use_mmap_buffer = model.hparams.dflash_dsv4 + ? create_dflash_dsv4_tensors(tn) + : create_dflash_tensors(tn); break; case LLM_ARCH_DFLASH2: use_mmap_buffer = create_dflash2_tensors(tn); break; case LLM_ARCH_DFLASH_DRAFT: diff --git a/src/llama-model-loader.h b/src/llama-model-loader.h index 901f78d8..f165470f 100644 --- a/src/llama-model-loader.h +++ b/src/llama-model-loader.h @@ -140,7 +140,8 @@ struct llama_model_loader { enum llm_arch get_arch() const { if (!arch_resolved) { resolved_arch = llm_kv.arch; - if (resolved_arch == LLM_ARCH_DFLASH && get_tensor_meta("selector_hidden.weight") != nullptr) { + if (resolved_arch == LLM_ARCH_DFLASH && + get_tensor_meta("selector_hidden.weight") != nullptr) { resolved_arch = LLM_ARCH_DFLASH2; } arch_resolved = true; diff --git a/src/llama-model.cpp b/src/llama-model.cpp index ace09ab3..8c43902f 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -964,6 +964,12 @@ static const std::map> LLM_TENSOR_NA { LLM_TENSOR_OUTPUT_NORM, "output_norm" }, { LLM_TENSOR_OUTPUT, "output" }, { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" }, + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" }, + { LLM_TENSOR_ATTN_Q_NORM, "blk.%d.attn_q_norm" }, + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" }, + { LLM_TENSOR_ATTN_K_NORM, "blk.%d.attn_k_norm" }, + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" }, + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" }, { LLM_TENSOR_ATTN_SINKS, "blk.%d.attn_sinks" }, { LLM_TENSOR_ATTN_Q_A_NORM, "blk.%d.attn_q_a_norm" }, { LLM_TENSOR_ATTN_KV_A_NORM, "blk.%d.attn_kv_a_norm" }, @@ -973,6 +979,7 @@ static const std::map> LLM_TENSOR_NA { LLM_TENSOR_ATTN_OUT_A, "blk.%d.attn_output_a" }, { LLM_TENSOR_ATTN_OUT_B, "blk.%d.attn_output_b" }, { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" }, + { LLM_TENSOR_ATTN_POST_NORM, "blk.%d.ffn_norm" }, { LLM_TENSOR_FFN_GATE_INP, "blk.%d.ffn_gate_inp" }, { LLM_TENSOR_FFN_GATE_EXPS, "blk.%d.ffn_gate_exps" }, { LLM_TENSOR_FFN_DOWN_EXPS, "blk.%d.ffn_down_exps" }, @@ -990,6 +997,9 @@ static const std::map> LLM_TENSOR_NA { LLM_TENSOR_HC_FFN_BASE, "blk.%d.hc_ffn_base" }, { LLM_TENSOR_HC_FFN_FN, "blk.%d.hc_ffn_fn" }, { LLM_TENSOR_HC_FFN_SCALE, "blk.%d.hc_ffn_scale" }, + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" }, + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" }, + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" }, { LLM_TENSOR_DFLASH_FC, "fc" }, { LLM_TENSOR_DFLASH_HIDDEN_NORM, "enc.output_norm" }, { LLM_TENSOR_DSPARK_MARKOV_W1, "markov_w1" }, diff --git a/src/llama-quantize.cpp b/src/llama-quantize.cpp index a2b9ebfd..917fb603 100644 --- a/src/llama-quantize.cpp +++ b/src/llama-quantize.cpp @@ -641,7 +641,8 @@ static ggml_type llama_tensor_get_type(quantize_state_internal & qs, ggml_type n new_type = GGML_TYPE_IQ6_K; } else if (qs.model.hparams.n_gqa() >= 4 && - !(arch == LLM_ARCH_DFLASH_DRAFT && + !((arch == LLM_ARCH_DFLASH_DRAFT || + (arch == LLM_ARCH_DFLASH && !qs.model.hparams.dflash_dsv4)) && (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M))) { if (new_type == GGML_TYPE_Q2_K || new_type == GGML_TYPE_IQ3_XXS) new_type = GGML_TYPE_IQ3_S; else if (new_type == GGML_TYPE_Q2_K_R4 || new_type == GGML_TYPE_IQ3_XXS_R4) new_type = GGML_TYPE_IQ3_K_R4;