Skip to content

Fix recall metrics with integer-valued floating labels - #233

Open
laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-recall-float-targets
Open

laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-recall-float-targets

Conversation

@laasyapriya2007

Copy link
Copy Markdown

Please read through our contribution guide prior to creating your pull request.

Summary:
In BinaryRecall, computing true positives via bitwise AND (input & target).sum() raises RuntimeError: "bitwise_and_cpu" not implemented for 'Float' when integer-valued floating-point targets (such as torch.tensor([1.0, 0.0])) are passed. In MulticlassRecall, floating targets reach scatter indexing and raise RuntimeError: scatter(): Expected dtype int32/int64 for index.

This PR:

  1. Replaces bitwise AND with multiplication (input * target).sum() in _binary_recall_update (consistent with BinaryPrecision and BinaryF1Score).
  2. Converts targets and inputs to torch.int64 before scatter accumulation in _recall_update.
  3. Adds unit tests covering torch.float32 and torch.float64 targets across both functional and class-based recall implementations (including multi-batch state merging and all averaging modes: None, micro, macro, weighted).

Test plan:

  • Ran pytest tests/metrics/functional/classification/test_recall.py (11 passed).
  • Ran pytest tests/metrics/classification/test_recall.py -k float_target (all passed).
  • Verified with pre-commit run --files torcheval/metrics/functional/classification/recall.py tests/metrics/classification/test_recall.py tests/metrics/functional/classification/test_recall.py (all hooks passed: trailing whitespace, flake8, ufmt).

Fixes #209

Summary:
In `BinaryRecall`, computing true positives via bitwise AND `(input & target).sum()` raises `RuntimeError: "bitwise_and_cpu" not implemented for 'Float'` when integer-valued floating-point targets (such as `torch.tensor([1.0, 0.0])`) are passed. In `MulticlassRecall`, floating targets reach scatter indexing and raise `RuntimeError: scatter(): Expected dtype int32/int64 for index`.

Use multiplication `(input * target).sum()` for `BinaryRecall` (consistent with `BinaryPrecision` and `BinaryF1Score`), and ensure targets and inputs are converted to `torch.int64` before scatter accumulation in `MulticlassRecall`.

Add unit tests covering float32 and float64 targets across both functional and class-based recall implementations (with state merging and all averaging modes).

Fixes meta-pytorch#209
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binary Recall not working with float

1 participant