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

Function _tensor_from_cpu_ptr

lmcache/python_ops_fallback.py:138–152  ·  view source on GitHub ↗

Zero-copy CPU tensor from a raw host pointer via ctypes + torch.frombuffer.

(
    ptr: int,
    shape: tuple[int, ...],
    dtype: torch.dtype,
    numel: int,
    total_bytes: int,
)

Source from the content-addressed store, hash-verified

136
137
138def _tensor_from_cpu_ptr(
139 ptr: int,
140 shape: tuple[int, ...],
141 dtype: torch.dtype,
142 numel: int,
143 total_bytes: int,
144) -> torch.Tensor:
145 """
146 Zero-copy CPU tensor from a raw host pointer via ctypes + torch.frombuffer.
147
148 """
149 buffer_type = ctypes.c_uint8 * total_bytes
150 buf = buffer_type.from_address(ptr)
151 # torch.frombuffer is zero-copy for contiguous byte buffers on CPU.
152 return torch.frombuffer(buf, dtype=dtype).view(*shape)
153
154
155# ====================================================================== #

Callers 1

_tensor_from_ptrFunction · 0.85

Calls 1

viewMethod · 0.80

Tested by

no test coverage detected