`RetrievalMAP` and `RetrievalMRR` do not validate invalid top_k values
bug / fixhelp wanted
## 🐛 Bug
`RetrievalMAP` and `RetrievalMRR` do not correctly validate invalid top_k values.
This behavior is inconsistent with other retrieval metrics such as `RetrievalNormalizedDCG`, which raises a clear `ValueError` for invalid top_k values.
For example, `RetrievalMRR(top_k=-1)` is accepted at initialization, but later fails inside `torch.topk` with a `RuntimeError`.
### To Reproduce
`RetrievalMRR(top_k=-1)` should raise a clear `ValueError`, but it currently raises a `RuntimeError` during metric computation.
<!--
Create a new Lightning Studio with code that reproduces the issue and share the link.
Also include all the relevant files and data required to reproduce shared issue.
In case the code does not crash, please add assert statements to show what is the real and expected output.
A simple guide on how to create such a studio can be found [here](
https://www.youtube.com/watch?v=YcW-2Zt_bFg&ab_channel=LightningAI).
-->
<details>
<summary>Code sample</summary>
```python
from torch import tensor
from torchmetrics.retrieval import RetrievalMRR
indexes = tensor([0, 0, 0, 1, 1, 1, 1])
preds = tensor([0.2, 0.3, 0.5, 0.1, 0.3, 0.5, 0.2])
target = tensor([False, False, True, False, True, False, True])
mrr = RetrievalMRR(top_k=-1)
mrr(preds, target, indexes=indexes)
```
Current error:
```text
RuntimeError: selected index k out of range
```
Expected behavior:
```text
ValueError: `top_k` has to be a positive integer or None
```
</details>
<details>
<summary>Environment</summary>
- TorchMetrics version (if build from source, add commit SHA): 1.9.0
- Python & PyTorch Version (e.g., 1.0): 3.12.13
- Any other relevant information such as OS (e.g., Linux): Run on Google Colab
</details>
### Additional context
I think the cause is the `top_k` validation condition in the following places:
- `RetrievalMRR`: https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/retrieval/reciprocal_rank.py#L113
- `retrieval_reciprocal_rank`: https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/functional/retrieval/reciprocal_rank.py#L52
- `RetrievalMAP`: https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/retrieval/average_precision.py#L113
- `retrieval_average_precision`: https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/functional/retrieval/average_precision.py#L52
The condition currently uses `and`, so invalid integer values such as `top_k=-1` can pass validation
If this needs to be fixed, I would be happy to open a PR.
<!-- Add any other context about the problem here. -->
0 条评论