mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-26 03:14:35 +00:00
6381d4e110
* gguf : first API pass
* gguf : read header + meta data
* gguf : read tensor info
* gguf : initial model loading - not tested
* gguf : add gguf_get_tensor_name()
* gguf : do not support passing existing ggml_context to gguf_init
* gguf : simplify gguf_get_val
* gguf : gguf.c is now part of ggml.c
* gguf : read / write sample models
* gguf : add comments
* refactor : reduce code duplication and better API (#2415)
* gguf : expose the gguf_type enum through the API for now
* gguf : add array support
* gguf.py : some code style changes
* convert.py : start a new simplified implementation by removing old stuff
* convert.py : remove GGML vocab + other obsolete stuff
* GGUF : write tensor (#2426)
* WIP: Write tensor
* GGUF : Support writing tensors in Python
* refactor : rm unused import and upd todos
* fix : fix errors upd writing example
* rm example.gguf
* gitignore *.gguf
* undo formatting
* gguf : add gguf_find_key (#2438)
* gguf.cpp : find key example
* ggml.h : add gguf_find_key
* ggml.c : add gguf_find_key
* gguf : fix writing tensors
* gguf : do not hardcode tensor names to read
* gguf : write sample tensors to read
* gguf : add tokenization constants
* quick and dirty conversion example
* gguf : fix writing gguf arrays
* gguf : write tensors one by one and code reuse
* gguf : fix writing gguf arrays
* gguf : write tensors one by one
* gguf : write tensors one by one
* gguf : write tokenizer data
* gguf : upd gguf conversion script
* Update convert-llama-h5-to-gguf.py
* gguf : handle already encoded string
* ggml.h : get array str and f32
* ggml.c : get arr str and f32
* gguf.py : support any type
* Update convert-llama-h5-to-gguf.py
* gguf : fix set is not subscriptable
* gguf : update convert-llama-h5-to-gguf.py
* constants.py : add layer norm eps
* gguf.py : add layer norm eps and merges
* ggml.h : increase GGML_MAX_NAME to 64
* ggml.c : add gguf_get_arr_n
* Update convert-llama-h5-to-gguf.py
* add gptneox gguf example
* Makefile : add gptneox gguf example
* Update convert-llama-h5-to-gguf.py
* add gptneox gguf example
* Update convert-llama-h5-to-gguf.py
* Update convert-gptneox-h5-to-gguf.py
* Update convert-gptneox-h5-to-gguf.py
* Update convert-llama-h5-to-gguf.py
* gguf : support custom alignment value
* gguf : fix typo in function call
* gguf : mmap tensor data example
* fix : update convert-llama-h5-to-gguf.py
* Update convert-llama-h5-to-gguf.py
* convert-gptneox-h5-to-gguf.py : Special tokens
* gptneox-main.cpp : special tokens
* Update gptneox-main.cpp
* constants.py : special tokens
* gguf.py : accumulate kv and tensor info data + special tokens
* convert-gptneox-h5-to-gguf.py : accumulate kv and ti + special tokens
* gguf : gguf counterpart of llama-util.h
* gguf-util.h : update note
* convert-llama-h5-to-gguf.py : accumulate kv / ti + special tokens
* convert-llama-h5-to-gguf.py : special tokens
* Delete gptneox-common.cpp
* Delete gptneox-common.h
* convert-gptneox-h5-to-gguf.py : gpt2bpe tokenizer
* gptneox-main.cpp : gpt2 bpe tokenizer
* gpt2 bpe tokenizer (handles merges and unicode)
* Makefile : remove gptneox-common
* gguf.py : bytesarray for gpt2bpe tokenizer
* cmpnct_gpt2bpe.hpp : comments
* gguf.py : use custom alignment if present
* gguf : minor stuff
* Update gptneox-main.cpp
* map tensor names
* convert-gptneox-h5-to-gguf.py : map tensor names
* convert-llama-h5-to-gguf.py : map tensor names
* gptneox-main.cpp : map tensor names
* gguf : start implementing libllama in GGUF (WIP)
* gguf : start implementing libllama in GGUF (WIP)
* rm binary commited by mistake
* upd .gitignore
* gguf : calculate n_mult
* gguf : inference with 7B model working (WIP)
* gguf : rm deprecated function
* gguf : start implementing gguf_file_saver (WIP)
* gguf : start implementing gguf_file_saver (WIP)
* gguf : start implementing gguf_file_saver (WIP)
* gguf : add gguf_get_kv_type
* gguf : add gguf_get_kv_type
* gguf : write metadata in gguf_file_saver (WIP)
* gguf : write metadata in gguf_file_saver (WIP)
* gguf : write metadata in gguf_file_saver
* gguf : rm references to old file formats
* gguf : shorter name for member variable
* gguf : rm redundant method
* gguf : get rid of n_mult, read n_ff from file
* Update gguf_tensor_map.py
* Update gptneox-main.cpp
* gguf : rm references to old file magics
* gguf : start implementing quantization (WIP)
* gguf : start implementing quantization (WIP)
* gguf : start implementing quantization (WIP)
* gguf : start implementing quantization (WIP)
* gguf : start implementing quantization (WIP)
* gguf : start implementing quantization (WIP)
* gguf : quantization is working
* gguf : roper closing of file
* gguf.py : no need to convert tensors twice
* convert-gptneox-h5-to-gguf.py : no need to convert tensors twice
* convert-llama-h5-to-gguf.py : no need to convert tensors twice
* convert-gptneox-h5-to-gguf.py : simplify nbytes
* convert-llama-h5-to-gguf.py : simplify nbytes
* gptneox-main.cpp : n_layer --> n_block
* constants.py : n_layer --> n_block
* gguf.py : n_layer --> n_block
* convert-gptneox-h5-to-gguf.py : n_layer --> n_block
* convert-llama-h5-to-gguf.py : n_layer --> n_block
* gptneox-main.cpp : n_layer --> n_block
* Update gguf_tensor_map.py
* convert-gptneox-h5-to-gguf.py : load model in parts to save memory
* convert-llama-h5-to-gguf.py : load model in parts to save memory
* convert : write more metadata for LLaMA
* convert : rm quantization version
* convert-gptneox-h5-to-gguf.py : add file_type key
* gptneox-main.cpp : add file_type key
* fix conflicts
* gguf : add todos and comments
* convert-gptneox-h5-to-gguf.py : tensor name map changes
* Create gguf_namemap.py : tensor name map changes
* Delete gguf_tensor_map.py
* gptneox-main.cpp : tensor name map changes
* convert-llama-h5-to-gguf.py : fixes
* gguf.py : dont add empty strings
* simple : minor style changes
* gguf : use UNIX line ending
* Create convert-llama-7b-pth-to-gguf.py
* llama : sync gguf-llama.cpp with latest llama.cpp (#2608)
* llama : sync gguf-llama.cpp with latest llama.cpp
* minor : indentation + assert
* llama : refactor gguf_buffer and gguf_ctx_buffer
* llama : minor
* gitignore : add gptneox-main
* llama : tokenizer fixes (#2549)
* Merge tokenizer fixes into the gguf branch.
* Add test vocabularies
* convert : update convert-new.py with tokenizer fixes (#2614)
* Merge tokenizer fixes into the gguf branch.
* Add test vocabularies
* Adapt convert-new.py (and fix a clang-cl compiler error on windows)
* llama : sync gguf-llama with llama (#2613)
* llama : sync gguf-llama with llama
* tests : fix build + warnings (test-tokenizer-1 still fails)
* tests : fix wstring_convert
* convert : fix layer names
* llama : sync gguf-llama.cpp
* convert : update HF converter to new tokenizer voodoo magics
* llama : update tokenizer style
* convert-llama-h5-to-gguf.py : add token types
* constants.py : add token types
* gguf.py : add token types
* convert-llama-7b-pth-to-gguf.py : add token types
* gguf-llama.cpp : fix n_head_kv
* convert-llama-h5-to-gguf.py : add 70b gqa support
* gguf.py : add tensor data layout
* convert-llama-h5-to-gguf.py : add tensor data layout
* convert-llama-7b-pth-to-gguf.py : add tensor data layout
* gptneox-main.cpp : add tensor data layout
* convert-llama-h5-to-gguf.py : clarify the reverse permute
* llama : refactor model loading code (#2620)
* llama : style formatting + remove helper methods
* llama : fix quantization using gguf tool
* llama : simplify gguf_file_saver
* llama : fix method names
* llama : simplify write_header()
* llama : no need to pass full file loader to the file saver
just gguf_ctx
* llama : gguf_file_saver write I32
* llama : refactor tensor names (#2622)
* gguf: update tensor names searched in quantization
* gguf : define tensor names as constants
* gguf : initial write API (not tested yet)
* gguf : write to file API (not tested)
* gguf : initial write API ready + example
* gguf : fix header write
* gguf : fixes + simplify example + add ggml_nbytes_pad()
* gguf : minor
* llama : replace gguf_file_saver with new gguf write API
* gguf : streaming support when writing files
* gguf : remove oboslete write methods
* gguf : remove obosolete gguf_get_arr_xxx API
* llama : simplify gguf_file_loader
* llama : move hparams and vocab from gguf_file_loader to llama_model_loader
* llama : merge gguf-util.h in llama.cpp
* llama : reorder definitions in .cpp to match .h
* llama : minor simplifications
* llama : refactor llama_model_loader (WIP)
wip : remove ggml_ctx from llama_model_loader
wip : merge gguf_file_loader in llama_model_loader
* llama : fix shape prints
* llama : fix Windows build + fix norm_rms_eps key
* llama : throw error on missing KV paris in model meta data
* llama : improve printing + log meta data
* llama : switch print order of meta data
---------
Co-authored-by: M. Yusuf Sarıgöz <yusufsarigoz@gmail.com>
* gguf : deduplicate (#2629)
* gguf : better type names
* dedup : CPU + Metal is working
* ggml : fix warnings about unused results
* llama.cpp : fix line feed and compiler warning
* llama : fix strncpy warning + note token_to_str does not write null
* llama : restore the original load/save session implementation
Will migrate this to GGUF in the future
* convert-llama-h5-to-gguf.py : support alt ctx param name
* ggml : assert when using ggml_mul with non-F32 src1
* examples : dedup simple
---------
Co-authored-by: klosax <131523366+klosax@users.noreply.github.com>
* gguf.py : merge all files in gguf.py
* convert-new.py : pick #2427 for HF 70B support
* examples/gguf : no need to keep q option for quantization any more
* llama.cpp : print actual model size
* llama.cpp : use ggml_elements()
* convert-new.py : output gguf (#2635)
* convert-new.py : output gguf (WIP)
* convert-new.py : add gguf key-value pairs
* llama : add hparams.ctx_train + no longer print ftype
* convert-new.py : minor fixes
* convert-new.py : vocab-only option should work now
* llama : fix tokenizer to use llama_char_to_byte
* tests : add new ggml-vocab-llama.gguf
* convert-new.py : tensor name mapping
* convert-new.py : add map for skipping tensor serialization
* convert-new.py : convert script now works
* gguf.py : pick some of the refactoring from #2644
* convert-new.py : minor fixes
* convert.py : update to support GGUF output
* Revert "ci : disable CI temporary to not waste energy"
This reverts commit 7e82d25f40
.
* convert.py : n_head_kv optional and .gguf file extension
* convert.py : better always have n_head_kv and default it to n_head
* llama : sync with recent PRs on master
* editorconfig : ignore models folder
ggml-ci
* ci : update ".bin" to ".gguf" extension
ggml-ci
* llama : fix llama_model_loader memory leak
* gptneox : move as a WIP example
* llama : fix lambda capture
ggml-ci
* ggml : fix bug in gguf_set_kv
ggml-ci
* common.h : .bin --> .gguf
* quantize-stats.cpp : .bin --> .gguf
* convert.py : fix HF tensor permuting / unpacking
ggml-ci
* llama.cpp : typo
* llama : throw error if gguf fails to init from file
ggml-ci
* llama : fix tensor name grepping during quantization
ggml-ci
* gguf.py : write tensors in a single pass (#2644)
* gguf : single pass for writing tensors + refactoring writer
* gguf : single pass for writing tensors + refactoring writer
* gguf : single pass for writing tensors + refactoring writer
* gguf : style fixes in simple conversion script
* gguf : refactor gptneox conversion script
* gguf : rename h5 to hf (for HuggingFace)
* gguf : refactor pth to gguf conversion script
* gguf : rm file_type key and method
* gguf.py : fix vertical alignment
* gguf.py : indentation
---------
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
* convert-gptneox-hf-to-gguf.py : fixes
* gguf.py : gptneox mapping
* convert-llama-hf-to-gguf.py : fixes
* convert-llama-7b-pth-to-gguf.py : fixes
* ggml.h : reverse GGUF_MAGIC
* gguf.py : reverse GGUF_MAGIC
* test-tokenizer-0.cpp : fix warning
* llama.cpp : print kv general.name
* llama.cpp : get special token kv and linefeed token id
* llama : print number of tensors per type + print arch + style
* tests : update vocab file with new magic
* editorconfig : fix whitespaces
* llama : re-order functions
* llama : remove C++ API + reorganize common source in /common dir
* llama : minor API updates
* llama : avoid hardcoded special tokens
* llama : fix MPI build
ggml-ci
* llama : introduce enum llama_vocab_type + remove hardcoded string constants
* convert-falcon-hf-to-gguf.py : falcon HF --> gguf conversion, not tested
* falcon-main.cpp : falcon inference example
* convert-falcon-hf-to-gguf.py : remove extra kv
* convert-gptneox-hf-to-gguf.py : remove extra kv
* convert-llama-7b-pth-to-gguf.py : remove extra kv
* convert-llama-hf-to-gguf.py : remove extra kv
* gguf.py : fix for falcon 40b
* falcon-main.cpp : fix for falcon 40b
* convert-falcon-hf-to-gguf.py : update ref
* convert-falcon-hf-to-gguf.py : add tensor data layout
* cmpnct_gpt2bpe.hpp : fixes
* falcon-main.cpp : fixes
* gptneox-main.cpp : fixes
* cmpnct_gpt2bpe.hpp : remove non-general stuff
* Update examples/server/README.md
Co-authored-by: slaren <slarengh@gmail.com>
* cmpnct_gpt2bpe.hpp : cleanup
* convert-llama-hf-to-gguf.py : special tokens
* convert-llama-7b-pth-to-gguf.py : special tokens
* convert-permute-debug.py : permute debug print
* convert-permute-debug-master.py : permute debug for master
* convert-permute-debug.py : change permute type of attn_q
* convert.py : 70b model working (change attn_q permute)
* Delete convert-permute-debug-master.py
* Delete convert-permute-debug.py
* convert-llama-hf-to-gguf.py : fix attn_q permute
* gguf.py : fix rope scale kv
* convert-llama-hf-to-gguf.py : rope scale and added tokens
* convert-llama-7b-pth-to-gguf.py : rope scale and added tokens
* llama.cpp : use rope scale kv
* convert-llama-7b-pth-to-gguf.py : rope scale fix
* convert-llama-hf-to-gguf.py : rope scale fix
* py : fix whitespace
* gguf : add Python script to convert GGMLv3 LLaMA models to GGUF (#2682)
* First pass at converting GGMLv3 LLaMA models to GGUF
* Cleanups, better output during conversion
* Fix vocab space conversion logic
* More vocab conversion fixes
* Add description to converted GGUF files
* Improve help text, expand warning
* Allow specifying name and description for output GGUF
* Allow overriding vocab and hyperparams from original model metadata
* Use correct params override var name
* Fix wrong type size for Q8_K
Better handling of original style metadata
* Set default value for gguf add_tensor raw_shape KW arg
* llama : improve token type support (#2668)
* Merge tokenizer fixes into the gguf branch.
* Add test vocabularies
* Adapt convert-new.py (and fix a clang-cl compiler error on windows)
* Improved tokenizer test
But does it work on MacOS?
* Improve token type support
- Added @klosax code to convert.py
- Improved token type support in vocabulary
* Exclude platform dependent tests
* More sentencepiece compatibility by eliminating magic numbers
* Restored accidentally removed comment
* llama : add API for token type
ggml-ci
* tests : use new tokenizer type API (#2692)
* Merge tokenizer fixes into the gguf branch.
* Add test vocabularies
* Adapt convert-new.py (and fix a clang-cl compiler error on windows)
* Improved tokenizer test
But does it work on MacOS?
* Improve token type support
- Added @klosax code to convert.py
- Improved token type support in vocabulary
* Exclude platform dependent tests
* More sentencepiece compatibility by eliminating magic numbers
* Restored accidentally removed comment
* Improve commentary
* Use token type API in test-tokenizer-1.cpp
* py : cosmetics
* readme : add notice about new file format
ggml-ci
---------
Co-authored-by: M. Yusuf Sarıgöz <yusufsarigoz@gmail.com>
Co-authored-by: klosax <131523366+klosax@users.noreply.github.com>
Co-authored-by: goerch <jhr.walter@t-online.de>
Co-authored-by: slaren <slarengh@gmail.com>
Co-authored-by: Kerfuffle <44031344+KerfuffleV2@users.noreply.github.com>
719 lines
27 KiB
Python
719 lines
27 KiB
Python
import shutil
|
|
import sys
|
|
import struct
|
|
import tempfile
|
|
import numpy as np
|
|
|
|
from enum import IntEnum, auto
|
|
from typing import Any, IO, List, Optional
|
|
|
|
#
|
|
# constants
|
|
#
|
|
|
|
GGUF_MAGIC = 0x46554747
|
|
GGUF_VERSION = 1
|
|
GGUF_DEFAULT_ALIGNMENT = 32
|
|
|
|
# general
|
|
KEY_GENERAL_ARCHITECTURE = "general.architecture"
|
|
KEY_GENERAL_QUANTIZATION_VERSION = "general.quantization_version"
|
|
KEY_GENERAL_ALIGNMENT = "general.alignment"
|
|
KEY_GENERAL_NAME = "general.name"
|
|
KEY_GENERAL_AUTHOR = "general.author"
|
|
KEY_GENERAL_URL = "general.url"
|
|
KEY_GENERAL_DESCRIPTION = "general.description"
|
|
KEY_GENERAL_LICENSE = "general.license"
|
|
KEY_GENERAL_SOURCE_URL = "general.source.url"
|
|
KEY_GENERAL_SOURCE_HF_REPO = "general.source.hugginface.repository"
|
|
|
|
# LLM
|
|
KEY_LLM_CONTEXT_LENGTH = "{arch}.context_length"
|
|
KEY_LLM_EMBEDDING_LENGTH = "{arch}.embedding_length"
|
|
KEY_LLM_BLOCK_COUNT = "{arch}.block_count"
|
|
KEY_LLM_FEED_FORWARD_LENGTH = "{arch}.feed_forward_length"
|
|
KEY_LLM_USE_PARALLEL_RESIDUAL = "{arch}.use_parallel_residual"
|
|
KEY_LLM_TENSOR_DATA_LAYOUT = "{arch}.tensor_data_layout"
|
|
|
|
# attention
|
|
KEY_ATTENTION_HEAD_COUNT = "{arch}.attention.head_count"
|
|
KEY_ATTENTION_HEAD_COUNT_KV = "{arch}.attention.head_count_kv"
|
|
KEY_ATTENTION_MAX_ALIBI_BIAS = "{arch}.attention.max_alibi_bias"
|
|
KEY_ATTENTION_CLAMP_KQV = "{arch}.attention.clamp_kqv"
|
|
KEY_ATTENTION_LAYERNORM_EPS = "{arch}.attention.layer_norm_epsilon"
|
|
KEY_ATTENTION_LAYERNORM_RMS_EPS = "{arch}.attention.layer_norm_rms_epsilon"
|
|
|
|
# RoPE
|
|
KEY_ROPE_DIMENSION_COUNT = "{arch}.rope.dimension_count"
|
|
KEY_ROPE_SCALE_LINEAR = "{arch}.rope.scale_linear"
|
|
|
|
# tokenization
|
|
KEY_TOKENIZER_MODEL = "tokenizer.ggml.model"
|
|
KEY_TOKENIZER_LIST = "tokenizer.ggml.tokens"
|
|
KEY_TOKENIZER_TOKEN_TYPE = "tokenizer.ggml.token_type"
|
|
KEY_TOKENIZER_SCORES = "tokenizer.ggml.scores"
|
|
KEY_TOKENIZER_MERGES = "tokenizer.ggml.merges"
|
|
KEY_TOKENIZER_BOS_ID = "tokenizer.ggml.bos_token_id"
|
|
KEY_TOKENIZER_EOS_ID = "tokenizer.ggml.eos_token_id"
|
|
KEY_TOKENIZER_UNK_ID = "tokenizer.ggml.unknown_token_id"
|
|
KEY_TOKENIZER_SEP_ID = "tokenizer.ggml.seperator_token_id"
|
|
KEY_TOKENIZER_PAD_ID = "tokenizer.ggml.padding_token_id"
|
|
KEY_TOKENIZER_HF_JSON = "tokenizer.huggingface.json"
|
|
KEY_TOKENIZER_RWKV = "tokenizer.rwkv.world"
|
|
|
|
|
|
#
|
|
# recommended mapping of model tensor names for storage in gguf
|
|
#
|
|
|
|
|
|
class MODEL_ARCH(IntEnum):
|
|
LLAMA = auto()
|
|
FALCON = auto()
|
|
GPT2 = auto()
|
|
GPTJ = auto()
|
|
GPTNEOX = auto()
|
|
MPT = auto()
|
|
|
|
|
|
class MODEL_TENSOR(IntEnum):
|
|
TOKEN_EMBD = auto()
|
|
POS_EMBD = auto()
|
|
OUTPUT = auto()
|
|
OUTPUT_NORM = auto()
|
|
ROPE_FREQS = auto()
|
|
ATTN_Q = auto()
|
|
ATTN_K = auto()
|
|
ATTN_V = auto()
|
|
ATTN_QKV = auto()
|
|
ATTN_OUT = auto()
|
|
ATTN_NORM = auto()
|
|
ATTN_NORM_2 = auto()
|
|
ATTN_ROT_EMBD = auto()
|
|
FFN_GATE = auto()
|
|
FFN_DOWN = auto()
|
|
FFN_UP = auto()
|
|
FFN_NORM = auto()
|
|
|
|
|
|
MODEL_ARCH_NAMES = {
|
|
MODEL_ARCH.LLAMA: "llama",
|
|
MODEL_ARCH.FALCON: "falcon",
|
|
MODEL_ARCH.GPT2: "gpt2",
|
|
MODEL_ARCH.GPTJ: "gptj",
|
|
MODEL_ARCH.GPTNEOX: "gptneox",
|
|
MODEL_ARCH.MPT: "mpt",
|
|
}
|
|
|
|
MODEL_TENSOR_NAMES = {
|
|
MODEL_ARCH.LLAMA: {
|
|
MODEL_TENSOR.TOKEN_EMBD: "token_embd",
|
|
MODEL_TENSOR.OUTPUT_NORM: "output_norm",
|
|
MODEL_TENSOR.OUTPUT: "output",
|
|
MODEL_TENSOR.ROPE_FREQS: "rope_freqs",
|
|
MODEL_TENSOR.ATTN_NORM: "blk.{bid}.attn_norm",
|
|
MODEL_TENSOR.ATTN_Q: "blk.{bid}.attn_q",
|
|
MODEL_TENSOR.ATTN_K: "blk.{bid}.attn_k",
|
|
MODEL_TENSOR.ATTN_V: "blk.{bid}.attn_v",
|
|
MODEL_TENSOR.ATTN_OUT: "blk.{bid}.attn_output",
|
|
MODEL_TENSOR.ATTN_ROT_EMBD: "blk.{bid}.attn_rot_embd",
|
|
MODEL_TENSOR.FFN_NORM: "blk.{bid}.ffn_norm",
|
|
MODEL_TENSOR.FFN_GATE: "blk.{bid}.ffn_gate",
|
|
MODEL_TENSOR.FFN_DOWN: "blk.{bid}.ffn_down",
|
|
MODEL_TENSOR.FFN_UP: "blk.{bid}.ffn_up",
|
|
},
|
|
MODEL_ARCH.GPTNEOX: {
|
|
MODEL_TENSOR.TOKEN_EMBD: "token_embd",
|
|
MODEL_TENSOR.OUTPUT_NORM: "output_norm",
|
|
MODEL_TENSOR.OUTPUT: "output",
|
|
MODEL_TENSOR.ATTN_NORM: "blk.{bid}.attn_norm",
|
|
MODEL_TENSOR.ATTN_QKV: "blk.{bid}.attn_qkv",
|
|
MODEL_TENSOR.ATTN_OUT: "blk.{bid}.attn_output",
|
|
MODEL_TENSOR.FFN_NORM: "blk.{bid}.ffn_norm",
|
|
MODEL_TENSOR.FFN_DOWN: "blk.{bid}.ffn_down",
|
|
MODEL_TENSOR.FFN_UP: "blk.{bid}.ffn_up",
|
|
},
|
|
MODEL_ARCH.FALCON: {
|
|
MODEL_TENSOR.TOKEN_EMBD: "token_embd",
|
|
MODEL_TENSOR.OUTPUT_NORM: "output_norm",
|
|
MODEL_TENSOR.OUTPUT: "output",
|
|
MODEL_TENSOR.ATTN_NORM: "blk.{bid}.attn_norm",
|
|
MODEL_TENSOR.ATTN_NORM_2: "blk.{bid}.attn_norm_2",
|
|
MODEL_TENSOR.ATTN_QKV: "blk.{bid}.attn_qkv",
|
|
MODEL_TENSOR.ATTN_OUT: "blk.{bid}.attn_output",
|
|
MODEL_TENSOR.FFN_DOWN: "blk.{bid}.ffn_down",
|
|
MODEL_TENSOR.FFN_UP: "blk.{bid}.ffn_up",
|
|
},
|
|
MODEL_ARCH.GPT2: {
|
|
# TODO
|
|
},
|
|
# TODO
|
|
}
|
|
|
|
# tensors that will not be serialized
|
|
MODEL_TENSOR_SKIP = {
|
|
MODEL_ARCH.LLAMA: [
|
|
MODEL_TENSOR.ROPE_FREQS,
|
|
MODEL_TENSOR.ATTN_ROT_EMBD,
|
|
],
|
|
}
|
|
|
|
|
|
# TODO: the following helper functions should be removed
|
|
# instead, get_tensor_name_map should return tuples of (name, MODEL_TENSOR)
|
|
# however, my Python is very bad, and I couldn't figure out how to do this, hence these functions
|
|
# REMOVE
|
|
def should_skip_tensor_TMP(arch: MODEL_ARCH, n_blocks: int, name: str) -> bool:
|
|
for skip in MODEL_TENSOR_SKIP.get(arch, []):
|
|
for i in range(n_blocks):
|
|
if name == MODEL_TENSOR_NAMES[arch][skip].format(bid=i):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def get_tensor_name_map(arch: MODEL_ARCH, n_blocks: int) -> dict:
|
|
tensor_map = {}
|
|
|
|
# Token embeddings
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.TOKEN_EMBD, None)
|
|
|
|
tensor_map["gpt_neox.embed_in"] = mapped_to # gptneox
|
|
tensor_map["transformer.wte"] = mapped_to # gpt2 mpt
|
|
tensor_map["transformer.word_embeddings"] = mapped_to # falcon
|
|
tensor_map["model.embed_tokens"] = mapped_to # llama-hf
|
|
tensor_map["tok_embeddings"] = mapped_to # llama-pth
|
|
|
|
# Position embeddings
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.POS_EMBD, None)
|
|
|
|
tensor_map["transformer.wpe"] = mapped_to # gpt2
|
|
|
|
# Output
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.OUTPUT, None)
|
|
|
|
tensor_map["embed_out"] = mapped_to # gptneox
|
|
tensor_map["lm_head"] = mapped_to # gpt2 mpt falcon llama-hf
|
|
tensor_map["output"] = mapped_to # llama-pth
|
|
|
|
# Output norm
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.OUTPUT_NORM, None)
|
|
|
|
tensor_map["gpt_neox.final_layer_norm"] = mapped_to # gptneox
|
|
tensor_map["transformer.ln_f"] = mapped_to # gpt2 falcon
|
|
tensor_map["transformer.norm_f"] = mapped_to # mpt
|
|
tensor_map["model.norm"] = mapped_to # llama-hf
|
|
tensor_map["norm"] = mapped_to # llama-pth
|
|
|
|
# Rope frequencies
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ROPE_FREQS, None)
|
|
|
|
tensor_map["rope.freqs"] = mapped_to # llama-pth
|
|
|
|
# Attention and feed-forward blocks
|
|
for i in range(0, n_blocks):
|
|
# Attention norm
|
|
# TODO: is there are simpler way to write these 2 lines in Python?
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_NORM, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".input_layernorm"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".ln_1"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".norm_1"] = mapped_to # mpt
|
|
tensor_map["transformer.h."+str(i)+".input_layernorm"] = mapped_to # falcon7b
|
|
tensor_map["transformer.h."+str(i)+".ln_mlp"] = mapped_to # falcon40b
|
|
tensor_map["model.layers."+str(i)+".input_layernorm"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention_norm"] = mapped_to # llama-pth
|
|
|
|
# Attention norm 2
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_NORM_2, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["transformer.h."+str(i)+".ln_attn"] = mapped_to # falcon40b
|
|
|
|
# Attention query-key-value
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_QKV, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".attention.query_key_value"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".attn.c_attn"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".attn.Wqkv"] = mapped_to # mpt
|
|
tensor_map["transformer.h."+str(i)+".self_attention.query_key_value"] = mapped_to # falcon
|
|
|
|
# Attention query
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_Q, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["model.layers."+str(i)+".self_attn.q_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention.wq"] = mapped_to # llama-pth
|
|
|
|
# Attention key
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_K, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["model.layers."+str(i)+".self_attn.k_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention.wk"] = mapped_to # llama-pth
|
|
|
|
# Attention value
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_V, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["model.layers."+str(i)+".self_attn.v_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention.wv"] = mapped_to # llama-pth
|
|
|
|
# Attention output
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_OUT, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".attention.dense"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".attn.c_proj"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".attn.out_proj"] = mapped_to # mpt
|
|
tensor_map["transformer.h."+str(i)+".self_attention.dense"] = mapped_to # falcon
|
|
tensor_map["model.layers."+str(i)+".self_attn.o_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention.wo"] = mapped_to # llama-pth
|
|
|
|
# Rotary embeddings
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.ATTN_ROT_EMBD, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["model.layers."+str(i)+".self_attn.rotary_emb.inv_freq"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".attention.inner_attention.rope.freqs"] = mapped_to # llama-pth
|
|
|
|
# Feed-forward norm
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.FFN_NORM, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".post_attention_layernorm"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".ln_2"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".norm_2"] = mapped_to # mpt
|
|
tensor_map["model.layers."+str(i)+".post_attention_layernorm"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".ffn_norm"] = mapped_to # llama-pth
|
|
|
|
# Feed-forward up
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.FFN_UP, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".mlp.dense_h_to_4h"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".mlp.c_fc"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".ffn.up_proj"] = mapped_to # mpt
|
|
tensor_map["transformer.h."+str(i)+".mlp.dense_h_to_4h"] = mapped_to # falcon
|
|
tensor_map["model.layers."+str(i)+".mlp.up_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".feed_forward.w3"] = mapped_to # llama-pth
|
|
|
|
# Feed-forward gate
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.FFN_GATE, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["model.layers."+str(i)+".mlp.gate_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".feed_forward.w1"] = mapped_to # llama-pth
|
|
|
|
# Feed-forward down
|
|
mapped_to = MODEL_TENSOR_NAMES[arch].get(MODEL_TENSOR.FFN_DOWN, None)
|
|
mapped_to = mapped_to.format(bid=i) if mapped_to is not None else None
|
|
|
|
tensor_map["gpt_neox.layers."+str(i)+".mlp.dense_4h_to_h"] = mapped_to # gptneox
|
|
tensor_map["transformer.h."+str(i)+".mlp.c_proj"] = mapped_to # gpt2
|
|
tensor_map["transformer.blocks."+str(i)+".ffn.down_proj"] = mapped_to # mpt
|
|
tensor_map["transformer.h."+str(i)+".mlp.dense_4h_to_h"] = mapped_to # falcon
|
|
tensor_map["model.layers."+str(i)+".mlp.down_proj"] = mapped_to # llama-hf
|
|
tensor_map["layers."+str(i)+".feed_forward.w2"] = mapped_to # llama-pth
|
|
|
|
return tensor_map
|
|
|
|
|
|
class TokenType(IntEnum):
|
|
NORMAL = 1
|
|
UNKNOWN = 2
|
|
CONTROL = 3
|
|
USER_DEFINED = 4
|
|
UNUSED = 5
|
|
BYTE = 6
|
|
|
|
#
|
|
# implementation
|
|
#
|
|
|
|
|
|
class GGMLQuantizationType(IntEnum):
|
|
F32 = 0
|
|
F16 = 1
|
|
Q4_0 = 2
|
|
Q4_1 = 3
|
|
Q5_0 = 6
|
|
Q5_1 = 7
|
|
Q8_0 = 8
|
|
Q8_1 = 9
|
|
Q2_K = 10
|
|
Q3_K = 11
|
|
Q4_K = 12
|
|
Q5_K = 13
|
|
Q6_K = 14
|
|
Q8_K = 15
|
|
|
|
|
|
class GGUFValueType(IntEnum):
|
|
UINT8 = 0
|
|
INT8 = 1
|
|
UINT16 = 2
|
|
INT16 = 3
|
|
UINT32 = 4
|
|
INT32 = 5
|
|
FLOAT32 = 6
|
|
BOOL = 7
|
|
STRING = 8
|
|
ARRAY = 9
|
|
|
|
@staticmethod
|
|
def get_type(val):
|
|
if isinstance(val, str) or isinstance(val, bytes) or isinstance(val, bytearray):
|
|
return GGUFValueType.STRING
|
|
elif isinstance(val, list):
|
|
return GGUFValueType.ARRAY
|
|
elif isinstance(val, float):
|
|
return GGUFValueType.FLOAT32
|
|
elif isinstance(val, bool):
|
|
return GGUFValueType.BOOL
|
|
elif isinstance(val, int):
|
|
return GGUFValueType.INT32
|
|
else:
|
|
print("Unknown type: "+str(type(val)))
|
|
sys.exit()
|
|
|
|
|
|
class GGUFWriter:
|
|
def __init__(self, path: str, arch: str, use_temp_file = True):
|
|
self.fout = open(path, "wb")
|
|
self.arch = arch
|
|
self.offset_tensor = 0
|
|
self.data_alignment = GGUF_DEFAULT_ALIGNMENT
|
|
self.kv_data = b""
|
|
self.kv_data_count = 0
|
|
self.ti_data = b""
|
|
self.ti_data_count = 0
|
|
self.add_architecture()
|
|
self.use_temp_file = use_temp_file
|
|
self.tensors = []
|
|
|
|
def write_header_to_file(self):
|
|
self.fout.write(struct.pack("<I", GGUF_MAGIC))
|
|
self.fout.write(struct.pack("<I", GGUF_VERSION))
|
|
self.fout.write(struct.pack("<I", self.ti_data_count))
|
|
self.fout.write(struct.pack("<I", self.kv_data_count))
|
|
self.flush()
|
|
# print("tensors " + str(self.ti_data_count) + " kv " + str(self.kv_data_count))
|
|
|
|
def write_kv_data_to_file(self):
|
|
self.fout.write(self.kv_data)
|
|
self.flush()
|
|
|
|
def write_ti_data_to_file(self):
|
|
self.fout.write(self.ti_data)
|
|
self.flush()
|
|
|
|
def add_key(self, key: str):
|
|
self.add_val(key, GGUFValueType.STRING, add_vtype=False)
|
|
|
|
def add_uint8(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.UINT8)
|
|
|
|
def add_int8(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.INT8)
|
|
|
|
def add_uint16(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.UINT16)
|
|
|
|
def add_int16(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.INT16)
|
|
|
|
def add_uint32(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.UINT32)
|
|
|
|
def add_int32(self, key: str, val: int):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.INT32)
|
|
|
|
def add_float32(self, key: str, val: float):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.FLOAT32)
|
|
|
|
def add_bool(self, key: str, val: bool):
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.BOOL)
|
|
|
|
def add_string(self, key: str, val: str):
|
|
if len(val) == 0:
|
|
return
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.STRING)
|
|
|
|
def add_array(self, key: str, val: list):
|
|
if not isinstance(val, list):
|
|
raise ValueError("Value must be a list for array type")
|
|
|
|
self.add_key(key)
|
|
self.add_val(val, GGUFValueType.ARRAY)
|
|
|
|
def add_val(self: str, val: Any, vtype: GGUFValueType = None, add_vtype: bool = True):
|
|
if vtype is None:
|
|
vtype = GGUFValueType.get_type(val)
|
|
|
|
if add_vtype:
|
|
self.kv_data += struct.pack("<I", vtype)
|
|
self.kv_data_count += 1
|
|
|
|
if vtype == GGUFValueType.UINT8:
|
|
self.kv_data += struct.pack("<B", val)
|
|
elif vtype == GGUFValueType.INT8:
|
|
self.kv_data += struct.pack("<b", val)
|
|
elif vtype == GGUFValueType.UINT16:
|
|
self.kv_data += struct.pack("<H", val)
|
|
elif vtype == GGUFValueType.INT16:
|
|
self.kv_data += struct.pack("<h", val)
|
|
elif vtype == GGUFValueType.UINT32:
|
|
self.kv_data += struct.pack("<I", val)
|
|
elif vtype == GGUFValueType.INT32:
|
|
self.kv_data += struct.pack("<i", val)
|
|
elif vtype == GGUFValueType.FLOAT32:
|
|
self.kv_data += struct.pack("<f", val)
|
|
elif vtype == GGUFValueType.BOOL:
|
|
self.kv_data += struct.pack("?", val)
|
|
elif vtype == GGUFValueType.STRING:
|
|
encoded_val = val.encode("utf8") if isinstance(val, str) else val
|
|
self.kv_data += struct.pack("<I", len(encoded_val))
|
|
self.kv_data += encoded_val
|
|
elif vtype == GGUFValueType.ARRAY:
|
|
ltype = set([GGUFValueType.get_type(item) for item in val])
|
|
assert len(ltype) == 1, "All items in a GGUF array should be of the same type"
|
|
self.kv_data += struct.pack("<I", list(ltype)[0])
|
|
self.kv_data += struct.pack("<I", len(val))
|
|
for item in val:
|
|
self.add_val(item, add_vtype=False)
|
|
else:
|
|
raise ValueError("Invalid GGUF metadata value type")
|
|
|
|
@staticmethod
|
|
def ggml_pad(x: int, n: int) -> int:
|
|
return ((x + n - 1) // n) * n
|
|
|
|
def add_tensor_info(self, name: str, tensor_shape: np.ndarray, tensor_dtype: np.dtype, tensor_nbytes: int, raw_dtype: Optional[GGMLQuantizationType] = None):
|
|
assert raw_dtype is not None or tensor_dtype in (np.float32, np.float16), "Only F32 and F16 tensors are supported for now"
|
|
|
|
encoded_name = name.encode("utf8")
|
|
self.ti_data += struct.pack("<I", len(encoded_name))
|
|
self.ti_data += encoded_name
|
|
n_dims = len(tensor_shape)
|
|
self.ti_data += struct.pack("<I", n_dims)
|
|
for i in range(n_dims):
|
|
self.ti_data += struct.pack("<I", tensor_shape[n_dims - 1 - i])
|
|
if raw_dtype is None:
|
|
dtype = GGMLQuantizationType.F32 if tensor_dtype == np.float32 else GGMLQuantizationType.F16
|
|
else:
|
|
dtype = raw_dtype
|
|
self.ti_data += struct.pack("<I", dtype)
|
|
self.ti_data += struct.pack("<Q", self.offset_tensor)
|
|
self.offset_tensor += GGUFWriter.ggml_pad(tensor_nbytes, self.data_alignment)
|
|
self.ti_data_count += 1
|
|
|
|
def add_tensor(self, name: str, tensor: np.ndarray, raw_shape: Optional[np.ndarray] = None, raw_dtype: Optional[GGMLQuantizationType] = None):
|
|
if self.use_temp_file and not hasattr(self, "temp_file"):
|
|
self.temp_file = tempfile.SpooledTemporaryFile(mode="w+b", max_size=256*1024*1024)
|
|
self.temp_file.seek(0)
|
|
|
|
self.add_tensor_info(name, raw_shape if raw_shape is not None else tensor.shape, tensor.dtype, tensor.nbytes, raw_dtype = raw_dtype)
|
|
|
|
pad = GGUFWriter.ggml_pad(tensor.nbytes, self.data_alignment) - tensor.nbytes
|
|
|
|
if not self.use_temp_file:
|
|
self.tensors.append((tensor, pad))
|
|
return
|
|
|
|
tensor.tofile(self.temp_file)
|
|
|
|
if pad != 0:
|
|
self.temp_file.write(bytes([0] * pad))
|
|
|
|
def write_tensor_data(self, tensor: np.ndarray):
|
|
pad = GGUFWriter.ggml_pad(self.fout.tell(), self.data_alignment) - self.fout.tell()
|
|
if pad != 0:
|
|
self.fout.write(bytes([0] * pad))
|
|
|
|
tensor.tofile(self.fout)
|
|
|
|
pad = GGUFWriter.ggml_pad(tensor.nbytes, self.data_alignment) - tensor.nbytes
|
|
if pad != 0:
|
|
self.fout.write(bytes([0] * pad))
|
|
|
|
def write_tensors_to_file(self):
|
|
self.write_ti_data_to_file()
|
|
|
|
pad = GGUFWriter.ggml_pad(self.fout.tell(), self.data_alignment) - self.fout.tell()
|
|
if pad != 0:
|
|
self.fout.write(bytes([0] * pad))
|
|
|
|
if not self.use_temp_file:
|
|
for (currtensor, currpad) in self.tensors:
|
|
currtensor.tofile(self.fout)
|
|
if currpad != 0:
|
|
self.fout.write(bytes([0] * currpad))
|
|
return
|
|
|
|
self.temp_file.seek(0)
|
|
|
|
shutil.copyfileobj(self.temp_file, self.fout)
|
|
self.flush()
|
|
self.temp_file.close()
|
|
|
|
def flush(self):
|
|
self.fout.flush()
|
|
|
|
def close(self):
|
|
self.fout.close()
|
|
|
|
def add_architecture(self):
|
|
self.add_string(KEY_GENERAL_ARCHITECTURE, self.arch)
|
|
|
|
def add_author(self, author: str):
|
|
self.add_string(KEY_GENERAL_AUTHOR, author)
|
|
|
|
def add_tensor_data_layout(self, layout: str):
|
|
self.add_string(KEY_LLM_TENSOR_DATA_LAYOUT.format(arch=self.arch), layout)
|
|
|
|
def add_url(self, url: str):
|
|
self.add_string(KEY_GENERAL_URL, url)
|
|
|
|
def add_description(self, description: str):
|
|
self.add_string(KEY_GENERAL_DESCRIPTION, description)
|
|
|
|
def add_source_url(self, url: str):
|
|
self.add_string(KEY_GENERAL_SOURCE_URL, url)
|
|
|
|
def add_source_hf_repo(self, repo: str):
|
|
self.add_string(KEY_GENERAL_SOURCE_HF_REPO, repo)
|
|
|
|
def add_name(self, name: str):
|
|
self.add_string(KEY_GENERAL_NAME, name)
|
|
|
|
def add_quantization_version(self, quantization_version: GGMLQuantizationType):
|
|
self.add_uint32(
|
|
KEY_GENERAL_QUANTIZATION_VERSION, quantization_version)
|
|
|
|
def add_custom_alignment(self, alignment: int):
|
|
self.data_alignment = alignment
|
|
self.add_uint32(KEY_GENERAL_ALIGNMENT, alignment)
|
|
|
|
def add_context_length(self, length: int):
|
|
self.add_uint32(
|
|
KEY_LLM_CONTEXT_LENGTH.format(arch=self.arch), length)
|
|
|
|
def add_embedding_length(self, length: int):
|
|
self.add_uint32(
|
|
KEY_LLM_EMBEDDING_LENGTH.format(arch=self.arch), length)
|
|
|
|
def add_block_count(self, length: int):
|
|
self.add_uint32(
|
|
KEY_LLM_BLOCK_COUNT.format(arch=self.arch), length)
|
|
|
|
def add_feed_forward_length(self, length: int):
|
|
self.add_uint32(
|
|
KEY_LLM_FEED_FORWARD_LENGTH.format(arch=self.arch), length)
|
|
|
|
def add_parallel_residual(self, use: bool):
|
|
self.add_bool(
|
|
KEY_LLM_USE_PARALLEL_RESIDUAL.format(arch=self.arch), use)
|
|
|
|
def add_tensor_data_layout(self, layout: str):
|
|
self.add_string(
|
|
KEY_LLM_TENSOR_DATA_LAYOUT.format(arch=self.arch), layout)
|
|
|
|
def add_head_count(self, count: int):
|
|
self.add_uint32(
|
|
KEY_ATTENTION_HEAD_COUNT.format(arch=self.arch), count)
|
|
|
|
def add_head_count_kv(self, count: int):
|
|
self.add_uint32(
|
|
KEY_ATTENTION_HEAD_COUNT_KV.format(arch=self.arch), count)
|
|
|
|
def add_max_alibi_bias(self, bias: float):
|
|
self.add_float32(
|
|
KEY_ATTENTION_MAX_ALIBI_BIAS.format(arch=self.arch), bias)
|
|
|
|
def add_clamp_kqv(self, value: float):
|
|
self.add_float32(
|
|
KEY_ATTENTION_CLAMP_KQV.format(arch=self.arch), value)
|
|
|
|
def add_layer_norm_eps(self, value: float):
|
|
self.add_float32(
|
|
KEY_ATTENTION_LAYERNORM_EPS.format(arch=self.arch), value)
|
|
|
|
def add_layer_norm_rms_eps(self, value: float):
|
|
self.add_float32(
|
|
KEY_ATTENTION_LAYERNORM_RMS_EPS.format(arch=self.arch), value)
|
|
|
|
def add_rope_dimension_count(self, count: int):
|
|
self.add_uint32(
|
|
KEY_ROPE_DIMENSION_COUNT.format(arch=self.arch), count)
|
|
|
|
def add_rope_scale_linear(self, value: float):
|
|
self.add_float32(KEY_ROPE_SCALE_LINEAR.format(arch=self.arch), value)
|
|
|
|
def add_tokenizer_model(self, model: str):
|
|
self.add_string(KEY_TOKENIZER_MODEL, model)
|
|
|
|
def add_token_list(self, tokens: List):
|
|
self.add_array(KEY_TOKENIZER_LIST, tokens)
|
|
|
|
def add_token_merges(self, merges: List):
|
|
self.add_array(KEY_TOKENIZER_MERGES, merges)
|
|
|
|
def add_token_types(self, types: List[int]):
|
|
self.add_array(KEY_TOKENIZER_TOKEN_TYPE, types)
|
|
|
|
def add_token_scores(self, scores: List[float]):
|
|
self.add_array(KEY_TOKENIZER_SCORES, scores)
|
|
|
|
def add_bos_token_id(self, id: int):
|
|
self.add_uint32(KEY_TOKENIZER_BOS_ID, id)
|
|
|
|
def add_eos_token_id(self, id: int):
|
|
self.add_uint32(KEY_TOKENIZER_EOS_ID, id)
|
|
|
|
def add_unk_token_id(self, id: int):
|
|
self.add_uint32(KEY_TOKENIZER_UNK_ID, id)
|
|
|
|
def add_sep_token_id(self, id: int):
|
|
self.add_uint32(KEY_TOKENIZER_SEP_ID, id)
|
|
|
|
def add_pad_token_id(self, id: int):
|
|
self.add_uint32(KEY_TOKENIZER_PAD_ID, id)
|
|
|
|
|
|
# Example usage:
|
|
if __name__ == "__main__":
|
|
# Example usage with a file
|
|
gguf_writer = GGUFWriter("example.gguf", "llama")
|
|
|
|
gguf_writer.add_architecture()
|
|
gguf_writer.add_block_count(12)
|
|
gguf_writer.add_uint32("answer", 42) # Write a 32-bit integer
|
|
gguf_writer.add_float32("answer_in_float", 42.0) # Write a 32-bit float
|
|
gguf_writer.add_custom_alignment(64)
|
|
|
|
tensor1 = np.ones((32,), dtype=np.float32) * 100.0
|
|
tensor2 = np.ones((64,), dtype=np.float32) * 101.0
|
|
tensor3 = np.ones((96,), dtype=np.float32) * 102.0
|
|
|
|
gguf_writer.add_tensor("tensor1", tensor1)
|
|
gguf_writer.add_tensor("tensor2", tensor2)
|
|
gguf_writer.add_tensor("tensor3", tensor3)
|
|
|
|
gguf_writer.write_header_to_file()
|
|
gguf_writer.write_kv_data_to_file()
|
|
gguf_writer.write_tensors_to_file()
|
|
|
|
gguf_writer.close()
|