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

Make numpy optional dependency for torch.cuda.amp #48154

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions torch/cuda/amp/autocast_mode.py
@@ -1,7 +1,10 @@
import torch
import functools
import warnings
import numpy as np
try:
import numpy as np
except ModuleNotFoundError:
np = None
from torch._six import container_abcs, string_classes


Expand Down Expand Up @@ -144,7 +147,7 @@ def _cast(value, dtype):
return value.to(dtype) if is_eligible else value
elif isinstance(value, string_classes):
return value
elif isinstance(value, np.ndarray):
elif np is not None and isinstance(value, np.ndarray):
return value
Comment on lines 148 to 151
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: was wondering if we can just remove these 2 and add comments on the last default return (since it is a passthrough)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That wouldn't work, since numpy arrays are iterable (i.e. isinstance(np.ndarray(5), container_abcs.Iterable) returns True )

elif isinstance(value, container_abcs.Mapping):
return {_cast(k, dtype): _cast(v, dtype) for k, v in value.items()}
Expand Down