bentoml / BentoML

The easiest way to serve AI apps and models - Build Model Inference APIs, Job queues, LLM apps, Multi-model pipelines, and more!
https://bentoml.com
Apache License 2.0
7.13k stars 791 forks source link

bug: It's not possible to save fastai models #2979

Closed ramtiin closed 2 years ago

ramtiin commented 2 years ago

Describe the bug

I want to save a fastai model in the bentoml form on google colab, but I get module 'cloudpickle' has no attribute 'Pickler'

To reproduce

Codes:

learner = unet_learner(data, arch, wd=wd, loss_func=feat_loss,
                     blur=True, norm_type=NormType.Weight).to_fp16()

bentoml.fastai.save_model("fastai_sentiment", learner)

or

bentoml.pytorch.save_model(
   "my_pytorch_model", learner.model, signatures={"__call__": {"batchable": True}}
)

Result:

### Traceback:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-50-78caf0c1c3a2>](https://localhost:8080/#) in <module>
      2 
      3 bentoml.pytorch.save_model(
----> 4    "my_pytorch_model", learn.model, signatures={"__call__": {"batchable": True}}
      5 )

2 frames
[/usr/local/lib/python3.7/dist-packages/bentoml/_internal/frameworks/pytorch.py](https://localhost:8080/#) in save_model(name, model, signatures, labels, custom_objects, external_modules, metadata)
    180         weight_file = bento_model.path_of(MODEL_FILENAME)
    181         with open(weight_file, "wb") as file:
--> 182             torch.save(model, file, pickle_module=cloudpickle)  # type: ignore
    183 
    184         return bento_model

[/usr/local/lib/python3.7/dist-packages/torch/serialization.py](https://localhost:8080/#) in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
    377         if _use_new_zipfile_serialization:
    378             with _open_zipfile_writer(opened_file) as opened_zipfile:
--> 379                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
    380                 return
    381         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)

[/usr/local/lib/python3.7/dist-packages/torch/serialization.py](https://localhost:8080/#) in _save(obj, zip_file, pickle_module, pickle_protocol)
    585     # Write the pickle data for `obj`
    586     data_buf = io.BytesIO()
--> 587     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
    588     pickler.persistent_id = persistent_id
    589     pickler.dump(obj)

AttributeError: module 'cloudpickle' has no attribute 'Pickler'

Expected behavior

The model should be saved correctly in Bentoml form.

Environment

Google Colab fastai: 2.7.9 torch: 1.12.1+cu113 python: 3.7.13

aarnphm commented 2 years ago

Hi @ramtiin, Can you share the whole training code for the learner?

aarnphm commented 2 years ago

Feel free to reopen if you are still running into this.