- Notifications
You must be signed in to change notification settings - Fork4
A library enabling easy transfer and handling of PyTorch models between .NET and Python environments
License
shaltielshmid/TorchSharp.PyBridge
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
TorchSharp.PyBridge is an extension library forTorchSharp, 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.
module.load_py(...)
,optim.load_py(...)
: Extension method for modules and optimizers for easily loading PyTorch models saved in the standard Python format (usingtorch.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 (usingtorch.load
), offering cross-platform model compatibility.module.load_safetensors(...)
,module.save_safetensors(...)
: Extension methods for modules for easily saving and loading model weights using thesafetensors 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'sPreTrainedModel.save_pretrained()
method.
TorchSharp.PyBridge is available on NuGet. You can install it using the following command:
dotnet add package TorchSharp.PyBridge
Install-Package TorchSharp.PyBridge
- .NET SDK
- TorchSharp library
Saving the model in Python:
importtorchmodel= ...torch.save(model.state_dict(),'path_to_your_model.pth')
Loading it in C#:
usingTorchSharp.PyBridge;varmodel= ...;model.load_py("path_to_your_model.pth");
To save a model in a format compatible with PyTorch:
usingTorchSharp.PyBridge;varmodel= ...;model.save_py("path_to_save_model.pth");
And loading it in in Python:
importtorchmodel= ...model.load_state_dict(torch.load('path_to_save_model.pth'))
Contributions to TorchSharp.PyBridge are welcome.
This project makes use of thepickle
library, a Java and .NET implementation of Python's pickle serialization protocol, developed by Irmen de Jong. Thepickle
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 thepickle
library, please visit theirGitHub repository.
For support, questions, or feedback, please open an issue in theGitHub repository.
About
A library enabling easy transfer and handling of PyTorch models between .NET and Python environments
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.