@@ -1272,6 +1272,14 @@ def llama_flash_attn_type_name(flash_attn_type: int, /) -> Optional[bytes]:
12721272 ...
12731273
12741274
1275+ # // Get the model file type (quantization) as a string, e.g. "Q8_0" or "Q4_K - Medium"
1276+ # LLAMA_API const char * llama_ftype_name(enum llama_ftype ftype);
1277+ @ctypes_function ("llama_ftype_name" , [ctypes .c_int ], ctypes .c_char_p )
1278+ def llama_ftype_name (ftype : int , / ) -> Optional [bytes ]:
1279+ '''Get the model file type (quantization) as a string, e.g. "Q8_0" or "Q4_K - Medium"'''
1280+ ...
1281+
1282+
12751283# // Initialize the llama + ggml backend
12761284# // If numa is true, use NUMA optimizations
12771285# // Call once at the start of the program
@@ -1777,7 +1785,8 @@ def llama_model_rope_freq_scale_train(model: llama_model_p, /) -> float: ...
17771785# LLAMA_API uint32_t llama_model_n_cls_out(const struct llama_model * model);
17781786@ctypes_function ("llama_model_n_cls_out" , [llama_model_p_ctypes ], ctypes .c_uint32 )
17791787def llama_model_n_cls_out (model : llama_model_p , / ) -> int :
1780- """Returns the number of classifier outputs (only valid for classifier models)"""
1788+ """Returns the number of classifier outputs (only valid for classifier models)
1789+ Undefined behavior for non-classifier models"""
17811790 ...
17821791
17831792
@@ -1843,7 +1852,7 @@ def llama_model_meta_count(model: llama_model_p, /) -> int:
18431852# LLAMA_API const char * llama_model_meta_key_str(enum llama_model_meta_key key);
18441853@ctypes_function ("llama_model_meta_key_str" , [ctypes .c_int ], ctypes .c_char_p )
18451854def llama_model_meta_key_str (key : int , / ) -> Optional [bytes ]:
1846- """Get sampling metadata key name. Returns None if the key is invalid. """
1855+ """Get sampling metadata key name. Returns None if the key is invalid"""
18471856 ...
18481857
18491858
@@ -1910,6 +1919,14 @@ def llama_model_desc(
19101919 ...
19111920
19121921
1922+ # // Get the model file type (quantization), e.g. LLAMA_FTYPE_MOSTLY_Q8_0
1923+ # LLAMA_API enum llama_ftype llama_model_ftype(const struct llama_model * model);
1924+ @ctypes_function ("llama_model_ftype" , [llama_model_p_ctypes ], ctypes .c_int )
1925+ def llama_model_ftype (model : llama_model_p , / ) -> int :
1926+ """Get the model file type (quantization), e.g. LLAMA_FTYPE_MOSTLY_Q8_0"""
1927+ ...
1928+
1929+
19131930# // Returns the total size of all the tensors in the model in bytes
19141931# LLAMA_API uint64_t llama_model_size(const struct llama_model * model);
19151932@ctypes_function ("llama_model_size" , [llama_model_p_ctypes ], ctypes .c_uint64 )
@@ -2485,7 +2502,9 @@ def llama_memory_can_shift(mem: llama_memory_t, /) -> bool:
24852502# LLAMA_API size_t llama_state_get_size(struct llama_context * ctx);
24862503@ctypes_function ("llama_state_get_size" , [llama_context_p_ctypes ], ctypes .c_size_t )
24872504def llama_state_get_size (ctx : llama_context_p , / ) -> int :
2488- """Returns the *actual* size in bytes of the state (logits, embedding and memory)"""
2505+ """Returns the *actual* size in bytes of the state
2506+ (logits, embedding and memory)
2507+ Only use when saving the state, not when restoring it, otherwise the size may be too small."""
24892508 ...
24902509
24912510
@@ -3046,9 +3065,12 @@ def llama_batch_free(batch: llama_batch, /):
30463065# struct llama_batch batch);
30473066@ctypes_function ("llama_encode" , [llama_context_p_ctypes , llama_batch ], ctypes .c_int32 )
30483067def llama_encode (ctx : llama_context_p , batch : llama_batch , / ) -> int :
3049- """Process a batch of tokens using the encoder.
3068+ """Process a batch of tokens.
3069+ In contrast to llama_decode() - this call does not use KV cache.
3070+ For encode-decoder contexts, processes the batch using the encoder.
3071+ Can store the encoder output internally for later use by the decoder's cross-attention layers.
30503072 0 - success
3051- < 0 - error"""
3073+ < 0 - error. the memory state is restored to the state before this call """
30523074 ...
30533075
30543076
@@ -3070,9 +3092,15 @@ def llama_encode(ctx: llama_context_p, batch: llama_batch, /) -> int:
30703092@ctypes_function ("llama_decode" , [llama_context_p_ctypes , llama_batch ], ctypes .c_int32 )
30713093def llama_decode (ctx : llama_context_p , batch : llama_batch , / ) -> int :
30723094 """Process a batch of tokens.
3095+ Requires the context to have a memory.
3096+ For encode-decoder contexts, processes the batch using the decoder.
3097+ Positive return values does not mean a fatal error, but rather a warning.
3098+ Upon fatal-error or abort, the ubatches that managed to be been processed will remain in the memory state of the context
3099+ To handle this correctly, query the memory state using llama_memory_seq_pos_min() and llama_memory_seq_pos_max()
3100+ Upon other return values, the memory state is restored to the state before this call
30733101 0 - success
30743102 1 - could not find a KV slot for the batch (try reducing the size of the batch or increase the context)
3075- 2 - aborted (processed ubatches will remain in the context's memory)
3103+ 2 - aborted (processed ubatches will remain in the context's memory)
30763104 -1 - invalid input batch
30773105 < -1 - fatal error (processed ubatches will remain in the context's memory)"""
30783106 ...
@@ -3108,15 +3136,15 @@ def llama_set_n_threads(
31083136# LLAMA_API int32_t llama_n_threads(struct llama_context * ctx);
31093137@ctypes_function ("llama_n_threads" , [llama_context_p_ctypes ], ctypes .c_int32 )
31103138def llama_n_threads (ctx : llama_context_p , / ) -> int :
3111- """Get the number of threads used for generation of a single token"""
3139+ """Get the number of threads used for generation of a single token. """
31123140 ...
31133141
31143142
31153143# // Get the number of threads used for prompt and batch processing (multiple token).
31163144# LLAMA_API int32_t llama_n_threads_batch(struct llama_context * ctx);
31173145@ctypes_function ("llama_n_threads_batch" , [llama_context_p_ctypes ], ctypes .c_int32 )
31183146def llama_n_threads_batch (ctx : llama_context_p , / ) -> int :
3119- """Get the number of threads used for prompt and batch processing (multiple token)"""
3147+ """Get the number of threads used for prompt and batch processing (multiple token). """
31203148 ...
31213149
31223150
@@ -3125,7 +3153,8 @@ def llama_n_threads_batch(ctx: llama_context_p, /) -> int:
31253153# LLAMA_API void llama_set_embeddings(struct llama_context * ctx, bool embeddings);
31263154@ctypes_function ("llama_set_embeddings" , [llama_context_p_ctypes , ctypes .c_bool ], None )
31273155def llama_set_embeddings (ctx : llama_context_p , embeddings : bool , / ):
3128- """Set whether the context outputs embeddings or not"""
3156+ """Set whether the context outputs embeddings or not
3157+ TODO: rename to avoid confusion with llama_get_embeddings()"""
31293158 ...
31303159
31313160
0 commit comments