https://github.com/vadimkantorov/dotnetdlpack
C# p/invoke bindings for DLPack
https://github.com/vadimkantorov/dotnetdlpack
csharp dlpack dotnet pinvoke
Last synced: 7 months ago
JSON representation
C# p/invoke bindings for DLPack
- Host: GitHub
- URL: https://github.com/vadimkantorov/dotnetdlpack
- Owner: vadimkantorov
- Created: 2020-08-17T13:57:29.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-18T11:52:00.000Z (about 5 years ago)
- Last Synced: 2025-01-12T19:22:03.013Z (9 months ago)
- Topics: csharp, dlpack, dotnet, pinvoke
- Language: C
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
P/Invoke bindings for [DLPack](http://github.com/dmlc/dlpack). It demonstrates consuming from C# a DLPack tensor produced in a C function.
Hopefully there will be official bindings: https://github.com/dmlc/dlpack/issues/56 and https://github.com/microsoft/onnxruntime/issues/4162.
Currently there is no example for constructing a DLPack tensor in C# and sending it to a C function.
Limitations: due to [`ReadOnlySpan`](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1.-ctor?view=netcore-3.1#System_ReadOnlySpan_1__ctor_System_Void__System_Int32_) supporting only Int32 lengths, the `ROS`-returning method is called `DataSpanLessThan2Gb()`. Issues: https://github.com/dotnet/corefxlab/issues/896 and https://github.com/dotnet/apireviews/tree/master/2016/11-04-SpanOfT#spant-and-64-bit. This code assumes Linux, but could be made to work on Windows with a different Makefile.
More advanced example with libtorch (using the same [`dlpack.cs`](./dlpack.cs): https://github.com/vadimkantorov/dotnetlibtorch
### Install .NET Core 3.1 SDK on Ubuntu 18.04
From https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#1804- :
```shell
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.debsudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-3.1
```### Usage
```shell
make dlpack
./dlpack golden.binmake libdlpack.so
dotnet run test.cs csharp.bin#ndim 2
#shape 3 2
#(0, 0) = 0
#(0, 1) = 1
#(1, 0) = 2
#(1, 1) = 3
#(2, 0) = 4
#(2, 1) = 5
#Deleter calling
#Deleter calleddiff golden.bin csharp.bin
```