Skip to content

Commit 926935c

Browse files
Merge branch 'main' into normalize_embeddings
2 parents 20dd941 + 710e19a commit 926935c

File tree

7 files changed

+643
-284
lines changed

7 files changed

+643
-284
lines changed

.github/workflows/build-wheels-cuda.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ jobs:
6161
- name: Setup Mamba
6262
uses: conda-incubator/[email protected]
6363
with:
64-
activate-environment: "build"
64+
activate-environment: "llamacpp"
6565
python-version: ${{ matrix.pyver }}
66-
miniforge-variant: Mambaforge
6766
miniforge-version: latest
68-
use-mamba: true
6967
add-pip-as-python-dependency: true
7068
auto-activate-base: false
7169

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.3.7]
11+
12+
- feat: Update llama.cpp to ggerganov/llama.cpp@794fe23f29fb40104975c91fe19f23798f7c726e
13+
- fix(ci): Fix the CUDA workflow by @oobabooga in #1894
14+
- fix: error showing time spent in llama perf context print, adds `no_perf` flag to `Llama` class by @shakalaca in #1898
15+
1016
## [0.3.6]
1117

1218
- feat: Update llama.cpp to ggerganov/llama.cpp@f7cd13301c2a88f97073fd119072b4cc92c08df1

llama_cpp/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .llama_cpp import *
22
from .llama import *
33

4-
__version__ = "0.3.6"
4+
__version__ = "0.3.7"

llama_cpp/_internals.py

+101-80
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ def __init__(
5555
if model is None:
5656
raise ValueError(f"Failed to load model from file: {path_model}")
5757

58+
vocab = llama_cpp.llama_model_get_vocab(model)
59+
60+
if vocab is None:
61+
raise ValueError(f"Failed to get vocab from model: {path_model}")
62+
5863
self.model = model
64+
self.vocab = vocab
5965

6066
def free_model():
6167
if self.model is None:
@@ -75,7 +81,7 @@ def vocab_type(self) -> int:
7581
return llama_cpp.llama_vocab_type(self.model)
7682

7783
def n_vocab(self) -> int:
78-
return llama_cpp.llama_n_vocab(self.model)
84+
return llama_cpp.llama_n_vocab(self.vocab)
7985

8086
def n_ctx_train(self) -> int:
8187
return llama_cpp.llama_n_ctx_train(self.model)
@@ -84,7 +90,7 @@ def n_embd(self) -> int:
8490
return llama_cpp.llama_n_embd(self.model)
8591

8692
def rope_freq_scale_train(self) -> float:
87-
return llama_cpp.llama_rope_freq_scale_train(self.model)
93+
return llama_cpp.llama_model_rope_freq_scale_train(self.model)
8894

8995
def desc(self) -> str:
9096
buf = ctypes.create_string_buffer(1024)
@@ -98,67 +104,67 @@ def n_params(self) -> int:
98104
return llama_cpp.llama_model_n_params(self.model)
99105

100106
def get_tensor(self, name: str) -> ctypes.c_void_p:
101-
return llama_cpp.llama_get_model_tensor(self.model, name.encode("utf-8"))
107+
raise NotImplementedError("get_tensor is not implemented in llama.cpp")
102108

103109
# Vocab
104110

105111
def token_get_text(self, token: int) -> str:
106-
return llama_cpp.llama_token_get_text(self.model, token).decode("utf-8")
112+
return llama_cpp.llama_token_get_text(self.vocab, token).decode("utf-8")
107113

108114
def token_get_score(self, token: int) -> float:
109-
return llama_cpp.llama_token_get_score(self.model, token)
115+
return llama_cpp.llama_token_get_score(self.vocab, token)
110116

111117
def token_get_attr(self, token: int) -> int:
112-
return llama_cpp.llama_token_get_attr(self.model, token)
118+
return llama_cpp.llama_token_get_attr(self.vocab, token)
113119

114120
# Special tokens
115121

116122
def token_bos(self) -> int:
117-
return llama_cpp.llama_token_bos(self.model)
123+
return llama_cpp.llama_token_bos(self.vocab)
118124

119125
def token_eos(self) -> int:
120-
return llama_cpp.llama_token_eos(self.model)
126+
return llama_cpp.llama_token_eos(self.vocab)
121127

122128
def token_cls(self) -> int:
123-
return llama_cpp.llama_token_cls(self.model)
129+
return llama_cpp.llama_token_cls(self.vocab)
124130

125131
def token_sep(self) -> int:
126-
return llama_cpp.llama_token_sep(self.model)
132+
return llama_cpp.llama_token_sep(self.vocab)
127133

128134
def token_nl(self) -> int:
129-
return llama_cpp.llama_token_nl(self.model)
135+
return llama_cpp.llama_token_nl(self.vocab)
130136

131137
def token_prefix(self) -> int:
132-
return llama_cpp.llama_token_prefix(self.model)
138+
raise NotImplementedError("token_prefix is not implemented in llama.cpp")
133139

134140
def token_middle(self) -> int:
135-
return llama_cpp.llama_token_middle(self.model)
141+
raise NotImplementedError("token_middle is not implemented in llama.cpp")
136142

137143
def token_suffix(self) -> int:
138-
return llama_cpp.llama_token_suffix(self.model)
144+
raise NotImplementedError("token_suffix is not implemented in llama.cpp")
139145

140146
def token_eot(self) -> int:
141-
return llama_cpp.llama_token_eot(self.model)
147+
return llama_cpp.llama_token_eot(self.vocab)
142148

143149
def add_bos_token(self) -> bool:
144-
return llama_cpp.llama_add_bos_token(self.model)
150+
return llama_cpp.llama_add_bos_token(self.vocab)
145151

146152
def add_eos_token(self) -> bool:
147-
return llama_cpp.llama_add_eos_token(self.model)
153+
return llama_cpp.llama_add_eos_token(self.vocab)
148154

149155
# Tokenization
150156

151157
def tokenize(self, text: bytes, add_bos: bool, special: bool):
152158
n_ctx = self.n_ctx_train()
153159
tokens = (llama_cpp.llama_token * n_ctx)()
154160
n_tokens = llama_cpp.llama_tokenize(
155-
self.model, text, len(text), tokens, n_ctx, add_bos, special
161+
self.vocab, text, len(text), tokens, n_ctx, add_bos, special
156162
)
157163
if n_tokens < 0:
158164
n_tokens = abs(n_tokens)
159165
tokens = (llama_cpp.llama_token * n_tokens)()
160166
n_tokens = llama_cpp.llama_tokenize(
161-
self.model, text, len(text), tokens, n_tokens, add_bos, special
167+
self.vocab, text, len(text), tokens, n_tokens, add_bos, special
162168
)
163169
if n_tokens < 0:
164170
raise RuntimeError(
@@ -168,7 +174,7 @@ def tokenize(self, text: bytes, add_bos: bool, special: bool):
168174

169175
def token_to_piece(self, token: int, special: bool = False) -> bytes:
170176
buf = ctypes.create_string_buffer(32)
171-
llama_cpp.llama_token_to_piece(self.model, token, buf, 32, 0, special)
177+
llama_cpp.llama_token_to_piece(self.vocab, token, buf, 32, 0, special)
172178
return bytes(buf)
173179

174180
def detokenize(self, tokens: List[int], special: bool = False) -> bytes:
@@ -177,7 +183,7 @@ def detokenize(self, tokens: List[int], special: bool = False) -> bytes:
177183
buffer = (ctypes.c_char * size)()
178184
for token in tokens:
179185
n = llama_cpp.llama_token_to_piece(
180-
self.model, llama_cpp.llama_token(token), buffer, size, 0, special
186+
self.vocab, llama_cpp.llama_token(token), buffer, size, 0, special
181187
)
182188
assert n <= size
183189
output += bytes(buffer[:n])
@@ -320,7 +326,8 @@ def get_embeddings(self):
320326

321327
def set_rng_seed(self, seed: int):
322328
# TODO: Fix
323-
llama_cpp.llama_set_rng_seed(self.ctx, seed)
329+
# llama_cpp.llama_set_rng_seed(self.ctx, seed)
330+
raise NotImplementedError("set_rng_seed is not implemented in llama.cpp")
324331

325332
def sample_repetition_penalties(
326333
self,
@@ -331,55 +338,63 @@ def sample_repetition_penalties(
331338
penalty_freq: float,
332339
penalty_present: float,
333340
):
334-
llama_cpp.llama_sample_repetition_penalties(
335-
self.ctx,
336-
llama_cpp.byref(candidates.candidates),
337-
last_tokens_data,
338-
penalty_last_n,
339-
penalty_repeat,
340-
penalty_freq,
341-
penalty_present,
342-
)
341+
# llama_cpp.llama_sample_repetition_penalties(
342+
# self.ctx,
343+
# llama_cpp.byref(candidates.candidates),
344+
# last_tokens_data,
345+
# penalty_last_n,
346+
# penalty_repeat,
347+
# penalty_freq,
348+
# penalty_present,
349+
# )
350+
raise NotImplementedError("sample_repetition_penalties is not implemented in llama.cpp")
343351

344352
def sample_softmax(self, candidates: "_LlamaTokenDataArray"):
345-
llama_cpp.llama_sample_softmax(
346-
self.ctx,
347-
llama_cpp.byref(candidates.candidates),
348-
)
353+
# llama_cpp.llama_sample_softmax(
354+
# self.ctx,
355+
# llama_cpp.byref(candidates.candidates),
356+
# )
357+
raise NotImplementedError("sample_softmax is not implemented in llama.cpp")
349358

350359
def sample_top_k(self, candidates: "_LlamaTokenDataArray", k: int, min_keep: int):
351-
llama_cpp.llama_sample_top_k(
352-
self.ctx, llama_cpp.byref(candidates.candidates), k, min_keep
353-
)
360+
# llama_cpp.llama_sample_top_k(
361+
# self.ctx, llama_cpp.byref(candidates.candidates), k, min_keep
362+
# )
363+
raise NotImplementedError("sample_top_k is not implemented in llama.cpp")
354364

355365
def sample_top_p(self, candidates: "_LlamaTokenDataArray", p: float, min_keep: int):
356-
llama_cpp.llama_sample_top_p(
357-
self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
358-
)
366+
# llama_cpp.llama_sample_top_p(
367+
# self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
368+
# )
369+
raise NotImplementedError("sample_top_p is not implemented in llama.cpp")
359370

360371
def sample_min_p(self, candidates: "_LlamaTokenDataArray", p: float, min_keep: int):
361-
llama_cpp.llama_sample_min_p(
362-
self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
363-
)
372+
# llama_cpp.llama_sample_min_p(
373+
# self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
374+
# )
375+
raise NotImplementedError("sample_min_p is not implemented in llama.cpp")
364376

365377
def sample_typical(
366378
self, candidates: "_LlamaTokenDataArray", p: float, min_keep: int
367379
):
368-
llama_cpp.llama_sample_typical(
369-
self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
370-
)
380+
# llama_cpp.llama_sample_typical(
381+
# self.ctx, llama_cpp.byref(candidates.candidates), p, min_keep
382+
# )
383+
raise NotImplementedError("sample_typical is not implemented in llama.cpp")
371384

372385
def sample_temp(self, candidates: "_LlamaTokenDataArray", temp: float):
373-
llama_cpp.llama_sample_temp(
374-
self.ctx, llama_cpp.byref(candidates.candidates), temp
375-
)
386+
# llama_cpp.llama_sample_temp(
387+
# self.ctx, llama_cpp.byref(candidates.candidates), temp
388+
# )
389+
raise NotImplementedError("sample_temp is not implemented in llama.cpp")
376390

377391
def sample_grammar(self, candidates: "_LlamaTokenDataArray", grammar: LlamaGrammar):
378-
llama_cpp.llama_sample_grammar(
379-
self.ctx,
380-
llama_cpp.byref(candidates.candidates),
381-
grammar.grammar,
382-
)
392+
# llama_cpp.llama_sample_grammar(
393+
# self.ctx,
394+
# llama_cpp.byref(candidates.candidates),
395+
# grammar.grammar,
396+
# )
397+
raise NotImplementedError("sample_grammar is not implemented in llama.cpp")
383398

384399
def sample_token_mirostat(
385400
self,
@@ -389,14 +404,15 @@ def sample_token_mirostat(
389404
m: int,
390405
mu: llama_cpp.CtypesPointerOrRef[ctypes.c_float],
391406
) -> int:
392-
return llama_cpp.llama_sample_token_mirostat(
393-
self.ctx,
394-
llama_cpp.byref(candidates.candidates),
395-
tau,
396-
eta,
397-
m,
398-
mu,
399-
)
407+
raise NotImplementedError("sample_token_mirostat is not implemented in llama.cpp")
408+
# return llama_cpp.llama_sample_token_mirostat(
409+
# self.ctx,
410+
# llama_cpp.byref(candidates.candidates),
411+
# tau,
412+
# eta,
413+
# m,
414+
# mu,
415+
# )
400416

401417
def sample_token_mirostat_v2(
402418
self,
@@ -405,29 +421,33 @@ def sample_token_mirostat_v2(
405421
eta: float,
406422
mu: llama_cpp.CtypesPointerOrRef[ctypes.c_float],
407423
) -> int:
408-
return llama_cpp.llama_sample_token_mirostat_v2(
409-
self.ctx,
410-
llama_cpp.byref(candidates.candidates),
411-
tau,
412-
eta,
413-
mu,
414-
)
424+
raise NotImplementedError("sample_token_mirostat_v2 is not implemented in llama.cpp")
425+
# return llama_cpp.llama_sample_token_mirostat_v2(
426+
# self.ctx,
427+
# llama_cpp.byref(candidates.candidates),
428+
# tau,
429+
# eta,
430+
# mu,
431+
# )
415432

416433
def sample_token_greedy(self, candidates: "_LlamaTokenDataArray") -> int:
417-
return llama_cpp.llama_sample_token_greedy(
418-
self.ctx,
419-
llama_cpp.byref(candidates.candidates),
420-
)
434+
raise NotImplementedError("sample_token_greedy is not implemented in llama.cpp")
435+
# return llama_cpp.llama_sample_token_greedy(
436+
# self.ctx,
437+
# llama_cpp.byref(candidates.candidates),
438+
# )
421439

422440
def sample_token(self, candidates: "_LlamaTokenDataArray") -> int:
423-
return llama_cpp.llama_sample_token(
424-
self.ctx,
425-
llama_cpp.byref(candidates.candidates),
426-
)
441+
raise NotImplementedError("sample_token is not implemented in llama.cpp")
442+
# return llama_cpp.llama_sample_token(
443+
# self.ctx,
444+
# llama_cpp.byref(candidates.candidates),
445+
# )
427446

428447
# Grammar
429448
def grammar_accept_token(self, grammar: LlamaGrammar, token: int):
430-
llama_cpp.llama_grammar_accept_token(grammar.grammar, self.ctx, token)
449+
raise NotImplementedError("grammar_accept_token is not implemented in llama.cpp")
450+
# llama_cpp.llama_grammar_accept_token(grammar.grammar, self.ctx, token)
431451

432452
def reset_timings(self):
433453
llama_cpp.llama_perf_context_reset(self.ctx)
@@ -788,7 +808,7 @@ def add_mirostat_v2(self, seed: int, tau: float, eta: float):
788808

789809
def add_grammar(self, model: LlamaModel, grammar: LlamaGrammar):
790810
sampler = llama_cpp.llama_sampler_init_grammar(
791-
model.model, grammar._grammar.encode("utf-8"), grammar._root.encode("utf-8")
811+
model.vocab, grammar._grammar.encode("utf-8"), grammar._root.encode("utf-8")
792812
)
793813
self._add_sampler(sampler)
794814

@@ -842,6 +862,7 @@ def get_seed(self) -> int:
842862

843863
def sample(self, ctx: LlamaContext, idx: int) -> int:
844864
assert self.sampler is not None
865+
assert ctx.ctx is not None
845866
return llama_cpp.llama_sampler_sample(self.sampler, ctx.ctx, idx)
846867

847868
def close(self):

0 commit comments

Comments
 (0)