Skip to content

ENH Let csr_row_norms support multi-thread #25598

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

Merged
merged 6 commits into from
Feb 28, 2023
Merged
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
21 changes: 12 additions & 9 deletions sklearn/utils/sparsefuncs_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ from libc.math cimport fabs, sqrt
cimport numpy as cnp
import numpy as np
from cython cimport floating
from cython.parallel cimport prange
from numpy.math cimport isnan

from sklearn.utils._openmp_helpers import _openmp_effective_n_threads

cnp.import_array()

ctypedef fused integral:
Expand All @@ -27,13 +30,14 @@ def csr_row_norms(X):
"""Squared L2 norm of each row in CSR matrix X."""
if X.dtype not in [np.float32, np.float64]:
X = X.astype(np.float64)
return _csr_row_norms(X.data, X.indices, X.indptr)
n_threads = _openmp_effective_n_threads()
return _sqeuclidean_row_norms_sparse(X.data, X.indptr, n_threads)


def _csr_row_norms(
def _sqeuclidean_row_norms_sparse(
const floating[::1] X_data,
const integral[::1] X_indices,
const integral[::1] X_indptr,
int n_threads,
):
cdef:
integral n_samples = X_indptr.shape[0] - 1
Expand All @@ -42,14 +46,13 @@ def _csr_row_norms(

dtype = np.float32 if floating is float else np.float64

cdef floating[::1] norms = np.zeros(n_samples, dtype=dtype)
cdef floating[::1] squared_row_norms = np.zeros(n_samples, dtype=dtype)

with nogil:
for i in range(n_samples):
for j in range(X_indptr[i], X_indptr[i + 1]):
norms[i] += X_data[j] * X_data[j]
for i in prange(n_samples, schedule='static', nogil=True, num_threads=n_threads):
for j in range(X_indptr[i], X_indptr[i + 1]):
squared_row_norms[i] += X_data[j] * X_data[j]

return np.asarray(norms)
return np.asarray(squared_row_norms)


def csr_mean_variance_axis0(X, weights=None, return_sum_weights=False):
Expand Down