Skip to content

Fix how PCA determines which solver is available for array API inputs - #34238

Closed
betatim wants to merge 1 commit into
scikit-learn:mainfrom
betatim:fix-pca-array-api-unsupported
Closed

betatim wants to merge 1 commit into
scikit-learn:mainfrom
betatim:fix-pca-array-api-unsupported

Conversation

@betatim

@betatim betatim commented Jun 10, 2026

Copy link
Copy Markdown
Member

PCA was checking for array API enabled or not to gate access to solvers in PCA. Instead we should check if the input is not array-like, which is a more precise way to gate this.

The invariants that should be true are:

  • numpy input with array API dispatching turned off and on should result in the same behaviour
  • torch-cpu input with array API dispatching turned off and on should result in the same features being available (e.g. the same solvers should be available)
  • torch-cpu input with array API dispatching disabled should have fitted attributes that are numpy.
  • torch-cpu input with array API dispatching enabled should have fitted attributes that are torch-cpu.

The change in the test_pca_mle_array_api_compliance test is a fix for float32. The check now makes sure that the "left over" components are all compatible with being noise. Even if the two runs can't agree on how many components there are.

I think we can clean up the code a bit more, but I'm starting the PR now to see if we can agree on the invariants and the behaviour. This is already the third iteration of this fix, each time thinking that "this time I got it right". This is why I now opened the PR to see if others agree on the invariants

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation

PCA was checking for array API enabled or not to gate access to solvers
in PCA. Instead we should check if the input is not array-like, which is
a more precise way to gate this.
# Dispatch disabled: any array-like is coerced to NumPy and always works.
# Force the config explicitly so the test is robust to the ambient config
# (e.g. when the whole suite is run with array_api_dispatch enabled).
with config_context(array_api_dispatch=False):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should remove this, maybe use an assert instead. Having this is unusual/inconsistent with how our other test are written.

return None
# np.ascontiguousarray avoids negative/0 strides (e.g. from the
# arpack output reversals) that some namespaces cannot consume.
return xp.asarray(np.ascontiguousarray(array), device=input_device)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe this should use move_to instead of making a new function.

I have to admit I didn't quiz Opus enough to understand why exactly we need the ascontiguousarray)

and _is_arraylike(X)
and not _is_xp_namespace(xp, "array_api_strict")
):
return self._fit_with_numpy_fallback(X, xp)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should pass via validate_data before starting to fit. So calling _fit_with_numpy_fallback here is too early.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant