Load state dict only from old model.pth file

I have an old model that I saved (complete model) in a .pth file. When I try to load it now, I get an exception from the unpickler, that the python module in which I defined the model cannot be found.
I was not aware that when saving full .pth models, the pickler only saves references to the documents that contain the definitions of the underlying python objects, and not the objects themselves. I refactored my codebase massively since then and can not reinstate the old version of the code, much less the former file directoy structure. I would have, however, the python object for the exact model.

Is there any way I can salvage my model? In particular I am only interested in the state_dict, i.e. the tensors that should still be in the .pth file. I tried looking at the source-code of torch.serialized, but I did not really grasp what is going on when models are saved/loaded and how I could get hold of the parameters.

As a side note, the whole saving/loading functionality with pytorch is in an unsatisfying situation, especially when working in teams on models and codebase. There are two options. One requires to save the model definitions including all used setting parameters separately from the state dict, the other requires to hold alot of legacy code that cannot be refactored. Is there no way to make it work in a way that pytorch models can be saved as they are and reused as they are?

You could script the model and export it so that it’ll be usable in standalone applications (e.g. in C++).
However, that wouldn’t store the source code of the model, which would be needed for further experiments.

You could try to use the patch tool via:

torch.nn.Module.dump_patches = True

which should create patch files with the diffs. I’ve never used it before, so I’m not sure, how easy it would be to restore your code.