vllm_omni.diffusion.models.krea2 ¶
Krea 2 diffusion model components.
Modules:
| Name | Description |
|---|---|
krea2_transformer | |
pipeline_krea2 | |
Krea2Pipeline ¶
Bases: Module, DiffusionPipelineProfilerMixin, ProgressBarMixin, SupportsComponentDiscovery
The Krea 2 text-to-image pipeline (vLLM-Omni port of diffusers.Krea2Pipeline).
Components: - scheduler: FlowMatchEulerDiscreteScheduler (resolution-aware exponential time shift). - vae: the Qwen-Image VAE (f8, 16 latent channels). - text_encoder: a Qwen3-VL model; the pipeline consumes a stack of hidden states tapped from several decoder layers rather than the last hidden state. - tokenizer: the tokenizer paired with the text encoder. - transformer: the Krea 2 single-stream MMDiT that predicts the flow-matching velocity.
image_processor instance-attribute ¶
prompt_template_encode_num_suffix_tokens instance-attribute ¶
prompt_template_encode_prefix instance-attribute ¶
prompt_template_encode_prefix = "<|im_start|>system\nDescribe the image by detailing the color, shape, size, texture, quantity, text, spatial relationships of the objects and background:<|im_end|>\n<|im_start|>user\n"
prompt_template_encode_suffix instance-attribute ¶
scheduler instance-attribute ¶
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
model,
subfolder="scheduler",
local_files_only=local_files_only,
)
text_encoder_select_layers instance-attribute ¶
tokenizer instance-attribute ¶
tokenizer = AutoTokenizer.from_pretrained(
model,
subfolder="tokenizer",
local_files_only=local_files_only,
)
transformer instance-attribute ¶
transformer = Krea2Transformer2DModel(
od_config=od_config,
quant_config=od_config.quantization_config,
**transformer_kwargs,
)
vae instance-attribute ¶
vae = from_pretrained_with_prefetch(
DistributedAutoencoderKLQwenImage.from_pretrained,
model,
subfolder="vae",
prefetch_list=subfolders,
local_files_only=local_files_only,
torch_dtype=od_config.dtype,
).to(self.device)
vae_scale_factor instance-attribute ¶
weights_sources instance-attribute ¶
weights_sources = [
DiffusersPipelineLoader.ComponentSource(
model_or_path=od_config.model,
subfolder="transformer",
revision=od_config.revision,
prefix="transformer.",
fall_back_to_pt=True,
)
]
diffuse ¶
diffuse(
latents: Tensor,
timesteps: Tensor,
position_ids: Tensor,
prompt_embeds: Tensor,
prompt_embeds_mask: Tensor,
negative_prompt_embeds: Tensor | None,
negative_prompt_embeds_mask: Tensor | None,
guidance_scale: float,
) -> Tensor
Run the flow-matching denoising loop and return the final latents.
encode_prompt ¶
encode_prompt(
prompt: str | list[str],
device: device | None = None,
num_images_per_prompt: int = 1,
prompt_embeds: Tensor | None = None,
prompt_embeds_mask: Tensor | None = None,
max_sequence_length: int = 512,
) -> tuple[Tensor, Tensor]
forward ¶
forward(
req: OmniDiffusionRequest,
prompt: str | list[str] | None = None,
negative_prompt: str | list[str] | None = None,
height: int = 1024,
width: int = 1024,
num_inference_steps: int = 28,
sigmas: list[float] | None = None,
guidance_scale: float = 4.5,
num_images_per_prompt: int = 1,
generator: Generator | list[Generator] | None = None,
latents: Tensor | None = None,
output_type: str | None = "pil",
max_sequence_length: int = 512,
) -> DiffusionOutput
get_text_hidden_states ¶
get_text_hidden_states(
prompt: str | list[str],
max_sequence_length: int = 512,
device: device | None = None,
) -> tuple[Tensor, Tensor]
Tokenize prompt into the fixed-length Krea 2 layout and tap the selected encoder hidden states.
Returns (hidden_states, attention_mask) of shapes (batch, text_seq_len, num_text_layers, text_hidden_dim) and (batch, text_seq_len) (bool).
prepare_latents ¶
prepare_latents(
batch_size,
num_channels_latents,
height,
width,
dtype,
device,
generator,
latents=None,
)
Krea2Transformer2DModel ¶
Bases: Module
The single-stream MMDiT flow-matching backbone used by the Krea 2 pipeline (vLLM-Omni port).
Text conditioning enters as a stack of hidden states tapped from several layers of a multimodal text encoder. A small text-fusion transformer collapses the layer axis and refines the token sequence; the result is concatenated with the patchified image latents into a single [text, image] sequence processed by the transformer blocks. The timestep conditions every block through one shared modulation vector plus per-block learned tables.
dtype instance-attribute ¶
final_layer instance-attribute ¶
final_layer = Krea2FinalLayer(
hidden_size,
in_channels,
norm_eps,
quant_config,
"final_layer",
)
img_in instance-attribute ¶
rotary_emb instance-attribute ¶
rotary_emb = Krea2RotaryPosEmbed(
theta=rope_theta, axes_dim=list(axes_dims_rope)
)
text_fusion instance-attribute ¶
text_fusion = Krea2TextFusion(
num_text_layers=num_text_layers,
dim=text_hidden_dim,
num_heads=text_num_attention_heads,
num_kv_heads=text_num_key_value_heads,
intermediate_size=text_intermediate_size,
num_layerwise_blocks=num_layerwise_text_blocks,
num_refiner_blocks=num_refiner_text_blocks,
eps=norm_eps,
quant_config=quant_config,
prefix="text_fusion",
)
time_embed instance-attribute ¶
time_embed = Krea2TimestepEmbedding(
timestep_embed_dim,
hidden_size,
quant_config,
"time_embed",
)
time_mod_proj instance-attribute ¶
transformer_blocks instance-attribute ¶
transformer_blocks = nn.ModuleList(
[
(
Krea2TransformerBlock(
hidden_size=hidden_size,
intermediate_size=intermediate_size,
num_heads=num_attention_heads,
num_kv_heads=num_key_value_heads,
norm_eps=norm_eps,
quant_config=quant_config,
prefix=f"transformer_blocks.{i}",
)
)
for i in (range(num_layers))
]
)
txt_in instance-attribute ¶
txt_in = Krea2TextProjection(
text_hidden_dim,
hidden_size,
norm_eps,
quant_config,
"txt_in",
)
forward ¶
forward(
hidden_states: Tensor,
encoder_hidden_states: Tensor,
timestep: Tensor,
position_ids: Tensor,
encoder_attention_mask: Tensor | None = None,
) -> Tensor
Predict the flow-matching velocity for the image tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states | Tensor | Packed (patchified) noisy image latents | required |
encoder_hidden_states | Tensor | Tapped text-encoder hidden states | required |
timestep | Tensor | Flow-matching time in | required |
position_ids | Tensor |
| required |
encoder_attention_mask | Tensor | None | Boolean mask marking valid text tokens | None |