| 805 | |
| 806 | |
| 807 | class OutputFile: |
| 808 | def __init__(self, fname_out: Path, arch: gguf.MODEL_ARCH, endianess:gguf.GGUFEndian=gguf.GGUFEndian.LITTLE) -> None: |
| 809 | self.gguf = gguf.GGUFWriter(fname_out, gguf.MODEL_ARCH_NAMES[arch], endianess=endianess) |
| 810 | |
| 811 | def add_meta_arch(self, params: Params) -> None: |
| 812 | name = "LLaMA" |
| 813 | |
| 814 | # TODO: better logic to determine model name |
| 815 | if params.n_ctx == 4096: |
| 816 | name = "LLaMA v2" |
| 817 | elif params.path_model is not None: |
| 818 | name = str(params.path_model).split('/')[-1] |
| 819 | |
| 820 | self.gguf.add_name (name) |
| 821 | self.gguf.add_context_length (params.n_ctx) |
| 822 | self.gguf.add_embedding_length (params.n_embd) |
| 823 | self.gguf.add_block_count (params.n_layer) |
| 824 | self.gguf.add_feed_forward_length (params.n_ff) |
| 825 | self.gguf.add_rope_dimension_count(params.n_embd // params.n_head) |
| 826 | self.gguf.add_head_count (params.n_head) |
| 827 | self.gguf.add_head_count_kv (params.n_head_kv) |
| 828 | self.gguf.add_layer_norm_rms_eps (params.f_norm_eps) |
| 829 | |
| 830 | if params.f_rope_freq_base is not None: |
| 831 | self.gguf.add_rope_freq_base(params.f_rope_freq_base) |
| 832 | |
| 833 | if params.rope_scaling_type: |
| 834 | assert params.f_rope_scale is not None |
| 835 | self.gguf.add_rope_scaling_type(params.rope_scaling_type) |
| 836 | self.gguf.add_rope_scaling_factor(params.f_rope_scale) |
| 837 | |
| 838 | if params.n_orig_ctx is not None: |
| 839 | self.gguf.add_rope_scaling_orig_ctx_len(params.n_orig_ctx) |
| 840 | |
| 841 | if params.rope_finetuned is not None: |
| 842 | self.gguf.add_rope_scaling_finetuned(params.rope_finetuned) |
| 843 | |
| 844 | if params.ftype is not None: |
| 845 | self.gguf.add_file_type(params.ftype) |
| 846 | |
| 847 | def add_meta_vocab(self, vocab: Vocab) -> None: |
| 848 | tokens = [] |
| 849 | scores = [] |
| 850 | toktypes = [] |
| 851 | # NOTE: `all_tokens` returns the base vocabulary and added tokens |
| 852 | for text, score, toktype in vocab.all_tokens(): |
| 853 | tokens.append(text) |
| 854 | scores.append(score) |
| 855 | toktypes.append(toktype) |
| 856 | |
| 857 | if isinstance(vocab, SentencePieceVocab): |
| 858 | self.gguf.add_tokenizer_model("llama") |
| 859 | elif isinstance(vocab, BpeVocab): |
| 860 | self.gguf.add_tokenizer_model("gpt2") |
| 861 | else: |
| 862 | raise ValueError('Unknown vocab type: Not BpeVocab or SentencePieceVocab') |
| 863 | self.gguf.add_token_list(tokens) |
| 864 | self.gguf.add_token_scores(scores) |
no outgoing calls
no test coverage detected