Download tests/test_models.py from HWresearch/GNN4Colliders: direct link, hf CLI and curl.
- Browser
- Download file 4.78 kB
-
https://huggingface.co/HWresearch/GNN4Colliders/resolve/main/tests/test_models.py
- Command line
-
hf download hf://HWresearch/GNN4Colliders/tests/test_models.py
-
curl -L -o test_models.py https://huggingface.co/HWresearch/GNN4Colliders/resolve/main/tests/test_models.py
4.78 kB
| import dataclasses | |
| import pytest | |
| import torch | |
| from conftest import PRETRAINED, random_events | |
| from gnn4colliders.config import ModelConfig | |
| from gnn4colliders.graphs import collate_graph_batch, edge_features | |
| from gnn4colliders.models import ( | |
| EdgeNetwork, | |
| FineTunedEdgeNetwork, | |
| build_model, | |
| pair_features, | |
| state_dict_of, | |
| ) | |
| from gnn4colliders.sequences import collate_sequence_batch | |
| CHECKPOINTS = [ | |
| PRETRAINED / "multiclass_pretrained_model_12/model_epoch_71.pt", | |
| PRETRAINED / "multilabel_pretrained_model_41/model_epoch_44.pt", | |
| ] | |
| def test_pretrained_checkpoints_fine_tune(checkpoint): | |
| if checkpoint.read_bytes()[:7] == b"version": | |
| pytest.fail(f"{checkpoint} is an unfetched Git LFS pointer") | |
| events = random_events(seed=1) | |
| batch = collate_graph_batch(events, torch.arange(len(events))) | |
| model = build_model( | |
| ModelConfig(out_size=5, pretrained=str(checkpoint), freeze_backbone=True), | |
| num_features=7, | |
| num_node_types=5, | |
| ) | |
| assert isinstance(model, FineTunedEdgeNetwork) | |
| pretrained = state_dict_of( | |
| torch.load(checkpoint, map_location="cpu", weights_only=False) | |
| ) | |
| for name, value in model.backbone.state_dict().items(): | |
| assert torch.equal(value, pretrained[name]) | |
| assert [name for name, p in model.named_parameters() if p.requires_grad] == [ | |
| "classifier.weight", | |
| "classifier.bias", | |
| ] | |
| logits = model(batch) | |
| assert logits.shape == (len(events), 5) and torch.isfinite(logits).all() | |
| def test_state_dict_keys_of_wrapped_and_original_models_are_mapped(): | |
| state = {"module.classify.weight": 1, "_orig_mod.node_encoder.0.bias": 2} | |
| assert state_dict_of({"model_state_dict": state}) == { | |
| "classifier.weight": 1, | |
| "node_encoder.0.bias": 2, | |
| } | |
| def test_edge_network_uses_global_features_when_configured(): | |
| events = random_events(seed=2) | |
| events = type(events)(**{**events.__dict__, "global_features": torch.rand(40, 2)}) | |
| batch = collate_graph_batch(events, torch.arange(8)) | |
| model = EdgeNetwork(7, 3, 2, 8, 3, 2, 1) | |
| assert model(batch).shape == (8, 3) | |
| SEQUENCE_CONFIGS = { | |
| "transformer": ModelConfig(name="transformer", hid_size=16, out_size=3, n_layers=1), | |
| "pair_bias": ModelConfig( | |
| name="transformer", hid_size=16, out_size=3, n_layers=1, pair_bias=True | |
| ), | |
| "deep_sets": ModelConfig(name="deep_sets", hid_size=16, out_size=3, n_layers=2), | |
| } | |
| def _sequence_model(name): | |
| torch.manual_seed(0) | |
| return build_model(SEQUENCE_CONFIGS[name], num_features=7, num_node_types=5).eval() | |
| def _typed_events(seed): | |
| events = random_events(seed=seed) | |
| events.object_features[:, 6] = torch.randint( | |
| 0, 5, (events.object_features.shape[0],) | |
| ).float() | |
| return events | |
| def test_sequence_models_ignore_padding_tokens(name): | |
| events, model = _typed_events(3), _sequence_model(name) | |
| alone = collate_sequence_batch(events, torch.tensor([0])) | |
| batch = collate_sequence_batch(events, torch.arange(8)) | |
| with torch.no_grad(): | |
| torch.testing.assert_close(model(batch)[:1], model(alone), rtol=1e-5, atol=1e-5) | |
| def test_sequence_models_do_not_depend_on_object_order(name): | |
| events, model = _typed_events(4), _sequence_model(name) | |
| batch = collate_sequence_batch(events, torch.arange(8)) | |
| permutation = torch.randperm(batch.tokens.shape[1]) | |
| shuffled = dataclasses.replace( | |
| batch, | |
| tokens=batch.tokens[:, permutation], | |
| token_mask=batch.token_mask[:, permutation], | |
| ) | |
| with torch.no_grad(): | |
| torch.testing.assert_close(model(shuffled), model(batch), rtol=1e-5, atol=1e-5) | |
| def test_pair_features_are_the_edge_features_and_the_pair_mass(): | |
| events = _typed_events(5) | |
| batch = collate_sequence_batch(events, torch.tensor([0])) | |
| tokens = batch.tokens[0] | |
| pairs = pair_features(batch.tokens)[0] | |
| src, dst = torch.meshgrid( | |
| torch.arange(tokens.shape[0]), torch.arange(tokens.shape[0]), indexing="ij" | |
| ) | |
| expected = edge_features(tokens, src.flatten(), dst.flatten()) | |
| torch.testing.assert_close(pairs[..., :3].reshape(-1, 3), expected) | |
| # The mass of two massless four-vectors. | |
| pt, eta, phi = tokens[:, 0], tokens[:, 1], tokens[:, 2] | |
| pt, eta, phi = pt.double(), eta.double(), phi.double() | |
| p = torch.stack( | |
| (pt * eta.cosh(), pt * phi.cos(), pt * phi.sin(), pt * eta.sinh()), dim=1 | |
| ) | |
| total = p[0] + p[1] | |
| m2 = total[0] ** 2 - (total[1:] ** 2).sum() | |
| torch.testing.assert_close( | |
| pairs[0, 1, 3].double(), torch.log1p(m2), rtol=1e-5, atol=0 | |
| ) | |