Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions python/cuml/cuml/neighbors/kernel_density.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
# Copyright (c) 2022-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,14 +61,22 @@ def tophat_log_kernel(x, h):


@cp.fuse()
def epanechnikov_log_kernel(x, h):
def _epanechnikov_log_kernel(x, h, h_squared):
# don't call log(0) otherwise we get NaNs
z = cp.maximum(1.0 - (x * x) / (h * h), 1e-30)
z = cp.maximum(1.0 - (x * x) / h_squared, 1e-30)
y = (x < h) * cp.log(z)
y += (x >= h) * np.finfo(y.dtype).min
return y


def epanechnikov_log_kernel(x, h):
# TODO: Due to https://github.com/cupy/cupy/issues/8536 cupy.fuse errors when trying
# to compile the elementwise operation in epanechnikov. Handling `h * h` on host
# (where `h` is a host scalar) seems to work around the bug completely. Once the upstream
# issue is fixed this can be reverted.
return _epanechnikov_log_kernel(x, h, h * h)


@cp.fuse()
def exponential_log_kernel(x, h):
return -x / h
Expand Down
12 changes: 3 additions & 9 deletions python/cuml/cuml/tests/test_kernel_density.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
# Copyright (c) 2022-2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@

from cuml.testing.utils import as_type
from sklearn.model_selection import GridSearchCV
import os
import pytest
from hypothesis.extra.numpy import arrays
from hypothesis import given, settings, assume, strategies as st
Expand Down Expand Up @@ -82,10 +81,6 @@ def array_strategy(draw):
)


@pytest.mark.skipif(
os.environ.get("CUDA_VERSION") == "12.0.1",
reason="Flaky hypothesis CI failure: https://github.com/rapidsai/cuml/issues/6173",
)
@settings(deadline=None)
@given(
array_strategy(),
Expand All @@ -101,9 +96,8 @@ def test_kernel_density(arrays, kernel, metric, bandwidth):
# cosine is numerically unstable at high dimensions
# for both cuml and sklearn
assume(X.shape[1] <= 20)
kde = KernelDensity(kernel=kernel, metric=metric, bandwidth=bandwidth).fit(
X, sample_weight=sample_weight
)
kde = KernelDensity(kernel=kernel, metric=metric, bandwidth=bandwidth)
kde.fit(X, sample_weight=sample_weight)
cuml_prob = kde.score_samples(X)
cuml_prob_test = kde.score_samples(X_test)

Expand Down