"""CPU contract tests; no backbone download or training required.""" import json,shutil,struct,tempfile,unittest from pathlib import Path import torch from safetensors.torch import save_file,load_file from neuro_compiler import NeuralCompiler from neuro_compiler.backbone import _extract from neuro_compiler.verification import verify ROOT=Path(__file__).resolve().parents[1] class ReleaseTests(unittest.TestCase): @classmethod def setUpClass(cls): torch.set_num_threads(2) cls.compiler=NeuralCompiler.from_pretrained(ROOT,device='cpu',load_backbone=False) def test_all_parameters_frozen(self): for model in (self.compiler.ast,self.compiler.ssa,self.compiler.isel,self.compiler.backend): self.assertFalse(model.training) self.assertTrue(all(not p.requires_grad for p in model.parameters())) def test_accepted_operation_range(self): cases=json.loads((ROOT/'evaluation/cases.json').read_text()) self.assertEqual(len(self.compiler._inputs([r['source'] for r in cases])),600) self.assertEqual(set(r['operations'] for r in cases),set(range(1,9))) def test_scope_rejections(self): cases=['int f(){return 1;}','unsigned f(unsigned a,unsigned b,unsigned c){return a+64u;}','unsigned f(unsigned a,unsigned b,unsigned c){return a+0u;}','unsigned f(unsigned a,unsigned b,unsigned c){return a+7;}','unsigned f(unsigned a,unsigned b,unsigned c){return a/b;}','unsigned f(unsigned a,unsigned b,unsigned c){return a;}','unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}'+(' '*106),'unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}'+chr(233),'unsigned f(unsigned a,unsigned b,unsigned c){return (((((((((a+b)+a)+a)+a)+a)+a)+a)+a)+a);}'] for source in cases: with self.subTest(source=source),self.assertRaises(ValueError):self.compiler._inputs([source]) def test_source_requires_backbone(self): with self.assertRaisesRegex(RuntimeError,'backbone'): self.compiler.compile('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}') def test_corrupt_weight_rejected(self): with tempfile.TemporaryDirectory() as d: root=Path(d);(root/'manifest.json').write_text((ROOT/'manifest.json').read_text());(root/'weights').mkdir() (root/'weights/ast.safetensors').write_bytes(b'corrupt') with self.assertRaisesRegex(ValueError,'integrity'):NeuralCompiler.from_pretrained(root,device='cpu',load_backbone=False) def test_manifest_lineage_rejected(self): with tempfile.TemporaryDirectory() as d: manifest=json.loads((ROOT/'manifest.json').read_text());manifest['stages']['ssa']['config']['ast_checkpoint_sha256']='0'*64 (Path(d)/'manifest.json').write_text(json.dumps(manifest)) with self.assertRaisesRegex(ValueError,'relationship'):NeuralCompiler.from_pretrained(d,device='cpu',load_backbone=False) def test_truncated_prefix_is_exact(self): with tempfile.TemporaryDirectory() as d: source=Path(d)/'full.safetensors';target=Path(d)/'prefix.safetensors' tensors={'embed_tokens.weight':torch.arange(12).reshape(3,4).float(),'layers.0.weight':torch.arange(8).float(),'layers.1.weight':torch.ones(4),'norm.weight':torch.ones(3)} save_file(tensors,str(source));original=source.read_bytes();_extract(source,target,1);loaded=load_file(str(target)) self.assertEqual(set(loaded),set(tensors)-{'layers.1.weight'}) for k,v in loaded.items():self.assertTrue(torch.equal(v,tensors[k])) self.assertEqual(source.read_bytes(),original) def test_prefix_rejects_truncated_file(self): with tempfile.TemporaryDirectory() as d: a=Path(d)/'a';b=Path(d)/'b';a.write_bytes(struct.pack('