torch.save fails with pickler.dump for bytes
See original GitHub issue❓ Questions and Help
This is probably not an issue with pytorch. I try to torch.save
an ops with bytes from pybind11 pickle. Here is the pybind11 registration link.
How do I torch.save
the ops:
import torchtext
import torch
from torchtext.experimental.transforms import PRETRAINED_SP_MODEL, load_sp_model
sp_model_path = torchtext.utils.download_from_url(PRETRAINED_SP_MODEL['text_unigram_25000'])
sp_model = load_sp_model(sp_model_path)
f = open("temp.pt", "wb")
torch.save(sp_model, f)
The error message I got:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-7-c66cdf260b7d> in <module>
1 f = open("temp.pt", "wb")
----> 2 torch.save(sp_model, f)
~/tmp/PyTorch/pytorch/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
370 if _use_new_zipfile_serialization:
371 with _open_zipfile_writer(opened_file) as opened_zipfile:
--> 372 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
373 return
374 _legacy_save(obj, opened_file, pickle_module, pickle_protocol)
~/tmp/PyTorch/pytorch/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)
474 pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
475 pickler.persistent_id = persistent_id
--> 476 pickler.dump(obj)
477 data_value = data_buf.getvalue()
478 zip_file.write_record('data.pkl', data_value, len(data_value))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 119: invalid start byte
cc @mruberry
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
torch.save fails with pickler.dump for bytes #1094 - GitHub
This is probably not an issue with pytorch. I try to torch.save an ops with bytes from pybind11 pickle. Here is the pybind11...
Read more >torch.save — PyTorch 1.13 documentation
The 1.6 release of PyTorch switched torch.save to use a new zipfile-based file format. torch.load still retains the ability to load files in...
Read more >Using pickle.dump - TypeError: must be str, not bytes
3 and I'm having a cryptic error when trying to pickle a simple dictionary. Here is the code: import os import pickle from...
Read more >torch.serialization — PyTorch master documentation
StringIO): msg = ('torch.save received unsupported StringIO.StringIO file object, whose ' 'write method does not return the number of bytes written.
Read more >Python API: torch/serialization.py Source File - Caffe2
231 msg = ('torch.save received unsupported StringIO.StringIO file object, whose '. 232 'write method does not return the number of bytes ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is about the usage of PyBind11. See https://pybind11.readthedocs.io/en/stable/advanced/cast/strings.html#return-c-strings-without-conversion
Changing the binding to this works https://github.com/mthrok/text/blob/b0f88ba56603590444a5c10e216573ebce0740ad/torchtext/csrc/register_bindings.cpp#L52-L74
Addressed in #1104