Team Ai
Datasetpublic

Brunobkr/llama.cpp_AlgMor24_github

ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes3kdownloads
mamba-base.cpp289 linesDownload Raw Back to models
1#include "models.h"2 3#include "llama-memory-recurrent.h"4 5llm_build_mamba_base::llm_build_mamba_base(const llm_graph_params & params) : llm_graph_context(params) {}6 7ggml_tensor * llm_build_mamba_base::build_mamba_layer(llm_graph_input_rs * inp,8                                                         ggml_tensor *        cur,9                                                         const llama_model &  model,10                                                         const llama_ubatch & ubatch,11                                                         int                  il) {12    const auto * mctx_cur = inp->mctx;13 14    const auto kv_head = mctx_cur->get_head();15 16    const auto & layer = model.layers[il];17 18    const int64_t d_conv         = hparams.ssm_d_conv;19    const int64_t d_inner        = hparams.ssm_d_inner;20    const int64_t d_state        = hparams.ssm_d_state;21    const int64_t dt_rank        = hparams.ssm_dt_rank;22    const int64_t n_head         = d_inner;23    const int64_t head_dim       = 1;24    const int64_t n_seqs         = ubatch.n_seqs;25    // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)26    const bool    ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;27 28    const int64_t n_seq_tokens = ubatch.n_seq_tokens;29 30    GGML_ASSERT(n_seqs != 0);31    GGML_ASSERT(ubatch.equal_seqs());32    GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);33    GGML_ASSERT(d_inner % n_head == 0);34 35    ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);36    ggml_tensor * ssm_states_all  = mctx_cur->get_s_l(il);37 38    ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);39    conv               = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);40 41    // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}42    cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);43 44    // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}45    ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur, layer.ssm_in_s);46    // split the above in two47    // => {d_inner, n_seq_tokens, n_seqs}48    ggml_tensor * x  = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);49    ggml_tensor * z =50        ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], d_inner * ggml_element_size(xz));51 52    // conv53    {54        // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}55        ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);56 57        // copy last (d_conv - 1) columns back into the state cache58        ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs, conv_x->nb[1], conv_x->nb[2],59                                               n_seq_tokens * (conv_x->nb[0]));60 61        ggml_build_forward_expand(62            gf, ggml_cpy(ctx0, last_conv,63                         ggml_view_1d(ctx0, conv_states_all, (d_conv - 1) * (d_inner) * (n_seqs),64                                      kv_head * (d_conv - 1) * (d_inner) *ggml_element_size(conv_states_all))));65 66        // 1D convolution67        // The equivalent is to make a self-overlapping view of conv_x68        // over d_conv columns at each stride in the 3rd dimension,69        // then element-wise multiply that with the conv1d weight,70        // then sum the elements of each row,71        // (the last two steps are a dot product over rows (also doable with mul_mat))72        // then permute away the ne[0] dimension,73        // and then you're left with the resulting x tensor.74        // For simultaneous sequences, all sequences need to have the same length.75        x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);76 77        // bias78        x = ggml_add(ctx0, x, layer.ssm_conv1d_b);79 80        x = ggml_silu(ctx0, x);81    }82 83    // ssm84    {85        // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}86        ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);87        // split88        ggml_tensor * dt   = ggml_view_3d(ctx0, x_db, dt_rank, n_seq_tokens, n_seqs, x_db->nb[1], x_db->nb[2], 0);89        ggml_tensor * B =90            ggml_view_4d(ctx0, x_db, d_state, /* n_group */ 1, n_seq_tokens, n_seqs, d_state * x_db->nb[0], x_db->nb[1],91                         x_db->nb[2], ggml_element_size(x_db) * dt_rank);92        ggml_tensor * C =93            ggml_view_4d(ctx0, x_db, d_state, /* n_group */ 1, n_seq_tokens, n_seqs, d_state * x_db->nb[0], x_db->nb[1],94                         x_db->nb[2], ggml_element_size(x_db) * (dt_rank + d_state));95 96        // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers97        if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {98            dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);99            B  = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);100            C  = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);101        }102 103        // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}104        dt = build_lora_mm(layer.ssm_dt, dt);105        dt = ggml_add(ctx0, dt, layer.ssm_dt_b);106 107        cur = x;108        x   = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);109 110        ggml_tensor * A = layer.ssm_a;111 112        // use the states and the indices provided by build_recurrent_state113        // (this is necessary in order to properly use the states before they are overwritten,114        //  while avoiding to make unnecessary copies of the states)115        auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {116            ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());117 118            // Custom operator to optimize the parallel associative scan119            // as described in the Annex D of the Mamba paper.120            // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}121            return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);122        };123 124        ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);125 126        // store last states127        ggml_build_forward_expand(128            gf, ggml_cpy(ctx0, ggml_view_1d(ctx0, y_ssm, d_state * d_inner * n_seqs, x->nb[3] * x->ne[3]),129                         ggml_view_1d(ctx0, ssm_states_all, d_state * d_inner * n_seqs,130                                      kv_head * d_state * d_inner * ggml_element_size(ssm_states_all))));131 132        ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);133 134        // TODO: skip computing output earlier for unused tokens135 136        y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));137        y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);138 139        // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}140        cur = build_lora_mm(layer.ssm_out, y, layer.ssm_out_s);141    }142 143    // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}144    cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);145 146    return cur;147}148 149ggml_tensor * llm_build_mamba_base::build_mamba2_layer(llm_graph_input_rs * inp,150                                                          ggml_tensor *        cur,151                                                          const llama_model &  model,152                                                          const llama_ubatch & ubatch,153                                                          int                  il) const {154    const auto * mctx_cur = inp->mctx;155 156    const auto kv_head = mctx_cur->get_head();157 158    const int64_t d_conv   = hparams.ssm_d_conv;159    const int64_t d_inner  = hparams.ssm_d_inner;160    const int64_t d_state  = hparams.ssm_d_state;161    const int64_t n_head   = hparams.ssm_dt_rank;162    const int64_t head_dim = d_inner / n_head;163    const int64_t n_group  = hparams.ssm_n_group;164    const int64_t n_seqs   = ubatch.n_seqs;165 166    const int64_t n_seq_tokens = ubatch.n_seq_tokens;167 168    GGML_ASSERT(n_seqs != 0);169    GGML_ASSERT(ubatch.equal_seqs());170    GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);171    GGML_ASSERT(d_inner % n_head  == 0);172    GGML_ASSERT(d_inner % n_group == 0);173 174    ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);175    ggml_tensor * ssm_states_all  = mctx_cur->get_s_l(il);176 177    ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);178    conv               = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2 * n_group * d_state, n_seqs);179 180    // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}181    cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);182 183    // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads184 185    // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}186    ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur, model.layers[il].ssm_in_s);187 188    // split the above in three189    ggml_tensor * z   = ggml_view_4d(ctx0, zxBCdt, head_dim, n_head, n_seq_tokens, n_seqs, head_dim * zxBCdt->nb[0],190                                     zxBCdt->nb[1], zxBCdt->nb[2], 0);191    ggml_tensor * xBC = ggml_view_3d(ctx0, zxBCdt, d_inner + 2 * n_group * d_state, n_seq_tokens, n_seqs, zxBCdt->nb[1],192                                     zxBCdt->nb[2], d_inner * ggml_element_size(zxBCdt));193    ggml_tensor * dt  = ggml_view_3d(ctx0, zxBCdt, n_head, n_seq_tokens, n_seqs, zxBCdt->nb[1], zxBCdt->nb[2],194                                     (2 * d_inner + 2 * n_group * d_state) * ggml_element_size(zxBCdt));195 196    // conv197    {198        // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}199        ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);200 201        // copy last (d_conv - 1) columns back into the state cache202        ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner + 2 * n_group * d_state, n_seqs,203                                               conv_x->nb[1], conv_x->nb[2], n_seq_tokens * (conv_x->nb[0]));204 205        ggml_build_forward_expand(gf, ggml_cpy(ctx0, last_conv,206                                               ggml_view_1d(ctx0, conv_states_all,207                                                            (d_conv - 1) * (d_inner + 2 * n_group * d_state) * (n_seqs),208                                                            kv_head * (d_conv - 1) * (d_inner + 2 * n_group * d_state) *209                                                                ggml_element_size(conv_states_all))));210 211        // 1D convolution212        // The equivalent is to make a self-overlapping view of conv_x213        // over d_conv columns at each stride in the 3rd dimension,214        // then element-wise multiply that with the conv1d weight,215        // then sum the elements of each row,216        // (the last two steps are a dot product over rows (also doable with mul_mat))217        // then permute away the ne[0] dimension,218        // and then you're left with the resulting x tensor.219        // For simultaneous sequences, all sequences need to have the same length.220        xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);221 222        // bias223        xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);224 225        xBC = ggml_silu(ctx0, xBC);226    }227 228    // ssm229    {230        // These correspond to V K Q in SSM/attention duality231        ggml_tensor * x = ggml_view_4d(ctx0, xBC, head_dim, n_head, n_seq_tokens, n_seqs, head_dim * xBC->nb[0],232                                       xBC->nb[1], xBC->nb[2], 0);233        ggml_tensor * B = ggml_view_4d(ctx0, xBC, d_state, n_group, n_seq_tokens, n_seqs, d_state * xBC->nb[0],234                                       xBC->nb[1], xBC->nb[2], d_inner * ggml_element_size(xBC));235        ggml_tensor * C = ggml_view_4d(ctx0, xBC, d_state, n_group, n_seq_tokens, n_seqs, d_state * xBC->nb[0],236                                       xBC->nb[1], xBC->nb[2], (d_inner + n_group * d_state) * ggml_element_size(xBC));237 238        // {n_head, n_seq_tokens, n_seqs}239        dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);240 241        ggml_tensor * A = model.layers[il].ssm_a;242 243        // use the states and the indices provided by build_recurrent_state244        // (this is necessary in order to properly use the states before they are overwritten,245        //  while avoiding to make unnecessary copies of the states)246        auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {247            ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());248 249            // TODO: use semistructured matrices to implement state-space duality250            // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}251            return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);252        };253 254        ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);255 256        // store last states257        ggml_build_forward_expand(258            gf, ggml_cpy(ctx0, ggml_view_1d(ctx0, y_ssm, d_state * d_inner * n_seqs, ggml_nelements(x) * x->nb[0]),259                         ggml_view_1d(ctx0, ssm_states_all, d_state * d_inner * n_seqs,260                                      kv_head * d_state * d_inner * ggml_element_size(ssm_states_all))));261 262        ggml_tensor * y = ggml_view_4d(ctx0, y_ssm, head_dim, n_head, n_seq_tokens, n_seqs, x->nb[1], n_head * x->nb[1],263                                       n_seq_tokens * n_head * x->nb[1], 0);264 265        // TODO: skip computing output earlier for unused tokens266 267        y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));268        cb(y, "mamba2_y_add_d", il);269        y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);270 271        // grouped RMS norm272        if (model.layers[il].ssm_norm) {273            y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);274            y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);275        }276 277        y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);278 279        // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}280        cur = build_lora_mm(model.layers[il].ssm_out, y, model.layers[il].ssm_out_s);281    }282 283    // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}284    cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);285    cb(cur, "mamba_out", il);286 287    return cur;288}289 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai