Skip to content

FIX Update randomized SVD benchmark #23373

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 2 commits into from
May 19, 2022
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
2 changes: 1 addition & 1 deletion benchmarks/bench_plot_randomized_svd.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def get_data(dataset_name):
elif dataset_name == "rcv1":
X = fetch_rcv1().data
elif dataset_name == "CIFAR":
if handle_missing_dataset(CIFAR_FOLDER) == "skip":
if handle_missing_dataset(CIFAR_FOLDER) == 0:
return
X1 = [unpickle("%sdata_batch_%d" % (CIFAR_FOLDER, i + 1)) for i in range(5)]
X = np.vstack(X1)
Expand Down
8 changes: 5 additions & 3 deletions sklearn/utils/extmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ def randomized_range_finder(

# Generating normal random vectors with shape: (A.shape[1], size)
Q = random_state.normal(size=(A.shape[1], size))
if A.dtype.kind == "f":
# Ensure f32 is preserved as f32
Q = Q.astype(A.dtype, copy=False)

# Deal with "auto" mode
if power_iteration_normalizer == "auto":
Expand All @@ -243,6 +240,11 @@ def randomized_range_finder(
# Sample the range of A using by linear projection of Q
# Extract an orthonormal basis
Q, _ = linalg.qr(safe_sparse_dot(A, Q), mode="economic")
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm so this changes now the dot product between A and Q will use float64 right, so will use twice more memory.

This seems a too wide-reaching change only to make the benchmark run ...

Copy link
Member

Choose a reason for hiding this comment

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

Ups I completely missed that we changed something extmath.py.
Sorry for merging this one.

We need to revert it.

Copy link
Contributor Author

@lorentzbao lorentzbao May 20, 2022

Choose a reason for hiding this comment

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

Sorry for the bad PR. I have overlooked the downsides of using the f64 matrix. Do you have other ideas on how to fix the benchmark? Thanks for the follow-up FIX in #23421. I should have taken a closer look at the documentation, which clearly indicates large n_iter might cause numeric instability when power_iteration_normalizer='none'. @lesteve @glemaitre.

power_iteration_normalizer : {'auto', 'QR', 'LU', 'none'}, default='auto'
Whether the power iterations are normalized with step-by-step
QR factorization (the slowest but most accurate), 'none'
(the fastest but numerically unstable when `n_iter` is large, e.g.
typically 5 or larger), or 'LU' factorization (numerically stable
but can lose slightly in accuracy). The 'auto' mode applies no
normalization if `n_iter` <= 2 and switches to LU otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

No worries, things like that happen! Thanks a lot for this PR, we are now very close to have a working benchmark 😉


if hasattr(A, "dtype") and A.dtype.kind == "f":
# Ensure f32 is preserved as f32
Q = Q.astype(A.dtype, copy=False)

return Q


Expand Down