@@ -263,7 +263,15 @@ void free(void* ptr) {
263
263
" ptr's stream uses vector is empty" );
264
264
265
265
if (C10_UNLIKELY (capture_underway)) {
266
- if (it->second .captured ) {
266
+ if (!it->second .captured ) {
267
+ TORCH_WARN_ONCE (" free() was called on an uncaptured allocation during graph capture. "
268
+ " This may be benign, for example, a Python tensor in the capture "
269
+ " might happen to shadow (use the same name as) an unrelated temporary "
270
+ " tensor from somewhere before capture, pushing the earlier tensor "
271
+ " out of scope.\n "
272
+ " However, if the tensor we're freeing here IS used by the capture, "
273
+ " freeing it is an error, and may cause illegal memory accesses or "
274
+ " memory corruption during graph replay." );
267
275
// See Note [Avoid freeing uncaptured ptrs during CUDA graph capture]
268
276
// Remembers the raw pointer, not the iterator.
269
277
// This forces notifyCaptureEnded to do another lookup,
@@ -272,9 +280,7 @@ void free(void* ptr) {
272
280
ungraphed_ptrs_defer_free_until_no_capture.push_back (ptr);
273
281
return ;
274
282
}
275
- }
276
-
277
- if (C10_UNLIKELY (it->second .captured )) {
283
+ } else if (C10_UNLIKELY (it->second .captured )) {
278
284
TORCH_WARN (" Attempting uncaptured free of a captured allocation. "
279
285
" This is technically allowed, but may indicate you are losing "
280
286
" the last user-visible tensor through which the allocation can "
0 commit comments