@@ -44,10 +44,16 @@ def _adapt_tokenizer(tokenizer):
44
44
"""Adapt vLLM's tokenizer to use to compile the FSM.
45
45
46
46
The API of Outlines tokenizers is slightly different to that of
47
- `transformers`. In addition we need to handle the missing spaces to
48
- Llama's tokenizer to be able to compile FSMs for this model.
47
+ `transformers`. The decoder of outlines, returns a list whereas
48
+ the decode of vLLM returns an str. To sync the vLLM decoder with
49
+ outlines internal api, the decoder should be adapted. In addition
50
+ we need to handle the missing spaces to Llama's tokenizer to be
51
+ able to compile FSMs for this model.
49
52
50
53
"""
54
+ if getattr (tokenizer , "_outlines_adapted" , False ):
55
+ return tokenizer
56
+
51
57
tokenizer .vocabulary = tokenizer .get_vocab ()
52
58
tokenizer .special_tokens = set (tokenizer .all_special_tokens )
53
59
@@ -65,13 +71,16 @@ def convert_token_to_string(token: str) -> str:
65
71
def change_decoder (
66
72
decoder : Callable [[List [int ]], str ]
67
73
) -> Callable [[List [int ]], List [str ]]:
74
+ """Sync vLLM's decoder with the outlines expectations by returning list"""
75
+
68
76
def new_decoder (inp_tokens : List [int ]) -> List [str ]:
69
77
return [decoder (inp_tokens )]
70
78
71
79
return new_decoder
72
80
73
81
tokenizer .convert_token_to_string = convert_token_to_string
74
82
tokenizer .decode = change_decoder (tokenizer .decode )
83
+ setattr (tokenizer , "_outlines_adapted" , True )
75
84
76
85
return tokenizer
77
86
0 commit comments