| 114 | } |
| 115 | |
| 116 | ggml_tensor * llm_build_lfm2::build_shortconv_block(ggml_tensor * cur, llm_graph_input_rs * inp_recr, int il) { |
| 117 | const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr(); |
| 118 | const uint32_t kv_head = mctx_cur->get_head(); |
| 119 | const int64_t n_seq_tokens = ubatch.n_seq_tokens; |
| 120 | const int64_t n_seqs = ubatch.n_seqs; |
| 121 | GGML_ASSERT(n_seqs != 0); |
| 122 | GGML_ASSERT(ubatch.equal_seqs()); |
| 123 | GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs); |
| 124 | |
| 125 | GGML_ASSERT(hparams.n_shortconv_l_cache > 1); |
| 126 | const uint32_t d_conv = hparams.n_shortconv_l_cache - 1; |
| 127 | |
| 128 | // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs} |
| 129 | cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs); |
| 130 | |
| 131 | auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur); |
| 132 | cb(bcx, "model.layers.{}.conv.in_proj", il); |
| 133 | |
| 134 | constexpr auto n_chunks = 3; |
| 135 | GGML_ASSERT(bcx->ne[0] % n_chunks == 0); |
| 136 | const auto chunk_size = bcx->ne[0] / n_chunks; |
| 137 | auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
| 138 | 0 * chunk_size * ggml_element_size(bcx)); |
| 139 | auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
| 140 | 1 * chunk_size * ggml_element_size(bcx)); |
| 141 | auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
| 142 | 2 * chunk_size * ggml_element_size(bcx)); |
| 143 | |
| 144 | auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x)); |
| 145 | |
| 146 | // read conv state |
| 147 | auto * conv_state = mctx_cur->get_r_l(il); |
| 148 | auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs); |
| 149 | auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs); |
| 150 | |
| 151 | bx = ggml_concat(ctx0, conv, bx, 0); |
| 152 | GGML_ASSERT(bx->ne[0] > conv->ne[0]); |
| 153 | |
| 154 | // last d_conv columns is a new conv state |
| 155 | auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2], |
| 156 | (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx)); |
| 157 | GGML_ASSERT(ggml_are_same_shape(conv, new_conv)); |
| 158 | |
| 159 | // write new conv conv state |
| 160 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv, |
| 161 | ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv), |
| 162 | kv_head * d_conv * n_embd * ggml_element_size(new_conv)))); |
| 163 | |
| 164 | auto * conv_kernel = model.layers[il].shortconv.conv; |
| 165 | auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel); |
| 166 | cb(conv_out, "model.layers.{}.conv.conv", il); |
| 167 | |
| 168 | auto * y = ggml_mul(ctx0, c, conv_out); |
| 169 | y = build_lora_mm(model.layers[il].shortconv.out_proj, y); |
| 170 | cb(y, "model.layers.{}.conv.out_proj", il); |
| 171 | // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens} |
| 172 | y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs); |
| 173 |
nothing calls this directly
no test coverage detected