Skip to content

Commit ef4cf86

Browse files
csarofeensoumith
authored andcommitted
Lazy init in set device, also should not be called in getDevCount (#4918)
1 parent ee8bcdc commit ef4cf86

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

torch/cuda/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ def __init__(self, idx):
215215
def __enter__(self):
216216
if self.idx is -1:
217217
return
218-
_lazy_init()
219218
self.prev_idx = torch._C._cuda_getDevice()
220219
if self.prev_idx != self.idx:
221220
torch._C._cuda_setDevice(self.idx)
221+
_lazy_init()
222222

223223
def __exit__(self, *args):
224224
if self.prev_idx != self.idx:
@@ -308,7 +308,6 @@ def stream(stream):
308308
def device_count():
309309
"""Returns the number of GPUs available."""
310310
if is_available():
311-
_lazy_init()
312311
return torch._C._cuda_getDeviceCount()
313312
else:
314313
return 0

0 commit comments

Comments
 (0)