{"id":24062327,"url":"https://github.com/shaltielshmid/torchsharp.pybridge","last_synced_at":"2025-10-08T17:12:28.855Z","repository":{"id":208073845,"uuid":"720763507","full_name":"shaltielshmid/TorchSharp.PyBridge","owner":"shaltielshmid","description":"A library enabling easy transfer and handling of PyTorch models between .NET and Python environments","archived":false,"fork":false,"pushed_at":"2024-12-02T17:07:13.000Z","size":219,"stargazers_count":26,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-08-31T09:14:43.851Z","etag":null,"topics":["libtorch","pybridge","pytorch","torchsharp"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shaltielshmid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-11-19T14:30:13.000Z","updated_at":"2025-05-10T18:09:33.000Z","dependencies_parsed_at":"2023-11-28T20:28:08.378Z","dependency_job_id":"64023115-b79f-4a78-94aa-7c4d197e8af8","html_url":"https://github.com/shaltielshmid/TorchSharp.PyBridge","commit_stats":null,"previous_names":["shaltielshmid/torchsharp.pybridge"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/shaltielshmid/TorchSharp.PyBridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaltielshmid%2FTorchSharp.PyBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaltielshmid%2FTorchSharp.PyBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaltielshmid%2FTorchSharp.PyBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaltielshmid%2FTorchSharp.PyBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shaltielshmid","download_url":"https://codeload.github.com/shaltielshmid/TorchSharp.PyBridge/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaltielshmid%2FTorchSharp.PyBridge/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278981518,"owners_count":26079640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["libtorch","pybridge","pytorch","torchsharp"],"created_at":"2025-01-09T08:39:51.396Z","updated_at":"2025-10-08T17:12:28.838Z","avatar_url":"https://github.com/shaltielshmid.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TorchSharp.PyBridge\n\n[![NuGet](https://img.shields.io/nuget/v/TorchSharp.PyBridge.svg)](https://www.nuget.org/packages/TorchSharp.PyBridge/)\n\nTorchSharp.PyBridge is an extension library for [TorchSharp](https://github.com/dotnet/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.\n\n## Features\n\n- `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.\n\n    \u003e This only works for when the `state_dict` was saved and not the whole model, see example below.\n\n- `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.\n\n- `module.load_safetensors(...)`, `module.save_safetensors(...)`: Extension methods for modules for easily saving and loading model weights using the [safetensors](https://github.com/huggingface/safetensors) format. \n\n- `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.  \n\n## Getting Started\n\n### Installation\n\nTorchSharp.PyBridge is available on NuGet. You can install it using the following command:\n\n#### .NET CLI\n```bash\ndotnet add package TorchSharp.PyBridge\n```\n\n#### NuGet Package Manager\n```powershell\nInstall-Package TorchSharp.PyBridge\n```\n\n### Prerequisites\n\n- .NET SDK\n- TorchSharp library\n\n## Usage\n\n### Loading a PyTorch Model in .NET\n\nSaving the model in Python:\n\n```python\nimport torch \n\nmodel = ...\ntorch.save(model.state_dict(), 'path_to_your_model.pth')\n```\n\nLoading it in C#:\n\n```csharp\nusing TorchSharp.PyBridge;\n\nvar model = ...;\nmodel.load_py(\"path_to_your_model.pth\");\n```\n\n### Saving a TorchSharp Model for PyTorch\n\nTo save a model in a format compatible with PyTorch:\n\n```csharp\nusing TorchSharp.PyBridge;\n\nvar model = ...;\nmodel.save_py(\"path_to_save_model.pth\");\n```\n\nAnd loading it in in Python:\n\n```python\nimport torch\n\nmodel = ...\nmodel.load_state_dict(torch.load('path_to_save_model.pth'))\n```\n\n## Contributing\n\nContributions to TorchSharp.PyBridge are welcome. \n\n## Acknowledgments\n\nThis 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](https://github.com/irmen/pickle).\n\n## Support and Contact\n\nFor support, questions, or feedback, please open an issue in the [GitHub repository](https://github.com/shaltielshmid/TorchSharp.PyBridge).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaltielshmid%2Ftorchsharp.pybridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaltielshmid%2Ftorchsharp.pybridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaltielshmid%2Ftorchsharp.pybridge/lists"}