"""Opt-in real-checkpoint tests. Run after publication with NTR_GPU_TEST=1.""" import os import pytest import torch @pytest.mark.skipif(os.environ.get("NTR_GPU_TEST")!="1",reason="set NTR_GPU_TEST=1 for the published real-checkpoint acceptance test") def test_real_checkpoint_replay_rows_and_restart(tmp_path,monkeypatch): from transformers import AutoTokenizer from novel_tokens.model import Policy,SYMBOLS from novel_tokens.policy import step,scratch from novel_tokens.tasks import panel from novel_tokens.training import build_optimizer,save_restart,resume assert torch.cuda.is_available() model=Policy(17,"symbols") optimizer=build_optimizer(model) rng=torch.Generator(device=model.device).manual_seed(411) initial=model.input_rows.detach().clone() assert model.tied and model.out_rows.data_ptr()==model.input_rows.data_ptr() assert len(set(model.new_ids))==32 and model.tok.decode(model.new_ids,skip_special_tokens=False)=="".join(SYMBOLS) assert not torch.equal(initial,model.embed.weight[model.new_ids].float()) batch=panel("cups","train",4,12) before=model.head.weight[:4].detach().clone() metrics,_=step(model,optimizer,batch,"symbols",rng,calibration=True) assert metrics["replay_error"]<.065 and model.input_rows.grad.abs().sum()>0 assert not torch.equal(initial,model.input_rows) assert torch.equal(before,model.head.weight[:4]) model.save_adapter(tmp_path) reloaded=AutoTokenizer.from_pretrained(tmp_path) assert [reloaded.encode(s,add_special_tokens=False) for s in SYMBOLS]==[[i] for i in model.new_ids] spec={"test":"restart"} save_restart(tmp_path,model,optimizer,rng,1,spec,[metrics]) pack=model.pack([batch[0].question]) expected=scratch(model,pack,"symbols",rng).ids trained=model.input_rows.detach().clone() with torch.no_grad(): model.input_rows.zero_() model.load_adapter(tmp_path) assert torch.equal(trained,model.input_rows) it,history=resume(tmp_path,model,optimizer,rng,spec) assert it==1 and history==[metrics] assert torch.equal(expected,scratch(model,pack,"symbols",rng).ids) from novel_tokens import evaluation,runtime monkeypatch.setattr(evaluation,"RESULTS",tmp_path/"panels") # The acceptance probe owns its temporary control directory, independently of live experiments. monkeypatch.setattr(runtime,"STORE",tmp_path/"local-controls") result=evaluation.evaluate(model,"smoke","symbols","cups","smoke",count=4,batch=4) assert result["n"]==4 and set(result["conditions"])=={"intact","donor","random","direct"} assert (tmp_path/"panels"/"smoke"/result["raw"]).is_file()