mstar.engine.resources.kv.cache#
Classes
|
Owns the KV storage and all layout-dependent addressing, so that consumers (KV transfer, attention) stay layout-agnostic. |
|
Simple page allocator using a FIFO queue of free page indices. |
- class mstar.engine.resources.kv.cache.KVCache(cfg, device, dtype=torch.bfloat16)[source]#
Bases:
objectOwns the KV storage and all layout-dependent addressing, so that consumers (KV transfer, attention) stay layout-agnostic.
- chunk_ptrs(layer_idx, page_idx, token_start, token_end, base_ptr=None)[source]#
Byte pointers to the contiguous chunks holding tokens [token_start, token_end) of one page of one layer, plus the size of each chunk. One pointer per chunk (K and V are separate under NHD).
base_ptraddresses a remote cache with this same layout/config; it defaults to this cache’s own storage.
- chunk_view(layer_idx, page_idx, token_start, token_end, tensor=None)[source]#
View of tokens [token_start, token_end) of one page of one layer, covering both K and V.
tensoroverrides the backing storage (e.g. a tensor rebuilt from another process’ cache with this same layout).
- copy_pages(src_pages, dst_pages)[source]#
Copy whole pages (every layer, both K and V, all tokens) within this cache:
src_pages[i]->dst_pages[i].
- layer_view(layer_idx)[source]#
One layer’s pages, in this cache’s layout — what an attention kernel consumes. NHD: [max_num_pages, 2, page_size, num_kv_heads, head_dim].
- read_tokens(layer_idx, page_idx, cache_idx)[source]#
Gather the (page, offset-in-page) slots written by
write_tokens. Returns [num_tokens, 2, num_kv_heads, head_dim] (K at index 0, V at 1); it is a gather, so a copy rather than a view.
- class mstar.engine.resources.kv.cache.PageAllocator(max_num_pages)[source]#
Bases:
objectSimple page allocator using a FIFO queue of free page indices.
Thread-safe: a
threading.Lockmakes the qsize-then-get sequence inallocate/try_allocateatomic against concurrentfreecalls. Required by the pre-plan path, where the plan thread runstry_allocatewhile the GPU thread runsfreefromreset_label— the unlocked qsize/get pair could false-negative (return None when pages are about to be freed) or partially fill the output list under multi-consumer contention.- Parameters:
max_num_pages (int)