Skip to content

Commit 4b65ca7

Browse files
committed
Keep compiled cache copies on the compute stream
1 parent b5c3641 commit 4b65ca7

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎sdm/models/base/model.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def _prefetch(
5050
) -> "_Prefetcher[T]":
5151

5252
stream: torch.cuda.Stream | None = None
53-
if device.type == "cuda":
53+
# Keep compiled copies and compute on one stream for safe buffer reuse.
54+
if device.type == "cuda" and not torch.compiler.is_compiling():
5455
if device not in self._transfer_streams:
5556
self._transfer_streams[device] = torch.cuda.Stream(device)
5657
stream = self._transfer_streams[device]
@@ -121,7 +122,10 @@ def __enter__(self) -> Self:
121122
self._transfer_stream.synchronize()
122123
raise
123124
else:
124-
self._next = self._items[0].to(self._device) # type: ignore
125+
self._next = self._items[0].to( # type: ignore
126+
device=self._device,
127+
non_blocking=self._device.type == "cuda",
128+
)
125129

126130
return self
127131

@@ -159,6 +163,9 @@ def __iter__(self) -> Iterator[T]:
159163
non_blocking=True,
160164
)
161165
else:
162-
self._next = self._next.to(self._device) # type: ignore
166+
self._next = self._next.to( # type: ignore
167+
device=self._device,
168+
non_blocking=self._device.type == "cuda",
169+
)
163170

164171
yield item

0 commit comments

Comments
 (0)