Skip to content

Commit cb16b16

Browse files
committed
Fix llamacpp caching by making LlamaCppTokenizer pickleable
1 parent 538f77a commit cb16b16

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

outlines/integrations/llamacpp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def __init__(self, model: "Llama"):
6666
def convert_token_to_string(self, token: str) -> str:
6767
return token
6868

69+
def __getstate__(self):
70+
"""Allow tokenizer to be used as hash key by excluding self.decode"""
71+
return (
72+
self.vocabulary.items(),
73+
self.eos_token_id,
74+
self.eos_token,
75+
self.pad_token_id,
76+
sorted(self.special_tokens),
77+
)
78+
6979

7080
class LogitsProcessor:
7181
"""Bias LlamaCpp generation using a finite state machine.

tests/generate/test_integration_llamacpp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,11 @@ def test_llama_cpp_pre_tokenizer_remains_broken():
279279
model = models.llamacpp(repo, model_path)
280280
with pytest.raises(RuntimeError):
281281
generate.choice(model, ["skirt", "dress", "pen", "jacket"])
282+
283+
284+
def test_create_states_mapping_llamacpp_tokenizer_regression(model):
285+
"""Minimal reproducer for #922, error passing llamacpp tokenizer to create_states_mapping"""
286+
from outlines.fsm.guide import create_states_mapping
287+
from outlines.integrations.llamacpp import LlamaCppTokenizer
288+
289+
create_states_mapping("a", LlamaCppTokenizer(model.model))

0 commit comments

Comments
 (0)