File size: 2,906 Bytes
feb6b86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""FableConfig — Hugging Face configuration for the `fable` miniature.

Mirrors `fable_lm.model.ModelConfig` (vocab_size=2048, context_len=512, n_layers=4,
d_model=256, n_heads=4, d_ff=768) using standard HF configuration names so the
model is loadable with `AutoConfig` / `trust_remote_code`.

This file is intentionally self-contained (no `fable_lm` import): it is copied
verbatim into the Hub repo, where only the files next to `config.json` exist.
"""

from __future__ import annotations

from transformers import PretrainedConfig


class FableConfig(PretrainedConfig):
    model_type = "fable"

    def __init__(
        self,
        vocab_size: int = 2048,
        max_position_embeddings: int = 512,
        num_hidden_layers: int = 4,
        hidden_size: int = 256,
        num_attention_heads: int = 4,
        intermediate_size: int = 768,
        dropout: float = 0.0,
        pad_token_id: int = 256,   # <|pad|>
        bos_token_id: int = 258,   # <|doc|> doubles as BOS (document separator)
        eos_token_id: int = 257,   # <|eos|>
        **kwargs,
    ):
        # architecture hyperparameters (names follow HF conventions)
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.num_hidden_layers = num_hidden_layers
        self.hidden_size = hidden_size
        self.num_attention_heads = num_attention_heads
        self.intermediate_size = intermediate_size
        self.dropout = dropout
        # token embeddings are tied into the LM head (FableLM: head.weight = tok_emb.weight)
        self.tie_word_embeddings = True
        super().__init__(
            pad_token_id=pad_token_id,
            bos_token_id=bos_token_id,
            eos_token_id=eos_token_id,
            **kwargs,
        )

    # ---- read-only aliases for the original repo-style names ----------------
    # Properties (not attributes) so they never appear in to_dict()/config.json;
    # serialization always uses the canonical HF names above.

    @property
    def context_len(self) -> int:
        return self.max_position_embeddings

    @property
    def n_layers(self) -> int:
        return self.num_hidden_layers

    @property
    def d_model(self) -> int:
        return self.hidden_size

    @property
    def n_heads(self) -> int:
        return self.num_attention_heads

    @property
    def d_ff(self) -> int:
        return self.intermediate_size


try:
    # Local (non-remote-code) use: `import hf_fable` makes AutoConfig aware of
    # "fable" without trust_remote_code. exist_ok: the module may also be
    # imported under a second name (hf_fable.configuration_fable vs a plain
    # configuration_fable copied next to config.json).
    from transformers import AutoConfig

    AutoConfig.register("fable", FableConfig, exist_ok=True)
except Exception:  # pragma: no cover - registration must never break import
    pass