Can someone help me understand when and how to use the _extra_files parameter is torch.jit.save
and torch.jit.load
? The code example in the documents (with extra_files['foo.txt'] = 'bar'
) didn’t help me understand what its practical use would be.
It lets you wrap up any extra data you want into the output of torch.save
and have it be decoded for you by torch.load
. Maybe you want to ship some documentation or versioning metadata with your saved model to help with deployment, extra_files
lets you do that without having to ship files separately alongside your .pt
file.
The .pt
file from torch.jit.save
is a zip file, so any extra_files
just get added to the zip archive.
To dig into the docs example a little more, for this code:
class M(nn.Module):
def forward(self):
pass
m = torch.jit.script(M())
extra_files = torch._C.ExtraFilesMap()
extra_files['foo.txt'] = 'bar'
torch.jit.save(m, 'scriptmodule.pt', _extra_files=extra_files)
Then in a shell
$ unzip scriptmodule.pt
Archive: scriptmodule.pt
extracting: scriptmodule/version
extracting: scriptmodule/extra/foo.txt
extracting: scriptmodule/data.pkl
extracting: scriptmodule/code/__torch__.py
extracting: scriptmodule/code/__torch__.py.debug_pkl
extracting: scriptmodule/constants.pkl
$ cat scriptmodule/extra/foo.txt
bar