File size: 2,790 Bytes
4807d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""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