mstar.engine.resources.attn.config#

What a model declares about attention: its backend, its spec, its step.

Kept free of the managers and their kernels so a submodule can declare a step without pulling FlashInfer in behind it.

Functions

derive_kv_groups(name, cfg, specs)

Pool name's groups (PagedKVConfig.keep_window), from the attentions over it whose sliding_window is shorter than its max_seq_len (a longer one keeps every token), and what about them no pool can serve, before anything builds or sizes from it; a pool with a problem keeps every token.

refuse_context_only(step, manager)

For the attention resources that don't read context_only: planned without it, the step's queries would attend to the tokens it appends as well.

Classes

AttentionConfig(kv_cache, backend, ...)

AttentionSpec(resource_key, nodes, config)

AttentionStep(segments, ...] | None = None, ...)

AttnBackend(*values)

BlockCausalAttentionSpec(resource_key, ...)

Self-attention over a paged KV cache in which a row's new tokens attend its cache block-causally: a token in block b (block_size tokens a block, from the row's first cached token) attends every key of blocks 0..b, so what the cache holds never changes and a step runs only new tokens.

CrossAttentionConfig(kv_cache[, ...])

Cross-attention against a context written once and never extended.

CrossAttentionSpec(resource_key, nodes, config)

class mstar.engine.resources.attn.config.AttentionConfig(kv_cache: str, backend: mstar.engine.resources.attn.config.AttnBackend = <AttnBackend.AUTO: 'auto'>, flashinfer_backend: str = 'auto', sm_scale: float | None = None, splits: tuple[mstar.engine.resources.spec.CombinedSplit, ...]=(), decode_tensor_cores: bool = True, decode_fmha: bool = False, sliding_window: int | None = None, layers: tuple[int, ...] | None=None)[source]#

Bases: object

Parameters:
backend: AttnBackend = 'auto'#
decode_fmha: bool = False#
decode_tensor_cores: bool = True#
flashinfer_backend: str = 'auto'#
kv_cache: str#
layers: tuple[int, ...] | None = None#
sliding_window: int | None = None#
sm_scale: float | None = None#
splits: tuple[CombinedSplit, ...] = ()#
class mstar.engine.resources.attn.config.AttentionSpec(resource_key: str, nodes: set[str], config: mstar.engine.resources.attn.config.AttentionConfig)[source]#

Bases: NodeResourceSpec

Parameters:
apply_yaml_overrides(backend=None, flashinfer_backend=None, decode_tensor_cores=None, decode_fmha=None)[source]#

Which kernel to run is the deployment’s call as much as the model’s — an image that cannot build FA3 pins FA2 here.

Cache geometry is not repeated here: it belongs to the KV resource this spec depends on, and is tuned under that resource’s own block.

Parameters:
  • backend (str | AttnBackend | None)

  • flashinfer_backend (str | None)

  • decode_tensor_cores (bool | None)

  • decode_fmha (bool | None)

combined_splits()[source]#

The combined walks this resource runs on split kernels.

Return type:

tuple[CombinedSplit, …]

config: AttentionConfig#
depends_on()[source]#

Keys whose specs this one builds against; the engine resolves them into EngineResourceInfo.dependencies.

Return type:

set[str]

derive(specs)[source]#

Set what this spec takes from the specs that read it (a KV pool keeps the window its attentions declare), after a deployment’s overrides; return what is wrong with it. Idempotent: it runs wherever the specs resolve (resolve_spec_dependencies).

Parameters:

specs (Mapping[str, NodeResourceSpec])

Return type:

list[str]

resolve_exchange(specs)[source]#

Its node’s sequence shard’s exchange over its pool’s storage; over KV every rank stores whole (stores_whole), kv_gather wherever the node splits.

Parameters:

specs (Mapping[str, NodeResourceSpec])

Return type:

Literal[‘none’, ‘ulysses’, ‘all_gather’, ‘ring’, ‘kv_gather’]

property resource_class: type[Resource]#

What builds this spec. Imported inside the property, so declaring a resource stays free of the manager and its kernels.

The builder, not necessarily the class built: an attention spec names AttentionManager, whose build picks a backend subclass.

shard_key: str | None = None#
split_step_refusals(node, shard, tp, sp, specs)[source]#

What this resource can’t run on node, whose steps shard splits over sp SP ranks (tp TP ranks each), among specs (every spec, by key); each a sentence.

Parameters:
Return type:

list[str]

splits_eagerly(specs)[source]#

Whether this resource’s split steps run eagerly only, their plans differing by rank where a graph holds one: a node with one captures no step that may split (SeqShardSpec.eager). False (the default) for one whose split steps replay in a graph. From raw fields, as resolve_exchange.

Parameters:

specs (Mapping[str, NodeResourceSpec])

Return type:

bool

stores_whole(kv)[source]#

Whether every rank holds this attention’s KV whole: its window layers of a pool placed by position, which keeps them whole on every partner so a decode step reads them with no collective (PagedKVConfig.placed_groups). From raw fields.

Parameters:

kv (NodeResourceSpec)

Return type:

bool

class mstar.engine.resources.attn.config.AttentionStep(segments: tuple[mstar.engine.resources.step.Segment, ...] | None = None, causal: bool = True, context_only: bool = False, fixed_layout: bool = False, *, combined_labels: dict[tuple[str, ...], str] = <factory>)[source]#

Bases: ResourceStep

Parameters:
causal: bool = True#
context_only: bool = False#
fixed_layout: bool = False#
class mstar.engine.resources.attn.config.AttnBackend(*values)[source]#

Bases: Enum

AUTO = 'auto'#
DENSE = 'dense'#
FLASHINFER = 'flashinfer'#
FLASHINFER_MLA = 'flashinfer_mla'#
FLEX = 'flex'#
XPU_PAGED = 'xpu_paged'#
class mstar.engine.resources.attn.config.BlockCausalAttentionSpec(resource_key, nodes, config, block_size=0)[source]#

Bases: AttentionSpec

Self-attention over a paged KV cache in which a row’s new tokens attend its cache block-causally: a token in block b (block_size tokens a block, from the row’s first cached token) attends every key of blocks 0..b, so what the cache holds never changes and a step runs only new tokens. A step’s new tokens start on a block. Declared with AttentionStep(causal=False).

Its own kind, not a flag on AttentionSpec: one step plans several blocks per row, on FlashInfer’s prefill kernel only. RaggedBlockCausalAttentionSpec is the same mask over one forward’s tokens, with no cache.

Parameters:
block_size: int = 0#
property resource_class: type[Resource]#

What builds this spec. Imported inside the property, so declaring a resource stays free of the manager and its kernels.

The builder, not necessarily the class built: an attention spec names AttentionManager, whose build picks a backend subclass.

class mstar.engine.resources.attn.config.CrossAttentionConfig(kv_cache, query_kv_cache=None, context_label='context', backend=AttnBackend.AUTO, flashinfer_backend='auto', decode_tensor_cores=True)[source]#

Bases: object

Cross-attention against a context written once and never extended.

kv_cache names the KV resource holding the encoder context; query_kv_cache names the decoder’s KV resource, whose plan defines this step’s query packing. They may be the same resource when the context shares the decoder’s head config — the context then lives in it under its own context_label. They differ when it does not, which is the usual case (an encoder’s head count rarely matches the decoder’s).

query_kv_cache=None covers the query side having no KV cache at all (nothing is cached across steps on it): the packing then comes off the cross-attention step’s own segments, one qo entry per segment in declared order.

Parameters:
  • kv_cache (str)

  • query_kv_cache (str | None)

  • context_label (str)

  • backend (AttnBackend)

  • flashinfer_backend (str)

  • decode_tensor_cores (bool)

backend: AttnBackend = 'auto'#
context_label: str = 'context'#
decode_tensor_cores: bool = True#
flashinfer_backend: str = 'auto'#
kv_cache: str#
query_kv_cache: str | None = None#
class mstar.engine.resources.attn.config.CrossAttentionSpec(resource_key: str, nodes: set[str], config: mstar.engine.resources.attn.config.CrossAttentionConfig)[source]#

Bases: NodeResourceSpec

Parameters:
apply_yaml_overrides(backend=None, flashinfer_backend=None, decode_tensor_cores=None)[source]#

Which kernel to run against the context cache; see AttentionSpec.

Parameters:
config: CrossAttentionConfig#
depends_on()[source]#

Keys whose specs this one builds against; the engine resolves them into EngineResourceInfo.dependencies.

Return type:

set[str]

property resource_class: type[Resource]#

What builds this spec. Imported inside the property, so declaring a resource stays free of the manager and its kernels.

The builder, not necessarily the class built: an attention spec names AttentionManager, whose build picks a backend subclass.

mstar.engine.resources.attn.config.derive_kv_groups(name, cfg, specs)[source]#

Pool name’s groups (PagedKVConfig.keep_window), from the attentions over it whose sliding_window is shorter than its max_seq_len (a longer one keeps every token), and what about them no pool can serve, before anything builds or sizes from it; a pool with a problem keeps every token. A pool that keeps a window doesn’t prefix cache yet, so its prefix_cache goes off. Run by KVSpec.derive.

Parameters:
Return type:

list[str]

mstar.engine.resources.attn.config.refuse_context_only(step, manager)[source]#

For the attention resources that don’t read context_only: planned without it, the step’s queries would attend to the tokens it appends as well.

Parameters:
Return type:

None