-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
227 lines (192 loc) · 8.17 KB
/
test.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
import argparse
import os
import pickle
import time
import warnings
import torch
from rl4co.data.transforms import StateAugmentation
from rl4co.utils.ops import gather_by_index, unbatchify
from tqdm.auto import tqdm
from routefinder.data.utils import get_dataloader
from routefinder.envs import MTVRPEnv
from routefinder.models import RouteFinderBase, RouteFinderMoE
from routefinder.models.baselines.mtpomo import MTPOMO
from routefinder.models.baselines.mvmoe import MVMoE
# Tricks for faster inference
try:
torch._C._jit_set_profiling_executor(False)
torch._C._jit_set_profiling_mode(False)
except AttributeError:
pass
torch.set_float32_matmul_precision("medium")
def test(
policy,
td,
env,
num_augment=8,
augment_fn="dihedral8", # or symmetric. Default is dihedral8 for reported eval
num_starts=None,
device="cuda",
):
costs_bks = td.get("costs_bks", None)
with torch.inference_mode():
with (
torch.amp.autocast("cuda")
if "cuda" in str(device)
else torch.inference_mode()
): # Use mixed precision if supported
n_start = env.get_num_starts(td) if num_starts is None else num_starts
if num_augment > 1:
td = StateAugmentation(num_augment=num_augment, augment_fn=augment_fn)(td)
# Evaluate policy
out = policy(td, env, phase="test", num_starts=n_start, return_actions=True)
# Unbatchify reward to [batch_size, num_augment, num_starts].
reward = unbatchify(out["reward"], (num_augment, n_start))
if n_start > 1:
# max multi-start reward
max_reward, max_idxs = reward.max(dim=-1)
out.update({"max_reward": max_reward})
if out.get("actions", None) is not None:
# Reshape batch to [batch_size, num_augment, num_starts, ...]
actions = unbatchify(out["actions"], (num_augment, n_start))
out.update(
{
"best_multistart_actions": gather_by_index(
actions, max_idxs, dim=max_idxs.dim()
)
}
)
out["actions"] = actions
# Get augmentation score only during inference
if num_augment > 1:
# If multistart is enabled, we use the best multistart rewards
reward_ = max_reward if n_start > 1 else reward
max_aug_reward, max_idxs = reward_.max(dim=1)
out.update({"max_aug_reward": max_aug_reward})
# If costs_bks is available, we calculate the gap to BKS
if costs_bks is not None:
# note: torch.abs is here as a temporary fix, since we forgot to
# convert rewards to costs. Does not affect the results.
gap_to_bks = (
100
* (-max_aug_reward - torch.abs(costs_bks))
/ torch.abs(costs_bks)
)
out.update({"gap_to_bks": gap_to_bks})
if out.get("actions", None) is not None:
actions_ = (
out["best_multistart_actions"] if n_start > 1 else out["actions"]
)
out.update({"best_aug_actions": gather_by_index(actions_, max_idxs)})
if out.get("gap_to_bks", None) is None:
out.update({"gap_to_bks": 69420}) # Dummy value
return out
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--checkpoint", type=str, required=True, help="Path to the model checkpoint"
)
parser.add_argument(
"--problem",
type=str,
default="all",
help="Problem name: cvrp, vrptw, etc. or all",
)
parser.add_argument(
"--size",
type=int,
default=100,
help="Problem size: 50, 100, for automatic loading",
)
parser.add_argument(
"--datasets",
help="Filename of the dataset(s) to evaluate. Defaults to all under data/{problem}/ dir",
default=None,
)
parser.add_argument("--batch_size", type=int, default=1000)
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument(
"--remove-mixed-backhaul",
action=argparse.BooleanOptionalAction,
default=True,
help="Remove mixed backhaul instances. Use --no-remove-mixed-backhaul to keep them.",
)
parser.add_argument(
"--save-results",
action=argparse.BooleanOptionalAction,
default=True,
help="Save results to results/main/{size}/{checkpoint",
)
# Use load_from_checkpoint with map_location, which is handled internally by Lightning
# Suppress FutureWarnings related to torch.load and weights_only
warnings.filterwarnings("ignore", message=".*weights_only.*", category=FutureWarning)
opts = parser.parse_args()
if "cuda" in opts.device and torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
if opts.datasets is not None:
data_paths = opts.datasets.split(",")
else:
# list recursively all npz files in data/
data_paths = []
for root, _, files in os.walk("data"):
for file in files:
# print(file)
if "test" not in root:
continue
if file.endswith(".npz"):
if opts.remove_mixed_backhaul and "m" in root:
continue
# if name in 50 or 100, append
if str(opts.size) in file:
if file == "50.npz" or file == "100.npz":
data_paths.append(os.path.join(root, file))
assert len(data_paths) > 0, "No datasets found. Check the data directory."
data_paths = sorted(sorted(data_paths), key=lambda x: len(x))
print(f"Found {len(data_paths)} datasets on the following paths: {data_paths}")
# Load model
print("Loading checkpoint from ", opts.checkpoint)
if "mvmoe" in opts.checkpoint:
BaseLitModule = MVMoE
elif "mtpomo" in opts.checkpoint:
BaseLitModule = MTPOMO
elif "moe" in opts.checkpoint:
BaseLitModule = RouteFinderMoE
else:
BaseLitModule = RouteFinderBase
model = BaseLitModule.load_from_checkpoint(
opts.checkpoint, map_location="cpu", strict=False
)
env = MTVRPEnv()
policy = model.policy.to(device).eval() # Use mixed precision if supported
results = {}
for dataset in tqdm(data_paths):
print(f"Loading {dataset}")
td_test = env.load_data(dataset) # this also adds the bks cost
dataloader = get_dataloader(td_test, batch_size=opts.batch_size)
start = time.time()
res = []
for batch in dataloader:
td_test = env.reset(batch).to(device)
o = test(policy, td_test, env, device=device)
res.append(o)
out = {}
out["max_aug_reward"] = torch.cat([o["max_aug_reward"] for o in res])
out["gap_to_bks"] = torch.cat([o["gap_to_bks"] for o in res])
inference_time = time.time() - start
dataset_name = dataset.split("/")[-3].split(".")[0].upper()
print(
f"{dataset_name} | Cost: {-out['max_aug_reward'].mean().item():.3f} | Gap: {out['gap_to_bks'].mean().item():.3f}% | Inference time: {inference_time:.3f} s"
)
if results.get(dataset_name, None) is None:
results[dataset_name] = {}
results[dataset_name]["cost"] = -out["max_aug_reward"].mean().item()
results[dataset_name]["gap"] = out["gap_to_bks"].mean().item()
results[dataset_name]["inference_time"] = inference_time
if opts.save_results:
# Save results with checkpoint name under results/main/
checkpoint_name = opts.checkpoint.split("/")[-1].split(".")[0]
savedir = f"results/main/{opts.size}/"
os.makedirs(savedir, exist_ok=True)
pickle.dump(results, open(savedir + checkpoint_name + ".pkl", "wb"))