How dotnet/torchsharp works
Project type and purpose
• This is not an end-user application but an open-source .NET wrapper library over PyTorch (LibTorch). It provides managed bindings (TorchSharp) plus two “extension” libraries (TorchAudio and TorchVision), delivered as NuGet packages. It also includes examples (C# and F#), native‐code glue, packaging projects, tests, and documentation.
File structure
• Top-level solution (TorchSharp.sln) with Directory.Build.props/targets for common MSBuild settings.
• build/ and azure-pipelines.yml: CI build orchestration and signing.
• src/Native: CMake‐based native glue layer (LibTorchSharp C++ code) that builds a small adaptor DLL for PInvoke.
• src/TorchSharp: managed code organized into sub-folders (PInvoke, JIT, NN, Tensor, Optimizers, Utils, etc.). PInvoke layer directly maps to the native adaptor.
• src/TorchAudio and src/TorchVision: extension libraries built on top of TorchSharp core.
• src/Examples and src/FSharp.Examples: sample applications illustrating usage.
• src/Redist: rules for shipping native LibTorch binaries.
• pkg/: MSBuild nupkgproj files to generate NuGet packages (TorchSharp, TorchAudio, TorchVision, backends).
• test/: xUnit tests and notebooks validating managed + native behavior.
• docfx/: static site generator content for documentation.
README for additional insights
• Confirms it’s a “wrapper library” staying close to the PyTorch API, distributed via NuGet.
• Highlights supported backends (CPU/CUDA), packaging strategy, and developer notes.
Main components and their relationships
a. Native Layer
• External LibTorch binaries (downloaded via NuGet or installed locally)
• src/Native: builds LibTorchSharp adaptor library via CMake (Windows/Linux/macOS)
Relationship: Managed code calls into this adaptor via PInvoke.
b. PInvoke Layer
• src/TorchSharp/PInvoke: auto-generated and handwritten C# signatures for the native adaptor
Relationship: Exposes low-level LibTorch APIs to the managed world.
c. Managed Wrapper
• Core: src/TorchSharp (Tensor, NN modules, Autograd, JIT, Optimizers, Distributions, DataLoader, etc.)
• Extensions: src/TorchAudio, src/TorchVision
Relationship: All high-level APIs reference the PInvoke layer to operate on native tensors and modules.
d. Packaging
• pkg/: one .nupkgproj per library or backend
Relationship: Each packaging project references the corresponding src project(s) and Redist definitions.
e. Examples & Tests
• Examples (C# & F#): depend on TorchSharp core (+ optionally TorchAudio/Vision) to demonstrate usage.
• Tests: validate correctness of core and extensions on CPU and CUDA backends.
f. Build & CI
• build/ + azure-pipelines.yml: orchestrate native build, managed build, test, signing, and NuGet packaging.
g. Documentation
• docfx/: builds static docs (memory management, modules, saving/loading, torchscript)
Relationship: Pulls XML doc comments from managed code and articles from docfx/articles.
Architectural patterns & design principles
• Layered architecture: Native → PInvoke → Managed core → Extension libraries
• Single responsibility: each project focuses on one aspect (core wrapper, audio, vision, packaging)
• Convention over configuration: Directory.Build.* centralizes settings
• CI-as-code: Azure Pipelines and build templates under build/ci ensure reproducible builds