Skip to content

Commit 660955b

Browse files
authored
Merge pull request #1 from JamePeng/main
Update llama_cpp: Sync LLAMA_API names with llama.cpp mainline. Needs more testing
2 parents 0580cf2 + 3ffc680 commit 660955b

File tree

4 files changed

+247
-216
lines changed

4 files changed

+247
-216
lines changed

llama_cpp/_internals.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(
4848
raise ValueError(f"Model path does not exist: {path_model}")
4949

5050
with suppress_stdout_stderr(disable=verbose):
51-
model = llama_cpp.llama_load_model_from_file(
51+
model = llama_cpp.llama_model_load_from_file(
5252
self.path_model.encode("utf-8"), self.params
5353
)
5454

@@ -60,7 +60,7 @@ def __init__(
6060
def free_model():
6161
if self.model is None:
6262
return
63-
llama_cpp.llama_free_model(self.model)
63+
llama_cpp.llama_model_free(self.model)
6464
self.model = None
6565

6666
self._exit_stack.callback(free_model)
@@ -71,20 +71,20 @@ def close(self):
7171
def __del__(self):
7272
self.close()
7373

74-
def vocab_type(self) -> int:
75-
return llama_cpp.llama_vocab_type(self.model)
74+
def vocab_type(self, _vocab:llama_cpp.llama_vocab_p) -> int:
75+
return llama_cpp.llama_vocab_type(_vocab)
7676

77-
def n_vocab(self) -> int:
78-
return llama_cpp.llama_n_vocab(self.model)
77+
def n_vocab(self, _vocab:llama_cpp.llama_vocab_p) -> int:
78+
return llama_cpp.llama_vocab_n_tokens(_vocab)
7979

8080
def n_ctx_train(self) -> int:
81-
return llama_cpp.llama_n_ctx_train(self.model)
81+
return llama_cpp.llama_model_n_ctx_train(self.model)
8282

8383
def n_embd(self) -> int:
84-
return llama_cpp.llama_n_embd(self.model)
84+
return llama_cpp.llama_model_n_embd(self.model)
8585

8686
def rope_freq_scale_train(self) -> float:
87-
return llama_cpp.llama_rope_freq_scale_train(self.model)
87+
return llama_cpp.llama_model_rope_freq_scale_train(self.model)
8888

8989
def desc(self) -> str:
9090
buf = ctypes.create_string_buffer(1024)
@@ -97,68 +97,68 @@ def size(self) -> int:
9797
def n_params(self) -> int:
9898
return llama_cpp.llama_model_n_params(self.model)
9999

100-
def get_tensor(self, name: str) -> ctypes.c_void_p:
101-
return llama_cpp.llama_get_model_tensor(self.model, name.encode("utf-8"))
102-
103100
# Vocab
104101

105-
def token_get_text(self, token: int) -> str:
106-
return llama_cpp.llama_token_get_text(self.model, token).decode("utf-8")
102+
def token_get_text(self, _vocab:llama_cpp.llama_vocab_p, token: int) -> str:
103+
return llama_cpp.llama_vocab_get_text(_vocab, token).decode("utf-8")
107104

108-
def token_get_score(self, token: int) -> float:
109-
return llama_cpp.llama_token_get_score(self.model, token)
105+
def token_get_score(self, _vocab:llama_cpp.llama_vocab_p, token: int) -> float:
106+
return llama_cpp.llama_vocab_get_score(_vocab, token)
110107

111-
def token_get_attr(self, token: int) -> int:
112-
return llama_cpp.llama_token_get_attr(self.model, token)
108+
def token_get_attr(self, _vocab:llama_cpp.llama_vocab_p, token: int) -> int:
109+
return llama_cpp.llama_vocab_get_attr(_vocab, token)
113110

114111
# Special tokens
115112

116-
def token_bos(self) -> int:
117-
return llama_cpp.llama_token_bos(self.model)
113+
def token_bos(self, _vocab:llama_cpp.llama_vocab_p) -> int:
114+
return llama_cpp.llama_vocab_bos(_vocab)
115+
116+
def token_eos(self, _vocab:llama_cpp.llama_vocab_p) -> int:
117+
return llama_cpp.llama_vocab_eos(_vocab)
118118

119-
def token_eos(self) -> int:
120-
return llama_cpp.llama_token_eos(self.model)
119+
def token_eot(self, _vocab:llama_cpp.llama_vocab_p) -> int:
120+
return llama_cpp.llama_vocab_eot(_vocab)
121121

122-
def token_cls(self) -> int:
123-
return llama_cpp.llama_token_cls(self.model)
122+
def token_cls(self, _vocab:llama_cpp.llama_vocab_p) -> int:
123+
return llama_cpp.llama_vocab_cls(_vocab)
124124

125-
def token_sep(self) -> int:
126-
return llama_cpp.llama_token_sep(self.model)
125+
def token_sep(self, _vocab:llama_cpp.llama_vocab_p) -> int:
126+
return llama_cpp.llama_vocab_sep(_vocab)
127127

128-
def token_nl(self) -> int:
129-
return llama_cpp.llama_token_nl(self.model)
128+
def token_nl(self, _vocab:llama_cpp.llama_vocab_p) -> int:
129+
return llama_cpp.llama_vocab_nl(_vocab)
130130

131-
def token_prefix(self) -> int:
132-
return llama_cpp.llama_token_prefix(self.model)
131+
def token_pad(self, _vocab:llama_cpp.llama_vocab_p) -> int:
132+
return llama_cpp.llama_vocab_pad(_vocab)
133133

134-
def token_middle(self) -> int:
135-
return llama_cpp.llama_token_middle(self.model)
134+
def token_prefix(self, _vocab:llama_cpp.llama_vocab_p) -> int:
135+
return llama_cpp.llama_vocab_fim_pre(_vocab)
136136

137-
def token_suffix(self) -> int:
138-
return llama_cpp.llama_token_suffix(self.model)
137+
def token_middle(self, _vocab:llama_cpp.llama_vocab_p) -> int:
138+
return llama_cpp.llama_vocab_fim_mid(_vocab)
139139

140-
def token_eot(self) -> int:
141-
return llama_cpp.llama_token_eot(self.model)
140+
def token_suffix(self, _vocab:llama_cpp.llama_vocab_p) -> int:
141+
return llama_cpp.llama_vocab_fim_suf(_vocab)
142142

143-
def add_bos_token(self) -> bool:
144-
return llama_cpp.llama_add_bos_token(self.model)
143+
def add_bos_token(self, _vocab:llama_cpp.llama_vocab_p) -> bool:
144+
return llama_cpp.llama_vocab_get_add_bos(_vocab)
145145

146-
def add_eos_token(self) -> bool:
147-
return llama_cpp.llama_add_eos_token(self.model)
146+
def add_eos_token(self, _vocab:llama_cpp.llama_vocab_p) -> bool:
147+
return llama_cpp.llama_vocab_get_add_eos(_vocab)
148148

149149
# Tokenization
150150

151-
def tokenize(self, text: bytes, add_bos: bool, special: bool):
151+
def tokenize(self, _vocab:llama_cpp.llama_vocab_p, text: bytes, add_bos: bool, special: bool):
152152
n_ctx = self.n_ctx_train()
153153
tokens = (llama_cpp.llama_token * n_ctx)()
154154
n_tokens = llama_cpp.llama_tokenize(
155-
self.model, text, len(text), tokens, n_ctx, add_bos, special
155+
_vocab, text, len(text), tokens, n_ctx, add_bos, special
156156
)
157157
if n_tokens < 0:
158158
n_tokens = abs(n_tokens)
159159
tokens = (llama_cpp.llama_token * n_tokens)()
160160
n_tokens = llama_cpp.llama_tokenize(
161-
self.model, text, len(text), tokens, n_tokens, add_bos, special
161+
_vocab, text, len(text), tokens, n_tokens, add_bos, special
162162
)
163163
if n_tokens < 0:
164164
raise RuntimeError(
@@ -605,10 +605,11 @@ def prev_str(self, ctx_main: LlamaContext, n: int) -> str:
605605
def sample(
606606
self,
607607
ctx_main: LlamaContext,
608+
_vocab:llama_cpp.llama_vocab_p,
608609
idx: int = 0,
609610
logits_array: Optional[npt.NDArray[np.single]] = None,
610611
):
611-
n_vocab = ctx_main.model.n_vocab()
612+
n_vocab = ctx_main.model.n_vocab(_vocab)
612613
id: int = 0
613614

614615
if logits_array is None:

llama_cpp/llama.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ def __init__(
374374
)
375375
)
376376

377+
self._vocab = llama_cpp.llama_model_get_vocab(self._model.model)
378+
377379
# Override tokenizer
378380
self.tokenizer_ = tokenizer or LlamaTokenizer(self)
379381

@@ -2171,23 +2173,23 @@ def n_embd(self) -> int:
21712173

21722174
def n_vocab(self) -> int:
21732175
"""Return the vocabulary size."""
2174-
return self._model.n_vocab()
2176+
return self._model.n_vocab(self._vocab)
21752177

21762178
def tokenizer(self) -> LlamaTokenizer:
21772179
"""Return the llama tokenizer for this model."""
21782180
return LlamaTokenizer(self)
21792181

21802182
def token_eos(self) -> int:
21812183
"""Return the end-of-sequence token."""
2182-
return self._model.token_eos()
2184+
return self._model.token_eos(self._vocab)
21832185

21842186
def token_bos(self) -> int:
21852187
"""Return the beginning-of-sequence token."""
2186-
return self._model.token_bos()
2188+
return self._model.token_bos(self._vocab)
21872189

21882190
def token_nl(self) -> int:
21892191
"""Return the newline token."""
2190-
return self._model.token_nl()
2192+
return self._model.token_nl(self._vocab)
21912193

21922194
def pooling_type(self) -> str:
21932195
"""Return the pooling type."""

0 commit comments

Comments
 (0)