{"id":5732,"library":"torch-c-dlpack-ext","title":"PyTorch C DLPack Extension","description":"An internal PyTorch C Extension that facilitates interoperability between PyTorch tensors and C/C++ libraries via DLPack. It is not intended for direct external use by end-users, primarily serving as a utility within the PyTorch ecosystem. Version 0.1.5 is the current release, and its release cadence is tied to PyTorch's internal development.","status":"active","version":"0.1.5","language":"en","source_language":"en","source_url":"https://github.com/pytorch/torch-c-dlpack-ext","tags":["pytorch","dlpack","c-extension","interoperability","internal-utility"],"install":[{"cmd":"pip install torch-c-dlpack-ext","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"Peer dependency; this library is a PyTorch C extension and requires PyTorch for its core functionality.","package":"torch","optional":false}],"imports":[{"note":"These are the primary functions exposed. Note that this is an internal utility and direct external usage is generally discouraged by the PyTorch project.","symbol":"to_dlpack, from_dlpack","correct":"from torch_c_dlpack_ext import to_dlpack, from_dlpack"}],"quickstart":{"code":"import torch\n\n# WARNING: This library is primarily an internal utility of PyTorch\n# and is not intended for direct use by external users. The API may change\n# without warning and may not follow semantic versioning for external consumption.\ntry:\n    from torch_c_dlpack_ext import to_dlpack, from_dlpack\nexcept ImportError:\n    print(\"ERROR: torch-c-dlpack-ext not installed or failed to import.\")\n    print(\"This library is an internal utility and not meant for direct use.\")\n    print(\"Please ensure 'torch' is also installed.\")\n    exit(1)\n\nprint(\"Successfully imported torch_c_dlpack_ext (internal utility).\")\n\n# 1. Create a PyTorch tensor\ntensor_orig = torch.randn(2, 3)\nprint(f\"\\nOriginal PyTorch Tensor:\\n{tensor_orig}\")\n\n# 2. Convert to DLPack capsule\ndlpack_capsule = to_dlpack(tensor_orig)\nprint(f\"\\nDLPack Capsule (type): {type(dlpack_capsule)}\")\n\n# 3. Convert back to a PyTorch tensor\ntensor_restored = from_dlpack(dlpack_capsule)\nprint(f\"\\nRestored PyTorch Tensor (from DLPack):\\n{tensor_restored}\")\n\n# 4. Verify data (DLPack shares underlying memory, so data should be identical)\nprint(f\"\\nAre restored and original tensor data close? {torch.allclose(tensor_orig, tensor_restored)}\")\nprint(f\"Are restored and original tensors the exact same Python object? {tensor_orig is tensor_restored}\")\n\n# Expected output for last line: False, as 'from_dlpack' creates a new Python Tensor object\n# sharing the same underlying data.","lang":"python","description":"Demonstrates converting a PyTorch tensor to a DLPack capsule and back, showcasing the `to_dlpack` and `from_dlpack` functions. This example emphasizes that the library is an internal utility not typically used directly by end-users."},"warnings":[{"fix":"Avoid direct dependency if possible; rely on PyTorch's internal use of this library. If direct usage is necessary, pin versions strictly and be prepared for breaking changes.","message":"This library is primarily an internal utility of PyTorch and is not intended for direct use by external users. Its API may change without warning, and it may not follow standard semantic versioning conventions for external consumption.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `pip install torch` (or `conda install pytorch`) is executed in your environment before using this library.","message":"Functionality heavily relies on PyTorch being installed. Ensure `torch` is available in your environment, as it's a peer dependency not explicitly listed in `install_requires`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure you are passing `torch.Tensor` instances to `to_dlpack` and a DLPack capsule object (returned by `to_dlpack`) to `from_dlpack`.","message":"The `to_dlpack` and `from_dlpack` functions are designed to operate on `torch.Tensor` objects. Passing objects of other types will result in runtime errors, often from the underlying C extension.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}