[RFC] WASM / pyodide as a (somewhat) officially supported platform for scikit-learn

We started having bug reports (at least one indirect, in real life report at a conference: #23707) from users of scikit-learn in WASM environment (e.g. pyodide / jupyterlite, pyscript...).

Shall we invest effort in setting CI tooling to properly test and maybe even handle packaging of scikit-learn to target that platform?

It's very likely that not all of scikit-learn will work out of the box, but with proper tooling in place we could maintain a public list of modules that have all their tests that pass and maybe a list of modules that required so patches to handle graceful degradation to target this platform (e.g. number of parallel worker threads with n_jobs).

@rth put some interesting info in the following comment on how to run the tests:

- https://github.com/scikit-learn/scikit-learn/issues/23707#issuecomment-1163053666

Pros:
- WASM is likely to be a very popular target platform, at least for education (can directly teach Python programming and ML concepts without having to teach how to install packages from the command line first).

Cons:
- test execution is probably much slower that on our regular CI targets;
- need to maintain a list of known issues / limitations;
- more packaging, release process will be even more complicated;
- SciPy is quite heavily patched because there is no working Fortran compiler on that platform  (that might change soon with lfotran) so it relies on a semi-hackish Fortran to C transpilation step that introduces additional complexity.

COMMENT:
Yes, I think running it in CI is probably indeed a bit early (and also it would be really slow). The first more investigative steps could be a good start,
 1. manually run the test suite module by module, see what fails (and report upstream). This would already be very helpful. Last time I did it was in 2018 in https://github.com/pyodide/pyodide/issues/139#issuecomment-436430260 and the situation should be much better now.
 2. figure out how to best run the test suite programmatically for a large package such as scikit-learn. Pyodide has a [pytest plugin](https://github.com/pyodide/pyodide/tree/main/pyodide-test-runner/pyodide_test_runner) which will be better packaged once https://github.com/pyodide/pyodide/pull/2742 is merged. Once installed [it exposes  pytest fixtures](https://github.com/pyodide/pyodide/blob/main/packages/scikit-learn/test_scikit-learn.py#L11)  that would allow running some Python code in Pyodide inside a browser (Chrome or Firefox) with selenium or Node.js. Though for now, this package has no users outside of Pyodide, so more work is likely necessary to make it standalone and re-usable by external packages.

Then once we have some way to run Python code inside the browser from a Python script (or pytest) on the host, the question remains how to best run the full scikit-learn test suite. The problem is that when running `pytest.main` over the full package directly it takes a while and no feedback is reported to the user until the run completes. Furthermore, if there is a fatal error in scipy somewhere (similar to a segfault in terms of outcome) then the whole session would crash. So it's probably better to run pytest inside WASM on smaller chunks, serialize back the results and concatenate them on the host. A  bit similar things about which I was wondering in https://github.com/pytest-dev/pytest-xdist/issues/336 as in the end the problem is very similar to running pytest on the remote node (except that communication is not happening over the network).

In any case, if anyone is interested in investigating this, I'd be happy to talk more about it.

Labels: RFC, Needs Investigation