-
Notifications
You must be signed in to change notification settings - Fork 778
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
add mixed precision support to deepxde #1650
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ def set_default_float(value): | |
The default floating point type is 'float32'. | ||
|
||
Args: | ||
value (String): 'float16', 'float32', or 'float64'. | ||
value (String): 'float16', 'float32', 'float64', or 'mixed' (mixed precision in https://arxiv.org/abs/2401.16645). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
""" | ||
if value == "float16": | ||
print("Set the default float type to float16") | ||
|
@@ -85,6 +85,20 @@ def set_default_float(value): | |
elif value == "float64": | ||
print("Set the default float type to float64") | ||
real.set_float64() | ||
elif value == "mixed": | ||
g-w1 marked this conversation as resolved.
Show resolved
Hide resolved
g-w1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Set the float type to mixed precision of float16 and float32") | ||
real.set_mixed() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is confusing. Here you do |
||
if backend_name == "tensorflow": | ||
real.set_float16() | ||
tf.keras.mixed_precision.set_global_policy("mixed_float16") | ||
return # don't try to set it again below | ||
if backend_name == "pytorch": | ||
# Use float16 during the forward and backward passes, but store in float32 | ||
real.set_float32() | ||
else: | ||
raise ValueError( | ||
f"{backend_name} backend does not currently support mixed precision." | ||
) | ||
else: | ||
raise ValueError(f"{value} not supported in deepXDE") | ||
if backend_name in ["tensorflow.compat.v1", "tensorflow"]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,9 +357,12 @@ def closure(): | |
total_loss = torch.sum(losses) | ||
self.opt.zero_grad() | ||
total_loss.backward() | ||
return total_loss | ||
|
||
self.opt.step(closure) | ||
def closure_mixed(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why delete line 360 |
||
with torch.autocast(device_type="cuda", dtype=torch.float16): | ||
closure() | ||
|
||
self.opt.step(closure if not config.real.mixed else closure_mixed) | ||
if self.lr_scheduler is not None: | ||
self.lr_scheduler.step() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.