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

add scipy #123

Merged
merged 23 commits into from
Aug 1, 2022
Merged

add scipy #123

merged 23 commits into from
Aug 1, 2022

Conversation

wolfv
Copy link
Contributor

@wolfv wolfv commented Jul 19, 2022

It's far from working, though!

@wolfv wolfv changed the title add pythran and scipy add scipy Jul 23, 2022
@DerThorsten
Copy link
Contributor

DerThorsten commented Jul 25, 2022

todos:

  • add pyodide LICENCE to patch folder
  • put FORTRAN.py also in different folder st. this folder can have pyodides LICENCE

@wolfv
Copy link
Contributor Author

wolfv commented Jul 29, 2022

After disabling pthreads in pypocketfft, things are loading (almost) correctly in the browser. Another needed manual fix is that _cpropack needs to come before _zpropack and I had to manually resort it in the python_data.js.

We still have some remaining issues:

  • pythran is not working yet
  • scipy.optimize - seems like the Fortran code cannot properly call the Python function
  • sorting in empack (as mentioned above)

The following does not work currently (FORTRAN error calling function?).

import numpy as np
from scipy.optimize import minimize, rosen, rosen_der

def f(x):
    return x**2 + 10*np.sin(x)

print(minimize(f, x0=0))

@wolfv
Copy link
Contributor Author

wolfv commented Jul 30, 2022

Pythran should be fixed after removing strip all here: #129
Will try now!

@wolfv
Copy link
Contributor Author

wolfv commented Jul 30, 2022

I checked the pyodide binaries and there is a difference with ours for the propack stuff.

Ours:

[w@fedora _propack]$ wasm-nm _cpropack.cpython-310.so | grep bb
i bbcom_
[w@fedora _propack]$ wasm-nm _zpropack.cpython-310.so | grep bb
i bbcom_

Pyodide:

[w@fedora _propack]$ wasm-nm _zpropack.cpython-310-wasm32-emscripten.so | grep bb
i bbcom_
[w@fedora _propack]$ wasm-nm _cpropack.cpython-310-wasm32-emscripten.so | grep bb

So somehow, only one of the two files seems to refer to bbcom. We should check what's different!

@wolfv
Copy link
Contributor Author

wolfv commented Jul 30, 2022

I removed the other unecessary patch that tried to import pyodide js.

I tried around a bit, and the same js apply() error happens with the following code:

# Fail out of bounds?
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve
from numpy.linalg import solve, norm
from numpy.random import rand

A = lil_matrix((1000, 1000))
A[0, :100] = rand(100)
A[1, 100:200] = A[0, :100]
A.setdiag(rand(1000))


A = A.tocsr()
b = rand(1000)
# fail with js apply error?
x = spsolve(A, b)

And after the first apply() issues, it says something about memory acces out of bounds.

@wolfv wolfv closed this Jul 30, 2022
@wolfv wolfv reopened this Jul 30, 2022
@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

I manually tested the tests:

from scipy.linalg import lapack

print(lapack.dlamch("Epsilon-Machine"))

Gives the same

ERROR CATCHED TypeError: Cannot read properties of undefined (reading 'apply')
    at stubs.<computed> (pyjs_main.js:9:40190)
    at f2pywrapdlamch_ (007a6452:0xf2675)
    at f2py_rout__flapack_dlamch (007a6452:0xd3d1c)
    at fortran_call (0003af1a:0x32c4)
    at method_call_trampoline (pyjs_main.js:9:31331)
    at pyjs_main.wasm:0x261c11
    at pyjs_main.wasm:0x32b557
    at pyjs_main.wasm:0x328da5
    at pyjs_main.wasm:0x322f53
    at pyjs_main.wasm:0x322266

as the optimize functions. Maybe something interacting with Fortran that we need to solve in general.

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

OK, I looked a bit further, and it seems that none of the symbols of clapack_all.so are loaded and that's why the call to dlamch_ fails.

Screenshot from 2022-07-31 11-17-56

@DerThorsten
Copy link
Contributor

Uhhm ist the lib in the packed js? If not, we might be on a too old empack Version.

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

@DerThorsten I think that is not the problem. I think the problem is that clapack_all.so is not properly linked in the right places.

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

Btw. we should improve emscripten to throw better error messages for this case

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

I think this is the minimal reproducer:

from scipy.linalg import _flapack
_flapack.dlamch('E')

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

things are working fine if I manually "preload" the symbols from clapack_all.so in the global space:

  var deps = await waitRunDependency()

  Module.loadDynamicLibrary("/home/w/micromamba/envs/emx/lib/clapack_all.so",{
                fs: themodule.FS,
                global: true,
                nodelete: true
            });

would love to understand how to do that automatically / properly.

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

I have high hopes that the tests are passing now! :)

@wolfv
Copy link
Contributor Author

wolfv commented Jul 31, 2022

locally, when running the tests in the pyjs REPL, they are passign for me. No idea what's going on here

@wolfv wolfv merged commit 8dcead4 into emscripten-forge:main Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants