Skip to content

MAINT Remove redundant sparse square euclidian distances function #25731

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 1 commit 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
22 changes: 3 additions & 19 deletions sklearn/metrics/_pairwise_distances_reduction/_base.pyx.tp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ from sklearn import get_config
from sklearn.utils import check_scalar
from ...utils._openmp_helpers import _openmp_effective_n_threads
from ...utils._typedefs import DTYPE, SPARSE_INDEX_TYPE
from ...utils.sparsefuncs_fast import _sqeuclidean_row_norms_sparse

cnp.import_array()

Expand Down Expand Up @@ -103,23 +104,6 @@ cdef DTYPE_t[::1] _sqeuclidean_row_norms32_dense(
return squared_row_norms


cdef DTYPE_t[::1] _sqeuclidean_row_norms64_sparse(
const DTYPE_t[:] X_data,
const SPARSE_INDEX_TYPE_t[:] X_indptr,
ITYPE_t num_threads,
):
cdef:
ITYPE_t n = X_indptr.shape[0] - 1
SPARSE_INDEX_TYPE_t X_i_ptr, idx = 0
DTYPE_t[::1] squared_row_norms = np.zeros(n, dtype=DTYPE)

for idx in prange(n, schedule='static', nogil=True, num_threads=num_threads):
for X_i_ptr in range(X_indptr[idx], X_indptr[idx+1]):
squared_row_norms[idx] += X_data[X_i_ptr] * X_data[X_i_ptr]

return squared_row_norms


{{for name_suffix, INPUT_DTYPE_t, INPUT_DTYPE in implementation_specific_values}}

from ._datasets_pair cimport DatasetsPair{{name_suffix}}
Expand All @@ -131,10 +115,10 @@ cpdef DTYPE_t[::1] _sqeuclidean_row_norms{{name_suffix}}(
):
if issparse(X):
# TODO: remove this instruction which is a cast in the float32 case
# by moving squared row norms computations in MiddleTermComputer.
# by moving squared row norms computations in MiddleTermComputer.
X_data = np.asarray(X.data, dtype=DTYPE)
X_indptr = np.asarray(X.indptr, dtype=SPARSE_INDEX_TYPE)
return _sqeuclidean_row_norms64_sparse(X_data, X_indptr, num_threads)
return _sqeuclidean_row_norms_sparse(X_data, X_indptr, num_threads)
else:
return _sqeuclidean_row_norms{{name_suffix}}_dense(X, num_threads)

Expand Down