Skip to content

Update llama_cpp: Sync LLAMA_API names with llama.cpp mainline. Needs more testing #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 27, 2025
89 changes: 45 additions & 44 deletions llama_cpp/_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
raise ValueError(f"Model path does not exist: {path_model}")

with suppress_stdout_stderr(disable=verbose):
model = llama_cpp.llama_load_model_from_file(
model = llama_cpp.llama_model_load_from_file(
self.path_model.encode("utf-8"), self.params
)

Expand All @@ -60,7 +60,7 @@ def __init__(
def free_model():
if self.model is None:
return
llama_cpp.llama_free_model(self.model)
llama_cpp.llama_model_free(self.model)
self.model = None

self._exit_stack.callback(free_model)
Expand All @@ -71,20 +71,20 @@ def close(self):
def __del__(self):
self.close()

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

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

def n_ctx_train(self) -> int:
return llama_cpp.llama_n_ctx_train(self.model)
return llama_cpp.llama_model_n_ctx_train(self.model)

def n_embd(self) -> int:
return llama_cpp.llama_n_embd(self.model)
return llama_cpp.llama_model_n_embd(self.model)

def rope_freq_scale_train(self) -> float:
return llama_cpp.llama_rope_freq_scale_train(self.model)
return llama_cpp.llama_model_rope_freq_scale_train(self.model)

def desc(self) -> str:
buf = ctypes.create_string_buffer(1024)
Expand All @@ -97,68 +97,68 @@ def size(self) -> int:
def n_params(self) -> int:
return llama_cpp.llama_model_n_params(self.model)

def get_tensor(self, name: str) -> ctypes.c_void_p:
return llama_cpp.llama_get_model_tensor(self.model, name.encode("utf-8"))

# Vocab

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

def token_get_score(self, token: int) -> float:
return llama_cpp.llama_token_get_score(self.model, token)
def token_get_score(self, _vocab:llama_cpp.llama_vocab_p, token: int) -> float:
return llama_cpp.llama_vocab_get_score(_vocab, token)

def token_get_attr(self, token: int) -> int:
return llama_cpp.llama_token_get_attr(self.model, token)
def token_get_attr(self, _vocab:llama_cpp.llama_vocab_p, token: int) -> int:
return llama_cpp.llama_vocab_get_attr(_vocab, token)

# Special tokens

def token_bos(self) -> int:
return llama_cpp.llama_token_bos(self.model)
def token_bos(self, _vocab:llama_cpp.llama_vocab_p) -> int:
return llama_cpp.llama_vocab_bos(_vocab)

def token_eos(self, _vocab:llama_cpp.llama_vocab_p) -> int:
return llama_cpp.llama_vocab_eos(_vocab)

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

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

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

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

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

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

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

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

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

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

# Tokenization

def tokenize(self, text: bytes, add_bos: bool, special: bool):
def tokenize(self, _vocab:llama_cpp.llama_vocab_p, text: bytes, add_bos: bool, special: bool):
n_ctx = self.n_ctx_train()
tokens = (llama_cpp.llama_token * n_ctx)()
n_tokens = llama_cpp.llama_tokenize(
self.model, text, len(text), tokens, n_ctx, add_bos, special
_vocab, text, len(text), tokens, n_ctx, add_bos, special
)
if n_tokens < 0:
n_tokens = abs(n_tokens)
tokens = (llama_cpp.llama_token * n_tokens)()
n_tokens = llama_cpp.llama_tokenize(
self.model, text, len(text), tokens, n_tokens, add_bos, special
_vocab, text, len(text), tokens, n_tokens, add_bos, special
)
if n_tokens < 0:
raise RuntimeError(
Expand Down Expand Up @@ -605,10 +605,11 @@ def prev_str(self, ctx_main: LlamaContext, n: int) -> str:
def sample(
self,
ctx_main: LlamaContext,
_vocab:llama_cpp.llama_vocab_p,
idx: int = 0,
logits_array: Optional[npt.NDArray[np.single]] = None,
):
n_vocab = ctx_main.model.n_vocab()
n_vocab = ctx_main.model.n_vocab(_vocab)
id: int = 0

if logits_array is None:
Expand Down
10 changes: 6 additions & 4 deletions llama_cpp/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ def __init__(
)
)

self._vocab = llama_cpp.llama_model_get_vocab(self._model.model)

# Override tokenizer
self.tokenizer_ = tokenizer or LlamaTokenizer(self)

Expand Down Expand Up @@ -2171,23 +2173,23 @@ def n_embd(self) -> int:

def n_vocab(self) -> int:
"""Return the vocabulary size."""
return self._model.n_vocab()
return self._model.n_vocab(self._vocab)

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

def token_eos(self) -> int:
"""Return the end-of-sequence token."""
return self._model.token_eos()
return self._model.token_eos(self._vocab)

def token_bos(self) -> int:
"""Return the beginning-of-sequence token."""
return self._model.token_bos()
return self._model.token_bos(self._vocab)

def token_nl(self) -> int:
"""Return the newline token."""
return self._model.token_nl()
return self._model.token_nl(self._vocab)

def pooling_type(self) -> str:
"""Return the pooling type."""
Expand Down
Loading