VOOZH about

URL: https://www.nuget.org/packages/TorchSharp.PyBridge/

⇱ NuGet Gallery | TorchSharp.PyBridge 1.4.3




TorchSharp.PyBridge 1.4.3

dotnet add package TorchSharp.PyBridge --version 1.4.3
 
 
NuGet\Install-Package TorchSharp.PyBridge -Version 1.4.3
 
 
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.4.3" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TorchSharp.PyBridge" Version="1.4.3" />
 
Directory.Packages.props
<PackageReference Include="TorchSharp.PyBridge" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TorchSharp.PyBridge --version 1.4.3
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TorchSharp.PyBridge, 1.4.3"
 
 
#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.
#:package TorchSharp.PyBridge@1.4.3
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TorchSharp.PyBridge&version=1.4.3
 
Install as a Cake Addin
#tool nuget:?package=TorchSharp.PyBridge&version=1.4.3
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

  • module.load_py(...), optim.load_py(...): Extension method for modules and optimizers for easily loading 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.

  • module.save_py(...), optim.save_py(...): Extension method for modules and optimizers for easily saving TorchSharp models in a format that can be directly loaded in PyTorch (using torch.load), offering cross-platform model compatibility.

  • module.load_safetensors(...), module.save_safetensors(...): Extension methods for modules for easily saving and loading model weights using the safetensors format.

  • module.load_checkpoint(...): Extension method for loading in a checkpoint (both safetensors and regular pytorch, including sharded models) from a directory saved using HuggingFace's PreTrainedModel.save_pretrained() method.

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 Versions Compatible and additional computed target framework versions.
.NET net6.0 net6.0 is compatible.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

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

Package Downloads
NeuralCodecs

Neural audio codec implementations in .NET.

Microsoft.ML.GenAI.LLaMA

Package Description

Microsoft.ML.GenAI.Mistral

Package Description

Microsoft.ML.GenAI.Phi

Package Description

Maomi.Torch

C# Pytorch 深度学习框架扩展包.

GitHub repositories (1)

Showing the top 1 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.
Version Downloads Last Updated
1.4.3 3,006 9/26/2024
1.4.2 322 8/7/2024
1.4.1 1,474 6/6/2024
1.4.0 273 5/14/2024
1.3.2 590 4/6/2024
1.3.1 473 2/29/2024
1.3.0 279 2/27/2024
1.2.0 803 1/31/2024
1.1.0 393 12/13/2023
1.0.0 490 11/19/2023

1.4.3:
     - Fixed #21: `strict` is not passed to `load_safetensor` in `load_checkpoint` extension
     1.4.2:
     - PR #20: Optimize load_py for memory and speed (@ejhg)
     1.4.1:
     - Fixed #17: How to disable tqdm output when loading sharded safetensors
     1.4.0:
     - Exposed `Safetensors`, `PytorchPickler` and `PytorchUnpickler` to allow for loading/saving python tensors outside of a model.
     - Fixed #16: SaveStateDict calls itself recursively and fails on locked file
     1.3.2:
     - Fixed #13: UnpickleStateDict on BatchNorm2d error
     1.3.1:
     - Fixed error on Apple Silicon devices
     1.3.0:
     - Added support for loading tensors that are greater than 2GB (following the update in TorchSharp 0.102.0)
     - Added support for loading and saving safetensors when model isn't on CPU.