Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'DataParallel' object has no attribute 'save_pretrained' #16971

Closed
bilalghanem opened this issue Apr 27, 2022 · 2 comments
Closed
Labels

Comments

@bilalghanem
Copy link

bilalghanem commented Apr 27, 2022

System Info

torch==1.10.2+cu113
transformers==4.18.0
Python 3.6.9
Linux "18.04.6 LTS (Bionic Beaver)"

I am training a T5 transformer (T5ForConditionalGeneration.from_pretrained(model_params["MODEL"])) to generate text. The model works well when I train it on a single GPU. But when I want to parallel the data across several GPUs by doing model = nn.DataParallel(model), I can't save the model.

The error is:

File "run.py", line 288, in T5Trainer
model.save_pretrained(path)
File "/home/USER_NAME/venv/pt_110/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1178, in getattr
type(self).name, name))
AttributeError: 'DataParallel' object has no attribute 'save_pretrained'

Reproduction

Wrap the model with model = nn.DataParallel(model).

Expected behavior

The model should be saved without any issues.
@LysandreJik
Copy link
Member

DataParallel wraps the model. To access the underlying module, you can use the module attribute:

>>> from torch.nn import DataParallel
>>> model = nn.DataParallel(model)
>>> model.module.save_pretrained(<directory>)

@bilalghanem
Copy link
Author

DataParallel wraps the model. To access the underlying module, you can use the module attribute:

>>> from torch.nn import DataParallel
>>> model = nn.DataParallel(model)
>>> model.module.save_pretrained(<directory>)

Thanks @LysandreJik!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants