forked from mllam/neural-lam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_training.py
103 lines (88 loc) · 2.83 KB
/
test_training.py
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
# Standard library
from pathlib import Path
# Third-party
import pytest
import pytorch_lightning as pl
import torch
import wandb
# First-party
from neural_lam import config as nlconfig
from neural_lam.create_graph import create_graph_from_datastore
from neural_lam.datastore import DATASTORES
from neural_lam.datastore.base import BaseRegularGridDatastore
from neural_lam.models.graph_lam import GraphLAM
from neural_lam.weather_dataset import WeatherDataModule
from tests.conftest import init_datastore_example
@pytest.mark.parametrize("datastore_name", DATASTORES.keys())
def test_training(datastore_name):
datastore = init_datastore_example(datastore_name)
if not isinstance(datastore, BaseRegularGridDatastore):
pytest.skip(
f"Skipping test for {datastore_name} as it is not a regular "
"grid datastore."
)
if torch.cuda.is_available():
device_name = "cuda"
torch.set_float32_matmul_precision(
"high"
) # Allows using Tensor Cores on A100s
else:
device_name = "cpu"
trainer = pl.Trainer(
max_epochs=1,
deterministic=True,
accelerator=device_name,
# XXX: `devices` has to be set to 2 otherwise
# neural_lam.models.ar_model.ARModel.aggregate_and_plot_metrics fails
# because it expects to aggregate over multiple devices
devices=2,
log_every_n_steps=1,
)
graph_name = "1level"
graph_dir_path = Path(datastore.root_path) / "graph" / graph_name
if not graph_dir_path.exists():
create_graph_from_datastore(
datastore=datastore,
output_root_path=str(graph_dir_path),
n_max_levels=1,
)
data_module = WeatherDataModule(
datastore=datastore,
ar_steps_train=3,
ar_steps_eval=5,
standardize=True,
batch_size=2,
num_workers=1,
num_past_forcing_steps=1,
num_future_forcing_steps=1,
)
class ModelArgs:
output_std = False
loss = "mse"
restore_opt = False
n_example_pred = 1
# XXX: this should be superfluous when we have already defined the
# model object no?
graph = graph_name
hidden_dim = 4
hidden_layers = 1
processor_layers = 2
mesh_aggr = "sum"
lr = 1.0e-3
val_steps_to_log = [1, 3]
metrics_watch = []
num_past_forcing_steps = 1
num_future_forcing_steps = 1
model_args = ModelArgs()
config = nlconfig.NeuralLAMConfig(
datastore=nlconfig.DatastoreSelection(
kind=datastore.SHORT_NAME, config_path=datastore.root_path
)
)
model = GraphLAM( # noqa
args=model_args,
datastore=datastore,
config=config,
)
wandb.init()
trainer.fit(model=model, datamodule=data_module)