/src/llama.cpp/src/models/jais.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_jais::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
5 | 0 | ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false); |
6 | |
|
7 | 0 | switch (hparams.n_layer()) { |
8 | 0 | case 24: type = LLM_TYPE_1_3B; break; |
9 | 0 | case 40: type = LLM_TYPE_13B; break; |
10 | | /* TODO: add variants */ |
11 | 0 | default: type = LLM_TYPE_UNKNOWN; |
12 | 0 | } |
13 | 0 | } |
14 | | |
15 | 0 | void llama_model_jais::load_arch_tensors(llama_model_loader &) { |
16 | 0 | LLAMA_LOAD_LOCALS; |
17 | |
|
18 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
19 | | |
20 | | // output |
21 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
22 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
23 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
24 | |
|
25 | 0 | for (int i = 0; i < n_layer; ++i) { |
26 | 0 | auto & layer = layers[i]; |
27 | |
|
28 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
29 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
30 | |
|
31 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
32 | 0 | layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
33 | |
|
34 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
35 | 0 | layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
36 | |
|
37 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
38 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
39 | |
|
40 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
41 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
42 | |
|
43 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
44 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0); |
45 | |
|
46 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
47 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | std::unique_ptr<llm_graph_context> llama_model_jais::build_arch_graph(const llm_graph_params & params) const { |
52 | 0 | return std::make_unique<graph>(*this, params); |
53 | 0 | } |
54 | | |
55 | 0 | llama_model_jais::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
56 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
57 | |
|
58 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
59 | |
|
60 | 0 | ggml_tensor * cur; |
61 | 0 | ggml_tensor * inpL; |
62 | |
|
63 | 0 | inpL = build_inp_embd(model.tok_embd); |
64 | |
|
65 | 0 | auto * inp_attn = build_attn_inp_kv(); |
66 | |
|
67 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
68 | |
|
69 | 0 | for (int il = 0; il < n_layer; ++il) { |
70 | 0 | cur = build_norm(inpL, |
71 | 0 | model.layers[il].attn_norm, |
72 | 0 | model.layers[il].attn_norm_b, |
73 | 0 | LLM_NORM, il); |
74 | 0 | cb(cur, "attn_norm", il); |
75 | | |
76 | | // self-attention |
77 | 0 | { |
78 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
79 | 0 | n_embd_head, n_head, n_head_kv, il); |
80 | |
|
81 | 0 | cur = build_attn(inp_attn, |
82 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
83 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il); |
84 | 0 | } |
85 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
86 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
87 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
88 | 0 | } |
89 | | // add the input |
90 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
91 | 0 | cb(ffn_inp, "ffn_inp", il); |
92 | | |
93 | | // FF |
94 | 0 | { |
95 | 0 | cur = build_norm(ffn_inp, |
96 | 0 | model.layers[il].ffn_norm, |
97 | 0 | model.layers[il].ffn_norm_b, |
98 | 0 | LLM_NORM, il); |
99 | 0 | cb(cur, "ffn_norm", il); |
100 | |
|
101 | 0 | cur = build_ffn(cur, |
102 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
103 | 0 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL, |
104 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
105 | 0 | NULL, |
106 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
107 | 0 | cb(cur, "ffn_out", il); |
108 | 0 | } |
109 | |
|
110 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
111 | |
|
112 | 0 | cur = build_cvec(cur, il); |
113 | 0 | cb(cur, "l_out", il); |
114 | | |
115 | | // input for next layer |
116 | 0 | inpL = cur; |
117 | 0 | } |
118 | 0 | cur = build_norm(inpL, |
119 | 0 | model.output_norm, |
120 | 0 | model.output_norm_b, |
121 | 0 | LLM_NORM, -1); |
122 | |
|
123 | 0 | cb(cur, "result_norm", -1); |
124 | 0 | res->t_embd = cur; |
125 | |
|
126 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
127 | |
|
128 | 0 | cb(cur, "result_output", -1); |
129 | 0 | res->t_logits = cur; |
130 | |
|
131 | 0 | ggml_build_forward_expand(gf, cur); |
132 | 0 | } |