Developer Tools

PyTorch exposes raw memory wrapper `torch._from_blob` for advanced users

New private function bypasses buffer protocol for direct memory access.

Deep Dive

PyTorch's latest development branch now includes `torch._from_blob`, a private Python function exposing the C++ utility `at::from_blob`. Designed for advanced use cases, it enables wrapping externally managed memory—such as from C libraries, custom allocators, or device pointers—where the Python buffer protocol is unavailable. Unlike the existing `torch.frombuffer`, this function takes a raw integer address and does not hold a reference to any Python object. The caller bears full responsibility for keeping the underlying memory alive. The function is deliberately excluded from `native_functions.yaml`, meaning it lacks autograd and JIT semantics, and is underscore-prefixed to signal no backward compatibility guarantees.

The release, authored with Claude and approved by zou3519, includes test coverage for numeric dtypes, 2D reshaping, custom strides, bidirectional shared memory verification, and explicit device/dtype handling. The test suite also validates default dtype behavior and rejection of invalid devices. PyTorch explicitly warns that the signature, behavior, and even existence of `_from_blob` may change or be removed without notice. This move gives advanced developers a low-level tool for zero-copy tensor creation from non-Python memory, but with sharp edges—no garbage collection, no autograd, and no promises.

Key Points
  • Creates tensors from a raw memory address without holding a Python object reference, unlike torch.frombuffer.
  • No autograd, JIT semantics, or backward compatibility guarantees; function may be removed at any time.
  • Tested across numeric dtypes, 2D reshaping, custom strides, shared memory, and explicit device/dtype configurations.

Why It Matters

Enables low-level tensor creation from external memory sources, but demands careful memory management and offers no stability.