> ## Documentation Index
> Fetch the complete documentation index at: https://docs.m-star.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Five model ports

> Find a port with the same execution pattern and see which choices are specific to its checkpoint.

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](https://github.com/mstar-project/mstar/blob/main/mstar/model/orpheus/orpheus_model.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/orpheus/submodules.py) 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](https://github.com/mstar-project/mstar/blob/main/test/modular/test_orpheus_snac_stream.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/bagel/bagel_model.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/bagel/submodules.py) 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](https://github.com/mstar-project/mstar/blob/main/configs/bagel_single_gpu.yaml) runs guidance branches in one forward. The [CFG-parallel config](https://github.com/mstar-project/mstar/blob/main/configs/bagel_cfg_parallel.yaml) 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](https://github.com/mstar-project/mstar/blob/main/test/modular/test_bagel_cfg_prune.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/whisper/whisper_model.py) 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](https://github.com/mstar-project/mstar/blob/main/configs/whisper_large.yaml) caps concurrency at 64. `validate_config_yaml` checks that both pools still fit after resource overrides. The [cache sizing tests](https://github.com/mstar-project/mstar/blob/main/test/modular/test_whisper_kv_sizing.py) cover this contract and the [decoder tests](https://github.com/mstar-project/mstar/blob/main/test/modular/test_whisper_decoder_node.py) exercise the step declarations.

For a new encoder-decoder model with an ordinary sampling decoder, start with [EncoderDecoderNode](https://github.com/mstar-project/mstar/blob/main/mstar/model/components/encoder_decoder.py), which already handles the fixed-context write and reads.

## FLUX.2 denoises an image

FLUX.2 [klein](https://github.com/mstar-project/mstar/blob/main/mstar/model/flux2_klein/flux2_klein_model.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/components/diffusion/denoise_loop.py). 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/components/diffusion/attention.py) uses a bound engine handle or falls back to SDPA.

For model components, start with the [FLUX.2 submodules](https://github.com/mstar-project/mstar/blob/main/mstar/model/flux2_klein/submodules.py). For tuned normalization and RoPE kernels, use the [eager equivalence tests](https://github.com/mstar-project/mstar/blob/main/test/modular/test_diffusion_dit_kernels.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/qwen3_5/qwen3_5_model.py) 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/multimodal_decoder.py) 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](https://github.com/mstar-project/mstar/blob/main/test/modular/test_qwen3_5_prompt_interleave.py) exercise a case that a single-image prompt misses: text and several images in one row.
3. Implement the forward passes in [node submodules](https://github.com/mstar-project/mstar/blob/main/mstar/model/qwen3_5/submodules.py). 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](https://github.com/mstar-project/mstar/blob/main/mstar/model/components/block_loop_encoder.py) 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](#worked-example-bagel) traces that path.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.