Download tests/tiny.py from CrazyH0rse/Discerna-v1-475M: direct link, hf CLI and curl.
- Browser
- Download file 2.79 kB
-
https://huggingface.co/CrazyH0rse/Discerna-v1-475M/resolve/main/tests/tiny.py
- Command line
-
hf download hf://CrazyH0rse/Discerna-v1-475M/tests/tiny.py
-
curl -L -o tiny.py https://huggingface.co/CrazyH0rse/Discerna-v1-475M/resolve/main/tests/tiny.py
2.79 kB
| """Tiny, fully offline stand-ins for tests: a character tokenizer with the real special tokens, and a | |
| small model folder built through the same code path as the released model.""" | |
| from __future__ import annotations | |
| import string | |
| from pathlib import Path | |
| import torch | |
| from discerna.model.codec import DELIMITERS, MARKER | |
| from discerna.model.decision_model import DecisionModel | |
| from discerna.model.weights import FORMAT, build, save_checkpoint | |
| SPECIAL = ["<|pad|>", "<|startoftext|>", "<|unk|>", MARKER, *DELIMITERS.values()] | |
| def tiny_tokenizer(): | |
| """Character-level tokenizer: every printable character is a token, plus the codec's special tokens.""" | |
| from tokenizers import Regex, Tokenizer, models, pre_tokenizers | |
| from transformers import PreTrainedTokenizerFast | |
| chars = sorted(set(string.printable) | set("¦")) | |
| vocab = {tok: i for i, tok in enumerate(SPECIAL + chars)} | |
| core = Tokenizer(models.WordLevel(vocab=vocab, unk_token="<|unk|>")) | |
| core.pre_tokenizer = pre_tokenizers.Split(Regex("."), behavior="isolated") | |
| return PreTrainedTokenizerFast(tokenizer_object=core, bos_token="<|startoftext|>", pad_token="<|pad|>", | |
| unk_token="<|unk|>", mask_token=MARKER, additional_special_tokens=SPECIAL[3:]) | |
| def tiny_spec(vision: bool = False, temperatures: dict | None = None) -> dict: | |
| return { | |
| "format": FORMAT, | |
| "name": "tiny", | |
| "tokenizer": {"path": "."}, | |
| "trunk": dict(vocab_size=len(tiny_tokenizer()), hidden_size=64, intermediate_size=192, | |
| layer_types=["conv", "conv", "full_attention", "conv"], num_attention_heads=4, | |
| num_key_value_heads=2, norm_eps=1e-5, rope_theta=10000.0), | |
| "head": {"type": "marker", "layers": 1}, | |
| "vision": {"type": "siglip2_tiled", "projector_hidden": 64, | |
| "vision_config": dict(hidden_size=32, intermediate_size=64, num_hidden_layers=2, | |
| num_attention_heads=2, num_channels=3, num_patches=256, patch_size=16), | |
| } if vision else None, | |
| "calibrator": {"temperatures": temperatures or {}, "calibrate_media": False}, | |
| "limits": {"max_length": 2048, "image_text_length": 512, "max_tokens": 65536}, | |
| } | |
| def tiny_model(vision: bool = False, seed: int = 0, temperatures: dict | None = None) -> DecisionModel: | |
| torch.manual_seed(seed) | |
| return build(tiny_spec(vision, temperatures), tok=tiny_tokenizer()) | |
| def tiny_folder(path: Path, vision: bool = False, seed: int = 0) -> Path: | |
| """A saved tiny model folder (config + weights + tokenizer), loadable like the released one.""" | |
| model = tiny_model(vision, seed) | |
| save_checkpoint(model, path) | |
| model.codec.tok.save_pretrained(path) | |
| return path | |