mini-beatrix-2s automodel: modeling_minibeatrix.py (mission final 16.101B, alephllm 0.8.6)
Browse files- modeling_minibeatrix.py +99 -0
modeling_minibeatrix.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""HF AutoModel wrapper for mini-beatrix-2s — the FULL-SPLAT governed craft
|
| 2 |
+
(a CausalSplatHUB constellation in every block; 16.101B-token mission,
|
| 3 |
+
completed 2026-08-31).
|
| 4 |
+
|
| 5 |
+
Loads with:
|
| 6 |
+
AutoModelForCausalLM.from_pretrained("AbstractPhil/mini-beatrix-2s",
|
| 7 |
+
trust_remote_code=True)
|
| 8 |
+
The model reads raw UTF-8 bytes: input_ids are byte values 0..255.
|
| 9 |
+
"""
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
| 14 |
+
from transformers.generation import GenerationMixin
|
| 15 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 16 |
+
|
| 17 |
+
from .presets import AlephLMConfig
|
| 18 |
+
from .alephlm import AlephLM
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class MiniBeatrixConfig(PretrainedConfig):
|
| 22 |
+
model_type = "mini-beatrix"
|
| 23 |
+
attribute_map = {"num_hidden_layers": "n_layers",
|
| 24 |
+
"hidden_size": "d_model",
|
| 25 |
+
"num_attention_heads": "n_heads",
|
| 26 |
+
"max_position_embeddings": "context"}
|
| 27 |
+
|
| 28 |
+
def __init__(self, name="mini-beatrix-2s", d_model=1024, n_layers=20,
|
| 29 |
+
n_heads=16, context=4096, vocab_size=256,
|
| 30 |
+
tokenizer="byte-trigram", hub_layers=tuple(range(20)),
|
| 31 |
+
hub_K=64, hub_D=128, tau=0.1, bank_experts=3, bank_ff=1024,
|
| 32 |
+
head_K=256, head_D=256, gate_init=-3.0,
|
| 33 |
+
tie_embeddings=False, hub_chunk=256, hub_const=4,
|
| 34 |
+
hub_ckpt=0, **kwargs):
|
| 35 |
+
self.name = name
|
| 36 |
+
self.d_model = d_model
|
| 37 |
+
self.n_layers = n_layers
|
| 38 |
+
self.n_heads = n_heads
|
| 39 |
+
self.context = context
|
| 40 |
+
self.vocab_size = vocab_size
|
| 41 |
+
self.tokenizer = tokenizer
|
| 42 |
+
self.hub_layers = list(hub_layers)
|
| 43 |
+
self.hub_K = hub_K
|
| 44 |
+
self.hub_D = hub_D
|
| 45 |
+
self.tau = tau
|
| 46 |
+
self.bank_experts = bank_experts
|
| 47 |
+
self.bank_ff = bank_ff
|
| 48 |
+
self.head_K = head_K
|
| 49 |
+
self.head_D = head_D
|
| 50 |
+
self.gate_init = gate_init
|
| 51 |
+
self.tie_embeddings = tie_embeddings
|
| 52 |
+
self.hub_chunk = hub_chunk
|
| 53 |
+
self.hub_const = hub_const
|
| 54 |
+
self.hub_ckpt = hub_ckpt # inference: always 0
|
| 55 |
+
super().__init__(**kwargs)
|
| 56 |
+
|
| 57 |
+
def to_aleph(self) -> AlephLMConfig:
|
| 58 |
+
return AlephLMConfig(
|
| 59 |
+
name=self.name, d_model=self.d_model, n_layers=self.n_layers,
|
| 60 |
+
n_heads=self.n_heads, context=self.context,
|
| 61 |
+
vocab_size=self.vocab_size, tokenizer=self.tokenizer,
|
| 62 |
+
hub_layers=tuple(self.hub_layers), hub_K=self.hub_K,
|
| 63 |
+
hub_D=self.hub_D, tau=self.tau, bank_experts=self.bank_experts,
|
| 64 |
+
bank_ff=self.bank_ff, head_K=self.head_K, head_D=self.head_D,
|
| 65 |
+
gate_init=self.gate_init, tie_embeddings=self.tie_embeddings,
|
| 66 |
+
hub_chunk=self.hub_chunk, hub_const=self.hub_const,
|
| 67 |
+
hub_ckpt=0)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class MiniBeatrixForCausalLM(PreTrainedModel, GenerationMixin):
|
| 71 |
+
config_class = MiniBeatrixConfig
|
| 72 |
+
main_input_name = "input_ids"
|
| 73 |
+
supports_gradient_checkpointing = False
|
| 74 |
+
_tied_weights_keys = {}
|
| 75 |
+
|
| 76 |
+
def __init__(self, config: MiniBeatrixConfig):
|
| 77 |
+
super().__init__(config)
|
| 78 |
+
self.model = AlephLM(config.to_aleph())
|
| 79 |
+
self.post_init()
|
| 80 |
+
|
| 81 |
+
def _init_weights(self, module):
|
| 82 |
+
pass # AlephLM initializes itself; loader must not re-init
|
| 83 |
+
|
| 84 |
+
def forward(self, input_ids=None, labels=None, attention_mask=None,
|
| 85 |
+
past_key_values=None, use_cache=None, **kwargs):
|
| 86 |
+
# attention_mask is safely ignored: causal model, right-padding
|
| 87 |
+
# with label masking is the trained convention. No KV cache in
|
| 88 |
+
# this wrapper; generate() recomputes the prefix each step.
|
| 89 |
+
if input_ids.shape[1] > self.config.context:
|
| 90 |
+
input_ids = input_ids[:, -self.config.context:]
|
| 91 |
+
if labels is not None:
|
| 92 |
+
labels = labels[:, -self.config.context:]
|
| 93 |
+
out = self.model(input_ids, labels=labels)
|
| 94 |
+
logits = out[0]
|
| 95 |
+
loss = out[1] if labels is not None else None
|
| 96 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
| 97 |
+
|
| 98 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 99 |
+
return {"input_ids": input_ids[:, -self.config.context:]}
|