forked from yilundu/ired_code_release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_inference.py
More file actions
350 lines (245 loc) · 10.3 KB
/
Copy pathrun_inference.py
File metadata and controls
350 lines (245 loc) · 10.3 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
Simple inference script - just run models with random data.
No profiling, no timing - you can add that yourself.
"""
import torch
import torch_xla
import torch_xla.debug.profiler as xp
import argparse
from models import (
EBM, DiffusionWrapper,
SudokuEBM, SudokuTransformerEBM, SudokuDenoise, SudokuLatentEBM,
GraphEBM, GraphReverse,
GNNConvEBM, GNNDiffusionWrapper, GNNConvDiffusionWrapper,
GNNConv1DEBMV2, GNNConv1DV2DiffusionWrapper, GNNConv1DReverse
)
def run_mlp(batch_size=64):
"""MLP model for continuous tasks (addition, inverse, lowrank)"""
device = torch_xla.device()
# 20x20 matrix = 400 dim
inp_dim = 400
out_dim = 400
model = EBM(inp_dim, out_dim, is_ebm=True).to(device)
model = DiffusionWrapper(model).to(device)
model.eval()
# Random inputs (DiffusionWrapper needs gradients)
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
# Forward pass
xp.start_trace("./traces/mlp_forward")
output = model(inp, out, t)
xp.stop_trace()
print(f"MLP: input={inp.shape}, output={output.shape}")
return output
def run_mlp_reverse(batch_size=64):
"""MLP reverse (denoising) model"""
device = torch_xla.device()
inp_dim = 400
out_dim = 400
model = EBM(inp_dim, out_dim, is_ebm=False).to(device)
model.eval()
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
# No wrapper, can use no_grad
with torch.no_grad():
output = model(inp, out, t)
print(f"MLP-Reverse: input={inp.shape}, output={output.shape}")
return output
def run_sudoku(batch_size=64):
"""Sudoku model"""
device = torch_xla.device()
# 9x9 grid with 9 channels = 729
inp_dim = 729
out_dim = 729
model = SudokuEBM(inp_dim, out_dim).to(device)
model = DiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"Sudoku: input={inp.shape}, output={output.shape}")
return output
def run_sudoku_latent(batch_size=64):
"""Sudoku latent model"""
device = torch_xla.device()
inp_dim = 729
out_dim = 243 # Latent representation: 9x9x3 = 243
model = SudokuLatentEBM(inp_dim, out_dim).to(device)
model = DiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"Sudoku-Latent: input={inp.shape}, output={output.shape}")
return output
def run_sudoku_transformer(batch_size=64):
"""Sudoku transformer model"""
device = torch_xla.device()
inp_dim = 729
out_dim = 729
model = SudokuTransformerEBM(inp_dim, out_dim).to(device)
model = DiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"Sudoku-Transformer: input={inp.shape}, output={output.shape}")
return output
def run_sudoku_reverse(batch_size=64):
"""Sudoku reverse (denoising) model"""
device = torch_xla.device()
inp_dim = 729
out_dim = 729
model = SudokuDenoise(inp_dim, out_dim).to(device)
model.eval()
inp = torch.randn(batch_size, inp_dim, device=device)
out = torch.randn(batch_size, out_dim, device=device)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
# No wrapper, can use no_grad
with torch.no_grad():
output = model(inp, out, t)
print(f"Sudoku-Reverse: input={inp.shape}, output={output.shape}")
return output
def run_gnn(batch_size=64):
"""GNN model for graph connectivity"""
device = torch_xla.device()
# 12x12 graph with 4 features per edge
n_nodes = 12
inp_dim = 4
out_dim = 1
model = GraphEBM(inp_dim, out_dim).to(device)
model = GNNDiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, n_nodes, n_nodes, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"GNN: input={inp.shape}, output={output.shape}")
return output
def run_gnn_reverse(batch_size=64):
"""GNN reverse model"""
device = torch_xla.device()
n_nodes = 12
inp_dim = 4
out_dim = 1
model = GraphReverse(inp_dim, out_dim).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, n_nodes, n_nodes, device=device)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
# No wrapper, can use no_grad
with torch.no_grad():
output = model(inp, out, t)
print(f"GNN-Reverse: input={inp.shape}, output={output.shape}")
return output
def run_gnn_conv(batch_size=64):
"""GNN conv model"""
device = torch_xla.device()
n_nodes = 12
inp_dim = 4
out_dim = 1
seq_len = 16
model = GNNConvEBM(inp_dim, out_dim, use_1d=False).to(device)
model = GNNConvDiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, seq_len, n_nodes, n_nodes, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"GNN-Conv: input={inp.shape}, output={output.shape}")
return output
def run_gnn_conv_1d(batch_size=64):
"""GNN conv 1D model"""
device = torch_xla.device()
n_nodes = 12
inp_dim = 2
out_dim = 1
seq_len = 16
model = GNNConvEBM(inp_dim, out_dim, use_1d=True).to(device)
model = GNNConvDiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, seq_len, n_nodes, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"GNN-Conv-1D: input={inp.shape}, output={output.shape}")
return output
def run_gnn_conv_1d_v2(batch_size=64):
"""GNN conv 1D v2 model (for planning)"""
device = torch_xla.device()
n_nodes = 20
inp_dim = 4
out_dim = 1
seq_len = 8
model = GNNConv1DEBMV2(inp_dim, out_dim).to(device)
model = GNNConv1DV2DiffusionWrapper(model).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, seq_len, n_nodes, out_dim, device=device, requires_grad=True)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
output = model(inp, out, t)
print(f"GNN-Conv-1D-V2: input={inp.shape}, output={output.shape}")
return output
def run_gnn_conv_1d_v2_reverse(batch_size=64):
"""GNN conv 1D v2 reverse model"""
device = torch_xla.device()
n_nodes = 20
inp_dim = 4
out_dim = 1
seq_len = 8
model = GNNConv1DReverse(inp_dim, out_dim).to(device)
model.eval()
inp = torch.randn(batch_size, n_nodes, n_nodes, inp_dim, device=device)
out = torch.randn(batch_size, seq_len, n_nodes, out_dim, device=device)
t = torch.zeros(batch_size, dtype=torch.long, device=device)
# No wrapper, can use no_grad
with torch.no_grad():
output = model(inp, out, t)
print(f"GNN-Conv-1D-V2-Reverse: input={inp.shape}, output={output.shape}")
return output
def main():
parser = argparse.ArgumentParser(description='Run IRED model inference with random data')
parser.add_argument('--model', type=str, default='mlp',
choices=['mlp', 'mlp-reverse', 'sudoku', 'sudoku-latent',
'sudoku-transformer', 'sudoku-reverse', 'gnn', 'gnn-reverse',
'gnn-conv', 'gnn-conv-1d', 'gnn-conv-1d-v2',
'gnn-conv-1d-v2-reverse', 'all'],
help='Which model to run')
parser.add_argument('--batch-size', type=int, default=64, help='Batch size')
args = parser.parse_args()
device = torch_xla.device()
print(f"Device: {device}")
print(f"Batch size: {args.batch_size}\n")
if args.model == 'all' or args.model == 'mlp':
run_mlp(args.batch_size)
if args.model == 'all' or args.model == 'mlp-reverse':
run_mlp_reverse(args.batch_size)
if args.model == 'all' or args.model == 'sudoku':
run_sudoku(args.batch_size)
if args.model == 'all' or args.model == 'sudoku-latent':
run_sudoku_latent(args.batch_size)
if args.model == 'all' or args.model == 'sudoku-transformer':
run_sudoku_transformer(args.batch_size)
if args.model == 'all' or args.model == 'sudoku-reverse':
run_sudoku_reverse(args.batch_size)
if args.model == 'all' or args.model == 'gnn':
run_gnn(args.batch_size)
if args.model == 'all' or args.model == 'gnn-reverse':
run_gnn_reverse(args.batch_size)
if args.model == 'all' or args.model == 'gnn-conv':
run_gnn_conv(args.batch_size)
if args.model == 'all' or args.model == 'gnn-conv-1d':
run_gnn_conv_1d(args.batch_size)
if args.model == 'all' or args.model == 'gnn-conv-1d-v2':
run_gnn_conv_1d_v2(args.batch_size)
if args.model == 'all' or args.model == 'gnn-conv-1d-v2-reverse':
run_gnn_conv_1d_v2_reverse(args.batch_size)
print("\nDone!")
if __name__ == '__main__':
server = xp.start_server(9012)
main()