{"id":20040837,"url":"https://github.com/vadimkantorov/dotnetlibtorch","last_synced_at":"2025-05-05T08:32:10.574Z","repository":{"id":146466420,"uuid":"288252919","full_name":"vadimkantorov/dotnetlibtorch","owner":"vadimkantorov","description":"The simplest possible interfacing of C# and libtorch via DLPack P/Invoke wrapper around C function and structures","archived":false,"fork":false,"pushed_at":"2020-09-07T07:45:49.000Z","size":64,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T19:47:23.567Z","etag":null,"topics":["csharp","dlpack","dotnet","libtorch","pinvoke","pytorch","torchscript"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vadimkantorov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-17T18:11:46.000Z","updated_at":"2025-04-07T05:15:20.000Z","dependencies_parsed_at":"2023-03-27T13:32:47.611Z","dependency_job_id":null,"html_url":"https://github.com/vadimkantorov/dotnetlibtorch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimkantorov%2Fdotnetlibtorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimkantorov%2Fdotnetlibtorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimkantorov%2Fdotnetlibtorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vadimkantorov%2Fdotnetlibtorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vadimkantorov","download_url":"https://codeload.github.com/vadimkantorov/dotnetlibtorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252466759,"owners_count":21752428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dlpack","dotnet","libtorch","pinvoke","pytorch","torchscript"],"created_at":"2024-11-13T10:44:09.579Z","updated_at":"2025-05-05T08:32:10.567Z","avatar_url":"https://github.com/vadimkantorov.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"In this [example](./dotnetlibtorch.cs):\n1. Export traced and scripted models from PyTorch code in Python ([`dotnetlibtorch.py`](./dotnetlibtorch.py))\n2. Create a DLPack tensor in C# ([`dotnetlibtorch.cs`](./dotnetlibtorch.cs))\n3. Pass it to C function that uses libtorch and provided model to process the input ([`dotnetlibtorch.cpp`](./dotnetlibtorch.cpp))\n4. Display the returned tensor in C# ([`dotnetlibtorch.cs`](./dotnetlibtorch.cs))\n\n### Build instructions\n```shell\n# build C++ library with an exported C function that consumes a DLPack tensor and returns a DLPack tensor\n# C++ library dotnetlibtorch.cpp adds 1 to the passed tensor\nCMAKE_PREFIX_PATH=$(python3 -c 'import torch; print(torch.__path__[0])')\nmkdir -p build\npushd build\ncmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=\"$CMAKE_PREFIX_PATH\" ..\nmake\npopd\n```\n\n### Running the example\n```shell\n# dump export a linear PyTorch model in two versions: jit_scripted_model.pt and jit_traced_model.pt\npython3 dotnetlibtorch.py\n\n# run C# caller that only does processing in PyTorch code\ndotnet run dotnetlibtorch.cs\n\n# run C# caller that uses JIT scripted model for processing\ndotnet run dotnetlibtorch.cs jit_scripted_model.pt\n\n# run C# caller that uses JIT scripted model for processing\ndotnet run dotnetlibtorch.cs jit_traced_model.pt\n```\n\n### Example output\n```\nBefore passing to libtorch\ntype_code=kDLInt, bits=32, lanes=1, ndim=2, shape=[2,3], strides=[3,1]\n(0, 0) = 0\n(0, 1) = 1\n(0, 2) = 2\n(1, 0) = 3\n(1, 1) = 4\n(1, 2) = 5\nAfter passing to libtorch\ntype_code=kDLInt, bits=32, lanes=1, ndim=2, shape=[2,3], strides=[3,1]\n(0, 0) = 1\n(0, 1) = 2\n(0, 2) = 3\n(1, 0) = 4\n(1, 1) = 5\n(1, 2) = 6\nCalled deleter\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvadimkantorov%2Fdotnetlibtorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvadimkantorov%2Fdotnetlibtorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvadimkantorov%2Fdotnetlibtorch/lists"}