[Model] Support Boogu/Boogu-Image-0.1-Base and Boogu/Boogu-Image-0.1-Edit (#4995)
Signed-off-by: zzehli <jaeli_ottawa@outlook.com> Signed-off-by: jl <jaeli_ottawa@outlook.com> Signed-off-by: Gao Han <hgaoaf@connect.ust.hk> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d34b91722f
commit
ee33954dff
26 files changed
+4973
No files matched your search
@@ -0,0 +1,12 @@
|
||||
from .boogu_image_transformer import BooguImageTransformer2DModel
|
||||
from .image_processor import BooguImageProcessor
|
||||
from .pipeline_boogu_image import BooguImagePipeline, get_boogu_image_pre_process_func
|
||||
from .scheduling_flow_match_euler_discrete_time_shifting import FlowMatchEulerDiscreteScheduler
|
||||
|
||||
__all__ = [
|
||||
"BooguImagePipeline",
|
||||
"BooguImageProcessor",
|
||||
"BooguImageTransformer2DModel",
|
||||
"FlowMatchEulerDiscreteScheduler",
|
||||
"get_boogu_image_pre_process_func",
|
||||
]
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,213 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
"""Native port of the upstream ``BooguImageProcessor``.
|
||||
|
||||
Ported from the external ``boogu`` package
|
||||
(``boogu/pipelines/image_processor.py``) so the native vLLM-Omni pipeline can
|
||||
reproduce Boogu-Image's reference-image and VLM preprocessing without requiring
|
||||
``pip install boogu-image``.
|
||||
|
||||
Only the two Boogu-specific methods are ported: ``get_new_height_width`` (the
|
||||
``max_pixels`` / ``max_side_length`` aware downscale-only resize) and
|
||||
``preprocess`` (which routes through it). Everything else is inherited from the
|
||||
stock diffusers ``VaeImageProcessor``.
|
||||
"""
|
||||
|
||||
import warnings
|
||||
|
||||
import numpy as np
|
||||
import PIL.Image
|
||||
import torch
|
||||
from diffusers.image_processor import (
|
||||
PipelineImageInput,
|
||||
VaeImageProcessor,
|
||||
is_valid_image_imagelist,
|
||||
)
|
||||
|
||||
|
||||
class BooguImageProcessor(VaeImageProcessor):
|
||||
"""VaeImageProcessor variant with Boogu-Image pixel/side-length constraints.
|
||||
|
||||
Resizing never upscales (the ratio is clamped to ``<= 1``) and always aligns
|
||||
the target height/width to multiples of ``vae_scale_factor``.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
do_resize: bool = True,
|
||||
vae_scale_factor: int = 16,
|
||||
resample: str = "lanczos",
|
||||
max_pixels: int | None = None,
|
||||
max_side_length: int | None = None,
|
||||
do_normalize: bool = True,
|
||||
do_binarize: bool = False,
|
||||
do_convert_rgb: bool = False,
|
||||
do_convert_grayscale: bool = False,
|
||||
):
|
||||
super().__init__(
|
||||
do_resize=do_resize,
|
||||
vae_scale_factor=vae_scale_factor,
|
||||
resample=resample,
|
||||
do_normalize=do_normalize,
|
||||
do_binarize=do_binarize,
|
||||
do_convert_rgb=do_convert_rgb,
|
||||
do_convert_grayscale=do_convert_grayscale,
|
||||
)
|
||||
self.max_pixels = max_pixels
|
||||
self.max_side_length = max_side_length
|
||||
|
||||
def get_new_height_width(
|
||||
self,
|
||||
image: PIL.Image.Image | np.ndarray | torch.Tensor,
|
||||
height: int | None = None,
|
||||
width: int | None = None,
|
||||
max_pixels: int | None = None,
|
||||
max_side_length: int | None = None,
|
||||
) -> tuple[int, int]:
|
||||
"""Return target ``(height, width)`` after downscale + alignment.
|
||||
|
||||
Faithful port of upstream ``BooguImageProcessor.get_new_height_width``.
|
||||
"""
|
||||
if height is None:
|
||||
if isinstance(image, PIL.Image.Image):
|
||||
height = image.height
|
||||
elif isinstance(image, torch.Tensor):
|
||||
height = image.shape[2]
|
||||
else:
|
||||
height = image.shape[1]
|
||||
|
||||
if width is None:
|
||||
if isinstance(image, PIL.Image.Image):
|
||||
width = image.width
|
||||
elif isinstance(image, torch.Tensor):
|
||||
width = image.shape[3]
|
||||
else:
|
||||
width = image.shape[2]
|
||||
|
||||
if max_side_length is None:
|
||||
max_side_length = self.max_side_length
|
||||
|
||||
if max_pixels is None:
|
||||
max_pixels = self.max_pixels
|
||||
|
||||
if height <= 0 or width <= 0:
|
||||
raise ValueError(f"Image height and width must be positive, got height={height}, width={width}")
|
||||
|
||||
max_side_length_ratio = 1.0
|
||||
if max_side_length is not None:
|
||||
if height > width:
|
||||
max_side_length_ratio = max_side_length / height
|
||||
else:
|
||||
max_side_length_ratio = max_side_length / width
|
||||
|
||||
cur_pixels = height * width
|
||||
max_pixels_ratio = (max_pixels / cur_pixels) ** 0.5 if max_pixels is not None else 1.0
|
||||
# Clamp ratio to <=1 to avoid upscaling input images in preprocessing.
|
||||
ratio = min(max_pixels_ratio, max_side_length_ratio, 1.0)
|
||||
|
||||
new_height, new_width = (
|
||||
int(height * ratio) // self.config.vae_scale_factor * self.config.vae_scale_factor,
|
||||
int(width * ratio) // self.config.vae_scale_factor * self.config.vae_scale_factor,
|
||||
)
|
||||
return new_height, new_width
|
||||
|
||||
def preprocess(
|
||||
self,
|
||||
image: PipelineImageInput,
|
||||
height: int | None = None,
|
||||
width: int | None = None,
|
||||
max_pixels: int | None = None,
|
||||
max_side_length: int | None = None,
|
||||
resize_mode: str = "default",
|
||||
crops_coords: tuple[int, int, int, int] | None = None,
|
||||
) -> torch.Tensor:
|
||||
"""Preprocess an image into a normalized ``[B, C, H, W]`` tensor.
|
||||
|
||||
Faithful port of upstream ``BooguImageProcessor.preprocess`` (PixArt-style
|
||||
downscale). Only the PIL branch is exercised by the native pipeline, but
|
||||
the numpy/tensor branches are kept for parity.
|
||||
"""
|
||||
supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor)
|
||||
|
||||
if self.config.do_convert_grayscale and isinstance(image, (torch.Tensor, np.ndarray)) and image.ndim == 3:
|
||||
if isinstance(image, torch.Tensor):
|
||||
image = image.unsqueeze(1)
|
||||
else:
|
||||
if image.shape[-1] == 1:
|
||||
image = np.expand_dims(image, axis=0)
|
||||
else:
|
||||
image = np.expand_dims(image, axis=-1)
|
||||
|
||||
if isinstance(image, list) and isinstance(image[0], np.ndarray) and image[0].ndim == 4:
|
||||
warnings.warn(
|
||||
"Passing `image` as a list of 4d np.ndarray is deprecated."
|
||||
"Please concatenate the list along the batch dimension and pass it as a single 4d np.ndarray",
|
||||
FutureWarning,
|
||||
)
|
||||
image = np.concatenate(image, axis=0)
|
||||
if isinstance(image, list) and isinstance(image[0], torch.Tensor) and image[0].ndim == 4:
|
||||
warnings.warn(
|
||||
"Passing `image` as a list of 4d torch.Tensor is deprecated."
|
||||
"Please concatenate the list along the batch dimension and pass it as a single 4d torch.Tensor",
|
||||
FutureWarning,
|
||||
)
|
||||
image = torch.cat(image, axis=0)
|
||||
|
||||
if not is_valid_image_imagelist(image):
|
||||
supported_formats_str = ", ".join(str(x) for x in supported_formats)
|
||||
raise ValueError(f"Input is in incorrect format. Currently, we only support {supported_formats_str}")
|
||||
|
||||
if not isinstance(image, list):
|
||||
image = [image]
|
||||
|
||||
if isinstance(image[0], PIL.Image.Image):
|
||||
if crops_coords is not None:
|
||||
image = [i.crop(crops_coords) for i in image]
|
||||
if self.config.do_resize:
|
||||
height, width = self.get_new_height_width(image[0], height, width, max_pixels, max_side_length)
|
||||
image = [self.resize(i, height, width, resize_mode=resize_mode) for i in image]
|
||||
if self.config.do_convert_rgb:
|
||||
image = [self.convert_to_rgb(i) for i in image]
|
||||
elif self.config.do_convert_grayscale:
|
||||
image = [self.convert_to_grayscale(i) for i in image]
|
||||
image = self.pil_to_numpy(image)
|
||||
image = self.numpy_to_pt(image)
|
||||
|
||||
elif isinstance(image[0], np.ndarray):
|
||||
image = np.concatenate(image, axis=0) if image[0].ndim == 4 else np.stack(image, axis=0)
|
||||
image = self.numpy_to_pt(image)
|
||||
height, width = self.get_new_height_width(image, height, width, max_pixels, max_side_length)
|
||||
if self.config.do_resize:
|
||||
image = self.resize(image, height, width)
|
||||
|
||||
elif isinstance(image[0], torch.Tensor):
|
||||
image = torch.cat(image, axis=0) if image[0].ndim == 4 else torch.stack(image, axis=0)
|
||||
|
||||
if self.config.do_convert_grayscale and image.ndim == 3:
|
||||
image = image.unsqueeze(1)
|
||||
|
||||
channel = image.shape[1]
|
||||
if channel == self.config.vae_latent_channels:
|
||||
return image
|
||||
|
||||
height, width = self.get_new_height_width(image, height, width, max_pixels, max_side_length)
|
||||
if self.config.do_resize:
|
||||
image = self.resize(image, height, width)
|
||||
|
||||
do_normalize = self.config.do_normalize
|
||||
if do_normalize and image.min() < 0:
|
||||
warnings.warn(
|
||||
"Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range "
|
||||
f"for image tensor is [0,1] when passing as pytorch tensor or numpy Array. You passed `image` with "
|
||||
f"value range [{image.min()},{image.max()}]",
|
||||
FutureWarning,
|
||||
)
|
||||
do_normalize = False
|
||||
if do_normalize:
|
||||
image = self.normalize(image)
|
||||
|
||||
if self.config.do_binarize:
|
||||
image = self.binarize(image)
|
||||
|
||||
return image
|
||||
@@ -0,0 +1,793 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
"""Native vLLM-Omni pipeline for Boogu-Image-0.1.
|
||||
|
||||
Ported from the upstream ``boogu`` package
|
||||
(``boogu/pipelines/boogu/pipeline_boogu.py``) with the following changes:
|
||||
|
||||
- Diffusers ``DiffusionPipeline``/``register_modules`` machinery replaced by a
|
||||
plain ``nn.Module`` constructed from ``OmniDiffusionConfig`` (components are
|
||||
loaded from the checkpoint subfolders; transformer weights arrive later via
|
||||
``weights_sources`` + ``load_weights``).
|
||||
- Upstream ``encode_instruction`` is exposed as ``encode_prompt`` (the
|
||||
vLLM-Omni convention, also hooked by the prompt-embed cache); only the
|
||||
text-to-image path is ported. Instruction rewriting, prompt tuning,
|
||||
reference-image (TI2I/I2I) encoding, double guidance, and vision-token
|
||||
stripping are not ported.
|
||||
- ``forward()`` (denoise loop + VAE decode) is intentionally not implemented
|
||||
yet; it lands together with the post-process registration.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from collections.abc import Iterable
|
||||
from typing import ClassVar, cast
|
||||
|
||||
import PIL.Image
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from diffusers.image_processor import VaeImageProcessor
|
||||
from diffusers.models.autoencoders.autoencoder_kl import AutoencoderKL
|
||||
from diffusers.utils.torch_utils import randn_tensor
|
||||
from torch import nn
|
||||
from transformers import Qwen3VLForConditionalGeneration, Qwen3VLProcessor
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.models.utils import AutoWeightsLoader
|
||||
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.utils import get_local_device
|
||||
from vllm_omni.diffusion.model_loader.diffusers_loader import DiffusersPipelineLoader
|
||||
from vllm_omni.diffusion.model_loader.hub_prefetch import from_pretrained_with_prefetch, prefetch_subfolders
|
||||
from vllm_omni.diffusion.models.boogu_image.boogu_image_transformer import (
|
||||
BooguImageDoubleStreamRotaryPosEmbed,
|
||||
BooguImageTransformer2DModel,
|
||||
)
|
||||
from vllm_omni.diffusion.models.boogu_image.image_processor import BooguImageProcessor
|
||||
from vllm_omni.diffusion.models.boogu_image.scheduling_flow_match_euler_discrete_time_shifting import (
|
||||
FlowMatchEulerDiscreteScheduler,
|
||||
)
|
||||
from vllm_omni.diffusion.models.interface import SupportImageInput, SupportsComponentDiscovery
|
||||
from vllm_omni.diffusion.models.progress_bar import ProgressBarMixin
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.worker.request_batch import DiffusionRequestBatch
|
||||
from vllm_omni.model_executor.model_loader.weight_utils import download_weights_from_hf_specific
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
# Reference-image preprocessing limits (upstream ``BooguImagePipeline.__call__``
|
||||
# defaults). The VLM copy is aggressively downscaled for the Qwen3VL encoder;
|
||||
# the VAE copy keeps near-native resolution for the reference latents.
|
||||
_MAX_VLM_INPUT_PIL_PIXELS = 384 * 384
|
||||
_MAX_VLM_INPUT_PIL_SIDE_LENGTH = 384 * 2
|
||||
_MAX_INPUT_IMAGE_PIXELS = 2048 * 2048
|
||||
_MAX_INPUT_IMAGE_SIDE_LENGTH = 2048 * 2
|
||||
|
||||
|
||||
def _load_vae_scale_factor(model_path: str) -> int:
|
||||
vae_config_path = os.path.join(model_path, "vae/config.json")
|
||||
try:
|
||||
with open(vae_config_path) as f:
|
||||
vae_config = json.load(f)
|
||||
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
|
||||
raise RuntimeError(f"Failed to load Boogu VAE config from {vae_config_path}: {exc}") from exc
|
||||
|
||||
if "block_out_channels" not in vae_config:
|
||||
return 8
|
||||
return 2 ** (len(vae_config["block_out_channels"]) - 1)
|
||||
|
||||
|
||||
def get_boogu_image_post_process_func(od_config: OmniDiffusionConfig):
|
||||
"""Build the post-process callable that converts decoded tensors to images.
|
||||
|
||||
Upstream ``BooguImageProcessor`` only customizes *pre*-processing; the
|
||||
``postprocess`` path is inherited from the stock diffusers
|
||||
``VaeImageProcessor``, so we reuse it directly here.
|
||||
"""
|
||||
model_name = od_config.model
|
||||
if os.path.exists(model_name):
|
||||
model_path = model_name
|
||||
else:
|
||||
model_path = download_weights_from_hf_specific(model_name, None, ["*"])
|
||||
|
||||
vae_scale_factor = _load_vae_scale_factor(model_path)
|
||||
|
||||
image_processor = VaeImageProcessor(vae_scale_factor=vae_scale_factor * 2)
|
||||
|
||||
def post_process_func(images: torch.Tensor):
|
||||
return image_processor.postprocess(images)
|
||||
|
||||
return post_process_func
|
||||
|
||||
|
||||
def get_boogu_image_pre_process_func(od_config: OmniDiffusionConfig):
|
||||
"""Build the pre-process callable for Boogu-Image reference (edit) input.
|
||||
|
||||
Text-to-image requests carry no image and are passed through unchanged (the
|
||||
Base checkpoint shares this pipeline class). Edit (TI2I) requests carry a
|
||||
single reference PIL image on ``prompt["multi_modal_data"]["image"]``; it is
|
||||
resized twice — once for the Qwen3VL encoder (``prompt_image``) and once for
|
||||
the VAE reference latents (``preprocessed_image``) — and stashed in
|
||||
``additional_information`` for ``forward`` to consume. Mirrors upstream
|
||||
``preprocess_vlm_input_pil_images`` + ``prepare_image``.
|
||||
|
||||
For a single reference image, upstream ``align_res`` (default ``True``)
|
||||
derives the output resolution from the VAE-encoded reference dimensions, so
|
||||
the request height/width are overwritten accordingly.
|
||||
"""
|
||||
model_name = od_config.model
|
||||
if os.path.exists(model_name):
|
||||
model_path = model_name
|
||||
else:
|
||||
model_path = download_weights_from_hf_specific(model_name, None, ["*"])
|
||||
|
||||
vae_scale_factor = _load_vae_scale_factor(model_path)
|
||||
|
||||
# Upstream builds ``BooguImageProcessor(vae_scale_factor=vae_scale_factor*2)``
|
||||
# so all resize targets align to multiples of ``vae_scale_factor * 2``.
|
||||
image_processor = BooguImageProcessor(vae_scale_factor=vae_scale_factor * 2, do_resize=True)
|
||||
|
||||
def pre_process_func(request: OmniDiffusionRequest):
|
||||
prompt = request.prompt
|
||||
if isinstance(prompt, str):
|
||||
# Plain-text prompt cannot carry an image -> text-to-image, no-op.
|
||||
return request
|
||||
|
||||
multi_modal_data = prompt.get("multi_modal_data") or {}
|
||||
raw_image = multi_modal_data.get("image")
|
||||
if not raw_image:
|
||||
# No reference image -> text-to-image, no-op (Base checkpoint).
|
||||
return request
|
||||
|
||||
if isinstance(raw_image, list):
|
||||
if len(raw_image) > 1:
|
||||
raise ValueError(f"Boogu-Image editing supports a single reference image; received {len(raw_image)}.")
|
||||
raw_image = raw_image[0]
|
||||
|
||||
if isinstance(raw_image, str):
|
||||
image = PIL.Image.open(raw_image)
|
||||
else:
|
||||
image = cast(PIL.Image.Image, raw_image)
|
||||
image = image.convert("RGB")
|
||||
|
||||
if "additional_information" not in prompt:
|
||||
prompt["additional_information"] = {}
|
||||
|
||||
# VLM-resized copy (PIL) for the Qwen3VL instruction encoder.
|
||||
vlm_height, vlm_width = image_processor.get_new_height_width(
|
||||
image, None, None, _MAX_VLM_INPUT_PIL_PIXELS, _MAX_VLM_INPUT_PIL_SIDE_LENGTH
|
||||
)
|
||||
prompt_image = image_processor.resize(image, vlm_height, vlm_width)
|
||||
|
||||
# VAE-ready copy (normalized [1, C, H, W] tensor) for reference latents.
|
||||
preprocessed_image = image_processor.preprocess(
|
||||
image, max_pixels=_MAX_INPUT_IMAGE_PIXELS, max_side_length=_MAX_INPUT_IMAGE_SIDE_LENGTH
|
||||
)
|
||||
|
||||
# align_res: single-image output resolution follows the reference dims.
|
||||
request.sampling_params.height = int(preprocessed_image.shape[-2])
|
||||
request.sampling_params.width = int(preprocessed_image.shape[-1])
|
||||
|
||||
prompt["additional_information"]["preprocessed_image"] = preprocessed_image
|
||||
prompt["additional_information"]["prompt_image"] = prompt_image
|
||||
request.prompt = prompt
|
||||
return request
|
||||
|
||||
return pre_process_func
|
||||
|
||||
|
||||
# System prompts matching upstream dataset logic (ported verbatim from
|
||||
# ``BooguImagePipeline.__init__``).
|
||||
SYSTEM_PROMPT_4_TI2I_UNIFIED = (
|
||||
"Describe the key features of the input image (color, shape, size, texture, objects, background), "
|
||||
"then explain how the user's text instruction should alter or modify the image. Generate a new image "
|
||||
"that meets the user's requirements while maintaining consistency with the original input where appropriate."
|
||||
)
|
||||
SYSTEM_PROMPT_4_T2I_UNIFIED = (
|
||||
"You are a helpful assistant that generates high-quality images based on user instructions. "
|
||||
"The instructions are as follows."
|
||||
)
|
||||
|
||||
|
||||
class BooguImagePipeline(nn.Module, ProgressBarMixin, SupportsComponentDiscovery, SupportImageInput):
|
||||
"""Boogu-Image text-to-image and image-editing (TI2I) pipeline.
|
||||
|
||||
Native vLLM-Omni implementation. A request with a reference image (edit /
|
||||
TI2I) is served by the same class as text-to-image; the reference latents
|
||||
and Qwen3VL image tokens are threaded through ``forward`` and the ported
|
||||
transformer's reference-image refiner path.
|
||||
"""
|
||||
|
||||
supports_request_batch = False
|
||||
|
||||
support_image_input: ClassVar[bool] = True
|
||||
color_format: ClassVar[str] = "RGB"
|
||||
|
||||
_dit_modules: ClassVar[list[str]] = ["transformer"]
|
||||
_encoder_modules: ClassVar[list[str]] = ["mllm"]
|
||||
_vae_modules: ClassVar[list[str]] = ["vae"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
od_config: OmniDiffusionConfig,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.od_config = od_config
|
||||
self._raise_unsupported_features()
|
||||
self.weights_sources = [
|
||||
DiffusersPipelineLoader.ComponentSource(
|
||||
model_or_path=od_config.model,
|
||||
subfolder="transformer",
|
||||
revision=None,
|
||||
prefix="transformer.",
|
||||
fall_back_to_pt=True,
|
||||
)
|
||||
]
|
||||
|
||||
self._execution_device = get_local_device()
|
||||
model = od_config.model
|
||||
local_files_only = os.path.exists(model)
|
||||
|
||||
# See ``hub_prefetch.py`` for the transformers v5 multi-worker subfolder
|
||||
# race; prefetch the whole component set before any from_pretrained.
|
||||
boogu_subfolders = ["scheduler", "vae", "mllm", "processor"]
|
||||
prefetch_subfolders(model, boogu_subfolders, local_files_only=local_files_only)
|
||||
|
||||
self.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
|
||||
model, subfolder="scheduler", local_files_only=local_files_only
|
||||
)
|
||||
|
||||
mllm = from_pretrained_with_prefetch(
|
||||
Qwen3VLForConditionalGeneration.from_pretrained,
|
||||
model,
|
||||
subfolder="mllm",
|
||||
prefetch_list=boogu_subfolders,
|
||||
local_files_only=local_files_only,
|
||||
torch_dtype=od_config.dtype,
|
||||
)
|
||||
# Upstream reuses the full VLM as an optional instruction rewriter and
|
||||
# encodes with its inner model (no ``lm_head``); the rewriter is not
|
||||
# ported, so keep only the inner ``Qwen3VLModel`` as the encoder.
|
||||
if hasattr(mllm, "lm_head"):
|
||||
mllm = mllm.model
|
||||
self.mllm = mllm.to(self._execution_device)
|
||||
|
||||
self.processor = Qwen3VLProcessor.from_pretrained(
|
||||
model, subfolder="processor", local_files_only=local_files_only
|
||||
)
|
||||
|
||||
self.vae = from_pretrained_with_prefetch(
|
||||
AutoencoderKL.from_pretrained,
|
||||
model,
|
||||
subfolder="vae",
|
||||
prefetch_list=boogu_subfolders,
|
||||
local_files_only=local_files_only,
|
||||
).to(self._execution_device)
|
||||
|
||||
self.transformer = BooguImageTransformer2DModel(od_config=od_config)
|
||||
|
||||
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1) if getattr(self, "vae", None) else 8
|
||||
self.default_sample_size = 128
|
||||
|
||||
self.SYSTEM_PROMPT_4_T2I = SYSTEM_PROMPT_4_T2I_UNIFIED
|
||||
# Upstream uses the TI2I prompt for empty instructions (the default
|
||||
# negative prompt "" hits this path).
|
||||
self.SYSTEM_PROMPT_DROP = SYSTEM_PROMPT_4_TI2I_UNIFIED
|
||||
# Edit (TI2I / I2I) system prompts (image present in the chat template).
|
||||
self.SYSTEM_PROMPT_4_TI2I = SYSTEM_PROMPT_4_TI2I_UNIFIED
|
||||
self.SYSTEM_PROMPT_4_I2I = SYSTEM_PROMPT_4_TI2I_UNIFIED
|
||||
|
||||
def _raise_unsupported_features(self) -> None:
|
||||
"""Reject execution modes that do not have Boogu-specific support."""
|
||||
parallel_config = self.od_config.parallel_config
|
||||
if parallel_config.tensor_parallel_size > 1:
|
||||
raise NotImplementedError("Tensor parallelism is not supported by BooguImagePipeline.")
|
||||
if (parallel_config.sequence_parallel_size or 1) > 1:
|
||||
raise NotImplementedError("Sequence parallelism is not supported by BooguImagePipeline.")
|
||||
if parallel_config.cfg_parallel_size > 1:
|
||||
raise NotImplementedError("CFG parallelism is not supported by BooguImagePipeline.")
|
||||
if parallel_config.use_hsdp:
|
||||
raise NotImplementedError("HSDP is not supported by BooguImagePipeline.")
|
||||
if self.od_config.cache_backend not in (None, "", "none"):
|
||||
raise NotImplementedError(
|
||||
f"Cache backend '{self.od_config.cache_backend}' is not supported by BooguImagePipeline."
|
||||
)
|
||||
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
|
||||
loader = AutoWeightsLoader(self)
|
||||
return loader.load_weights(weights)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Prompt encoding (upstream ``encode_instruction``, t2i path)
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _apply_chat_template(
|
||||
self,
|
||||
instruction: str,
|
||||
input_pil_images: list[PIL.Image.Image] | None = None,
|
||||
) -> list[dict]:
|
||||
"""Build the chat messages for one instruction (text-to-image or edit).
|
||||
|
||||
Mirrors upstream ``_apply_chat_template`` (``system_prompt_follows_task_type``
|
||||
is always ``False`` here): the system prompt is picked by whether images
|
||||
are present and whether the instruction is empty, and reference images
|
||||
are placed *before* the instruction text in the user turn.
|
||||
"""
|
||||
user_text_content = [{"type": "text", "text": instruction}]
|
||||
|
||||
has_images = input_pil_images is not None and len(input_pil_images) > 0
|
||||
instruction_empty = instruction is None or len(instruction.strip()) == 0
|
||||
|
||||
if not has_images:
|
||||
system_prompt = self.SYSTEM_PROMPT_DROP if instruction_empty else self.SYSTEM_PROMPT_4_T2I
|
||||
else:
|
||||
system_prompt = self.SYSTEM_PROMPT_4_I2I if instruction_empty else self.SYSTEM_PROMPT_4_TI2I
|
||||
|
||||
system_role = {"role": "system", "content": [{"type": "text", "text": system_prompt}]}
|
||||
if not has_images:
|
||||
return [system_role, {"role": "user", "content": user_text_content}]
|
||||
|
||||
images_content = [{"type": "image", "image": pil_img} for pil_img in input_pil_images]
|
||||
return [system_role, {"role": "user", "content": images_content + user_text_content}]
|
||||
|
||||
def _get_instruction_feature_embeds(
|
||||
self,
|
||||
instruction: str | list[str],
|
||||
input_pil_images: list[list[PIL.Image.Image] | None] | None = None,
|
||||
device: torch.device | None = None,
|
||||
max_sequence_length: int = 256,
|
||||
truncate_instruction_sequence: bool = False,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Encode instructions (and optional reference images) with Qwen3VL.
|
||||
|
||||
``input_pil_images`` is a per-sample list (outer length == batch size);
|
||||
each entry is the sample's already-VLM-resized reference images or
|
||||
``None``. Returns the last hidden state (or the last-N layers as a list
|
||||
when the transformer config asks for more than one) and the attention
|
||||
mask.
|
||||
"""
|
||||
device = device or self._execution_device
|
||||
instruction = [instruction] if isinstance(instruction, str) else instruction
|
||||
|
||||
if input_pil_images is None:
|
||||
per_sample_images: list[list[PIL.Image.Image] | None] = [None] * len(instruction)
|
||||
else:
|
||||
assert len(input_pil_images) == len(instruction), (
|
||||
"`input_pil_images` outer length must match the instruction batch size."
|
||||
)
|
||||
per_sample_images = input_pil_images
|
||||
|
||||
prompts = [self._apply_chat_template(text, per_sample_images[i]) for i, text in enumerate(instruction)]
|
||||
|
||||
vlm_inputs = self.processor.apply_chat_template(
|
||||
prompts,
|
||||
padding="longest",
|
||||
max_length=max_sequence_length,
|
||||
truncation=truncate_instruction_sequence,
|
||||
padding_side="right",
|
||||
return_tensors="pt",
|
||||
tokenize=True,
|
||||
return_dict=True,
|
||||
)
|
||||
for k in vlm_inputs.keys():
|
||||
if isinstance(vlm_inputs[k], torch.Tensor):
|
||||
vlm_inputs[k] = vlm_inputs[k].to(device)
|
||||
|
||||
final_instruction_mask = vlm_inputs["attention_mask"]
|
||||
|
||||
num_instruction_feature_layers = self.transformer.instruction_feature_configs.get(
|
||||
"num_instruction_feature_layers", 1
|
||||
)
|
||||
|
||||
with torch.no_grad():
|
||||
text_encoder_outputs = self.mllm(**vlm_inputs, output_hidden_states=True, return_dict=True)
|
||||
if num_instruction_feature_layers > 1:
|
||||
instruction_feats = list(text_encoder_outputs.hidden_states)[-num_instruction_feature_layers:]
|
||||
else:
|
||||
instruction_feats = text_encoder_outputs.hidden_states[-1]
|
||||
|
||||
dtype = self.mllm.dtype if self.mllm is not None else self.transformer.dtype
|
||||
|
||||
if isinstance(instruction_feats, (list, tuple)):
|
||||
final_instruction_feats = [feat.to(dtype=dtype, device=device) for feat in instruction_feats]
|
||||
else:
|
||||
final_instruction_feats = instruction_feats.to(dtype=dtype, device=device)
|
||||
final_instruction_mask = final_instruction_mask.to(device=device)
|
||||
|
||||
return final_instruction_feats, final_instruction_mask
|
||||
|
||||
def _reshape_embeds_and_mask(self, embeds, mask, num_images_per_prompt: int):
|
||||
"""Duplicate embeddings/mask for each generation per prompt (mps-friendly)."""
|
||||
if isinstance(embeds, (list, tuple)):
|
||||
batch_size, seq_len, _ = embeds[0].shape
|
||||
reshaped_embeds = []
|
||||
for embed in embeds:
|
||||
embed = embed.repeat(1, num_images_per_prompt, 1)
|
||||
reshaped_embeds.append(embed.view(batch_size * num_images_per_prompt, seq_len, -1))
|
||||
else:
|
||||
batch_size, seq_len, _ = embeds.shape
|
||||
embeds = embeds.repeat(1, num_images_per_prompt, 1)
|
||||
reshaped_embeds = embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
|
||||
|
||||
mask = mask.repeat(num_images_per_prompt, 1)
|
||||
reshaped_mask = mask.view(batch_size * num_images_per_prompt, -1)
|
||||
|
||||
return batch_size, seq_len, reshaped_embeds, reshaped_mask
|
||||
|
||||
def encode_prompt(
|
||||
self,
|
||||
prompt: str | list[str],
|
||||
do_classifier_free_guidance: bool = True,
|
||||
negative_prompt: str | list[str] | None = None,
|
||||
num_images_per_prompt: int = 1,
|
||||
device: torch.device | None = None,
|
||||
prompt_embeds: torch.Tensor | None = None,
|
||||
negative_prompt_embeds: torch.Tensor | None = None,
|
||||
prompt_attention_mask: torch.Tensor | None = None,
|
||||
negative_prompt_attention_mask: torch.Tensor | None = None,
|
||||
max_sequence_length: int = 1280,
|
||||
truncate_instruction_sequence: bool = False,
|
||||
input_images: list[list[PIL.Image.Image] | None] | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None]:
|
||||
"""Encode prompt (and negative prompt for CFG) into Qwen3VL hidden states.
|
||||
|
||||
Port of upstream ``encode_instruction`` for text-to-image and the
|
||||
text-guided image-editing (TI2I) path. Reference images are attached to
|
||||
the *positive* instruction only (upstream default
|
||||
``use_input_images_4_neg_instruct=False``). Instruction rewriting,
|
||||
prompt tuning, and double-guidance empty instructions are not ported.
|
||||
The default ``max_sequence_length`` matches the upstream ``__call__``
|
||||
default (1280), not the upstream ``encode_instruction`` default (256).
|
||||
|
||||
Args:
|
||||
input_images: Per-sample list (outer length == batch size) of
|
||||
already-VLM-resized reference images, or ``None`` for pure
|
||||
text-to-image.
|
||||
|
||||
Returns:
|
||||
``(prompt_embeds, prompt_attention_mask, negative_prompt_embeds,
|
||||
negative_prompt_attention_mask)`` where each embeds tensor has shape
|
||||
``[batch_size * num_images_per_prompt, seq_len, dim]``. The negative
|
||||
pair is ``None`` when ``do_classifier_free_guidance`` is off and no
|
||||
precomputed negative embeddings were passed.
|
||||
"""
|
||||
device = device or self._execution_device
|
||||
|
||||
prompt = [prompt] if isinstance(prompt, str) else prompt
|
||||
|
||||
if prompt_embeds is None:
|
||||
prompt_embeds, prompt_attention_mask = self._get_instruction_feature_embeds(
|
||||
instruction=prompt,
|
||||
input_pil_images=input_images,
|
||||
device=device,
|
||||
max_sequence_length=max_sequence_length,
|
||||
truncate_instruction_sequence=truncate_instruction_sequence,
|
||||
)
|
||||
|
||||
batch_size, _, prompt_embeds, prompt_attention_mask = self._reshape_embeds_and_mask(
|
||||
prompt_embeds, prompt_attention_mask, num_images_per_prompt
|
||||
)
|
||||
|
||||
if do_classifier_free_guidance and negative_prompt_embeds is None:
|
||||
negative_prompt = negative_prompt if negative_prompt is not None else ""
|
||||
negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt
|
||||
|
||||
if batch_size != len(negative_prompt):
|
||||
raise ValueError(
|
||||
f"`negative_prompt` has batch size {len(negative_prompt)}, but `prompt` has"
|
||||
f" batch size {batch_size}. Please make sure that passed `negative_prompt`"
|
||||
" matches the batch size of `prompt`."
|
||||
)
|
||||
|
||||
negative_prompt_embeds, negative_prompt_attention_mask = self._get_instruction_feature_embeds(
|
||||
instruction=negative_prompt,
|
||||
device=device,
|
||||
max_sequence_length=max_sequence_length,
|
||||
truncate_instruction_sequence=truncate_instruction_sequence,
|
||||
)
|
||||
|
||||
_, _, negative_prompt_embeds, negative_prompt_attention_mask = self._reshape_embeds_and_mask(
|
||||
negative_prompt_embeds, negative_prompt_attention_mask, num_images_per_prompt
|
||||
)
|
||||
|
||||
return (
|
||||
prompt_embeds,
|
||||
prompt_attention_mask,
|
||||
negative_prompt_embeds,
|
||||
negative_prompt_attention_mask,
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Denoise loop + VAE decode (upstream ``__call__`` / ``processing``, t2i)
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
|
||||
"""Sample initial noise latents (upstream ``prepare_latents``)."""
|
||||
height = int(height) // self.vae_scale_factor
|
||||
width = int(width) // self.vae_scale_factor
|
||||
shape = (batch_size, num_channels_latents, height, width)
|
||||
if latents is None:
|
||||
latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
|
||||
else:
|
||||
latents = latents.to(device)
|
||||
return latents
|
||||
|
||||
def _resolve_output_size(self, height, width):
|
||||
"""t2i branch of upstream ``_resolve_output_and_original_size``.
|
||||
|
||||
Clamps the working resolution to ``max_input_image_pixels`` (2048**2),
|
||||
rounding down to a multiple of ``vae_scale_factor * 2``; the requested
|
||||
size is remembered so the decoded image can be resized back.
|
||||
"""
|
||||
img_scale_num = self.vae_scale_factor * 2
|
||||
ori_height, ori_width = height, width
|
||||
max_pixels = 2048 * 2048
|
||||
cur_pixels = height * width
|
||||
ratio = min((max_pixels / cur_pixels) ** 0.5, 1.0)
|
||||
height = int(height * ratio) // img_scale_num * img_scale_num
|
||||
width = int(width * ratio) // img_scale_num * img_scale_num
|
||||
return height, width, ori_height, ori_width
|
||||
|
||||
def predict(
|
||||
self, t, latents, instruction_embeds, freqs_cis, instruction_attention_mask, ref_image_hidden_states=None
|
||||
):
|
||||
"""One transformer velocity prediction (upstream ``predict``).
|
||||
|
||||
``ref_image_hidden_states`` is ``None`` for text-to-image, or the
|
||||
per-sample reference latents (``list[list[Tensor[C, H, W]]]``) for the
|
||||
image-editing path.
|
||||
"""
|
||||
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
||||
return self.transformer(
|
||||
latents,
|
||||
timestep,
|
||||
instruction_embeds,
|
||||
freqs_cis,
|
||||
instruction_attention_mask,
|
||||
ref_image_hidden_states=ref_image_hidden_states,
|
||||
)
|
||||
|
||||
def _encode_vae_image(self, img: torch.Tensor, generator=None) -> torch.Tensor:
|
||||
"""Encode an image tensor into the VAE latent space (upstream ``encode_vae``).
|
||||
|
||||
Upstream leaves ``latent_dist.sample()`` unseeded; the native path
|
||||
threads the request generator through so a fixed seed gives a
|
||||
reproducible reference latent.
|
||||
"""
|
||||
z0 = self.vae.encode(img.to(dtype=self.vae.dtype)).latent_dist.sample(generator=generator)
|
||||
if self.vae.config.shift_factor is not None:
|
||||
z0 = z0 - self.vae.config.shift_factor
|
||||
if self.vae.config.scaling_factor is not None:
|
||||
z0 = z0 * self.vae.config.scaling_factor
|
||||
return z0.to(dtype=self.vae.dtype)
|
||||
|
||||
def _build_ref_latents(
|
||||
self,
|
||||
preprocessed_images: list[torch.Tensor | None],
|
||||
num_images_per_prompt: int,
|
||||
device: torch.device,
|
||||
generator=None,
|
||||
) -> list[list[torch.Tensor] | None]:
|
||||
"""VAE-encode per-sample reference images into the transformer's format.
|
||||
|
||||
Mirrors upstream ``prepare_image``: returns a list of length
|
||||
``batch_size * num_images_per_prompt`` where each entry is either
|
||||
``None`` (no reference / text-to-image) or a list of ``[C, H, W]``
|
||||
reference latents (one per reference image). Boogu editing uses a single
|
||||
reference image, so each non-empty entry is a one-element list.
|
||||
"""
|
||||
# ``latent_dist.sample`` accepts only a single generator; a per-output
|
||||
# generator list (num_outputs_per_prompt > 1) falls back to unseeded.
|
||||
vae_generator = generator if isinstance(generator, torch.Generator) else None
|
||||
|
||||
ref_latents: list[list[torch.Tensor] | None] = []
|
||||
for image in preprocessed_images:
|
||||
if image is None:
|
||||
sample_latents: list[torch.Tensor] | None = None
|
||||
else:
|
||||
latent = self._encode_vae_image(image.to(device=device), generator=vae_generator).squeeze(0)
|
||||
sample_latents = [latent]
|
||||
for _ in range(num_images_per_prompt):
|
||||
ref_latents.append(sample_latents)
|
||||
return ref_latents
|
||||
|
||||
@staticmethod
|
||||
def _extract_reference_images(
|
||||
prompts: list,
|
||||
) -> tuple[list[PIL.Image.Image | None], list[torch.Tensor | None]]:
|
||||
"""Pull per-sample reference images out of ``additional_information``.
|
||||
|
||||
Returns ``(prompt_images, preprocessed_images)`` where entries are
|
||||
``None`` for pure text-to-image samples. Populated by
|
||||
:func:`get_boogu_image_pre_process_func`.
|
||||
"""
|
||||
prompt_images: list[PIL.Image.Image | None] = []
|
||||
preprocessed_images: list[torch.Tensor | None] = []
|
||||
for p in prompts:
|
||||
if isinstance(p, str):
|
||||
prompt_images.append(None)
|
||||
preprocessed_images.append(None)
|
||||
continue
|
||||
ai = p.get("additional_information") or {}
|
||||
prompt_images.append(ai.get("prompt_image"))
|
||||
preprocessed_images.append(ai.get("preprocessed_image"))
|
||||
return prompt_images, preprocessed_images
|
||||
|
||||
def forward(self, req: DiffusionRequestBatch) -> DiffusionOutput:
|
||||
# Prompt / negative-prompt extraction (mirrors the Ovis pattern; the
|
||||
# online API sometimes passes ``{"negative_prompt": None}``).
|
||||
prompt = [p if isinstance(p, str) else (p.get("prompt") or "") for p in req.prompts]
|
||||
if all(isinstance(p, str) or p.get("negative_prompt") is None for p in req.prompts):
|
||||
negative_prompt = None
|
||||
else:
|
||||
negative_prompt = ["" if isinstance(p, str) else (p.get("negative_prompt") or "") for p in req.prompts]
|
||||
|
||||
# Reference (edit / TI2I) images, if any.
|
||||
prompt_images, preprocessed_images = self._extract_reference_images(req.prompts)
|
||||
has_reference = any(img is not None for img in preprocessed_images)
|
||||
task_type = "ti2i" if has_reference else "t2i"
|
||||
|
||||
sp = req.sampling_params
|
||||
device = self._execution_device
|
||||
|
||||
height = sp.height or self.default_sample_size * self.vae_scale_factor
|
||||
width = sp.width or self.default_sample_size * self.vae_scale_factor
|
||||
num_inference_steps = sp.num_inference_steps or 50
|
||||
# Upstream default text guidance is 4.0; the engine coerces an unset
|
||||
# guidance_scale to 1.0, so only honor a caller-provided value.
|
||||
text_guidance_scale = sp.guidance_scale if sp.guidance_scale_provided else 4.0
|
||||
# Image guidance rides on ``guidance_scale_2`` (upstream default 1.0 =
|
||||
# off); only a caller-provided value enables the double-guidance path.
|
||||
image_guidance_scale = sp.guidance_scale_2 if sp.guidance_scale_2_provided else 1.0
|
||||
if not has_reference:
|
||||
image_guidance_scale = 1.0
|
||||
num_images_per_prompt = sp.num_outputs_per_prompt if sp.num_outputs_per_prompt > 0 else 1
|
||||
generator = sp.generator
|
||||
max_sequence_length = sp.max_sequence_length or 1280
|
||||
output_type = sp.output_type or "pil"
|
||||
cfg_range = (0.0, 1.0)
|
||||
|
||||
# Negative instruction embeddings are needed whenever text guidance is
|
||||
# active (t2i text CFG, ti2i text-only, and ti2i double guidance).
|
||||
do_classifier_free_guidance = text_guidance_scale > 1.0
|
||||
|
||||
batch_size = len(prompt)
|
||||
|
||||
# Per-sample VLM reference images for the positive instruction only
|
||||
# (upstream default ``use_input_images_4_neg_instruct=False``).
|
||||
input_images = None
|
||||
if has_reference:
|
||||
input_images = [[img] if img is not None else None for img in prompt_images]
|
||||
|
||||
# 1. Encode prompts.
|
||||
(
|
||||
instruction_embeds,
|
||||
instruction_attention_mask,
|
||||
negative_instruction_embeds,
|
||||
negative_instruction_attention_mask,
|
||||
) = self.encode_prompt(
|
||||
prompt,
|
||||
do_classifier_free_guidance=do_classifier_free_guidance,
|
||||
negative_prompt=negative_prompt,
|
||||
num_images_per_prompt=num_images_per_prompt,
|
||||
device=device,
|
||||
max_sequence_length=max_sequence_length,
|
||||
input_images=input_images,
|
||||
)
|
||||
|
||||
# 2. Resolve working / output resolution.
|
||||
height, width, ori_height, ori_width = self._resolve_output_size(height, width)
|
||||
|
||||
# 3. Reference latents (edit path) and initial noise latents.
|
||||
dtype = self.vae.dtype
|
||||
latent_channels = self.transformer.in_channels
|
||||
ref_latents = None
|
||||
if has_reference:
|
||||
ref_latents = self._build_ref_latents(preprocessed_images, num_images_per_prompt, device, generator)
|
||||
|
||||
latents = self.prepare_latents(
|
||||
batch_size * num_images_per_prompt,
|
||||
latent_channels,
|
||||
height,
|
||||
width,
|
||||
instruction_embeds.dtype,
|
||||
device,
|
||||
generator,
|
||||
)
|
||||
|
||||
freqs_cis = BooguImageDoubleStreamRotaryPosEmbed.get_freqs_cis(
|
||||
self.transformer.axes_dim_rope,
|
||||
self.transformer.axes_lens,
|
||||
theta=10000,
|
||||
)
|
||||
|
||||
# 4. Timesteps (the ported scheduler consumes ``num_tokens``).
|
||||
num_tokens = latents.shape[-2] * latents.shape[-1]
|
||||
self.scheduler.set_timesteps(num_inference_steps, device=device, num_tokens=num_tokens)
|
||||
timesteps = self.scheduler.timesteps
|
||||
num_timesteps = len(timesteps)
|
||||
|
||||
# 5. Denoise loop with (sequential) classifier-free guidance. Reproduces
|
||||
# the branch priority of upstream ``processing`` (double > text-only >
|
||||
# image-only > t2i text). Reference latents are kept in the conditional
|
||||
# (and, for text-only ti2i, the unconditional) predictions.
|
||||
with self.progress_bar(total=num_timesteps) as progress_bar:
|
||||
for i, t in enumerate(timesteps):
|
||||
in_cfg_range = cfg_range[0] <= i / num_timesteps <= cfg_range[1]
|
||||
text_gs = text_guidance_scale if in_cfg_range else 1.0
|
||||
image_gs = image_guidance_scale if in_cfg_range else 1.0
|
||||
|
||||
model_pred = self.predict(
|
||||
t, latents, instruction_embeds, freqs_cis, instruction_attention_mask, ref_latents
|
||||
)
|
||||
|
||||
if task_type == "ti2i" and text_gs > 1.0 and image_gs > 1.0:
|
||||
# Double guidance: 3 predictions (cond+ref, neg+ref, neg+no-ref).
|
||||
model_pred_drop_text = self.predict(
|
||||
t,
|
||||
latents,
|
||||
negative_instruction_embeds,
|
||||
freqs_cis,
|
||||
negative_instruction_attention_mask,
|
||||
ref_latents,
|
||||
)
|
||||
model_pred_drop_all = self.predict(
|
||||
t,
|
||||
latents,
|
||||
negative_instruction_embeds,
|
||||
freqs_cis,
|
||||
negative_instruction_attention_mask,
|
||||
None,
|
||||
)
|
||||
delta_text = model_pred - model_pred_drop_text
|
||||
delta_image = model_pred_drop_text - model_pred_drop_all
|
||||
model_pred = model_pred + (text_gs - 1) * delta_text + (image_gs - 1) * delta_image
|
||||
elif task_type == "ti2i" and text_gs > 1.0:
|
||||
# Text-only ti2i guidance: reference kept in the uncond pred.
|
||||
model_pred_drop_text = self.predict(
|
||||
t,
|
||||
latents,
|
||||
negative_instruction_embeds,
|
||||
freqs_cis,
|
||||
negative_instruction_attention_mask,
|
||||
ref_latents,
|
||||
)
|
||||
model_pred = model_pred + (text_gs - 1) * (model_pred - model_pred_drop_text)
|
||||
elif task_type == "ti2i" and image_gs > 1.0:
|
||||
# Image-only ti2i guidance: drop the reference in the uncond pred.
|
||||
model_pred_drop_image = self.predict(
|
||||
t, latents, instruction_embeds, freqs_cis, instruction_attention_mask, None
|
||||
)
|
||||
model_pred = model_pred + (image_gs - 1) * (model_pred - model_pred_drop_image)
|
||||
elif text_gs > 1.0:
|
||||
# Text-to-image classifier-free guidance.
|
||||
model_pred_drop_all = self.predict(
|
||||
t,
|
||||
latents,
|
||||
negative_instruction_embeds,
|
||||
freqs_cis,
|
||||
negative_instruction_attention_mask,
|
||||
None,
|
||||
)
|
||||
model_pred = model_pred + (text_gs - 1) * (model_pred - model_pred_drop_all)
|
||||
|
||||
latents = self.scheduler.step(model_pred, t, latents, return_dict=False)[0]
|
||||
latents = latents.to(dtype=instruction_embeds.dtype)
|
||||
progress_bar.update()
|
||||
|
||||
# 6. Decode.
|
||||
if output_type == "latent":
|
||||
image = latents
|
||||
else:
|
||||
latents = latents.to(dtype=dtype)
|
||||
if self.vae.config.scaling_factor is not None:
|
||||
latents = latents / self.vae.config.scaling_factor
|
||||
if self.vae.config.shift_factor is not None:
|
||||
latents = latents + self.vae.config.shift_factor
|
||||
image = self.vae.decode(latents, return_dict=False)[0]
|
||||
if (ori_height, ori_width) != (height, width):
|
||||
image = F.interpolate(image, size=(ori_height, ori_width), mode="bilinear")
|
||||
|
||||
return DiffusionOutput(output=image)
|
||||
+318
@@ -0,0 +1,318 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Ported for vLLM-Omni native Boogu-Image support from
|
||||
# boogu/schedulers/scheduling_flow_match_euler_discrete_time_shifting.py
|
||||
# (https://github.com/boogu-project/Boogu-Image). The scheduling logic is
|
||||
# kept verbatim; only debug comments were removed and typing modernized.
|
||||
#
|
||||
# --- Upstream header preserved below ---
|
||||
#
|
||||
# Copyright (C) 2026 Boogu Team.
|
||||
#
|
||||
# This file is adapted by Boogu Team from prior open-source scheduler work.
|
||||
# Boogu-specific modifications include static/dynamic time-shift handling used
|
||||
# by the released Boogu pipeline.
|
||||
#
|
||||
# Original work:
|
||||
# Copyright 2024 Stability AI, Katherine Crowson and The HuggingFace Team.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
||||
from diffusers.schedulers.scheduling_utils import SchedulerMixin
|
||||
from diffusers.utils import BaseOutput, logging
|
||||
|
||||
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlowMatchEulerDiscreteSchedulerOutput(BaseOutput):
|
||||
"""
|
||||
Output class for the scheduler's `step` function output.
|
||||
|
||||
Args:
|
||||
prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
|
||||
Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
|
||||
denoising loop.
|
||||
"""
|
||||
|
||||
prev_sample: torch.FloatTensor
|
||||
|
||||
|
||||
class FlowMatchEulerDiscreteScheduler(SchedulerMixin, ConfigMixin):
|
||||
"""
|
||||
Euler scheduler with Boogu's training-consistent time shifting.
|
||||
|
||||
Timesteps run ascending 0 -> 1 (not descending sigmas as in stock
|
||||
diffusers), `set_timesteps` accepts a `num_tokens` argument for the
|
||||
dynamic-shift variants, and `step()` integrates with `t_next - t`
|
||||
directly.
|
||||
|
||||
This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
|
||||
methods the library implements for all schedulers such as loading and saving.
|
||||
|
||||
Args:
|
||||
num_train_timesteps (`int`, defaults to 1000):
|
||||
The number of diffusion steps to train the model.
|
||||
do_shift (`bool`, defaults to `True`):
|
||||
Whether to apply the training-consistent time shift in `set_timesteps`.
|
||||
dynamic_time_shift (`bool`, defaults to `True`):
|
||||
If `True`, the shift depends on the per-sample `num_tokens`; if `False`, it uses the configured `seq_len`.
|
||||
time_shift_version (`str`, defaults to `"v2"`):
|
||||
`"v1"` (logistic transform with a linear mu mapping) or `"v2"` (rational transform).
|
||||
seq_len (`int`, *optional*):
|
||||
Token count used to compute the static shift when `dynamic_time_shift=False` (mirrors training).
|
||||
base_shift (`float`, defaults to 0.5):
|
||||
v1 linear mapping lower bound (matches training defaults).
|
||||
max_shift (`float`, defaults to 1.15):
|
||||
v1 linear mapping upper bound (matches training defaults).
|
||||
"""
|
||||
|
||||
_compatibles = []
|
||||
order = 1
|
||||
|
||||
@register_to_config
|
||||
def __init__(
|
||||
self,
|
||||
num_train_timesteps: int = 1000,
|
||||
do_shift: bool = True,
|
||||
dynamic_time_shift: bool = True,
|
||||
time_shift_version: str = "v2",
|
||||
# seq_len is used to mirror training-side static time shift (when dynamic_time_shift=False)
|
||||
# In training, seq_len is the token count used to compute shift.
|
||||
seq_len: int | None = None,
|
||||
# v1 linear mapping range (matches training defaults)
|
||||
base_shift: float = 0.5,
|
||||
max_shift: float = 1.15,
|
||||
time_shift_v2_half_scaling_factor: float = 60.0,
|
||||
):
|
||||
timesteps = torch.linspace(0, 1, num_train_timesteps + 1, dtype=torch.float32)[:-1]
|
||||
|
||||
self.timesteps = timesteps
|
||||
|
||||
self._step_index = None
|
||||
self._begin_index = None
|
||||
self.time_shift_v2_scaling_factor = time_shift_v2_half_scaling_factor * 2
|
||||
|
||||
@property
|
||||
def step_index(self):
|
||||
"""
|
||||
The index counter for current timestep. It will increase 1 after each scheduler step.
|
||||
"""
|
||||
return self._step_index
|
||||
|
||||
@property
|
||||
def begin_index(self):
|
||||
"""
|
||||
The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
|
||||
"""
|
||||
return self._begin_index
|
||||
|
||||
# Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
|
||||
def set_begin_index(self, begin_index: int = 0):
|
||||
"""
|
||||
Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
|
||||
|
||||
Args:
|
||||
begin_index (`int`):
|
||||
The begin index for the scheduler.
|
||||
"""
|
||||
self._begin_index = begin_index
|
||||
|
||||
def index_for_timestep(self, timestep, schedule_timesteps=None):
|
||||
if schedule_timesteps is None:
|
||||
schedule_timesteps = self._timesteps
|
||||
|
||||
indices = (schedule_timesteps == timestep).nonzero()
|
||||
|
||||
# The sigma index that is taken for the **very** first `step`
|
||||
# is always the second index (or the last index if there is only 1)
|
||||
# This way we can ensure we don't accidentally skip a sigma in
|
||||
# case we start in the middle of the denoising schedule (e.g. for image-to-image)
|
||||
pos = 1 if len(indices) > 1 else 0
|
||||
|
||||
return indices[pos].item()
|
||||
|
||||
# --- Helpers to mirror training-side shift logic ---
|
||||
@staticmethod
|
||||
def _get_lin_function(x1: float = 256, y1: float = 0.5, x2: float = 4096, y2: float = 1.15):
|
||||
m = (y2 - y1) / (x2 - x1)
|
||||
b = y1 - m * x1
|
||||
return lambda x: m * x + b
|
||||
|
||||
@staticmethod
|
||||
def _time_shift_v1(t_np: np.ndarray, mu: float, sigma: float = 1.0) -> np.ndarray:
|
||||
# Matches training: t <- 1 - t; logistic transform; then t <- 1 - t
|
||||
eps = 1e-8
|
||||
t1 = 1.0 - t_np
|
||||
t1 = np.clip(t1, eps, 1.0 - eps)
|
||||
num = math.exp(mu)
|
||||
denom = num + np.power(1.0 / t1 - 1.0, sigma)
|
||||
y = num / denom
|
||||
out = 1.0 - y
|
||||
return out.astype(np.float32)
|
||||
|
||||
@staticmethod
|
||||
def _time_shift_v2(t_np: np.ndarray, m: float) -> np.ndarray:
|
||||
# Matches training: t' = t / (m - m t + t)
|
||||
return (t_np / (m - m * t_np + t_np)).astype(np.float32)
|
||||
|
||||
def set_timesteps(
|
||||
self,
|
||||
num_inference_steps: int | None = None,
|
||||
device: str | torch.device | None = None,
|
||||
timesteps: list[float] | None = None,
|
||||
num_tokens: int | None = None,
|
||||
):
|
||||
"""
|
||||
Sets the discrete timesteps used for the diffusion chain (to be run before inference).
|
||||
|
||||
Args:
|
||||
num_inference_steps (`int`):
|
||||
The number of diffusion steps used when generating samples with a pre-trained model.
|
||||
device (`str` or `torch.device`, *optional*):
|
||||
The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
|
||||
timesteps (`list[float]`, *optional*):
|
||||
Custom timesteps to use. If provided, `num_inference_steps` is ignored.
|
||||
num_tokens (`int`, *optional*):
|
||||
Per-sample token count, used by the dynamic time-shift variants.
|
||||
"""
|
||||
|
||||
if timesteps is None:
|
||||
self.num_inference_steps = num_inference_steps
|
||||
t_arr = np.linspace(0, 1, num_inference_steps + 1, dtype=np.float32)[:-1]
|
||||
|
||||
# Apply training-consistent time shift only when requested
|
||||
if self.config.do_shift:
|
||||
# dynamic or static
|
||||
if self.config.dynamic_time_shift:
|
||||
# dynamic: depend on per-sample token count
|
||||
if self.config.time_shift_version == "v1":
|
||||
# In training dynamic v1: mu is computed from tokens' linear map where
|
||||
# tokens are approximately (H_lat//2)*(W_lat//2). We approximate this with num_tokens//4.
|
||||
if num_tokens is not None and num_tokens > 0:
|
||||
tokens_reduced = max(1, int(num_tokens) // 4)
|
||||
lin = self._get_lin_function(y1=self.config.base_shift, y2=self.config.max_shift)
|
||||
mu = lin(tokens_reduced) ## 4096 for 1024x1024 resolution
|
||||
|
||||
t_arr = self._time_shift_v1(t_arr, mu, sigma=1.0)
|
||||
# else: no-op if we lack num_tokens
|
||||
elif self.config.time_shift_version == "v2":
|
||||
# MUST remain identical to current behavior when v2 + dynamic=True
|
||||
# m = sqrt(num_tokens) / 40; t' = t / (m - m t + t)
|
||||
# When input resolution is 320 * 320, m = 1, when input resolution is 512 * 512,
|
||||
# m = 1.6, when input resolution is 1024 * 1024, m = 3.2
|
||||
if num_tokens is not None and num_tokens > 0:
|
||||
m = float(np.sqrt(num_tokens)) / self.time_shift_v2_scaling_factor
|
||||
t_arr = self._time_shift_v2(t_arr, m)
|
||||
# else: no-op
|
||||
else:
|
||||
# static: depend on seq_len configured at scheduler init
|
||||
if self.config.time_shift_version == "v1":
|
||||
if self.config.seq_len is not None and self.config.seq_len > 0:
|
||||
lin = self._get_lin_function(y1=self.config.base_shift, y2=self.config.max_shift)
|
||||
mu = lin(int(self.config.seq_len))
|
||||
t_arr = self._time_shift_v1(t_arr, mu, sigma=1.0)
|
||||
elif self.config.time_shift_version == "v2":
|
||||
if self.config.seq_len is not None and self.config.seq_len > 0:
|
||||
# training static v2 uses m = sqrt(seq_len) / 40
|
||||
m = float(np.sqrt(self.config.seq_len)) / self.time_shift_v2_scaling_factor
|
||||
t_arr = self._time_shift_v2(t_arr, m)
|
||||
|
||||
timesteps = t_arr
|
||||
|
||||
timesteps = torch.from_numpy(timesteps).to(dtype=torch.float32, device=device)
|
||||
_timesteps = torch.cat([timesteps, torch.ones(1, device=timesteps.device)])
|
||||
|
||||
self.timesteps = timesteps
|
||||
self._timesteps = _timesteps
|
||||
self._step_index = None
|
||||
self._begin_index = None
|
||||
|
||||
def _init_step_index(self, timestep):
|
||||
if self.begin_index is None:
|
||||
if isinstance(timestep, torch.Tensor):
|
||||
timestep = timestep.to(self.timesteps.device)
|
||||
self._step_index = self.index_for_timestep(timestep)
|
||||
else:
|
||||
self._step_index = self._begin_index
|
||||
|
||||
def step(
|
||||
self,
|
||||
model_output: torch.FloatTensor,
|
||||
timestep: float | torch.FloatTensor,
|
||||
sample: torch.FloatTensor,
|
||||
generator: torch.Generator | None = None, # noqa: ARG002 - kept for API compatibility
|
||||
return_dict: bool = True,
|
||||
) -> FlowMatchEulerDiscreteSchedulerOutput | tuple:
|
||||
"""
|
||||
Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
|
||||
process from the learned model outputs (most often the predicted noise).
|
||||
|
||||
Args:
|
||||
model_output (`torch.FloatTensor`):
|
||||
The direct output from learned diffusion model.
|
||||
timestep (`float`):
|
||||
The current discrete timestep in the diffusion chain.
|
||||
sample (`torch.FloatTensor`):
|
||||
A current instance of a sample created by the diffusion process.
|
||||
generator (`torch.Generator`, *optional*):
|
||||
A random number generator.
|
||||
return_dict (`bool`):
|
||||
Whether or not to return a [`FlowMatchEulerDiscreteSchedulerOutput`] or tuple.
|
||||
|
||||
Returns:
|
||||
[`FlowMatchEulerDiscreteSchedulerOutput`] or `tuple`:
|
||||
If return_dict is `True`, [`FlowMatchEulerDiscreteSchedulerOutput`] is returned, otherwise a tuple is
|
||||
returned where the first element is the sample tensor.
|
||||
"""
|
||||
|
||||
if isinstance(timestep, int) or isinstance(timestep, torch.IntTensor) or isinstance(timestep, torch.LongTensor):
|
||||
raise ValueError(
|
||||
(
|
||||
"Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
|
||||
" `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
|
||||
" one of the `scheduler.timesteps` as a timestep."
|
||||
),
|
||||
)
|
||||
|
||||
if self.step_index is None:
|
||||
self._init_step_index(timestep)
|
||||
# Upcast to avoid precision issues when computing prev_sample
|
||||
sample = sample.to(torch.float32)
|
||||
t = self._timesteps[self.step_index]
|
||||
t_next = self._timesteps[self.step_index + 1]
|
||||
|
||||
prev_sample = sample + (t_next - t) * model_output
|
||||
|
||||
# Cast sample back to model compatible dtype
|
||||
prev_sample = prev_sample.to(model_output.dtype)
|
||||
|
||||
# upon completion increase step index by one
|
||||
self._step_index += 1
|
||||
|
||||
if not return_dict:
|
||||
return (prev_sample,)
|
||||
|
||||
return FlowMatchEulerDiscreteSchedulerOutput(prev_sample=prev_sample)
|
||||
|
||||
def __len__(self):
|
||||
return self.config.num_train_timesteps
|
||||
Reference in new issue
Block a user