Skip to content

Fix plan leak in cuTENSOR tensortrace! - #294

Merged
lkdvos merged 3 commits into
mainfrom
fix-cutensor-trace-plan-leak
Aug 7, 2026
Merged

Fix plan leak in cuTENSOR tensortrace!#294
lkdvos merged 3 commits into
mainfrom
fix-cutensor-trace-plan-leak

Conversation

@lkdvos

@lkdvos lkdvos commented Aug 7, 2026

Copy link
Copy Markdown
Member

plan_trace builds a CuTensorPlan and hands it to the plan-taking cuTENSOR.reduce!, which by contract does not free it — only the planless overload does. The reduction workspace is therefore reclaimed only by the finalizer, which the GC has no reason to run promptly for device memory.

Measured on an RTX A6000: 128.0 KiB leaked per trace, so 10⁴ traces grew live device memory by 1.221 GiB. After the fix: 9.8 KiB, matching a non-tracing contraction control exactly.

Also closes a matching exception leak in plan_trace, where the operation descriptor and plan preference were destroyed after plan creation rather than in a finally.

Adds a regression test; the full test/cutensor.jl suite passes.

🤖 Generated with Claude Code

lkdvos and others added 3 commits August 7, 2026 10:29
`plan_trace` builds a `CuTensorPlan` and passes it to the plan-taking
`cuTENSOR.reduce!`, which by contract does not free it. The reduction
workspace (128 KiB) was therefore only reclaimed by the finalizer, which
the GC has no reason to run promptly for device memory: 10^4 traces grew
live device memory by 1.22 GiB. After the fix, 9.8 KiB.

Also close a matching exception leak in `plan_trace`, where the operation
descriptor and plan preference were destroyed after plan creation rather
than in a `finally`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
cuTENSOR.jl itself uses no `finally` anywhere and accepts leaks on throw,
both in `plan_reduction` and in the planless `reduce!`/`contract!`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@lkdvos
lkdvos requested a review from kshyatt August 7, 2026 15:03
@lkdvos

lkdvos commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Failure is windows + enzyme striking again

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
ext/TensorOperationscuTENSORExt.jl 95.87% <100.00%> (+93.77%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos
lkdvos merged commit 609be29 into main Aug 7, 2026
16 of 17 checks passed
@lkdvos
lkdvos deleted the fix-cutensor-trace-plan-leak branch August 7, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant