https://github.com/vadimkantorov/dotnetlibtorch
The simplest possible interfacing of C# and libtorch via DLPack P/Invoke wrapper around C function and structures
https://github.com/vadimkantorov/dotnetlibtorch
csharp dlpack dotnet libtorch pinvoke pytorch torchscript
Last synced: 5 months ago
JSON representation
The simplest possible interfacing of C# and libtorch via DLPack P/Invoke wrapper around C function and structures
- Host: GitHub
- URL: https://github.com/vadimkantorov/dotnetlibtorch
- Owner: vadimkantorov
- Created: 2020-08-17T18:11:46.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-07T07:45:49.000Z (about 5 years ago)
- Last Synced: 2025-04-08T19:47:23.567Z (6 months ago)
- Topics: csharp, dlpack, dotnet, libtorch, pinvoke, pytorch, torchscript
- Language: C#
- Homepage:
- Size: 62.5 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
In this [example](./dotnetlibtorch.cs):
1. Export traced and scripted models from PyTorch code in Python ([`dotnetlibtorch.py`](./dotnetlibtorch.py))
2. Create a DLPack tensor in C# ([`dotnetlibtorch.cs`](./dotnetlibtorch.cs))
3. Pass it to C function that uses libtorch and provided model to process the input ([`dotnetlibtorch.cpp`](./dotnetlibtorch.cpp))
4. Display the returned tensor in C# ([`dotnetlibtorch.cs`](./dotnetlibtorch.cs))### Build instructions
```shell
# build C++ library with an exported C function that consumes a DLPack tensor and returns a DLPack tensor
# C++ library dotnetlibtorch.cpp adds 1 to the passed tensor
CMAKE_PREFIX_PATH=$(python3 -c 'import torch; print(torch.__path__[0])')
mkdir -p build
pushd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" ..
make
popd
```### Running the example
```shell
# dump export a linear PyTorch model in two versions: jit_scripted_model.pt and jit_traced_model.pt
python3 dotnetlibtorch.py# run C# caller that only does processing in PyTorch code
dotnet run dotnetlibtorch.cs# run C# caller that uses JIT scripted model for processing
dotnet run dotnetlibtorch.cs jit_scripted_model.pt# run C# caller that uses JIT scripted model for processing
dotnet run dotnetlibtorch.cs jit_traced_model.pt
```### Example output
```
Before passing to libtorch
type_code=kDLInt, bits=32, lanes=1, ndim=2, shape=[2,3], strides=[3,1]
(0, 0) = 0
(0, 1) = 1
(0, 2) = 2
(1, 0) = 3
(1, 1) = 4
(1, 2) = 5
After passing to libtorch
type_code=kDLInt, bits=32, lanes=1, ndim=2, shape=[2,3], strides=[3,1]
(0, 0) = 1
(0, 1) = 2
(0, 2) = 3
(1, 0) = 4
(1, 1) = 5
(1, 2) = 6
Called deleter
```