https://github.com/amirmardan/pytorch_extending_cpp_binding
Binding C++ to PyTorch and extending PyTorch
https://github.com/amirmardan/pytorch_extending_cpp_binding
autograd cpp-bindings pytorch-examples pytorch-extension pytorch-learning torch-cpp
Last synced: 6 months ago
JSON representation
Binding C++ to PyTorch and extending PyTorch
- Host: GitHub
- URL: https://github.com/amirmardan/pytorch_extending_cpp_binding
- Owner: AmirMardan
- Created: 2022-10-30T02:27:27.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-02T15:08:44.000Z (about 3 years ago)
- Last Synced: 2024-04-28T05:11:37.648Z (over 1 year ago)
- Topics: autograd, cpp-bindings, pytorch-examples, pytorch-extension, pytorch-learning, torch-cpp
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
Extending Torch and binding C++ function
========================================
We can extend the autograd operation in both Python and C++ which is discussed in folders `example_extend_in_python` and `example_load_multifile_cpp`.
To bind C++ to Python, we can use just-in-time (JIT) method.
For this purpose, we write our functions in C++ and at the end of the C++ file, we define the functions that need to be wrapped using pybind11 as
```C++
TORCH_LIBRARY(module_name, m) {
m.def("name_of_function_in_python", &name_of_function_in_cpp);
}
```
Then, we load C++ files as
```Python
from torch.utils.cpp_extension import load
module = load(
name='module_name',
sources=['source1.cpp',
'source2.cu'],
extra_cflags=['-O2'],
verbose=True)
```
and now, we can call the created function as
```Python
add = torch.ops.module_name.name_of_function_in_python
```
Reference
==========
- [Torch C++ Extension](https://pytorch.org/docs/stable/cpp_extension.html#torch-utils-cpp-extension)