forked from mllam/neural-lam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
275 lines (227 loc) · 8.5 KB
/
utils.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Standard library
import os
import shutil
# Third-party
import numpy as np
import torch
from torch import nn
from tueplots import bundles, figsizes
def load_dataset_stats(dataset_name, device="cpu"):
"""
Load arrays with stored dataset statistics from pre-processing
"""
static_dir_path = os.path.join("data", dataset_name, "static")
def loads_file(fn):
return torch.load(
os.path.join(static_dir_path, fn), map_location=device
)
data_mean = loads_file("parameter_mean.pt") # (d_features,)
data_std = loads_file("parameter_std.pt") # (d_features,)
flux_stats = loads_file("flux_stats.pt") # (2,)
flux_mean, flux_std = flux_stats
return {
"data_mean": data_mean,
"data_std": data_std,
"flux_mean": flux_mean,
"flux_std": flux_std,
}
def load_static_data(dataset_name, device="cpu"):
"""
Load static files related to dataset
"""
static_dir_path = os.path.join("data", dataset_name, "static")
def loads_file(fn):
return torch.load(
os.path.join(static_dir_path, fn), map_location=device
)
# Load border mask, 1. if node is part of border, else 0.
border_mask_np = np.load(os.path.join(static_dir_path, "border_mask.npy"))
border_mask = (
torch.tensor(border_mask_np, dtype=torch.float32, device=device)
.flatten(0, 1)
.unsqueeze(1)
) # (N_grid, 1)
grid_static_features = loads_file(
"grid_features.pt"
) # (N_grid, d_grid_static)
# Load step diff stats
step_diff_mean = loads_file("diff_mean.pt") # (d_f,)
step_diff_std = loads_file("diff_std.pt") # (d_f,)
# Load parameter std for computing validation errors in original data scale
data_mean = loads_file("parameter_mean.pt") # (d_features,)
data_std = loads_file("parameter_std.pt") # (d_features,)
# Load loss weighting vectors
param_weights = torch.tensor(
np.load(os.path.join(static_dir_path, "parameter_weights.npy")),
dtype=torch.float32,
device=device,
) # (d_f,)
return {
"border_mask": border_mask,
"grid_static_features": grid_static_features,
"step_diff_mean": step_diff_mean,
"step_diff_std": step_diff_std,
"data_mean": data_mean,
"data_std": data_std,
"param_weights": param_weights,
}
class BufferList(nn.Module):
"""
A list of torch buffer tensors that sit together as a Module with no
parameters and only buffers.
This should be replaced by a native torch BufferList once implemented.
See: https://github.com/pytorch/pytorch/issues/37386
"""
def __init__(self, buffer_tensors, persistent=True):
super().__init__()
self.n_buffers = len(buffer_tensors)
for buffer_i, tensor in enumerate(buffer_tensors):
self.register_buffer(f"b{buffer_i}", tensor, persistent=persistent)
def __getitem__(self, key):
return getattr(self, f"b{key}")
def __len__(self):
return self.n_buffers
def __iter__(self):
return (self[i] for i in range(len(self)))
def load_graph(graph_name, device="cpu"):
"""
Load all tensors representing the graph
"""
# Define helper lambda function
graph_dir_path = os.path.join("graphs", graph_name)
def loads_file(fn):
return torch.load(os.path.join(graph_dir_path, fn), map_location=device)
# Load edges (edge_index)
m2m_edge_index = BufferList(
loads_file("m2m_edge_index.pt"), persistent=False
) # List of (2, M_m2m[l])
g2m_edge_index = loads_file("g2m_edge_index.pt") # (2, M_g2m)
m2g_edge_index = loads_file("m2g_edge_index.pt") # (2, M_m2g)
n_levels = len(m2m_edge_index)
hierarchical = n_levels > 1 # Nor just single level mesh graph
# Load static edge features
m2m_features = loads_file("m2m_features.pt") # List of (M_m2m[l], d_edge_f)
g2m_features = loads_file("g2m_features.pt") # (M_g2m, d_edge_f)
m2g_features = loads_file("m2g_features.pt") # (M_m2g, d_edge_f)
# Normalize by dividing with longest edge (found in m2m)
longest_edge = max(
torch.max(level_features[:, 0]) for level_features in m2m_features
) # Col. 0 is length
m2m_features = BufferList(
[level_features / longest_edge for level_features in m2m_features],
persistent=False,
)
g2m_features = g2m_features / longest_edge
m2g_features = m2g_features / longest_edge
# Load static node features
mesh_static_features = loads_file(
"mesh_features.pt"
) # List of (N_mesh[l], d_mesh_static)
# Some checks for consistency
assert (
len(m2m_features) == n_levels
), "Inconsistent number of levels in mesh"
assert (
len(mesh_static_features) == n_levels
), "Inconsistent number of levels in mesh"
if hierarchical:
# Load up and down edges and features
mesh_up_edge_index = BufferList(
loads_file("mesh_up_edge_index.pt"), persistent=False
) # List of (2, M_up[l])
mesh_down_edge_index = BufferList(
loads_file("mesh_down_edge_index.pt"), persistent=False
) # List of (2, M_down[l])
mesh_up_features = loads_file(
"mesh_up_features.pt"
) # List of (M_up[l], d_edge_f)
mesh_down_features = loads_file(
"mesh_down_features.pt"
) # List of (M_down[l], d_edge_f)
# Rescale
mesh_up_features = BufferList(
[
edge_features / longest_edge
for edge_features in mesh_up_features
],
persistent=False,
)
mesh_down_features = BufferList(
[
edge_features / longest_edge
for edge_features in mesh_down_features
],
persistent=False,
)
mesh_static_features = BufferList(
mesh_static_features, persistent=False
)
else:
# Extract single mesh level
m2m_edge_index = m2m_edge_index[0]
m2m_features = m2m_features[0]
mesh_static_features = mesh_static_features[0]
(
mesh_up_edge_index,
mesh_down_edge_index,
mesh_up_features,
mesh_down_features,
) = ([], [], [], [])
return hierarchical, {
"g2m_edge_index": g2m_edge_index,
"m2g_edge_index": m2g_edge_index,
"m2m_edge_index": m2m_edge_index,
"mesh_up_edge_index": mesh_up_edge_index,
"mesh_down_edge_index": mesh_down_edge_index,
"g2m_features": g2m_features,
"m2g_features": m2g_features,
"m2m_features": m2m_features,
"mesh_up_features": mesh_up_features,
"mesh_down_features": mesh_down_features,
"mesh_static_features": mesh_static_features,
}
def make_mlp(blueprint, layer_norm=True):
"""
Create MLP from list blueprint, with
input dimensionality: blueprint[0]
output dimensionality: blueprint[-1] and
hidden layers of dimensions: blueprint[1], ..., blueprint[-2]
if layer_norm is True, includes a LayerNorm layer at
the output (as used in GraphCast)
"""
hidden_layers = len(blueprint) - 2
assert hidden_layers >= 0, "Invalid MLP blueprint"
layers = []
for layer_i, (dim1, dim2) in enumerate(zip(blueprint[:-1], blueprint[1:])):
layers.append(nn.Linear(dim1, dim2))
if layer_i != hidden_layers:
layers.append(nn.SiLU()) # Swish activation
# Optionally add layer norm to output
if layer_norm:
layers.append(nn.LayerNorm(blueprint[-1]))
return nn.Sequential(*layers)
def fractional_plot_bundle(fraction):
"""
Get the tueplots bundle, but with figure width as a fraction of
the page width.
"""
# If latex is not available, some visualizations might not render correctly,
# but will at least not raise an error.
# Alternatively, use unicode raised numbers.
usetex = True if shutil.which("latex") else False
bundle = bundles.neurips2023(usetex=usetex, family="serif")
bundle.update(figsizes.neurips2023())
original_figsize = bundle["figure.figsize"]
bundle["figure.figsize"] = (
original_figsize[0] / fraction,
original_figsize[1],
)
return bundle
def init_wandb_metrics(wandb_logger, val_steps):
"""
Set up wandb metrics to track
"""
experiment = wandb_logger.experiment
experiment.define_metric("val_mean_loss", summary="min")
for step in val_steps:
experiment.define_metric(f"val_loss_unroll{step}", summary="min")