Safetensors
File size: 27,080 Bytes
522bf24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
"""
Unified Inference Script for Multi-Modal Image Generation and Editing

Supports three modes:
1. t2i (Text-to-Image): Generate images from text prompts (txt file)
2. geneval: Generate multiple samples per prompt for evaluation (jsonl file)
3. edit: Edit images based on prompts (parquet file)

Example usage:
    # Text-to-Image
    python unified_inference.py --mode t2i --model_path ./model --model_type flux \
        --prompt_file prompts.txt --output_dir outputs/t2i

    # GenEval
    python unified_inference.py --mode geneval --model_path ./model --model_type flux \
        --metadata_file evaluation_metadata.jsonl --output_dir outputs/geneval --n_samples 4

    # Image Editing
    python unified_inference.py --mode edit --model_path ./model --model_type kontext \
        --data_file data.parquet --output_dir outputs/edit
"""

from concurrent.futures import ThreadPoolExecutor, as_completed
import argparse
import json
import os
import traceback
from tqdm import tqdm
import torch
import numpy as np
from PIL import Image
from transformers import AutoProcessor
import random
import multiprocessing as mp
import pandas as pd
from io import BytesIO
import base64
from torchvision import transforms as TF

# Model imports
from unimodel.qwenflux.qwenflux_inference import QwenFluxForInferenceLM
from unimodel.qwenkontext.qwenkontext_inference import QwenKontextForInferenceLM

# Global configuration
NUM_DEVICE = 8
NUM_PROCESSES = 8


# =============================================================================
# CoT Prompt Templates
# =============================================================================
COT_PROMPT_TEMPLATES = {
    # General enhancement
    "geneval": """Please provide an enhanced prompt for the following image generation prompt to make the image more realistic, detailed, with clear separation and precise alignment of all entities.
Original prompt: {original_prompt}. Directly provide the improved prompt in <answer> </answer> tags.""",


    "ocr_clarity_v2": """Please enhance the following image generation prompt with specific focus on TEXT clarity and readability.
Original prompt: {original_prompt}. Directly provide the improved prompt in <answer> </answer> tags.""",


    "quality_purev2": """Rewrite the following image generation prompt to improve its visual quality, detail level, realism, and artistic sophistication. 

Original prompt: {original_prompt}

Directly provide the enhanced version directly in <answer></answer> tags.""",


    "edit_general": """Please provide an enhanced prompt for the following image editing prompt. 
Ensure the revised prompt is clear, specific, and includes detailed instructions to achieve the desired outcome while maintaining the original intent. 
Original prompt: {original_prompt}. Directly provide the improved prompt in <answer> </answer> tags.""",

}


# =============================================================================
# Utility Functions
# =============================================================================
def set_global_seed(seed):
    """Set global random seed for reproducibility."""
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)




# =============================================================================
# Model Loading
# =============================================================================
def load_model_pipeline(model_path, model_type, device):
    """Load model pipeline based on model type."""
    processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct")
    subfolder = model_path.split('/')[-1]
    model_path = model_path.replace(f"/{subfolder}", "")
    if model_type == "flux":
        model = QwenFluxForInferenceLM.from_pretrained(
            model_path, torch_dtype=torch.bfloat16, subfolder=subfolder
        )
    elif model_type == "sana":
        model = QwenSanaForInferenceLM.from_pretrained(
            model_path, torch_dtype=torch.bfloat16, subfolder=subfolder
        )
    elif model_type == "sd3":
        model = QwenSD3ForInferenceLM.from_pretrained(
            model_path, torch_dtype=torch.bfloat16, subfolder=subfolder
        )
    elif model_type == "kontext":
        model = QwenKontextForInferenceLM.from_pretrained(
            model_path, torch_dtype=torch.bfloat16, subfolder=subfolder
        )
    else:
        raise ValueError(f"Unknown model type: {model_type}")
    
    processor.tokenizer.padding_side = "left"  # for batch inference
    model.to(device)
    
    return model, processor


# =============================================================================
# Data Loading Functions
# =============================================================================
def load_prompts_from_txt(txt_file):
    """Load prompts from text file (one per line)."""
    with open(txt_file, 'r', encoding='utf-8') as f:
        prompts = [line.strip() for line in f if line.strip()]
    return prompts


def load_prompts_from_jsonl(metadata_file):
    """Load prompts and metadata from JSONL file."""
    with open(metadata_file) as fp:
        metadatas = [json.loads(line) for line in fp]
    prompts = [metadata['prompt'].strip() for metadata in metadatas]
    return prompts, metadatas


def load_data_from_parquet(parquet_file):
    """Load images and prompts from parquet file."""
    df = pd.read_parquet(parquet_file)
    
    # Identify column names
    image_col = None
    prompt_col = None
    id_col = None
    
    for col in df.columns:
        col_lower = col.lower()
        if 'image' in col_lower and image_col is None:
            image_col = col
        elif any(kw in col_lower for kw in ['prompt', 'text', 'caption', 'instruction']) and prompt_col is None:
            prompt_col = col
        elif any(kw in col_lower for kw in ['id', 'index']) and id_col is None:
            id_col = col
    
    if image_col is None or prompt_col is None:
        raise ValueError(
            f"Cannot identify columns. Found: {df.columns.tolist()}\n"
            f"Expected 'image' and 'prompt'/'text'/'caption'"
        )
    
    print(f"Using columns - Image: '{image_col}', Prompt: '{prompt_col}', ID: '{id_col}'")
    
    data_list = []
    for idx, row in tqdm(df.iterrows(), total=len(df), desc="Loading parquet"):
        try:
            image_data = row[image_col]["bytes"]
            
            if isinstance(image_data, bytes):
                image = Image.open(BytesIO(image_data)).convert('RGB')
            elif isinstance(image_data, str):
                if image_data.startswith('data:image') or image_data.startswith('/9j/') or image_data.startswith('iVBOR'):
                    if 'base64,' in image_data:
                        image_data = image_data.split('base64,')[1]
                    image_bytes = base64.b64decode(image_data)
                    image = Image.open(BytesIO(image_bytes)).convert('RGB')
                else:
                    image = Image.open(image_data).convert('RGB')
            else:
                print(f"Warning: Skipping row {idx} - unsupported image format")
                continue
            
            prompt = str(row[prompt_col])
            item_id = row[id_col] if id_col else idx
            
            data_list.append({
                'image': image,
                'prompt': prompt,
                'id': item_id,
                'index': idx
            })
        except Exception as e:
            print(f"Error loading row {idx}: {e}")
            continue
    
    print(f"Loaded {len(data_list)} samples from parquet")
    return data_list


# =============================================================================
# Image Grid Utility
# =============================================================================
def create_image_grid(images, rows, cols):
    """Create a grid image from a list of images."""
    assert len(images) == rows * cols
    width, height = images[0].size
    grid_width = width * cols
    grid_height = height * rows
    grid_image = Image.new('RGB', (grid_width, grid_height))
    for i, image in enumerate(images):
        x = (i % cols) * width
        y = (i // cols) * height
        grid_image.paste(image, (x, y))
    return grid_image


# =============================================================================
# Generation Functions
# =============================================================================
def generate_t2i_batch(
    prompts, start_idx, pipeline, processor, output_dir, batch_size,
    guidance_scale, num_inference_steps, seed, use_cot, cot_template_name,
    add_instruction, device_id
):
    """Generate images from text prompts (T2I mode)."""
    os.makedirs(output_dir, exist_ok=True)
    
    for i in tqdm(range(0, len(prompts), batch_size), desc=f"GPU {device_id} T2I"):
        batch_prompts = prompts[i:i + batch_size]
        batch_start_idx = start_idx + i
        original_prompts = batch_prompts.copy()
        
        if add_instruction:
            batch_prompts = [
                f"Please generate image based on the following caption: {p}"
                for p in batch_prompts
            ]
        
        diffusion_kwargs = dict(
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            num_images_per_prompt=1,
            generator=torch.Generator("cpu").manual_seed(seed)
        )
        
        try:
            with torch.no_grad():
                if use_cot:
                    llm_kwargs = dict(
                        max_new_tokens=256, temperature=0.7, top_p=0.9,
                        do_sample=False, num_return_sequences=1
                    )
                    cot_template = COT_PROMPT_TEMPLATES.get(cot_template_name)
                    outputs = pipeline.generate_image_cot(
                        texts=batch_prompts,
                        diffusion_kwargs=diffusion_kwargs,
                        processor=processor,
                        llm_kwargs=llm_kwargs,
                        cot_prompt_template=cot_template
                    )
                    images = outputs["images"]
                    thinking_prompts = outputs.get("improved_prompts", [])
                else:
                    images = pipeline.generate_image(
                        texts=batch_prompts,
                        diffusion_kwargs=diffusion_kwargs
                    )
                    thinking_prompts = []
            
            for j, img in enumerate(images):
                img_idx = batch_start_idx + j
                base_name = f"{img_idx:05d}"
                
                img.save(os.path.join(output_dir, f"{base_name}.png"))
                
                with open(os.path.join(output_dir, f"{base_name}_caption.txt"), 'w', encoding='utf-8') as f:
                    f.write(original_prompts[j])
                
                if use_cot and j < len(thinking_prompts):
                    with open(os.path.join(output_dir, f"{base_name}_thinking.txt"), 'w', encoding='utf-8') as f:
                        f.write(thinking_prompts[j])
                        
        except Exception as e:
            print(f"Error at batch {batch_start_idx}: {e}")
            traceback.print_exc()


def generate_geneval_batch(
    prompts, metadatas, start_idx, pipeline, processor, output_dir, batch_size,
    guidance_scale, num_inference_steps, seed, n_samples, use_cot,
    cot_template_name, skip_grid, device_id
):
    """Generate multiple samples per prompt for evaluation (GenEval mode)."""
    for prompt_idx, (prompt, metadata) in enumerate(zip(prompts, metadatas)):
        global_idx = start_idx + prompt_idx
        outpath = os.path.join(output_dir, f"{device_id}_{prompt_idx:0>5}")
        os.makedirs(outpath, exist_ok=True)
        sample_path = os.path.join(outpath, "samples")
        os.makedirs(sample_path, exist_ok=True)
        
        with open(os.path.join(outpath, "metadata.jsonl"), "w") as fp:
            json.dump(metadata, fp)
        
        sample_count = 0
        all_samples = []
        enhanced_prompts = []
        total_batches = (n_samples + batch_size - 1) // batch_size
        
        for batch_idx in tqdm(range(total_batches), desc=f"GPU {device_id} prompt {prompt_idx}"):
            num_images = min(batch_size, n_samples - sample_count)
            
            diffusion_kwargs = dict(
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                num_images_per_prompt=num_images,
                generator=torch.Generator("cpu").manual_seed(seed)
            )
            
            try:
                with torch.inference_mode():
                    if use_cot:
                        llm_kwargs = dict(
                            max_new_tokens=256, temperature=0.7, top_p=0.9,
                            do_sample=False, num_return_sequences=1
                        )
                        cot_template = COT_PROMPT_TEMPLATES.get(cot_template_name)
                        outputs = pipeline.generate_image_cot(
                            texts=prompt,
                            diffusion_kwargs=diffusion_kwargs,
                            processor=processor,
                            llm_kwargs=llm_kwargs,
                            cot_prompt_template=cot_template
                        )
                        images = outputs["images"]
                        enhanced_prompts.extend(outputs.get("improved_prompts", []))
                    else:
                        images = pipeline.generate_image(
                            texts=prompt,
                            diffusion_kwargs=diffusion_kwargs
                        )
                
                for img in images:
                    img.save(os.path.join(sample_path, f"{sample_count:05}.png"))
                    sample_count += 1
                    if not skip_grid:
                        all_samples.append(img)
                        
            except Exception as e:
                print(f"Error at prompt {prompt_idx}, batch {batch_idx}: {e}")
                traceback.print_exc()
        
        # Save enhanced prompts
        with open(os.path.join(outpath, "thinking_prompts.txt"), "w") as fp:
            for ep in enhanced_prompts:
                fp.write(f"{ep}\n")
        
        # Create grid
        if not skip_grid and all_samples:
            rows = int(np.sqrt(n_samples))
            cols = (n_samples + rows - 1) // rows
            if rows * cols >= len(all_samples):
                grid_image = create_image_grid(all_samples[:rows * cols], rows, cols)
                grid_image.save(os.path.join(outpath, "grid.jpg"))


def generate_edit_batch(
    data_batch, start_idx, pipeline, processor, output_dir, batch_size,
    guidance_scale, num_inference_steps, seed, use_cot, cot_template_name,
    device_id, resolution
):
    """Edit images based on prompts (Edit mode)."""
    os.makedirs(output_dir, exist_ok=True)
    
    transform = TF.Compose([
        TF.Resize(resolution),
        TF.CenterCrop(resolution)
    ])
    
    for i in tqdm(range(0, len(data_batch), batch_size), desc=f"GPU {device_id} Edit"):
        batch_data = data_batch[i:i + batch_size]
        batch_start_idx = start_idx + i
        
        batch_images = [transform(item['image']) for item in batch_data]
        batch_prompts = [item['prompt'] for item in batch_data]
        batch_ids = [item['id'] for item in batch_data]
        
        diffusion_kwargs = dict(
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            num_images_per_prompt=1,
            generator=torch.Generator("cpu").manual_seed(seed),
            max_area=resolution ** 2
        )
        
        try:
            with torch.no_grad():
                if use_cot:
                    llm_kwargs = dict(
                        max_new_tokens=256, temperature=0.7, top_p=0.9,
                        do_sample=False, num_return_sequences=1
                    )
                    cot_template = COT_PROMPT_TEMPLATES.get(cot_template_name)
                    outputs = pipeline.generate_image_cot(
                        images=batch_images,
                        texts=batch_prompts,
                        diffusion_kwargs=diffusion_kwargs,
                        processor=processor,
                        llm_kwargs=llm_kwargs,
                        cot_prompt_template=cot_template
                    )
                    edited_images = outputs["images"]
                    improved_prompts = outputs.get("improved_prompts", [])
                else:
                    edited_images = pipeline.generate_image(
                        images=batch_images,
                        texts=batch_prompts,
                        diffusion_kwargs=diffusion_kwargs
                    )
                    improved_prompts = []
            
            for j, (edited_img, ref_img) in enumerate(zip(edited_images, batch_images)):
                item_id = batch_ids[j]
                base_name = f"{item_id}"
                
                edited_img.save(os.path.join(output_dir, f"{base_name}_edited.png"))
                ref_img.save(os.path.join(output_dir, f"{base_name}_reference.png"))
                
                with open(os.path.join(output_dir, f"{base_name}_prompt.txt"), 'w', encoding='utf-8') as f:
                    f.write(batch_prompts[j])
                
                if use_cot and j < len(improved_prompts):
                    with open(os.path.join(output_dir, f"{base_name}_improved_prompt.txt"), 'w', encoding='utf-8') as f:
                        f.write(improved_prompts[j])
                        
        except Exception as e:
            print(f"Error at batch {batch_start_idx}: {e}")
            traceback.print_exc()


# =============================================================================
# Worker Process
# =============================================================================
def worker_process(
    device_id, mode, data, start_idx, pipeline, processor, output_dir,
    batch_size, guidance_scale, num_inference_steps, seed, use_cot,
    cot_template_name, add_instruction, n_samples, skip_grid, resolution, metadatas=None
):
    """Single GPU worker process."""
    torch.cuda.set_device(f"cuda:{device_id % NUM_DEVICE}")
    
    print(f"GPU {device_id}: Processing {len(data)} items (indices {start_idx} to {start_idx + len(data) - 1})")
    
    if mode == "t2i":
        generate_t2i_batch(
            prompts=data, start_idx=start_idx, pipeline=pipeline,
            processor=processor, output_dir=output_dir, batch_size=batch_size,
            guidance_scale=guidance_scale, num_inference_steps=num_inference_steps,
            seed=seed, use_cot=use_cot, cot_template_name=cot_template_name,
            add_instruction=add_instruction, device_id=device_id
        )
    elif mode == "geneval":
        generate_geneval_batch(
            prompts=data, metadatas=metadatas, start_idx=start_idx,
            pipeline=pipeline, processor=processor, output_dir=output_dir,
            batch_size=batch_size, guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps, seed=seed,
            n_samples=n_samples, use_cot=use_cot, cot_template_name=cot_template_name,
            skip_grid=skip_grid, device_id=device_id
        )
    elif mode == "edit":
        generate_edit_batch(
            data_batch=data, start_idx=start_idx, pipeline=pipeline,
            processor=processor, output_dir=output_dir, batch_size=batch_size,
            guidance_scale=guidance_scale, num_inference_steps=num_inference_steps,
            seed=seed, use_cot=use_cot, cot_template_name=cot_template_name,
            device_id=device_id, resolution=resolution
        )
    
    print(f"GPU {device_id}: Completed!")


# =============================================================================
# Argument Parser
# =============================================================================
def parse_args():
    parser = argparse.ArgumentParser(
        description="Unified Inference Script for Image Generation and Editing"
    )
    
    # Mode selection
    parser.add_argument(
        "--mode", type=str, required=True,
        choices=["t2i", "geneval", "edit"],
        help="Inference mode: t2i (text-to-image), geneval (evaluation), edit (image editing)"
    )
    
    # Input/Output
    parser.add_argument("--prompt_file", type=str, help="Text file with prompts (for t2i mode)")
    parser.add_argument("--metadata_file", type=str, help="JSONL metadata file (for geneval mode)")
    parser.add_argument("--data_file", type=str, help="Parquet file with images and prompts (for edit mode)")
    parser.add_argument("--output_dir", type=str, default="outputs", help="Output directory")
    
    # Model configuration
    parser.add_argument("--model_path", type=str, required=True, help="Model path")
    parser.add_argument(
        "--model_type", type=str, default="flux",
        choices=["flux", "sana", "sd3", "kontext"],
        help="Model type"
    )
    
    # Generation parameters
    parser.add_argument("--batch_size", type=int, default=8, help="Batch size")
    parser.add_argument("--resolution", type=int, default=1024, help="Image resolution")
    parser.add_argument("--guidance_scale", type=float, default=3.5, help="CFG guidance scale")
    parser.add_argument("--num_inference_steps", type=int, default=40, help="Inference steps")
    parser.add_argument("--seed", type=int, default=42, help="Random seed")
    
    # CoT options
    parser.add_argument("--use_cot", action="store_true", help="Use Chain of Thought")
    parser.add_argument(
        "--cot_template", type=str, default="general",
        choices=list(COT_PROMPT_TEMPLATES.keys()),
        help="CoT prompt template"
    )
    parser.add_argument("--add_instruction", action="store_true", help="Add instruction prefix (t2i mode)")
    
    # GenEval specific
    parser.add_argument("--n_samples", type=int, default=4, help="Samples per prompt (geneval mode)")
    parser.add_argument("--skip_grid", action="store_true", help="Skip grid image (geneval mode)")
    
    # Hardware
    parser.add_argument("--num_gpus", type=int, default=None, help="Number of GPUs to use")
    parser.add_argument("--max_samples", type=int, default=None, help="Max samples to process")
    
    return parser.parse_args()


# =============================================================================
# Main Function
# =============================================================================
def main():
    mp.set_start_method('spawn', force=True)
    args = parse_args()
    
    global NUM_PROCESSES
    if args.num_gpus is not None:
        NUM_PROCESSES = min(args.num_gpus, NUM_DEVICE)
    
    # Validate mode-specific arguments
    if args.mode == "t2i" and not args.prompt_file:
        raise ValueError("--prompt_file is required for t2i mode")
    if args.mode == "geneval" and not args.metadata_file:
        raise ValueError("--metadata_file is required for geneval mode")
    if args.mode == "edit" and not args.data_file:
        raise ValueError("--data_file is required for edit mode")
    if args.mode == "edit" and args.model_type != "kontext":
        print(f"Warning: edit mode typically uses kontext model, but got {args.model_type}")
    
    # Load data based on mode
    print(f"Mode: {args.mode}")
    metadatas = None
    
    if args.mode == "t2i":
        print(f"Loading prompts from {args.prompt_file}...")
        data = load_prompts_from_txt(args.prompt_file)
    elif args.mode == "geneval":
        print(f"Loading metadata from {args.metadata_file}...")
        data, metadatas = load_prompts_from_jsonl(args.metadata_file)
    elif args.mode == "edit":
        print(f"Loading data from {args.data_file}...")
        data = load_data_from_parquet(args.data_file)
    
    # Apply max_samples limit
    if args.max_samples is not None:
        if args.mode == "geneval":
            data = data[:args.max_samples]
            metadatas = metadatas[:args.max_samples]
        else:
            data = data[:args.max_samples]
        print(f"Limited to {len(data)} samples")
    
    print(f"Total samples: {len(data)}")
    
    # Create output directory
    os.makedirs(args.output_dir, exist_ok=True)
    
    # Save configuration
    config_path = os.path.join(args.output_dir, "config.json")
    config_dict = vars(args).copy()
    with open(config_path, 'w') as f:
        json.dump(config_dict, f, indent=2)
    print(f"Config saved to {config_path}")
    
    # Load models
    print("Loading models...")
    pipelines = []
    processors = []
    
    for i in range(NUM_DEVICE):
        print(f"Loading model {i+1}/{NUM_DEVICE} on cuda:{i % NUM_DEVICE}...")
        pipeline, processor = load_model_pipeline(
            args.model_path, args.model_type, f"cuda:{i % NUM_DEVICE}"
        )
        pipelines.append(pipeline)
        processors.append(processor)
    
    print("All models loaded!")
    
    # Distribute data across GPUs
    samples_per_gpu = len(data) // NUM_PROCESSES
    
    with ThreadPoolExecutor(max_workers=NUM_PROCESSES) as executor:
        futures = []
        
        for device_id in range(NUM_PROCESSES):
            start_idx = device_id * samples_per_gpu
            end_idx = len(data) if device_id == NUM_PROCESSES - 1 else start_idx + samples_per_gpu
            
            gpu_data = data[start_idx:end_idx]
            gpu_metadatas = metadatas[start_idx:end_idx] if metadatas else None
            
            future = executor.submit(
                worker_process,
                device_id=device_id,
                mode=args.mode,
                data=gpu_data,
                start_idx=start_idx,
                pipeline=pipelines[device_id % NUM_DEVICE],
                processor=processors[device_id % NUM_DEVICE],
                output_dir=args.output_dir,
                batch_size=args.batch_size,
                guidance_scale=args.guidance_scale,
                num_inference_steps=args.num_inference_steps,
                seed=args.seed,
                use_cot=args.use_cot,
                cot_template_name=args.cot_template,
                add_instruction=args.add_instruction,
                n_samples=args.n_samples,
                skip_grid=args.skip_grid,
                resolution=args.resolution,
                metadatas=gpu_metadatas
            )
            futures.append(future)
        
        for future in as_completed(futures):
            try:
                future.result()
            except Exception as e:
                print(f"Worker failed: {e}")
                traceback.print_exc()
    
    print(f"\n✓ Done! Results saved to {args.output_dir}")
    print(f"  Total processed: {len(data)}")


if __name__ == "__main__":
    main()