-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.py
More file actions
391 lines (330 loc) · 10.3 KB
/
Copy pathgenerate.py
File metadata and controls
391 lines (330 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""
@ 2025 MetaDiT project
Generate new materials from MetaDiT, supports multi-GPU
"""
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import numpy as np
import time
from torch.utils.data import DataLoader
from datetime import timedelta
from loggers import WrappedLogger
from argparse import ArgumentParser
from utils import save_json
from model.dit import DIT_MODEL, DiT
from diffusion import create_diffusion, SpacedDiffusion
from datapipe import FreeFormDataset
from tqdm import tqdm
logger = WrappedLogger(__name__)
def setup_distributed(rank: int, world_size: int):
"""Initializes the distributed process group."""
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355' # An unused port
# Initialize the process group
# To avoid NCCL timeout
TIMEOUT = timedelta(hours=24)
dist.init_process_group(
"nccl", rank=rank, world_size=world_size,
device_id=torch.device(f"cuda:{rank}"),
timeout=TIMEOUT
)
# Pin the current process to a specific GPU
torch.cuda.set_device(rank)
def cleanup_distributed():
"""Cleans up the distributed process group."""
dist.destroy_process_group()
torch.cuda.empty_cache()
def split_data(data: list[dict], world_size: int):
"""
Splits a list of data into 'world_size' chunks, handling uneven splits.
Uses numpy.array_split for a simple and robust split.
"""
# np.array_split will create as-equal-as-possible chunks.
# It returns a list of numpy arrays, so we convert them back to lists.
return [arr.tolist() for arr in np.array_split(data, world_size)]
def build_model(
model_path: str,
model_type: str,
time_steps: int,
condition_channel: int
) -> tuple[DiT, SpacedDiffusion]:
diffusion = create_diffusion(str(time_steps), learn_sigma=False)
model = DIT_MODEL[model_type](
diffusion=diffusion, condition_channel=condition_channel
)
state_dict = torch.load(model_path)
model.load_state_dict(state_dict, strict=True)
print("Model loaded on CPU")
return model, diffusion
def prepare_data(data_path: str, batch_size: int) -> list[dict]:
dataset = FreeFormDataset(data_path)
loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
drop_last=False
)
return [batch for batch in loader]
def generate_one_batch(
payload: dict,
sampling_params: dict,
model: DiT,
diffusion: SpacedDiffusion,
device: str
) -> list[dict]:
resolution = sampling_params["resolution"]
cfg_scale = sampling_params["cfg_scale"]
condition = payload["condition"].to(device)
z = torch.randn(
condition.shape[0],
3,
resolution,
resolution,
device=device
)
z = torch.cat([z, z], 0) # half for classifier, half for classifier free
null_condition = torch.ones_like(condition) * 0.5
condition = torch.cat([condition, null_condition], 0)
model_kwargs = dict(y=condition, cfg_scale=cfg_scale)
samples = diffusion.p_sample_loop(
model.forward_with_cfg,
z.shape,
z,
clip_denoised=False,
model_kwargs=model_kwargs,
progress=True,
device=device
)
samples, _ = samples.chunk(2, dim=0) # Remove null class samples
batch_result = [
{
"condition": payload["condition"][j].tolist(),
"reference": payload["inputs"][j].tolist(),
"generation": sample.round(decimals=2).cpu().tolist()
}
for j, sample in enumerate(samples)
]
return batch_result
def single_gpu_inference(
model: DiT,
data: list[dict],
sampling_params: dict,
diffusion: SpacedDiffusion,
gpu_id: int = 0
):
"""
Runs inference on a single GPU.
"""
device = f"cuda:{gpu_id}"
print(f"--- Running single-GPU inference on {device} ---")
model.to(device)
model.eval()
all_results = []
def generate_and_store(payload: dict, storage: list):
batch_result = generate_one_batch(
payload, sampling_params, model,
diffusion, device
)
storage.extend(batch_result)
with torch.no_grad():
for item in tqdm(data, desc="Single-GPU Inference"):
generate_and_store(item, all_results)
return all_results
def worker_fn(
rank: int,
world_size,
data_path,
model_path,
model_type,
time_steps,
condition_channel,
sampling_params,
temp_dir,
seed=0
):
"""
The main function for each parallel worker process.
"""
print(f"[Rank {rank}] Worker process started.")
setup_distributed(rank, world_size)
torch.manual_seed(seed)
model, diffusion = build_model(
model_path, model_type, time_steps, condition_channel
)
device = f"cuda:{rank}"
model.to(device)
model.eval()
# Get this rank's chunk of data
data = prepare_data(data_path, sampling_params["batch_size"])
data_chunks = split_data(data, world_size)
local_data = data_chunks[rank]
print(f"[Rank {rank}] local data size {len(local_data)}")
dist.barrier()
local_results = []
iterable = tqdm(
local_data,
desc=f"Rank {rank} Inference",
position=rank,
leave=True
)
def generate_and_store(payload: dict, storage: list):
batch_result = generate_one_batch(
payload, sampling_params, model,
diffusion, device
)
storage.extend(batch_result)
with torch.no_grad():
for item in iterable:
generate_and_store(item, local_results)
iterable.close()
# Save this rank's results to a temporary file
temp_file = os.path.join(temp_dir, f"results_rank_{rank}.pt")
torch.save(local_results, temp_file)
print(f"[Rank {rank}] Results saved to {temp_file}")
dist.barrier()
cleanup_distributed()
print(f"[Rank {rank}] Worker process finished.")
def main(args):
assert torch.cuda.is_available(), "Inference supports GPU only!"
available_gpus = torch.cuda.device_count()
if available_gpus == 0:
raise ValueError("Error: No GPUs available.")
if args.num_gpus > available_gpus:
print(f"Warning: Requested {args.num_gpus} GPUs, but only {available_gpus} are available.")
args.num_gpus = available_gpus
torch.manual_seed(args.seed)
torch.set_grad_enabled(False)
model, diffusion = None, None
if args.num_gpus == 1:
print("Loading model onto CPU...")
model, diffusion = build_model(
args.model_path, args.model_type, args.condition_channel
)
data = prepare_data(args.data_path, args.batch_size)
print(f"Loaded data size {len(data)}")
# In multi-GPU inference, we create model, dataset inside the worker,
# to avoid shm overflow
all_results = []
sampling_params = {
"resolution": args.resolution,
"cfg_scale": args.cfg_scale,
"batch_size": args.batch_size
}
start_time = time.time()
if args.num_gpus == 1:
all_results = single_gpu_inference(
model, data, sampling_params, diffusion, gpu_id=0
)
else:
print(f"--- Spawning {args.num_gpus} processes for parallel inference ---")
world_size = args.num_gpus
os.makedirs(args.temp_dir, exist_ok=True)
worker_args = (
world_size, args.data_path, args.model_path, args.model_type,
args.time_steps, args.condition_channel, sampling_params,
args.temp_dir, args.seed
)
mp.spawn(
worker_fn,
args=worker_args,
nprocs=world_size,
join=True
)
print("All workers finished. Gathering results...")
all_results_split = []
for i in range(world_size):
temp_file = os.path.join(args.temp_dir, f"results_rank_{i}.pt")
try:
chunk = torch.load(temp_file)
all_results_split.append(chunk)
os.remove(temp_file) # Clean up
except FileNotFoundError:
print(f"Error: Could not find temp file {temp_file}")
all_results = [item for sublist in all_results_split for item in sublist]
# Clean up temp directory
try:
os.rmdir(args.temp_dir)
except OSError:
pass # Directory might not be empty, which is fine.
end_time = time.time()
# --- Done ---
print("\n--- Inference Complete ---")
print(f"Total results gathered: {len(all_results)}")
print(f"Total time taken: {end_time - start_time:.2f} seconds")
parent = os.path.dirname(args.save_path)
if not os.path.exists(parent):
os.makedirs(parent)
save_json(args.save_path, all_results)
exit(0)
if __name__ == "__main__":
parser = ArgumentParser(description="Inference script for MetaDiT")
# Infra settings
parser.add_argument(
"--num_gpus",
type=int,
default=1,
help="Number of GPUs to use for inference. Defaults to 1."
)
parser.add_argument(
"--temp_dir",
type=str,
default="cache/inference"
)
# data information
parser.add_argument(
"--data_path",
type=str,
default=None
)
# Model information
parser.add_argument(
"--model_path",
type=str,
default=None
)
parser.add_argument(
"--model_type",
type=str,
default="metadit_s"
)
parser.add_argument(
"--condition_channel",
type=int,
default=301
)
# Sampling Params
parser.add_argument(
"--seed",
type=int,
default=0
)
parser.add_argument(
"--resolution",
type=int,
default=32
)
parser.add_argument(
"--cfg_scale",
type=float,
default=4.0
)
parser.add_argument(
"--batch_size",
type=int,
default=256
)
parser.add_argument(
"--time_steps",
type=int,
default=500
)
# Save
parser.add_argument(
"--save_path",
type=str,
default="test/results.json"
)
args = parser.parse_args()
main(args)