Download tests/test_tokenizer.py from Scinetics-ai/all-atom-structure-tokenizer: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/Scinetics-ai/all-atom-structure-tokenizer/resolve/main/tests/test_tokenizer.py
- Command line
-
hf download hf://Scinetics-ai/all-atom-structure-tokenizer/tests/test_tokenizer.py
-
curl -L -o test_tokenizer.py https://huggingface.co/Scinetics-ai/all-atom-structure-tokenizer/resolve/main/tests/test_tokenizer.py
3.24 kB
| from pathlib import Path | |
| import pytest | |
| import torch | |
| from atomtoken import Config, FSQ, StructureTokenizer, load_assembly, tokenizer_loss | |
| from atomtoken.geometry import neighbors, signed_volumes | |
| EXAMPLE = Path(__file__).parents[1] / "examples" / "peptide_ligand.json" | |
| def assembly(): | |
| torch.manual_seed(19) | |
| torch.set_num_threads(1) | |
| return (*load_assembly(EXAMPLE), StructureTokenizer(Config.tiny()).eval()) | |
| def test_all_codes_round_trip(): | |
| fsq = FSQ(5) | |
| with torch.no_grad(): | |
| fsq.project.weight.copy_(torch.eye(5)) | |
| fsq.project.bias.zero_() | |
| ids = torch.arange(8192) | |
| coords = fsq.from_ids(ids) | |
| actual, codes, _ = fsq(torch.atanh(coords.clamp(-.999999, .999999))) | |
| assert torch.equal(actual, ids) | |
| torch.testing.assert_close(codes, coords) | |
| with pytest.raises(ValueError): | |
| fsq.from_ids(torch.tensor([8192])) | |
| def test_rigid_motion_and_chirality(assembly): | |
| chem, x, mask, model = assembly | |
| rot, _ = torch.linalg.qr(torch.randn(3,3)) | |
| rot[:, 0] *= torch.linalg.det(rot).sign() | |
| transformed = x@rot + torch.tensor([7., -3., 2.]) | |
| h, _ = model.encoder(chem,x,mask) | |
| h2, _ = model.encoder(chem,transformed,mask) | |
| torch.testing.assert_close(h,h2,rtol=2e-5,atol=3e-5) | |
| ids = model.encode(chem,x,mask) | |
| assert torch.equal(ids, model.encode(chem,transformed,mask)) | |
| assert ids.shape == (5,) | |
| code = model.fsq.from_ids(ids) | |
| v = model.decoder(chem,code,x/10,.4) | |
| v2 = model.decoder(chem,code,transformed/10,.4) | |
| torch.testing.assert_close(v2,v@rot,rtol=2e-4,atol=1e-5) | |
| vol = signed_volumes(x,chem.triplets) | |
| assert vol.abs().max()>.1 | |
| torch.testing.assert_close(vol, -signed_volumes(-x,chem.triplets)) | |
| def test_missing_atoms_and_graph_types(assembly): | |
| chem, x, mask, model = assembly | |
| # A stretched covalent link remains present, including when spatial caps are zero. | |
| far = x.clone() | |
| far[0] += 100 | |
| dst, src, rel, _ = neighbors(chem, far, mask, caps=(0,0)) | |
| assert len(dst)==2*len(chem.bonds) and ((dst==0)&(src==1)&(rel==0)).any() | |
| dst, src, rel, _ = neighbors(chem, x, mask, caps=(1,1)) | |
| assert (rel==2).any() and (rel==1).any() | |
| for kind in (1,2): | |
| assert torch.bincount(dst[rel==kind], minlength=len(x)).max()<=1 | |
| mask[[3,11]] = False | |
| orig = model.encode(chem,x,mask) | |
| x[~mask] = torch.nan | |
| assert torch.equal(orig,model.encode(chem,x,mask)) and orig[-1]==-1 | |
| loss,_=tokenizer_loss(model,chem,x,mask) | |
| assert torch.isfinite(loss) | |
| loss.backward() | |
| for module in (model.encoder,model.fsq,model.decoder): | |
| grads=[p.grad for p in module.parameters() if p.grad is not None] | |
| assert all(g.isfinite().all() for g in grads) and sum(g.abs().sum() for g in grads)>0 | |
| def test_decoder_uses_codes_and_seed(assembly): | |
| chem, x, mask, model = assembly | |
| ids = model.encode(chem,x,mask) | |
| sample = lambda i: model.decode(chem,i,steps=3,generator=torch.Generator().manual_seed(5)) | |
| a = sample(ids) | |
| x.zero_() | |
| assert torch.equal(a,sample(ids)) and a.shape == (12,3) | |
| assert not torch.allclose(a,sample((ids+27)%8192)) | |
| assert a.isfinite().all() | |
| torch.testing.assert_close(a.mean(0),torch.zeros(3),atol=1e-5,rtol=0) | |