/src/llama.cpp/src/models/neo-bert.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_neo_bert::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | |
|
6 | 0 | if (hparams.n_layer() == 28) { |
7 | 0 | type = LLM_TYPE_250M; |
8 | 0 | } |
9 | 0 | } |
10 | | |
11 | 0 | void llama_model_neo_bert::load_arch_tensors(llama_model_loader &) { |
12 | 0 | LLAMA_LOAD_LOCALS; |
13 | |
|
14 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
15 | |
|
16 | 0 | cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED); |
17 | 0 | cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
18 | |
|
19 | 0 | cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
20 | 0 | cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
21 | |
|
22 | 0 | output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0); |
23 | |
|
24 | 0 | for (int i = 0; i < n_layer; ++i) { |
25 | 0 | auto & layer = layers[i]; |
26 | |
|
27 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
28 | |
|
29 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
30 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
31 | |
|
32 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
33 | |
|
34 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0); |
35 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | std::unique_ptr<llm_graph_context> llama_model_neo_bert::build_arch_graph(const llm_graph_params & params) const { |
40 | 0 | return std::make_unique<graph>(*this, params); |
41 | 0 | } |
42 | | |
43 | 0 | llama_model_neo_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
44 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
45 | |
|
46 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
47 | |
|
48 | 0 | ggml_tensor * cur; |
49 | 0 | ggml_tensor * inpL; |
50 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
51 | | |
52 | | // construct input embeddings (token, type, position) |
53 | 0 | inpL = build_inp_embd(model.tok_embd); |
54 | 0 | cb(inpL, "inp_embd", -1); |
55 | |
|
56 | 0 | auto * inp_attn = build_attn_inp_no_cache(); |
57 | |
|
58 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
59 | |
|
60 | 0 | for (int il = 0; il < n_layer; ++il) { |
61 | 0 | ggml_tensor * cur = inpL; |
62 | | |
63 | | // pre-norm |
64 | 0 | cur = build_norm(inpL, |
65 | 0 | model.layers[il].attn_norm, NULL, |
66 | 0 | LLM_NORM_RMS, il); |
67 | |
|
68 | 0 | { |
69 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
70 | 0 | n_embd_head, n_head, n_head_kv, il); |
71 | | |
72 | | // RoPE |
73 | 0 | Qcur = ggml_rope_ext( |
74 | 0 | ctx0, Qcur, inp_pos, nullptr, |
75 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
76 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
77 | 0 | ); |
78 | |
|
79 | 0 | Kcur = ggml_rope_ext( |
80 | 0 | ctx0, Kcur, inp_pos, nullptr, |
81 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
82 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
83 | 0 | ); |
84 | |
|
85 | 0 | cb(Qcur, "Qcur", il); |
86 | 0 | cb(Kcur, "Kcur", il); |
87 | 0 | cb(Vcur, "Vcur", il); |
88 | |
|
89 | 0 | cur = build_attn(inp_attn, |
90 | 0 | model.layers[il].wo, nullptr, model.layers[il].wo_s, |
91 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
92 | 0 | cb(cur, "kqv_out", il); |
93 | 0 | } |
94 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
95 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
96 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
97 | 0 | } |
98 | | // re-add the layer input |
99 | 0 | cur = ggml_add(ctx0, cur, inpL); |
100 | |
|
101 | 0 | ggml_tensor * ffn_inp = cur; |
102 | 0 | cb(ffn_inp, "ffn_inp", il); |
103 | | |
104 | | // pre-norm |
105 | 0 | cur = build_norm(ffn_inp, |
106 | 0 | model.layers[il].ffn_norm, NULL, |
107 | 0 | LLM_NORM_RMS, il); |
108 | 0 | cb(cur, "ffn_norm", il); |
109 | | |
110 | | // feed-forward network |
111 | 0 | cur = build_ffn(cur, |
112 | 0 | model.layers[il].ffn_up, |
113 | 0 | NULL, NULL, NULL, NULL, NULL, |
114 | 0 | model.layers[il].ffn_down, |
115 | 0 | NULL, NULL, NULL, |
116 | 0 | LLM_FFN_SWIGLU, LLM_FFN_SEQ, il); |
117 | | |
118 | | // attentions bypass the intermediate layer |
119 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
120 | | |
121 | | // input for next layer |
122 | 0 | inpL = cur; |
123 | 0 | } |
124 | 0 | cur = inpL; |
125 | |
|
126 | 0 | cur = build_norm(cur, |
127 | 0 | model.output_norm_enc, NULL, |
128 | 0 | LLM_NORM_RMS, -1); |
129 | |
|
130 | 0 | cb(cur, "result_embd", -1); |
131 | 0 | res->t_embd = cur; |
132 | |
|
133 | 0 | ggml_build_forward_expand(gf, cur); |
134 | 0 | } |