/src/llama.cpp/src/models/paddleocr.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | std::unique_ptr<llm_graph_context> llama_model_paddleocr::build_arch_graph(const llm_graph_params & params) const { |
4 | 0 | return std::make_unique<graph>(*this, params); |
5 | 0 | } |
6 | | |
7 | | llama_model_paddleocr::graph::graph(const llama_model & model, const llm_graph_params & params) : |
8 | 0 | llm_graph_context(params) { |
9 | | |
10 | | // NOTE: same with qwen2vl.cpp, but bias tensors are optional |
11 | |
|
12 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
13 | |
|
14 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
15 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
16 | |
|
17 | 0 | ggml_tensor * cur; |
18 | 0 | ggml_tensor * inpL; |
19 | |
|
20 | 0 | inpL = build_inp_embd(model.tok_embd); |
21 | |
|
22 | 0 | int sections[4]; |
23 | 0 | std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
24 | | |
25 | | // inp_pos - contains the positions |
26 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
27 | |
|
28 | 0 | auto * inp_attn = build_attn_inp_kv(); |
29 | |
|
30 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
31 | |
|
32 | 0 | for (int il = 0; il < n_layer; ++il) { |
33 | 0 | ggml_tensor * inpSA = inpL; |
34 | | |
35 | | // norm |
36 | 0 | { |
37 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
38 | 0 | cb(cur, "attn_norm", il); |
39 | 0 | } |
40 | | // self-attention |
41 | 0 | { |
42 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
43 | 0 | n_embd_head, n_head, n_head_kv, il); |
44 | |
|
45 | 0 | Qcur = ggml_rope_multi( |
46 | 0 | ctx0, Qcur, inp_pos, nullptr, |
47 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
48 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
49 | 0 | ); |
50 | |
|
51 | 0 | Kcur = ggml_rope_multi( |
52 | 0 | ctx0, Kcur, inp_pos, nullptr, |
53 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
54 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
55 | 0 | ); |
56 | |
|
57 | 0 | cb(Qcur, "Qcur", il); |
58 | 0 | cb(Kcur, "Kcur", il); |
59 | 0 | cb(Vcur, "Vcur", il); |
60 | |
|
61 | 0 | cur = build_attn(inp_attn, |
62 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
63 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
64 | 0 | } |
65 | 0 | if (il == n_layer - 1) { |
66 | | // skip computing output for unused tokens |
67 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
68 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
69 | 0 | } |
70 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
71 | 0 | cb(ffn_inp, "ffn_inp", il); |
72 | | |
73 | | // feed-forward network |
74 | 0 | { |
75 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
76 | 0 | cb(cur, "ffn_norm", il); |
77 | |
|
78 | 0 | cur = build_ffn(cur, |
79 | 0 | model.layers[il].ffn_up, NULL, NULL, |
80 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
81 | 0 | model.layers[il].ffn_down, NULL, NULL, |
82 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
83 | 0 | cb(cur, "ffn_out", il); |
84 | 0 | } |
85 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
86 | |
|
87 | 0 | cur = build_cvec(cur, il); |
88 | 0 | cb(cur, "l_out", il); |
89 | | |
90 | | // input for next layer |
91 | 0 | inpL = cur; |
92 | 0 | } |
93 | 0 | cur = inpL; |
94 | |
|
95 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
96 | |
|
97 | 0 | cb(cur, "result_norm", -1); |
98 | 0 | res->t_embd = cur; |
99 | | |
100 | | // lm_head |
101 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
102 | |
|
103 | 0 | cb(cur, "result_output", -1); |
104 | 0 | res->t_logits = cur; |
105 | |
|
106 | 0 | ggml_build_forward_expand(gf, cur); |
107 | 0 | } |