@@ -55,7 +55,13 @@ def __init__(
55
55
if model is None :
56
56
raise ValueError (f"Failed to load model from file: { path_model } " )
57
57
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
+
58
63
self .model = model
64
+ self .vocab = vocab
59
65
60
66
def free_model ():
61
67
if self .model is None :
@@ -75,7 +81,7 @@ def vocab_type(self) -> int:
75
81
return llama_cpp .llama_vocab_type (self .model )
76
82
77
83
def n_vocab (self ) -> int :
78
- return llama_cpp .llama_n_vocab (self .model )
84
+ return llama_cpp .llama_n_vocab (self .vocab )
79
85
80
86
def n_ctx_train (self ) -> int :
81
87
return llama_cpp .llama_n_ctx_train (self .model )
@@ -84,7 +90,7 @@ def n_embd(self) -> int:
84
90
return llama_cpp .llama_n_embd (self .model )
85
91
86
92
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 )
88
94
89
95
def desc (self ) -> str :
90
96
buf = ctypes .create_string_buffer (1024 )
@@ -98,67 +104,67 @@ def n_params(self) -> int:
98
104
return llama_cpp .llama_model_n_params (self .model )
99
105
100
106
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" )
102
108
103
109
# Vocab
104
110
105
111
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" )
107
113
108
114
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 )
110
116
111
117
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 )
113
119
114
120
# Special tokens
115
121
116
122
def token_bos (self ) -> int :
117
- return llama_cpp .llama_token_bos (self .model )
123
+ return llama_cpp .llama_token_bos (self .vocab )
118
124
119
125
def token_eos (self ) -> int :
120
- return llama_cpp .llama_token_eos (self .model )
126
+ return llama_cpp .llama_token_eos (self .vocab )
121
127
122
128
def token_cls (self ) -> int :
123
- return llama_cpp .llama_token_cls (self .model )
129
+ return llama_cpp .llama_token_cls (self .vocab )
124
130
125
131
def token_sep (self ) -> int :
126
- return llama_cpp .llama_token_sep (self .model )
132
+ return llama_cpp .llama_token_sep (self .vocab )
127
133
128
134
def token_nl (self ) -> int :
129
- return llama_cpp .llama_token_nl (self .model )
135
+ return llama_cpp .llama_token_nl (self .vocab )
130
136
131
137
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" )
133
139
134
140
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" )
136
142
137
143
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" )
139
145
140
146
def token_eot (self ) -> int :
141
- return llama_cpp .llama_token_eot (self .model )
147
+ return llama_cpp .llama_token_eot (self .vocab )
142
148
143
149
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 )
145
151
146
152
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 )
148
154
149
155
# Tokenization
150
156
151
157
def tokenize (self , text : bytes , add_bos : bool , special : bool ):
152
158
n_ctx = self .n_ctx_train ()
153
159
tokens = (llama_cpp .llama_token * n_ctx )()
154
160
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
156
162
)
157
163
if n_tokens < 0 :
158
164
n_tokens = abs (n_tokens )
159
165
tokens = (llama_cpp .llama_token * n_tokens )()
160
166
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
162
168
)
163
169
if n_tokens < 0 :
164
170
raise RuntimeError (
@@ -168,7 +174,7 @@ def tokenize(self, text: bytes, add_bos: bool, special: bool):
168
174
169
175
def token_to_piece (self , token : int , special : bool = False ) -> bytes :
170
176
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 )
172
178
return bytes (buf )
173
179
174
180
def detokenize (self , tokens : List [int ], special : bool = False ) -> bytes :
@@ -177,7 +183,7 @@ def detokenize(self, tokens: List[int], special: bool = False) -> bytes:
177
183
buffer = (ctypes .c_char * size )()
178
184
for token in tokens :
179
185
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
181
187
)
182
188
assert n <= size
183
189
output += bytes (buffer [:n ])
@@ -320,7 +326,8 @@ def get_embeddings(self):
320
326
321
327
def set_rng_seed (self , seed : int ):
322
328
# 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" )
324
331
325
332
def sample_repetition_penalties (
326
333
self ,
@@ -331,55 +338,63 @@ def sample_repetition_penalties(
331
338
penalty_freq : float ,
332
339
penalty_present : float ,
333
340
):
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" )
343
351
344
352
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" )
349
358
350
359
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" )
354
364
355
365
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" )
359
370
360
371
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" )
364
376
365
377
def sample_typical (
366
378
self , candidates : "_LlamaTokenDataArray" , p : float , min_keep : int
367
379
):
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" )
371
384
372
385
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" )
376
390
377
391
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" )
383
398
384
399
def sample_token_mirostat (
385
400
self ,
@@ -389,14 +404,15 @@ def sample_token_mirostat(
389
404
m : int ,
390
405
mu : llama_cpp .CtypesPointerOrRef [ctypes .c_float ],
391
406
) -> 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
+ # )
400
416
401
417
def sample_token_mirostat_v2 (
402
418
self ,
@@ -405,29 +421,33 @@ def sample_token_mirostat_v2(
405
421
eta : float ,
406
422
mu : llama_cpp .CtypesPointerOrRef [ctypes .c_float ],
407
423
) -> 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
+ # )
415
432
416
433
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
+ # )
421
439
422
440
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
+ # )
427
446
428
447
# Grammar
429
448
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)
431
451
432
452
def reset_timings (self ):
433
453
llama_cpp .llama_perf_context_reset (self .ctx )
@@ -788,7 +808,7 @@ def add_mirostat_v2(self, seed: int, tau: float, eta: float):
788
808
789
809
def add_grammar (self , model : LlamaModel , grammar : LlamaGrammar ):
790
810
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" )
792
812
)
793
813
self ._add_sampler (sampler )
794
814
@@ -842,6 +862,7 @@ def get_seed(self) -> int:
842
862
843
863
def sample (self , ctx : LlamaContext , idx : int ) -> int :
844
864
assert self .sampler is not None
865
+ assert ctx .ctx is not None
845
866
return llama_cpp .llama_sampler_sample (self .sampler , ctx .ctx , idx )
846
867
847
868
def close (self ):
0 commit comments