"""Reviewed native Slayer loaders. Only pinned config/tokenizer/safetensors are downloaded.""" import hashlib,json from pathlib import Path from types import SimpleNamespace FILES=('sub1b_native.py','sub1b_slayer139.py','sub1b_gollem.py','sub1b_balanced.py') def fingerprints(): root=Path(__file__).resolve().parent return {n:hashlib.sha256((root/n).read_bytes()).hexdigest() for n in FILES} def load(info): import torch from huggingface_hub import hf_hub_download from safetensors.torch import load_model from tokenizers import Tokenizer files={n:hf_hub_download(info['id'],n,revision=info['revision'],token=False) for n in ('config.json','model.safetensors','tokenizer.json')} config=json.loads(Path(files['config.json']).read_text());adapter=info['adapter'] if adapter=='balanced': from sub1b_balanced import BalancedLM native=BalancedLM(config) elif adapter in ('gollem','slayer139'): if adapter=='gollem':from sub1b_gollem import GPT else:from sub1b_slayer139 import GPT cfg=SimpleNamespace(**config);native=GPT(cfg.vocab,cfg.n_layer,cfg.n_embd,cfg.n_head,cfg.block,cfg) else:raise ValueError('Unreviewed native adapter') load_model(native,files['model.safetensors'],strict=True);native=native.float().cuda().eval() raw=Tokenizer.from_file(files['tokenizer.json']);raw.no_truncation();raw.no_padding() class Tokens: bos_token_id=config.get('bos_token_id',0);eos_token_id=config.get('eos_token_id',0) def encode(self,text,add_special_tokens=False):return raw.encode(text,add_special_tokens=add_special_tokens).ids class Wrapper(torch.nn.Module): def __init__(self):super().__init__();self.native=native def forward(self,ids,attention_mask,position_ids,use_cache,logits_to_keep): # Native architectures have no padding-mask interface. Strip padding for each forward; # preserve exact positions and predictive context, never let pad tokens change a score. values=[] for row,mask in zip(ids,attention_mask): logits=self.native(row[mask.bool()][None]);logits=logits[0] if isinstance(logits,tuple) else logits logits=logits[:,-logits_to_keep:] if logits.shape[1]