Skip to content

Commit 41e480a

Browse files
committed
Lazy init in set device, also should not be called in getDevCount
1 parent ba93c03 commit 41e480a

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
@@ -202,10 +202,10 @@ def __init__(self, idx):
202202
def __enter__(self):
203203
if self.idx is -1:
204204
return
205-
_lazy_init()
206205
self.prev_idx = torch._C._cuda_getDevice()
207206
if self.prev_idx != self.idx:
208207
torch._C._cuda_setDevice(self.idx)
208+
_lazy_init()
209209

210210
def __exit__(self, *args):
211211
if self.prev_idx != self.idx:
@@ -291,7 +291,6 @@ def stream(stream):
291291
def device_count():
292292
"""Returns the number of GPUs available."""
293293
if is_available():
294-
_lazy_init()
295294
return torch._C._cuda_getDeviceCount()
296295
else:
297296
return 0

0 commit comments

Comments
 (0)