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,
)
| 136 | |
| 137 | |
| 138 | def _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 | # ====================================================================== # |
no test coverage detected