| 1 | #include "models.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | llm_build_nemotron_h::llm_build_nemotron_h(const llama_model & model, const llm_graph_params & params) : |
| 6 | llm_graph_context_mamba(params) { |
| 7 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 8 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 9 | |
| 10 | ggml_tensor * cur; |
| 11 | ggml_tensor * inpL; |
| 12 | |
| 13 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 14 | ggml_build_forward_expand(cgraph: gf, tensor: inpL); |
| 15 | |
| 16 | auto * inp = build_inp_mem_hybrid(); |
| 17 | |
| 18 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 19 | |
| 20 | for (int il = 0; il < n_layer; ++il) { |
| 21 | struct ggml_tensor * inpSA = inpL; |
| 22 | |
| 23 | // norm |
| 24 | cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il); |
| 25 | cb(cur, name: "attn_norm" , il); |
| 26 | |
| 27 | if (hparams.is_recurrent(il)) { |
| 28 | // ssm layer // |
| 29 | cur = build_mamba2_layer(inp: inp->get_recr(), cur, model, ubatch, il); |
| 30 | } else if (hparams.n_ff(il) == 0) { |
| 31 | // attention layer // |
| 32 | cur = build_attention_layer(cur, inp_attn: inp->get_attn(), model, n_embd_head, il); |
| 33 | } else { |
| 34 | cur = build_ffn_layer(cur, model, il); |
| 35 | } |
| 36 | |
| 37 | if (il == n_layer - 1 && inp_out_ids) { |
| 38 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 39 | inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids); |
| 40 | } |
| 41 | |
| 42 | // add residual |
| 43 | cur = ggml_add(ctx: ctx0, a: cur, b: inpSA); |
| 44 | cb(cur, name: "nemotron_h_block_out" , il); |
| 45 | |
| 46 | // input for next layer |
| 47 | inpL = cur; |
| 48 | } |
| 49 | |
| 50 | cur = inpL; |
| 51 | |
| 52 | cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 53 | |
| 54 | cb(cur, name: "result_norm" , il: -1); |
| 55 | res->t_embd = cur; |
| 56 | |
| 57 | // lm_head |
| 58 | cur = build_lora_mm(w: model.output, cur); |
| 59 | cb(cur, name: "result_output" , il: -1); |
| 60 | res->t_logits = cur; |
| 61 | |
| 62 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 63 | } |
| 64 | |
| 65 | ggml_tensor * llm_build_nemotron_h::build_attention_layer(ggml_tensor * cur, |
| 66 | llm_graph_input_attn_kv * inp_attn, |
| 67 | const llama_model & model, |
| 68 | const int64_t n_embd_head, |
| 69 | const int il) { |
| 70 | // compute Q and K and (optionally) RoPE them |
| 71 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 72 | cb(cur: Qcur, name: "Qcur" , il); |
| 73 | if (model.layers[il].bq) { |
| 74 | Qcur = ggml_add(ctx: ctx0, a: Qcur, b: model.layers[il].bq); |
| 75 | cb(cur: Qcur, name: "Qcur" , il); |
| 76 | } |
| 77 | |
| 78 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 79 | cb(cur: Kcur, name: "Kcur" , il); |
| 80 | if (model.layers[il].bk) { |
| 81 | Kcur = ggml_add(ctx: ctx0, a: Kcur, b: model.layers[il].bk); |
| 82 | cb(cur: Kcur, name: "Kcur" , il); |
| 83 | } |
| 84 | |
| 85 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 86 | cb(cur: Vcur, name: "Vcur" , il); |
| 87 | if (model.layers[il].bv) { |
| 88 | Vcur = ggml_add(ctx: ctx0, a: Vcur, b: model.layers[il].bv); |
| 89 | cb(cur: Vcur, name: "Vcur" , il); |
| 90 | } |
| 91 | |
| 92 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: hparams.n_head(il), ne2: n_tokens); |
| 93 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: hparams.n_head_kv(il), ne2: n_tokens); |
| 94 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: hparams.n_head_kv(il), ne2: n_tokens); |
| 95 | |
| 96 | cb(cur: Qcur, name: "Qcur" , il); |
| 97 | cb(cur: Kcur, name: "Kcur" , il); |
| 98 | cb(cur: Vcur, name: "Vcur" , il); |
| 99 | |
| 100 | const float kq_scale = |
| 101 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(x: float(n_embd_head)) : hparams.f_attention_scale; |
| 102 | cur = build_attn(inp: inp_attn, |
| 103 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 104 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale, il); |
| 105 | cb(cur, name: "attn_out" , il); |
| 106 | return cur; |
| 107 | } |
| 108 | |
| 109 | ggml_tensor * llm_build_nemotron_h::build_ffn_layer(ggml_tensor * cur, const llama_model & model, const int il) { |
| 110 | cur = build_ffn(cur, |
| 111 | up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL, |
| 112 | NULL, NULL, NULL, |
| 113 | down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL, |
| 114 | NULL, type_op: LLM_FFN_RELU_SQR, type_gate: LLM_FFN_PAR, il); |
| 115 | cb(cur, name: "ffn_out" , il); |
| 116 | |
| 117 | cur = build_cvec(cur, il); |
| 118 | cb(cur, name: "l_out" , il); |
| 119 | |
| 120 | return cur; |
| 121 | } |
| 122 | |