Environment:Dotnet Machinelearning TorchSharp Environment
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Deep_Learning |
| Last Updated | 2026-02-09 11:00 GMT |
Overview
TorchSharp 0.102.7 with LibTorch 2.2.1.1 environment for deep learning operations within ML.NET, targeting net8.0 on 64-bit platforms only.
Description
This environment provides the TorchSharp integration for ML.NET's deep learning capabilities including GenAI (LLaMA, Phi, Mistral), text classification, and named entity recognition. It wraps LibTorch 2.2.1.1 via TorchSharp 0.102.7 bindings. The environment is 64-bit only and requires net8.0 target framework. GPU support requires separate CUDA-specific LibTorch packages.
Usage
Use this environment for GenAI causal language model inference (LLaMA, Phi, Mistral), text classification with deep learning, and named entity recognition. It is required by the Microsoft.ML.TorchSharp and Microsoft.ML.GenAI packages.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Windows, Linux (libc >= 2.23) | macOS support limited |
| Hardware | 64-bit CPU required | GPU optional (requires CUDA LibTorch package) |
| RAM | 8GB+ recommended | Model loading and inference require significant memory |
| Disk | 2GB+ | LibTorch binaries and model weights |
Dependencies
NuGet Packages
- `TorchSharp` = 0.102.7
- `libtorch-cpu` or `libtorch-cuda-*` = 2.2.1.1
- `TorchSharp.PyBridge` = 1.4.1
Framework Requirements
- Target framework: `net8.0` (required for GenAI modules)
- Architecture: 64-bit only (x64 or ARM64)
System Requirements
- Linux: `libc` >= 2.23
Credentials
No credentials required for TorchSharp itself. Model downloads from HuggingFace may require authentication tokens.
Quick Install
# Via NuGet for CPU inference
dotnet add package TorchSharp --version 0.102.7
dotnet add package libtorch-cpu --version 2.2.1.1
# For CUDA GPU support (replace with your CUDA version)
# dotnet add package libtorch-cuda-12.1 --version 2.2.1.1
Code Evidence
TorchSharp version from `eng/Versions.props:80-81`:
<TorchSharpVersion>0.102.7</TorchSharpVersion>
<LibTorchVersion>2.2.1.1</LibTorchVersion>
PyBridge version from `eng/Versions.props:77`:
<TorchSharpPyBridgeVersion>1.4.1</TorchSharpPyBridgeVersion>
64-bit only constraint from `test/Microsoft.ML.TestFrameworkCommon/Attributes/TorchSharpFactAttribute.cs:16`:
// TorchSharp is 64-bit only
libc requirement from `test/Microsoft.ML.TestFrameworkCommon/Attributes/TorchSharpFactAttribute.cs:28-30`:
// Requires Linux with libc >= 2.23 (64-bit only)
GPU test disabled by default:
EnableRunningGpuTest = false // Requires separate CUDA package configuration
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `DllNotFoundException: LibTorchSharp` | LibTorch native binaries not installed | Add `libtorch-cpu` or `libtorch-cuda-*` NuGet package |
| `System.BadImageFormatException` | Running on 32-bit process | Ensure 64-bit runtime; set platform target to x64 |
| `GLIBC_2.23 not found` | Linux libc version below 2.23 | Upgrade Linux distribution to one with libc >= 2.23 |
| `CUDA error: no kernel image is available` | CUDA version mismatch with LibTorch | Match `libtorch-cuda-*` package to your installed CUDA version |
Compatibility Notes
- 32-bit: TorchSharp is strictly 64-bit only; no 32-bit support.
- GPU: GPU tests are disabled by default (`EnableRunningGpuTest = false`). Enable by installing matching CUDA LibTorch package.
- macOS: Limited support; LibTorch CPU should work on Apple Silicon via Rosetta.
- Model Configs: Pre-configured for LLaMA 3.1 (8B/70B/405B), LLaMA 3.2 (1B/3B), Mistral 7B, Phi-2, Phi-3, with embedded JSON configuration files.