Download kawk50m/config.py from Infinity08/KAWK50M-Project: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/kawk50m/config.py
- Command line
-
hf download hf://datasets/Infinity08/KAWK50M-Project/kawk50m/config.py
-
curl -L -o config.py https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/kawk50m/config.py
2.51 kB
| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Any | |
| import yaml | |
| from transformers import LlamaConfig | |
| def load_yaml(path: str | Path) -> dict[str, Any]: | |
| config_path = Path(path) | |
| with config_path.open("r", encoding="utf-8") as handle: | |
| data = yaml.safe_load(handle) | |
| if not isinstance(data, dict): | |
| raise ValueError(f"YAML root must be a mapping: {config_path}") | |
| return data | |
| def _positive_int(value: Any, name: str) -> int: | |
| if not isinstance(value, int) or isinstance(value, bool) or value <= 0: | |
| raise ValueError(f"{name} must be a positive integer, got {value!r}") | |
| return value | |
| def build_llama_config( | |
| model_data: dict[str, Any], | |
| *, | |
| tokenizer_vocab_size: int | None = None, | |
| ) -> tuple[LlamaConfig, int | None]: | |
| """Validate a model mapping and return a Hugging Face LlamaConfig.""" | |
| if "model" in model_data: | |
| model_data = model_data["model"] | |
| if not isinstance(model_data, dict): | |
| raise ValueError("model configuration must be a mapping") | |
| values = dict(model_data) | |
| expected_parameters = values.pop("expected_parameters", None) | |
| required = ( | |
| "vocab_size", | |
| "hidden_size", | |
| "intermediate_size", | |
| "num_hidden_layers", | |
| "num_attention_heads", | |
| "num_key_value_heads", | |
| "max_position_embeddings", | |
| ) | |
| for name in required: | |
| values[name] = _positive_int(values.get(name), name) | |
| if values["hidden_size"] % values["num_attention_heads"] != 0: | |
| raise ValueError("hidden_size must be divisible by num_attention_heads") | |
| if values["num_attention_heads"] % values["num_key_value_heads"] != 0: | |
| raise ValueError( | |
| "num_attention_heads must be divisible by num_key_value_heads" | |
| ) | |
| if tokenizer_vocab_size is not None and values["vocab_size"] != tokenizer_vocab_size: | |
| raise ValueError( | |
| "Tokenizer/model vocabulary mismatch: " | |
| f"tokenizer={tokenizer_vocab_size}, model={values['vocab_size']}" | |
| ) | |
| config = LlamaConfig(**values) | |
| return config, expected_parameters | |
| def count_unique_parameters(model: Any) -> int: | |
| """Count parameters without double-counting tied tensors.""" | |
| seen: set[int] = set() | |
| total = 0 | |
| for parameter in model.parameters(): | |
| pointer = parameter.untyped_storage().data_ptr() | |
| if pointer not in seen: | |
| seen.add(pointer) | |
| total += parameter.numel() | |
| return total | |