Skip to content
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

FEA Add support for float32 on PairwiseDistancesReduction using Tempita #23865

Merged
merged 22 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ sklearn/utils/_weight_vector.pxd
sklearn/linear_model/_sag_fast.pyx
sklearn/metrics/_dist_metrics.pyx
sklearn/metrics/_dist_metrics.pxd
sklearn/metrics/_pairwise_distances_reduction/_argkmin.pxd
sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx
sklearn/metrics/_pairwise_distances_reduction/_base.pxd
sklearn/metrics/_pairwise_distances_reduction/_base.pyx
sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pxd
sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pyx
sklearn/metrics/_pairwise_distances_reduction/_gemm_term_computer.pxd
sklearn/metrics/_pairwise_distances_reduction/_gemm_term_computer.pyx
sklearn/metrics/_pairwise_distances_reduction/_radius_neighborhood.pxd
sklearn/metrics/_pairwise_distances_reduction/_radius_neighborhood.pyx
6 changes: 5 additions & 1 deletion doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ Changelog

For instance :class:`sklearn.neighbors.NearestNeighbors.kneighbors` and
:class:`sklearn.neighbors.NearestNeighbors.radius_neighbors`
can respectively be up to ×20 and ×5 faster than previously.
can respectively be up to ×20 and ×5 faster than previously on a laptop.

Moreover, implementations of those two algorithms are now suitable
for machine with many cores, making them usable for datasets consisting
of millions of samples.

:pr:`21987`, :pr:`22064`, :pr:`22065`, :pr:`22288` and :pr:`22320`
by :user:`Julien Jerphanion <jjerphan>`.
Expand Down
36 changes: 36 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ Changelog
parameter `base_estimator` is deprecated and will be removed in 1.4.
:pr:`22054` by :user:`Kevin Roice <kevroi>`.

- |Efficiency| Low-level routines for reductions on pairwise distances
for dense float32 datasets have been refactored. The following functions
and estimators now benefit from improved performances in terms of hardware
scalability and speed-ups:

- :func:`sklearn.metrics.pairwise_distances_argmin`
- :func:`sklearn.metrics.pairwise_distances_argmin_min`
- :class:`sklearn.cluster.AffinityPropagation`
- :class:`sklearn.cluster.Birch`
- :class:`sklearn.cluster.MeanShift`
- :class:`sklearn.cluster.OPTICS`
- :class:`sklearn.cluster.SpectralClustering`
- :func:`sklearn.feature_selection.mutual_info_regression`
- :class:`sklearn.neighbors.KNeighborsClassifier`
- :class:`sklearn.neighbors.KNeighborsRegressor`
- :class:`sklearn.neighbors.RadiusNeighborsClassifier`
- :class:`sklearn.neighbors.RadiusNeighborsRegressor`
- :class:`sklearn.neighbors.LocalOutlierFactor`
- :class:`sklearn.neighbors.NearestNeighbors`
- :class:`sklearn.manifold.Isomap`
- :class:`sklearn.manifold.LocallyLinearEmbedding`
- :class:`sklearn.manifold.TSNE`
- :func:`sklearn.manifold.trustworthiness`
- :class:`sklearn.semi_supervised.LabelPropagation`
- :class:`sklearn.semi_supervised.LabelSpreading`

For instance :class:`sklearn.neighbors.NearestNeighbors.kneighbors` and
:class:`sklearn.neighbors.NearestNeighbors.radius_neighbors`
can respectively be up to ×20 and ×5 faster than previously on a laptop.

Moreover, implementations of those two algorithms are now suitable
for machine with many cores, making them usable for datasets consisting
of millions of samples.

:pr:`23865` by :user:`Julien Jerphanion <jjerphan>`.

:mod:`sklearn.cluster`
......................

Expand Down
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ ignore =
sklearn/utils/_weight_vector.pxd
sklearn/metrics/_dist_metrics.pyx
sklearn/metrics/_dist_metrics.pxd
sklearn/metrics/_pairwise_distances_reduction/_argkmin.pxd
sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx
sklearn/metrics/_pairwise_distances_reduction/_base.pxd
sklearn/metrics/_pairwise_distances_reduction/_base.pyx
sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pxd
sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pyx
sklearn/metrics/_pairwise_distances_reduction/_gemm_term_computer.pxd
sklearn/metrics/_pairwise_distances_reduction/_gemm_term_computer.pyx
sklearn/metrics/_pairwise_distances_reduction/_radius_neighborhood.pxd
sklearn/metrics/_pairwise_distances_reduction/_radius_neighborhood.pyx


[codespell]
skip = ./.git,./.mypy_cache,./doc/themes/scikit-learn-modern/static/js,./sklearn/feature_extraction/_stop_words.py,./doc/_build,./doc/auto_examples,./doc/modules/generated
Expand Down
64 changes: 43 additions & 21 deletions sklearn/manifold/tests/test_t_sne.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,32 +1048,54 @@ def test_gradient_bh_multithread_match_sequential():
assert_allclose(grad_multithread, grad_multithread)


def test_tsne_with_different_distance_metrics():
@pytest.mark.parametrize(
"metric, dist_func",
[("manhattan", manhattan_distances), ("cosine", cosine_distances)],
)
@pytest.mark.parametrize("method", ["barnes_hut", "exact"])
def test_tsne_with_different_distance_metrics(metric, dist_func, method):
"""Make sure that TSNE works for different distance metrics"""

if method == "barnes_hut" and metric == "manhattan":
# The distances computed by `manhattan_distances` differ slightly from those
# computed internally by NearestNeighbors via the PairwiseDistancesReduction
# Cython code-based. This in turns causes T-SNE to converge to a different
# solution but this should not impact the qualitative results as both
# methods.
# NOTE: it's probably not valid from a mathematical point of view to use the
# Manhattan distance for T-SNE...
# TODO: re-enable this test if/when `manhattan_distances` is refactored to
# reuse the same underlying Cython code NearestNeighbors.
# For reference, see:
# https://github.com/scikit-learn/scikit-learn/pull/23865/files#r925721573
pytest.xfail(
"Distance computations are different for method == 'barnes_hut' and metric"
" == 'manhattan', but this is expected."
)

random_state = check_random_state(0)
n_components_original = 3
n_components_embedding = 2
X = random_state.randn(50, n_components_original).astype(np.float32)
metrics = ["manhattan", "cosine"]
dist_funcs = [manhattan_distances, cosine_distances]
for metric, dist_func in zip(metrics, dist_funcs):
X_transformed_tsne = TSNE(
metric=metric,
n_components=n_components_embedding,
random_state=0,
n_iter=300,
init="random",
learning_rate="auto",
).fit_transform(X)
X_transformed_tsne_precomputed = TSNE(
metric="precomputed",
n_components=n_components_embedding,
random_state=0,
n_iter=300,
init="random",
learning_rate="auto",
).fit_transform(dist_func(X))
assert_array_equal(X_transformed_tsne, X_transformed_tsne_precomputed)
X_transformed_tsne = TSNE(
metric=metric,
method=method,
n_components=n_components_embedding,
random_state=0,
n_iter=300,
init="random",
learning_rate="auto",
).fit_transform(X)
X_transformed_tsne_precomputed = TSNE(
metric="precomputed",
method=method,
n_components=n_components_embedding,
random_state=0,
n_iter=300,
init="random",
learning_rate="auto",
).fit_transform(dist_func(X))
assert_array_equal(X_transformed_tsne, X_transformed_tsne_precomputed)


# TODO: Remove in 1.2
Expand Down
2 changes: 1 addition & 1 deletion sklearn/metrics/_dist_metrics.pxd.tp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t


######################################################################
# DistanceMetric base class
# DistanceMetric{{name_suffix}} base class
cdef class DistanceMetric{{name_suffix}}:
# The following attributes are required for a few of the subclasses.
# we must define them here so that cython's limited polymorphism will work.
Expand Down
33 changes: 0 additions & 33 deletions sklearn/metrics/_pairwise_distances_reduction/_argkmin.pxd

This file was deleted.

50 changes: 50 additions & 0 deletions sklearn/metrics/_pairwise_distances_reduction/_argkmin.pxd.tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{py:

implementation_specific_values = [
# Values are the following ones:
#
# name_suffix, INPUT_DTYPE_t, INPUT_DTYPE
#
# We also use the float64 dtype and C-type names as defined in
# `sklearn.utils._typedefs` to maintain consistency.
#
('64', 'cnp.float64_t', 'np.float64'),
('32', 'cnp.float32_t', 'np.float32')
]

}}

cimport numpy as cnp
from ...utils._typedefs cimport ITYPE_t, DTYPE_t

cnp.import_array()

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

from ._base cimport PairwiseDistancesReduction{{name_suffix}}
from ._gemm_term_computer cimport GEMMTermComputer{{name_suffix}}

cdef class PairwiseDistancesArgKmin{{name_suffix}}(PairwiseDistancesReduction{{name_suffix}}):
"""{{name_suffix}}bit implementation of PairwiseDistancesArgKmin."""

cdef:
ITYPE_t k

ITYPE_t[:, ::1] argkmin_indices
DTYPE_t[:, ::1] argkmin_distances

# Used as array of pointers to private datastructures used in threads.
DTYPE_t ** heaps_r_distances_chunks
ITYPE_t ** heaps_indices_chunks


cdef class FastEuclideanPairwiseDistancesArgKmin{{name_suffix}}(PairwiseDistancesArgKmin{{name_suffix}}):
"""EuclideanDistance-specialized {{name_suffix}}bit implementation for PairwiseDistancesArgKmin."""
cdef:
GEMMTermComputer{{name_suffix}} gemm_term_computer
const DTYPE_t[::1] X_norm_squared
const DTYPE_t[::1] Y_norm_squared

bint use_squared_distances

{{endfor}}
Loading