Text Generation
Transformers
Safetensors
English
babylm
babylm-2026
strict-small
linear-attention
state-tracking
delta-rule
custom_code
Instructions to use SecludedCorner/bind2_0 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SecludedCorner/bind2_0 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="SecludedCorner/bind2_0", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("SecludedCorner/bind2_0", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use SecludedCorner/bind2_0 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "SecludedCorner/bind2_0" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "SecludedCorner/bind2_0", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/SecludedCorner/bind2_0
- SGLang
How to use SecludedCorner/bind2_0 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "SecludedCorner/bind2_0" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "SecludedCorner/bind2_0", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "SecludedCorner/bind2_0" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "SecludedCorner/bind2_0", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use SecludedCorner/bind2_0 with Docker Model Runner:
docker model run hf.co/SecludedCorner/bind2_0
bind2_0 23.9M build (BabyLM 2026 strict-small training)
Browse files- config.json +29 -0
- model.safetensors +3 -0
- modeling_babylm.py +257 -0
- requirements_pins.txt +25 -0
- tokenizer.json +0 -0
- tokenizer_config.json +9 -0
config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"K": 16,
|
| 3 |
+
"T": 3,
|
| 4 |
+
"arch": "bind2_0",
|
| 5 |
+
"architectures": [
|
| 6 |
+
"BabyLMForCausalLM"
|
| 7 |
+
],
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "modeling_babylm.BabyLMConfig",
|
| 10 |
+
"AutoModel": "modeling_babylm.BabyLMModel",
|
| 11 |
+
"AutoModelForCausalLM": "modeling_babylm.BabyLMForCausalLM"
|
| 12 |
+
},
|
| 13 |
+
"chunk": 32,
|
| 14 |
+
"core_n": 4,
|
| 15 |
+
"depth": 12,
|
| 16 |
+
"dim": 384,
|
| 17 |
+
"dtype": "float32",
|
| 18 |
+
"gdn_hd": 72,
|
| 19 |
+
"gdn_heads": 4,
|
| 20 |
+
"in_n": 3,
|
| 21 |
+
"mlp_hidden": 576,
|
| 22 |
+
"model_type": "babylm",
|
| 23 |
+
"n_layer": 12,
|
| 24 |
+
"nhead": 6,
|
| 25 |
+
"out_n": 3,
|
| 26 |
+
"tie_word_embeddings": false,
|
| 27 |
+
"transformers_version": "5.13.0",
|
| 28 |
+
"vocab_size": 16000
|
| 29 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b4728e7632322b053e928183348aca0144a0471b56134f8047645d29ef4db22
|
| 3 |
+
size 120270360
|
modeling_babylm.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Self-contained HuggingFace wrapper for the BabyLM entry (LoopLM) and monolith (LM), so the
|
| 3 |
+
models load as a stock AutoModelForCausalLM (trust_remote_code) for babylm-eval / leaderboard.
|
| 4 |
+
Model code is INLINED (no import of train_*.py) so this file is portable on the HF hub.
|
| 5 |
+
The ACTIVE class defs (LoopLMv2/Bind2 for arch "loop2", LM for the monolith) are byte-for-byte
|
| 6 |
+
the current training defs (train_loop.py / train_stage1.py) so state_dicts load exactly; the
|
| 7 |
+
legacy v1 defs (LoopLM/Bind) are retained ONLY to load the already-published v1 bypass
|
| 8 |
+
checkpoint (paper §4b diagnostic) and no longer exist in train_loop.py. forward() runs the whole loop inside a standard causal pass and
|
| 9 |
+
returns CausalLMOutput(logits, loss); empty-context, stateless across examples.
|
| 10 |
+
|
| 11 |
+
BabyLMModel (AutoModel entry) exists for the GLUE finetuning pipeline, which pools
|
| 12 |
+
last_hidden_state through its own classifier head. attention_mask is honored only on that
|
| 13 |
+
path (padded batches); the causal-LM path is unchanged — attn_mask=None reproduces the
|
| 14 |
+
exact zero-shot behavior the published eval numbers came from.
|
| 15 |
+
"""
|
| 16 |
+
import math, torch, torch.nn as nn, torch.nn.functional as F
|
| 17 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 18 |
+
from transformers.modeling_outputs import CausalLMOutput, BaseModelOutput
|
| 19 |
+
|
| 20 |
+
def build_rope(T, D, device, base=10000.0):
|
| 21 |
+
inv = 1.0/(base**(torch.arange(0,D,2,device=device).float()/D)); t = torch.arange(T,device=device).float()
|
| 22 |
+
f = torch.outer(t, inv); emb = torch.cat([f, f], dim=-1); return emb.cos(), emb.sin()
|
| 23 |
+
def rotate_half(x):
|
| 24 |
+
x1, x2 = x.chunk(2, dim=-1); return torch.cat((-x2, x1), dim=-1)
|
| 25 |
+
def apply_rope(x, cos, sin):
|
| 26 |
+
return x*cos[None,None] + rotate_half(x)*sin[None,None]
|
| 27 |
+
|
| 28 |
+
class Attn(nn.Module):
|
| 29 |
+
def __init__(self, d, nh):
|
| 30 |
+
super().__init__(); self.nh=nh; self.hd=d//nh
|
| 31 |
+
self.qkv=nn.Linear(d,3*d,bias=False); self.o=nn.Linear(d,d,bias=False)
|
| 32 |
+
def forward(self, x, cos, sin, attn_mask=None):
|
| 33 |
+
B,T,D=x.shape; qkv=self.qkv(x).view(B,T,3,self.nh,self.hd).permute(2,0,3,1,4)
|
| 34 |
+
q,k,v=qkv[0],qkv[1],qkv[2]; q=apply_rope(q,cos,sin); k=apply_rope(k,cos,sin)
|
| 35 |
+
if attn_mask is None: o=F.scaled_dot_product_attention(q,k,v,is_causal=True)
|
| 36 |
+
else: o=F.scaled_dot_product_attention(q,k,v,attn_mask=attn_mask)
|
| 37 |
+
return self.o(o.transpose(1,2).reshape(B,T,D))
|
| 38 |
+
class SwiGLU(nn.Module):
|
| 39 |
+
def __init__(self, d, h):
|
| 40 |
+
super().__init__(); self.w1=nn.Linear(d,h,bias=False); self.w3=nn.Linear(d,h,bias=False); self.w2=nn.Linear(h,d,bias=False)
|
| 41 |
+
def forward(self, x): return self.w2(F.silu(self.w1(x))*self.w3(x))
|
| 42 |
+
class Block(nn.Module):
|
| 43 |
+
def __init__(self, d, nh, h):
|
| 44 |
+
super().__init__(); self.n1=nn.RMSNorm(d); self.attn=Attn(d,nh); self.n2=nn.RMSNorm(d); self.mlp=SwiGLU(d,h)
|
| 45 |
+
def forward(self, x, cos, sin, attn_mask=None):
|
| 46 |
+
x=x+self.attn(self.n1(x),cos,sin,attn_mask); return x+self.mlp(self.n2(x))
|
| 47 |
+
|
| 48 |
+
class LM(nn.Module): # monolith (train_stage1.LM)
|
| 49 |
+
def __init__(self, vocab, d=384, nl=12, nh=6):
|
| 50 |
+
super().__init__(); h=((int(8/3*d)+63)//64)*64
|
| 51 |
+
self.emb=nn.Embedding(vocab,d); self.blocks=nn.ModuleList([Block(d,nh,h) for _ in range(nl)])
|
| 52 |
+
self.nf=nn.RMSNorm(d); self.head=nn.Linear(d,vocab,bias=False); self.head.weight=self.emb.weight
|
| 53 |
+
self.d=d; self.nh=nh
|
| 54 |
+
def hidden(self, ids, attn_mask=None):
|
| 55 |
+
cos,sin=build_rope(ids.shape[1], self.d//self.nh, ids.device); h=self.emb(ids)
|
| 56 |
+
for b in self.blocks: h=b(h,cos,sin,attn_mask)
|
| 57 |
+
return self.nf(h)
|
| 58 |
+
def forward(self, ids): return self.head(self.hidden(ids))
|
| 59 |
+
|
| 60 |
+
class Bind(nn.Module): # 想 + 行 (train_loop.Bind)
|
| 61 |
+
def __init__(self, d, K=16, dr=64):
|
| 62 |
+
super().__init__(); self.role=nn.Linear(d,K,bias=False); self.R=nn.Parameter(torch.randn(K,dr)*0.02)
|
| 63 |
+
self.up=nn.Linear(dr,d,bias=False); self.trust=nn.Linear(d,1)
|
| 64 |
+
def forward(self, h):
|
| 65 |
+
a=torch.softmax(self.role(h),dim=-1); lab=a@self.R; tau=torch.sigmoid(self.trust(h)); return h+tau*self.up(lab)
|
| 66 |
+
class LoopLM(nn.Module): # entry (train_loop.LoopLM)
|
| 67 |
+
def __init__(self, vocab, d=384, in_n=3, core_n=4, out_n=3, nh=6, T=3, K=16):
|
| 68 |
+
super().__init__(); hdim=((int(8/3*d)+63)//64)*64
|
| 69 |
+
self.emb=nn.Embedding(vocab,d)
|
| 70 |
+
self.inb=nn.ModuleList([Block(d,nh,hdim) for _ in range(in_n)])
|
| 71 |
+
self.core=nn.ModuleList([Block(d,nh,hdim) for _ in range(core_n)])
|
| 72 |
+
self.outb=nn.ModuleList([Block(d,nh,hdim) for _ in range(out_n)])
|
| 73 |
+
self.bind=Bind(d,K); self.vhead=nn.Linear(d,1)
|
| 74 |
+
self.nf=nn.RMSNorm(d); self.head=nn.Linear(d,vocab,bias=False); self.head.weight=self.emb.weight
|
| 75 |
+
self.d=d; self.nh=nh; self.T=T
|
| 76 |
+
def hidden(self, ids, attn_mask=None):
|
| 77 |
+
cos,sin=build_rope(ids.shape[1], self.d//self.nh, ids.device); h=self.emb(ids)
|
| 78 |
+
for b in self.inb: h=b(h,cos,sin,attn_mask)
|
| 79 |
+
for _ in range(self.T):
|
| 80 |
+
z=self.bind(h); h2=z
|
| 81 |
+
for b in self.core: h2=b(h2,cos,sin,attn_mask)
|
| 82 |
+
v=torch.sigmoid(self.vhead(h2)); h=h+(1.0-v)*(h2-h)
|
| 83 |
+
for b in self.outb: h=b(h,cos,sin,attn_mask)
|
| 84 |
+
return self.nf(h)
|
| 85 |
+
def forward(self, ids): return self.head(self.hidden(ids))
|
| 86 |
+
|
| 87 |
+
class Bind2(nn.Module): # v2 想+行 (train_loop.Bind, arch "loop2"): 受-driven trust + 熏習 prior + role-slice re-stamp
|
| 88 |
+
def __init__(self, d, K=16, dr=64):
|
| 89 |
+
super().__init__()
|
| 90 |
+
self.dr = dr
|
| 91 |
+
self.role = nn.Linear(d, K, bias=False)
|
| 92 |
+
self.role_scale = nn.Parameter(torch.ones(1))
|
| 93 |
+
self.R = nn.Parameter(torch.randn(K, dr) * 0.02)
|
| 94 |
+
self.trust = nn.Linear(d, 1)
|
| 95 |
+
self.v_gain = nn.Parameter(torch.zeros(1))
|
| 96 |
+
self.vasana = nn.Parameter(torch.zeros(K))
|
| 97 |
+
def forward(self, h, v_prev):
|
| 98 |
+
a = torch.softmax(self.role_scale * self.role(h), dim=-1)
|
| 99 |
+
lab = a @ self.R
|
| 100 |
+
tau = torch.sigmoid(self.trust(h) + (a @ self.vasana)[..., None] + self.v_gain * (0.5 - v_prev))
|
| 101 |
+
s = h[..., -self.dr:]
|
| 102 |
+
return torch.cat([h[..., :-self.dr], (1.0 - tau) * s + tau * lab], dim=-1), a, tau
|
| 103 |
+
|
| 104 |
+
class LoopLMv2(nn.Module): # entry v2 (train_loop.LoopLM, arch "loop2")
|
| 105 |
+
def __init__(self, vocab, d=384, in_n=3, core_n=4, out_n=3, nh=6, T=3, K=16):
|
| 106 |
+
super().__init__(); hdim=((int(8/3*d)+63)//64)*64
|
| 107 |
+
self.emb=nn.Embedding(vocab,d)
|
| 108 |
+
self.inb=nn.ModuleList([Block(d,nh,hdim) for _ in range(in_n)])
|
| 109 |
+
self.core=nn.ModuleList([Block(d,nh,hdim) for _ in range(core_n)])
|
| 110 |
+
self.outb=nn.ModuleList([Block(d,nh,hdim) for _ in range(out_n)])
|
| 111 |
+
self.bind=Bind2(d,K); self.vhead=nn.Linear(d,1)
|
| 112 |
+
self.nf=nn.RMSNorm(d); self.head=nn.Linear(d,vocab,bias=False); self.head.weight=self.emb.weight
|
| 113 |
+
self.d=d; self.nh=nh; self.T=T
|
| 114 |
+
def hidden(self, ids, attn_mask=None):
|
| 115 |
+
cos,sin=build_rope(ids.shape[1], self.d//self.nh, ids.device); h=self.emb(ids)
|
| 116 |
+
for b in self.inb: h=b(h,cos,sin,attn_mask)
|
| 117 |
+
v=torch.full_like(h[..., :1], 0.5)
|
| 118 |
+
for _ in range(self.T):
|
| 119 |
+
z,a,tau=self.bind(h,v); h2=z
|
| 120 |
+
for b in self.core: h2=b(h2,cos,sin,attn_mask)
|
| 121 |
+
v=torch.sigmoid(self.vhead(h2)); h=h2 # state flows through the loop (no bypass)
|
| 122 |
+
for b in self.outb: h=b(h,cos,sin,attn_mask)
|
| 123 |
+
return self.nf(h)
|
| 124 |
+
def forward(self, ids): return self.head(self.hidden(ids))
|
| 125 |
+
|
| 126 |
+
# --- delta-rule + forced-bottleneck (arch "bind2_0"); class defs byte-for-byte from modeling_bind2_0.py
|
| 127 |
+
# (train_bind2_0_babylm.py) so state_dicts load exactly. fla is imported lazily inside GDNBlock so
|
| 128 |
+
# this module still imports without fla for the mono/loop2 paths. ---
|
| 129 |
+
class ChunkedAttn(nn.Module):
|
| 130 |
+
"""Forced bottleneck: causal attention restricted to within non-overlapping chunks of size C."""
|
| 131 |
+
def __init__(self, d, nh, chunk):
|
| 132 |
+
super().__init__()
|
| 133 |
+
self.nh=nh; self.hd=d//nh; self.chunk=chunk
|
| 134 |
+
self.qkv=nn.Linear(d,3*d,bias=False); self.o=nn.Linear(d,d,bias=False)
|
| 135 |
+
def forward(self, x, cos, sin):
|
| 136 |
+
B,T,D=x.shape
|
| 137 |
+
qkv=self.qkv(x).view(B,T,3,self.nh,self.hd).permute(2,0,3,1,4)
|
| 138 |
+
q,k,v=qkv[0],qkv[1],qkv[2]
|
| 139 |
+
q=apply_rope(q,cos,sin); k=apply_rope(k,cos,sin)
|
| 140 |
+
idx=torch.arange(T,device=x.device)
|
| 141 |
+
same=(idx[:,None]//self.chunk)==(idx[None,:]//self.chunk)
|
| 142 |
+
causal=idx[:,None]>=idx[None,:]
|
| 143 |
+
keep=same&causal
|
| 144 |
+
mask=torch.zeros(T,T,device=x.device,dtype=q.dtype).masked_fill(~keep,float("-inf"))
|
| 145 |
+
o=F.scaled_dot_product_attention(q,k,v,attn_mask=mask)
|
| 146 |
+
return self.o(o.transpose(1,2).reshape(B,T,D))
|
| 147 |
+
class GDNBlock(nn.Module):
|
| 148 |
+
def __init__(self, d, idx, mlp_hidden, gdn_heads=4, gdn_hd=72):
|
| 149 |
+
super().__init__()
|
| 150 |
+
from fla.layers import GatedDeltaNet # lazy: only bind2_0 needs fla
|
| 151 |
+
self.n1=nn.RMSNorm(d)
|
| 152 |
+
self.gdn=GatedDeltaNet(hidden_size=d, num_heads=gdn_heads, head_dim=gdn_hd, layer_idx=idx)
|
| 153 |
+
self.n2=nn.RMSNorm(d); self.mlp=SwiGLU(d, mlp_hidden)
|
| 154 |
+
def forward(self, x):
|
| 155 |
+
m=self.gdn(self.n1(x))[0] # fla returns (output, attn, cache)
|
| 156 |
+
x=x+m
|
| 157 |
+
return x+self.mlp(self.n2(x))
|
| 158 |
+
class AttnBlock(nn.Module):
|
| 159 |
+
def __init__(self, d, nh, chunk, mlp_hidden):
|
| 160 |
+
super().__init__()
|
| 161 |
+
self.n1=nn.RMSNorm(d); self.attn=ChunkedAttn(d,nh,chunk)
|
| 162 |
+
self.n2=nn.RMSNorm(d); self.mlp=SwiGLU(d,mlp_hidden)
|
| 163 |
+
def forward(self, x, cos, sin):
|
| 164 |
+
x=x+self.attn(self.n1(x),cos,sin)
|
| 165 |
+
return x+self.mlp(self.n2(x))
|
| 166 |
+
class Bind2_0LM(nn.Module): # delta-rule + forced-bottleneck (modeling_bind2_0.Bind2_0LM)
|
| 167 |
+
def __init__(self, vocab, d=384, depth=12, nh=6, chunk=32, mlp_hidden=576, gdn_heads=4, gdn_hd=72):
|
| 168 |
+
super().__init__()
|
| 169 |
+
self.emb=nn.Embedding(vocab,d)
|
| 170 |
+
self.kinds=["attn" if (i+1)%4==0 else "gdn" for i in range(depth)] # 3:1 GDN:attn
|
| 171 |
+
self.blocks=nn.ModuleList([
|
| 172 |
+
GDNBlock(d,i,mlp_hidden,gdn_heads,gdn_hd) if k=="gdn" else AttnBlock(d,nh,chunk,mlp_hidden)
|
| 173 |
+
for i,k in enumerate(self.kinds)])
|
| 174 |
+
self.nf=nn.RMSNorm(d); self.head=nn.Linear(d,vocab,bias=False); self.head.weight=self.emb.weight
|
| 175 |
+
self.d=d; self.nh=nh; self.chunk=chunk
|
| 176 |
+
def hidden(self, ids, attn_mask=None): # attn_mask unused: chunked attn carries its own intra-chunk
|
| 177 |
+
cos,sin=build_rope(ids.shape[1], self.d//self.nh, ids.device); h=self.emb(ids) # mask (pad-mask
|
| 178 |
+
for blk,k in zip(self.blocks,self.kinds): # for GLUE is TODO,
|
| 179 |
+
h=blk(h) if k=="gdn" else blk(h,cos,sin) # zero-shot unaffected)
|
| 180 |
+
return self.nf(h)
|
| 181 |
+
def forward(self, ids): return self.head(self.hidden(ids))
|
| 182 |
+
|
| 183 |
+
def _build_backbone(config):
|
| 184 |
+
if config.arch == "bind2_0":
|
| 185 |
+
return Bind2_0LM(config.vocab_size, config.dim, config.depth, config.nhead,
|
| 186 |
+
chunk=config.chunk, mlp_hidden=config.mlp_hidden,
|
| 187 |
+
gdn_heads=config.gdn_heads, gdn_hd=config.gdn_hd)
|
| 188 |
+
if config.arch == "loop2":
|
| 189 |
+
return LoopLMv2(config.vocab_size, config.dim, config.in_n, config.core_n,
|
| 190 |
+
config.out_n, config.nhead, config.T, config.K)
|
| 191 |
+
if config.arch == "loop":
|
| 192 |
+
return LoopLM(config.vocab_size, config.dim, config.in_n, config.core_n,
|
| 193 |
+
config.out_n, config.nhead, config.T, config.K)
|
| 194 |
+
return LM(config.vocab_size, config.dim, config.n_layer, config.nhead)
|
| 195 |
+
|
| 196 |
+
class BabyLMConfig(PretrainedConfig):
|
| 197 |
+
model_type = "babylm"
|
| 198 |
+
# the GLUE finetuning classifier reads config.hidden_size
|
| 199 |
+
attribute_map = {"hidden_size": "dim", "num_attention_heads": "nhead", "num_hidden_layers": "n_layer"}
|
| 200 |
+
def __init__(self, arch="loop", vocab_size=16000, dim=384, in_n=3, core_n=4, out_n=3,
|
| 201 |
+
T=3, K=16, nhead=6, n_layer=12,
|
| 202 |
+
depth=12, chunk=32, mlp_hidden=576, gdn_heads=4, gdn_hd=72, **kw):
|
| 203 |
+
self.arch=arch; self.vocab_size=vocab_size; self.dim=dim; self.in_n=in_n; self.core_n=core_n
|
| 204 |
+
self.out_n=out_n; self.T=T; self.K=K; self.nhead=nhead; self.n_layer=n_layer
|
| 205 |
+
self.depth=depth; self.chunk=chunk; self.mlp_hidden=mlp_hidden; self.gdn_heads=gdn_heads; self.gdn_hd=gdn_hd
|
| 206 |
+
super().__init__(**kw)
|
| 207 |
+
|
| 208 |
+
class BabyLMForCausalLM(PreTrainedModel):
|
| 209 |
+
config_class = BabyLMConfig
|
| 210 |
+
def __init__(self, config):
|
| 211 |
+
super().__init__(config)
|
| 212 |
+
self.backbone = _build_backbone(config)
|
| 213 |
+
# Untie the LM head for a clean HF save (no shared tensors). Inference-equivalent: the head
|
| 214 |
+
# weight is loaded from the checkpoint, which equals the tied embedding used at train time.
|
| 215 |
+
self.backbone.head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 216 |
+
self.config.tie_word_embeddings = False
|
| 217 |
+
self.post_init()
|
| 218 |
+
def tie_weights(self, *args, **kwargs):
|
| 219 |
+
pass # head intentionally untied for export
|
| 220 |
+
def get_input_embeddings(self): return self.backbone.emb
|
| 221 |
+
def set_input_embeddings(self, v): self.backbone.emb = v
|
| 222 |
+
def get_output_embeddings(self): return self.backbone.head
|
| 223 |
+
def forward(self, input_ids=None, labels=None, attention_mask=None, **kw):
|
| 224 |
+
logits = self.backbone(input_ids)
|
| 225 |
+
loss = None
|
| 226 |
+
if labels is not None:
|
| 227 |
+
loss = F.cross_entropy(logits[:, :-1].reshape(-1, logits.size(-1)).float(), labels[:, 1:].reshape(-1))
|
| 228 |
+
return CausalLMOutput(loss=loss, logits=logits)
|
| 229 |
+
|
| 230 |
+
def padding_causal_mask(attention_mask):
|
| 231 |
+
# bool SDPA mask (B,1,T,T): attend where causal AND the key is a real (non-pad) token.
|
| 232 |
+
# Pad-query rows would be fully masked (softmax NaN) with left padding, so the diagonal
|
| 233 |
+
# stays open; their outputs are finite and get zero weight from every real query.
|
| 234 |
+
B, T = attention_mask.shape; dev = attention_mask.device
|
| 235 |
+
causal = torch.tril(torch.ones(T, T, dtype=torch.bool, device=dev))
|
| 236 |
+
m = causal[None, None] & attention_mask.to(torch.bool)[:, None, None, :]
|
| 237 |
+
return m | torch.eye(T, dtype=torch.bool, device=dev)[None, None]
|
| 238 |
+
|
| 239 |
+
class BabyLMModel(PreTrainedModel):
|
| 240 |
+
"""AutoModel entry (base model, no LM head applied) for the GLUE finetuning pipeline.
|
| 241 |
+
Same backbone module tree as BabyLMForCausalLM so the exported checkpoint loads key-for-key."""
|
| 242 |
+
config_class = BabyLMConfig
|
| 243 |
+
def __init__(self, config):
|
| 244 |
+
super().__init__(config)
|
| 245 |
+
self.backbone = _build_backbone(config)
|
| 246 |
+
self.backbone.head = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 247 |
+
self.config.tie_word_embeddings = False
|
| 248 |
+
self.post_init()
|
| 249 |
+
def tie_weights(self, *args, **kwargs):
|
| 250 |
+
pass # head intentionally untied for export
|
| 251 |
+
def get_input_embeddings(self): return self.backbone.emb
|
| 252 |
+
def set_input_embeddings(self, v): self.backbone.emb = v
|
| 253 |
+
def forward(self, input_ids=None, attention_mask=None, **kw):
|
| 254 |
+
attn_mask = None
|
| 255 |
+
if attention_mask is not None and not bool(attention_mask.all()):
|
| 256 |
+
attn_mask = padding_causal_mask(attention_mask)
|
| 257 |
+
return BaseModelOutput(last_hidden_state=self.backbone.hidden(input_ids, attn_mask))
|
requirements_pins.txt
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Exact dependency versions the bind2_0 checkpoints were trained/exported with.
|
| 2 |
+
# Source: local conda env `babylm-smoke` (C:/Users/yulin/.conda/envs/babylm-smoke),
|
| 3 |
+
# queried via pip on 2026-07-15.
|
| 4 |
+
#
|
| 5 |
+
# Python: 3.11.15
|
| 6 |
+
#
|
| 7 |
+
# NOTE on fla (flash-linear-attention): the inlined HF modeling code
|
| 8 |
+
# (modeling_babylm.py) DOES import it — lazily, inside GDNBlock:
|
| 9 |
+
# `from fla.layers import GatedDeltaNet` (only executed when arch == "bind2_0")
|
| 10 |
+
# Since the bind2_0 exports instantiate GDN blocks, fla IS required at runtime
|
| 11 |
+
# to load/run these checkpoints. Installed from PyPI as release 0.5.1
|
| 12 |
+
# (no commit-pin / direct-URL metadata present in the env; pulls fla-core 0.5.1).
|
| 13 |
+
#
|
| 14 |
+
# NOTE on triton: the env uses the Windows fork `triton-windows`
|
| 15 |
+
# (github.com/woct0rdho/triton-windows); on Linux use the matching upstream
|
| 16 |
+
# `triton` that your torch build requires.
|
| 17 |
+
# torch build is CUDA 12.6 (`+cu126`); pick the equivalent build for your platform.
|
| 18 |
+
|
| 19 |
+
torch==2.12.1+cu126
|
| 20 |
+
transformers==5.13.0
|
| 21 |
+
triton-windows==3.7.1.post27
|
| 22 |
+
flash-linear-attention==0.5.1
|
| 23 |
+
fla-core==0.5.1
|
| 24 |
+
safetensors==0.8.0
|
| 25 |
+
numpy==2.4.6
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<bos>",
|
| 4 |
+
"eos_token": "<eos>",
|
| 5 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 6 |
+
"pad_token": "<pad>",
|
| 7 |
+
"tokenizer_class": "TokenizersBackend",
|
| 8 |
+
"unk_token": "<unk>"
|
| 9 |
+
}
|