| 6065 | } |
| 6066 | |
| 6067 | struct ggml_cgraph * build_stablelm() { |
| 6068 | struct ggml_cgraph * gf = ggml_new_graph(ctx0); |
| 6069 | |
| 6070 | struct ggml_tensor * cur; |
| 6071 | struct ggml_tensor * inpL; |
| 6072 | |
| 6073 | inpL = llm_build_inp_embd(ctx0, hparams, batch, model.tok_embd, cb); |
| 6074 | cb(inpL, "inp_embd", -1); |
| 6075 | |
| 6076 | // inp_pos - contains the positions |
| 6077 | struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens); |
| 6078 | cb(inp_pos, "inp_pos", -1); |
| 6079 | |
| 6080 | // KQ_scale |
| 6081 | struct ggml_tensor * KQ_scale = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1); |
| 6082 | cb(KQ_scale, "KQ_scale", -1); |
| 6083 | |
| 6084 | // KQ_mask (mask for 1 head, it will be broadcasted to all heads) |
| 6085 | struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_kv, n_tokens, 1); |
| 6086 | cb(KQ_mask, "KQ_mask", -1); |
| 6087 | |
| 6088 | // shift the entire K-cache if needed |
| 6089 | if (do_rope_shift) { |
| 6090 | llm_build_k_shift(ctx0, hparams, cparams, kv_self, gf, LLM_ROPE_NEOX, n_ctx, hparams.n_rot, freq_base, freq_scale, cb); |
| 6091 | } |
| 6092 | |
| 6093 | for (int il = 0; il < n_layer; ++il) { |
| 6094 | struct ggml_tensor * inpSA = inpL; |
| 6095 | |
| 6096 | // norm |
| 6097 | cur = llm_build_norm(ctx0, inpL, hparams, |
| 6098 | model.layers[il].attn_norm, |
| 6099 | model.layers[il].attn_norm_b, |
| 6100 | LLM_NORM, cb, il); |
| 6101 | cb(cur, "attn_norm", il); |
| 6102 | |
| 6103 | // self-attention |
| 6104 | { |
| 6105 | // compute Q and K and RoPE them |
| 6106 | struct ggml_tensor * tmpq = ggml_mul_mat(ctx0, model.layers[il].wq, cur); |
| 6107 | cb(tmpq, "tmpq", il); |
| 6108 | |
| 6109 | struct ggml_tensor * tmpk = ggml_mul_mat(ctx0, model.layers[il].wk, cur); |
| 6110 | cb(tmpk, "tmpk", il); |
| 6111 | |
| 6112 | struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur); |
| 6113 | cb(Vcur, "Vcur", il); |
| 6114 | |
| 6115 | // RoPE the first n_rot of q/k, pass the other half, and concat. |
| 6116 | struct ggml_tensor * qrot = ggml_cont(ctx0, ggml_view_3d( |
| 6117 | ctx0, tmpq, hparams.n_rot, n_head, n_tokens, |
| 6118 | ggml_element_size(tmpq) * n_embd_head, |
| 6119 | ggml_element_size(tmpq) * n_embd_head * n_head, |
| 6120 | 0 |
| 6121 | )); |
| 6122 | cb(qrot, "qrot", il); |
| 6123 | |
| 6124 | struct ggml_tensor * krot = ggml_cont(ctx0, ggml_view_3d( |
no test coverage detected