convert-*.py: parameter_class_attribute --> size_label

This commit is contained in:
brian khuu 2024-07-11 21:01:52 +10:00
parent 6eb08ac868
commit f8b5931180
7 changed files with 31 additions and 31 deletions

View file

@ -106,20 +106,20 @@ class Model:
self.metadata.name = dir_model.name self.metadata.name = dir_model.name
# Generate parameter weight class (useful for leader boards) if not yet determined # Generate parameter weight class (useful for leader boards) if not yet determined
if self.metadata.parameter_class_attribute is None: if self.metadata.size_label is None:
expert_count = self.hparams.get("num_local_experts", 0) expert_count = self.hparams.get("num_local_experts", 0)
sum_weight_estimate = self.calculate_total_weight_count() sum_weight_estimate = self.calculate_total_weight_count()
# Calculate weight estimate per model # Calculate weight estimate per model
per_model_weight_estimate: int = (sum_weight_estimate / expert_count) if (expert_count > 0) else sum_weight_estimate per_model_weight_estimate: int = (sum_weight_estimate / expert_count) if (expert_count > 0) else sum_weight_estimate
self.metadata.parameter_class_attribute = gguf.parameter_class_attribute(expert_count, per_model_weight_estimate) self.metadata.size_label = gguf.size_label(expert_count, per_model_weight_estimate)
# Extracts and converts the encoding scheme from the given file type name. e.g. 'gguf.LlamaFileType.ALL_F32' --> 'F32' # Extracts and converts the encoding scheme from the given file type name. e.g. 'gguf.LlamaFileType.ALL_F32' --> 'F32'
output_type = self.ftype.name.partition("_")[2] output_type = self.ftype.name.partition("_")[2]
# 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.metadata.name, self.metadata.basename, self.metadata.finetune, self.metadata.version, self.metadata.parameter_class_attribute, output_type) self.fname_default = gguf.naming_convention(self.metadata.name, self.metadata.basename, self.metadata.finetune, self.metadata.version, self.metadata.size_label, output_type)
# Filename Output # Filename Output
if fname_out is not None: if fname_out is not None:

View file

@ -802,8 +802,8 @@ class OutputFile:
if metadata.quantized_by is not None: if metadata.quantized_by is not None:
self.gguf.add_quantized_by(metadata.quantized_by) self.gguf.add_quantized_by(metadata.quantized_by)
if metadata.parameter_class_attribute is not None: if metadata.size_label is not None:
self.gguf.add_parameter_class_attribute(metadata.parameter_class_attribute) self.gguf.add_size_label(metadata.size_label)
if metadata.license is not None: if metadata.license is not None:
self.gguf.add_license(metadata.license) self.gguf.add_license(metadata.license)
@ -1255,7 +1255,7 @@ def default_convention_outfile(file_type: GGMLFileType, expert_count:int | None,
basename = metadata.basename if metadata.basename is not None else None basename = metadata.basename if metadata.basename is not None else None
finetune = metadata.finetune if metadata.finetune is not None else None finetune = metadata.finetune if metadata.finetune is not None else None
version = metadata.version if metadata.version is not None else None version = metadata.version if metadata.version is not None else None
parameter_class_attribute = metadata.parameter_class_attribute if metadata.parameter_class_attribute is not None else gguf.parameter_class_attribute(expert_count, model_params_count) size_label = metadata.size_label if metadata.size_label is not None else gguf.size_label(expert_count, model_params_count)
output_type = { output_type = {
GGMLFileType.AllF32: "F32", GGMLFileType.AllF32: "F32",
@ -1263,7 +1263,7 @@ def default_convention_outfile(file_type: GGMLFileType, expert_count:int | None,
GGMLFileType.MostlyQ8_0: "Q8_0", GGMLFileType.MostlyQ8_0: "Q8_0",
}[file_type] }[file_type]
return gguf.naming_convention(name, basename, finetune, version, parameter_class_attribute, output_type) return gguf.naming_convention(name, basename, finetune, version, size_label, output_type)
def default_outfile(model_paths: list[Path], file_type: GGMLFileType, expert_count:int | None, model_params_count: int, metadata: gguf.Metadata) -> Path: def default_outfile(model_paths: list[Path], file_type: GGMLFileType, expert_count:int | None, model_params_count: int, metadata: gguf.Metadata) -> Path:
@ -1427,7 +1427,7 @@ def main(args_in: list[str] | None = None) -> None:
model = convert_to_output_type(model, ftype) model = convert_to_output_type(model, ftype)
outfile = args.outfile or default_outfile(model_plus.paths, ftype, params.n_experts, model_params_count, metadata=metadata) outfile = args.outfile or default_outfile(model_plus.paths, ftype, params.n_experts, model_params_count, metadata=metadata)
metadata.parameter_class_attribute = gguf.parameter_class_attribute(params.n_experts, model_params_count) metadata.size_label = gguf.size_label(params.n_experts, model_params_count)
params.ftype = ftype params.ftype = ftype
logger.info(f"Writing {outfile}, format {ftype}") logger.info(f"Writing {outfile}, format {ftype}")

View file

@ -37,7 +37,7 @@ class Keys:
DESCRIPTION = "general.description" DESCRIPTION = "general.description"
QUANTIZED_BY = "general.quantized_by" QUANTIZED_BY = "general.quantized_by"
PARAMETER_CLASS_ATTRIBUTE = "general.parameter_class_attribute" SIZE_LABEL = "general.size_label"
# Licensing details # Licensing details
LICENSE = "general.license" LICENSE = "general.license"

View file

@ -480,8 +480,8 @@ class GGUFWriter:
def add_quantized_by(self, quantized: str) -> None: def add_quantized_by(self, quantized: str) -> None:
self.add_string(Keys.General.QUANTIZED_BY, quantized) self.add_string(Keys.General.QUANTIZED_BY, quantized)
def add_parameter_class_attribute(self, parameter_class_attribute: str) -> None: def add_size_label(self, size_label: str) -> None:
self.add_string(Keys.General.PARAMETER_CLASS_ATTRIBUTE, parameter_class_attribute) self.add_string(Keys.General.SIZE_LABEL, size_label)
def add_license(self, license: str) -> None: def add_license(self, license: str) -> None:
self.add_string(Keys.General.LICENSE, license) self.add_string(Keys.General.LICENSE, license)

View file

@ -23,7 +23,7 @@ class Metadata:
basename: Optional[str] = None basename: Optional[str] = None
description: Optional[str] = None description: Optional[str] = None
quantized_by: Optional[str] = None quantized_by: Optional[str] = None
parameter_class_attribute: Optional[str] = None size_label: Optional[str] = None
url: Optional[str] = None url: Optional[str] = None
doi: Optional[str] = None doi: Optional[str] = None
uuid: Optional[str] = None uuid: Optional[str] = None
@ -69,7 +69,7 @@ class Metadata:
metadata.description = metadata_override.get(Keys.General.DESCRIPTION, metadata.description) metadata.description = metadata_override.get(Keys.General.DESCRIPTION, metadata.description)
metadata.quantized_by = metadata_override.get(Keys.General.QUANTIZED_BY, metadata.quantized_by) metadata.quantized_by = metadata_override.get(Keys.General.QUANTIZED_BY, metadata.quantized_by)
metadata.parameter_class_attribute = metadata_override.get(Keys.General.PARAMETER_CLASS_ATTRIBUTE, metadata.parameter_class_attribute) metadata.size_label = metadata_override.get(Keys.General.SIZE_LABEL, metadata.size_label)
metadata.license_name = metadata_override.get(Keys.General.LICENSE_NAME, metadata.license_name) metadata.license_name = metadata_override.get(Keys.General.LICENSE_NAME, metadata.license_name)
metadata.license_link = metadata_override.get(Keys.General.LICENSE_LINK, metadata.license_link) metadata.license_link = metadata_override.get(Keys.General.LICENSE_LINK, metadata.license_link)
@ -164,7 +164,7 @@ class Metadata:
# Regular expression to extract model name components # Regular expression to extract model name components
# Heuristic to match against cases such as 'Mixtral-8x7B-Instruct-v0.1' or 'Codestral-22B-v0.1' # Heuristic to match against cases such as 'Mixtral-8x7B-Instruct-v0.1' or 'Codestral-22B-v0.1'
regex_match = re.compile(r'^(?P<basename>[A-Za-z0-9\s]*(?:(?:-(?:(?:[A-Za-z\s][A-Za-z0-9\s]*)|(?:[0-9\s]*)))*))' regex_match = re.compile(r'^(?P<basename>[A-Za-z0-9\s]*(?:(?:-(?:(?:[A-Za-z\s][A-Za-z0-9\s]*)|(?:[0-9\s]*)))*))'
r'(?:-(?P<parameter_class_attribute>(?:\d+x)?\d+[A-Za-z]+)(?:-(?P<finetune>[A-Za-z0-9\s-]+))?)?' r'(?:-(?P<size_label>(?:\d+x)?\d+[A-Za-z]+)(?:-(?P<finetune>[A-Za-z0-9\s-]+))?)?'
r'(?:-(?P<version>v\d+(?:\.\d+)*))?$').match(model_full_name_component) r'(?:-(?P<version>v\d+(?:\.\d+)*))?$').match(model_full_name_component)
if not regex_match: if not regex_match:
@ -174,9 +174,9 @@ class Metadata:
basename = components.get("basename") basename = components.get("basename")
finetune = components.get("finetune") finetune = components.get("finetune")
version = components.get("version") version = components.get("version")
parameter_class_attribute = components.get("parameter_class_attribute") size_label = components.get("size_label")
return model_full_name_component, org_component, basename, finetune, version, parameter_class_attribute return model_full_name_component, org_component, basename, finetune, version, size_label
@staticmethod @staticmethod
def apply_metadata_heuristic(metadata: Metadata, model_card: Optional[dict] = None, hf_params: Optional[dict] = None, model_path: Optional[Path] = None) -> Metadata: def apply_metadata_heuristic(metadata: Metadata, model_card: Optional[dict] = None, hf_params: Optional[dict] = None, model_path: Optional[Path] = None) -> Metadata:
@ -218,7 +218,7 @@ class Metadata:
metadata.base_models = [] metadata.base_models = []
for model_id in metadata_base_models: for model_id in metadata_base_models:
model_full_name_component, org_component, basename, finetune, version, parameter_class_attribute = Metadata.get_model_id_components(model_id) model_full_name_component, org_component, basename, finetune, version, size_label = Metadata.get_model_id_components(model_id)
base_model = {} base_model = {}
if model_full_name_component is not None: if model_full_name_component is not None:
base_model["name"] = Metadata.id_to_title(model_full_name_component) base_model["name"] = Metadata.id_to_title(model_full_name_component)
@ -297,7 +297,7 @@ class Metadata:
# Use _name_or_path only if its actually a model name and not some computer path # Use _name_or_path only if its actually a model name and not some computer path
# e.g. 'meta-llama/Llama-2-7b-hf' # e.g. 'meta-llama/Llama-2-7b-hf'
model_id = hf_name_or_path model_id = hf_name_or_path
model_full_name_component, org_component, basename, finetune, version, parameter_class_attribute = Metadata.get_model_id_components(model_id) model_full_name_component, org_component, basename, finetune, version, size_label = Metadata.get_model_id_components(model_id)
if metadata.name is None and model_full_name_component is not None: if metadata.name is None and model_full_name_component is not None:
metadata.name = Metadata.id_to_title(model_full_name_component) metadata.name = Metadata.id_to_title(model_full_name_component)
if metadata.organization is None and org_component is not None: if metadata.organization is None and org_component is not None:
@ -308,14 +308,14 @@ class Metadata:
metadata.finetune = finetune metadata.finetune = finetune
if metadata.version is None and version is not None: if metadata.version is None and version is not None:
metadata.version = version metadata.version = version
if metadata.parameter_class_attribute is None and parameter_class_attribute is not None: if metadata.size_label is None and size_label is not None:
metadata.parameter_class_attribute = parameter_class_attribute metadata.size_label = size_label
# Directory Folder Name Fallback Heuristics # Directory Folder Name Fallback Heuristics
############################################ ############################################
if model_path is not None: if model_path is not None:
model_id = model_path.name model_id = model_path.name
model_full_name_component, org_component, basename, finetune, version, parameter_class_attribute = Metadata.get_model_id_components(model_id) model_full_name_component, org_component, basename, finetune, version, size_label = Metadata.get_model_id_components(model_id)
if metadata.name is None and model_full_name_component is not None: if metadata.name is None and model_full_name_component is not None:
metadata.name = Metadata.id_to_title(model_full_name_component) metadata.name = Metadata.id_to_title(model_full_name_component)
if metadata.organization is None and org_component is not None: if metadata.organization is None and org_component is not None:
@ -326,8 +326,8 @@ class Metadata:
metadata.finetune = finetune metadata.finetune = finetune
if metadata.version is None and version is not None: if metadata.version is None and version is not None:
metadata.version = version metadata.version = version
if metadata.parameter_class_attribute is None and parameter_class_attribute is not None: if metadata.size_label is None and size_label is not None:
metadata.parameter_class_attribute = parameter_class_attribute metadata.size_label = size_label
return metadata return metadata
@ -352,8 +352,8 @@ class Metadata:
if self.quantized_by is not None: if self.quantized_by is not None:
gguf_writer.add_quantized_by(self.quantized_by) gguf_writer.add_quantized_by(self.quantized_by)
if self.parameter_class_attribute is not None: if self.size_label is not None:
gguf_writer.add_parameter_class_attribute(self.parameter_class_attribute) gguf_writer.add_size_label(self.size_label)
if self.license is not None: if self.license is not None:
gguf_writer.add_license(self.license) gguf_writer.add_license(self.license)

View file

@ -34,7 +34,7 @@ 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 parameter_class_attribute(expert_count_int:int | None, model_params_count: int) -> str: def size_label(expert_count_int:int | None, model_params_count: int) -> str:
per_model_rounded_weight_estimate = model_weight_count_rounded_notation(model_params_count) 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: if expert_count_int is not None and expert_count_int > 0:
@ -45,7 +45,7 @@ def parameter_class_attribute(expert_count_int:int | None, model_params_count: i
return size_class return size_class
def naming_convention(model_name: str | None, base_name: str | None, finetune_string:str | None, version_string:str | None, parameter_class_attribute: str | None, output_type: str | None) -> str: def naming_convention(model_name: str | None, base_name: str | None, finetune_string:str | None, version_string:str | None, size_label: str | None, output_type: str | None) -> 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
if base_name is not None: if base_name is not None:
@ -55,7 +55,7 @@ def naming_convention(model_name: str | None, base_name: str | None, finetune_st
else: else:
name = "ggml-model" name = "ggml-model"
parameters = f"-{parameter_class_attribute}" if parameter_class_attribute is not None else "" parameters = f"-{size_label}" if size_label is not None else ""
finetune = f"-{finetune_string.strip().title().replace(' ', '-')}" if finetune_string is not None else "" finetune = f"-{finetune_string.strip().title().replace(' ', '-')}" if finetune_string is not None else ""

View file

@ -42,17 +42,17 @@ class TestMetadataMethod(unittest.TestCase):
'base_model': ["EmbeddedLLM/Mistral-7B-Merge-14-v0", "janai-hq/trinity-v1"] 'base_model': ["EmbeddedLLM/Mistral-7B-Merge-14-v0", "janai-hq/trinity-v1"]
} }
got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), model_card, None, None) got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), model_card, None, None)
expect = gguf.Metadata(name=None, author=None, version=None, organization=None, finetune=None, basename=None, description=None, quantized_by=None, parameter_class_attribute=None, url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=[{'name': 'Mistral 7B Merge 14 v0', 'organization': 'EmbeddedLLM', 'repo_url': 'https://huggingface.co/EmbeddedLLM/Mistral-7B-Merge-14-v0'}, {'name': 'Trinity v1', 'organization': 'Janai Hq', 'repo_url': 'https://huggingface.co/janai-hq/trinity-v1'}], tags=['Llama-3', 'instruct', 'finetune', 'chatml', 'DPO', 'RLHF', 'gpt4', 'synthetic data', 'distillation', 'function calling', 'json mode', 'axolotl'], languages=['en'], datasets=['teknium/OpenHermes-2.5']) expect = gguf.Metadata(name=None, author=None, version=None, organization=None, finetune=None, basename=None, description=None, quantized_by=None, size_label=None, url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=[{'name': 'Mistral 7B Merge 14 v0', 'organization': 'EmbeddedLLM', 'repo_url': 'https://huggingface.co/EmbeddedLLM/Mistral-7B-Merge-14-v0'}, {'name': 'Trinity v1', 'organization': 'Janai Hq', 'repo_url': 'https://huggingface.co/janai-hq/trinity-v1'}], tags=['Llama-3', 'instruct', 'finetune', 'chatml', 'DPO', 'RLHF', 'gpt4', 'synthetic data', 'distillation', 'function calling', 'json mode', 'axolotl'], languages=['en'], datasets=['teknium/OpenHermes-2.5'])
self.assertEqual(got, expect) self.assertEqual(got, expect)
def test_apply_metadata_heuristic_from_hf_parameters(self): def test_apply_metadata_heuristic_from_hf_parameters(self):
hf_params = {"_name_or_path": "./hermes-2-pro-llama-3-8b-DPO"} hf_params = {"_name_or_path": "./hermes-2-pro-llama-3-8b-DPO"}
got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), None, hf_params, None) got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), None, hf_params, None)
expect = gguf.Metadata(name='Hermes 2 Pro Llama 3 8b DPO', author=None, version=None, organization=None, finetune='DPO', basename='hermes-2-pro-llama-3', description=None, quantized_by=None, parameter_class_attribute='8b', url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=None, tags=None, languages=None, datasets=None) expect = gguf.Metadata(name='Hermes 2 Pro Llama 3 8b DPO', author=None, version=None, organization=None, finetune='DPO', basename='hermes-2-pro-llama-3', description=None, quantized_by=None, size_label='8b', url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=None, tags=None, languages=None, datasets=None)
self.assertEqual(got, expect) self.assertEqual(got, expect)
def test_apply_metadata_heuristic_from_model_dir(self): def test_apply_metadata_heuristic_from_model_dir(self):
model_dir_path = Path("./hermes-2-pro-llama-3-8b-DPO") model_dir_path = Path("./hermes-2-pro-llama-3-8b-DPO")
got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), None, None, model_dir_path) got = gguf.Metadata.apply_metadata_heuristic(gguf.Metadata(), None, None, model_dir_path)
expect = gguf.Metadata(name='Hermes 2 Pro Llama 3 8b DPO', author=None, version=None, organization=None, finetune='DPO', basename='hermes-2-pro-llama-3', description=None, quantized_by=None, parameter_class_attribute='8b', url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=None, tags=None, languages=None, datasets=None) expect = gguf.Metadata(name='Hermes 2 Pro Llama 3 8b DPO', author=None, version=None, organization=None, finetune='DPO', basename='hermes-2-pro-llama-3', description=None, quantized_by=None, size_label='8b', url=None, doi=None, uuid=None, repo_url=None, license=None, license_name=None, license_link=None, base_models=None, tags=None, languages=None, datasets=None)
self.assertEqual(got, expect) self.assertEqual(got, expect)