mstar.engine.resources.kv.page_copier#
Page copies between KV caches: the device cache and the host page pool, or two device caches (CUDA IPC KV transfer).
Both caches are laid out [layers, pages, ...], so a page is one contiguous
block per layer, a layer stride apart. copy_ of that strided view between
device and pinned host memory blocks the host until the copy stream drains and
runs at a fraction of the link’s bandwidth. On CUDA and ROCm each run of pages
adjacent in both caches is instead one cudaMemcpy2DAsync / hipMemcpy2DAsync
with the layer stride as its pitch: async, and run on the copy engines.
make_page_copier picks the first copier that loads and passes a round trip, once per process: on CUDA CudartCopier, then CudaBindingsCopier, whose calls drop the GIL; on ROCm HipCopier; then TorchCopier.
Functions
The fastest copier that works here; TorchCopier off CUDA and ROCm. |
Classes
Through |
|
libcudart, the runtime torch loaded, through |
|
libamdhip64 on ROCm, where |
|
Copies every layer of |
|
Per-page |
- class mstar.engine.resources.kv.page_copier.CudaBindingsCopier[source]#
Bases:
_Memcpy2DCopierThrough
cuda.bindings, which Linux CUDA builds of torch 2.10+ require.- name = 'cuda.bindings'#
- class mstar.engine.resources.kv.page_copier.CudartCopier[source]#
Bases:
_MappedRuntimeCopierlibcudart, the runtime torch loaded, through
ctypes.PyDLLso each call keeps the GIL. Dropping it costs up to a switch interval per call while another thread runs Python. A call that waits on the driver stalls every Python thread.- name = 'libcudart'#
- class mstar.engine.resources.kv.page_copier.HipCopier[source]#
Bases:
_MappedRuntimeCopierlibamdhip64 on ROCm, where
copy_of a strided page blocks the host too.- name = 'libamdhip64'#
- class mstar.engine.resources.kv.page_copier.PageCopier[source]#
Bases:
objectCopies every layer of
src_pages[i]ofsrcontodst_pages[i]ofdst, queued onstream.- copy_rows(dst, src, stream)[source]#
dst.copy_(src)queued onstream, for two views of the same shape whose rows ([i]) are each contiguous: e.g. a page or a token range of it, every layer.
- name = 'base'#
- class mstar.engine.resources.kv.page_copier.TorchCopier[source]#
Bases:
PageCopierPer-page
copy_: works everywhere, but on CUDA blocks the host.- copy_rows(dst, src, stream)[source]#
dst.copy_(src)queued onstream, for two views of the same shape whose rows ([i]) are each contiguous: e.g. a page or a token range of it, every layer.
- name = 'torch'#