@@ -488,6 +488,15 @@ def byref(obj: CtypesCData, offset: Optional[int] = None) -> CtypesRef[CtypesCDa |
488 | 488 | LLAMA_POOLING_TYPE_CLS = 2 |
489 | 489 | LLAMA_POOLING_TYPE_LAST = 3 |
490 | 490 |
|
| 491 | +# enum llama_attention_type { |
| 492 | +# LLAMA_ATTENTION_TYPE_UNSPECIFIED = -1, |
| 493 | +# LLAMA_ATTENTION_TYPE_CAUSAL = 0, |
| 494 | +# LLAMA_ATTENTION_TYPE_NON_CAUSAL = 1, |
| 495 | +# }; |
| 496 | +LLAMA_ATTENTION_TYPE_UNSPECIFIED = -1 |
| 497 | +LLAMA_ATTENTION_TYPE_CAUSAL = 0 |
| 498 | +LLAMA_ATTENTION_TYPE_NON_CAUSAL = 1 |
| 499 | + |
491 | 500 | # enum llama_split_mode { |
492 | 501 | # LLAMA_SPLIT_MODE_NONE = 0, // single GPU |
493 | 502 | # LLAMA_SPLIT_MODE_LAYER = 1, // split layers and KV across GPUs |
@@ -775,6 +784,7 @@ class llama_model_params(ctypes.Structure): |
775 | 784 |
|
776 | 785 | # enum llama_rope_scaling_type rope_scaling_type; // RoPE scaling type, from `enum llama_rope_scaling_type` |
777 | 786 | # enum llama_pooling_type pooling_type; // whether to pool (sum) embedding results by sequence id |
| 787 | +# enum llama_attention_type attention_type; // attention type to use for embeddings |
778 | 788 |
|
779 | 789 | # // ref: https://github.com/ggerganov/llama.cpp/pull/2054 |
780 | 790 | # float rope_freq_base; // RoPE base frequency, 0 = from model |
@@ -817,6 +827,7 @@ class llama_context_params(ctypes.Structure): |
817 | 827 | n_threads_batch (int): number of threads to use for batch processing |
818 | 828 | rope_scaling_type (int): RoPE scaling type, from `enum llama_rope_scaling_type` |
819 | 829 | pooling_type (int): whether to pool (sum) embedding results by sequence id (ignored if no pooling layer) |
| 830 | + attention_type (int): attention type to use for embeddings |
820 | 831 | rope_freq_base (float): RoPE base frequency, 0 = from model |
821 | 832 | rope_freq_scale (float): RoPE frequency scaling factor, 0 = from model |
822 | 833 | yarn_ext_factor (float): YaRN extrapolation mix factor, negative = from model |
@@ -847,6 +858,7 @@ class llama_context_params(ctypes.Structure): |
847 | 858 | n_threads_batch: int |
848 | 859 | rope_scaling_type: int |
849 | 860 | pooling_type: int |
| 861 | + attention_type: int |
850 | 862 | rope_freq_base: float |
851 | 863 | rope_freq_scale: float |
852 | 864 | yarn_ext_factor: float |
@@ -876,6 +888,7 @@ class llama_context_params(ctypes.Structure): |
876 | 888 | ("n_threads_batch", ctypes.c_uint32), |
877 | 889 | ("rope_scaling_type", ctypes.c_int), |
878 | 890 | ("pooling_type", ctypes.c_int), |
| 891 | + ("attention_type", ctypes.c_int), |
879 | 892 | ("rope_freq_base", ctypes.c_float), |
880 | 893 | ("rope_freq_scale", ctypes.c_float), |
881 | 894 | ("yarn_ext_factor", ctypes.c_float), |
@@ -2642,6 +2655,7 @@ def llama_token_eot(model: llama_model_p, /) -> int: ... |
2642 | 2655 | # /// @param tokens The tokens pointer must be large enough to hold the resulting tokens. |
2643 | 2656 | # /// @return Returns the number of tokens on success, no more than n_tokens_max |
2644 | 2657 | # /// @return Returns a negative number on failure - the number of tokens that would have been returned |
| 2658 | +# /// @param add_special Allow to add BOS and EOS tokens if model is configured to do so. |
2645 | 2659 | # /// @param parse_special Allow tokenizing special and/or control tokens which otherwise are not exposed and treated |
2646 | 2660 | # /// as plaintext. Does not insert a leading space. |
2647 | 2661 | # LLAMA_API int32_t llama_tokenize( |
@@ -2683,7 +2697,7 @@ def llama_tokenize( |
2683 | 2697 | text_len: The length of the text. |
2684 | 2698 | tokens: The tokens pointer must be large enough to hold the resulting tokens. |
2685 | 2699 | n_max_tokens: The maximum number of tokens to return. |
2686 | | - add_special: Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. Does not insert a leading space. |
| 2700 | + add_special: Allow adding special tokenns if the model is configured to do so. |
2687 | 2701 | parse_special: Allow parsing special tokens. |
2688 | 2702 |
|
2689 | 2703 | Returns: |
@@ -2696,13 +2710,14 @@ def llama_tokenize( |
2696 | 2710 | # // Token Id -> Piece. |
2697 | 2711 | # // Uses the vocabulary in the provided context. |
2698 | 2712 | # // Does not write null terminator to the buffer. |
2699 | | -# // User code is responsible to remove the leading whitespace of the first non-BOS token when decoding multiple tokens. |
| 2713 | +# // User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix') |
2700 | 2714 | # // @param special If true, special tokens are rendered in the output. |
2701 | 2715 | # LLAMA_API int32_t llama_token_to_piece( |
2702 | 2716 | # const struct llama_model * model, |
2703 | 2717 | # llama_token token, |
2704 | 2718 | # char * buf, |
2705 | 2719 | # int32_t length, |
| 2720 | +# int32_t lstrip, |
2706 | 2721 | # bool special); |
2707 | 2722 | @ctypes_function( |
2708 | 2723 | "llama_token_to_piece", |
@@ -2711,6 +2726,7 @@ def llama_tokenize( |
2711 | 2726 | llama_token, |
2712 | 2727 | ctypes.c_char_p, |
2713 | 2728 | ctypes.c_int32, |
| 2729 | + ctypes.c_int32, |
2714 | 2730 | ctypes.c_bool, |
2715 | 2731 | ], |
2716 | 2732 | ctypes.c_int32, |
@@ -2720,6 +2736,7 @@ def llama_token_to_piece( |
2720 | 2736 | token: Union[llama_token, int], |
2721 | 2737 | buf: Union[ctypes.c_char_p, bytes, CtypesArray[ctypes.c_char]], |
2722 | 2738 | length: Union[ctypes.c_int, int], |
| 2739 | + lstrip: Union[ctypes.c_int, int], |
2723 | 2740 | special: Union[ctypes.c_bool, bool], |
2724 | 2741 | /, |
2725 | 2742 | ) -> int: |
@@ -2733,10 +2750,61 @@ def llama_token_to_piece( |
2733 | 2750 | token: The token to convert. |
2734 | 2751 | buf: The buffer to write the token to. |
2735 | 2752 | length: The length of the buffer. |
| 2753 | + lstrip: The number of leading spaces to skip. |
2736 | 2754 | special: If true, special tokens are rendered in the output.""" |
2737 | 2755 | ... |
2738 | 2756 |
|
2739 | 2757 |
|
| 2758 | +# /// @details Convert the provided tokens into text (inverse of llama_tokenize()). |
| 2759 | +# /// @param text The char pointer must be large enough to hold the resulting text. |
| 2760 | +# /// @return Returns the number of chars/bytes on success, no more than text_len_max. |
| 2761 | +# /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned. |
| 2762 | +# /// @param remove_special Allow to remove BOS and EOS tokens if model is configured to do so. |
| 2763 | +# /// @param unparse_special If true, special tokens are rendered in the output. |
| 2764 | +# LLAMA_API int32_t llama_detokenize( |
| 2765 | +# const struct llama_model * model, |
| 2766 | +# const llama_token * tokens, |
| 2767 | +# int32_t n_tokens, |
| 2768 | +# char * text, |
| 2769 | +# int32_t text_len_max, |
| 2770 | +# bool remove_special, |
| 2771 | +# bool unparse_special); |
| 2772 | +@ctypes_function( |
| 2773 | + "llama_detokenize", |
| 2774 | + [ |
| 2775 | + llama_model_p_ctypes, |
| 2776 | + ctypes.POINTER(llama_token), |
| 2777 | + ctypes.c_int32, |
| 2778 | + ctypes.c_char_p, |
| 2779 | + ctypes.c_int32, |
| 2780 | + ctypes.c_bool, |
| 2781 | + ctypes.c_bool, |
| 2782 | + ], |
| 2783 | + ctypes.c_int32, |
| 2784 | +) |
| 2785 | +def llama_detokenize( |
| 2786 | + model: llama_model_p, |
| 2787 | + tokens: CtypesArray[llama_token], |
| 2788 | + n_tokens: Union[ctypes.c_int, int], |
| 2789 | + text: bytes, |
| 2790 | + text_len_max: Union[ctypes.c_int, int], |
| 2791 | + remove_special: Union[ctypes.c_bool, bool], |
| 2792 | + unparse_special: Union[ctypes.c_bool, bool], |
| 2793 | + /, |
| 2794 | +) -> int: |
| 2795 | + """Convert the provided tokens into text (inverse of llama_tokenize()). |
| 2796 | +
|
| 2797 | + Args: |
| 2798 | + model: The model to use for tokenization. |
| 2799 | + tokens: The tokens to convert. |
| 2800 | + n_tokens: The number of tokens. |
| 2801 | + text: The buffer to write the text to. |
| 2802 | + text_len_max: The length of the buffer. |
| 2803 | + remove_special: Allow to remove BOS and EOS tokens if model is configured to do so. |
| 2804 | + unparse_special: If true, special tokens are rendered in the output.""" |
| 2805 | + ... |
| 2806 | + |
| 2807 | + |
2740 | 2808 | # /// Apply chat template. Inspired by hf apply_chat_template() on python. |
2741 | 2809 | # /// Both "model" and "custom_template" are optional, but at least one is required. "custom_template" has higher precedence than "model" |
2742 | 2810 | # /// NOTE: This function does not use a jinja parser. It only support a pre-defined list of template. See more: https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template |
|