cutlass
cutlass copied to clipboard
[BUG] CuTe DSL: reading leading_dim keeps from_dlpack tensors alive
Which component has the problem? CuTe DSL
Describe the bug
Reading leading_dim on a from_dlpack() tensor keeps the wrapper, and the tensor it wraps, alive after the caller drops it, up to 128 of them.
Steps/Code to reproduce bug
import gc
import torch
from cutlass.cute.runtime import from_dlpack
for _ in range(8):
x = torch.empty(128 * 2**20, dtype=torch.uint8, device="cuda") # 128 MiB
t = from_dlpack(x)
t.leading_dim
del x, t
gc.collect()
print(torch.cuda.memory_allocated() // 2**20, "MiB")
1024 MiB
Expected behavior
0 MiB, as without the leading_dim read.
Environment details
nvidia-cutlass-dsl 4.8.1 (latest on PyPI), torch 2.9.0, Python 3.13, Cloud (Modal), A10G (sm_86). The code involved is unchanged on main (b2f249a).
Additional context
_Tensor.leading_dim in cute/runtime.py is decorated with lru_cache(maxsize=128), which holds a strong reference to self in the cache key. I have a fix with a test and will open a PR.