mstar.worker.batch_builder#

Batch composition: which ready rows of one (node, walk) run this step.

The MicroScheduler picks the (node, walk) and owns the mechanics (popping ready queues, the backlog, holds, TP follow); a BaseBatchBuilder decides which rows the step takes out of a speculation chain’s own rows, the backlog and the ready queues (pick), and splits what was popped into the step, the rows that stay parked in the backlog and the ones that go back to their ready queues (build_batch).

Functions

fill_token_budget(rids, tokens, chunkable, ...)

Max-min fair share of budget over rids: rows under an equal share go in whole, the rest split what is left (chunkable rows) or sit out.

fill_token_budget_in_start_order(rids, ...)

budget over rids first come, first served.

make_batch_builder(kind)

off_group_rids(batch, capture_group[, ...])

The rids of batch outside anchor's capture group, or outside the first eligible rid's when no anchor is given.

Classes

BaseBatchBuilder()

BatchBuildRequest(node_name, graph_walk, ...)

BatchBuildResult(scheduled, backlog, ...)

BatchBuilderType(*values)

FIFOBatchBuilder()

One capture group per step, up to the cap; who goes first when the rows don't all fit is the node's StepOrder.

FairShareBatchBuilder()

FIFOBatchBuilder with the token budget shared max-min fair (fill_token_budget): every waiting prompt advances each step, so a long prompt's first token waits on every other prompt's prefill too.

LineOrder(key, strict)

How rows line up for a step that can't take them all.

StepCandidates(graph_walk[, continuing, ...])

Every row that could run one step of a scheduling key, for pick.

StepPick(taken, continuing, room, held_back, ...)

class mstar.worker.batch_builder.BaseBatchBuilder[source]#

Bases: ABC

abstractmethod build_batch(request)[source]#

One step’s rows out of request’s backlog and entries.

Parameters:

request (BatchBuildRequest)

Return type:

BatchBuildResult

chain_must_yield(backlog, live_rids, anchor, capture_group)[source]#

Whether a speculation chain anchored at anchor has to stop so backlog’s live rows can run: a step sharing the chain’s capture group would never take them.

Parameters:
Return type:

bool

abstractmethod pick(candidates)[source]#

Who runs one step: the one place a capped step’s rows are chosen, for a speculation chain and for a step the scheduler builds fresh.

Parameters:

candidates (StepCandidates)

Return type:

StepPick

class mstar.worker.batch_builder.BatchBuildRequest(node_name: str, graph_walk: str, backlog: 'ScheduledBatch | None' = None, fresh: 'ScheduledBatch | None' = None, pick: mstar.worker.batch_builder.StepPick | None = None, continuing: 'ScheduledBatch | None' = None, max_batch_tokens: int | None = None, walk_max_tokens: dict[str, int] | None=None, row_tokens: dict[int, int]=<factory>, chunkable: set[int] = <factory>, min_chunk_tokens: int = 1, chunk_alignment: int = 1, start_order: dict[int, int]=<factory>)[source]#

Bases: object

Parameters:
backlog: ScheduledBatch | None = None#
chunk_alignment: int = 1#
chunkable: set[int]#
continuing: ScheduledBatch | None = None#
fresh: ScheduledBatch | None = None#
graph_walk: str#
max_batch_tokens: int | None = None#
min_chunk_tokens: int = 1#
node_name: str#
pick: StepPick | None = None#
row_tokens: dict[int, int]#
start_order: dict[int, int]#
walk_max_tokens: dict[str, int] | None = None#
class mstar.worker.batch_builder.BatchBuildResult(scheduled, backlog, returned, chunk_tokens)[source]#

Bases: NamedTuple

Parameters:
backlog: ScheduledBatch | None#

Alias for field number 1

chunk_tokens: dict[int, int] | None#

Alias for field number 3

returned: dict[int, int] | None#

Alias for field number 2

scheduled: ScheduledBatch | None#

Alias for field number 0

class mstar.worker.batch_builder.BatchBuilderType(*values)[source]#

Bases: Enum

FAIR_SHARE = 'fair_share'#
FIFO = 'fifo'#
class mstar.worker.batch_builder.FIFOBatchBuilder[source]#

Bases: BaseBatchBuilder

One capture group per step, up to the cap; who goes first when the rows don’t all fit is the node’s StepOrder.

When every row fits, the step takes them in the order backlog, the chain’s own rows, then the queues: nothing is left out, so the order only decides the capture group, which the chain’s or else the first row’s sets, and a row of another group waits for a step of its own. When they don’t all fit, every row goes in the order’s sort and the first sets the group. A row of another group then waits for a step of its own: under TURNS the rows behind it pass it, since it leads the next step; under ARRIVAL they don’t, or rows that came later would join the step’s holders and keep it from it.

Under a combined walk a step of one walk takes that walk’s cap and a mixed step the combined walk’s own, or with none the smallest of the caps of the walks present in it; a walk with no row in the step does not cap it. Rows join in order; one whose walk would cap the step at or below the rows already in it is left out.

Of the rows build_batch gets, a fresh row the cap pushed out goes back to its ready queue only on a step that served a backlog. Parking it would refill the backlog from the queues on every pass, so it would never drain, and since a key holding a backlog is served before any other node, that node starves the rest: on wan22.yaml with dit capped at 1 a fifth request’s text_encoder waited 48.7 s for its first step against 0.2 s before backlogs were topped up. With no backlog it parks, so a split set drains before anything new starts. A row held back for any other reason – OOM backoff, pending remove, a foreign capture group, a walk the combined cap left out – always parks: it cannot run this step whoever holds it.

Under a token budget, of the rows the pick took, prompts are served in the order they started (fill_token_budget_in_start_order), and the caller’s rows mid-prefill come first. A row the budget leaves out always parks, ahead of the cap’s leftovers, because a budgeted walk’s backlog is its line: the Rust runtime scans ready rows in handle order, so a row sent back rejoins behind rows that arrived after it. Such a backlog can stay non-empty under sustained load; the worker’s fairness peek still hands the step after a prompt step to other ready work.

build_batch(request)[source]#

One step’s rows out of request’s backlog and entries.

Parameters:

request (BatchBuildRequest)

Return type:

BatchBuildResult

pick(candidates)[source]#

Who runs one step: the one place a capped step’s rows are chosen, for a speculation chain and for a step the scheduler builds fresh.

Parameters:

candidates (StepCandidates)

Return type:

StepPick

static share_token_budget(rids, tokens, chunkable, budget, min_chunk, start_order, align=1)[source]#

How the step’s rows share its token budget: (rid -> tokens taken, rids left out).

Parameters:
Return type:

tuple[dict[int, int], set[int]]

class mstar.worker.batch_builder.FairShareBatchBuilder[source]#

Bases: FIFOBatchBuilder

FIFOBatchBuilder with the token budget shared max-min fair (fill_token_budget): every waiting prompt advances each step, so a long prompt’s first token waits on every other prompt’s prefill too.

static share_token_budget(rids, tokens, chunkable, budget, min_chunk, start_order, align=1)[source]#

How the step’s rows share its token budget: (rid -> tokens taken, rids left out).

Parameters:
Return type:

tuple[dict[int, int], set[int]]

class mstar.worker.batch_builder.LineOrder(key, strict)[source]#

Bases: NamedTuple

How rows line up for a step that can’t take them all.

Parameters:
key: Callable[[int], Any]#

Alias for field number 0

strict: bool#

Alias for field number 1

class mstar.worker.batch_builder.StepCandidates(graph_walk, continuing=(), own_walk=None, backlog=(), queued=(), walk_of=<function StepCandidates.<lambda>>, one_walk=None, ready=<function StepCandidates.<lambda>>, capture_group=None, walk_caps=<factory>, walk_row_caps=<factory>, line_order=<function StepCandidates.<lambda>>)[source]#

Bases: object

Every row that could run one step of a scheduling key, for pick.

Parameters:
backlog: Sequence[int] = ()#
capture_group: Callable[[int], Any] | None = None#
continuing: Sequence[int] = ()#
graph_walk: str#
line_order()#
one_walk: str | None = None#
own_walk: str | None = None#
queued: Sequence[int] = ()#
ready()#
walk_caps: dict[str, int | None]#
walk_of()#
walk_row_caps: dict[str, int]#
class mstar.worker.batch_builder.StepPick(taken, continuing, room, held_back, ordered)[source]#

Bases: NamedTuple

Parameters:
continuing: list[int]#

Alias for field number 1

held_back: set[int]#

Alias for field number 3

ordered: bool#

Alias for field number 4

room: int | None#

Alias for field number 2

taken: list[int]#

Alias for field number 0

mstar.worker.batch_builder.fill_token_budget(rids, tokens, chunkable, budget, min_chunk=1, align=1)[source]#

Max-min fair share of budget over rids: rows under an equal share go in whole, the rest split what is left (chunkable rows) or sit out. A cut row takes at least min_chunk tokens, or all it has left if that is fewer: when the share is smaller, only as many rows as what is left holds are cut, and if it holds none, they wait for the next step. Never empty while a row exists: the first row runs over budget if it must. Returns (rid -> tokens taken, rids left out).

Parameters:
Return type:

tuple[dict[int, int], set[int]]

mstar.worker.batch_builder.fill_token_budget_in_start_order(rids, tokens, chunkable, budget, min_chunk=1, start_order=None, align=1)[source]#

budget over rids first come, first served. Rows under an equal share go in whole, and then any row that can finish in what is left, shortest first, so a short prompt never waits behind a long one. The rest follow in start order – prompts already mid-prefill by when they started (start_order), then the others as listed – each taking all it needs of what is left, so a prompt finishes before the next one starts and its first token does not wait on every other prompt’s prefill. The first that does not fit is cut to what is left, rounded down to a multiple of align, if that is at least min_chunk tokens, and otherwise waits for the next step; one that cannot be cut sits out without holding up the rows behind it. Never empty while a row exists: the first row runs over budget if it must.

The first prompt in line (the oldest mid-prefill, else the first not started) keeps min_chunk of the budget from the other prompts in the finishing pass, so a steady stream of short prompts cannot starve it, before its first chunk or after. Returns (rid -> tokens taken, rids left out).

Parameters:
Return type:

tuple[dict[int, int], set[int]]

mstar.worker.batch_builder.make_batch_builder(kind)[source]#
Parameters:

kind (BatchBuilderType)

Return type:

BaseBatchBuilder

mstar.worker.batch_builder.off_group_rids(batch, capture_group, exclude_rids=frozenset(), anchor=None)[source]#

The rids of batch outside anchor’s capture group, or outside the first eligible rid’s when no anchor is given.

Parameters:
Return type:

set[int]