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

Allow the numba cache to be used, for development #441

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

hyanwong
Copy link
Member

@hyanwong hyanwong commented Dec 1, 2024

Copied from SGKIT. Fixes #438

@hyanwong hyanwong mentioned this pull request Dec 1, 2024
@nspope
Copy link
Contributor

nspope commented Dec 1, 2024

I ran into the same issues as you here-- the bulk of stuff is still being compiled on import. I think pulling all the EP stuff into a low-level extension is the way to go.

@hyanwong
Copy link
Member Author

hyanwong commented Dec 1, 2024

I think pulling all the EP stuff into a low-level extension is the way to go.

Can we do this while keeping it in python (I presume not)? Or are you thinking of writing the low-level e.g. in rust?

@nspope
Copy link
Contributor

nspope commented Dec 1, 2024

It may be possible to build the extension with numba, but I think it'd be stabler and easier to maintain a C or rust extension.

Numba complains about these
@nspope
Copy link
Contributor

nspope commented Jan 16, 2025

I could make the edits to the scipy special functions on this branch if you'd like @hyanwong ?

@hyanwong
Copy link
Member Author

Yes, why not. Thank you.

@hyanwong
Copy link
Member Author

Do you want me to have a go at swapping in math.lgamma for the script version, @nspope ?

@nspope
Copy link
Contributor

nspope commented Jan 20, 2025

Sure, sorry to drop the ball on this. Where I'm thinking is, https://github.com/tskit-dev/tsdate/blob/240593d01a1a77561dadebee82cfb5fae67f83bc/tsdate/hypergeo.py#L72C17-L74C26

the _gammaln_f8 should be changed to math.lgamma -- hopefully that's enough to make more functions cache-able?

@hyanwong
Copy link
Member Author

No problem,. I'll give it a whirl. Thanks for the pointer.

@hyanwong
Copy link
Member Author

hyanwong commented Jan 20, 2025

OK, with the new commits and the math.lgamma function this now seems to work, and cuts import down from 29 seconds (with TSDATE_DISABLE_NUMBA_CACHE=1) to 9 seconds (with TSDATE_DISABLE_NUMBA_CACHE=0) on my laptop.

I put _hyp2f1_unity as an inner function within _hyp2f1_laplace, as I couldn't figure out how to pass things neatly otherwise. I guess you could rework that if it grates or is expected to make things slow @nspope. I haven't figured out how to make _gammainc_inv (and the related approximate_gamma_iqr) cacheable either, so that would make loading faster too, if you can figure it out.

@hyanwong
Copy link
Member Author

Aha. I spotted some more functions I could mark to cache. Now down to 5 secs for import.

@hyanwong
Copy link
Member Author

I guess this is good to go, at least for the time being.

To enable the caching (e.g. for development), you need to explicitly set TSDATE_DISABLE_NUMBA_CACHE=0 which seems a bit of a weird way to phrase it (I would have chosen TSDATE_ENABLE_NUMBA_CACHE=1 I think). I just copied that from sgkit: is there any reason for the choice of name / polarisation in sgkit, @jeromekelleher ?

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.

Reduce import time
2 participants