Spaces:
Running on Zero
Running on Zero
Switch Turbo to Edit-Turbo (TI2I 4-step distilled); reuse Edit AoTI graph; drop pure-T2I DMD path
Browse files25 unmodified lines2627282930313233343536373839402 unmodified lines4344454647484950511 unmodified line53545556575859606162636465661 unmodified line6869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115229 unmodified lines34534634734834935035135220 unmodified lines3733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024037 unmodified lines41141241341441541641726 unmodified lines44444544644744844945060 unmodified lines51151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554625 unmodified linesfrom PIL import Imagefrom boogu.pipelines.boogu.pipeline_boogu import BooguImagePipelinefrom boogu.pipelines.boogu.pipeline_boogu_turbo import BooguImageTurboPipelineMODEL_ID = "Boogu/Boogu-Image-0.1-Edit"TURBO_ID = "Boogu/Boogu-Image-0.1-Turbo"AOTI_REPO = "multimodalart/Boogu-Image-0.1-Edit-aoti"# Set to a Turbo AoTI repo to patch the Turbo single-stream blocks (None = eager).# Flip between "...-Turbo-aoti" (default compile) and "...-Turbo-aoti-mat" (max_autotune)# to A/B the compiled variants. Leave None to keep the eager 3.3s baseline.TURBO_AOTI_REPO = os.environ.get("TURBO_AOTI_REPO") or Nonepipe = BooguImagePipeline.from_pretrained(MODEL_ID,2 unmodified lines)pipe.to("cuda")# Turbo shares the (byte-identical) mllm / vae / processor / scheduler with Edit;# only the transformer differs. Load just the Turbo transformer and build a Turbo# pipeline reusing the already-resident components — no duplicate 17.5GB mllm.turbo_transformer = type(pipe.transformer).from_pretrained(TURBO_ID,subfolder="transformer",1 unmodified line)_turbo_components = dict(pipe.components)_turbo_components["transformer"] = turbo_transformerturbo_pipe = BooguImageTurboPipeline(**_turbo_components)turbo_pipe.text_instruction_rewriter = pipe.text_instruction_rewriterturbo_pipe.instruction_rewriter_processor = pipe.instruction_rewriter_processorturbo_pipe.to("cuda")# Swap the 24 repeated single-stream blocks for their AoTI-compiled graph# (one shared compiled graph, per-block weights). Falls back to eager on any error.# Only the Edit transformer is compiled for now; Turbo runs eager (baseline).try:from pathlib import Pathfrom huggingface_hub import snapshot_download1 unmodified line_block_dir = Path(snapshot_download(AOTI_REPO)) / "BooguImageTransformerBlock"if (_block_dir / "package.pt2").exists():aoti_load_from_module_dir(pipe.transformer.single_stream_layers, _block_dir)print(f"AoTI: patched {len(pipe.transformer.single_stream_layers)} Edit single-stream blocks")else:print("AoTI: Edit package.pt2 not found, running eager")except Exception as exc: # noqa: BLE001print(f"AoTI (Edit) load failed ({exc!r}); running eager")# Optionally patch the Turbo single-stream blocks too (off by default = eager baseline).if TURBO_AOTI_REPO:try:from pathlib import Pathfrom huggingface_hub import snapshot_downloadfrom spaces.zero.torch.aoti import aoti_load_from_module_dir_t_dir = Path(snapshot_download(TURBO_AOTI_REPO)) / "BooguImageTransformerBlock"if (_t_dir / "package.pt2").exists():aoti_load_from_module_dir(turbo_pipe.transformer.single_stream_layers, _t_dir)print(f"AoTI: patched {len(turbo_pipe.transformer.single_stream_layers)} Turbo blocks from {TURBO_AOTI_REPO}")else:print(f"AoTI: Turbo package.pt2 not found in {TURBO_AOTI_REPO}, running eager")except Exception as exc: # noqa: BLE001print(f"AoTI (Turbo) load failed ({exc!r}); running eager")# EXPERIMENT (#10): optionally patch the 2 Turbo double-stream blocks with a second# AoTI graph. WARNING: that graph bakes the captured per-sample seq lengths as# constants (the block takes them as python int lists, not dynamic tensors), so it# is only correct for prompts whose instruction tokenizes to the captured length.DS_TURBO_AOTI_REPO = os.environ.get("DS_TURBO_AOTI_REPO") or Noneif DS_TURBO_AOTI_REPO:try:from pathlib import Pathfrom huggingface_hub import snapshot_downloadfrom spaces.zero.torch.aoti import aoti_load_from_module_dir_ds_dir = Path(snapshot_download(DS_TURBO_AOTI_REPO)) / "BooguImageDoubleStreamTransformerBlock"if (_ds_dir / "package.pt2").exists():aoti_load_from_module_dir(turbo_pipe.transformer.double_stream_layers, _ds_dir)print(f"AoTI: patched {len(turbo_pipe.transformer.double_stream_layers)} Turbo double-stream blocks from {DS_TURBO_AOTI_REPO}")else:print(f"AoTI: Turbo double-stream package.pt2 not found in {DS_TURBO_AOTI_REPO}, running eager")except Exception as exc: # noqa: BLE001print(f"AoTI (Turbo double-stream) load failed ({exc!r}); running eager")MAX_SEED = 2**31 - 1229 unmodified linesdef _duration(image, instruction, model_choice, resolution, num_inference_steps, *args, **kwargs):per_step = 4 if model_choice == "Turbo" else 4base = int(num_inference_steps) * per_step + (40 if model_choice == "Turbo" else 60)return base * 2 if resolution == "2K" else base20 unmodified linesres = RESOLUTIONS[resolution]generator = torch.Generator("cuda").manual_seed(seed)input_pil = Noneif model_choice == "Turbo":# DMD few-step text-to-image: no reference image, no CFG (all scales == 1.0).size = 1024 if resolution == "1K" else 2048result = turbo_pipe(instruction=[instruction.strip()],negative_instruction="",empty_instruction="",height=size,width=size,max_input_image_pixels=res["pixels"],max_input_image_side_length=res["side"],num_inference_steps=int(num_inference_steps),text_guidance_scale=1.0,image_guidance_scale=1.0,empty_instruction_guidance_scale=0.0,use_dmd_student_inference=True,dmd_conditioning_sigma=0.001,generator=generator,device="cuda",).images[0]elif image is None:# Text-to-image: no reference image, output size is set explicitly.size = 1024 if resolution == "1K" else 2048result = pipe(instruction=[instruction.strip()],negative_instruction="",height=size,7 unmodified lines).images[0]else:input_pil = Image.open(image).convert("RGB")result = pipe(instruction=[instruction.strip()],input_image_paths=[[image]],input_images=[[input_pil]],26 unmodified lines"""# 🍊 Boogu-Image-0.1Unified generation/editing with [Boogu-Image-0.1](https://huggingface.co/Boogu) - a 10B model(Qwen3-VL + FLUX VAE)""")with gr.Row():60 unmodified lines)def _on_model_change(choice):if choice == "Turbo":return (gr.update(visible=False), # image (Turbo is T2I only)gr.update(value=4, minimum=1, maximum=8, label="Inference steps (Turbo)"),gr.update(value=1.0, interactive=False), # text guidance (CFG off for DMD)gr.update(value=1.0, interactive=False), # image guidance (unused)gr.update(visible=False), # slider_col (T2I has no before image)gr.update(visible=True), # image_col)return (gr.update(visible=True),gr.update(value=32, minimum=1, maximum=50, label="Inference steps"),gr.update(value=4.0, interactive=True),gr.update(value=1.0, interactive=True),gr.update(visible=True), # slider_col (Edit shows before/after)gr.update(visible=False), # image_col)model_choice.change(_on_model_change,inputs=[model_choice],outputs=[image, num_inference_steps, text_guidance_scale, image_guidance_scale,slider_col, image_col,],)def _result_visibility(model_choice, image):# Comparison only when there is a genuine before/after (Edit + reference image).is_compare = model_choice != "Turbo" and image is not Nonereturn gr.update(visible=is_compare), gr.update(visible=not is_compare)inputs = [25 unmodified lines2627282930313233343536373839404142432 unmodified lines4647484950515253541 unmodified line5657585960616263646566676869701 unmodified line72737475767778798081828384229 unmodified lines31431531631731831932020 unmodified lines3413423433443453463473483493503513523533543553563573587 unmodified lines36636736836937037137226 unmodified lines39940040140240340440540640760 unmodified lines46846947047147247347447547647747847948048148248348448548648748848949049149249349449549649725 unmodified linesfrom PIL import Imagefrom boogu.pipelines.boogu.pipeline_boogu import BooguImagePipelineMODEL_ID = "Boogu/Boogu-Image-0.1-Edit"# Edit-Turbo is a 4-step DMD-distilled variant of the Edit model. Its model_index# declares the plain BooguImagePipeline and its mllm / vae / processor / scheduler /# transformer *architecture* are byte-identical to Edit — only the (distilled)# transformer weights differ. So it runs through the exact same edit/T2I code path,# just at ~4 steps with CFG disabled (guidance == 1.0).TURBO_ID = "Boogu/Boogu-Image-0.1-Edit-Turbo"# One shared AoTI single-stream-block graph. Because the Edit and Edit-Turbo# transformers have an identical config, the graph compiled from Edit# (weights are runtime inputs, not baked constants) applies to both pipelines.AOTI_REPO = "multimodalart/Boogu-Image-0.1-Edit-aoti"pipe = BooguImagePipeline.from_pretrained(MODEL_ID,2 unmodified lines)pipe.to("cuda")# Edit-Turbo shares the (byte-identical) mllm / vae / processor / scheduler with# Edit; only the transformer differs. Load just the Turbo transformer and build a# second pipeline reusing the already-resident components — no duplicate 17.5GB mllm.turbo_transformer = type(pipe.transformer).from_pretrained(TURBO_ID,subfolder="transformer",1 unmodified line)_turbo_components = dict(pipe.components)_turbo_components["transformer"] = turbo_transformerturbo_pipe = BooguImagePipeline(**_turbo_components)turbo_pipe.text_instruction_rewriter = pipe.text_instruction_rewriterturbo_pipe.instruction_rewriter_processor = pipe.instruction_rewriter_processorturbo_pipe.to("cuda")# Swap the repeated single-stream blocks for their AoTI-compiled graph (one shared# compiled graph, per-block weights streamed in at runtime). The Edit and Edit-Turbo# transformers share an identical block architecture, so the same package.pt2 patches# both. Falls back to eager on any error.try:from pathlib import Pathfrom huggingface_hub import snapshot_download1 unmodified line_block_dir = Path(snapshot_download(AOTI_REPO)) / "BooguImageTransformerBlock"if (_block_dir / "package.pt2").exists():for _name, _p in (("Edit", pipe), ("Edit-Turbo", turbo_pipe)):aoti_load_from_module_dir(_p.transformer.single_stream_layers, _block_dir)print(f"AoTI: patched {len(_p.transformer.single_stream_layers)} {_name} single-stream blocks")else:print("AoTI: package.pt2 not found, running eager")except Exception as exc: # noqa: BLE001print(f"AoTI load failed ({exc!r}); running eager")MAX_SEED = 2**31 - 1229 unmodified linesdef _duration(image, instruction, model_choice, resolution, num_inference_steps, *args, **kwargs):base = int(num_inference_steps) * 4 + (40 if model_choice == "Turbo" else 60)return base * 2 if resolution == "2K" else base20 unmodified linesres = RESOLUTIONS[resolution]generator = torch.Generator("cuda").manual_seed(seed)# Edit-Turbo runs the same pipeline as Edit, just distilled to few steps with CFG# disabled — both text and image guidance are pinned to 1.0 for the Turbo path.active_pipe = turbo_pipe if model_choice == "Turbo" else pipeif model_choice == "Turbo":text_guidance_scale = 1.0image_guidance_scale = 1.0input_pil = Noneif image is None:# Text-to-image: no reference image, output size is set explicitly.size = 1024 if resolution == "1K" else 2048result = active_pipe(instruction=[instruction.strip()],negative_instruction="",height=size,7 unmodified lines).images[0]else:input_pil = Image.open(image).convert("RGB")result = active_pipe(instruction=[instruction.strip()],input_image_paths=[[image]],input_images=[[input_pil]],26 unmodified lines"""# 🍊 Boogu-Image-0.1Unified generation/editing with [Boogu-Image-0.1](https://huggingface.co/Boogu) - a 10B model(Qwen3-VL + FLUX VAE). **Edit** is the full model; **Turbo** is the 4-stepdistilled [Edit-Turbo](https://huggingface.co/Boogu/Boogu-Image-0.1-Edit-Turbo).Both edit an input image, or generate from scratch when none is given.""")with gr.Row():60 unmodified lines)def _on_model_change(choice):# Both models share the edit/T2I path; Turbo just runs distilled at ~4 steps# with CFG disabled, so lock the guidance sliders when it is selected.if choice == "Turbo":return (gr.update(value=4, minimum=1, maximum=8, label="Inference steps (Turbo)"),gr.update(value=1.0, interactive=False), # text guidance (CFG off, distilled)gr.update(value=1.0, interactive=False), # image guidance (CFG off, distilled))return (gr.update(value=32, minimum=1, maximum=50, label="Inference steps"),gr.update(value=4.0, interactive=True),gr.update(value=1.0, interactive=True),)model_choice.change(_on_model_change,inputs=[model_choice],outputs=[num_inference_steps, text_guidance_scale, image_guidance_scale],)def _result_visibility(model_choice, image):# Comparison whenever there is a genuine before/after (any edit with a# reference image); plain result view for text-to-image.is_compare = image is not Nonereturn gr.update(visible=is_compare), gr.update(visible=not is_compare)inputs = [
app.py+44-93