Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Memory Leak in BinaryPrecisionRecallCurve by Clearing Stored Predictions and Targets #2945

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/torchmetrics/classification/precision_recall_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,23 @@ def update(self, preds: Tensor, target: Tensor) -> None:

def compute(self) -> tuple[Tensor, Tensor, Tensor]:
"""Compute metric."""
state = (dim_zero_cat(self.preds), dim_zero_cat(self.target)) if self.thresholds is None else self.confmat
return _binary_precision_recall_curve_compute(state, self.thresholds)
if self.thresholds is None:
if not self.preds or not self.target:
return (
torch.ones(1, dtype=torch.float32), # precision
torch.zeros(1, dtype=torch.float32), # recall
torch.zeros(1, dtype=torch.float32), # thresholds
)

state = (torch.cat(self.preds), torch.cat(self.target))
self.preds.clear()
self.target.clear()
return _binary_precision_recall_curve_compute(state, None)

state = self.confmat
precision, recall, thresholds = _binary_precision_recall_curve_compute(state, self.thresholds)
self.confmat.zero_()
return precision, recall, thresholds if thresholds is not None else torch.zeros(0)

def plot(
self,
Expand Down
Loading