File size: 4,777 Bytes
f43b1e5
 
0cef53c
 
 
 
 
df3eb75
0cef53c
 
 
 
df3eb75
0cef53c
 
 
 
 
 
973bea8
0cef53c
 
 
 
 
 
 
 
 
 
 
 
f43b1e5
0cef53c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df3eb75
 
 
 
 
 
 
 
 
 
 
 
f43b1e5
 
 
 
 
 
 
 
 
 
df3eb75
 
 
0cef53c
 
 
 
f43b1e5
 
df3eb75
 
 
f43b1e5
 
 
 
 
 
 
 
 
df3eb75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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",
]


@pytest.mark.parametrize("checkpoint", CHECKPOINTS, ids=lambda path: path.parent.name)
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


@pytest.mark.parametrize("name", SEQUENCE_CONFIGS)
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)


@pytest.mark.parametrize("name", SEQUENCE_CONFIGS)
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
    )