|
| 1 | +import torch |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +# Device setup |
| 6 | +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 7 | + |
| 8 | +# Input shape config |
| 9 | +num_nodes = 1 |
| 10 | +x_dim = 9 |
| 11 | +pe_dim = 20 |
| 12 | +edge_attr_dim = 2 |
| 13 | + |
| 14 | +# List of models and reference files to check |
| 15 | +models_to_test = [ |
| 16 | + ( |
| 17 | + "v0_1_2", |
| 18 | + "examples/models/GridFM_v0_1_2.pth", |
| 19 | + "tests/data/reference_output_v0_1_2.npy", |
| 20 | + ), |
| 21 | + ( |
| 22 | + "v0_2_3", |
| 23 | + "examples/models/GridFM_v0_2_3.pth", |
| 24 | + "tests/data/reference_output_v0_2_3.npy", |
| 25 | + ), |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("version, model_path, ref_output_path", models_to_test) |
| 30 | +def test_model_matches_reference(version, model_path, ref_output_path): |
| 31 | + torch.manual_seed(0) |
| 32 | + |
| 33 | + # Prepare zero input |
| 34 | + x = torch.zeros((num_nodes, x_dim), device=device) |
| 35 | + pe = torch.zeros((num_nodes, pe_dim), device=device) |
| 36 | + edge_index = torch.tensor([[0], [0]], device=device) |
| 37 | + edge_attr = torch.zeros((1, edge_attr_dim), device=device) |
| 38 | + batch = torch.zeros(num_nodes, dtype=torch.long, device=device) |
| 39 | + |
| 40 | + # Load model |
| 41 | + model = torch.load(model_path, weights_only=False, map_location=device).to(device) |
| 42 | + model.eval() |
| 43 | + |
| 44 | + # Get current output |
| 45 | + with torch.no_grad(): |
| 46 | + output = model(x, pe, edge_index, edge_attr, batch).cpu().numpy() |
| 47 | + |
| 48 | + # Load saved reference |
| 49 | + reference = np.load(ref_output_path) |
| 50 | + |
| 51 | + # Exact match assertion |
| 52 | + assert np.allclose(output, reference, rtol=1e-5, atol=1e-6), ( |
| 53 | + f"Model output for {version} does not match reference within tolerance.\n" |
| 54 | + f"Max absolute difference: {np.max(np.abs(output - reference))}" |
| 55 | + ) |
0 commit comments