| File: | root/firefox-clang/third_party/llama.cpp/src/models/rwkv6qwen2.cpp |
| Warning: | line 128, column 13 Value stored to 'cur' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "models.h" |
| 2 | |
| 3 | void llama_model_rwkv6qwen2::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 | |
| 26 | void llama_model_rwkv6qwen2::load_arch_tensors(llama_model_loader &) { |
| 27 | LLAMA_LOAD_LOCALSconst int n_layer = hparams.n_layer(); (void)(n_layer); const int n_layer_all = hparams.n_layer_all; (void)(n_layer_all); const int n_layer_nextn = hparams.n_layer_nextn; (void)(n_layer_nextn ); const int64_t n_head = hparams.n_head(); (void)(n_head); const int64_t n_head_kv = hparams.n_head_kv(); (void)(n_head_kv); const int64_t n_embd = hparams.n_embd; (void)(n_embd); const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(); (void)(n_embd_k_gqa); const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(); (void)( n_embd_v_gqa); const int64_t n_embd_head_k = hparams.n_embd_head_k (); (void)(n_embd_head_k); const int64_t n_embd_head_v = hparams .n_embd_head_v(); (void)(n_embd_head_v); const int64_t n_ff = hparams.n_ff(); (void)(n_ff); const int64_t n_embd_gqa = n_embd_v_gqa ; (void)(n_embd_gqa); const int64_t n_vocab = vocab.n_tokens( ); (void)(n_vocab); const int64_t n_token_types = vocab.n_token_types (); (void)(n_token_types); const int64_t n_rot = hparams.n_rot (); (void)(n_rot); const int64_t n_expert = hparams.n_expert; (void)(n_expert); const int64_t n_expert_used = hparams.n_expert_used ; (void)(n_expert_used); const int64_t n_ctx_train = hparams. n_ctx_train; (void)(n_ctx_train);; |
| 28 | |
| 29 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
| 30 | |
| 31 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
| 32 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
| 33 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
| 34 | |
| 35 | const int time_mix_extra_dim = hparams.time_mix_extra_dim; |
| 36 | const int time_decay_extra_dim = hparams.time_decay_extra_dim; |
| 37 | const int head_size = hparams.wkv_head_size; |
| 38 | const int attn_hidden_size = n_embd; |
| 39 | int attn_key_value_size; |
| 40 | if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) { |
| 41 | attn_key_value_size = attn_hidden_size; |
| 42 | } else { |
| 43 | attn_key_value_size = n_head_kv * head_size; |
| 44 | } |
| 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 | |
| 51 | layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0); |
| 52 | layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0); |
| 53 | |
| 54 | layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0); |
| 55 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0); |
| 56 | |
| 57 | layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED); |
| 58 | layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0); |
| 59 | layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0); |
| 60 | layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0); |
| 61 | layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0); |
| 62 | layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0); |
| 63 | layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0); |
| 64 | layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0); |
| 65 | // optional bias tensors |
| 66 | layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED); |
| 67 | layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED); |
| 68 | layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED); |
| 69 | |
| 70 | layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0); |
| 71 | |
| 72 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
| 73 | |
| 74 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
| 75 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
| 76 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | std::unique_ptr<llm_graph_context> llama_model_rwkv6qwen2::build_arch_graph(const llm_graph_params & params) const { |
| 81 | return std::make_unique<graph>(*this, params); |
| 82 | } |
| 83 | |
| 84 | llama_model_rwkv6qwen2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) { |
| 85 | GGML_ASSERT(n_embd == hparams.n_embd_r())if (!(n_embd == hparams.n_embd_r())) ggml_abort("/root/firefox-clang/third_party/llama.cpp/src/models/rwkv6qwen2.cpp" , 85, "GGML_ASSERT(%s) failed", "n_embd == hparams.n_embd_r()" ); |
| 86 | |
| 87 | ggml_tensor * cur; |
| 88 | ggml_tensor * inpL; |
| 89 | |
| 90 | inpL = build_inp_embd(model.tok_embd); |
| 91 | |
| 92 | auto * rs_inp = build_rs_inp(); |
| 93 | |
| 94 | const auto n_embd = hparams.n_embd; |
| 95 | const auto n_seq_tokens = ubatch.n_seq_tokens; |
| 96 | const auto n_seqs = ubatch.n_seqs; |
| 97 | |
| 98 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 99 | |
| 100 | for (int il = 0; il < n_layer; ++il) { |
| 101 | const llama_layer * layer = &model.layers[il]; |
| 102 | inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs); |
| 103 | |
| 104 | ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il); |
| 105 | |
| 106 | ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il); |
| 107 | cb(att_norm, "attn_norm", il); |
| 108 | |
| 109 | ggml_tensor * x_prev = ggml_concat( |
| 110 | ctx0, |
| 111 | token_shift, |
| 112 | ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0), |
| 113 | 1 |
| 114 | ); |
| 115 | |
| 116 | cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il); |
| 117 | |
| 118 | token_shift = ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(att_norm)); |
| 119 | ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il)); |
| 120 | |
| 121 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
| 122 | cb(ffn_inp, "ffn_inp", il); |
| 123 | |
| 124 | cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens); |
| 125 | ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens); |
| 126 | |
| 127 | if (il == n_layer - 1 && inp_out_ids) { |
| 128 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
Value stored to 'cur' is never read | |
| 129 | ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids); |
| 130 | } |
| 131 | |
| 132 | // feed-forward network |
| 133 | cur = build_norm(ffn_inp, |
| 134 | model.layers[il].ffn_norm, NULL__null, |
| 135 | LLM_NORM_RMS, il); |
| 136 | cb(cur, "ffn_norm", il); |
| 137 | |
| 138 | cur = build_ffn(cur, |
| 139 | model.layers[il].ffn_up, NULL__null, NULL__null, |
| 140 | model.layers[il].ffn_gate, NULL__null, NULL__null, |
| 141 | model.layers[il].ffn_down, NULL__null, NULL__null, |
| 142 | NULL__null, |
| 143 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
| 144 | cb(cur, "ffn_out", il); |
| 145 | |
| 146 | cur = ggml_add(ctx0, cur, ffn_inp); |
| 147 | |
| 148 | cur = build_cvec(cur, il); |
| 149 | cb(cur, "l_out", il); |
| 150 | |
| 151 | // input for next layer |
| 152 | inpL = cur; |
| 153 | } |
| 154 | |
| 155 | cur = inpL; |
| 156 | cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1); |
| 157 | |
| 158 | cb(cur, "result_norm", -1); |
| 159 | res->t_embd = cur; |
| 160 | |
| 161 | cur = build_lora_mm(model.output, cur, model.output_s); |
| 162 | |
| 163 | cb(cur, "result_output", -1); |
| 164 | res->t_logits = cur; |
| 165 | |
| 166 | ggml_build_forward_expand(gf, cur); |
| 167 | } |