forked from mllam/neural-lam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_parameter_weights.py
208 lines (177 loc) · 6.85 KB
/
create_parameter_weights.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
# Standard library
import os
import subprocess
from argparse import ArgumentParser
# Third-party
import numpy as np
import torch
import torch.distributed as dist
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm
# First-party
from neural_lam import constants
from neural_lam.weather_dataset import WeatherDataModule
def get_rank():
"""Get the rank of the current process in the distributed group."""
return int(os.environ["SLURM_PROCID"])
def get_world_size():
"""Get the number of processes in the distributed group."""
return int(os.environ["SLURM_NTASKS"])
def setup(rank, world_size): # pylint: disable=redefined-outer-name
"""Initialize the distributed group."""
try:
master_node = (
subprocess.check_output(
"scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1",
shell=True,
)
.strip()
.decode("utf-8")
)
except Exception as e:
print(f"Error getting master node IP: {e}")
raise
master_port = "12355"
os.environ["MASTER_ADDR"] = master_node
os.environ["MASTER_PORT"] = master_port
if torch.cuda.is_available():
dist.init_process_group("nccl", rank=rank, world_size=world_size)
else:
dist.init_process_group("gloo", rank=rank, world_size=world_size)
def cleanup():
"""Destroy the distributed group."""
dist.destroy_process_group()
def main(rank, world_size): # pylint: disable=redefined-outer-name
"""Compute the mean and standard deviation of the input data."""
setup(rank, world_size)
parser = ArgumentParser(description="Training arguments")
parser.add_argument("--dataset", type=str, default="meps_example")
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--subset", type=int, default=8760)
parser.add_argument("--n_workers", type=int, default=4)
args = parser.parse_args()
if args.subset % (world_size * args.batch_size) != 0:
raise ValueError(
"Subset size must be divisible by (world_size * batch_size)"
)
device = torch.device(
f"cuda:{rank % torch.cuda.device_count()}"
if torch.cuda.is_available()
else "cpu"
)
static_dir_path = os.path.join("data", args.dataset, "static")
data_module = WeatherDataModule(
dataset_name=args.dataset,
standardize=False,
subset=args.subset,
batch_size=args.batch_size,
num_workers=args.n_workers,
)
data_module.setup(stage="fit")
train_sampler = DistributedSampler(
data_module.train_dataset, num_replicas=world_size, rank=rank
)
train_loader = torch.utils.data.DataLoader(
data_module.train_dataset,
batch_size=args.batch_size,
sampler=train_sampler,
num_workers=args.n_workers,
)
if rank == 0:
w_list = [
pw * lw
for var_name, pw in zip(
constants.PARAM_NAMES_SHORT, constants.PARAM_WEIGHTS.values()
)
for lw in (
constants.LEVEL_WEIGHTS.values()
if constants.IS_3D[var_name]
else [1]
)
]
np.save(
os.path.join(static_dir_path, "parameter_weights.npy"),
np.array(w_list, dtype="float32"),
)
means = []
squares = []
flux_means = []
flux_squares = []
for init_batch, target_batch, _, forcing_batch in tqdm(
train_loader, disable=rank != 0
):
batch = torch.cat((init_batch, target_batch), dim=1).to(device)
means.append(torch.mean(batch, dim=(1, 2)))
squares.append(torch.mean(batch**2, dim=(1, 2)))
if constants.GRID_FORCING_DIM > 0:
flux_batch = forcing_batch[:, :, :, 1].to(device)
flux_means.append(torch.mean(flux_batch))
flux_squares.append(torch.mean(flux_batch**2))
dist.barrier()
means_gathered = [None] * world_size
squares_gathered = [None] * world_size
dist.all_gather_object(means_gathered, torch.cat(means, dim=0))
dist.all_gather_object(squares_gathered, torch.cat(squares, dim=0))
if rank == 0:
means_all = torch.cat(means_gathered, dim=0)
squares_all = torch.cat(squares_gathered, dim=0)
mean = torch.mean(means_all, dim=0)
second_moment = torch.mean(squares_all, dim=0)
std = torch.sqrt(second_moment - mean**2)
torch.save(mean, os.path.join(static_dir_path, "parameter_mean.pt"))
torch.save(std, os.path.join(static_dir_path, "parameter_std.pt"))
if constants.GRID_FORCING_DIM > 0:
flux_means_all = torch.stack(flux_means)
flux_squares_all = torch.stack(flux_squares)
flux_mean = torch.mean(flux_means_all)
flux_second_moment = torch.mean(flux_squares_all)
flux_std = torch.sqrt(flux_second_moment - flux_mean**2)
torch.save(
{"mean": flux_mean, "std": flux_std},
os.path.join(static_dir_path, "flux_stats.pt"),
)
data_module = WeatherDataModule(
dataset_name=args.dataset,
standardize=True,
subset=args.subset,
batch_size=args.batch_size,
num_workers=args.n_workers,
)
data_module.setup(stage="fit")
train_sampler = DistributedSampler(
data_module.train_dataset, num_replicas=world_size, rank=rank
)
train_loader = torch.utils.data.DataLoader(
data_module.train_dataset,
batch_size=args.batch_size,
sampler=train_sampler,
num_workers=args.n_workers,
)
# Compute mean and std-dev of one-step differences
diff_means = []
diff_squares = []
for init_batch, target_batch, _, _ in tqdm(train_loader, disable=rank != 0):
batch = torch.cat((init_batch, target_batch), dim=1).to(device)
diffs = batch[:, 1:] - batch[:, :-1]
diff_means.append(torch.mean(diffs, dim=(1, 2)))
diff_squares.append(torch.mean(diffs**2, dim=(1, 2)))
dist.barrier()
diff_means_gathered = [None] * world_size
diff_squares_gathered = [None] * world_size
dist.all_gather_object(diff_means_gathered, torch.cat(diff_means, dim=0))
dist.all_gather_object(
diff_squares_gathered, torch.cat(diff_squares, dim=0)
)
if rank == 0:
diff_means_all = torch.cat(diff_means_gathered, dim=0)
diff_squares_all = torch.cat(diff_squares_gathered, dim=0)
diff_mean = torch.mean(diff_means_all, dim=0)
diff_second_moment = torch.mean(diff_squares_all, dim=0)
diff_std = torch.sqrt(diff_second_moment - diff_mean**2)
torch.save(diff_mean, os.path.join(static_dir_path, "diff_mean.pt"))
torch.save(diff_std, os.path.join(static_dir_path, "diff_std.pt"))
cleanup()
if __name__ == "__main__":
rank = get_rank()
world_size = get_world_size()
main(rank, world_size)