pickle icon indicating copy to clipboard operation
pickle copied to clipboard

Valid for TorchSharp?

Open GeorgeS2019 opened this issue 1 year ago • 10 comments

Please feedback if this is valid for loading pytorch torch. Save pickle format?

GeorgeS2019 avatar Jun 29 '23 02:06 GeorgeS2019

I am not sure what the question is. Can you elaborate?

Also I have zero experience with pytorch / torchsharp, so I am afraid I am not qualified to answer anything about it...

irmen avatar Jun 30 '23 08:06 irmen

Note that an enhancement concerning Pytorch (and torchsharp? not sure.) got merged see #15

irmen avatar Nov 15 '23 23:11 irmen

Thank you @irmen for helping push this along! @GeorgeS2019 An extension methods package for TorchSharp is now published on NuGet using the pickle library allowing loading & saving PyTorch format models from TorchSharp: https://www.nuget.org/packages/TorchSharp.PyBridge

shaltielshmid avatar Nov 19 '23 15:11 shaltielshmid

@shaltielshmid -- to be clear, it's just the parameters and buffers that are loaded, right? You still have to translate the PyTorch model to TorchSharp in code, just like with the TorchSharp-specific format, is that correct?

I'm referring to TorchSharp.PyBridge, not this library, of course.

NiklasGustafsson avatar Nov 21 '23 16:11 NiklasGustafsson

Correct. I read all the parameters and buffers from the pickled file into a state dictionary, and then call Module.load_state_dict.

The PyTorch format is very similar to how TorchSharp saves models, with slight changes due to the Pickle format. The PyTorch format is a zip file with a main pickle file which stores a mapping between each name and information about the Tensor (stride, dimensions, etc.), plus a link to another binary file in the zip which is just the bytes of the tensor.

shaltielshmid avatar Nov 21 '23 16:11 shaltielshmid

We could add a wiki article to TorchSharp linking to your repo, if you want.

NiklasGustafsson avatar Nov 21 '23 17:11 NiklasGustafsson

That would be great! Would you like me to write up a draft?

shaltielshmid avatar Nov 21 '23 17:11 shaltielshmid

@shaltielshmid How challenging to display the information from the pickle file as a graph, as an intermediary or visual guide , before translating to TorchSharp codes?

GeorgeS2019 avatar Nov 21 '23 20:11 GeorgeS2019

The information from the pickle file is simply a dictionary mapping a parameter/buffer name to a tensor. Similar to what you would get from calling Module.state_dict() in TorchSharp or PyTorch. Would kind of visual guide would you want to see?

shaltielshmid avatar Nov 21 '23 20:11 shaltielshmid

@GeorgeS2019 -- I imagine that you're thinking of something like Netron.

However, if you think that the PyBridge is somehow translating the pickled model to TorchSharp code, that's not what it is doing. It's a dictionary of string -> Tensor, that's it. Just like the TorchSharp format: you can load and save weights.

NiklasGustafsson avatar Nov 21 '23 20:11 NiklasGustafsson