Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""FLUX.2 [dev] text encoder invocation.
FLUX.2 [dev] uses a Mistral Small 3 (hidden_size=5120) text encoder. Two variants
are supported (see ``MistralVariantType``), both read at the same hidden-state
indices (10, 20, 30):
- **Mistral24B** — the 40-layer encoder BFL ships in
``black-forest-labs/FLUX.2-dev/text_encoder``. This is the default pipeline
encoder; the loader keeps its final RMSNorm.
- **Cow** — the "cow-mistral3-small" build, the first 30 of those layers: all
FLUX.2 [dev] reads. On a 30-layer model the indices map to (1/3, 2/3, last), and
the loader drops the final RMSNorm to match ComfyUI's reference (``final_norm=False``).
Pipeline, per generation:
- A fixed system message biases the model toward structured image descriptions.
- The user prompt is wrapped in Mistral's chat template via the multimodal
AutoProcessor.
- The three hidden states are concatenated to a (B, seq, 3 * hidden_size) =
(B, seq, 15360) tensor matching the FLUX.2 transformer's joint_attention_dim.
"""
from contextlib import ExitStack
from typing import Any, Iterator, Literal, Optional, Tuple, cast
import torch
from transformers import PreTrainedModel
from invokeai.app.invocations.baseinvocation import BaseInvocation, Classification, invocation
from invokeai.app.invocations.fields import (
FieldDescriptions,
FluxConditioningField,
Input,
InputField,
TensorField,
UIComponent,
)
from invokeai.app.invocations.model import MistralEncoderField
from invokeai.app.invocations.primitives import FluxConditioningOutput
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.backend.model_manager.load.model_cache.utils import get_effective_device
from invokeai.backend.patches.layer_patcher import LayerPatcher
from invokeai.backend.patches.lora_conversions.flux_lora_constants import FLUX_LORA_T5_PREFIX
from invokeai.backend.patches.model_patch_raw import ModelPatchRaw
from invokeai.backend.quantization.dequantizing_linear import peak_dequant_transient_bytes, requires_sidecar_patching
from invokeai.backend.stable_diffusion.diffusion.conditioning_data import ConditioningFieldData, FLUXConditioningInfo
from invokeai.backend.util.devices import TorchDevice
# System prompt used by the FLUX.2 [dev] reference pipeline. Byte-for-byte
# identical to ComfyUI's ``Flux2Tokenizer.llama_template`` — note the literal
# ``\n`` between "object" and "attribution"; that's part of the trained-against
# token sequence, not a formatting artifact.
FLUX2_DEV_SYSTEM_MESSAGE = (
"You are an AI that reasons about image descriptions. You give structured "
"responses focusing on object relationships, object\nattribution and actions "
"without speculation."
)
# Raw chat template fed straight to the tokenizer — matches Comfy's approach
# (no ``apply_chat_template`` indirection). ``[SYSTEM_PROMPT]`` / ``[INST]`` are
# special tokens in Mistral Small 3's Tekken vocab, so the encoder produces the
# exact token sequence BFL trained the joint attention against.
FLUX2_DEV_PROMPT_TEMPLATE = "[SYSTEM_PROMPT]{system}[/SYSTEM_PROMPT][INST]{prompt}[/INST]"
# Indices into hidden_states[] (hidden_states[0] is the embedding output) that
# FLUX.2 [dev]'s joint attention was trained to consume. ComfyUI uses these
# same indices for both the 30-layer cow build and the 40-layer Mistral
# Small 3; for cow they hit (1/3, 2/3, last), and the loader strips the final
# RMSNorm so the layer-30 readout is the raw post-layer-29 state.
DEV_EXTRACTION_LAYERS = (10, 20, 30)
# Default max sequence length for FLUX.2 [dev]. The reference pipeline caps at 512.
DEV_MAX_SEQ_LEN = 512
@invocation(
"flux2_dev_text_encoder",
title="Prompt - FLUX.2 [dev]",
tags=["prompt", "conditioning", "flux", "flux2", "dev", "mistral"],
category="prompt",
version="1.0.1",
classification=Classification.Prototype,
idle_gpu_offloadable=True,
)
class Flux2DevTextEncoderInvocation(BaseInvocation):
"""Encode a prompt for FLUX.2 [dev] using its Mistral Small 3.2 text encoder."""
prompt: str = InputField(description="Text prompt to encode.", ui_component=UIComponent.Textarea)
mistral_encoder: MistralEncoderField = InputField(
title="Mistral Encoder",
description=FieldDescriptions.mistral_encoder,
input=Input.Connection,
)
max_seq_len: Literal[256, 512] = InputField(
default=DEV_MAX_SEQ_LEN,
description="Max sequence length for the Mistral encoder.",
)
mask: Optional[TensorField] = InputField(
default=None,
description="A mask defining the region that this conditioning prompt applies to.",
)
@torch.no_grad()
def invoke(self, context: InvocationContext) -> FluxConditioningOutput:
with ExitStack() as exit_stack:
mistral_embeds = self._encode_prompt(context, exit_stack)
# Save the conditioning CPU-backed: this node is idle_gpu_offloadable, so the
# encode may have run on a BORROWED GPU whose pool lock is released the moment
# this node returns — tensors left on it would pin VRAM on a device another
# session may immediately start using. Mirrors flux2_klein_text_encoder.
mistral_embeds = mistral_embeds.detach().to("cpu")
# FLUX.2 [dev] does not consume a pooled / CLIP-style embedding; we
# reuse the FLUX conditioning structure (Klein does the same) and put
# the Mistral hidden states in the `t5_embeds` slot, which the
# FLUX.2 denoise loop already wires into `encoder_hidden_states`.
conditioning_data = ConditioningFieldData(
conditionings=[
FLUXConditioningInfo(
clip_embeds=torch.zeros(1, device=mistral_embeds.device, dtype=mistral_embeds.dtype),
t5_embeds=mistral_embeds,
)
]
)
conditioning_name = context.conditioning.save(conditioning_data)
return FluxConditioningOutput(
conditioning=FluxConditioningField(conditioning_name=conditioning_name, mask=self.mask)
)
def _encode_prompt(self, context: InvocationContext, exit_stack: ExitStack) -> torch.Tensor:
text_encoder_info = context.models.load(self.mistral_encoder.text_encoder)
text_encoder_format = context.models.get_config(self.mistral_encoder.text_encoder).format
# An nvfp4 or GGUF build dequantizes each packed Linear per forward, a transient its resident size does not
# cover. Read from the unlocked model, before the lock the reservation applies to; zero for other builds.
dequant_bytes = peak_dequant_transient_bytes(
text_encoder_info.model, TorchDevice.choose_bfloat16_safe_dtype(text_encoder_info.compute_device)
)
(cached_weights, text_encoder) = exit_stack.enter_context(
text_encoder_info.model_on_device(working_mem_bytes=dequant_bytes)
)
processor_info = context.models.load(self.mistral_encoder.tokenizer)
(_, processor) = exit_stack.enter_context(processor_info.model_on_device())
repaired_tensors = text_encoder_info.repair_required_tensors_on_device()
device = get_effective_device(text_encoder)
if repaired_tensors > 0:
context.logger.warning(
f"Recovered {repaired_tensors} required Mistral tensor(s) on {device} after a partial device mismatch."
)
# Apply any LoRAs attached to the text encoder.
lora_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)
exit_stack.enter_context(
LayerPatcher.apply_smart_model_patches(
model=text_encoder,
patches=self._lora_iterator(context),
prefix=FLUX_LORA_T5_PREFIX,
dtype=lora_dtype,
cached_weights=cached_weights,
# Quantized weights (packed nvfp4 Linears, GGUF, SDNQ) cannot take a direct patch, so
# their LoRAs ride as sidecars.
force_sidecar_patching=requires_sidecar_patching(text_encoder, text_encoder_format),
)
)
context.util.signal_progress("Running Mistral text encoder (FLUX.2 [dev])")
if not isinstance(text_encoder, PreTrainedModel):
raise TypeError(
f"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. "
"The Mistral encoder model may be corrupted or incompatible."
)
# Build the raw FLUX.2 [dev] prompt template — matches ComfyUI's
# `Flux2Tokenizer.llama_template.format(text)` byte-for-byte. `[SYSTEM_PROMPT]`,
# `[/SYSTEM_PROMPT]`, `[INST]`, `[/INST]` are Tekken special tokens, so any of
# the three processors we can land on (Pixtral/Mistral3 processor, plain HF
# LlamaTokenizerFast, our embedded-Tekken adapter) emit the same sequence.
text = FLUX2_DEV_PROMPT_TEMPLATE.format(system=FLUX2_DEV_SYSTEM_MESSAGE, prompt=self.prompt)
# Comfy pads on the LEFT (`pad_left=True`), keeping the meaningful tokens
# at the right edge of the sequence. HF processors expose this via the
# `padding_side` attribute on their underlying tokenizer; we set it
# explicitly so the call matches Comfy's behavior regardless of the
# tokenizer's default. `processor` is typed as the `AnyModel` union;
# narrow to `Any` for the duration of the tokenizer call.
proc = cast(Any, processor)
tokenizer = getattr(proc, "tokenizer", proc)
if hasattr(tokenizer, "padding_side"):
tokenizer.padding_side = "left"
# Pass the prompt as the `text=` keyword, NOT positionally: the diffusers
# FLUX.2-dev encoder loads a `PixtralProcessor`, whose first positional
# parameter is `images` (`__call__(self, images=None, text=None, ...)`).
# A positional `proc(text, ...)` would route the prompt string into
# `images` and process it as an image. The `text` keyword is correct for
# all three processors we can land on (PixtralProcessor, a plain HF
# tokenizer, and our `_TekkenRawTextAdapter`).
inputs = proc(
text=text,
return_tensors="pt",
padding="max_length",
padding_side="left",
truncation=True,
max_length=self.max_seq_len,
)
input_ids = inputs["input_ids"].to(device)
attention_mask = inputs["attention_mask"].to(device)
# Mistral3ForConditionalGeneration wraps the LM under `.language_model`.
# For pure text encoding, run that sub-module to skip the (unused) vision
# tower and to avoid emitting a generation; for plain MistralModel /
# MistralForCausalLM, run the model directly.
forward_target = getattr(text_encoder, "language_model", None) or text_encoder
outputs = forward_target(
input_ids=input_ids,
attention_mask=attention_mask,
output_hidden_states=True,
use_cache=False,
)
if not hasattr(outputs, "hidden_states") or outputs.hidden_states is None:
raise RuntimeError(
"Mistral encoder did not return hidden_states. "
"Ensure output_hidden_states=True is supported by this model."
)
num_hidden_states = len(outputs.hidden_states) # = num_hidden_layers + 1 (embedding output)
num_layers = num_hidden_states - 1
# The standalone Mistral encoder loaders only accept 30-layer cow or 40-layer
# Mistral Small 3 weights, so hidden_states[] should always contain the layers
# FLUX.2 [dev]'s joint attention was trained to read (10/20/30). A text encoder
# extracted from a Main_Diffusers_Flux2 pipeline, however, is loaded via generic
# from_pretrained with no layer-count validation — so a nonstandard pipeline with
# a <30-layer encoder could reach here. Fail loudly instead of inventing extraction
# indices that would silently produce off-distribution (degraded) embeddings.
if num_layers < max(DEV_EXTRACTION_LAYERS):
raise RuntimeError(
f"Mistral encoder returned only {num_layers} hidden layer(s), but FLUX.2 [dev] reads "
f"layers {DEV_EXTRACTION_LAYERS} and requires at least {max(DEV_EXTRACTION_LAYERS)}. "
"This is not a supported FLUX.2 [dev] text encoder."
)
extraction_layers = DEV_EXTRACTION_LAYERS
# Concatenate the selected layers along the hidden dim: (B, seq, 3 * hidden_size).
# This is byte-identical to stack(dim=1).permute(0,2,1,3).reshape(...) but avoids
# the two intermediate full copies that stack + permute-reshape would allocate.
prompt_embeds = torch.cat([outputs.hidden_states[i] for i in extraction_layers], dim=-1)
prompt_embeds = prompt_embeds.to(dtype=text_encoder.dtype, device=device)
return prompt_embeds
def _lora_iterator(self, context: InvocationContext) -> Iterator[Tuple[ModelPatchRaw, float]]:
"""Iterate over LoRAs to apply to the Mistral encoder."""
for lora in self.mistral_encoder.loras:
lora_info = context.models.load(lora.lora)
if not isinstance(lora_info.model, ModelPatchRaw):
raise TypeError(
f"Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type(lora_info.model).__name__}. "
"The LoRA model may be corrupted or incompatible."
)
yield (lora_info.model, lora.weight)
del lora_info