Skip to content

Raise error when using AMP on non-CUDA device#7869

Merged
sgugger merged 3 commits into
huggingface:masterfrom
BramVanroy:patch-2
Oct 19, 2020
Merged

Raise error when using AMP on non-CUDA device#7869
sgugger merged 3 commits into
huggingface:masterfrom
BramVanroy:patch-2

Conversation

@BramVanroy

@BramVanroy BramVanroy commented Oct 17, 2020

Copy link
Copy Markdown
Collaborator

The trainer currently implements native AMP in such a way that a GradScaler is always used. AFAIK, and as supported by this post and this report on the forums, this will only work on CUDA devices. Therefore, an error should probably be thrown when a user tries to use --fp16 alongside a non-CUDA device.

This PR is currently very brief because I am uncertain about a few things:

  • I am not sure if the current implementation of --fp16 in the trainer works with TPUs;
  • I am not sure about differences in behaviour between using apex and native AMP in this respect;
  • I am not entirely sure whether _post_init is the right place to do an argument sanity check. Particularly because I am now calling self.device which will set the device through _set_devices, even though you may (?) want to delay that as long as possible until the arguments are used in the trainer.

cc @sgugger

@BramVanroy
BramVanroy requested a review from sgugger October 17, 2020 12:48

@sgugger sgugger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the postinit is a great place for this sanity check, so the error comes fast to the user, and I don't think our current FP16 implementations (Apex and Amp) work with TPUs. I can check the latter on Monday and report back here.

@BramVanroy

Copy link
Copy Markdown
Collaborator Author

I think the postinit is a great place for this sanity check, so the error comes fast to the user, and I don't think our current FP16 implementations (Apex and Amp) work with TPUs. I can check the latter on Monday and report back here.

If you'd rather be more forgiving, let me know. In that case we can do a logging.warning(f"AMP only supported when using CUDA. Turning off AMP because not using a CUDA device ({self.device.type})") and subsequently automatically turning off --fp16.

@sgugger

sgugger commented Oct 19, 2020

Copy link
Copy Markdown
Collaborator

Ok, just tested on TPUs and FP16 does not work on them indeed, though native amp fails graciously (testing on TPUs with PyTorch < 1.6 is suicide so I did not try APEX). So if you fix the styling issue, I think this is good to be merged!

@BramVanroy BramVanroy closed this Oct 19, 2020
@BramVanroy BramVanroy reopened this Oct 19, 2020
@sgugger

sgugger commented Oct 19, 2020

Copy link
Copy Markdown
Collaborator

It has to be run by the user, I can force-push the styling on your branch if you don't have the setup to run it easily.

@BramVanroy

Copy link
Copy Markdown
Collaborator Author

It has to be run by the user, I can force-push the styling on your branch if you don't have the setup to run it easily.

Thanks but it should work fine now. Updated local dependencies and all that and reformatted.

@sgugger
sgugger merged commit 55bcd0c into huggingface:master Oct 19, 2020
@sgugger

sgugger commented Oct 19, 2020

Copy link
Copy Markdown
Collaborator

Thanks for your contribution!

@BramVanroy
BramVanroy deleted the patch-2 branch October 19, 2020 20:13
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 this pull request may close these issues.

2 participants