convert-*.py: add basename and finetune metadata
This commit is contained in:
parent
dbb1b471e4
commit
a42c2b7efc
5 changed files with 50 additions and 9 deletions
|
@ -33,6 +33,8 @@ logger = logging.getLogger("hf-to-gguf")
|
||||||
@dataclass
|
@dataclass
|
||||||
class Metadata:
|
class Metadata:
|
||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
|
basename: Optional[str] = None
|
||||||
|
finetune: Optional[str] = None
|
||||||
author: Optional[str] = None
|
author: Optional[str] = None
|
||||||
version: Optional[str] = None
|
version: Optional[str] = None
|
||||||
url: Optional[str] = None
|
url: Optional[str] = None
|
||||||
|
@ -55,6 +57,8 @@ class Metadata:
|
||||||
# Assigning values to Metadata attributes if they exist in the JSON file
|
# Assigning values to Metadata attributes if they exist in the JSON file
|
||||||
# This is based on LLM_KV_NAMES mapping in llama.cpp
|
# This is based on LLM_KV_NAMES mapping in llama.cpp
|
||||||
metadata.name = data.get("general.name")
|
metadata.name = data.get("general.name")
|
||||||
|
metadata.basename = data.get("general.basename")
|
||||||
|
metadata.finetune = data.get("general.finetune")
|
||||||
metadata.author = data.get("general.author")
|
metadata.author = data.get("general.author")
|
||||||
metadata.version = data.get("general.version")
|
metadata.version = data.get("general.version")
|
||||||
metadata.url = data.get("general.url")
|
metadata.url = data.get("general.url")
|
||||||
|
@ -201,7 +205,7 @@ class Model:
|
||||||
weight_estimate = per_model_weight_count_estimation(model_tensors, expert_count)
|
weight_estimate = per_model_weight_count_estimation(model_tensors, expert_count)
|
||||||
|
|
||||||
# Generate default filename based on model specification and available metadata
|
# Generate default filename based on model specification and available metadata
|
||||||
self.fname_default = gguf.naming_convention(self.model_name, self.metadata.version, expert_count, weight_estimate, encodingScheme)
|
self.fname_default = gguf.naming_convention(self.model_name, self.metadata.basename, self.metadata.finetune, self.metadata.version, expert_count, weight_estimate, encodingScheme)
|
||||||
|
|
||||||
# Filename Output
|
# Filename Output
|
||||||
if fname_out is not None:
|
if fname_out is not None:
|
||||||
|
@ -311,6 +315,10 @@ class Model:
|
||||||
self.gguf_writer.add_name(self.model_name)
|
self.gguf_writer.add_name(self.model_name)
|
||||||
|
|
||||||
if self.metadata is not None:
|
if self.metadata is not None:
|
||||||
|
if self.metadata.basename is not None:
|
||||||
|
self.gguf_writer.add_basename(self.metadata.basename)
|
||||||
|
if self.metadata.finetune is not None:
|
||||||
|
self.gguf_writer.add_finetune(self.metadata.finetune)
|
||||||
if self.metadata.author is not None:
|
if self.metadata.author is not None:
|
||||||
self.gguf_writer.add_author(self.metadata.author)
|
self.gguf_writer.add_author(self.metadata.author)
|
||||||
if self.metadata.version is not None:
|
if self.metadata.version is not None:
|
||||||
|
|
|
@ -349,6 +349,8 @@ class Params:
|
||||||
@dataclass
|
@dataclass
|
||||||
class Metadata:
|
class Metadata:
|
||||||
name: Optional[str] = None
|
name: Optional[str] = None
|
||||||
|
basename: Optional[str] = None
|
||||||
|
finetune: Optional[str] = None
|
||||||
author: Optional[str] = None
|
author: Optional[str] = None
|
||||||
version: Optional[str] = None
|
version: Optional[str] = None
|
||||||
url: Optional[str] = None
|
url: Optional[str] = None
|
||||||
|
@ -371,6 +373,8 @@ class Metadata:
|
||||||
# Assigning values to Metadata attributes if they exist in the JSON file
|
# Assigning values to Metadata attributes if they exist in the JSON file
|
||||||
# This is based on LLM_KV_NAMES mapping in llama.cpp
|
# This is based on LLM_KV_NAMES mapping in llama.cpp
|
||||||
metadata.name = data.get("general.name")
|
metadata.name = data.get("general.name")
|
||||||
|
metadata.basename = data.get("general.basename")
|
||||||
|
metadata.finetune = data.get("general.finetune")
|
||||||
metadata.author = data.get("general.author")
|
metadata.author = data.get("general.author")
|
||||||
metadata.version = data.get("general.version")
|
metadata.version = data.get("general.version")
|
||||||
metadata.url = data.get("general.url")
|
metadata.url = data.get("general.url")
|
||||||
|
@ -820,6 +824,10 @@ class OutputFile:
|
||||||
self.gguf.add_name(name)
|
self.gguf.add_name(name)
|
||||||
|
|
||||||
if metadata is not None:
|
if metadata is not None:
|
||||||
|
if metadata.basename is not None:
|
||||||
|
self.gguf.add_basename(metadata.basename)
|
||||||
|
if metadata.finetune is not None:
|
||||||
|
self.gguf.add_finetune(metadata.finetune)
|
||||||
if metadata.author is not None:
|
if metadata.author is not None:
|
||||||
self.gguf.add_author(metadata.author)
|
self.gguf.add_author(metadata.author)
|
||||||
if metadata.version is not None:
|
if metadata.version is not None:
|
||||||
|
@ -1226,6 +1234,8 @@ class VocabFactory:
|
||||||
|
|
||||||
def default_convention_outfile(file_type: GGMLFileType, model_name:str, expert_count:int, model_params_count: int, metadata: Metadata) -> str:
|
def default_convention_outfile(file_type: GGMLFileType, model_name:str, expert_count:int, model_params_count: int, metadata: Metadata) -> str:
|
||||||
name = metadata.name if metadata is not None and metadata.name is not None else model_name
|
name = metadata.name if metadata is not None and metadata.name is not None else model_name
|
||||||
|
basename = metadata.basename if metadata is not None and metadata.basename is not None else None
|
||||||
|
finetune = metadata.finetune if metadata is not None and metadata.finetune is not None else None
|
||||||
version = metadata.version if metadata is not None and metadata.version is not None else None
|
version = metadata.version if metadata is not None and metadata.version is not None else None
|
||||||
|
|
||||||
encodingScheme = {
|
encodingScheme = {
|
||||||
|
@ -1234,7 +1244,7 @@ def default_convention_outfile(file_type: GGMLFileType, model_name:str, expert_c
|
||||||
GGMLFileType.MostlyQ8_0: "Q8_0",
|
GGMLFileType.MostlyQ8_0: "Q8_0",
|
||||||
}[file_type]
|
}[file_type]
|
||||||
|
|
||||||
return gguf.naming_convention(name, version, expert_count, model_params_count, encodingScheme)
|
return gguf.naming_convention(name, basename, finetune, version, expert_count, model_params_count, encodingScheme)
|
||||||
|
|
||||||
|
|
||||||
def default_outfile(model_paths: list[Path], file_type: GGMLFileType, model_name:str, expert_count:int, model_params_count: int, metadata: Metadata) -> Path:
|
def default_outfile(model_paths: list[Path], file_type: GGMLFileType, model_name:str, expert_count:int, model_params_count: int, metadata: Metadata) -> Path:
|
||||||
|
|
|
@ -24,6 +24,8 @@ class Keys:
|
||||||
QUANTIZATION_VERSION = "general.quantization_version"
|
QUANTIZATION_VERSION = "general.quantization_version"
|
||||||
ALIGNMENT = "general.alignment"
|
ALIGNMENT = "general.alignment"
|
||||||
NAME = "general.name"
|
NAME = "general.name"
|
||||||
|
BASENAME = "general.basename"
|
||||||
|
FINETUNE = "general.finetune"
|
||||||
AUTHOR = "general.author"
|
AUTHOR = "general.author"
|
||||||
VERSION = "general.version"
|
VERSION = "general.version"
|
||||||
URL = "general.url"
|
URL = "general.url"
|
||||||
|
|
|
@ -430,6 +430,12 @@ class GGUFWriter:
|
||||||
def add_architecture(self) -> None:
|
def add_architecture(self) -> None:
|
||||||
self.add_string(Keys.General.ARCHITECTURE, self.arch)
|
self.add_string(Keys.General.ARCHITECTURE, self.arch)
|
||||||
|
|
||||||
|
def add_basename(self, basename: str) -> None:
|
||||||
|
self.add_string(Keys.General.BASENAME, basename)
|
||||||
|
|
||||||
|
def add_finetune(self, finetune: str) -> None:
|
||||||
|
self.add_string(Keys.General.FINETUNE, finetune)
|
||||||
|
|
||||||
def add_author(self, author: str) -> None:
|
def add_author(self, author: str) -> None:
|
||||||
self.add_string(Keys.General.AUTHOR, author)
|
self.add_string(Keys.General.AUTHOR, author)
|
||||||
|
|
||||||
|
|
|
@ -25,11 +25,26 @@ def model_weight_count_rounded_notation(model_params_count: int) -> str:
|
||||||
return f"{round(scaled_model_params)}{scale_suffix}"
|
return f"{round(scaled_model_params)}{scale_suffix}"
|
||||||
|
|
||||||
|
|
||||||
def naming_convention(model_name: str, version_string:str, expert_count_int:int, model_params_count: int, encodingScheme: str) -> str:
|
def naming_convention(model_name: str, base_name: str, finetune_string:str, version_string:str, expert_count_int:int, model_params_count: int, encoding_scheme: str) -> str:
|
||||||
# Reference: https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#gguf-naming-convention
|
# Reference: https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#gguf-naming-convention
|
||||||
name = model_name.strip().replace(' ', '-') if model_name is not None else "ggml-model"
|
|
||||||
version = f"-{version_string}" if version_string is not None else ""
|
if base_name is not None:
|
||||||
expert_count_chunk = f"{expert_count_int}x" if expert_count_int is not None and expert_count_int > 0 else ""
|
name = base_name.strip().title().replace(' ', '_')
|
||||||
parameters = model_weight_count_rounded_notation(model_params_count)
|
elif model_name is not None:
|
||||||
encodingScheme = encodingScheme.upper()
|
name = model_name.strip().title().replace(' ', '_')
|
||||||
return f"{name}{version}-{expert_count_chunk}{parameters}-{encodingScheme}"
|
else:
|
||||||
|
name = "ggml-model"
|
||||||
|
|
||||||
|
per_model_rounded_weight_estimate = model_weight_count_rounded_notation(model_params_count)
|
||||||
|
if expert_count_int is not None and expert_count_int > 0:
|
||||||
|
parameters = f"-{expert_count_int}x{per_model_rounded_weight_estimate}"
|
||||||
|
else:
|
||||||
|
parameters = f"-{per_model_rounded_weight_estimate}"
|
||||||
|
|
||||||
|
finetune = f"-{finetune_string.strip().title().replace(' ', '_')}" if finetune_string is not None else ""
|
||||||
|
|
||||||
|
version = f"-{version_string.strip().replace(' ', '_')}" if version_string is not None else ""
|
||||||
|
|
||||||
|
encoding = f"-{encoding_scheme.strip().replace(' ', '_').upper()}"
|
||||||
|
|
||||||
|
return f"{name}{parameters}{finetune}{version}{encoding}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue