> ## 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.

# Batching, capture, and placement

> Choose compatible rows, replay the right graph, and place the same model on several GPUs.

Start with one correct eager forward. Then batch two requests with different inputs and check each output against its single-request result. Capture and multi-rank placement add constraints to the same forward.

## Choose a batch

The worker chooses which ready rows to run. The model describes which rows are compatible in `can_batch`, sets `input_seq_len` in `prepare_inputs`, and implements `forward_batched`. Include any distinction that changes the forward, such as an image grid, a guidance branch, or a different tensor layout. Stack or pack the selected rows in `preprocess`.

For decode, a batch usually contains one token per row. Packed prefill rows have different token lengths. A combined walk can execute prefill and decode rows in the same step. Keep the segment order, attention layout, positions, and returned rows consistent. Under chunked prefill, a submodule whose sampled token only matters on the final chunk should keep only the final chunk’s sample. [Decoder scaffold](https://github.com/mstar-project/mstar/blob/main/mstar/model/components/sampled_token_decoder.py) · [Scheduler tests](https://github.com/mstar-project/mstar/blob/main/test/modular/test_micro_scheduler.py)

## Declare capture shapes

| Forward shape | Capture configuration |
| - | - |
| Rows of equal length, such as one decode token each | `BatchedAcceleratorGraphConfig`. Supply a representative row and supported batch sizes. |
| Packed variable-length tokens | `PackedAcceleratorGraphConfig`. Supply token-count buckets and a factory for capture inputs. |
| A repeated inner region inside an otherwise eager forward | `PiecewiseBatchedConfig` or `PiecewisePackedConfig`. Declare the region’s input geometry and invoke it through the engine. |

Capture inputs must exercise the same operations as a real replay. If two rows with the same count can follow different forwards, include that distinction in the capture key. For BAGEL, guided and unguided decode need separate captures. The submodule’s `cg_key_info` and the step’s matching capture metadata must describe the same bucket. The worker leases a slot before declaring the step. A batch that misses the capture runs eagerly. The [capture tests](https://github.com/mstar-project/mstar/blob/main/test/modular/test_accelerator_graph_capture.py) cover bucket matching; the [config types](https://github.com/mstar-project/mstar/blob/main/mstar/engine/accelerator_graph_config.py) specify the actual fields.

Keep raw graph capture and resource allocation in the engine. A buffer rewritten on a planning stream can race with a previous forward that is still reading it. Engine resources that copy host data into fixed buffers may need double buffering. Padding rows must address the resource’s sink and cannot advance a real request. [Runner](https://github.com/mstar-project/mstar/blob/main/mstar/engine/accelerator_graph_runner.py) · [Resource base](https://github.com/mstar-project/mstar/blob/main/mstar/engine/resources/base.py)

## Place nodes on ranks

A `node_groups` entry names graph nodes, ranks, and optionally the walks on which that placement applies. A single-rank configuration can place several nodes together. Giving different nodes different ranks allows requests to overlap or different stages to stream. A rank list alone does not teach a model how to shard its weights.

| Deployment choice | What the model must support |
| - | - |
| Tensor parallelism | Compatible sharded layers and weight loading. Partner ranks must perform the same collectives in the same order and with the same shapes. |
| Sequence or context parallelism | A declared token shard or placement-capable KV and reads. Check head counts and page geometry for the group. [State and exchange choices](https://docs.m-star.org/contributing/state-contract.md#sequence-parallelism) |
| Guidance parallelism | A graph that produces and combines the conditional and unconditional branches. [BAGEL CFG placement](https://github.com/mstar-project/mstar/blob/main/configs/bagel_cfg_parallel.yaml) |
| Expert parallelism | An MoE model with expert dispatch and combine across the chosen ranks. Validate the checkpoint’s expert geometry and deployment with the model’s config. |
| Data-parallel replicas | The conductor chooses among instances. Resources can supply a routing estimate, for example for prefix reuse. [Routing](https://docs.m-star.org/contributing/state-contract.md#routing) |

Before benchmarking, check the actual [single-GPU BAGEL config](https://github.com/mstar-project/mstar/blob/main/configs/bagel_single_gpu.yaml) against the CFG-parallel one. A request without guidance should take the unguided path in either. Lockstep ranks must also agree on a failure, admission decision, and teardown. A per-rank early return before a collective can hang its peers. [Lockstep worker](https://github.com/mstar-project/mstar/blob/main/mstar/worker/lockstep.py)

## Tune a deployment

Override capacities by declared name under `resources:` in YAML. This is where a deployment chooses KV pages and host offload, the attention backend, or sampler capacity. The model declares the geometry needed for correctness. See the [state reference](https://docs.m-star.org/contributing/state-contract.md#resources) for how names map to the resources.

The model can set preprocessing thread defaults for host work. Measure with the model’s actual media inputs before changing a limit. For kernel selection, use existing tuned tables keyed by architecture and geometry instead of a branch on device name in a model. Capture variants and larger buckets cost startup time and memory. Compare throughput, latency, and startup at the target concurrency, with a second workload to catch regressions. [Kernel tables](https://github.com/mstar-project/mstar/tree/main/mstar/kernels/tables) · [Evaluation](https://docs.m-star.org/evaluation.md)


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