TorchSharp.PyBridge 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TorchSharp.PyBridge --version 1.0.0                
NuGet\Install-Package TorchSharp.PyBridge -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TorchSharp.PyBridge" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TorchSharp.PyBridge --version 1.0.0                
#r "nuget: TorchSharp.PyBridge, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TorchSharp.PyBridge as a Cake Addin
#addin nuget:?package=TorchSharp.PyBridge&version=1.0.0

// Install TorchSharp.PyBridge as a Cake Tool
#tool nuget:?package=TorchSharp.PyBridge&version=1.0.0                

TorchSharp.PyBridge

NuGet

TorchSharp.PyBridge is an extension library for TorchSharp, providing seamless interoperability between .NET and Python for model serialization. It simplifies the process of saving and loading PyTorch models in a .NET environment, enabling developers to easily develop models in both .NET and Python and transfer models easily.

Features

  • load_py Method: Easily load PyTorch models saved in the standard Python format (using torch.save) directly into TorchSharp.

    This only works for when the state_dict was saved and not the whole model, see example below.

  • save_py Method: Save TorchSharp models in a format that can be directly loaded in PyTorch (using torch.load), offering cross-platform model compatibility.

Getting Started

Installation

TorchSharp.PyBridge is available on NuGet. You can install it using the following command:

.NET CLI
dotnet add package TorchSharp.PyBridge
NuGet Package Manager
Install-Package TorchSharp.PyBridge

Prerequisites

  • .NET SDK
  • TorchSharp library

Usage

Loading a PyTorch Model in .NET

Saving the model in Python:

import torch 

model = ...
torch.save(model.state_dict(), 'path_to_your_model.pth')

Loading it in C#:

using TorchSharp.PyBridge;

var model = ...;
model.load_py("path_to_your_model.pth");

Saving a TorchSharp Model for PyTorch

To save a model in a format compatible with PyTorch:

using TorchSharp.PyBridge;

var model = ...;
model.save_py("path_to_save_model.pth");

And loading it in in Python:

import torch

model = ...
model.load_state_dict(torch.load('path_to_save_model.pth'))

Contributing

Contributions to TorchSharp.PyBridge are welcome.

Acknowledgments

This project makes use of the pickle library, a Java and .NET implementation of Python's pickle serialization protocol, developed by Irmen de Jong. The pickle library plays a vital role in enabling the serialization features within TorchSharp.PyBridge. We extend our thanks to the developer for their significant contributions to the open-source community. For more details about the pickle library, please visit their GitHub repository.

Support and Contact

For support, questions, or feedback, please open an issue in the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on TorchSharp.PyBridge:

Package Downloads
Microsoft.ML.GenAI.LLaMA

Package Description

Microsoft.ML.GenAI.Phi

Package Description

Microsoft.ML.GenAI.Mistral

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on TorchSharp.PyBridge:

Repository Stars
dotnet/machinelearning
ML.NET is an open source and cross-platform machine learning framework for .NET.
dotnet/ai-samples
Version Downloads Last updated
1.4.3 263 9/26/2024
1.4.2 151 8/7/2024
1.4.1 210 6/6/2024
1.4.0 122 5/14/2024
1.3.2 404 4/6/2024
1.3.1 316 2/29/2024
1.3.0 128 2/27/2024
1.2.0 501 1/31/2024
1.1.0 252 12/13/2023
1.0.0 316 11/19/2023