MCPcopy Create free account
hub / github.com/LMCache/LMCache / _alloc_page_aligned_pinned_view

Function _alloc_page_aligned_pinned_view

lmcache/python_ops_fallback.py:380–397  ·  view source on GitHub ↗

Allocate a pinned CPU buffer whose first usable byte is page-aligned, and return a torch view of ``size`` bytes plus its base pointer. Internally over-allocates one extra page on a backing tensor, then slices the aligned region out. The slice shares storage with the backing ten

(size: int)

Source from the content-addressed store, hash-verified

378
379
380def _alloc_page_aligned_pinned_view(size: int) -> Tuple[torch.Tensor, int]:
381 """
382 Allocate a pinned CPU buffer whose first usable byte is page-aligned,
383 and return a torch view of ``size`` bytes plus its base pointer.
384
385 Internally over-allocates one extra page on a backing tensor, then
386 slices the aligned region out. The slice shares storage with the
387 backing tensor, so keeping the slice alive keeps the underlying
388 memory alive (no need to track the backing tensor separately).
389 """
390 backing = torch.empty(size + _PAGE_SIZE, dtype=torch.uint8, pin_memory=False)
391 # First-touch initialization on the entire backing region
392 backing.fill_(0)
393 base = backing.data_ptr()
394 # Distance from `base` to the next page boundary (0..PAGE_SIZE-1).
395 offset = (-base) % _PAGE_SIZE
396 aligned_view = backing[offset : offset + size]
397 return aligned_view, aligned_view.data_ptr()
398
399
400def alloc_pinned_numa_ptr(size: int, numa_id: int = 0) -> int:

Callers 2

alloc_pinned_numa_ptrFunction · 0.85
alloc_pinned_ptrFunction · 0.85

Calls 1

data_ptrMethod · 0.45

Tested by

no test coverage detected