pytorch / pytorch/ignite

[Enhancement] Native PyTorch Implementation for `SpearmanRankCorrelation`

Open
#3,663 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.8k
Forks
726
Avg merge
5d 21h
Merged PRs (30d)
5

Description

Description

The current implementation of SpearmanRankCorrelation in ignite.metrics.regression is a wrapper around scipy.stats.spearmanr and inherits from EpochMetric. This introduces several limitations:

Hard Dependency:
It requires scipy to be installed. As shown in the reproduction below, the metric cannot even be initialized in a pure PyTorch environment.

Memory Inefficiency:
As an EpochMetric, it stores all predictions and targets in a list (O(n) memory complexity), which can lead to Out-Of-Memory (OOM) issues on large datasets.

Performance Bottleneck:
Data must be moved to the CPU and converted to NumPy/Scipy, preventing the metric from running natively on accelerators such as GPU/TPU.


Steps to Reproduce (In Environment Without SciPy)

import torch
from ignite.metrics.regression import SpearmanRankCorrelation

# This raises ModuleNotFoundError immediately
metric = SpearmanRankCorrelation()

Actual Output

ModuleNotFoundError: This module requires scipy to be installed.

Proposed Improvement

I propose a Native PyTorch implementation of SpearmanRankCorrelation that:

Remove the SciPy Dependency

Use torch.argsort or a unique/cumsum-based ranking logic to compute ranks directly in PyTorch.

Use Streaming Accumulation

Instead of relying on EpochMetric, accumulate the necessary sums for the Pearson correlation of ranks incrementally.
This reduces memory complexity from:

O(n) → O(1)

(or O(batch) depending on the ranking approach).

Device Agnostic

Keep all computations on the original device (CPU/GPU) so the metric can run efficiently on accelerators.


Hi @vfdev-5, I'd like to work on refactoring this as part of my GSoC contributions. I believe making this PyTorch-native will significantly improve the portability and performance of Ignite's regression suite. Waiting for your thoughts! 😊.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SpearmanRankCorrelation in ignite.metrics.regression and inspect its current EpochMetric and scipy.stats.spearmanr usage. Review the ranking and accumulation requirements in the issue before deciding how ties and batches should be handled. Done means the metric works without SciPy, avoids storing the full epoch, and keeps computation on the input device.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.