| 3 | |
| 4 | |
| 5 | llm_build_bert::llm_build_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 6 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 7 | const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
| 8 | |
| 9 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 10 | |
| 11 | ggml_tensor * cur; |
| 12 | ggml_tensor * inpL; |
| 13 | ggml_tensor * inp_pos = nullptr; |
| 14 | |
| 15 | if (model.arch != LLM_ARCH_JINA_BERT_V2) { |
| 16 | inp_pos = build_inp_pos(); |
| 17 | } |
| 18 | |
| 19 | // construct input embeddings (token, type, position) |
| 20 | inpL = build_inp_embd(model.tok_embd); |
| 21 | |
| 22 | // token types are hardcoded to zero ("Sentence A") |
| 23 | if (model.type_embd) { |
| 24 | ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0); |
| 25 | inpL = ggml_add(ctx0, inpL, type_row0); |
| 26 | } |
| 27 | if (model.arch == LLM_ARCH_BERT) { |
| 28 | inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL); |
| 29 | } |
| 30 | cb(inpL, "inp_embd", -1); |
| 31 | |
| 32 | // embed layer norm |
| 33 | inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1); |
| 34 | cb(inpL, "inp_norm", -1); |
| 35 | |
| 36 | auto * inp_attn = build_attn_inp_no_cache(); |
| 37 | |
| 38 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 39 | |
| 40 | for (int il = 0; il < n_layer; ++il) { |
| 41 | ggml_tensor * cur = inpL; |
| 42 | |
| 43 | { |
| 44 | ggml_tensor * Qcur; |
| 45 | ggml_tensor * Kcur; |
| 46 | ggml_tensor * Vcur; |
| 47 | |
| 48 | // self-attention |
| 49 | if (model.layers[il].wqkv) { |
| 50 | cur = build_lora_mm(model.layers[il].wqkv, cur); |
| 51 | cb(cur, "wqkv", il); |
| 52 | |
| 53 | if (model.layers[il].bqkv) { |
| 54 | cur = ggml_add(ctx0, cur, model.layers[il].bqkv); |
| 55 | cb(cur, "bqkv", il); |
| 56 | } |
| 57 | |
| 58 | Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), cur->nb[1], |
| 59 | 0 * sizeof(float) * (n_embd)); |
| 60 | Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
| 61 | cur->nb[1], 1 * sizeof(float) * (n_embd)); |
| 62 | Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
nothing calls this directly
no test coverage detected