Download tests/test_release.py from chrisuser/neuro-compiler: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/chrisuser/neuro-compiler/resolve/main/tests/test_release.py
- Command line
-
hf download hf://chrisuser/neuro-compiler/tests/test_release.py
-
curl -L -o test_release.py https://huggingface.co/chrisuser/neuro-compiler/resolve/main/tests/test_release.py
5.42 kB
| """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): | |
| 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('<Q',999999999)) | |
| with self.assertRaises(ValueError):_extract(a,b,1) | |
| self.assertFalse(b.exists()) | |
| def test_verifier_correct_add(self): | |
| r=verify('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}',bytes.fromhex('89f801f0c3'),samples=16) | |
| self.assertTrue(r['abi'] and r['semantic']);self.assertFalse(r['native_execution']);self.assertEqual(r['samples'],16) | |
| def test_verifier_semantic_error(self): | |
| r=verify('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}',bytes.fromhex('31c0c3'),samples=16) | |
| self.assertTrue(r['abi']);self.assertFalse(r['semantic']) | |
| def test_verifier_abi_failure(self): | |
| r=verify('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}',bytes.fromhex('31db89f801f0c3'),samples=4) | |
| self.assertFalse(r['abi']);self.assertIn('ABI',r['error']) | |
| def test_verifier_invalid_instruction(self): | |
| r=verify('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}',bytes.fromhex('0f05c3'),samples=4) | |
| self.assertFalse(r['abi']);self.assertIn('Unsupported',r['error']) | |
| def test_verifier_uninitialized_memory(self): | |
| r=verify('unsigned f(unsigned a,unsigned b,unsigned c){return (a+b);}',bytes.fromhex('8b4424fcc3'),samples=4) | |
| self.assertFalse(r['abi']);self.assertIn('Uninitialized',r['error']) | |
| def test_verifier_invalid_samples(self): | |
| for count in (0,1001,True): | |
| with self.assertRaises(ValueError):verify('a',b'a',samples=count) | |
| if __name__=='__main__':unittest.main() | |