/src/llama.cpp/src/models/deepseek2ocr.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_deepseek2ocr::load_arch_hparams(llama_model_loader & ml) { |
4 | | // similar to deepseek2, but without MLA |
5 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
6 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
7 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
8 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
9 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
10 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
11 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
12 | |
|
13 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
14 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX; |
15 | 0 | } |
16 | |
|
17 | 0 | switch (hparams.n_layer()) { |
18 | 0 | case 12: type = LLM_TYPE_3B; break; |
19 | 0 | default: type = LLM_TYPE_UNKNOWN; |
20 | 0 | } |
21 | 0 | } |
22 | | |
23 | 0 | void llama_model_deepseek2ocr::load_arch_tensors(llama_model_loader &) { |
24 | 0 | LLAMA_LOAD_LOCALS; |
25 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
26 | | |
27 | | // similar to deepseek2, but without MLA |
28 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
29 | |
|
30 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
31 | | |
32 | | // output |
33 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
34 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
35 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
36 | 0 | if (!output) { |
37 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
38 | 0 | } |
39 | |
|
40 | 0 | for (int i = 0; i < n_layer; ++i) { |
41 | 0 | auto & layer = layers[i]; |
42 | |
|
43 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
44 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd}, 0); |
45 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd}, 0); |
46 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
47 | | |
48 | | // norm |
49 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
50 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
51 | |
|
52 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
53 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
54 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
55 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
56 | 0 | } else { |
57 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
58 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
59 | |
|
60 | 0 | if (n_expert == 0) { |
61 | 0 | throw std::runtime_error("n_expert must be > 0"); |
62 | 0 | } |
63 | 0 | if (n_expert_used == 0) { |
64 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
65 | 0 | } |
66 | | |
67 | | // MoE branch |
68 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
69 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0); |
70 | | |
71 | | // Shared expert branch |
72 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
73 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
74 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
75 | 0 | } |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | 0 | std::unique_ptr<llm_graph_context> llama_model_deepseek2ocr::build_arch_graph(const llm_graph_params & params) const { |
80 | 0 | return std::make_unique<graph>(*this, params); |
81 | 0 | } |
82 | | |