← all repositories
dotnet/TorchSharp

PyTorch moves in with C#—and brings its own naming conventions

TorchSharp wraps LibTorch so .NET developers can write PyTorch-style deep learning code without switching languages.

TorchSharp
Not currently ranked — collecting fresh signals.
star history

What it does

TorchSharp is a thin .NET wrapper around LibTorch, the C++ engine that powers PyTorch. It exposes tensors, neural network layers, and optimizers to C# and F# with a deliberate bias toward the Python API surface. The project describes itself as “no more, no less” than a wrapper, and underpins higher-level libraries like DiffSharp.

The interesting bit

The maintainers made the slightly heretical choice to discard .NET naming conventions in favor of Python style—so Conv1d lives in a nested Modules namespace and factory methods mirror PyTorch constructors. They also enforce proper .NET enums where PyTorch uses magic strings, letting the static type system catch errors that Python would miss.

Key highlights

  • Binds LibTorch with a focus on tensors and the torch.nn module API
  • Deliberately copies Python naming to ease porting PyTorch examples into .NET
  • Leverages .NET method overloading and enum types where they improve on Python’s dynamic approach
  • Ships via NuGet with bundled CPU and CUDA 12.1 backends for Linux and Windows, plus Apple Silicon macOS
  • Powers DiffSharp, a differentiable programming library for .NET

Caveats

  • Intel Macs are no longer supported as of version 0.103.0; only Apple Silicon remains
  • The project is pre-v1.0 and explicitly warns that breaking changes will continue
  • .NET Framework projects on Windows must target x64, not Any CPU
  • Direct copy-paste from Python is still blocked by C# syntax differences, such as named-parameter syntax

Verdict

Worth a look if you are committed to the .NET ecosystem but need native PyTorch model training. If you are already productive in Python, or you deploy to older Intel Macs, this adds friction you may not need.

Frequently asked

What is dotnet/TorchSharp?
TorchSharp wraps LibTorch so .NET developers can write PyTorch-style deep learning code without switching languages.
Is TorchSharp open source?
Yes — dotnet/TorchSharp is open source, released under the MIT license.
What language is TorchSharp written in?
dotnet/TorchSharp is primarily written in C#.
How popular is TorchSharp?
dotnet/TorchSharp has 1.8k stars on GitHub.
Where can I find TorchSharp?
dotnet/TorchSharp is on GitHub at https://github.com/dotnet/TorchSharp.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.