mstar.engine.resources.dsa.manager#
DeepSeek sparse attention (DSA) over the paged MLA cache: the index keys, each query’s top-k and the attention over it.
A model’s FULL indexer layers hand this resource their tokens’ index keys (store_keys) and
their queries and head weights (select): each query row scores its request’s keys,
sum_h w[h] * relu(q[h] . k) in fp32, and keeps its top topk positions. SHARED layers
reuse the last FULL layer’s pick (shared_selection), and attend runs absorbed MLA over
just the picked latents of the MLA cache. A step’s rows come from the KV plan: each request’s
rows, the position they start at and its pages.
The keys live in per-request buffers that grow with the context and the selection runs per
request on the host, so the path is eager only; past topk it decodes (one row per request).
The follow-up moves the keys onto the MLA cache’s pages, under its page table, allocator and
eviction, and gives selection and attention fixed-shape buffers so a decode can capture.
Functions
|
Top- |
|
Per-row top- |
Classes
|
DSA for one node's layers over its MLA cache ( |
|
A label's rows this step. |
|
One request's rows of a step: |
Per-request, per-layer growing buffers of the indexer's keys, in position order. |
- class mstar.engine.resources.dsa.manager.DSAManager(config, page_size, device)[source]#
Bases:
ResourceDSA for one node’s layers over its MLA cache (
config.kv_cache).- attend(selection, q_nope, q_pe, latent_layer, sm_scale, label=_MAIN)[source]#
Absorbed MLA of each row over its picked latents,
(T, H, kv_lora_rank).latent_layeris the MLA cache’s layer view[pages, page_size, kv_lora_rank + rope], this step’s latents already written;selectionholds request-local positions (-1: none). Pasttopka request decodes, so rowris requestr’s.
- classmethod build(spec, info)[source]#
- Parameters:
spec (DSASpec)
info (EngineResourceInfo)
- plan(step, ctx)[source]#
Each label’s rows, as the KV plan placed them: a request’s rows follow the one before it, and its first sits at its length before the step.
- Parameters:
step (DSAStep)
ctx (StepContext)
- Return type:
- select(layer_idx, q, weights, label=_MAIN)[source]#
Each row’s top
topkpositions of its request (index_topk) over this layer’s stored keys,(T, topk)int32, -1 padded; SHARED layers reuse it this forward.
The last FULL layer’s pick this forward, for a SHARED layer (IndexShare).
- class mstar.engine.resources.dsa.manager.DSAPlan(spans, needs_selection, last_selection=None, pages=None, row_page_offset=None, row_start=None)[source]#
Bases:
objectA label’s rows this step.
needs_selection: some row sees more thantopkkeys;last_selection: the last FULL layer’s pick this forward, which SHARED layers reuse.Under selection every row is its request’s one decode row, and
pagesholds the rows’ page tables back to back on the device: rowr’s starts atrow_page_offset[r], and its first token sitsrow_start[r]slots into it.- Parameters:
- class mstar.engine.resources.dsa.manager.DSASpan(request_id, q_start, q_len, ctx_start, pages, page_start=0)[source]#
Bases:
objectOne request’s rows of a step:
q_lenrows from rowq_start, the first at positionctx_start;pagesits MLA cache pages,page_startits first token’s offset.- Parameters:
- class mstar.engine.resources.dsa.manager.IndexKeyStore[source]#
Bases:
objectPer-request, per-layer growing buffers of the indexer’s keys, in position order.
- append(request_id, layer_idx, keys, start_pos)[source]#
Append one chunk of
(n, head_dim)keys at positionsstart_pos ...
- evict(request_id)[source]#
Drop all of a request’s buffers. Idempotent — the engine may retire a request that never reached a DSA forward.
- Parameters:
request_id (str)
- Return type:
None
- history(request_id, layer_idx, length)[source]#
The first
lengthstored keys — a view, not a copy. Raises if fewer are stored (a scoring window must never exceed the appended history: the current chunk is appended before selection).
- mstar.engine.resources.dsa.manager.index_topk(q, weights, keys, positions, topk)[source]#
Top-
topkkey positions per query row,(T, topk)int32, -1 padded.q(T, H, D)andweights(T, H)are the indexer’s queries and head weights (any scales folded in),keys(S, D)a request’s keys by position andpositions(T,)the rows’ own: a row sees keys0 .. position, itself included.
- mstar.engine.resources.dsa.manager.select_topk_causal(scores, positions, topk)[source]#
Per-row top-
topkkey positions of causally maskedscores.scoresis(T, num_keys)with every key past a row’s position already-inf; the result is(T, topk)int32, -1 padded where a row sees fewer thantopkkeys. One stable sort for the whole batch: no per-token topk launch, no host sync. Ties break toward the earlier key position, so a row’s selection does not depend on how the batch is packed or on which device ran it (torch.topk’s tie order is neither).