Developer Tools

PyTorch exposes torch._from_blob for raw memory wrapping without buffer protocol

New private function bypasses Python object references for direct C memory integration.

Deep Dive

PyTorch has merged pull request #185850, authored with Claude, which exposes the internal C++ function `at::from_blob` to Python as `torch._from_blob`. This private function is designed for advanced users who need to wrap externally managed memory—such as memory from C libraries, custom allocators, or device pointers—where the standard Python buffer protocol is unavailable. Unlike `torch.frombuffer`, which requires a Python buffer-like object and holds a reference to it, `torch._from_blob` takes only a raw integer memory address. This means no Python object reference is maintained; the caller is solely responsible for ensuring the underlying memory remains valid throughout the tensor's lifetime.

The function is deliberately not registered in `native_functions.yaml`, so it has no autograd or JIT semantics. The underscore prefix signals that it offers absolutely no backward compatibility guarantees—its signature, behavior, and even existence may change or be removed without notice in any future release. Tests cover basic creation across numeric dtypes, 2D reshape, custom strides, bidirectional shared memory verification, explicit device/dtype, default dtype behavior, and invalid device rejection. This is a low-level tool for high-performance or unconventional memory management scenarios.

Key Points
  • Takes raw integer address instead of Python buffer object, with no reference holding.
  • No autograd, JIT, or stability guarantees—underscore-prefixed private function.
  • Targets advanced use cases like custom allocators, device pointers, or C library integration.

Why It Matters

Enables direct tensor creation from C-managed memory, critical for performance-critical or non-Python memory scenarios.