Download common.py from SlayerLab/Slayer1B-Poetry: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/SlayerLab/Slayer1B-Poetry/resolve/main/common.py
- Command line
-
hf download hf://SlayerLab/Slayer1B-Poetry/common.py
-
curl -L -o common.py https://huggingface.co/SlayerLab/Slayer1B-Poetry/resolve/main/common.py
1.9 kB
| """Pinned native Slayer model acquisition and LoRA attachment.""" | |
| import hashlib,importlib.util,json,pathlib | |
| from types import SimpleNamespace | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from lora import inject | |
| def load_base(recipe,device='cuda'): | |
| b=recipe['base'];path=hf_hub_download(b['archive_repo'],b['checkpoint'],revision=b['archive_revision']) | |
| sums=pathlib.Path(hf_hub_download(b['archive_repo'],'SHA256SUMS',revision=b['archive_revision'])).read_text() | |
| expected={s.split()[1]:s.split()[0] for s in sums.splitlines() if s.strip()}[b['checkpoint']] | |
| with open(path,'rb') as f:actual=hashlib.file_digest(f,'sha256').hexdigest() | |
| if actual!=expected:raise ValueError('Base checkpoint SHA256 mismatch') | |
| trainer_path=hf_hub_download(b['model_repo'],'train_gpt_ref_r24_2.py',revision=b['trainer_revision']) | |
| spec=importlib.util.spec_from_file_location('slayer_native_trainer',trainer_path);mod=importlib.util.module_from_spec(spec);spec.loader.exec_module(mod) | |
| state=torch.load(path,map_location='cpu',weights_only=True,mmap=True);cfg=SimpleNamespace(**state['config']) | |
| with torch.device('meta'):model=mod.GPT(cfg.vocab,cfg.n_layer,cfg.n_embd,cfg.n_head,cfg.block,cfg) | |
| model.load_state_dict(state['model'],strict=True,assign=True);model.head.weight=model.tok.weight;del state | |
| model=model.to(device=device,dtype=torch.bfloat16);cfg.act_ckpt=recipe['training'].get('activation_checkpointing',True);cfg.loss_chunk=recipe['training'].get('loss_chunk',128);cfg.z_loss=0. | |
| return model,{'base_sha256':actual,'trainer_sha256':hashlib.sha256(pathlib.Path(trainer_path).read_bytes()).hexdigest(),'native_config':vars(cfg)} | |
| def attach(model,recipe): | |
| c=recipe['lora'];names=inject(model,c['rank'],c['alpha'],c['dropout'],c['targets']) | |
| if len(names)!=100:raise ValueError(f'Expected 100 LoRA linears, found {len(names)}') | |
| return names | |