import subprocess import os subprocess.run( "pip install flash-attn --no-build-isolation", env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"}, shell=True, ) subprocess.run( "pip install huggingface_hub==0.25.0", shell=True, ) subprocess.run( "pip install numpy==1.26.4", shell=True, ) os.makedirs("/home/user/app/checkpoints", exist_ok=True) from huggingface_hub import snapshot_download snapshot_download( repo_id="Alpha-VLLM/Lumina-Image-2.0", local_dir="/home/user/app/checkpoints" ) hf_token = os.environ["HF_TOKEN"] import argparse import os import builtins import json import math import multiprocessing as mp import os import random import socket import traceback #import fairscale.nn.model_parallel.initialize as fs_init import gradio as gr import numpy as np from safetensors.torch import load_file import torch #i#mport torch.distributed as dist from torchvision.transforms.functional import to_pil_image import spaces from imgproc import generate_crop_size_list import models from transport import Sampler, create_transport from multiprocessing import Process,Queue,set_start_method,get_context class ModelFailure: pass # Adapted from pipelines.StableDiffusionXLPipeline.encode_prompt def encode_prompt(prompt_batch, text_encoder, tokenizer, proportion_empty_prompts, is_train=True): captions = [] for caption in prompt_batch: if random.random() < proportion_empty_prompts: captions.append("") elif isinstance(caption, str): captions.append(caption) elif isinstance(caption, (list, np.ndarray)): captions.append(random.choice(caption) if is_train else caption[0]) with torch.no_grad(): text_inputs = tokenizer( captions, padding=True, pad_to_multiple_of=8, max_length=256, truncation=True, return_tensors="pt", ) print(f"Text Encoder Device: {text_encoder.device}") text_input_ids = text_inputs.input_ids.cuda() prompt_masks = text_inputs.attention_mask.cuda() print(f"Text Input Ids Device: {text_input_ids.device}") print(f"Prompt Masks Device: {prompt_masks.device}") prompt_embeds = text_encoder( input_ids=text_input_ids, attention_mask=prompt_masks, output_hidden_states=True, ).hidden_states[-2] text_encoder.cpu() return prompt_embeds, prompt_masks @torch.no_grad() def model_main(args, master_port, rank): # import here to avoid huggingface Tokenizer parallelism warnings from diffusers.models import AutoencoderKL from transformers import AutoModel, AutoTokenizer # override the default print function since the delay can be large for child process original_print = builtins.print # Redefine the print function with flush=True by default def print(*args, **kwargs): kwargs.setdefault("flush", True) original_print(*args, **kwargs) # Override the built-in print with the new version builtins.print = print train_args = torch.load(os.path.join(args.ckpt, "model_args.pth")) print("Loaded model arguments:", json.dumps(train_args.__dict__, indent=2)) print(f"Creating lm: Gemma-2-2B") dtype = {"bf16": torch.bfloat16, "fp16": torch.float16, "fp32": torch.float32}[args.precision] text_encoder = AutoModel.from_pretrained( "google/gemma-2-2b", torch_dtype=dtype, token=hf_token ).eval().to("cuda") cap_feat_dim = text_encoder.config.hidden_size if args.num_gpus > 1: raise NotImplementedError("Inference with >1 GPUs not yet supported") tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b", token=hf_token) tokenizer.padding_side = "right" vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", token=hf_token).cuda() print(f"Creating DiT: {train_args.model}") model = models.__dict__[train_args.model]( in_channels=16, qk_norm=train_args.qk_norm, cap_feat_dim=cap_feat_dim, ) model.eval().to("cuda", dtype=dtype) assert train_args.model_parallel_size == args.num_gpus if args.ema: print("Loading ema model.") print('load model') ckpt_path = os.path.join( args.ckpt, f"consolidated{'_ema' if args.ema else ''}.{rank:02d}-of-{args.num_gpus:02d}.safetensors", ) if os.path.exists(ckpt_path): ckpt = load_file(ckpt_path) else: ckpt_path = os.path.join( args.ckpt, f"consolidated{'_ema' if args.ema else ''}.{rank:02d}-of-{args.num_gpus:02d}.pth", ) assert os.path.exists(ckpt_path) ckpt = torch.load(ckpt_path, map_location="cuda") model.load_state_dict(ckpt, strict=True) print('load model finish') return text_encoder, tokenizer, vae, model @torch.no_grad() def inference(args, infer_args, text_encoder, tokenizer, vae, model): dtype = {"bf16": torch.bfloat16, "fp16": torch.float16, "fp32": torch.float32}[ args.precision ] train_args = torch.load(os.path.join(args.ckpt, "model_args.pth")) torch.cuda.set_device(0) with torch.autocast("cuda", dtype): while True: ( cap, neg_cap, system_type, resolution, num_sampling_steps, cfg_scale, cfg_trunc, renorm_cfg, solver, t_shift, seed, scaling_method, scaling_watershed, proportional_attn, ) = infer_args system_prompt = system_type cap = system_prompt + cap if neg_cap != "": neg_cap = system_prompt + neg_cap metadata = dict( real_cap=cap, real_neg_cap=neg_cap, system_type=system_type, resolution=resolution, num_sampling_steps=num_sampling_steps, cfg_scale=cfg_scale, cfg_trunc=cfg_trunc, renorm_cfg=renorm_cfg, solver=solver, t_shift=t_shift, seed=seed, scaling_method=scaling_method, scaling_watershed=scaling_watershed, proportional_attn=proportional_attn, ) print("> params:", json.dumps(metadata, indent=2)) try: # begin sampler if solver == "dpm": transport = create_transport( "Linear", "velocity", ) sampler = Sampler(transport) sample_fn = sampler.sample_dpm( model.forward_with_cfg, model_kwargs=model_kwargs, ) else: transport = create_transport( args.path_type, args.prediction, args.loss_weight, args.train_eps, args.sample_eps, ) sampler = Sampler(transport) sample_fn = sampler.sample_ode( sampling_method=solver, num_steps=num_sampling_steps, atol=args.atol, rtol=args.rtol, reverse=args.reverse, time_shifting_factor=t_shift, ) # end sampler resolution = resolution.split(" ")[-1] w, h = resolution.split("x") w, h = int(w), int(h) latent_w, latent_h = w // 8, h // 8 if int(seed) != 0: torch.random.manual_seed(int(seed)) z = torch.randn([1, 16, latent_h, latent_w], device="cuda").to(dtype) z = z.repeat(2, 1, 1, 1) with torch.no_grad(): if neg_cap != "": cap_feats, cap_mask = encode_prompt([cap] + [neg_cap], text_encoder, tokenizer, 0.0) else: cap_feats, cap_mask = encode_prompt([cap] + [""], text_encoder, tokenizer, 0.0) cap_mask = cap_mask.to(cap_feats.device) model_kwargs = dict( cap_feats=cap_feats, cap_mask=cap_mask, cfg_scale=cfg_scale, cfg_trunc=1 - cfg_trunc, renorm_cfg=renorm_cfg, ) #if dist.get_rank() == 0: print(f"> caption: {cap}") print(f"> num_sampling_steps: {num_sampling_steps}") print(f"> cfg_scale: {cfg_scale}") print("> start sample") if solver == "dpm": samples = sample_fn(z, steps=num_sampling_steps, order=2, skip_type="time_uniform_flow", method="multistep", flow_shift=t_shift) else: samples = sample_fn(z, model.forward_with_cfg, **model_kwargs)[-1] samples = samples[:1] print("smaple_dtype", samples.dtype) vae_scale = { "sdxl": 0.13025, "sd3": 1.5305, "ema": 0.18215, "mse": 0.18215, "cogvideox": 1.15258426, "flux": 0.3611, }["flux"] vae_shift = { "sdxl": 0.0, "sd3": 0.0609, "ema": 0.0, "mse": 0.0, "cogvideox": 0.0, "flux": 0.1159, }["flux"] print(f"> vae scale: {vae_scale}, shift: {vae_shift}") print("samples.shape", samples.shape) samples = vae.decode(samples / vae_scale + vae_shift).sample samples = (samples + 1.0) / 2.0 samples.clamp_(0.0, 1.0) img = to_pil_image(samples[0].float()) print("> generated image, done.") return img, metadata except Exception: print(traceback.format_exc()) return ModelFailure() def none_or_str(value): if value == "None": return None return value def parse_transport_args(parser): group = parser.add_argument_group("Transport arguments") group.add_argument( "--path-type", type=str, default="Linear", choices=["Linear", "GVP", "VP"], help="the type of path for transport: 'Linear', 'GVP' (Geodesic Vector Pursuit), or 'VP' (Vector Pursuit).", ) group.add_argument( "--prediction", type=str, default="velocity", choices=["velocity", "score", "noise"], help="the prediction model for the transport dynamics.", ) group.add_argument( "--loss-weight", type=none_or_str, default=None, choices=[None, "velocity", "likelihood"], help="the weighting of different components in the loss function, can be 'velocity' for dynamic modeling, 'likelihood' for statistical consistency, or None for no weighting.", ) group.add_argument("--sample-eps", type=float, help="sampling in the transport model.") group.add_argument("--train-eps", type=float, help="training to stabilize the learning process.") def parse_ode_args(parser): group = parser.add_argument_group("ODE arguments") group.add_argument( "--atol", type=float, default=1e-6, help="Absolute tolerance for the ODE solver.", ) group.add_argument( "--rtol", type=float, default=1e-3, help="Relative tolerance for the ODE solver.", ) group.add_argument("--reverse", action="store_true", help="run the ODE solver in reverse.") group.add_argument( "--likelihood", action="store_true", help="Enable calculation of likelihood during the ODE solving process.", ) def find_free_port() -> int: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("", 0)) port = sock.getsockname()[1] sock.close() return port def main(): parser = argparse.ArgumentParser() parser.add_argument("--num_gpus", type=int, default=1) parser.add_argument("--ckpt", type=str,default='/home/user/app/checkpoints', required=False) parser.add_argument("--ema", action="store_true") parser.add_argument("--precision", default="bf16", choices=["bf16", "fp32"]) parser.add_argument("--hf_token", type=str, default=None, help="huggingface read token for accessing gated repo.") parser.add_argument("--res", type=int, default=1024, choices=[256, 512, 1024]) parser.add_argument("--port", type=int, default=12123) parse_transport_args(parser) parse_ode_args(parser) args = parser.parse_known_args()[0] if args.num_gpus != 1: raise NotImplementedError("Multi-GPU Inference is not yet supported") master_port = find_free_port() text_encoder, tokenizer, vae, model = model_main(args, master_port, 0) description = "Lumina-Image 2.0 ([Github](https://github.com/Alpha-VLLM/Lumina-Image-2.0/tree/main))" with gr.Blocks() as demo: with gr.Row(): gr.Markdown(description) with gr.Row(): with gr.Column(): cap = gr.Textbox( lines=2, label="Caption", interactive=True, value="Majestic landscape photograph of snow-capped mountains under a dramatic sky at sunset. The mountains dominate the lower half of the image, with rugged peaks and deep crevasses visible. A glacier flows down the right side, partially illuminated by the warm light. The sky is filled with fiery orange and golden clouds, contrasting with the cool tones of the snow. The central peak is partially obscured by clouds, adding a sense of mystery. The foreground features dark, shadowed forested areas, enhancing the depth. High contrast, natural lighting, warm color palette, photorealistic, expansive, awe-inspiring, serene, visually balanced, dynamic composition.", placeholder="Enter a caption.", ) neg_cap = gr.Textbox( lines=2, label="Negative Caption", interactive=True, value="", placeholder="Enter a negative caption.", ) default_value = "You are an assistant designed to generate superior images with the superior degree of image-text alignment based on textual prompts or user prompts." system_type = gr.Dropdown( value=default_value, choices=[ "You are an assistant designed to generate high-quality images with the highest degree of image-text alignment based on textual prompts.", "You are an assistant designed to generate superior images with the superior degree of image-text alignment based on textual prompts or user prompts.", "", ], label="System Type", ) with gr.Row(): res_choices = [f"{w}x{h}" for w, h in generate_crop_size_list((args.res // 64) ** 2, 64)] default_value = "1024x1024" # Set the default value to 256x256 resolution = gr.Dropdown( value=default_value, choices=res_choices, label="Resolution" ) with gr.Row(): num_sampling_steps = gr.Slider( minimum=1, maximum=70, value=40, step=1, interactive=True, label="Sampling steps", ) seed = gr.Slider( minimum=0, maximum=int(1e5), value=0, step=1, interactive=True, label="Seed (0 for random)", ) cfg_trunc = gr.Slider( minimum=0, maximum=1, value=0, step=0.01, interactive=True, label="CFG Truncation", ) with gr.Row(): solver = gr.Dropdown( value="euler", choices=["euler", "midpoint", "rk4"], label="solver", ) t_shift = gr.Slider( minimum=1, maximum=20, value=6, step=1, interactive=True, label="Time shift", ) cfg_scale = gr.Slider( minimum=1.0, maximum=20.0, value=4.0, interactive=True, label="CFG scale", ) with gr.Row(): renorm_cfg = gr.Dropdown( value=True, choices=[True, False, 2.0], label="CFG Renorm", ) with gr.Accordion("Advanced Settings for Resolution Extrapolation", open=False): with gr.Row(): scaling_method = gr.Dropdown( value="Time-aware", choices=["Time-aware", "None"], label="RoPE scaling method", ) scaling_watershed = gr.Slider( minimum=0.0, maximum=1.0, value=0.3, interactive=True, label="Linear/NTK watershed", ) with gr.Row(): proportional_attn = gr.Checkbox( value=True, interactive=True, label="Proportional attention", ) with gr.Row(): submit_btn = gr.Button("Submit", variant="primary") with gr.Column(): output_img = gr.Image( label="Generated image", interactive=False, ) with gr.Accordion(label="Generation Parameters", open=True): gr_metadata = gr.JSON(label="metadata", show_label=False) with gr.Row(): prompts=[ "Close-up portrait of a young woman with light brown hair, looking to the right, illuminated by warm, golden sunlight. Her hair is gently tousled, catching the light and creating a halo effect around her head. She wears a white garment with a V-neck, visible in the lower left of the frame. The background is dark and out of focus, enhancing the contrast between her illuminated face and the shadows. Soft, ethereal lighting, high contrast, warm color palette, shallow depth of field, natural backlighting, serene and contemplative mood, cinematic quality, intimate and visually striking composition.", "一个剑客,武侠风,红色腰带,戴着斗笠,低头,盖住眼睛,白色背景,细致,精品,杰作,水墨画,墨烟,墨云,泼墨,色带,墨水,墨黑白莲花,光影艺术,笔触。", "Aesthetic photograph of a bouquet of pink and white ranunculus flowers in a clear glass vase, centrally positioned on a wooden surface. The flowers are in full bloom, displaying intricate layers of petals with a soft gradient from pale pink to white. The vase is filled with water, visible through the clear glass, and the stems are submerged. In the background, a blurred vase with green stems is partially visible, adding depth to the composition. The lighting is warm and natural, casting soft shadows and highlighting the delicate textures of the petals. The scene is serene and intimate, with a focus on the organic beauty of the flowers. Photorealistic, shallow depth of field, soft natural lighting, warm color palette, high contrast, glossy texture, tranquil, visually balanced.", "一只优雅的白猫穿着一件紫色的旗袍,旗袍上绣有精致的牡丹花图案,显得高贵典雅。它头上戴着一朵金色的发饰,嘴里叼着一根象征好运的红色丝带。周围环绕着许多飘动的纸鹤和金色的光点,营造出一种祥瑞和梦幻的氛围。超写实风格。" ] prompts = [[_] for _ in prompts] gr.Examples( # noqa prompts, [cap], label="Examples", ) # noqa @spaces.GPU(duration=200) def on_submit(*infer_args, progress=gr.Progress(track_tqdm=True),): result = inference(args, infer_args, text_encoder, tokenizer, vae, model) if isinstance(result, ModelFailure): raise RuntimeError("Model failed to generate the image.") return result submit_btn.click( on_submit, [ cap, neg_cap, system_type, resolution, num_sampling_steps, cfg_scale, cfg_trunc, renorm_cfg, solver, t_shift, seed, scaling_method, scaling_watershed, proportional_attn, ], [output_img, gr_metadata], ) def show_scaling_watershed(scaling_m): return gr.update(visible=scaling_m == "Time-aware") scaling_method.change(show_scaling_watershed, scaling_method, scaling_watershed) demo.queue().launch(server_name="0.0.0.0") if __name__ == "__main__": main()