|
|
@@ -2,12 +2,17 @@
|
|
|
|
|
|
#include "llama-impl.h"
|
|
|
#include "llama-mmap.h"
|
|
|
+#include "llama-batch.h"
|
|
|
+#include "llama-cparams.h"
|
|
|
#include "llama-model-loader.h"
|
|
|
+#include "llama-kv-cache.h"
|
|
|
|
|
|
#include "ggml-cpp.h"
|
|
|
|
|
|
#include <algorithm>
|
|
|
#include <cassert>
|
|
|
+#include <cmath>
|
|
|
+#include <cfloat>
|
|
|
#include <cstring>
|
|
|
#include <cmath>
|
|
|
#include <functional>
|
|
|
@@ -244,6 +249,7 @@ static ggml_backend_buffer_type_t select_weight_buft(const llama_hparams & hpara
|
|
|
return cur_buft;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
@@ -302,7 +308,7 @@ static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & de
|
|
|
}
|
|
|
|
|
|
// GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
|
|
|
-static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, enum llama_split_mode split_mode, const float * tensor_split) {
|
|
|
+static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
|
|
|
buft_list_t buft_list;
|
|
|
|
|
|
// add the device split buffer type if requested and available
|
|
|
@@ -369,7 +375,7 @@ struct llama_model::impl {
|
|
|
std::vector<layer_dev> dev_layer;
|
|
|
};
|
|
|
|
|
|
-llama_model::llama_model(const struct llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
|
|
|
+llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
|
|
|
}
|
|
|
|
|
|
llama_model::~llama_model() {}
|
|
|
@@ -391,7 +397,7 @@ void llama_model::load_hparams(llama_model_loader & ml) {
|
|
|
|
|
|
// get metadata as string
|
|
|
for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
|
|
|
- enum gguf_type type = gguf_get_kv_type(ctx, i);
|
|
|
+ gguf_type type = gguf_get_kv_type(ctx, i);
|
|
|
if (type == GGUF_TYPE_ARRAY) {
|
|
|
continue;
|
|
|
}
|
|
|
@@ -1444,7 +1450,10 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
|
|
|
|
|
|
// skip unused tensors
|
|
|
if (info.op == GGML_OP_NONE) {
|
|
|
- LLAMA_LOG_WARN("model has unused tensor %s -- ignoring\n", tn.str().c_str());
|
|
|
+ const size_t nbytes = ggml_nbytes(t_meta);
|
|
|
+ LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
|
|
|
+
|
|
|
+ ml.size_data -= nbytes;
|
|
|
ml.n_created++;
|
|
|
|
|
|
return nullptr;
|
|
|
@@ -3631,8 +3640,8 @@ size_t llama_model::size() const {
|
|
|
return pimpl->n_bytes;
|
|
|
}
|
|
|
|
|
|
-size_t llama_model::max_nodes() const {
|
|
|
- return std::max<size_t>(8192, tensors_by_name.size()*5);
|
|
|
+size_t llama_model::n_tensors() const {
|
|
|
+ return tensors_by_name.size();
|
|
|
}
|
|
|
|
|
|
size_t llama_model::n_devices() const {
|
|
|
@@ -3745,7 +3754,7 @@ void llama_model::print_info() const {
|
|
|
LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
|
|
|
LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
|
|
|
LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
|
|
|
- LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((enum llama_expert_gating_func_type) hparams.expert_gating_func));
|
|
|
+ LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
|
|
|
LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
|
|
|
}
|
|
|
|
|
|
@@ -3821,9 +3830,9 @@ ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-const struct ggml_tensor * llama_model::get_tensor(const char * name) const {
|
|
|
+const ggml_tensor * llama_model::get_tensor(const char * name) const {
|
|
|
auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
|
|
|
- [name](const std::pair<std::string, struct ggml_tensor *> & it) {
|
|
|
+ [name](const std::pair<std::string, ggml_tensor *> & it) {
|
|
|
return it.first == name;
|
|
|
});
|
|
|
if (it == tensors_by_name.end()) {
|
|
|
@@ -3833,255 +3842,7556 @@ const struct ggml_tensor * llama_model::get_tensor(const char * name) const {
|
|
|
return it->second;
|
|
|
}
|
|
|
|
|
|
-//
|
|
|
-// interface implementation
|
|
|
-//
|
|
|
+struct llm_build_llama : public llm_graph_context {
|
|
|
+ llm_build_llama(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
|
|
|
-struct llama_model_params llama_model_default_params() {
|
|
|
- struct llama_model_params result = {
|
|
|
- /*.devices =*/ nullptr,
|
|
|
- /*.n_gpu_layers =*/ 0,
|
|
|
- /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
|
|
|
- /*.main_gpu =*/ 0,
|
|
|
- /*.tensor_split =*/ nullptr,
|
|
|
- /*.progress_callback =*/ nullptr,
|
|
|
- /*.progress_callback_user_data =*/ nullptr,
|
|
|
- /*.kv_overrides =*/ nullptr,
|
|
|
- /*.vocab_only =*/ false,
|
|
|
- /*.use_mmap =*/ true,
|
|
|
- /*.use_mlock =*/ false,
|
|
|
- /*.check_tensors =*/ false,
|
|
|
- };
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
|
|
|
-#ifdef GGML_USE_METAL
|
|
|
- // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
|
|
|
- result.n_gpu_layers = 999;
|
|
|
-#endif
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
|
|
|
- return result;
|
|
|
-}
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
|
|
|
-const struct llama_vocab * llama_model_get_vocab(const struct llama_model * model) {
|
|
|
- return &model->vocab;
|
|
|
-}
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
|
|
|
-void llama_free_model(struct llama_model * model) {
|
|
|
- llama_model_free(model);
|
|
|
-}
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
|
|
|
-void llama_model_free(struct llama_model * model) {
|
|
|
- delete model;
|
|
|
-}
|
|
|
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
|
|
|
-int32_t llama_model_n_ctx_train(const struct llama_model * model) {
|
|
|
- return model->hparams.n_ctx_train;
|
|
|
-}
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
|
|
|
-int32_t llama_model_n_embd(const struct llama_model * model) {
|
|
|
- return model->hparams.n_embd;
|
|
|
-}
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
|
|
|
-int32_t llama_model_n_layer(const struct llama_model * model) {
|
|
|
- return model->hparams.n_layer;
|
|
|
-}
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
|
|
|
-int32_t llama_model_n_head(const struct llama_model * model) {
|
|
|
- return model->hparams.n_head();
|
|
|
-}
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
|
|
|
-int32_t llama_model_n_head_kv(const struct llama_model * model) {
|
|
|
- return model->hparams.n_head_kv();
|
|
|
-}
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, kq_scale, il);
|
|
|
+ }
|
|
|
|
|
|
-// deprecated
|
|
|
-int32_t llama_n_ctx_train(const struct llama_model * model) {
|
|
|
- return llama_model_n_ctx_train(model);
|
|
|
-}
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
|
|
|
-// deprecated
|
|
|
-int32_t llama_n_embd(const struct llama_model * model) {
|
|
|
- return llama_model_n_embd(model);
|
|
|
-}
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_residual_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
|
|
|
+ }
|
|
|
|
|
|
-// deprecated
|
|
|
-int32_t llama_n_layer(const struct llama_model * model) {
|
|
|
- return llama_model_n_layer(model);
|
|
|
-}
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
|
|
|
-// deprecated
|
|
|
-int32_t llama_n_head(const struct llama_model * model) {
|
|
|
- return llama_model_n_head(model);
|
|
|
-}
|
|
|
+ // feed-forward network
|
|
|
+ if (model.layers[il].ffn_gate_inp == nullptr) {
|
|
|
|
|
|
-enum llama_rope_type llama_model_rope_type(const struct llama_model * model) {
|
|
|
- switch (model->arch) {
|
|
|
- // these models do not use RoPE
|
|
|
- case LLM_ARCH_GPT2:
|
|
|
- case LLM_ARCH_GPTJ:
|
|
|
- case LLM_ARCH_MPT:
|
|
|
- case LLM_ARCH_REFACT:
|
|
|
- case LLM_ARCH_BLOOM:
|
|
|
- case LLM_ARCH_MAMBA:
|
|
|
- case LLM_ARCH_JINA_BERT_V2:
|
|
|
- case LLM_ARCH_T5:
|
|
|
- case LLM_ARCH_T5ENCODER:
|
|
|
- case LLM_ARCH_JAIS:
|
|
|
- case LLM_ARCH_RWKV6:
|
|
|
- case LLM_ARCH_RWKV6QWEN2:
|
|
|
- case LLM_ARCH_WAVTOKENIZER_DEC:
|
|
|
- return LLAMA_ROPE_TYPE_NONE;
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
|
|
|
- // use what we call a normal RoPE, operating on pairs of consecutive head values
|
|
|
- case LLM_ARCH_LLAMA:
|
|
|
- case LLM_ARCH_DECI:
|
|
|
- case LLM_ARCH_BAICHUAN:
|
|
|
- case LLM_ARCH_STARCODER:
|
|
|
- case LLM_ARCH_PLAMO:
|
|
|
- case LLM_ARCH_ORION:
|
|
|
- case LLM_ARCH_INTERNLM2:
|
|
|
- case LLM_ARCH_MINICPM:
|
|
|
- case LLM_ARCH_XVERSE:
|
|
|
- case LLM_ARCH_COMMAND_R:
|
|
|
- case LLM_ARCH_COHERE2:
|
|
|
- case LLM_ARCH_OLMO:
|
|
|
- case LLM_ARCH_ARCTIC:
|
|
|
- case LLM_ARCH_DEEPSEEK:
|
|
|
- case LLM_ARCH_DEEPSEEK2:
|
|
|
- case LLM_ARCH_CHATGLM:
|
|
|
- case LLM_ARCH_GRANITE:
|
|
|
- case LLM_ARCH_GRANITE_MOE:
|
|
|
- case LLM_ARCH_CHAMELEON:
|
|
|
- return LLAMA_ROPE_TYPE_NORM;
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ } else {
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+ }
|
|
|
|
|
|
- // the pairs of head values are offset by n_rot/2
|
|
|
- case LLM_ARCH_FALCON:
|
|
|
- case LLM_ARCH_GROK:
|
|
|
- case LLM_ARCH_DBRX:
|
|
|
- case LLM_ARCH_BERT:
|
|
|
- case LLM_ARCH_NOMIC_BERT:
|
|
|
- case LLM_ARCH_STABLELM:
|
|
|
- case LLM_ARCH_BITNET:
|
|
|
- case LLM_ARCH_QWEN:
|
|
|
- case LLM_ARCH_QWEN2:
|
|
|
- case LLM_ARCH_QWEN2MOE:
|
|
|
- case LLM_ARCH_OLMO2:
|
|
|
- case LLM_ARCH_OLMOE:
|
|
|
- case LLM_ARCH_PHI2:
|
|
|
- case LLM_ARCH_PHI3:
|
|
|
- case LLM_ARCH_PHIMOE:
|
|
|
- case LLM_ARCH_GEMMA:
|
|
|
- case LLM_ARCH_GEMMA2:
|
|
|
- case LLM_ARCH_GEMMA3:
|
|
|
- case LLM_ARCH_STARCODER2:
|
|
|
- case LLM_ARCH_OPENELM:
|
|
|
- case LLM_ARCH_GPTNEOX:
|
|
|
- case LLM_ARCH_CODESHELL:
|
|
|
- case LLM_ARCH_NEMOTRON:
|
|
|
- case LLM_ARCH_EXAONE:
|
|
|
- case LLM_ARCH_MINICPM3:
|
|
|
- return LLAMA_ROPE_TYPE_NEOX;
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_residual_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
|
|
|
+ }
|
|
|
|
|
|
- case LLM_ARCH_QWEN2VL:
|
|
|
- return LLAMA_ROPE_TYPE_MROPE;
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
|
|
|
- // all model arches should be listed explicitly here
|
|
|
- case LLM_ARCH_UNKNOWN:
|
|
|
- GGML_ABORT("unknown architecture");
|
|
|
- }
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
|
|
|
- return LLAMA_ROPE_TYPE_NONE;
|
|
|
-}
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
|
|
|
-float llama_model_rope_freq_scale_train(const struct llama_model * model) {
|
|
|
- return model->hparams.rope_freq_scale_train;
|
|
|
-}
|
|
|
+ cur = inpL;
|
|
|
|
|
|
-int32_t llama_model_meta_val_str(const struct llama_model * model, const char * key, char * buf, size_t buf_size) {
|
|
|
- const auto & it = model->gguf_kv.find(key);
|
|
|
- if (it == model->gguf_kv.end()) {
|
|
|
- if (buf_size > 0) {
|
|
|
- buf[0] = '\0';
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_logit_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
|
|
|
}
|
|
|
- return -1;
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
}
|
|
|
- return snprintf(buf, buf_size, "%s", it->second.c_str());
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
-int32_t llama_model_meta_count(const struct llama_model * model) {
|
|
|
- return (int)model->gguf_kv.size();
|
|
|
-}
|
|
|
+struct llm_build_deci : public llm_graph_context {
|
|
|
+ llm_build_deci(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
|
|
|
-int32_t llama_model_meta_key_by_index(const struct llama_model * model, int i, char * buf, size_t buf_size) {
|
|
|
- if (i < 0 || i >= (int)model->gguf_kv.size()) {
|
|
|
- if (buf_size > 0) {
|
|
|
- buf[0] = '\0';
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+ const int64_t n_head_kv = hparams.n_head_kv(il);
|
|
|
+ const int64_t n_head = hparams.n_head(il);
|
|
|
+
|
|
|
+ if (n_head == 0) {
|
|
|
+ // attention-free layer of Llama-3_1-Nemotron-51B
|
|
|
+ cur = inpL;
|
|
|
+ } else {
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (n_head > 0 && n_head_kv == 0) {
|
|
|
+ // "linear attention" of Llama-3_1-Nemotron-51B
|
|
|
+ cur = build_lora_mm(model.layers[il].wo, cur);
|
|
|
+ cb(cur, "wo", il);
|
|
|
+ } else if (n_head > 0) {
|
|
|
+ // self-attention
|
|
|
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, kq_scale, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_residual_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ // modified to support attention-free layer of Llama-3_1-Nemotron-51B
|
|
|
+ ggml_tensor * ffn_inp = cur;
|
|
|
+ if (n_head > 0) {
|
|
|
+ ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ if (model.layers[il].ffn_gate_inp == nullptr) {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_residual_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
}
|
|
|
- return -1;
|
|
|
- }
|
|
|
- auto it = model->gguf_kv.begin();
|
|
|
- std::advance(it, i);
|
|
|
- return snprintf(buf, buf_size, "%s", it->first.c_str());
|
|
|
-}
|
|
|
|
|
|
-int32_t llama_model_meta_val_str_by_index(const struct llama_model * model, int32_t i, char * buf, size_t buf_size) {
|
|
|
- if (i < 0 || i >= (int)model->gguf_kv.size()) {
|
|
|
- if (buf_size > 0) {
|
|
|
- buf[0] = '\0';
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ // For Granite architecture
|
|
|
+ if (hparams.f_logit_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
|
|
|
}
|
|
|
- return -1;
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
}
|
|
|
- auto it = model->gguf_kv.begin();
|
|
|
- std::advance(it, i);
|
|
|
- return snprintf(buf, buf_size, "%s", it->second.c_str());
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
-int32_t llama_model_desc(const struct llama_model * model, char * buf, size_t buf_size) {
|
|
|
- return snprintf(buf, buf_size, "%s", model->desc().c_str());
|
|
|
-}
|
|
|
+struct llm_build_baichuan : public llm_graph_context {
|
|
|
+ llm_build_baichuan(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
|
|
|
-uint64_t llama_model_size(const struct llama_model * model) {
|
|
|
- return model->size();
|
|
|
-}
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
|
|
|
-const char * llama_model_chat_template(const struct llama_model * model, const char * name) {
|
|
|
- const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE_N)
|
|
|
- : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
|
|
|
- const auto & it = model->gguf_kv.find(key);
|
|
|
- if (it == model->gguf_kv.end()) {
|
|
|
- return nullptr;
|
|
|
- }
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
|
|
|
- return it->second.c_str();
|
|
|
-}
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
|
|
|
-uint64_t llama_model_n_params(const struct llama_model * model) {
|
|
|
- return model->n_elements();
|
|
|
-}
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
|
|
|
|
|
|
-bool llama_model_has_encoder(const struct llama_model * model) {
|
|
|
- switch (model->arch) {
|
|
|
- case LLM_ARCH_T5: return true;
|
|
|
- case LLM_ARCH_T5ENCODER: return true;
|
|
|
- default: return false;
|
|
|
- }
|
|
|
-}
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
|
|
|
-bool llama_model_has_decoder(const struct llama_model * model) {
|
|
|
- switch (model->arch) {
|
|
|
- case LLM_ARCH_T5ENCODER: return false;
|
|
|
- default: return true;
|
|
|
- }
|
|
|
-}
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
|
|
|
-llama_token llama_model_decoder_start_token(const struct llama_model * model) {
|
|
|
- return model->hparams.dec_start_token_id;
|
|
|
-}
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
|
|
|
-bool llama_model_is_recurrent(const struct llama_model * model) {
|
|
|
- switch (model->arch) {
|
|
|
- case LLM_ARCH_MAMBA: return true;
|
|
|
- case LLM_ARCH_RWKV6: return true;
|
|
|
- case LLM_ARCH_RWKV6QWEN2: return true;
|
|
|
- default: return false;
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ switch (model.type) {
|
|
|
+ case LLM_TYPE_7B:
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ break;
|
|
|
+ case LLM_TYPE_13B:
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd/n_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd/n_head, n_head, n_tokens);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ GGML_ABORT("fatal error");
|
|
|
+ }
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
}
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_xverse : public llm_graph_context {
|
|
|
+ llm_build_xverse(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_falcon : public llm_graph_context {
|
|
|
+ llm_build_falcon(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * attn_norm;
|
|
|
+
|
|
|
+ attn_norm = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(attn_norm, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ if (model.layers[il].attn_norm_2) {
|
|
|
+ // Falcon-40B
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm_2,
|
|
|
+ model.layers[il].attn_norm_2_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm_2", il);
|
|
|
+ } else {
|
|
|
+ cur = attn_norm;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ // using mode = 2 for neox mode
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = cur;
|
|
|
+
|
|
|
+ // feed forward
|
|
|
+ {
|
|
|
+ cur = build_ffn(attn_norm, // !! use the attn norm, not the result
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_grok : public llm_graph_context {
|
|
|
+ llm_build_grok(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // multiply by embedding_multiplier_scale of 78.38367176906169
|
|
|
+ inpL = ggml_scale(ctx0, inpL, 78.38367176906169f);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Grok
|
|
|
+ // if attn_out_norm is present then apply it before adding the input
|
|
|
+ if (model.layers[il].attn_out_norm) {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_out_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_out_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_GELU, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ // Grok
|
|
|
+ // if layer_out_norm is present then apply it before adding the input
|
|
|
+ // Idea: maybe ffn_out_norm is a better name
|
|
|
+ if (model.layers[il].layer_out_norm) {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].layer_out_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "layer_out_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ // Grok
|
|
|
+ // multiply logits by output_multiplier_scale of 0.5773502691896257
|
|
|
+
|
|
|
+ cur = ggml_scale(ctx0, cur, 0.5773502691896257f);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_dbrx : public llm_graph_context {
|
|
|
+ llm_build_dbrx(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = nullptr;
|
|
|
+ ggml_tensor * Kcur = nullptr;
|
|
|
+ ggml_tensor * Vcur = nullptr;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
|
+ cb(cur, "wqkv_clamped", il);
|
|
|
+
|
|
|
+ Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].attn_out_norm, NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_out_norm", il);
|
|
|
+
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_starcoder : public llm_graph_context {
|
|
|
+ llm_build_starcoder(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
|
|
|
+ cb(pos, "pos_embd", -1);
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, inpL, pos);
|
|
|
+ cb(inpL, "inpL", -1);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_refact : public llm_graph_context {
|
|
|
+ llm_build_refact(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_bert : public llm_graph_context {
|
|
|
+ llm_build_bert(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+ ggml_tensor * inp_pos = nullptr;
|
|
|
+
|
|
|
+ if (model.arch != LLM_ARCH_JINA_BERT_V2) {
|
|
|
+ inp_pos = build_inp_pos();
|
|
|
+ }
|
|
|
+
|
|
|
+ // construct input embeddings (token, type, position)
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // token types are hardcoded to zero ("Sentence A")
|
|
|
+ ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
|
|
|
+ inpL = ggml_add(ctx0, inpL, type_row0);
|
|
|
+ if (model.arch == LLM_ARCH_BERT) {
|
|
|
+ inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
|
|
|
+ }
|
|
|
+ cb(inpL, "inp_embd", -1);
|
|
|
+
|
|
|
+ // embed layer norm
|
|
|
+ inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
|
|
|
+ cb(inpL, "inp_norm", -1);
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_no_cache();
|
|
|
+
|
|
|
+ // iterate layers
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * cur = inpL;
|
|
|
+
|
|
|
+ ggml_tensor * Qcur;
|
|
|
+ ggml_tensor * Kcur;
|
|
|
+ ggml_tensor * Vcur;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_JINA_BERT_V2) {
|
|
|
+ Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
|
|
|
+
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
+ model.layers[il].attn_q_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
|
|
|
+
|
|
|
+ if (model.layers[il].attn_k_norm) {
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
+ model.layers[il].attn_k_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ } else {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ cb(cur, "kqv_out", il);
|
|
|
+
|
|
|
+ if (il == n_layer - 1 && pooling_type == LLAMA_POOLING_TYPE_NONE) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // re-add the layer input
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+
|
|
|
+ // attention layer norm
|
|
|
+ cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
|
|
|
+
|
|
|
+ if (model.layers[il].attn_norm_2 != nullptr) {
|
|
|
+ cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
|
|
|
+ cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = cur;
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ if (model.arch == LLM_ARCH_BERT) {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_PAR, il);
|
|
|
+ } else {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ }
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ // attentions bypass the intermediate layer
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ // output layer norm
|
|
|
+ cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cb(cur, "result_embd", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_bloom : public llm_graph_context {
|
|
|
+ llm_build_bloom(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ inpL = build_norm(inpL,
|
|
|
+ model.tok_norm,
|
|
|
+ model.tok_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+ cb(inpL, "inp_norm", -1);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_mpt : public llm_graph_context {
|
|
|
+ llm_build_mpt(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * pos;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ if (model.pos_embd) {
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+ pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
|
|
|
+ cb(pos, "pos_embd", -1);
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, inpL, pos);
|
|
|
+ cb(inpL, "inpL", -1);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * attn_norm;
|
|
|
+
|
|
|
+ attn_norm = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(attn_norm, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = attn_norm;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ if (model.layers[il].bqkv){
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hparams.f_clamp_kqv > 0.0f) {
|
|
|
+ cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
|
+ cb(cur, "wqkv_clamped", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ // Q/K Layernorm
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
+ model.layers[il].attn_q_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
+ model.layers[il].attn_k_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ } else {
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed forward
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ model.layers[il].ffn_act,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_stablelm : public llm_graph_context {
|
|
|
+ llm_build_stablelm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * inpSA = cur;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+ if (model.layers[il].attn_k_norm) {
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ if (model.layers[il].ffn_norm) {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+ } else {
|
|
|
+ // parallel residual
|
|
|
+ cur = inpSA;
|
|
|
+ }
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_qwen : public llm_graph_context {
|
|
|
+ llm_build_qwen(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 2*sizeof(float)*(n_embd)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ // using mode = 2 for neox mode
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward forward
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_qwen2 : public llm_graph_context {
|
|
|
+ llm_build_qwen2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_qwen2vl : public llm_graph_context {
|
|
|
+ llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ int sections[4];
|
|
|
+ std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_multi(
|
|
|
+ ctx0,
|
|
|
+ ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_multi(
|
|
|
+ ctx0,
|
|
|
+ ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_qwen2moe : public llm_graph_context {
|
|
|
+ llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self_attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * moe_out =
|
|
|
+ build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, false,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ // FFN shared expert
|
|
|
+ {
|
|
|
+ ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
|
|
|
+ cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
|
|
|
+
|
|
|
+ // sigmoid
|
|
|
+ ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
|
|
|
+ cb(cur_gate, "ffn_shexp_gate", il);
|
|
|
+
|
|
|
+ ggml_tensor * cur_ffn = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down_shexp, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur_ffn, "ffn_shexp", il);
|
|
|
+
|
|
|
+ ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
|
|
|
+ cb(ffn_shexp_out, "ffn_shexp_out", il);
|
|
|
+
|
|
|
+ moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
|
|
|
+ cb(moe_out, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = moe_out;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_phi2 : public llm_graph_context {
|
|
|
+ llm_build_phi2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * attn_norm_output;
|
|
|
+ ggml_tensor * ffn_output;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ attn_norm_output = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(attn_norm_output, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = nullptr;
|
|
|
+ ggml_tensor * Kcur = nullptr;
|
|
|
+ ggml_tensor * Vcur = nullptr;
|
|
|
+
|
|
|
+ if (model.layers[il].wqkv) {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+ } else {
|
|
|
+ Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
|
|
|
+ Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
|
|
|
+ Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ // with phi2, we scale the Q to avoid precision issues
|
|
|
+ // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
|
|
|
+ Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ ffn_output = build_ffn(attn_norm_output,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(ffn_output, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_output);
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+ cb(cur, "result_output_no_bias", -1);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.output_b);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_phi3 : public llm_graph_context {
|
|
|
+ llm_build_phi3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, true);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ auto * residual = inpL;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // rope freq factors for 128k context
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ ggml_tensor* attn_norm_output = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(attn_norm_output, "attn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = nullptr;
|
|
|
+ ggml_tensor * Kcur = nullptr;
|
|
|
+ ggml_tensor * Vcur = nullptr;
|
|
|
+
|
|
|
+ if (model.layers[il].wqkv) {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0 * sizeof(float) * (n_embd)));
|
|
|
+ Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1 * sizeof(float) * (n_embd)));
|
|
|
+ Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)));
|
|
|
+ } else {
|
|
|
+ Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
|
|
|
+ Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
|
|
|
+ Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor* inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ residual = ggml_get_rows(ctx0, residual, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, residual);
|
|
|
+ residual = cur;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ if (model.layers[il].ffn_gate_inp == nullptr) {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ } else {
|
|
|
+ // MoE branch
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, residual, cur);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ if (model.output_b != nullptr) {
|
|
|
+ cb(cur, "result_output_no_bias", -1);
|
|
|
+ cur = ggml_add(ctx0, cur, model.output_b);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_plamo : public llm_graph_context {
|
|
|
+ llm_build_plamo(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * attention_norm = cur;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+ ggml_tensor * sa_out = cur;
|
|
|
+
|
|
|
+ cur = attention_norm;
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ sa_out = ggml_get_rows(ctx0, sa_out, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, sa_out);
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_gpt2 : public llm_graph_context {
|
|
|
+ llm_build_gpt2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * pos;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
|
|
|
+ cb(pos, "pos_embd", -1);
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, inpL, pos);
|
|
|
+ cb(inpL, "inpL", -1);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_codeshell : public llm_graph_context {
|
|
|
+ llm_build_codeshell(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * tmpq = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * tmpk = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(tmpq, "tmpq", il);
|
|
|
+ cb(tmpk, "tmpk", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, tmpq, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, tmpk, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_orion : public llm_graph_context {
|
|
|
+ llm_build_orion(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ // if (model.layers[il].bq) {
|
|
|
+ // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ // cb(Qcur, "Qcur", il);
|
|
|
+ // }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ // if (model.layers[il].bk) {
|
|
|
+ // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ // cb(Kcur, "Kcur", il);
|
|
|
+ // }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ // if (model.layers[il].bv) {
|
|
|
+ // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ // cb(Vcur, "Vcur", il);
|
|
|
+ // }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_internlm2 : public llm_graph_context {
|
|
|
+ llm_build_internlm2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_minicpm3 : public llm_graph_context {
|
|
|
+ llm_build_minicpm3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ //TODO: if the model varies, these parameters need to be read from the model
|
|
|
+ const int64_t n_embd_base = 256;
|
|
|
+ const float scale_embd = 12.0f;
|
|
|
+ const float scale_depth = 1.4f;
|
|
|
+ const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
|
|
|
+
|
|
|
+ const uint32_t n_embd_head_qk_rope = hparams.n_rot;
|
|
|
+ const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
|
|
|
+ const uint32_t kv_lora_rank = hparams.n_lora_kv;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // scale the input embeddings
|
|
|
+ inpL = ggml_scale(ctx0, inpL, scale_embd);
|
|
|
+ cb(inpL, "inp_scaled", -1);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self_attention
|
|
|
+ {
|
|
|
+ ggml_tensor * q = NULL;
|
|
|
+ // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
|
|
|
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
|
|
|
+ cb(q, "q", il);
|
|
|
+
|
|
|
+ q = build_norm(q,
|
|
|
+ model.layers[il].attn_q_a_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(q, "q", il);
|
|
|
+
|
|
|
+ // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
|
|
|
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
|
|
|
+ cb(q, "q", il);
|
|
|
+
|
|
|
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
|
+ ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
|
+ 0);
|
|
|
+ cb(q_nope, "q_nope", il);
|
|
|
+
|
|
|
+ // and {n_head * n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
|
+ ggml_row_size(q->type, n_embd_head_qk_nope));
|
|
|
+ cb(q_pe, "q_pe", il);
|
|
|
+
|
|
|
+ // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
|
|
|
+ cb(kv_pe_compresseed, "kv_pe_compresseed", il);
|
|
|
+
|
|
|
+ // split into {kv_lora_rank, n_tokens}
|
|
|
+ ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ 0);
|
|
|
+ cb(kv_compressed, "kv_compressed", il);
|
|
|
+
|
|
|
+ // and {n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
|
|
|
+ cb(k_pe, "k_pe", il);
|
|
|
+
|
|
|
+ // TODO: the CUDA backend used to not support non-cont. (RMS) norm, investigate removing ggml_cont
|
|
|
+ kv_compressed = ggml_cont(ctx0, kv_compressed);
|
|
|
+ kv_compressed = build_norm(kv_compressed,
|
|
|
+ model.layers[il].attn_kv_a_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(kv_compressed, "kv_compressed", il);
|
|
|
+
|
|
|
+ // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
|
|
|
+ ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
|
|
|
+ cb(kv, "kv", il);
|
|
|
+
|
|
|
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
|
+ ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
|
|
|
+ ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
|
+ 0);
|
|
|
+ cb(k_nope, "k_nope", il);
|
|
|
+
|
|
|
+ // and {n_head * n_embd_head_v, n_tokens}
|
|
|
+ ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope)));
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ v_states = ggml_cont(ctx0, v_states);
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
|
|
|
+ 0);
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ q_pe = ggml_cont(ctx0, q_pe); // TODO: the CUDA backend used to not support non-cont. RoPE, investigate removing this
|
|
|
+ q_pe = ggml_rope_ext(
|
|
|
+ ctx0, q_pe, inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(q_pe, "q_pe", il);
|
|
|
+
|
|
|
+ // shared RoPE key
|
|
|
+ k_pe = ggml_cont(ctx0, k_pe); // TODO: the CUDA backend used to not support non-cont. RoPE, investigate removing this
|
|
|
+ k_pe = ggml_rope_ext(
|
|
|
+ ctx0, k_pe, inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(k_pe, "k_pe", il);
|
|
|
+
|
|
|
+ ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
|
|
|
+ cb(q_states, "q_states", il);
|
|
|
+
|
|
|
+ ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
|
|
|
+ cb(k_states, "k_states", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ q_states, k_states, v_states, nullptr, kq_scale, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // scale_res - scale the hidden states for residual connection
|
|
|
+ const float scale_res = scale_depth/sqrtf(float(n_layer));
|
|
|
+ cur = ggml_scale(ctx0, cur, scale_res);
|
|
|
+ cb(cur, "hidden_scaled", il);
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // scale the hidden states for residual connection
|
|
|
+ cur = ggml_scale(ctx0, cur, scale_res);
|
|
|
+ cb(cur, "hidden_scaled_ffn", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head scaling
|
|
|
+ const float scale_lmhead = float(n_embd_base)/float(n_embd);
|
|
|
+ cur = ggml_scale(ctx0, cur, scale_lmhead);
|
|
|
+ cb(cur, "lmhead_scaling", -1);
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_gemma : public llm_graph_context {
|
|
|
+ llm_build_gemma(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head_k = hparams.n_embd_head_k;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
|
|
|
+ cb(inpL, "inp_scaled", -1);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head_k)));
|
|
|
+ cb(Qcur, "Qcur_scaled", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(sa_out, "sa_out", il);
|
|
|
+
|
|
|
+ cur = build_norm(sa_out,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, sa_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_gemma2 : public llm_graph_context {
|
|
|
+ llm_build_gemma2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head_k = hparams.n_embd_head_k;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
|
|
|
+ cb(inpL, "inp_scaled", -1);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, true);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ // ref: https://github.com/google/gemma_pytorch/commit/03e657582d17cb5a8617ebf333c1c16f3694670e
|
|
|
+ switch (model.type) {
|
|
|
+ case LLM_TYPE_2B:
|
|
|
+ case LLM_TYPE_9B: Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head_k))); break;
|
|
|
+ case LLM_TYPE_27B: Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd / n_head))); break;
|
|
|
+ default: GGML_ABORT("fatal error");
|
|
|
+ };
|
|
|
+ cb(Qcur, "Qcur_scaled", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_post_norm", il);
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(sa_out, "sa_out", il);
|
|
|
+
|
|
|
+ cur = build_norm(sa_out,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+ cb(cur, "ffn_post_norm", -1);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, sa_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ // final logit soft-capping
|
|
|
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
|
|
|
+ cur = ggml_tanh(ctx0, cur);
|
|
|
+ cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_gemma3 : public llm_graph_context {
|
|
|
+ llm_build_gemma3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head_k = hparams.n_embd_head_k;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
|
|
|
+ if (ubatch.token) {
|
|
|
+ inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
|
|
|
+ cb(inpL, "inp_scaled", -1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ // TODO: is causal == true correct? might need some changes
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, true);
|
|
|
+
|
|
|
+ // "5-to-1 interleaved attention"
|
|
|
+ // 5 layers of local attention followed by 1 layer of global attention
|
|
|
+ static const int sliding_window_pattern = 6;
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ const bool is_sliding = il % sliding_window_pattern < (sliding_window_pattern - 1);
|
|
|
+
|
|
|
+ const float freq_base_l = is_sliding ? 10000.0f : freq_base;
|
|
|
+ const float freq_scale_l = is_sliding ? 1.0f : freq_scale;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head, n_tokens);
|
|
|
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
|
|
|
+ cb(Qcur, "Qcur_normed", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv, n_tokens);
|
|
|
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
|
|
|
+ cb(Kcur, "Kcur_normed", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, hparams.f_attention_scale, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_post_norm", il);
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(sa_out, "sa_out", il);
|
|
|
+
|
|
|
+ cur = build_norm(sa_out,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+ cb(cur, "ffn_post_norm", -1);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, sa_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// TODO: move up next to build_starcoder
|
|
|
+struct llm_build_starcoder2 : public llm_graph_context {
|
|
|
+ llm_build_starcoder2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_mamba : public llm_graph_context {
|
|
|
+ const llama_model & model;
|
|
|
+
|
|
|
+ llm_build_mamba(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params), model(model) {
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ // {n_embd, n_tokens}
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ ggml_tensor * state_copy = build_inp_s_copy();
|
|
|
+ ggml_tensor * state_mask = build_inp_s_mask();
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ //cur = build_mamba_layer(gf, cur, state_copy, state_mask, il);
|
|
|
+ cur = build_mamba_layer(gf, cur, state_copy, state_mask, ubatch, il);
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // residual
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ // final rmsnorm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: split
|
|
|
+ ggml_tensor * build_mamba_layer(
|
|
|
+ ggml_cgraph * gf,
|
|
|
+ ggml_tensor * cur,
|
|
|
+ ggml_tensor * state_copy,
|
|
|
+ ggml_tensor * state_mask,
|
|
|
+ const llama_ubatch & ubatch,
|
|
|
+ int il) const {
|
|
|
+ const llama_kv_cache_unified * kv_self = static_cast<const llama_kv_cache_unified *>(memory);
|
|
|
+
|
|
|
+ const auto kv_head = kv_self->head;
|
|
|
+
|
|
|
+ const int64_t d_conv = hparams.ssm_d_conv;
|
|
|
+ const int64_t d_inner = hparams.ssm_d_inner;
|
|
|
+ const int64_t d_state = hparams.ssm_d_state;
|
|
|
+ const int64_t dt_rank = hparams.ssm_dt_rank;
|
|
|
+ const int64_t n_seqs = ubatch.n_seqs;
|
|
|
+ // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
|
|
|
+ const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
|
|
|
+ // Use the same RMS norm as the final layer norm
|
|
|
+ const float norm_rms_eps = hparams.f_norm_rms_eps;
|
|
|
+
|
|
|
+ const int64_t n_seq_tokens = ubatch.n_seq_tokens;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_seqs != 0);
|
|
|
+ GGML_ASSERT(ubatch.equal_seqs);
|
|
|
+ GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
|
|
|
+
|
|
|
+ ggml_tensor * conv_states_all = kv_self->k_l[il];
|
|
|
+ ggml_tensor * ssm_states_all = kv_self->v_l[il];
|
|
|
+
|
|
|
+ // (ab)using the KV cache to store the states
|
|
|
+ ggml_tensor * conv = build_copy_mask_state(
|
|
|
+ gf, conv_states_all, state_copy, state_mask,
|
|
|
+ hparams.n_embd_k_s(), n_seqs);
|
|
|
+ conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
|
|
|
+ ggml_tensor * ssm = build_copy_mask_state(
|
|
|
+ gf, ssm_states_all, state_copy, state_mask,
|
|
|
+ hparams.n_embd_v_s(), n_seqs);
|
|
|
+ ssm = ggml_reshape_3d(ctx0, ssm, d_state, d_inner, n_seqs);
|
|
|
+
|
|
|
+ // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
|
|
|
+ cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
|
|
|
+
|
|
|
+ // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
|
|
|
+ ggml_tensor * xz = build_lora_mm(model.layers[il].ssm_in, cur);
|
|
|
+ // split the above in two
|
|
|
+ // => {d_inner, n_seq_tokens, n_seqs}
|
|
|
+ ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
|
|
|
+ ggml_tensor * z = 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));
|
|
|
+
|
|
|
+ // conv
|
|
|
+ {
|
|
|
+ // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
|
|
|
+ ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
|
|
|
+
|
|
|
+ // copy last (d_conv - 1) columns back into the state cache
|
|
|
+ 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], n_seq_tokens*(conv_x->nb[0]));
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf,
|
|
|
+ ggml_cpy(ctx0, last_conv,
|
|
|
+ ggml_view_1d(ctx0, conv_states_all,
|
|
|
+ (d_conv - 1)*(d_inner)*(n_seqs),
|
|
|
+ kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
|
|
|
+
|
|
|
+ // 1D convolution
|
|
|
+ // The equivalent is to make a self-overlapping view of conv_x
|
|
|
+ // over d_conv columns at each stride in the 3rd dimension,
|
|
|
+ // then element-wise multiply that with the conv1d weight,
|
|
|
+ // then sum the elements of each row,
|
|
|
+ // (the last two steps are a dot product over rows (also doable with mul_mat))
|
|
|
+ // then permute away the ne[0] dimension,
|
|
|
+ // and then you're left with the resulting x tensor.
|
|
|
+ // For simultaneous sequences, all sequences need to have the same length.
|
|
|
+ x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
|
|
|
+
|
|
|
+ // bias
|
|
|
+ x = ggml_add(ctx0, x, model.layers[il].ssm_conv1d_b);
|
|
|
+
|
|
|
+ x = ggml_silu(ctx0, x);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ssm
|
|
|
+ {
|
|
|
+ // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
|
|
|
+ ggml_tensor * x_db = build_lora_mm(model.layers[il].ssm_x, x);
|
|
|
+ // split
|
|
|
+ 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);
|
|
|
+ ggml_tensor * B = ggml_view_3d(ctx0, x_db, d_state, n_seq_tokens, n_seqs, x_db->nb[1], x_db->nb[2], ggml_element_size(x_db)*dt_rank);
|
|
|
+ ggml_tensor * C = ggml_view_3d(ctx0, x_db, d_state, n_seq_tokens, n_seqs, x_db->nb[1], x_db->nb[2], ggml_element_size(x_db)*(dt_rank+d_state));
|
|
|
+
|
|
|
+ // Some Mamba variants (e.g. FalconMamba) apply RMS norm in B, C & Dt layers
|
|
|
+ if (ssm_dt_b_c_rms) {
|
|
|
+ dt = ggml_rms_norm(ctx0, dt, norm_rms_eps);
|
|
|
+ B = ggml_rms_norm(ctx0, B, norm_rms_eps);
|
|
|
+ C = ggml_rms_norm(ctx0, C, norm_rms_eps);
|
|
|
+ }
|
|
|
+
|
|
|
+ // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
|
|
|
+ dt = build_lora_mm(model.layers[il].ssm_dt, dt);
|
|
|
+ dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
|
|
|
+
|
|
|
+ // Custom operator to optimize the parallel associative scan
|
|
|
+ // as described in the Annex D of the Mamba paper.
|
|
|
+ // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
|
|
|
+ ggml_tensor * y_ssm = ggml_ssm_scan(ctx0, ssm, x, dt, model.layers[il].ssm_a, B, C);
|
|
|
+
|
|
|
+ // store last states
|
|
|
+ ggml_build_forward_expand(gf,
|
|
|
+ ggml_cpy(ctx0,
|
|
|
+ ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]),
|
|
|
+ ggml_view_1d(ctx0, ssm_states_all, d_state*d_inner*n_seqs, kv_head*d_state*d_inner*ggml_element_size(ssm_states_all))));
|
|
|
+
|
|
|
+ ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[1], x->nb[2], 0);
|
|
|
+
|
|
|
+ // TODO: skip computing output earlier for unused tokens
|
|
|
+
|
|
|
+ // {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
|
|
|
+ y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
|
|
|
+ y = ggml_mul(ctx0, y, ggml_silu(ctx0, ggml_cont(ctx0, z)));
|
|
|
+
|
|
|
+ // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
|
|
|
+ cur = build_lora_mm(model.layers[il].ssm_out, y);
|
|
|
+ }
|
|
|
+
|
|
|
+ // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
|
|
|
+ cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
|
|
|
+ //cb(cur, "mamba_out", il);
|
|
|
+
|
|
|
+ return cur;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_command_r : public llm_graph_context {
|
|
|
+ llm_build_command_r(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ const float f_logit_scale = hparams.f_logit_scale;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+ ggml_tensor * ffn_inp = cur;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
+ Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
|
|
|
+ ggml_element_size(Qcur) * n_embd_head,
|
|
|
+ ggml_element_size(Qcur) * n_embd_head * n_head,
|
|
|
+ 0);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
|
|
|
+ ggml_element_size(Kcur) * n_embd_head,
|
|
|
+ ggml_element_size(Kcur) * n_embd_head * n_head_kv,
|
|
|
+ 0);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * attn_out = cur;
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(ffn_inp,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add together residual + FFN + self-attention
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+ cur = ggml_add(ctx0, cur, attn_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ if (f_logit_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, f_logit_scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_cohere2 : public llm_graph_context {
|
|
|
+ llm_build_cohere2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ const float f_logit_scale = hparams.f_logit_scale;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, true);
|
|
|
+
|
|
|
+ // sliding window switch pattern
|
|
|
+ const int32_t sliding_window_pattern = 4;
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ // three layers sliding window attention (window size 4096) and ROPE
|
|
|
+ // fourth layer uses global attention without positional embeddings
|
|
|
+ const bool is_sliding = il % sliding_window_pattern < (sliding_window_pattern - 1);
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+ ggml_tensor * ffn_inp = cur;
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // rope freq factors for 128k context
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (is_sliding) {
|
|
|
+ Qcur = ggml_rope_ext(ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor, attn_factor,
|
|
|
+ beta_fast, beta_slow);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos,
|
|
|
+ rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
|
|
|
+ attn_factor, beta_fast, beta_slow);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ } else {
|
|
|
+ // For non-sliding layers, just reshape without applying RoPE
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * attn_out = cur;
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
|
|
|
+ NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add together residual + FFN + self-attention
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+ cur = ggml_add(ctx0, cur, attn_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ if (f_logit_scale) {
|
|
|
+ cur = ggml_scale(ctx0, cur, f_logit_scale);
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// ref: https://allenai.org/olmo
|
|
|
+// based on the original build_llama() function, changes:
|
|
|
+// * non-parametric layer norm
|
|
|
+// * clamp qkv
|
|
|
+// * removed bias
|
|
|
+// * removed MoE
|
|
|
+struct llm_build_olmo : public llm_graph_context {
|
|
|
+ llm_build_olmo(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ NULL, NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (hparams.f_clamp_kqv > 0.0f) {
|
|
|
+ Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (hparams.f_clamp_kqv > 0.0f) {
|
|
|
+ Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (hparams.f_clamp_kqv > 0.0f) {
|
|
|
+ Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, nullptr,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ NULL, NULL,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ NULL, NULL,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_olmo2 : public llm_graph_context {
|
|
|
+ llm_build_olmo2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ // self_attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Qcur, "Qcur_normed", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Kcur, "Kcur_normed", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur_rope", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur_rope", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_post_norm", il);
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_ffn(ffn_inp,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_post_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+ cb(cur, "ffn_post_norm", -1);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// based on the build_qwen2moe() function, changes:
|
|
|
+// * removed shared experts
|
|
|
+// * removed bias
|
|
|
+// * added q, k norm
|
|
|
+struct llm_build_olmoe : public llm_graph_context {
|
|
|
+ llm_build_olmoe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self_attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Qcur, "Qcur_normed", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Kcur, "Kcur_normed", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur_rope", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur_rope", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, false,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_openelm : public llm_graph_context {
|
|
|
+ llm_build_openelm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ const int64_t n_head = hparams.n_head(il);
|
|
|
+ const int64_t n_head_kv = hparams.n_head_kv(il);
|
|
|
+ const int64_t n_head_qkv = 2*n_head_kv + n_head;
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+ ggml_tensor * residual = cur;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0));
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*n_head));
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*(n_head+n_head_kv)));
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, Qcur, inp_pos, NULL, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, Kcur, inp_pos, NULL, n_rot, rope_type, n_ctx_orig,
|
|
|
+ freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Vcur = ggml_reshape_2d(ctx0, Vcur, n_embd_head * n_head_kv, n_tokens);
|
|
|
+ cb(Qcur, "Vcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ residual = ggml_get_rows(ctx0, residual, inp_out_ids);
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_gptneox : public llm_graph_context {
|
|
|
+ llm_build_gptneox(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ffn
|
|
|
+ if (hparams.use_par_res) {
|
|
|
+ // attention and ffn are computed in parallel
|
|
|
+ // x = x + attn(ln1(x)) + ffn(ln2(x))
|
|
|
+
|
|
|
+ ggml_tensor * attn_out = cur;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, attn_out);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ } else {
|
|
|
+ // attention and ffn are computed sequentially
|
|
|
+ // x = x + attn(ln1(x))
|
|
|
+ // x = x + ffn(ln2(x))
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_arctic : public llm_graph_context {
|
|
|
+ llm_build_arctic(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(ffn_out, "ffn_out", il);
|
|
|
+
|
|
|
+ // MoE
|
|
|
+ cur = build_norm(inpSA,
|
|
|
+ model.layers[il].ffn_norm_exps, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm_exps", il);
|
|
|
+
|
|
|
+ cur = build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_out);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_deepseek : public llm_graph_context {
|
|
|
+ llm_build_deepseek(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, kq_scale, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ if ((uint32_t) il < hparams.n_layer_dense_lead) {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ } else {
|
|
|
+ // MoE branch
|
|
|
+ ggml_tensor * moe_out =
|
|
|
+ build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ nullptr,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, false,
|
|
|
+ false, hparams.expert_weights_scale,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(moe_out, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ // FFN shared expert
|
|
|
+ {
|
|
|
+ ggml_tensor * ffn_shexp = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down_shexp, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(ffn_shexp, "ffn_shexp", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_deepseek2 : public llm_graph_context {
|
|
|
+ llm_build_deepseek2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ bool is_lite = (hparams.n_layer == 27);
|
|
|
+
|
|
|
+ // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
|
|
|
+ // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
|
|
|
+ const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
|
|
|
+ const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(hparams.n_embd_head_k));
|
|
|
+ const float attn_factor_scaled = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
|
|
|
+
|
|
|
+ const uint32_t n_embd_head_qk_rope = hparams.n_rot;
|
|
|
+ const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
|
|
|
+ const uint32_t kv_lora_rank = hparams.n_lora_kv;
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ // {n_embd, n_tokens}
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self_attention
|
|
|
+ {
|
|
|
+ ggml_tensor * q = NULL;
|
|
|
+ if (!is_lite) {
|
|
|
+ // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
|
|
|
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
|
|
|
+ cb(q, "q", il);
|
|
|
+
|
|
|
+ q = build_norm(q,
|
|
|
+ model.layers[il].attn_q_a_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(q, "q", il);
|
|
|
+
|
|
|
+ // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
|
|
|
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
|
|
|
+ cb(q, "q", il);
|
|
|
+ } else {
|
|
|
+ q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
|
|
|
+ cb(q, "q", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
|
+ ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
|
+ 0);
|
|
|
+ cb(q_nope, "q_nope", il);
|
|
|
+
|
|
|
+ // and {n_head * n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k),
|
|
|
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
|
|
|
+ ggml_row_size(q->type, n_embd_head_qk_nope));
|
|
|
+ cb(q_pe, "q_pe", il);
|
|
|
+
|
|
|
+ // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
|
|
|
+ cb(kv_pe_compresseed, "kv_pe_compresseed", il);
|
|
|
+
|
|
|
+ // split into {kv_lora_rank, n_tokens}
|
|
|
+ ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ 0);
|
|
|
+ cb(kv_compressed, "kv_compressed", il);
|
|
|
+
|
|
|
+ // and {n_embd_head_qk_rope, n_tokens}
|
|
|
+ ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ kv_pe_compresseed->nb[1],
|
|
|
+ ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
|
|
|
+ cb(k_pe, "k_pe", il);
|
|
|
+
|
|
|
+ // TODO: the CUDA backend used to not support non-cont. (RMS) norm, investigate removing ggml_cont
|
|
|
+ kv_compressed = ggml_cont(ctx0, kv_compressed);
|
|
|
+ kv_compressed = build_norm(kv_compressed,
|
|
|
+ model.layers[il].attn_kv_a_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(kv_compressed, "kv_compressed", il);
|
|
|
+
|
|
|
+ // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
|
|
|
+ ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
|
|
|
+ cb(kv, "kv", il);
|
|
|
+
|
|
|
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
|
|
|
+ ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
|
|
|
+ ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
|
+ 0);
|
|
|
+ cb(k_nope, "k_nope", il);
|
|
|
+
|
|
|
+ // and {n_head * n_embd_head_v, n_tokens}
|
|
|
+ ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
|
|
|
+ ggml_row_size(kv->type, (n_embd_head_qk_nope)));
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ v_states = ggml_cont(ctx0, v_states);
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
|
|
|
+ ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
|
|
|
+ 0);
|
|
|
+ cb(v_states, "v_states", il);
|
|
|
+
|
|
|
+ q_pe = ggml_cont(ctx0, q_pe); // TODO: the CUDA backend used to not support non-cont. RoPE, investigate removing this
|
|
|
+ q_pe = ggml_rope_ext(
|
|
|
+ ctx0, q_pe, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor_scaled, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(q_pe, "q_pe", il);
|
|
|
+
|
|
|
+ // shared RoPE key
|
|
|
+ k_pe = ggml_cont(ctx0, k_pe); // TODO: the CUDA backend used to not support non-cont. RoPE, investigate removing this
|
|
|
+ k_pe = ggml_rope_ext(
|
|
|
+ ctx0, k_pe, inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor_scaled, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(k_pe, "k_pe", il);
|
|
|
+
|
|
|
+ ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
|
|
|
+ cb(q_states, "q_states", il);
|
|
|
+
|
|
|
+ ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
|
|
|
+ cb(k_states, "k_states", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ q_states, k_states, v_states, nullptr, kq_scale, il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ if ((uint32_t) il < hparams.n_layer_dense_lead) {
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ } else {
|
|
|
+ // MoE branch
|
|
|
+ ggml_tensor * moe_out =
|
|
|
+ build_moe_ffn(cur,
|
|
|
+ model.layers[il].ffn_gate_inp,
|
|
|
+ model.layers[il].ffn_up_exps,
|
|
|
+ model.layers[il].ffn_gate_exps,
|
|
|
+ model.layers[il].ffn_down_exps,
|
|
|
+ model.layers[il].ffn_exp_probs_b,
|
|
|
+ n_expert, n_expert_used,
|
|
|
+ LLM_FFN_SILU, hparams.expert_weights_norm,
|
|
|
+ true, hparams.expert_weights_scale,
|
|
|
+ (llama_expert_gating_func_type) hparams.expert_gating_func,
|
|
|
+ il);
|
|
|
+ cb(moe_out, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ // FFN shared expert
|
|
|
+ {
|
|
|
+ ggml_tensor * ffn_shexp = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down_shexp, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(ffn_shexp, "ffn_shexp", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = ggml_mul_mat(ctx0, model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_bitnet : public llm_graph_context {
|
|
|
+ llm_build_bitnet(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ if (model.layers[il].wq_scale) {
|
|
|
+ Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
|
|
|
+ }
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // B1.K
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ if (model.layers[il].wk_scale) {
|
|
|
+ Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
|
|
|
+ }
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // B1.V
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ if (model.layers[il].wv_scale) {
|
|
|
+ Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
|
|
|
+ }
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ NULL, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_sub_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_sub_norm", il);
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.layers[il].wo, cur);
|
|
|
+ if (model.layers[il].wo_scale) {
|
|
|
+ cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
|
|
|
+ }
|
|
|
+ if (model.layers[il].bo) {
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bo);
|
|
|
+ }
|
|
|
+ cb(cur, "attn_o_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward forward
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
|
|
|
+ model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_sub_out", il);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_sub_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_sub_norm", il);
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.layers[il].ffn_down, cur);
|
|
|
+ if (model.layers[il].ffn_down_scale) {
|
|
|
+ cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
|
|
|
+ }
|
|
|
+ cb(cur, "ffn_down", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ // FIXME: do not use model.tok_embd directly, duplicate as model.output
|
|
|
+ cur = build_lora_mm(model.tok_embd, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_t5_enc : public llm_graph_context {
|
|
|
+ llm_build_t5_enc(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_no_cache();
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm_enc, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b_enc ? model.layers[il].attn_rel_b_enc : model.layers[0].attn_rel_b_enc;
|
|
|
+ ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo_enc, nullptr,
|
|
|
+ Qcur, Kcur, Vcur, kq_b, 1.0f, il);
|
|
|
+ cb(cur, "kqv_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm_enc, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // T5 uses relu, flan-T5 uses gelu-gated
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up_enc, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate_enc, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down_enc, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
|
|
|
+ model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+ cb(cur, "result_embd", -1);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm_enc, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_t5_dec : public llm_graph_context {
|
|
|
+ llm_build_t5_dec(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ ggml_tensor * embd_enc = build_inp_cross_embd();
|
|
|
+ ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
|
|
|
+
|
|
|
+ const int64_t n_outputs_enc = embd_enc->ne[1];
|
|
|
+
|
|
|
+ auto * inp_attn_self = build_attn_inp_kv_unified(true, false);
|
|
|
+ auto * inp_attn_cross = build_attn_inp_cross();
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
+
|
|
|
+ ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
|
|
|
+ ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn_self, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, kq_b, 1.0f, il);
|
|
|
+ cb(cur, "kqv_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(cur, "cross_inp", il);
|
|
|
+
|
|
|
+ ggml_tensor * inpCA = cur;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_norm_cross, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm_cross", il);
|
|
|
+
|
|
|
+ // cross-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
|
|
|
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn_cross, gf,
|
|
|
+ model.layers[il].wo_cross, nullptr,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f, il);
|
|
|
+ cb(cur, "kqv_out", il);
|
|
|
+
|
|
|
+ //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
|
|
|
+ //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
|
|
|
+
|
|
|
+ //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
|
|
|
+ //cb(kq, "kq", il);
|
|
|
+
|
|
|
+ //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
|
|
|
+ //cb(kq, "kq_soft_max_ext", il);
|
|
|
+
|
|
|
+ //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
|
|
|
+ //cb(v, "v", il);
|
|
|
+
|
|
|
+ //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
|
|
|
+ //cb(kqv, "kqv", il);
|
|
|
+
|
|
|
+ //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
|
|
|
+ //cb(kqv_merged, "kqv_merged", il);
|
|
|
+
|
|
|
+ //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
|
|
|
+ //cb(cur, "kqv_merged_cont", il);
|
|
|
+
|
|
|
+ //ggml_build_forward_expand(gf, cur);
|
|
|
+
|
|
|
+ //cur = build_lora_mm(model.layers[il].wo_cross, cur);
|
|
|
+ //cb(cur, "kqv_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ // T5 uses relu, flan-T5 uses gelu-gated
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
|
|
|
+ model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
|
|
|
+ il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+ cb(cur, "result_embd", -1);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_jais : public llm_graph_context {
|
|
|
+ llm_build_jais(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+
|
|
|
+ ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*cur->nb[0]*(n_embd)));
|
|
|
+ ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd)));
|
|
|
+ ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa)));
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/float(n_embd_head), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(inpL, "l_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_chatglm : public llm_graph_context {
|
|
|
+ llm_build_chatglm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ ggml_tensor * Qcur = nullptr;
|
|
|
+ ggml_tensor * Kcur = nullptr;
|
|
|
+ ggml_tensor * Vcur = nullptr;
|
|
|
+
|
|
|
+ if (model.layers[il].wqkv == nullptr) {
|
|
|
+ Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ }
|
|
|
+ Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ }
|
|
|
+ Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
|
|
|
+ cb(cur, "wqkv", il);
|
|
|
+ if (model.layers[il].bqkv) {
|
|
|
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
|
|
|
+ cb(cur, "bqkv", il);
|
|
|
+ }
|
|
|
+ Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
|
|
|
+ Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
|
|
|
+ Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
|
|
|
+ }
|
|
|
+
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur_rope", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur_rope", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add the input
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // FF
|
|
|
+ {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(inpL, "l_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.output_norm,
|
|
|
+ NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_nemotron : public llm_graph_context {
|
|
|
+ llm_build_nemotron(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ //GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm,
|
|
|
+ model.layers[il].attn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_exaone : public llm_graph_context {
|
|
|
+ llm_build_exaone(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
|
|
|
+ ggml_tensor * rope_factors = static_cast<const llama_kv_cache_unified *>(memory)->cbs.get_rope_factors(n_ctx_per_seq, il);
|
|
|
+
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, rope_factors,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, model.layers[il].bo,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_rwkv6_base : public llm_graph_context {
|
|
|
+ const llama_model & model;
|
|
|
+
|
|
|
+ llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * build_rwkv6_channel_mix(
|
|
|
+ const llama_layer * layer,
|
|
|
+ ggml_tensor * cur,
|
|
|
+ ggml_tensor * x_prev,
|
|
|
+ llm_arch arch) const {
|
|
|
+ ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
|
|
|
+ switch (arch) {
|
|
|
+ case LLM_ARCH_RWKV6:
|
|
|
+ {
|
|
|
+ ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
|
|
|
+ ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
|
|
|
+
|
|
|
+ ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
|
|
|
+ ggml_tensor * k = ggml_sqr(
|
|
|
+ ctx0,
|
|
|
+ ggml_relu(
|
|
|
+ ctx0,
|
|
|
+ build_lora_mm(layer->channel_mix_key, xk)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
|
|
|
+ } break;
|
|
|
+ default:
|
|
|
+ GGML_ABORT("fatal error");
|
|
|
+ }
|
|
|
+
|
|
|
+ return cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * build_rwkv6_time_mix(
|
|
|
+ ggml_cgraph * gf,
|
|
|
+ ggml_tensor * cur,
|
|
|
+ ggml_tensor * x_prev,
|
|
|
+ ggml_tensor * state_copy,
|
|
|
+ ggml_tensor * state_mask,
|
|
|
+ const llama_ubatch & ubatch,
|
|
|
+ int il) const {
|
|
|
+ const llama_kv_cache_unified * kv_self = static_cast<const llama_kv_cache_unified *>(memory);
|
|
|
+
|
|
|
+ const auto n_tokens = ubatch.n_tokens;
|
|
|
+ const auto n_seqs = ubatch.n_seqs;
|
|
|
+ const auto n_embd = hparams.n_embd;
|
|
|
+ const auto head_size = hparams.wkv_head_size;
|
|
|
+ const auto n_head = n_embd / head_size;
|
|
|
+ const auto n_head_kv = hparams.n_head_kv(il);
|
|
|
+
|
|
|
+ const auto kv_head = kv_self->head;
|
|
|
+
|
|
|
+ const auto & layer = model.layers[il];
|
|
|
+
|
|
|
+ bool is_qrwkv = layer.time_mix_first == nullptr;
|
|
|
+
|
|
|
+ ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
|
|
|
+ ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
|
|
|
+
|
|
|
+ xxx = ggml_reshape_4d(
|
|
|
+ ctx0,
|
|
|
+ ggml_tanh(
|
|
|
+ ctx0,
|
|
|
+ ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
|
|
|
+ ),
|
|
|
+ layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
|
|
|
+ );
|
|
|
+
|
|
|
+ xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
|
|
|
+
|
|
|
+ xxx = ggml_mul_mat(
|
|
|
+ ctx0,
|
|
|
+ ggml_reshape_4d(
|
|
|
+ ctx0,
|
|
|
+ layer.time_mix_w2,
|
|
|
+ layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
|
|
|
+ ),
|
|
|
+ xxx
|
|
|
+ );
|
|
|
+
|
|
|
+ ggml_tensor *xw, *xk, *xv, *xr, *xg;
|
|
|
+ if (layer.time_mix_lerp_fused) {
|
|
|
+ // fusing these weights makes some performance improvement
|
|
|
+ sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
|
|
|
+ cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
|
|
|
+ xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
|
|
|
+ xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
|
|
|
+ xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
|
|
|
+ xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
|
|
|
+ xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
|
|
|
+ xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
|
|
|
+ } else {
|
|
|
+ // for backward compatibility
|
|
|
+ xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
|
|
|
+ xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
|
|
|
+ xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
|
|
|
+ xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
|
|
|
+ xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
|
|
|
+
|
|
|
+ xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
|
|
|
+ xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
|
|
|
+ xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
|
|
|
+ xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
|
|
|
+ xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
|
|
|
+ ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
|
|
|
+ ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
|
|
|
+ if (layer.time_mix_receptance_b) {
|
|
|
+ r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
|
|
|
+ }
|
|
|
+ if (layer.time_mix_key_b) {
|
|
|
+ k = ggml_add(ctx0, k, layer.time_mix_key_b);
|
|
|
+ }
|
|
|
+ if (layer.time_mix_value_b) {
|
|
|
+ v = ggml_add(ctx0, v, layer.time_mix_value_b);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
|
|
|
+ if (is_qrwkv) {
|
|
|
+ g = ggml_sigmoid(ctx0, g);
|
|
|
+ } else {
|
|
|
+ g = ggml_silu(ctx0, g);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (n_head_kv != 0 && n_head_kv != n_head) {
|
|
|
+ GGML_ASSERT(n_head % n_head_kv == 0);
|
|
|
+ k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
|
|
|
+ v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
|
|
|
+ ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
|
|
|
+ k = ggml_repeat(ctx0, k, tmp);
|
|
|
+ v = ggml_repeat(ctx0, v, tmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
|
|
|
+ v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
|
|
|
+ r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
|
|
|
+
|
|
|
+ ggml_tensor * w = ggml_mul_mat(
|
|
|
+ ctx0,
|
|
|
+ layer.time_mix_decay_w2,
|
|
|
+ ggml_tanh(
|
|
|
+ ctx0,
|
|
|
+ ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ w = ggml_add(ctx0, w, layer.time_mix_decay);
|
|
|
+ w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
|
|
|
+ w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
|
|
|
+
|
|
|
+ if (is_qrwkv) {
|
|
|
+ // k = k * (1 - w)
|
|
|
+ k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * wkv_state = build_copy_mask_state(
|
|
|
+ gf, kv_self->v_l[il], state_copy, state_mask,
|
|
|
+ hparams.n_embd_v_s(), n_seqs);
|
|
|
+
|
|
|
+ ggml_tensor * wkv_output;
|
|
|
+ if (is_qrwkv) {
|
|
|
+ wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
|
|
|
+ } else {
|
|
|
+ wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
|
|
|
+ }
|
|
|
+ cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
|
|
|
+ wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
|
|
|
+
|
|
|
+ ggml_build_forward_expand(
|
|
|
+ gf,
|
|
|
+ ggml_cpy(
|
|
|
+ ctx0,
|
|
|
+ wkv_state,
|
|
|
+ ggml_view_1d(
|
|
|
+ ctx0,
|
|
|
+ kv_self->v_l[il],
|
|
|
+ hparams.n_embd_v_s() * n_seqs,
|
|
|
+ hparams.n_embd_v_s() * kv_head * ggml_element_size(kv_self->v_l[il])
|
|
|
+ )
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!is_qrwkv) {
|
|
|
+ // group norm with head_count groups
|
|
|
+ cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
|
|
|
+ cur = ggml_norm(ctx0, cur, 64e-5f);
|
|
|
+
|
|
|
+ // Convert back to regular vectors.
|
|
|
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
|
|
|
+ cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
|
|
|
+ } else {
|
|
|
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_mul(ctx0, cur, g);
|
|
|
+ cur = build_lora_mm(layer.time_mix_output, cur);
|
|
|
+
|
|
|
+ return cur;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_rwkv6 : public llm_build_rwkv6_base {
|
|
|
+ llm_build_rwkv6(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv6_base(model, params) {
|
|
|
+ GGML_ASSERT(hparams.token_shift_count == 2);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+ inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
|
|
|
+
|
|
|
+ ggml_tensor * state_copy = build_inp_s_copy();
|
|
|
+ ggml_tensor * state_mask = build_inp_s_mask();
|
|
|
+
|
|
|
+ const auto n_embd = hparams.n_embd;
|
|
|
+ const auto n_seq_tokens = ubatch.n_seq_tokens;
|
|
|
+ const auto n_seqs = ubatch.n_seqs;
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ const llama_layer * layer = &model.layers[il];
|
|
|
+
|
|
|
+ ggml_tensor * token_shift = build_rwkv_token_shift_load(
|
|
|
+ gf, state_copy, state_mask, ubatch, il
|
|
|
+ );
|
|
|
+
|
|
|
+ ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
|
|
|
+ ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], n_embd * ggml_element_size(token_shift));
|
|
|
+
|
|
|
+ ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
|
|
|
+ cb(att_norm, "attn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * x_prev = ggml_concat(
|
|
|
+ ctx0,
|
|
|
+ att_shift,
|
|
|
+ ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+
|
|
|
+ cur = build_rwkv6_time_mix(gf, att_norm, x_prev, state_copy, state_mask, ubatch, il);
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
|
|
|
+ cb(ffn_norm, "ffn_norm", il);
|
|
|
+
|
|
|
+ x_prev = ggml_concat(
|
|
|
+ ctx0,
|
|
|
+ ffn_shift,
|
|
|
+ ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+
|
|
|
+ cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ token_shift = ggml_concat(ctx0,
|
|
|
+ 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)),
|
|
|
+ ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(ffn_norm)),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+ ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
|
|
|
+
|
|
|
+ if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
|
|
|
+ cur = ggml_scale(ctx0, cur, 0.5F);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+
|
|
|
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+
|
|
|
+ cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
|
|
|
+struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
|
|
|
+ llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv6_base(model, params) {
|
|
|
+ GGML_ASSERT(n_embd == hparams.n_embd_k_s());
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ ggml_tensor * state_copy = build_inp_s_copy();
|
|
|
+ ggml_tensor * state_mask = build_inp_s_mask();
|
|
|
+
|
|
|
+ const auto n_embd = hparams.n_embd;
|
|
|
+ const auto n_seq_tokens = ubatch.n_seq_tokens;
|
|
|
+ const auto n_seqs = ubatch.n_seqs;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ const llama_layer * layer = &model.layers[il];
|
|
|
+
|
|
|
+ ggml_tensor * token_shift = build_rwkv_token_shift_load(
|
|
|
+ gf, state_copy, state_mask, ubatch, il
|
|
|
+ );
|
|
|
+
|
|
|
+ ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
|
|
|
+ cb(att_norm, "attn_norm", il);
|
|
|
+
|
|
|
+ ggml_tensor * x_prev = ggml_concat(
|
|
|
+ ctx0,
|
|
|
+ token_shift,
|
|
|
+ ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+
|
|
|
+ cur = build_rwkv6_time_mix(gf, att_norm, x_prev, state_copy, state_mask, ubatch, il);
|
|
|
+
|
|
|
+ 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));
|
|
|
+ ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+
|
|
|
+ cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// ref: https://github.com/facebookresearch/chameleon
|
|
|
+// based on the original build_llama() function, changes:
|
|
|
+// * qk-norm
|
|
|
+// * swin-norm
|
|
|
+// * removed bias
|
|
|
+// * removed MoE
|
|
|
+struct llm_build_chameleon : public llm_graph_context {
|
|
|
+ llm_build_chameleon(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ // inp_pos - contains the positions
|
|
|
+ ggml_tensor * inp_pos = build_inp_pos();
|
|
|
+
|
|
|
+ auto * inp_attn = build_attn_inp_kv_unified(true, false);
|
|
|
+
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+
|
|
|
+ // norm
|
|
|
+ if (hparams.swin_norm) {
|
|
|
+ cur = inpL;
|
|
|
+ } else {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
+ Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
|
|
|
+ ggml_element_size(Qcur) * n_embd_head,
|
|
|
+ ggml_element_size(Qcur) * n_embd_head * n_head,
|
|
|
+ 0);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Qcur = build_norm(Qcur,
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
+ model.layers[il].attn_q_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.layers[il].attn_k_norm) {
|
|
|
+ Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
|
|
|
+ ggml_element_size(Kcur) * n_embd_head,
|
|
|
+ ggml_element_size(Kcur) * n_embd_head * n_head_kv,
|
|
|
+ 0);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ Kcur = build_norm(Kcur,
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
+ model.layers[il].attn_k_norm_b,
|
|
|
+ LLM_NORM, il);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ Qcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+
|
|
|
+ Kcur = ggml_rope_ext(
|
|
|
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, nullptr,
|
|
|
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
|
|
|
+ ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
+ );
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+
|
|
|
+ cur = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, nullptr,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+
|
|
|
+ if (hparams.swin_norm) {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (il == n_layer - 1) {
|
|
|
+ // skip computing output for unused tokens
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
+ cb(ffn_inp, "ffn_inp", il);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ if (!hparams.swin_norm) {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ if (hparams.swin_norm) {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ cur = build_cvec(cur, il);
|
|
|
+ cb(cur, "l_out", il);
|
|
|
+
|
|
|
+ // input for next layer
|
|
|
+ inpL = cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm, NULL,
|
|
|
+ LLM_NORM_RMS, -1);
|
|
|
+
|
|
|
+ cb(cur, "result_norm", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+ cb(cur, "result_output_with_img_logits", -1);
|
|
|
+
|
|
|
+ // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
|
|
|
+ // Needs to be removed once image outputs are supported.
|
|
|
+ int img_token_end_idx = 8196;
|
|
|
+ int img_token_start_idx = 4;
|
|
|
+ int num_img_tokens = img_token_end_idx - img_token_start_idx;
|
|
|
+ // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
|
|
|
+ // which ensures that text token values are always at least larger than image token values
|
|
|
+ ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
|
|
|
+ img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
|
|
|
+ cb(img_logits, "img_logits", -1);
|
|
|
+
|
|
|
+ cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
|
|
|
+
|
|
|
+ cb(cur, "result_output", -1);
|
|
|
+ res->t_logits = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+struct llm_build_wavtokenizer_dec : public llm_graph_context {
|
|
|
+ llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ ggml_tensor * cur;
|
|
|
+ ggml_tensor * inpL;
|
|
|
+
|
|
|
+ inpL = build_inp_embd(model.tok_embd);
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
|
|
|
+
|
|
|
+ cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
|
|
|
+ cur = ggml_add(ctx0, cur, model.conv1d_b);
|
|
|
+
|
|
|
+ // posnet
|
|
|
+ for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
|
|
|
+ const auto & layer = model.layers[il].posnet;
|
|
|
+
|
|
|
+ inpL = cur;
|
|
|
+
|
|
|
+ switch (il) {
|
|
|
+ case 0:
|
|
|
+ case 1:
|
|
|
+ case 3:
|
|
|
+ case 4:
|
|
|
+ {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ layer.norm1,
|
|
|
+ layer.norm1_b,
|
|
|
+ LLM_NORM_GROUP, 0);
|
|
|
+
|
|
|
+ cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
|
|
|
+
|
|
|
+ cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
|
|
|
+ cur = ggml_add(ctx0, cur, layer.conv1_b);
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ layer.norm2,
|
|
|
+ layer.norm2_b,
|
|
|
+ LLM_NORM_GROUP, 0);
|
|
|
+
|
|
|
+ cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
|
|
|
+
|
|
|
+ cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
|
|
|
+ cur = ggml_add(ctx0, cur, layer.conv2_b);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+ } break;
|
|
|
+ case 2:
|
|
|
+ {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ layer.attn_norm,
|
|
|
+ layer.attn_norm_b,
|
|
|
+ LLM_NORM_GROUP, 0);
|
|
|
+
|
|
|
+ ggml_tensor * q;
|
|
|
+ ggml_tensor * k;
|
|
|
+ ggml_tensor * v;
|
|
|
+
|
|
|
+ q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
|
|
|
+ k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
|
|
|
+ v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
|
|
|
+
|
|
|
+ q = ggml_add(ctx0, q, layer.attn_q_b);
|
|
|
+ k = ggml_add(ctx0, k, layer.attn_k_b);
|
|
|
+ v = ggml_add(ctx0, v, layer.attn_v_b);
|
|
|
+
|
|
|
+ q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
|
|
|
+ k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
|
|
|
+
|
|
|
+ ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
|
|
|
+
|
|
|
+ kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
|
|
|
+
|
|
|
+ cur = ggml_mul_mat(ctx0, kq, v);
|
|
|
+
|
|
|
+ cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
|
|
|
+ cur = ggml_add(ctx0, cur, layer.attn_o_b);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, inpL);
|
|
|
+ } break;
|
|
|
+ case 5:
|
|
|
+ {
|
|
|
+ cur = build_norm(cur,
|
|
|
+ layer.norm,
|
|
|
+ layer.norm_b,
|
|
|
+ LLM_NORM_GROUP, 0);
|
|
|
+ } break;
|
|
|
+ default: GGML_ABORT("unknown posnet layer");
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.tok_norm,
|
|
|
+ model.tok_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
|
|
|
+
|
|
|
+ inpL = cur;
|
|
|
+
|
|
|
+ // convnext
|
|
|
+ for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
|
|
|
+ const auto & layer = model.layers[il].convnext;
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
|
|
|
+ cur = ggml_add(ctx0, cur, layer.dw_b);
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ layer.norm,
|
|
|
+ layer.norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ layer.pw1, layer.pw1_b, NULL,
|
|
|
+ NULL, NULL, NULL,
|
|
|
+ layer.pw2, layer.pw2_b, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_GELU, LLM_FFN_SEQ, il);
|
|
|
+
|
|
|
+ cur = ggml_mul(ctx0, cur, layer.gamma);
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
|
|
|
+
|
|
|
+ inpL = ggml_add(ctx0, cur, inpL);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = inpL;
|
|
|
+
|
|
|
+ cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
|
|
|
+
|
|
|
+ cur = build_norm(cur,
|
|
|
+ model.output_norm,
|
|
|
+ model.output_norm_b,
|
|
|
+ LLM_NORM, -1);
|
|
|
+
|
|
|
+ // lm_head
|
|
|
+ cur = build_lora_mm(model.output, cur);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, model.output_b);
|
|
|
+
|
|
|
+ cb(cur, "result_embd", -1);
|
|
|
+ res->t_embd = cur;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+llama_memory_i * llama_model::create_memory() const {
|
|
|
+ llama_memory_i * res;
|
|
|
+
|
|
|
+ switch (arch) {
|
|
|
+ case LLM_ARCH_RWKV6:
|
|
|
+ case LLM_ARCH_RWKV6QWEN2:
|
|
|
+ case LLM_ARCH_MAMBA:
|
|
|
+ {
|
|
|
+ res = new llama_kv_cache_unified(hparams, {
|
|
|
+ /*.get_rope_factors =*/ nullptr
|
|
|
+ });
|
|
|
+ } break;
|
|
|
+ default:
|
|
|
+ {
|
|
|
+ res = new llama_kv_cache_unified(hparams, {
|
|
|
+ /*.get_rope_factors =*/ [this](uint32_t n_ctx_per_seq, int il) {
|
|
|
+ // choose long/short freq factors based on the context size
|
|
|
+ if (layers[il].rope_freqs != nullptr) {
|
|
|
+ return layers[il].rope_freqs;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
|
|
|
+ return layers[il].rope_long;
|
|
|
+ }
|
|
|
+
|
|
|
+ return layers[il].rope_short;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+llm_graph_result_ptr llama_model::build_graph(
|
|
|
+ const llm_graph_params & params,
|
|
|
+ ggml_cgraph * gf,
|
|
|
+ llm_graph_type type) const {
|
|
|
+ std::unique_ptr<llm_graph_context> llm;
|
|
|
+
|
|
|
+ switch (arch) {
|
|
|
+ case LLM_ARCH_LLAMA:
|
|
|
+ case LLM_ARCH_MINICPM:
|
|
|
+ case LLM_ARCH_GRANITE:
|
|
|
+ case LLM_ARCH_GRANITE_MOE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_llama>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_DECI:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_deci>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_BAICHUAN:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_baichuan>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_FALCON:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_falcon>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GROK:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_grok>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_STARCODER:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_starcoder>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_REFACT:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_refact>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_BERT:
|
|
|
+ case LLM_ARCH_JINA_BERT_V2:
|
|
|
+ case LLM_ARCH_NOMIC_BERT:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_bert>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_BLOOM:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_bloom>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_MPT:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_mpt>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_STABLELM:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_stablelm>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_QWEN:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_qwen>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_QWEN2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_qwen2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_QWEN2VL:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_qwen2vl>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_QWEN2MOE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_qwen2moe>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_PHI2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_phi2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_PHI3:
|
|
|
+ case LLM_ARCH_PHIMOE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_phi3>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_PLAMO:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_plamo>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GPT2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_gpt2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_CODESHELL:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_codeshell>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_ORION:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_orion>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_INTERNLM2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_internlm2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_MINICPM3:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_minicpm3>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GEMMA:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_gemma>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GEMMA2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_gemma2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GEMMA3:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_gemma3>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_STARCODER2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_starcoder2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_MAMBA:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_mamba>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_XVERSE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_xverse>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_COMMAND_R:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_command_r>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_COHERE2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_cohere2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_DBRX:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_dbrx>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_OLMO:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_olmo>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_OLMO2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_olmo2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_OLMOE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_olmoe>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_OPENELM:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_openelm>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_GPTNEOX:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_gptneox>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_ARCTIC:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_arctic>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_DEEPSEEK:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_deepseek>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_DEEPSEEK2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_deepseek2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_CHATGLM:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_chatglm>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_BITNET:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_bitnet>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_T5:
|
|
|
+ {
|
|
|
+ switch (type) {
|
|
|
+ case LLM_GRAPH_TYPE_ENCODER:
|
|
|
+ llm = std::make_unique<llm_build_t5_enc>(*this, params, gf);
|
|
|
+ break;
|
|
|
+ case LLM_GRAPH_TYPE_DEFAULT:
|
|
|
+ case LLM_GRAPH_TYPE_DECODER:
|
|
|
+ llm = std::make_unique<llm_build_t5_dec>(*this, params, gf);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ GGML_ABORT("invalid graph type");
|
|
|
+ };
|
|
|
+ } break;
|
|
|
+ //case LLM_ARCH_T5ENCODER:
|
|
|
+ // {
|
|
|
+ // llm.build_t5_enc(gf);
|
|
|
+ // } break;
|
|
|
+ case LLM_ARCH_JAIS:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_jais>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_NEMOTRON:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_nemotron>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_EXAONE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_exaone>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_RWKV6:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_rwkv6>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_RWKV6QWEN2:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_CHAMELEON:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_chameleon>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ case LLM_ARCH_WAVTOKENIZER_DEC:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params, gf);
|
|
|
+ } break;
|
|
|
+ default:
|
|
|
+ GGML_ABORT("fatal error");
|
|
|
+ }
|
|
|
+
|
|
|
+ // add on pooling layer
|
|
|
+ llm->build_pooling(gf, cls, cls_b, cls_out, cls_out_b);
|
|
|
+
|
|
|
+ return std::move(llm->res);
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+// interface implementation
|
|
|
+//
|
|
|
+
|
|
|
+llama_model_params llama_model_default_params() {
|
|
|
+ llama_model_params result = {
|
|
|
+ /*.devices =*/ nullptr,
|
|
|
+ /*.n_gpu_layers =*/ 0,
|
|
|
+ /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
|
|
|
+ /*.main_gpu =*/ 0,
|
|
|
+ /*.tensor_split =*/ nullptr,
|
|
|
+ /*.progress_callback =*/ nullptr,
|
|
|
+ /*.progress_callback_user_data =*/ nullptr,
|
|
|
+ /*.kv_overrides =*/ nullptr,
|
|
|
+ /*.vocab_only =*/ false,
|
|
|
+ /*.use_mmap =*/ true,
|
|
|
+ /*.use_mlock =*/ false,
|
|
|
+ /*.check_tensors =*/ false,
|
|
|
+ };
|
|
|
+
|
|
|
+#ifdef GGML_USE_METAL
|
|
|
+ // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
|
|
|
+ result.n_gpu_layers = 999;
|
|
|
+#endif
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+const llama_vocab * llama_model_get_vocab(const llama_model * model) {
|
|
|
+ return &model->vocab;
|
|
|
+}
|
|
|
+
|
|
|
+void llama_free_model(llama_model * model) {
|
|
|
+ llama_model_free(model);
|
|
|
+}
|
|
|
+
|
|
|
+void llama_model_free(llama_model * model) {
|
|
|
+ delete model;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_n_ctx_train(const llama_model * model) {
|
|
|
+ return model->hparams.n_ctx_train;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_n_embd(const llama_model * model) {
|
|
|
+ return model->hparams.n_embd;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_n_layer(const llama_model * model) {
|
|
|
+ return model->hparams.n_layer;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_n_head(const llama_model * model) {
|
|
|
+ return model->hparams.n_head();
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_n_head_kv(const llama_model * model) {
|
|
|
+ return model->hparams.n_head_kv();
|
|
|
+}
|
|
|
+
|
|
|
+// deprecated
|
|
|
+int32_t llama_n_ctx_train(const llama_model * model) {
|
|
|
+ return llama_model_n_ctx_train(model);
|
|
|
+}
|
|
|
+
|
|
|
+// deprecated
|
|
|
+int32_t llama_n_embd(const llama_model * model) {
|
|
|
+ return llama_model_n_embd(model);
|
|
|
+}
|
|
|
+
|
|
|
+// deprecated
|
|
|
+int32_t llama_n_layer(const llama_model * model) {
|
|
|
+ return llama_model_n_layer(model);
|
|
|
+}
|
|
|
+
|
|
|
+// deprecated
|
|
|
+int32_t llama_n_head(const llama_model * model) {
|
|
|
+ return llama_model_n_head(model);
|
|
|
+}
|
|
|
+
|
|
|
+llama_rope_type llama_model_rope_type(const llama_model * model) {
|
|
|
+ switch (model->arch) {
|
|
|
+ // these models do not use RoPE
|
|
|
+ case LLM_ARCH_GPT2:
|
|
|
+ case LLM_ARCH_GPTJ:
|
|
|
+ case LLM_ARCH_MPT:
|
|
|
+ case LLM_ARCH_REFACT:
|
|
|
+ case LLM_ARCH_BLOOM:
|
|
|
+ case LLM_ARCH_MAMBA:
|
|
|
+ case LLM_ARCH_JINA_BERT_V2:
|
|
|
+ case LLM_ARCH_T5:
|
|
|
+ case LLM_ARCH_T5ENCODER:
|
|
|
+ case LLM_ARCH_JAIS:
|
|
|
+ case LLM_ARCH_RWKV6:
|
|
|
+ case LLM_ARCH_RWKV6QWEN2:
|
|
|
+ case LLM_ARCH_WAVTOKENIZER_DEC:
|
|
|
+ return LLAMA_ROPE_TYPE_NONE;
|
|
|
+
|
|
|
+ // use what we call a normal RoPE, operating on pairs of consecutive head values
|
|
|
+ case LLM_ARCH_LLAMA:
|
|
|
+ case LLM_ARCH_DECI:
|
|
|
+ case LLM_ARCH_BAICHUAN:
|
|
|
+ case LLM_ARCH_STARCODER:
|
|
|
+ case LLM_ARCH_PLAMO:
|
|
|
+ case LLM_ARCH_ORION:
|
|
|
+ case LLM_ARCH_INTERNLM2:
|
|
|
+ case LLM_ARCH_MINICPM:
|
|
|
+ case LLM_ARCH_XVERSE:
|
|
|
+ case LLM_ARCH_COMMAND_R:
|
|
|
+ case LLM_ARCH_COHERE2:
|
|
|
+ case LLM_ARCH_OLMO:
|
|
|
+ case LLM_ARCH_ARCTIC:
|
|
|
+ case LLM_ARCH_DEEPSEEK:
|
|
|
+ case LLM_ARCH_DEEPSEEK2:
|
|
|
+ case LLM_ARCH_CHATGLM:
|
|
|
+ case LLM_ARCH_GRANITE:
|
|
|
+ case LLM_ARCH_GRANITE_MOE:
|
|
|
+ case LLM_ARCH_CHAMELEON:
|
|
|
+ return LLAMA_ROPE_TYPE_NORM;
|
|
|
+
|
|
|
+ // the pairs of head values are offset by n_rot/2
|
|
|
+ case LLM_ARCH_FALCON:
|
|
|
+ case LLM_ARCH_GROK:
|
|
|
+ case LLM_ARCH_DBRX:
|
|
|
+ case LLM_ARCH_BERT:
|
|
|
+ case LLM_ARCH_NOMIC_BERT:
|
|
|
+ case LLM_ARCH_STABLELM:
|
|
|
+ case LLM_ARCH_BITNET:
|
|
|
+ case LLM_ARCH_QWEN:
|
|
|
+ case LLM_ARCH_QWEN2:
|
|
|
+ case LLM_ARCH_QWEN2MOE:
|
|
|
+ case LLM_ARCH_OLMO2:
|
|
|
+ case LLM_ARCH_OLMOE:
|
|
|
+ case LLM_ARCH_PHI2:
|
|
|
+ case LLM_ARCH_PHI3:
|
|
|
+ case LLM_ARCH_PHIMOE:
|
|
|
+ case LLM_ARCH_GEMMA:
|
|
|
+ case LLM_ARCH_GEMMA2:
|
|
|
+ case LLM_ARCH_GEMMA3:
|
|
|
+ case LLM_ARCH_STARCODER2:
|
|
|
+ case LLM_ARCH_OPENELM:
|
|
|
+ case LLM_ARCH_GPTNEOX:
|
|
|
+ case LLM_ARCH_CODESHELL:
|
|
|
+ case LLM_ARCH_NEMOTRON:
|
|
|
+ case LLM_ARCH_EXAONE:
|
|
|
+ case LLM_ARCH_MINICPM3:
|
|
|
+ return LLAMA_ROPE_TYPE_NEOX;
|
|
|
+
|
|
|
+ case LLM_ARCH_QWEN2VL:
|
|
|
+ return LLAMA_ROPE_TYPE_MROPE;
|
|
|
+
|
|
|
+ // all model arches should be listed explicitly here
|
|
|
+ case LLM_ARCH_UNKNOWN:
|
|
|
+ GGML_ABORT("unknown architecture");
|
|
|
+ }
|
|
|
+
|
|
|
+ return LLAMA_ROPE_TYPE_NONE;
|
|
|
+}
|
|
|
+
|
|
|
+float llama_model_rope_freq_scale_train(const llama_model * model) {
|
|
|
+ return model->hparams.rope_freq_scale_train;
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
|
|
|
+ const auto & it = model->gguf_kv.find(key);
|
|
|
+ if (it == model->gguf_kv.end()) {
|
|
|
+ if (buf_size > 0) {
|
|
|
+ buf[0] = '\0';
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return snprintf(buf, buf_size, "%s", it->second.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_meta_count(const llama_model * model) {
|
|
|
+ return (int)model->gguf_kv.size();
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
|
|
|
+ if (i < 0 || i >= (int)model->gguf_kv.size()) {
|
|
|
+ if (buf_size > 0) {
|
|
|
+ buf[0] = '\0';
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ auto it = model->gguf_kv.begin();
|
|
|
+ std::advance(it, i);
|
|
|
+ return snprintf(buf, buf_size, "%s", it->first.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
|
|
|
+ if (i < 0 || i >= (int)model->gguf_kv.size()) {
|
|
|
+ if (buf_size > 0) {
|
|
|
+ buf[0] = '\0';
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ auto it = model->gguf_kv.begin();
|
|
|
+ std::advance(it, i);
|
|
|
+ return snprintf(buf, buf_size, "%s", it->second.c_str());
|
|
|
+}
|
|
|
+
|
|
|
+int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
|
|
|
+ return snprintf(buf, buf_size, "%s", model->desc().c_str());
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t llama_model_size(const llama_model * model) {
|
|
|
+ return model->size();
|
|
|
+}
|
|
|
+
|
|
|
+const char * llama_model_chat_template(const llama_model * model, const char * name) {
|
|
|
+ const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE_N)
|
|
|
+ : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
|
|
|
+ const auto & it = model->gguf_kv.find(key);
|
|
|
+ if (it == model->gguf_kv.end()) {
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return it->second.c_str();
|
|
|
+}
|
|
|
+
|
|
|
+uint64_t llama_model_n_params(const llama_model * model) {
|
|
|
+ return model->n_elements();
|
|
|
+}
|
|
|
+
|
|
|
+bool llama_model_has_encoder(const llama_model * model) {
|
|
|
+ switch (model->arch) {
|
|
|
+ case LLM_ARCH_T5: return true;
|
|
|
+ case LLM_ARCH_T5ENCODER: return true;
|
|
|
+ default: return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+bool llama_model_has_decoder(const llama_model * model) {
|
|
|
+ switch (model->arch) {
|
|
|
+ case LLM_ARCH_T5ENCODER: return false;
|
|
|
+ default: return true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+llama_token llama_model_decoder_start_token(const llama_model * model) {
|
|
|
+ return model->hparams.dec_start_token_id;
|
|
|
+}
|
|
|
+
|
|
|
+bool llama_model_is_recurrent(const llama_model * model) {
|
|
|
+ switch (model->arch) {
|
|
|
+ case LLM_ARCH_MAMBA: return true;
|
|
|
+ case LLM_ARCH_RWKV6: return true;
|
|
|
+ case LLM_ARCH_RWKV6QWEN2: return true;
|
|
|
+ default: return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
|
|
|
+ return model->tensors_by_name;
|
|
|
}
|