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