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

Compute noise_variance_ in PCA implementation #6234

Open
wants to merge 2 commits into
base: branch-25.02
Choose a base branch
from

Conversation

jcrist
Copy link
Member

@jcrist jcrist commented Jan 17, 2025

Previously noise_vars was an output parameter passed to the cuda PCA implementation, but it was unimplemented. This adds support for computing noise_vars in the cuda code, and tests that the results are valid by comparing to the scikit-learn implementation.

The previous code would always have a noise_variance_ of 0, resulting in downstream issues interpreting results after converting a cuml estimator to its sklearn equivalent (e.g. broken score_samples).

@jcrist jcrist requested review from a team as code owners January 17, 2025 16:27
@jcrist jcrist requested review from bdice and wphicks January 17, 2025 16:27
@github-actions github-actions bot added Cython / Python Cython or Python issue CUDA/C++ labels Jan 17, 2025
@jcrist jcrist force-pushed the pca-noise-variance branch from f73ec37 to c4939b6 Compare January 17, 2025 16:28
@jcrist jcrist added non-breaking Non-breaking change bug Something isn't working labels Jan 17, 2025
@@ -128,9 +128,6 @@ void fit_impl(raft::handle_t& handle,
streams,
n_streams,
verbose);
for (std::uint32_t i = 0; i < n_streams; i++) {
handle.sync_stream(streams[i]);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sync point was unneeded, it was already handled after exiting this branch below.

true,
stream);
} else {
raft::matrix::setValue(noise_vars, noise_vars, math_t{0}, 1, stream);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noise_vars is a len-1 device array that I want to set to 0. This seems to work (and is used at least one other place in cuml already), but I'm honestly not sure if it's the best spelling of that. In particular, I don't understand why a method for filling an output array with a single value also takes in an input array (which is the same as the output array in all calling locations I can find).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use raft::matrix::fill from raft/include/matrix/init.cuh Line 68. It is the more up-to-date function. You would need to create a mdspan to use it as such:

auto noise_vars_span = raft::make_device_vector_view(noise_vars, 1);

Previously `noise_vars` was an output parameter passed to the cuda PCA
implementation, but it was unimplemented. This adds support for
computing `noise_vars` in the cuda code, and tests that the results
are valid by comparing to the scikit-learn implementation.

The previous code would always have a `noise_variance_` of 0, resulting
in downstream issues interpreting results after converting a cuml
estimator to its sklearn equivalent (e.g. broken `score_samples`).
@jcrist jcrist force-pushed the pca-noise-variance branch from c4939b6 to 4a927b3 Compare January 17, 2025 22:48
true,
stream);
} else {
raft::matrix::setValue(noise_vars, noise_vars, math_t{0}, 1, stream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use raft::matrix::fill from raft/include/matrix/init.cuh Line 68. It is the more up-to-date function. You would need to create a mdspan to use it as such:

auto noise_vars_span = raft::make_device_vector_view(noise_vars, 1);

// Compute the scalar noise_vars defined as (pseudocode)
// (n_components < min(n_cols, n_rows)) ? explained_var_all[n_components:].mean() : 0
if (prms.n_components < prms.n_cols && prms.n_components < prms.n_rows) {
raft::stats::mean(noise_vars,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the newer mdspan-API of this mean function as well?

Copy link
Member Author

@jcrist jcrist Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my understanding that since the PCA code here doesn't create streams from the handle (#2470), and is mostly using the legacy APIs here that take streams instead of handles, that switching to the mdspan APIs instead would require a substantial refactor. I'm happy to take that on in a follow-up, but would prefer to port all the code to the newer APIs at the same time, rather than try to force it in as part of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's a valid point, let's keep that port for an other dedicated PR.

pytest.param(20, 10, id="n_features <= n_components"),
],
)
def test_noise_variance_zero(n_samples, n_features):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a test other than zero be added? To check the correctness of the computation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is checking the case where the noise variance is defined as zero since there weren't enough samples or features. Other cases where it's non zero are already tested thoroughly above on line 75.

Copy link
Contributor

@lowener lowener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// Compute the scalar noise_vars defined as (pseudocode)
// (n_components < min(n_cols, n_rows)) ? explained_var_all[n_components:].mean() : 0
if (prms.n_components < prms.n_cols && prms.n_components < prms.n_rows) {
raft::stats::mean(noise_vars,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's a valid point, let's keep that port for an other dedicated PR.

Copy link
Member

@dantegd dantegd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python codeowner approval based in @lowener approval

@dantegd
Copy link
Member

dantegd commented Jan 29, 2025

/merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CUDA/C++ Cython / Python Cython or Python issue non-breaking Non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants