Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llama_cpp/_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ def detokenize(self, tokens: List[int], special: bool = False) -> bytes:
n = llama_cpp.llama_token_to_piece(
self.vocab, llama_cpp.llama_token(token), buffer, size, 0, special
)
if n < 0:
# The piece did not fit; llama_token_to_piece wrote nothing and
# returned -(required size). Grow the buffer and ask again. The
# larger buffer is kept for the remaining tokens.
size = -n
buffer = (ctypes.c_char * size)()
n = llama_cpp.llama_token_to_piece(
self.vocab, llama_cpp.llama_token(token), buffer, size, 0, special
)
if n < 0:
raise RuntimeError(
f"Failed to detokenize: token={token} n={n} size={size}"
)
assert n <= size
output += bytes(buffer[:n])
# NOTE: Llama1 models automatically added a space at the start of the prompt
Expand Down
Loading