|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +def parse_args(): |
| 6 | + parser = argparse.ArgumentParser() |
| 7 | + parser.add_argument( |
| 8 | + "--model", |
| 9 | + default="ByteDance-Seed/BAGEL-7B-MoT", |
| 10 | + help="Path to merged model directory.", |
| 11 | + ) |
| 12 | + parser.add_argument("--prompts", nargs="+", default=None, help="Input text prompts.") |
| 13 | + parser.add_argument( |
| 14 | + "--txt-prompts", |
| 15 | + type=str, |
| 16 | + default=None, |
| 17 | + help="Path to a .txt file with one prompt per line (preferred).", |
| 18 | + ) |
| 19 | + parser.add_argument("--prompt_type", default="text", choices=["text"]) |
| 20 | + |
| 21 | + parser.add_argument( |
| 22 | + "--modality", |
| 23 | + default="text2img", |
| 24 | + choices=["text2img", "img2img", "img2text", "text2text"], |
| 25 | + help="Modality mode to control stage execution.", |
| 26 | + ) |
| 27 | + |
| 28 | + parser.add_argument( |
| 29 | + "--image-path", |
| 30 | + type=str, |
| 31 | + default=None, |
| 32 | + help="Path to input image for img2img.", |
| 33 | + ) |
| 34 | + |
| 35 | + # OmniLLM init args |
| 36 | + parser.add_argument("--enable-stats", action="store_true", default=False) |
| 37 | + parser.add_argument("--init-sleep-seconds", type=int, default=20) |
| 38 | + parser.add_argument("--batch-timeout", type=int, default=5) |
| 39 | + parser.add_argument("--init-timeout", type=int, default=300) |
| 40 | + parser.add_argument("--shm-threshold-bytes", type=int, default=65536) |
| 41 | + parser.add_argument("--worker-backend", type=str, default="process", choices=["process", "ray"]) |
| 42 | + parser.add_argument("--ray-address", type=str, default=None) |
| 43 | + parser.add_argument("--stage-configs-path", type=str, default=None) |
| 44 | + parser.add_argument("--steps", type=int, default=50, help="Number of inference steps.") |
| 45 | + |
| 46 | + args = parser.parse_args() |
| 47 | + return args |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + args = parse_args() |
| 52 | + model_name = args.model |
| 53 | + try: |
| 54 | + # Preferred: load from txt file (one prompt per line) |
| 55 | + if getattr(args, "txt_prompts", None) and args.prompt_type == "text": |
| 56 | + with open(args.txt_prompts, encoding="utf-8") as f: |
| 57 | + lines = [ln.strip() for ln in f.readlines()] |
| 58 | + args.prompts = [ln for ln in lines if ln != ""] |
| 59 | + print(f"[Info] Loaded {len(args.prompts)} prompts from {args.txt_prompts}") |
| 60 | + except Exception as e: |
| 61 | + print(f"[Error] Failed to load prompts: {e}") |
| 62 | + raise |
| 63 | + |
| 64 | + if args.prompts is None: |
| 65 | + # Default prompt for text2img test if none provided |
| 66 | + args.prompts = ["<|im_start|>A cute cat<|im_end|>"] |
| 67 | + print(f"[Info] No prompts provided, using default: {args.prompts}") |
| 68 | + omni_outputs = [] |
| 69 | + |
| 70 | + from PIL import Image |
| 71 | + |
| 72 | + if args.modality == "img2img": |
| 73 | + from PIL import Image |
| 74 | + |
| 75 | + from vllm_omni.entrypoints.omni_diffusion import OmniDiffusion |
| 76 | + |
| 77 | + print("[Info] Running in img2img mode (Stage 1 only)") |
| 78 | + client = OmniDiffusion(model=model_name) |
| 79 | + |
| 80 | + generate_kwargs = { |
| 81 | + "prompt": args.prompts, |
| 82 | + "seed": 52, |
| 83 | + "need_kv_receive": False, |
| 84 | + "num_inference_steps": args.steps, |
| 85 | + } |
| 86 | + |
| 87 | + if args.image_path: |
| 88 | + if os.path.exists(args.image_path): |
| 89 | + loaded_image = Image.open(args.image_path).convert("RGB") |
| 90 | + generate_kwargs["pil_image"] = loaded_image |
| 91 | + else: |
| 92 | + print(f"[Warning] Image path {args.image_path} does not exist.") |
| 93 | + |
| 94 | + result = client.generate(**generate_kwargs) |
| 95 | + |
| 96 | + # Ensure result is a list for iteration |
| 97 | + if not isinstance(result, list): |
| 98 | + omni_outputs = [result] |
| 99 | + else: |
| 100 | + omni_outputs = result |
| 101 | + |
| 102 | + else: |
| 103 | + import copy |
| 104 | + |
| 105 | + from vllm_omni.entrypoints.omni import Omni |
| 106 | + |
| 107 | + omni_kwargs = {} |
| 108 | + if args.stage_configs_path: |
| 109 | + omni_kwargs["stage_configs_path"] = args.stage_configs_path |
| 110 | + |
| 111 | + omni_kwargs.update( |
| 112 | + { |
| 113 | + "log_stats": args.enable_stats, |
| 114 | + "init_sleep_seconds": args.init_sleep_seconds, |
| 115 | + "batch_timeout": args.batch_timeout, |
| 116 | + "init_timeout": args.init_timeout, |
| 117 | + "shm_threshold_bytes": args.shm_threshold_bytes, |
| 118 | + "worker_backend": args.worker_backend, |
| 119 | + "ray_address": args.ray_address, |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + omni = Omni(model=model_name, **omni_kwargs) |
| 124 | + |
| 125 | + formatted_prompts = [] |
| 126 | + for p in args.prompts: |
| 127 | + if args.modality == "img2text": |
| 128 | + if args.image_path: |
| 129 | + loaded_image = Image.open(args.image_path).convert("RGB") |
| 130 | + final_prompt_text = f"<|im_start|>user\n<|image_pad|>\n{p}<|im_end|>\n<|im_start|>assistant\n" |
| 131 | + prompt_dict = { |
| 132 | + "prompt": final_prompt_text, |
| 133 | + "multi_modal_data": {"image": loaded_image}, |
| 134 | + "modalities": ["text"], |
| 135 | + } |
| 136 | + formatted_prompts.append(prompt_dict) |
| 137 | + elif args.modality == "text2text": |
| 138 | + final_prompt_text = f"<|im_start|>user\n{p}<|im_end|>\n<|im_start|>assistant\n" |
| 139 | + prompt_dict = {"prompt": final_prompt_text, "modalities": ["text"]} |
| 140 | + formatted_prompts.append(prompt_dict) |
| 141 | + else: |
| 142 | + # text2img |
| 143 | + final_prompt_text = f"<|im_start|>{p}<|im_end|>" |
| 144 | + prompt_dict = {"prompt": final_prompt_text, "modalities": ["image"]} |
| 145 | + formatted_prompts.append(prompt_dict) |
| 146 | + |
| 147 | + params_list = copy.deepcopy(omni.default_sampling_params_list) |
| 148 | + if args.modality == "text2img": |
| 149 | + params_list[0]["max_tokens"] = 1 |
| 150 | + if len(params_list) > 1: |
| 151 | + params_list[1]["num_inference_steps"] = args.steps |
| 152 | + |
| 153 | + omni_outputs = list(omni.generate(prompts=formatted_prompts, sampling_params_list=params_list)) |
| 154 | + |
| 155 | + for i, req_output in enumerate(omni_outputs): |
| 156 | + images = getattr(req_output, "images", None) |
| 157 | + if not images and hasattr(req_output, "output"): |
| 158 | + if isinstance(req_output.output, list): |
| 159 | + images = req_output.output |
| 160 | + else: |
| 161 | + images = [req_output.output] |
| 162 | + |
| 163 | + if images: |
| 164 | + for j, img in enumerate(images): |
| 165 | + img.save(f"output_{i}_{j}.png") |
| 166 | + |
| 167 | + if hasattr(req_output, "request_output") and req_output.request_output: |
| 168 | + for stage_out in req_output.request_output: |
| 169 | + if hasattr(stage_out, "images") and stage_out.images: |
| 170 | + for k, img in enumerate(stage_out.images): |
| 171 | + save_path = f"output_{i}_stage_{getattr(stage_out, 'stage_id', '?')}_{k}.png" |
| 172 | + img.save(save_path) |
| 173 | + print(f"[Info] Saved stage output image to {save_path}") |
| 174 | + |
| 175 | + print(omni_outputs) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments