mirror of
https://github.com/LostRuins/koboldcpp.git
synced 2026-09-19 17:25:07 +02:00
fossilize ggml library ver 3, to support ggjtv3
This commit is contained in:
+119
-119
@@ -1,4 +1,4 @@
|
||||
#include "ggml.h"
|
||||
#include "ggml_v3.h"
|
||||
#include "otherarch.h"
|
||||
|
||||
#include "utils.h"
|
||||
@@ -17,10 +17,10 @@
|
||||
#include "model_adapter.h"
|
||||
|
||||
#ifdef GGML_USE_CUBLAS
|
||||
#include "ggml-cuda.h"
|
||||
#include "ggml_v3-cuda.h"
|
||||
#endif
|
||||
#if defined(GGML_USE_CLBLAST)
|
||||
#include "ggml-opencl.h"
|
||||
#include "ggml_v3-opencl.h"
|
||||
#endif
|
||||
|
||||
// load the model's weights from a file
|
||||
@@ -58,7 +58,7 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
|
||||
hparams.n_ctx = std::min(hparams.max_seq_len, hparams.n_ctx);
|
||||
|
||||
const int32_t qntvr = hparams.ftype / GGML_QNT_VERSION_FACTOR;
|
||||
const int32_t qntvr = hparams.ftype / GGML_V3_QNT_VERSION_FACTOR;
|
||||
|
||||
printf("%s: d_model = %d\n", __func__, hparams.d_model);
|
||||
printf("%s: max_seq_len = %d\n", __func__, hparams.max_seq_len);
|
||||
@@ -71,7 +71,7 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
printf("%s: ftype = %d\n", __func__, hparams.ftype);
|
||||
printf("%s: qntvr = %d\n", __func__, qntvr);
|
||||
|
||||
hparams.ftype %= GGML_QNT_VERSION_FACTOR;
|
||||
hparams.ftype %= GGML_V3_QNT_VERSION_FACTOR;
|
||||
}
|
||||
|
||||
// load vocab
|
||||
@@ -107,8 +107,8 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
// for the big tensors, we have the option to store the data in 16-bit
|
||||
// floats or quantized in order to save memory and also to speed up the
|
||||
// computation
|
||||
ggml_type wtype = ggml_ftype_to_ggml_type((ggml_ftype)(model.hparams.ftype));
|
||||
if (wtype == GGML_TYPE_COUNT) {
|
||||
ggml_v3_type wtype = ggml_v3_ftype_to_ggml_v3_type((ggml_v3_ftype)(model.hparams.ftype));
|
||||
if (wtype == GGML_V3_TYPE_COUNT) {
|
||||
fprintf(stderr, "%s: invalid model file '%s' (bad ftype value %d)\n", __func__, fname.c_str(),
|
||||
model.hparams.ftype);
|
||||
return false;
|
||||
@@ -126,18 +126,18 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
const size_t n_layer = hparams.n_layers;
|
||||
const size_t n_vocab = hparams.n_vocab;
|
||||
|
||||
ctx_size += n_embd * n_vocab * ggml_type_sizef(wtype); // wte_weight
|
||||
ctx_size += n_embd * ggml_type_sizef(GGML_TYPE_F32); // norm_f_weight
|
||||
ctx_size += n_embd * n_vocab * ggml_v3_type_sizef(wtype); // wte_weight
|
||||
ctx_size += n_embd * ggml_v3_type_sizef(GGML_V3_TYPE_F32); // norm_f_weight
|
||||
|
||||
ctx_size += n_layer * (n_embd * ggml_type_sizef(GGML_TYPE_F32)); // ln_1_weight
|
||||
ctx_size += n_layer * (3 * n_embd * n_embd * ggml_type_sizef(wtype)); // attn_Wqkv_weight
|
||||
ctx_size += n_layer * (n_embd * n_embd * ggml_type_sizef(wtype)); // attn_out_proj_weight
|
||||
ctx_size += n_layer * (n_embd * ggml_type_sizef(GGML_TYPE_F32)); // ln_2_weight
|
||||
ctx_size += n_layer * (4 * n_embd * n_embd * ggml_type_sizef(wtype)); // mlp_mlp_up_weight
|
||||
ctx_size += n_layer * (n_embd * n_embd * 4 * ggml_type_sizef(wtype)); // mlp_mlp_down_weight
|
||||
ctx_size += n_layer * (n_embd * ggml_v3_type_sizef(GGML_V3_TYPE_F32)); // ln_1_weight
|
||||
ctx_size += n_layer * (3 * n_embd * n_embd * ggml_v3_type_sizef(wtype)); // attn_Wqkv_weight
|
||||
ctx_size += n_layer * (n_embd * n_embd * ggml_v3_type_sizef(wtype)); // attn_out_proj_weight
|
||||
ctx_size += n_layer * (n_embd * ggml_v3_type_sizef(GGML_V3_TYPE_F32)); // ln_2_weight
|
||||
ctx_size += n_layer * (4 * n_embd * n_embd * ggml_v3_type_sizef(wtype)); // mlp_mlp_up_weight
|
||||
ctx_size += n_layer * (n_embd * n_embd * 4 * ggml_v3_type_sizef(wtype)); // mlp_mlp_down_weight
|
||||
|
||||
ctx_size += n_ctx * n_layer * n_embd * ggml_type_sizef(GGML_TYPE_F16); // memory_k
|
||||
ctx_size += n_ctx * n_layer * n_embd * ggml_type_sizef(GGML_TYPE_F16); // memory_v
|
||||
ctx_size += n_ctx * n_layer * n_embd * ggml_v3_type_sizef(GGML_V3_TYPE_F16); // memory_k
|
||||
ctx_size += n_ctx * n_layer * n_embd * ggml_v3_type_sizef(GGML_V3_TYPE_F16); // memory_v
|
||||
|
||||
ctx_size += (6 + 6 * n_layer) * 512; // object overhead
|
||||
|
||||
@@ -146,14 +146,14 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
|
||||
// create the ggml context
|
||||
{
|
||||
struct ggml_init_params params;
|
||||
struct ggml_v3_init_params params;
|
||||
params.mem_size = ctx_size;
|
||||
params.mem_buffer = NULL;
|
||||
params.no_alloc = false;
|
||||
|
||||
model.ctx = ggml_init(params);
|
||||
model.ctx = ggml_v3_init(params);
|
||||
if (!model.ctx) {
|
||||
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
|
||||
fprintf(stderr, "%s: ggml_v3_init() failed\n", __func__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -168,8 +168,8 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
|
||||
model.layers.resize(n_layer);
|
||||
|
||||
model.wte_weight = ggml_new_tensor_2d(ctx, wtype, n_embd, n_vocab);
|
||||
model.norm_f_weight = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
|
||||
model.wte_weight = ggml_v3_new_tensor_2d(ctx, wtype, n_embd, n_vocab);
|
||||
model.norm_f_weight = ggml_v3_new_tensor_1d(ctx, GGML_V3_TYPE_F32, n_embd);
|
||||
|
||||
// map by name
|
||||
model.tensors["transformer.wte.weight"] = model.wte_weight;
|
||||
@@ -178,12 +178,12 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
for (int i = 0; i < (int) n_layer; ++i) {
|
||||
auto & layer = model.layers[i];
|
||||
|
||||
layer.norm_1_weight = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
|
||||
layer.c_attn_wqkv_weight = ggml_new_tensor_2d(ctx, wtype, n_embd, 3 * n_embd);
|
||||
layer.c_attn_out_proj_weight = ggml_new_tensor_2d(ctx, wtype, n_embd, n_embd);
|
||||
layer.norm_2_weight = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd);
|
||||
layer.ffn_up_proj = ggml_new_tensor_2d(ctx, wtype, n_embd, 4 * n_embd);
|
||||
layer.ffn_down_proj = ggml_new_tensor_2d(ctx, wtype, 4 * n_embd, n_embd);
|
||||
layer.norm_1_weight = ggml_v3_new_tensor_1d(ctx, GGML_V3_TYPE_F32, n_embd);
|
||||
layer.c_attn_wqkv_weight = ggml_v3_new_tensor_2d(ctx, wtype, n_embd, 3 * n_embd);
|
||||
layer.c_attn_out_proj_weight = ggml_v3_new_tensor_2d(ctx, wtype, n_embd, n_embd);
|
||||
layer.norm_2_weight = ggml_v3_new_tensor_1d(ctx, GGML_V3_TYPE_F32, n_embd);
|
||||
layer.ffn_up_proj = ggml_v3_new_tensor_2d(ctx, wtype, n_embd, 4 * n_embd);
|
||||
layer.ffn_down_proj = ggml_v3_new_tensor_2d(ctx, wtype, 4 * n_embd, n_embd);
|
||||
|
||||
// map by name
|
||||
model.tensors["transformer.blocks." + std::to_string(i) + ".norm_1.weight"] = layer.norm_1_weight;
|
||||
@@ -205,10 +205,10 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
const int64_t n_mem = n_layer * n_ctx;
|
||||
const int64_t n_elements = n_embd * n_mem;
|
||||
|
||||
model.memory_k = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, n_elements);
|
||||
model.memory_v = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, n_elements);
|
||||
model.memory_k = ggml_v3_new_tensor_1d(ctx, GGML_V3_TYPE_F16, n_elements);
|
||||
model.memory_v = ggml_v3_new_tensor_1d(ctx, GGML_V3_TYPE_F16, n_elements);
|
||||
|
||||
const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v);
|
||||
const size_t memory_size = ggml_v3_nbytes(model.memory_k) + ggml_v3_nbytes(model.memory_v);
|
||||
|
||||
printf("%s: memory_size = %8.2f MB, n_mem = %" PRId64 "\n", __func__, memory_size / 1024.0 / 1024.0, n_mem);
|
||||
}
|
||||
@@ -249,7 +249,7 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
}
|
||||
|
||||
auto tensor = model.tensors[name.data()];
|
||||
if (ggml_nelements(tensor) != nelements) {
|
||||
if (ggml_v3_nelements(tensor) != nelements) {
|
||||
fprintf(stderr, "%s: tensor '%s' has wrong size in model file\n", __func__, name.data());
|
||||
return false;
|
||||
}
|
||||
@@ -265,22 +265,22 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
// for debugging
|
||||
if (0) {
|
||||
printf("%24s - [%5d, %5d], type = %6s, %6.2f MB, %9zu bytes\n", name.data(), ne[0], ne[1],
|
||||
ggml_type_name(ggml_type(ttype)), ggml_nbytes(tensor) / 1024.0 / 1024.0, ggml_nbytes(tensor));
|
||||
ggml_v3_type_name(ggml_v3_type(ttype)), ggml_v3_nbytes(tensor) / 1024.0 / 1024.0, ggml_v3_nbytes(tensor));
|
||||
}
|
||||
|
||||
const size_t bpe = ggml_type_size(ggml_type(ttype));
|
||||
const size_t bpe = ggml_v3_type_size(ggml_v3_type(ttype));
|
||||
|
||||
if ((nelements * bpe) / ggml_blck_size(tensor->type) != ggml_nbytes(tensor)) {
|
||||
if ((nelements * bpe) / ggml_v3_blck_size(tensor->type) != ggml_v3_nbytes(tensor)) {
|
||||
fprintf(stderr,
|
||||
"%s: tensor '%s' has wrong size in model file: got %zu, "
|
||||
"expected %zu\n",
|
||||
__func__, name.data(), ggml_nbytes(tensor), nelements * bpe);
|
||||
__func__, name.data(), ggml_v3_nbytes(tensor), nelements * bpe);
|
||||
return false;
|
||||
}
|
||||
|
||||
fin.read(reinterpret_cast<char *>(tensor->data), ggml_nbytes(tensor));
|
||||
fin.read(reinterpret_cast<char *>(tensor->data), ggml_v3_nbytes(tensor));
|
||||
|
||||
total_size += ggml_nbytes(tensor);
|
||||
total_size += ggml_v3_nbytes(tensor);
|
||||
if (++n_tensors % 8 == 0) {
|
||||
printf(".");
|
||||
fflush(stdout);
|
||||
@@ -308,20 +308,20 @@ bool mpt_model_load(const std::string & fname, mpt_model & model, gpt_vocab & vo
|
||||
#endif
|
||||
for (int i = 0; i < n_gpu; ++i) {
|
||||
const auto & layer = model.layers[i];
|
||||
layer.ffn_up_proj->backend = GGML_BACKEND_GPU;
|
||||
layer.ffn_down_proj->backend = GGML_BACKEND_GPU;
|
||||
layer.c_attn_wqkv_weight->backend = GGML_BACKEND_GPU;
|
||||
layer.c_attn_out_proj_weight->backend = GGML_BACKEND_GPU;
|
||||
layer.ffn_up_proj->backend = GGML_V3_BACKEND_GPU;
|
||||
layer.ffn_down_proj->backend = GGML_V3_BACKEND_GPU;
|
||||
layer.c_attn_wqkv_weight->backend = GGML_V3_BACKEND_GPU;
|
||||
layer.c_attn_out_proj_weight->backend = GGML_V3_BACKEND_GPU;
|
||||
#if defined(GGML_USE_CLBLAST)
|
||||
ggml_cl_transform_tensor(layer.ffn_up_proj->data,layer.ffn_up_proj); vram_total += ggml_nbytes(layer.ffn_up_proj);
|
||||
ggml_cl_transform_tensor(layer.ffn_down_proj->data,layer.ffn_down_proj); vram_total += ggml_nbytes(layer.ffn_down_proj);
|
||||
ggml_cl_transform_tensor(layer.c_attn_wqkv_weight->data,layer.c_attn_wqkv_weight); vram_total += ggml_nbytes(layer.c_attn_wqkv_weight);
|
||||
ggml_cl_transform_tensor(layer.c_attn_out_proj_weight->data,layer.c_attn_out_proj_weight); vram_total += ggml_nbytes(layer.c_attn_out_proj_weight);
|
||||
ggml_v3_cl_transform_tensor(layer.ffn_up_proj->data,layer.ffn_up_proj); vram_total += ggml_v3_nbytes(layer.ffn_up_proj);
|
||||
ggml_v3_cl_transform_tensor(layer.ffn_down_proj->data,layer.ffn_down_proj); vram_total += ggml_v3_nbytes(layer.ffn_down_proj);
|
||||
ggml_v3_cl_transform_tensor(layer.c_attn_wqkv_weight->data,layer.c_attn_wqkv_weight); vram_total += ggml_v3_nbytes(layer.c_attn_wqkv_weight);
|
||||
ggml_v3_cl_transform_tensor(layer.c_attn_out_proj_weight->data,layer.c_attn_out_proj_weight); vram_total += ggml_v3_nbytes(layer.c_attn_out_proj_weight);
|
||||
#else
|
||||
ggml_cuda_transform_tensor(layer.ffn_up_proj->data,layer.ffn_up_proj); vram_total += ggml_nbytes(layer.ffn_up_proj);
|
||||
ggml_cuda_transform_tensor(layer.ffn_down_proj->data,layer.ffn_down_proj); vram_total += ggml_nbytes(layer.ffn_down_proj);
|
||||
ggml_cuda_transform_tensor(layer.c_attn_wqkv_weight->data,layer.c_attn_wqkv_weight); vram_total += ggml_nbytes(layer.c_attn_wqkv_weight);
|
||||
ggml_cuda_transform_tensor(layer.c_attn_out_proj_weight->data,layer.c_attn_out_proj_weight); vram_total += ggml_nbytes(layer.c_attn_out_proj_weight);
|
||||
ggml_v3_cuda_transform_tensor(layer.ffn_up_proj->data,layer.ffn_up_proj); vram_total += ggml_v3_nbytes(layer.ffn_up_proj);
|
||||
ggml_v3_cuda_transform_tensor(layer.ffn_down_proj->data,layer.ffn_down_proj); vram_total += ggml_v3_nbytes(layer.ffn_down_proj);
|
||||
ggml_v3_cuda_transform_tensor(layer.c_attn_wqkv_weight->data,layer.c_attn_wqkv_weight); vram_total += ggml_v3_nbytes(layer.c_attn_wqkv_weight);
|
||||
ggml_v3_cuda_transform_tensor(layer.c_attn_out_proj_weight->data,layer.c_attn_out_proj_weight); vram_total += ggml_v3_nbytes(layer.c_attn_out_proj_weight);
|
||||
#endif
|
||||
}
|
||||
#if defined(GGML_USE_CLBLAST)
|
||||
@@ -384,32 +384,32 @@ bool mpt_eval(const mpt_model & model, const int n_threads, const int n_past,
|
||||
}
|
||||
}
|
||||
|
||||
struct ggml_init_params params;
|
||||
struct ggml_v3_init_params params;
|
||||
params.mem_size = buf_size;
|
||||
params.mem_buffer = buf;
|
||||
params.no_alloc = false;
|
||||
|
||||
struct ggml_context * ctx0 = ggml_init(params);
|
||||
struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, GGML_MAX_NODES, false);
|
||||
struct ggml_v3_context * ctx0 = ggml_v3_init(params);
|
||||
struct ggml_v3_cgraph * gf = ggml_v3_new_graph_custom(ctx0, GGML_V3_MAX_NODES, false);
|
||||
|
||||
struct ggml_tensor * embd = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
|
||||
memcpy(embd->data, embd_inp.data(), N * ggml_element_size(embd));
|
||||
struct ggml_v3_tensor * embd = ggml_v3_new_tensor_1d(ctx0, GGML_V3_TYPE_I32, N);
|
||||
memcpy(embd->data, embd_inp.data(), N * ggml_v3_element_size(embd));
|
||||
|
||||
struct ggml_tensor * inpL = ggml_get_rows(ctx0, model.wte_weight, embd);
|
||||
struct ggml_v3_tensor * inpL = ggml_v3_get_rows(ctx0, model.wte_weight, embd);
|
||||
|
||||
for (int il = 0; il < n_layer; ++il) {
|
||||
|
||||
struct ggml_tensor * cur;
|
||||
struct ggml_v3_tensor * cur;
|
||||
|
||||
if(use_scratch){
|
||||
ggml_set_scratch(ctx0, { 0, scr0_size, scr0, });
|
||||
ggml_v3_set_scratch(ctx0, { 0, scr0_size, scr0, });
|
||||
}
|
||||
|
||||
// a = self.ln_1(x)
|
||||
{
|
||||
cur = ggml_norm(ctx0, inpL, default_norm_eps);
|
||||
cur = ggml_v3_norm(ctx0, inpL, default_norm_eps);
|
||||
|
||||
cur = ggml_mul(ctx0, ggml_repeat(ctx0, model.layers[il].norm_1_weight, cur), cur);
|
||||
cur = ggml_v3_mul(ctx0, ggml_v3_repeat(ctx0, model.layers[il].norm_1_weight, cur), cur);
|
||||
}
|
||||
|
||||
// self-attention
|
||||
@@ -418,164 +418,164 @@ bool mpt_eval(const mpt_model & model, const int n_threads, const int n_past,
|
||||
// is_causal=is_causal)
|
||||
{
|
||||
// compute QKV
|
||||
cur = ggml_mul_mat(ctx0, model.layers[il].c_attn_wqkv_weight, cur);
|
||||
cur = ggml_v3_mul_mat(ctx0, model.layers[il].c_attn_wqkv_weight, cur);
|
||||
|
||||
if (model.hparams.clip_qkv > 0.0f) {
|
||||
cur = ggml_clamp(ctx0, cur, -model.hparams.clip_qkv, model.hparams.clip_qkv);
|
||||
cur = ggml_v3_clamp(ctx0, cur, -model.hparams.clip_qkv, model.hparams.clip_qkv);
|
||||
}
|
||||
|
||||
struct ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 0 * sizeof(float) * n_embd);
|
||||
struct ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 1 * sizeof(float) * n_embd);
|
||||
struct ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 2 * sizeof(float) * n_embd);
|
||||
struct ggml_v3_tensor * Qcur = ggml_v3_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 0 * sizeof(float) * n_embd);
|
||||
struct ggml_v3_tensor * Kcur = ggml_v3_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 1 * sizeof(float) * n_embd);
|
||||
struct ggml_v3_tensor * Vcur = ggml_v3_view_2d(ctx0, cur, n_embd, N, cur->nb[1], 2 * sizeof(float) * n_embd);
|
||||
|
||||
// store key and value to memory
|
||||
{
|
||||
struct ggml_tensor * k =
|
||||
ggml_view_1d(ctx0, model.memory_k, N * n_embd,
|
||||
(ggml_element_size(model.memory_k) * n_embd) * (il * n_ctx + n_past));
|
||||
struct ggml_tensor * v =
|
||||
ggml_view_1d(ctx0, model.memory_v, N * n_embd,
|
||||
(ggml_element_size(model.memory_v) * n_embd) * (il * n_ctx + n_past));
|
||||
struct ggml_v3_tensor * k =
|
||||
ggml_v3_view_1d(ctx0, model.memory_k, N * n_embd,
|
||||
(ggml_v3_element_size(model.memory_k) * n_embd) * (il * n_ctx + n_past));
|
||||
struct ggml_v3_tensor * v =
|
||||
ggml_v3_view_1d(ctx0, model.memory_v, N * n_embd,
|
||||
(ggml_v3_element_size(model.memory_v) * n_embd) * (il * n_ctx + n_past));
|
||||
|
||||
ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, k));
|
||||
ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, v));
|
||||
ggml_v3_build_forward_expand(gf, ggml_v3_cpy(ctx0, Kcur, k));
|
||||
ggml_v3_build_forward_expand(gf, ggml_v3_cpy(ctx0, Vcur, v));
|
||||
}
|
||||
|
||||
// Q = Qcur.contiguous().view(n_embd/n_head, n_head, N).permute(0,
|
||||
// 2, 1, 3) [64, N, 12]
|
||||
struct ggml_tensor * Q = ggml_permute(
|
||||
ctx0, ggml_cpy(ctx0, Qcur, ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_embd / n_head, n_head, N)), 0, 2,
|
||||
struct ggml_v3_tensor * Q = ggml_v3_permute(
|
||||
ctx0, ggml_v3_cpy(ctx0, Qcur, ggml_v3_new_tensor_3d(ctx0, GGML_V3_TYPE_F32, n_embd / n_head, n_head, N)), 0, 2,
|
||||
1, 3);
|
||||
|
||||
// K = Kmem.view(n_embd/n_head, n_head, n_past + N).permute(0, 2, 1,
|
||||
// 3) [64, n_past + N, 12]
|
||||
struct ggml_tensor * K =
|
||||
ggml_permute(ctx0,
|
||||
ggml_reshape_3d(ctx0,
|
||||
ggml_view_1d(ctx0, model.memory_k, (n_past + N) * n_embd,
|
||||
il * n_ctx * ggml_element_size(model.memory_k) * n_embd),
|
||||
struct ggml_v3_tensor * K =
|
||||
ggml_v3_permute(ctx0,
|
||||
ggml_v3_reshape_3d(ctx0,
|
||||
ggml_v3_view_1d(ctx0, model.memory_k, (n_past + N) * n_embd,
|
||||
il * n_ctx * ggml_v3_element_size(model.memory_k) * n_embd),
|
||||
n_embd / n_head, n_head, n_past + N),
|
||||
0, 2, 1, 3);
|
||||
// K * Q
|
||||
struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
|
||||
struct ggml_v3_tensor * KQ = ggml_v3_mul_mat(ctx0, K, Q);
|
||||
|
||||
// KQ_scaled = KQ / sqrt(n_embd/n_head)
|
||||
struct ggml_tensor * KQ_scaled =
|
||||
ggml_scale(ctx0, KQ, 1.0f / sqrt(float(n_embd) / n_head));
|
||||
struct ggml_v3_tensor * KQ_scaled =
|
||||
ggml_v3_scale(ctx0, KQ, 1.0f / sqrt(float(n_embd) / n_head));
|
||||
|
||||
struct ggml_tensor * KQ_scaled_alibi =
|
||||
ggml_alibi(ctx0, KQ_scaled, n_past, n_head, model.hparams.alibi_bias_max);
|
||||
struct ggml_v3_tensor * KQ_scaled_alibi =
|
||||
ggml_v3_alibi(ctx0, KQ_scaled, n_past, n_head, model.hparams.alibi_bias_max);
|
||||
|
||||
// KQ_masked = mask_past(KQ_scaled)
|
||||
struct ggml_tensor * KQ_masked = ggml_diag_mask_inf(ctx0, KQ_scaled_alibi, n_past);
|
||||
struct ggml_v3_tensor * KQ_masked = ggml_v3_diag_mask_inf(ctx0, KQ_scaled_alibi, n_past);
|
||||
|
||||
// KQ = soft_max(KQ_masked)
|
||||
struct ggml_tensor * KQ_soft_max = ggml_soft_max(ctx0, KQ_masked);
|
||||
struct ggml_v3_tensor * KQ_soft_max = ggml_v3_soft_max(ctx0, KQ_masked);
|
||||
|
||||
// V_trans = Vmem.view(n_embd/n_head, n_head, n_past + N).permute(1,
|
||||
// 2, 0, 3).contiguous() [n_past + N, 64, 12]
|
||||
struct ggml_tensor * V_trans = ggml_cpy(
|
||||
struct ggml_v3_tensor * V_trans = ggml_v3_cpy(
|
||||
ctx0,
|
||||
ggml_permute(ctx0,
|
||||
ggml_reshape_3d(ctx0,
|
||||
ggml_view_1d(ctx0, model.memory_v, (n_past + N) * n_embd,
|
||||
il * n_ctx * ggml_element_size(model.memory_v) * n_embd),
|
||||
ggml_v3_permute(ctx0,
|
||||
ggml_v3_reshape_3d(ctx0,
|
||||
ggml_v3_view_1d(ctx0, model.memory_v, (n_past + N) * n_embd,
|
||||
il * n_ctx * ggml_v3_element_size(model.memory_v) * n_embd),
|
||||
n_embd / n_head, n_head, n_past + N),
|
||||
1, 2, 0, 3),
|
||||
ggml_new_tensor_3d(ctx0, model.memory_v->type, n_past + N, n_embd / n_head, n_head));
|
||||
ggml_v3_new_tensor_3d(ctx0, model.memory_v->type, n_past + N, n_embd / n_head, n_head));
|
||||
|
||||
// KQV = transpose(V) * KQ_soft_max
|
||||
struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V_trans, KQ_soft_max);
|
||||
struct ggml_v3_tensor * KQV = ggml_v3_mul_mat(ctx0, V_trans, KQ_soft_max);
|
||||
|
||||
// KQV_merged = KQV.permute(0, 2, 1, 3)
|
||||
struct ggml_tensor * KQV_merged = ggml_permute(ctx0, KQV, 0, 2, 1, 3);
|
||||
struct ggml_v3_tensor * KQV_merged = ggml_v3_permute(ctx0, KQV, 0, 2, 1, 3);
|
||||
|
||||
// cur = KQV_merged.contiguous().view(n_embd, N)
|
||||
cur = ggml_cpy(ctx0, KQV_merged, ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, N));
|
||||
cur = ggml_v3_cpy(ctx0, KQV_merged, ggml_v3_new_tensor_2d(ctx0, GGML_V3_TYPE_F32, n_embd, N));
|
||||
|
||||
// projection
|
||||
{ cur = ggml_mul_mat(ctx0, model.layers[il].c_attn_out_proj_weight, cur); }
|
||||
{ cur = ggml_v3_mul_mat(ctx0, model.layers[il].c_attn_out_proj_weight, cur); }
|
||||
}
|
||||
|
||||
inpL = ggml_add(ctx0, inpL, cur);
|
||||
inpL = ggml_v3_add(ctx0, inpL, cur);
|
||||
|
||||
if(use_scratch){
|
||||
ggml_set_scratch(ctx0, { 0, scr1_size, scr1, });
|
||||
ggml_v3_set_scratch(ctx0, { 0, scr1_size, scr1, });
|
||||
}
|
||||
|
||||
// m = self.ln_2(x)
|
||||
{
|
||||
cur = ggml_norm(ctx0, inpL, default_norm_eps);
|
||||
cur = ggml_v3_norm(ctx0, inpL, default_norm_eps);
|
||||
|
||||
cur = ggml_mul(ctx0, ggml_repeat(ctx0, model.layers[il].norm_2_weight, cur), cur);
|
||||
cur = ggml_v3_mul(ctx0, ggml_v3_repeat(ctx0, model.layers[il].norm_2_weight, cur), cur);
|
||||
}
|
||||
|
||||
// n = self.mlp(m)
|
||||
{
|
||||
|
||||
cur = ggml_mul_mat(ctx0, model.layers[il].ffn_up_proj, cur);
|
||||
cur = ggml_v3_mul_mat(ctx0, model.layers[il].ffn_up_proj, cur);
|
||||
|
||||
// GELU activation
|
||||
cur = ggml_gelu(ctx0, cur);
|
||||
cur = ggml_v3_gelu(ctx0, cur);
|
||||
|
||||
// projection
|
||||
// cur = proj_w*cur + proj_b
|
||||
cur = ggml_mul_mat(ctx0, model.layers[il].ffn_down_proj, cur);
|
||||
cur = ggml_v3_mul_mat(ctx0, model.layers[il].ffn_down_proj, cur);
|
||||
}
|
||||
|
||||
// x = x + n
|
||||
inpL = ggml_add(ctx0, inpL, cur);
|
||||
inpL = ggml_v3_add(ctx0, inpL, cur);
|
||||
}
|
||||
|
||||
if(use_scratch){
|
||||
ggml_set_scratch(ctx0, { 0, scr0_size, scr0, });
|
||||
ggml_v3_set_scratch(ctx0, { 0, scr0_size, scr0, });
|
||||
}
|
||||
|
||||
// norm
|
||||
{
|
||||
inpL = ggml_norm(ctx0, inpL, default_norm_eps);
|
||||
inpL = ggml_v3_norm(ctx0, inpL, default_norm_eps);
|
||||
// inpL = ln_f_g*inpL
|
||||
inpL = ggml_mul(ctx0, ggml_repeat(ctx0, model.norm_f_weight, inpL), inpL);
|
||||
inpL = ggml_v3_mul(ctx0, ggml_v3_repeat(ctx0, model.norm_f_weight, inpL), inpL);
|
||||
}
|
||||
|
||||
if(use_scratch){
|
||||
ggml_set_scratch(ctx0, { 0, 0, nullptr, });
|
||||
ggml_v3_set_scratch(ctx0, { 0, 0, nullptr, });
|
||||
}
|
||||
|
||||
// output embedding weight tied to input embedding
|
||||
inpL = ggml_mul_mat(ctx0, model.wte_weight, inpL);
|
||||
inpL = ggml_v3_mul_mat(ctx0, model.wte_weight, inpL);
|
||||
|
||||
// logits -> probs
|
||||
// inpL = ggml_soft_max(ctx0, inpL);
|
||||
// inpL = ggml_v3_soft_max(ctx0, inpL);
|
||||
|
||||
// run the computation
|
||||
ggml_build_forward_expand(gf, inpL);
|
||||
ggml_v3_build_forward_expand(gf, inpL);
|
||||
kcpp_graph_compute_helper(gf, n_threads);
|
||||
|
||||
// std::cout << "Qcur" << std::endl;
|
||||
// print_tensor(Qcur);
|
||||
|
||||
// if (n_past%100 == 0) {
|
||||
// ggml_graph_print(&gf);
|
||||
// ggml_graph_dump_dot(&gf, NULL, "mpt-model.dot");
|
||||
// ggml_v3_graph_print(&gf);
|
||||
// ggml_v3_graph_dump_dot(&gf, NULL, "mpt-model.dot");
|
||||
// }
|
||||
|
||||
if (logits_all) {
|
||||
// return result for all tokens
|
||||
embd_w.resize(n_vocab *N);
|
||||
memcpy(embd_w.data(), (float *)ggml_get_data(inpL) , sizeof(float) * n_vocab * N);
|
||||
memcpy(embd_w.data(), (float *)ggml_v3_get_data(inpL) , sizeof(float) * n_vocab * N);
|
||||
} else {
|
||||
// return result for just the last token
|
||||
embd_w.resize(n_vocab);
|
||||
memcpy(embd_w.data(), (float *)ggml_get_data(inpL) + (n_vocab * (N - 1)), sizeof(float) * n_vocab);
|
||||
memcpy(embd_w.data(), (float *)ggml_v3_get_data(inpL) + (n_vocab * (N - 1)), sizeof(float) * n_vocab);
|
||||
}
|
||||
|
||||
if (mem_per_token == 0) {
|
||||
mem_per_token = ggml_used_mem(ctx0) / N;
|
||||
mem_per_token = ggml_v3_used_mem(ctx0) / N;
|
||||
}
|
||||
// printf("used_mem = %zu\n", ggml_used_mem(ctx0));
|
||||
// printf("used_mem = %zu\n", ggml_v3_used_mem(ctx0));
|
||||
|
||||
ggml_free(ctx0);
|
||||
ggml_v3_free(ctx0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user