CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
rwkv6.cpp186 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_rwkv6::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS,     hparams.f_norm_eps, false);5    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);6    ml.get_key(LLM_KV_WKV_HEAD_SIZE,               hparams.wkv_head_size);7    ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM,          hparams.time_mix_extra_dim);8    ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM,        hparams.time_decay_extra_dim);9    ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS,      hparams.rescale_every_n_layers, false);10    ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT,           hparams.token_shift_count, false);11 12    switch (hparams.n_layer()) {13        case 24: type = LLM_TYPE_1_6B; break;14        case 32:15            switch (hparams.n_embd) {16                case 2560: type = LLM_TYPE_3B; break;17                case 4096: type = LLM_TYPE_7B; break;18                default: type = LLM_TYPE_UNKNOWN;19            } break;20        case 61: type = LLM_TYPE_14B; break;21        case 64: type = LLM_TYPE_32B; break;22        default: type = LLM_TYPE_UNKNOWN;23    }24}25 26void llama_model_rwkv6::load_arch_tensors(llama_model_loader &) {27    LLAMA_LOAD_LOCALS;28 29    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);30 31    // Block 0, LN032    tok_norm   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);33    tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias",   0), {n_embd}, 0);34 35    // output36    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);37    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);38    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);39 40    const int time_mix_extra_dim = hparams.time_mix_extra_dim;41    const int time_decay_extra_dim = hparams.time_decay_extra_dim;42    const int head_size = hparams.wkv_head_size;43    const int attn_hidden_size = n_embd;44    const int ffn_size = hparams.n_ff_arr[0];45 46    for (int i = 0; i < n_layer; ++i) {47        auto & layer = layers[i];48 49        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);50        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);51 52        layer.attn_norm_2   = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);53        layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i),   {n_embd}, 0);54 55        layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);56        layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);57 58        layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);59        layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);60        layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);61        layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);62        layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);63        layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);64        layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);65        GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));66 67        layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);68        layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);69        layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);70        layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);71        layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);72        layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);73        layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);74        layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);75 76        layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);77        layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);78        layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);79 80        layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);81        layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);82 83        layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);84        layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);85        layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);86    }87 88}89 90std::unique_ptr<llm_graph_context> llama_model_rwkv6::build_arch_graph(const llm_graph_params & params) const {91    return std::make_unique<graph>(*this, params);92}93 94llama_model_rwkv6::graph::graph(const llama_model & model, const llm_graph_params & params) :95    llm_build_rwkv6_base(model, params) {96    GGML_ASSERT(hparams.token_shift_count == 2);97 98    ggml_tensor * cur;99    ggml_tensor * inpL;100 101    inpL = build_inp_embd(model.tok_embd);102    inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, 0);103 104    auto * rs_inp = build_rs_inp();105 106    const auto n_embd       = hparams.n_embd;107    const auto n_seq_tokens = ubatch.n_seq_tokens;108    const auto n_seqs       = ubatch.n_seqs;109 110    ggml_tensor * inp_out_ids = build_inp_out_ids();111 112    for (int il = 0; il < n_layer; ++il) {113        const llama_layer * layer = &model.layers[il];114        inpL                      = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);115 116        ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);117 118        ggml_tensor * att_shift =119            ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);120        ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1],121                                               token_shift->nb[2], n_embd * ggml_element_size(token_shift));122 123        ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);124        cb(att_norm, "attn_norm", il);125 126        ggml_tensor * x_prev = ggml_concat(127            ctx0, att_shift,128            ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0), 1);129 130        cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);131 132        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);133        cb(ffn_inp, "ffn_inp", il);134 135        ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);136        cb(ffn_norm, "ffn_norm", il);137 138        x_prev = ggml_concat(139            ctx0, ffn_shift,140            ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0), 1);141 142        token_shift = ggml_concat(ctx0,143                                  ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2],144                                               (n_seq_tokens - 1) * n_embd * ggml_element_size(att_norm)),145                                  ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2],146                                               (n_seq_tokens - 1) * n_embd * ggml_element_size(ffn_norm)),147                                  1);148        ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));149 150        ffn_inp  = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);151        ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);152        x_prev   = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);153        cur      = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);154 155        if (il == n_layer - 1 && inp_out_ids) {156            ffn_inp  = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);157            ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);158            x_prev   = ggml_get_rows(ctx0, x_prev, inp_out_ids);159            cur      = ggml_get_rows(ctx0, cur, inp_out_ids);160        }161        cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);162        cur = ggml_add(ctx0, cur, ffn_inp);163 164        if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {165            cur = ggml_scale(ctx0, cur, 0.5F);166        }167        cur = build_cvec(cur, il);168        cb(cur, "l_out", il);169 170        // input for next layer171        inpL = cur;172    }173    cur = inpL;174    cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);175 176    cb(cur, "result_norm", -1);177    res->t_embd = cur;178 179    cur = build_lora_mm(model.output, cur, model.output_s);180 181    cb(cur, "result_output", -1);182    res->t_logits = cur;183 184    ggml_build_forward_expand(gf, cur);185}186