mstar.engine.resources.kv.config#
What a model declares about a KV cache: its shape, its spec, its step.
Kept free of the manager and its kernels so a submodule can declare a step without pulling FlashInfer in behind it.
Functions
|
The model is at fault and not the client, so this raises RuntimeError, which fails the request with a 500. |
|
Only the KV manager's ingest reads the layouts and caps, so they are dropped before the configs go out with every frame between workers. |
Classes
|
The model geometry every KV storage strategy needs, and nothing else. |
|
|
|
|
|
|
|
|
|
Fixed-size pages, appended to as a sequence grows. |
|
One write to a keyed stream, as the preprocess worker laid it out. |
|
FIFO retention for a stream that keeps committing (windowed / rolling generation): once the committed tokens behind |
|
A fixed horizon of frame slots per layer, overwritten in place. |
|
One layer's ring geometry. |
|
One ring clock per request in the step's batch. |
|
LabelLayout as the KV manager holds it. |
- class mstar.engine.resources.kv.config.KVConfig(*, num_layers, num_kv_heads, head_dim, num_qo_heads=None)[source]#
Bases:
ABCThe model geometry every KV storage strategy needs, and nothing else.
What a cache holds (layers, heads, head dim) is a checkpoint fact and lives here; how it is stored — paged or ringed, is storage policy and lives in a subclass.
KVSpecdispatches on which one it was handed.kw_onlyis load-bearing, not style:num_qo_headscarries a default here while both subclasses add required fields, which is an invalid positional dataclass field order.- abstractmethod apply_yaml_overrides(**kwargs)[source]#
Patch this deployment’s tunables; see
NodeResourceSpec.Abstract so each storage policy names exactly what it accepts and lets the rest raise — a paged key against a ring config is a typo, and the spec’s contract is that a typo is loud.
- Return type:
None
- shard(num_shards)[source]#
Narrow the head counts to one rank’s slice.
Idempotent because one KVConfig is shared by the KV resource and the attention resources planned against it, and each shards on construction.
num_shardsis the instance session size (tp * sp): Ulysses SP all-to-alls heads, so attention runs at head-degree tp*sp.- Parameters:
num_shards (int)
- Return type:
None
- class mstar.engine.resources.kv.config.KVLayout(*values)[source]#
Bases:
Enum- MLA = 'MLA'#
- NHD = 'NHD'#
- class mstar.engine.resources.kv.config.KVReqConfig(needed_labels: list[str] | None = None, needed_labels_per_node: dict[str, list[str]] = <factory>, needed_labels_per_node_walk: dict[tuple[str, str], list[str]] = <factory>, publish_labels_per_node_walk: dict[tuple[str, str], list[str]] | None = None, final_publish_labels_per_node_walk: dict[tuple[str, str], list[str]] | None = None, prefix_keys: dict[str, list[bytes]] | None = None, prefix_tail: dict[str, list[int]] | None = None, prefix_decode: dict[str, str] | None = None, prefix_cache: bool = True, kv_layout: dict[str, 'StreamLayout'] | None = None, loop_caps: dict[str, int] | None = None)[source]#
Bases:
ResourceReqConfig- Parameters:
- apply_conductor_config(prefix_keys=None, prefix_tail=None, prefix_decode=None, prefix_cache=None, kv_layout=None, loop_caps=None, **kwargs)[source]#
- kv_layout: dict[str, StreamLayout] | None = None#
- loop_room(spec=None)[source]#
The most iterations each loop can run before the request outgrows the resource built from
spec. A loop the resource doesn’t bound is left out.
- validate(spec=None)[source]#
ValueError(a 400) when a label would hold more positions at the request’s caps than its pool’smax_seq_len. A label under retention is left out, since Cosmos3, the one model that declares one, computes its positions in each forward.- Parameters:
spec (KVSpec | None)
- Return type:
None
- class mstar.engine.resources.kv.config.KVSpec(resource_key: str, nodes: set[str], config: mstar.engine.resources.kv.config.KVConfig)[source]#
Bases:
NodeResourceSpec
- class mstar.engine.resources.kv.config.KVStep(segments: tuple[mstar.engine.resources.step.Segment, ...] | None = None, commit: bool = True, combined_labels: dict[tuple[str, ...], str] = <factory>, pre_forks: tuple[tuple[str, str], ...] = (), post_forks: tuple[tuple[str, str], ...] = (), fork_rids: frozenset | None = None, sink: frozenset[tuple[str, str]] = frozenset(), retention: collections.abc.Mapping[tuple[str, str], mstar.engine.resources.kv.config.RetentionPolicy] = <factory>)[source]#
Bases:
ResourceStep- Parameters:
- class mstar.engine.resources.kv.config.PagedKVConfig(*, num_layers, num_kv_heads, head_dim, num_qo_heads=None, max_seq_len, max_num_pages=2048, page_size=128, layout=KVLayout.NHD, cpu_offload_pages=0, prefix_cache_salt='', prefix_cache=True, prefix_cache_tp=False, kv_lora_rank=None, qk_rope_head_dim=None)[source]#
Bases:
KVConfigFixed-size pages, appended to as a sequence grows. The default.
- Parameters:
- class mstar.engine.resources.kv.config.PrefixSpan(length, advance, walk, scratch=0, digest=None, end_key=None)[source]#
Bases:
objectOne write to a keyed stream, as the preprocess worker laid it out.
lengthis how many slots the write fills andadvanceis how far it moves the position counter.digestis an item’s digest, and None for ids.end_keyis set for an item that ends inside a page, and keys that page up to the item’s end.- Parameters:
- class mstar.engine.resources.kv.config.RetentionPolicy(context_budget, protected_prefix=0)[source]#
Bases:
objectFIFO retention for a stream that keeps committing (windowed / rolling generation): once the committed tokens behind
protected_prefixexceedcontext_budget, the oldest unprotected pages are released.A committing step declares it for its stream on
KVStep.retentionandKVManager.commitapplies it, so the release happens between steps as far as every planner is concerned: a pre-plan of the next step gates on this commit and sees the compacted stream. Whole pages only (the page straddling the prefix boundary and a partial tail page stay), so the realized context can run over the budget by up to a page; the excess is re-offered at the next commit.protected_prefixtokens at the head (a text prompt, say) are never released.
- class mstar.engine.resources.kv.config.RingKVConfig(*, num_layers, num_kv_heads, head_dim, num_qo_heads=None, tokens_per_frame, layers, num_sessions=1)[source]#
Bases:
KVConfigA fixed horizon of frame slots per layer, overwritten in place.
Every layer’s storage is allocated once and reused for the life of the process, and a write to an occupied slot is the intended behaviour.
- Parameters:
- apply_yaml_overrides(num_sessions=None, **kwargs)[source]#
num_sessionsonly. Nothing else here is a deployment knob.- Parameters:
num_sessions (int | None)
- Return type:
None
- layers: tuple[RingKVLayerConfig, ...]#
- property total_sessions: int#
Resident sessions plus one shared scratch session for padding rows.
A replay padded to its capture bucket parks the dummy tail on this session (see
RingKVManager.plan); it is never handed to a request, so resident capacity staysnum_sessionsand the deployment knob keeps its meaning. The ring buffer and the flex mask both size on this count so the padding session is a real, addressable span.
- class mstar.engine.resources.kv.config.RingKVLayerConfig(ring_frames, ring_buckets, pinned_dilation)[source]#
Bases:
objectOne layer’s ring geometry. Layers can hold frames at different strides.
- class mstar.engine.resources.kv.config.RingKVStep(segments=None, *, frames)[source]#
Bases:
ResourceStepOne ring clock per request in the step’s batch.
- class mstar.engine.resources.kv.config.StreamLayout(spans=(), growth=(), scratch=0, retention=None, keyed_spans=0)[source]#
Bases:
objectLabelLayout as the KV manager holds it.
- Parameters:
spans (tuple[PrefixSpan, ...])
scratch (int)
retention (RetentionPolicy | None)
keyed_spans (int)
- pages(caps, page_size)[source]#
The most pages the stream can hold, where
capsgives each growth loop’s iterations.Under
retentionthe stream holds a window. KVManager._apply_retention leaves less than a page over the budget, so the window peaks at the protected prefix plus the budget, or at the prefix rounded up to a page if that is larger. The bound adds a page of overrun and the largest step past the prefix to that peak.
- retention: RetentionPolicy | None = None#
- spans: tuple[PrefixSpan, ...] = ()#
- mstar.engine.resources.kv.config.refuse_undeclared_labels(model, declared, kv_layout, configs)[source]#
The model is at fault and not the client, so this raises RuntimeError, which fails the request with a 500.
- mstar.engine.resources.kv.config.without_layouts(configs)[source]#
Only the KV manager’s ingest reads the layouts and caps, so they are dropped before the configs go out with every frame between workers.
- Parameters:
configs (Mapping[str, ResourceReqConfig])
- Return type: