Skip to content

Commit 8ddcadc

Browse files
authored
feat: reduce startup time for cpu cache (#1146)
1 parent aff4049 commit 8ddcadc

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

lightllm/utils/kv_cache_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ def _get_default_hugepage_size() -> int:
189189
raise Exception("Error attaching shared memory")
190190
logger.info(f"Shared cpu kv cache tensor memory at address: {shm_addr}")
191191

192+
# Best-effort memory prefaulting in background to speed up subsequent cudaHostRegister
193+
def _pre_warm_memory():
194+
page_size = _get_default_hugepage_size() if use_hugetlb else 4096
195+
arr = np.ctypeslib.as_array(ctypes.cast(shm_addr, ctypes.POINTER(ctypes.c_uint8)), shape=(size_to_alloc,))
196+
volatile_sum = int(arr[::page_size].sum())
197+
logger.info(f"pre warmed shared memory pages successfully, checksum={volatile_sum})")
198+
199+
th = threading.Thread(target=_pre_warm_memory, name="cpu_cache_pre_warm", daemon=True)
200+
th.start()
201+
192202
return shm_addr
193203

194204

0 commit comments

Comments
 (0)