Skip to content

Commit cdeebe5

Browse files
committed
llama : hparams
ggml-ci
1 parent 48ead50 commit cdeebe5

7 files changed

+212
-178
lines changed

src/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ llama_add_compile_flags()
99
add_library(llama
1010
../include/llama.h
1111
llama.cpp
12+
llama-adapter.cpp
1213
llama-arch.cpp
1314
llama-batch.cpp
1415
llama-chat.cpp
1516
llama-context.cpp
16-
llama-adapter.cpp
17+
llama-hparams.cpp
1718
llama-grammar.cpp
1819
llama-kv-cache.cpp
1920
llama-mmap.cpp

src/llama-adapter.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
#include "llama-model.h" // TODO: need only hparams
77

8-
#include <vector>
9-
#include <map>
108
#include <algorithm>
9+
#include <cassert>
10+
#include <map>
11+
#include <vector>
1112

1213
//
1314
// llama_adapter_vec

src/llama-chat.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string>
44
#include <vector>
5+
#include <cstdint>
56

67
enum llm_chat_template {
78
LLM_CHAT_TEMPLATE_CHATML,

src/llama-hparams.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "llama-hparams.h"
2+
3+
#include "ggml.h"
4+
5+
uint32_t llama_hparams::n_head(uint32_t il) const {
6+
if (il < n_layer) {
7+
return n_head_arr[il];
8+
}
9+
10+
GGML_ABORT("fatal error");
11+
}
12+
13+
uint32_t llama_hparams::n_head_kv(uint32_t il) const {
14+
if (il < n_layer) {
15+
return n_head_kv_arr[il];
16+
}
17+
18+
GGML_ABORT("fatal error");
19+
}
20+
21+
uint32_t llama_hparams::n_ff(uint32_t il) const {
22+
if (il < n_layer) {
23+
return n_ff_arr[il];
24+
}
25+
26+
GGML_ABORT("fatal error");
27+
}
28+
29+
uint32_t llama_hparams::n_gqa(uint32_t il) const {
30+
const uint32_t n_head = this->n_head(il);
31+
const uint32_t n_head_kv = this->n_head_kv(il);
32+
33+
if (n_head_kv == 0) {
34+
return 0;
35+
}
36+
37+
return n_head/n_head_kv;
38+
}
39+
40+
uint32_t llama_hparams::n_embd_k_gqa(uint32_t il) const {
41+
const uint32_t n_head_kv = this->n_head_kv(il);
42+
43+
return n_embd_head_k * n_head_kv;
44+
}
45+
46+
uint32_t llama_hparams::n_embd_v_gqa(uint32_t il) const {
47+
const uint32_t n_head_kv = this->n_head_kv(il);
48+
49+
return n_embd_head_v * n_head_kv;
50+
}
51+
52+
uint32_t llama_hparams::n_embd_k_s() const {
53+
if (wkv_head_size != 0) {
54+
// for RWKV models
55+
return 2 * n_embd;
56+
}
57+
58+
// TODO: maybe support other convolution strides than 1
59+
// NOTE: since the first column of the conv_state is shifted out each time, it's not actually needed
60+
return (ssm_d_conv > 0 ? ssm_d_conv - 1 : 0) * ssm_d_inner;
61+
}
62+
63+
uint32_t llama_hparams::n_embd_v_s() const {
64+
if (wkv_head_size != 0) {
65+
// corresponds to RWKV's wkv_states size
66+
return n_embd * wkv_head_size;
67+
}
68+
69+
// corresponds to Mamba's ssm_states size
70+
return ssm_d_state * ssm_d_inner;
71+
}

src/llama-hparams.h

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#pragma once
2+
3+
#include "llama.h"
4+
5+
#include <array>
6+
7+
// bump if necessary
8+
#define LLAMA_MAX_LAYERS 512
9+
#define LLAMA_MAX_EXPERTS 160 // DeepSeekV2
10+
11+
struct llama_hparams_posnet {
12+
uint32_t n_embd;
13+
uint32_t n_layer;
14+
};
15+
16+
struct llama_hparams_convnext {
17+
uint32_t n_embd;
18+
uint32_t n_layer;
19+
};
20+
21+
struct llama_hparams {
22+
bool vocab_only;
23+
bool rope_finetuned;
24+
bool use_par_res;
25+
bool swin_norm;
26+
27+
uint32_t n_vocab = 0;
28+
uint32_t n_ctx_train; // context size the model was trained on
29+
uint32_t n_embd;
30+
uint32_t n_embd_features = 0;
31+
uint32_t n_layer;
32+
uint32_t n_rot;
33+
uint32_t n_swa = 0; // sliding window attention (SWA)
34+
uint32_t n_embd_head_k; // dimension of keys (d_k). d_q is assumed to be the same, but there are n_head q heads, and only n_head_kv k-v heads
35+
uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
36+
uint32_t n_expert = 0;
37+
uint32_t n_expert_used = 0;
38+
uint32_t n_vocab_type = 0; // for BERT-style token types
39+
uint32_t n_rel_attn_bkts = 0;
40+
41+
// for WavTokenizer
42+
struct llama_hparams_posnet posnet;
43+
struct llama_hparams_convnext convnext;
44+
45+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
46+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
47+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
48+
49+
uint32_t n_layer_dense_lead = 0;
50+
uint32_t n_lora_q = 0;
51+
uint32_t n_lora_kv = 0;
52+
uint32_t n_ff_exp = 0;
53+
uint32_t n_ff_shexp = 0;
54+
uint32_t n_expert_shared = 0;
55+
uint32_t n_norm_groups = 0;
56+
57+
float expert_weights_scale = 0.0;
58+
59+
float f_norm_eps;
60+
float f_norm_rms_eps;
61+
float f_norm_group_eps;
62+
63+
float f_attn_logit_softcapping = 50.0f;
64+
float f_final_logit_softcapping = 30.0f;
65+
66+
// for RWKV
67+
uint32_t rescale_every_n_layers = 0;
68+
uint32_t time_mix_extra_dim = 0;
69+
uint32_t time_decay_extra_dim = 0;
70+
uint32_t wkv_head_size = 0;
71+
72+
float rope_attn_factor = 1.0f;
73+
float rope_freq_base_train;
74+
float rope_freq_scale_train;
75+
uint32_t n_ctx_orig_yarn;
76+
float rope_yarn_log_mul;
77+
int rope_sections[4]; // TODO: actually this should be std::array (I was wrong)
78+
79+
// for State Space Models
80+
uint32_t ssm_d_conv = 0;
81+
uint32_t ssm_d_inner = 0;
82+
uint32_t ssm_d_state = 0;
83+
uint32_t ssm_dt_rank = 0;
84+
85+
bool ssm_dt_b_c_rms = false;
86+
87+
float f_clamp_kqv = 0.0f;
88+
float f_max_alibi_bias = 0.0f;
89+
float f_logit_scale = 0.0f;
90+
91+
// Additional scale factors (Granite/Granite MoE)
92+
float f_residual_scale = 0.0f;
93+
float f_embedding_scale = 0.0f;
94+
float f_attention_scale = 0.0f;
95+
96+
bool causal_attn = true;
97+
bool use_alibi = false;
98+
bool attn_soft_cap = false;
99+
100+
// needed by encoder-decoder models (e.g. T5, FLAN-T5)
101+
// ref: https://github.com/ggerganov/llama.cpp/pull/8141
102+
llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
103+
104+
enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
105+
enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
106+
enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
107+
108+
uint32_t n_head(uint32_t il = 0) const;
109+
110+
uint32_t n_head_kv(uint32_t il = 0) const;
111+
112+
uint32_t n_ff(uint32_t il = 0) const;
113+
114+
uint32_t n_gqa(uint32_t il = 0) const;
115+
116+
// dimension of key embeddings across all k-v heads
117+
uint32_t n_embd_k_gqa(uint32_t il = 0) const;
118+
119+
// dimension of value embeddings across all k-v heads
120+
uint32_t n_embd_v_gqa(uint32_t il = 0) const;
121+
122+
// dimension of the rolling state embeddings
123+
// corresponds to Mamba's conv_states size or RWKV's token_shift states size
124+
uint32_t n_embd_k_s() const;
125+
126+
// dimension of the recurrent state embeddings
127+
uint32_t n_embd_v_s() const;
128+
};
129+
130+
static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
131+

src/llama-model.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "llama-impl.h"
44

5+
#include <cassert>
6+
57
const char * llm_type_name(llm_type type) {
68
switch (type) {
79
case MODEL_14M: return "14M";

0 commit comments

Comments
 (0)