Skip to content

Commit 543fd01

Browse files
committed
hparams : remove n_vocab_types
ggml-ci
1 parent c2008b5 commit 543fd01

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/llama-hparams.h

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct llama_hparams {
4040
uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
4141
uint32_t n_expert = 0;
4242
uint32_t n_expert_used = 0;
43-
uint32_t n_vocab_type = 0; // for BERT-style token types
4443
uint32_t n_rel_attn_bkts = 0;
4544

4645
// for WavTokenizer

src/llama-model.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ void llama_model::load_hparams(llama_model_loader & ml) {
497497
hparams.n_embd_head_v = 0;
498498
}
499499

500+
// for differentiating model types
500501
uint32_t n_vocab = 0;
501-
502502
ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
503503

504504
// arch-specific KVs
@@ -622,7 +622,6 @@ void llama_model::load_hparams(llama_model_loader & ml) {
622622
{
623623
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
624624
ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
625-
ml.get_key(LLM_KV_TOKENIZER_TOKEN_TYPE_COUNT, hparams.n_vocab_type);
626625
ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
627626

628627
switch (hparams.n_layer) {
@@ -645,7 +644,6 @@ void llama_model::load_hparams(llama_model_loader & ml) {
645644
{
646645
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
647646
ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
648-
ml.get_key(LLM_KV_TOKENIZER_TOKEN_TYPE_COUNT, hparams.n_vocab_type);
649647
ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
650648
hparams.f_max_alibi_bias = 8.0f;
651649

@@ -659,7 +657,6 @@ void llama_model::load_hparams(llama_model_loader & ml) {
659657
{
660658
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
661659
ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
662-
ml.get_key(LLM_KV_TOKENIZER_TOKEN_TYPE_COUNT, hparams.n_vocab_type);
663660
ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
664661

665662
if (hparams.n_layer == 12 && hparams.n_embd == 768) {
@@ -1367,7 +1364,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
13671364
const int64_t n_ff = hparams.n_ff();
13681365
const int64_t n_embd_gqa = n_embd_v_gqa;
13691366
const int64_t n_vocab = vocab.n_vocab();
1370-
const int64_t n_vocab_type = hparams.n_vocab_type;
1367+
const int64_t n_token_types = vocab.n_token_types();
13711368
const int64_t n_rot = hparams.n_rot;
13721369
const int64_t n_expert = hparams.n_expert;
13731370
const int64_t n_expert_used = hparams.n_expert_used;
@@ -1812,7 +1809,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
18121809
case LLM_ARCH_NOMIC_BERT:
18131810
{
18141811
tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
1815-
type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_vocab_type}, 0);
1812+
type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0);
18161813

18171814
if (arch == LLM_ARCH_BERT) {
18181815
pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
@@ -1866,7 +1863,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
18661863
case LLM_ARCH_JINA_BERT_V2:
18671864
{
18681865
tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
1869-
type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_vocab_type}, 0); // token_type_embeddings
1866+
type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
18701867

18711868
tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
18721869
tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias

src/llama-vocab.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ struct fragment_buffer_variant {
12051205

12061206
struct llama_vocab::impl {
12071207
uint32_t n_vocab = 0;
1208+
uint32_t n_token_types = 0; // for BERT-style token types
12081209

12091210
std::unordered_map<std::string, llama_token> token_to_id;
12101211
std::vector<token_data> id_to_token;
@@ -1286,6 +1287,7 @@ void llama_vocab::load(llama_model_loader & ml, const LLM_KV & kv) {
12861287
struct gguf_context * ctx = ml.meta.get();
12871288

12881289
auto & n_vocab = pimpl->n_vocab;
1290+
auto & n_token_types = pimpl->n_token_types;
12891291
auto & id_to_token = pimpl->id_to_token;
12901292
auto & token_to_id = pimpl->token_to_id;
12911293
auto & special_eog_ids = pimpl->special_eog_ids;
@@ -1300,6 +1302,8 @@ void llama_vocab::load(llama_model_loader & ml, const LLM_KV & kv) {
13001302
ml.get_key(LLM_KV_TOKENIZER_MODEL, tokenizer_model);
13011303
ml.get_key(LLM_KV_TOKENIZER_PRE, tokenizer_pre, false);
13021304

1305+
ml.get_key(LLM_KV_TOKENIZER_TOKEN_TYPE_COUNT, n_token_types, false);
1306+
13031307
if (tokenizer_model == "no_vocab" || tokenizer_model == "none") {
13041308
type = LLAMA_VOCAB_TYPE_NONE;
13051309

@@ -2013,6 +2017,10 @@ uint32_t llama_vocab::n_vocab() const {
20132017
return (uint32_t) pimpl->id_to_token.size();
20142018
}
20152019

2020+
uint32_t llama_vocab::n_token_types() const {
2021+
return (uint32_t) pimpl->n_token_types;
2022+
}
2023+
20162024
std::string llama_vocab::type_name() const{
20172025
switch (type) {
20182026
case LLAMA_VOCAB_TYPE_NONE: return "no vocab";

src/llama-vocab.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct llama_vocab {
2424
enum llama_vocab_type get_type() const;
2525
enum llama_vocab_pre_type get_pre_type() const;
2626

27-
// TODO: how to deduplicate with llama_hparams.n_vocab ?
2827
uint32_t n_vocab() const;
28+
uint32_t n_token_types() const;
2929

3030
std::string type_name() const;
3131

0 commit comments

Comments
 (0)