Skip to main content
Choose the example by what must cross a node boundary or survive a step. Each links to its implementation and a test that exercises the distinction.

Orpheus streams audio during decode

Orpheus turns {voice}: {text} into 24 kHz speech. Its model declares a paged KV cache and sampler for the LLM. The LLM prefills, then runs a decode loop that sends audio tokens across a streaming edge to a second partition. That partition decodes a sliding window of tokens while the LLM generates the next ones. The SNAC decoder needs no declared state. The model’s get_node_precision keeps it in fp32 without autocast, the submodule sets disable_torch_compile, and it emits int16 PCM. The streaming tests exercise chunk boundaries and the final flush.

BAGEL switches between text and image generation

BAGEL uses one Qwen2 backbone for text decoding and image denoising. Its walk planner sends text spans through prefill_text, image patches through prefill_vit, and image latents through prefill_vae. For interleaved prompts it binds each walk to its own span. Text output runs decode. Image output runs denoising followed by the VAE decoder. In think_mode, text decoding precedes denoising. Three choices in the LLM submodule matter when adapting this design:
  • Edges. A declared output is a possible output. The same LLM emits new_token on text steps and can update the cache without emitting a token on image steps. Keeping embedding, transformer, and output heads together avoids transferring intermediate tensors between colocated nodes.
  • State. Text reads are causal. Image reads are bidirectional. On a guided request, the cfg_text branch forks before text is written but after an image is committed. A denoise step reads the cache with keep=False. Rows and position IDs must follow the same label order, including placeholders for unguided rows. The capture key distinguishes guided and unguided decode.
  • Placement. The single-GPU config runs guidance branches in one forward. The CFG-parallel config gives LLM_cfg_text and LLM_cfg_img their own GPUs and joins them at combine_cfg. The request’s guidance window determines its walks and loop caps. Unguided requests skip the branch GPUs. Include every denoise walk when assigning LLM ranks per walk.
The guidance tests exercise walk selection. Read the planner and declare_step together when changing a guidance branch so the selected walks, cache labels, and capture keys agree.

Whisper reads a fixed encoder context

Whisper’s audio encoder and decoder have different lifetimes. The encoder needs no persistent state. During prefill the decoder writes the fixed encoder context to cross_kv_cache using ArrayStep(eager_add=True). Later steps declare zero additional context and read the resident pages. Decoder self-attention appends to kv_cache on each token. Its positions index learned embeddings. A separate decoder_state holds timestamp rules and the transcript used by alignment. Size each pool for what one request can retain. At 128 tokens per page, a 448-token decoder needs four pages per request and a 1,500-token encoder context needs twelve. For 64 requests plus one sink page, that gives 257 self-attention pages and 769 cross-attention pages. The deployment caps concurrency at 64. validate_config_yaml checks that both pools still fit after resource overrides. The cache sizing tests cover this contract and the decoder tests exercise the step declarations. For a new encoder-decoder model with an ordinary sampling decoder, start with EncoderDecoderNode, which already handles the fixed-context write and reads.

FLUX.2 denoises an image

FLUX.2 klein encodes the prompt once, denoises latents in a loop, and decodes the final latents with the VAE. Image editing also encodes a reference image. Conditioning persists across iterations. The loop carries latents from one step to the next. Implement the model-specific hooks in DenoiseLoopSubmodule. The base class batches compatible rows, draws seeded noise, caches schedules, and captures the denoiser and Euler update together.
  • Choose compatible rows. bucket_key_for supplies both the batch and capture key. Include any property that changes the forward, such as the latent grid, text length, or guidance mode. request_inputs supplies conditioning for a real row and capture_request_inputs supplies the matching dummy inputs.
  • Initialize and run the denoiser. Implement seed_latents for the first iteration, num_tokens for the latent token count and denoise for the model prediction.
  • Define the schedule. Return the same step count from the model’s get_loop_caps and the node’s schedule_for. Use the engine’s per-row loop counter. For additional device-side scalars, name schedule attributes in STEP_SCALARS. Set PREFETCH_SEEDS only if initial loop-back tensors depend solely on the bucket key and request seed.
  • Carry solver history. A multistep solver names extra loop-back tensors in SOLVER_STATE, initializes them in seed_loop_back, and returns them from each step. For UniPC, use UniPCDenoiseLoopSubmodule and implement model_output. Rows can then share a forward at different iterations.
  • Declare attention. Describe each layer’s attended spans in attention_segments and declare arrays with max_len=0 for temporary tokens. The shared attention implementation uses a bound engine handle or falls back to SDPA.
For model components, start with the FLUX.2 submodules. For tuned normalization and RoPE kernels, use the eager equivalence tests to check rounding. For OpenAI image generation and editing, register DiffusionImageAdapter and its Rust counterpart. The SDK calls are generate_image and edit_image.

Qwen3.5 interleaves images and text

Qwen3.5 uses a vision encoder and a decoder LLM. A text-only prompt goes straight to the LLM. A prompt with images runs the vision encoder first, then prefills the whole prompt in one row with the image embeddings in their original positions. Both paths finish in the same decode loop.
  1. Subclass DeclaredDecoderModel for a model using declared state. Supply NAME, METADATA_FILES, the tokenizer, media intakes, and resource declarations. Implement _prompt_ids and _create_submodule. The base loads the checkpoint’s generation defaults, chooses walks for the requested media, constructs the state layout, and budgets decode after the token produced by prefill. Set TEXT_PROMPTS = False for a model that always requires media.
  2. Describe each encoder with a MediaEncoder. Its output must contain one embedding row per media pad, in prompt order. Return the patch or frame layout in PreparedMedia. The scaffold stores it under the metadata key media_layout. The prompt-interleaving tests exercise a case that a single-image prompt misses: text and several images in one row.
  3. Implement the forward passes in node submodules. Qwen’s LLM subclasses DeclaredTokenDecoder, returns cache and sampler steps from node_step, and supplies its three-axis positions with prompt_positions. The shared decoder batches prefill and decode, captures supported shapes, and loops sampled IDs back on the device. Its vision encoder uses PackedBlockLoopEncoder to pack patches and replay the block loop by shape.
  4. Check whether every declared resource can drop a request and rebuild it. The scaffold can retain prompt IDs and host copies of encoder outputs for reprefill. Override rebuild_policies to return an empty mapping when a resource cannot support this.
BAGEL’s interleaved span walks solve a different problem: guidance branches fork at specific text or image boundaries and an image can contribute both ViT patches and VAE latents. Its worked example traces that path.