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

T5 fp16 forward yields nan #4287

Closed
2 of 4 tasks
binshengliu opened this issue May 11, 2020 · 6 comments · Fixed by #4436
Closed
2 of 4 tasks

T5 fp16 forward yields nan #4287

binshengliu opened this issue May 11, 2020 · 6 comments · Fixed by #4436
Assignees

Comments

@binshengliu
Copy link

binshengliu commented May 11, 2020

🐛 Bug

Information

Model I am using (Bert, XLNet ...): T5

Language I am using the model on (English, Chinese ...): English

The problem arises when using:

  • the official example scripts: (give details below)
  • my own modified scripts: (give details below)

The tasks I am working on is:

  • an official GLUE/SQUaD task: (give the name)
  • my own task or dataset: (give details below)

To reproduce

I use pytorch-lightning to manage fp16. This is the minimal example that reproduces the result.

from transformers import T5Model, T5Tokenizer

tokenizer = T5Tokenizer.from_pretrained("t5-base")
model = T5Model.from_pretrained("t5-base").cuda().half()
text = "hello world!"
inputs = tokenizer.encode(text, return_tensors="pt").cuda()
out = model(input_ids=inputs, decoder_input_ids=inputs)
print(out[0][:, :, :10])

output:

tensor([[[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
         [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
         [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]]], device='cuda:0',
       dtype=torch.float16, grad_fn=<SliceBackward>)

Expected behavior

Get non-nan values.

Environment info

  • transformers version: 2.9.0
  • Platform: Linux-4.15.0-88-generic-x86_64-with-debian-buster-sid
  • Python version: 3.7.6
  • PyTorch version (GPU?): 1.4.0 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Using GPU in script?: Yes
  • Using distributed or parallel set-up in script?: No
@patrickvonplaten patrickvonplaten self-assigned this May 11, 2020
@patrickvonplaten patrickvonplaten linked a pull request May 18, 2020 that will close this issue
@patrickvonplaten
Copy link
Contributor

Thanks for the detailed error description @binshengliu! I linked a PR that should fix it :-)

@binshengliu
Copy link
Author

Hi, there's still some chance we get nan values during training. It does not always happen but is still pretty easy to reproduce. My current version is 2.11.0.

import torch
from transformers import T5Model, T5Tokenizer

torch.manual_seed(0)
tokenizer = T5Tokenizer.from_pretrained("t5-base")
model = T5Model.from_pretrained("t5-base").cuda().half()
text = "hello world!"
model.train()
inputs = tokenizer.encode(text, return_tensors="pt").cuda()
for idx in range(1000):
    out = model(input_ids=inputs, decoder_input_ids=inputs)
    if torch.isnan(out[0]).any():
        print(idx)
        print(out[0][:, :, :10])
        exit()

Output:

143
tensor([[[nan, nan, nan, nan, nan, nan, nan, nan, nan, 0.],
         [nan, nan, nan, 0., nan, nan, nan, nan, nan, 0.],
         [nan, nan, 0., nan, nan, 0., 0., 0., nan, nan]]], device='cuda:0',
       dtype=torch.float16, grad_fn=<SliceBackward>)

@binshengliu
Copy link
Author

Sorry I just noticed you commented #4586 (comment). I can confirm the issue I encountered is caused by the same reason.

@yeliu918
Copy link

Hi, I have the same problem. I get NAN when using fp16. And when I set fp16=False, the NAN problem is gone.

@patrickvonplaten
Copy link
Contributor

Yeah that's still an open problem....not sure how easy it will be to solve it, see: #4586 (comment)

@SamsTheGreatest
Copy link

Same when fine-tuning GPT Neo.

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

Successfully merging a pull request may close this issue.

4 participants