You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use Cython, Numba, or C/C++ for algorithmic code #126
The default today seems to be Cython. This would be a fine choice. It seems to be fine for speed and community adoptability. I do think that it imposes a serious barrier to attracting new developers (I'd say 80% of the candidate pool will avoid Cython), and it will force a more complex build-and-deploy process. For example we can no longer just point people to try things on master to get quick feedback. We'll get questions from people on how to install from source on windows, etc..
C/C++ is, I think, a little cleaner than Cython in terms of attracting new developers. I think that C tends to be more often within people's comfort zone.
Numba is great for new developers and maintenance, but has issues in community adoptability (folks aren't accustomed to depending on it, thoughts here). Numba also has issues if we're going to be doing a lot with dynamic data structures and want to use std::map, std::vector and friends.
Having some familiarity with both Numba and Cython I personally also find the development cycle to be must faster with Numba than with Cython.
Suggestion
If I were doing this work I would stick to Numba until numba became an issue (either due to a need for dynamic data structures or downstream libraries being unwilling to depend on it) and then I would switch to something else. The cost of starting with Numba to me seems to be zero. We don't have to update our release mechanism, we don't have to have special documentation or field extra compilation help requests. The cost to switch to Cython if necessary is likely very small. We'll likely be writing exactly the same code we would write for Cython, just with less stuff around it.
Numba is also likely to be faster without us thinking as much, which I think is useful when experimenting with new algorithms, like in #125
However
However, I'm unlikely to do any of this work in the near future, and the preferences of those doing the work should have precedence.
cc @rgommers@ogrisel@stefanv@shoyer for thoughts and hopefully critical feedback. In what system would you like to see low-level code in the pydata/sparse library written?
Speed is near-native C++ with the benefits of Python.
Community adoptability: We will only need it for development, not as a hard dependency, so it has zero impact.
Barriers to attracting new developers: Only a few algorithms need to be in Cython/Wrapped C++, others will be pure Python. Developers will have the choice of both wrapped C++ with Cython, or pure Cython.
Wrapping logic is really trivial. BLAS and friends have wrappers in SciPy, we could use those.
Making DOK faster is a priority for me for batch assignments (i.e. ndarray and SparseArray assignments), this needs std::unordered_map.
Switching can be a somewhat expensive process down the line.
@shoyer's algorithm for elemwise discussed in Support Everything that XArray Expects #1 will come in useful for CSD. Since the passes in that algorithm are likely to be slow, Numba could be almost twice as slow as std::vector. Other uses are very likely to pop up sooner or later.
Debugging is better. This can be critical with complex logic.
Interleaving code is relatively painless.
We could re-use some things such as for radix sort.
In favor of Numba
Pure Python. This could be useful for attracting devs.
Faster performance overall.
Iteration of N-dimensional arrays is possible without complex logic. This will be useful for advanced indexing.
GPU support (?)
My Concerns
I guess it comes down to exactly two questions for me: Will we have to switch at some point (which I would like to avoid outright), or will Numba devs be willing to add support for accelerated dict and list operations, and interleaving? If the answers to both these questions are in the favor of Numba, then it's a no-brainer for me.
If, in the other hand, the answer to even one of these questions is "no"... Then my vote is for a mixture of Cython and Cython-wrapped C++.
I guess my main worries with Numba come down to the following:
Not everything is possible in it. I can think of a few occasions I ran into a brick wall with Numba.
We might have to switch, which could be more expensive than going with Cython in the first place.
I would not expect accelerated list/dict implementations from Numba. This is probably out of scope for them.
Where possible I would like to see us avoid dynamic data structures for performance reasons
I would not expect std::map to be significantly faster than Python's dict. At best I would hope for 2-3x improvements, not 10x.
I find debugging to be much nicer in numba, I just remove numba.jit. I'd be curious to learn your debugging technique for Cython
Can you expand what you mean by interleaving?
I think that the presence of Cython in the codebase is itself a barrier to adoption. People won't be able to run from master without passing through a compilation step.
I'm surprised to hear that switching from numba to cython seems expensive to you. To me it seems like the only cost is the work saved in the first place. In my experiences going the opposite direction (Cython -> Numba) code typically works after I strip out all of the annotations, rename the file from pyx to py, and put numba.jit on the function. For numeric for-loopy code they seem to be more or less equivalent to me.
However, mostly this is me trying to convince you not to adopt Cython prematurely. At this point you're doing most of the work and should probably decide.
As a co-maintainer though I would appreciate it if, prior to actually writing algorithmic code in Cython you first to setup the build process, packaging support (we should consider conda-forge), and documentation. I don't expect this to be particularly difficult, I don't think we'll need any external libraries to speak of, but it's best to understand the costs ahead of time and build in solutions in case you leave this project in the future.
FWIW while I'm probably biased towards numba due to professional association I also have a lot of respect for Cython. As someone who has used both in different projects though (numba for personal things, cython for geopandas) and as someone who maintains a number of projects, the added cost of maintaining a Cython project in terms of person-hours is high, and that cost tends to be focused on fewer maintainers. I think I tend to optimize these days more to reduce and diffuse maintenance costs than most other things.
I personally would probably stop spending as much personal time fielding maintenance questions or build/release issues if we go for Cython. This is something that the community would need to pick up long term.
I would not expect std::map to be significantly faster than Python's dict. At best I would hope for 2-3x improvements, not 10x.
There is also std::unordered_map, which is 2x-3x faster than std::map, and is the best equivalent to dict. std::map is more of a binary search tree.
I'd be curious to learn your debugging technique for Cython
GDB supports python natively, and Cython has extensions for GDB. It's on the command line, though (I've tried and failed to find a good IDE solution), so it is a bit of a pain compared to your solution. Also, it freezes your interpreter in, so conda env is a no-no.
Can you expand what you mean by interleaving?
Interleaving Pure Python/optimized code. Other problems would be calling unoptimized code from optimized.
People won't be able to run from master without passing through a compilation step
In my experiences going the opposite direction (Cython -> Numba) code typically works after I strip out all of the annotations, rename the file from pyx to py, and put numba.jit on the function.
There are also costs in terms of alienated developers we may have accrued that are used to Numba.
However, mostly this is me trying to convince you not to adopt Cython prematurely.
Of course, I understand the costs, and if the consensus is on Cython, then I would build these solutions in, along with docs, right at the very first check-in. I believe properly > quickly.
After your convincing and experience, I'm not too opposed to Numba, but would certainly like to wait for thoughts of others (particularly Scipy devs and whether they would consider it to be a blocker) before committing to it.
At this point you're doing most of the work and should probably decide.
My vote is still slightly in favor of Cython, but I'm open to Numba as an option. I don't believe I should make the decision, I would really wait for community consensus.
I have started watching this project only recently and although I was super happy to finally a sparse multidimensional tensor lib that I could contribute to I'm just an observer here.
Funnily enough I spent some time yesterday trying to implement specialized tensor products for DOK with numba, and found out that dicts are not a recognized type yet. I used ordered list of tuples instead of dicts everywhere, which seems to be supported well but @mrocklin must know the details better than I do.
Here is one argument in favour of numba, which I haven't seen listed above. I have found very useful in other projects the ability to generate code, based on number of dimensions for instance, and jit-compile it to a fast, memory-savvy option. There are several instances, where I found this approach to be much faster than using numpy's generic functions, and I wouldn't know how to perform the same with Cython or C/C++ for that matter as dimension combinations are not known at compile-time.
At this point, if Scipy devs came out and said they would be okay to depend on this project if Numba was used, I'd go for Numba. I just had a look at the docs and they're significantly better than Cython's (which alone makes them worth considering).
It'd be nice to have another contributor, @albop. :-)
@hameerabbasi : I was considering saying something about me contributing to the project, but wouldn't like to disappoint if it takes me a bit too long to start. I'll try to create some issues first ;-) (with the usecase I have in mind...)
@albop If you have any questions about where something is handled, or how to implement something, don't hesitate to raise an issue and cc me on it. :-)
C++ is fine as long as the wrapper tool is modern enough to make it easy to package as a wheel (e.g. Cython or pybind11, no boost python please). I am fine with numba as a dependency as well. I can't speak for scipy developers.
I can't speak for all SciPy developers, but if Numba continues to improve and be maintained (not sure of its status now with all the changes at Anaconda?) then at some point we'll likely adopt it. The concerns regarding build dependencies etc. are minor compared to all the C/C++/Fortran we have already. If you want me to start that discussion now on scipy-dev, I'd be happy to do so.
Re debugging: it depends on what you're debugging. If it's an issue with the logic of your code, then Numba is much better (just remove @jit). If it's a problem with Numba or Cython behavior though, then Cython's debugging support is far better than Numba's. The latter case is probably less common, but not that uncommon - both due to changes in new releases and things that don't work identically to plain Python code in the first place. I'd say that as a contributor I would prefer Numba, and as a library maintainer I'd prefer Cython.
Being able to nest containers (like list[list[int]]) should be available in Numba 0.39 (scheduled for late June). However, we suspect that will reveal a new bottleneck for some use cases, where the unboxing cost of nested Python lists (when transitioning from Python to nopython mode) becomes prohibitive. For temporary lists, this won't matter.
The architecture support limitations should soon be resolved. By next release, ARMv7 will be working (except some issues that are also present in SciPy linalg functions), ARMv8 is working except for a few NaT conversion issues when working with datatime dtypes, and POWER8 will be solved with the future LLVM 6.0.1 release which contains some required PPC64 bug fixes.
We've had a request from HPAT for a typed dictionary that can be used as a temporary data structure inside algorithms. This is something we want to do for several reasons, but we're currently juggling development priorities between these sort of container features, refactoring our ufunc/gufunc compiler, and supporting the ability to jump back and forth between python and nopython mode in the same function. As a result I don't have an ETA, but it is a request we are aware of.
Reacted by Hameer Abbasi, Amos Bird and Michaël Defferrard
@seibert Seems like you're well ahead of schedule. 😃 Thanks a lot. I'll try to juggle around things so I do things that don't need list[list[int]] first. However, sorting (we need a counting/radix argsort) is the biggest bottleneck right now that needs it, the rest I think I can make work without this.
I'm not entirely sure who would use sparse arrays in xarray, but my guess is that all-purpose flexibility of COO would suffice for most analytics purposes.
That said, for data analysis, I think the most useful capability would be support for fill-values other than zero (namely, NaN).
One intriguing usecase for sparse arrays in xarray is to reproduce the capabilities of the "multi-dimensional" databases used in business intelligence (something like Online analytical processing).
Hrm, that's an interesting thought. Do we know people who care about OLAP cubes? Perhaps @Stiivi ? It would be useful to get an introduction to someone that might need an in-memory sparse multi-dimensional data structure and would be willing to try things out and provide feedback from concrete applications.
Fill values are easy to build and I have them planned soon-ish. Most operations can be made to work with it, the only one I can think of that won't is dot/tensordot.
I'm still waiting on numba/numba#2560, it's a blocker for a lot of features. Failing that in 0.39, I'd like to discuss Cython and/or C/C++ wrapping alternatives.
Looks like the decisions was to go with numba, and given the amount of work invested there, this would be unlikely to change in the future? Should this be issue closed then?
Performance would be a significant argument for or against adoption (#331). For users who don't need much extra functionality outside of scipy.sparse (but just want a sparse COO/CSD 2D ndarray class), if this library can be made faster than scipy.sparse than accepting 2 extra dependencies (numba, llvmlite) can likely be acceptable. If the performance is similar or worse, that's going to be more difficult to sell (putting aside whether numba is generally faster than cython or not).
While I'm somewhat of a fan of Rust myself, it seems hard, due to the amount of work invested, as you say. Not to mention, some upcoming features absolutely need JIT, due to an "explosion of types".
If Rust or C++ JIT is a stable thing (better than Numba) I'd like to hear about it.
Activity
mrocklin commentedon Mar 4, 2018
My personal thoughts
The default today seems to be Cython. This would be a fine choice. It seems to be fine for speed and community adoptability. I do think that it imposes a serious barrier to attracting new developers (I'd say 80% of the candidate pool will avoid Cython), and it will force a more complex build-and-deploy process. For example we can no longer just point people to try things on master to get quick feedback. We'll get questions from people on how to install from source on windows, etc..
C/C++ is, I think, a little cleaner than Cython in terms of attracting new developers. I think that C tends to be more often within people's comfort zone.
Numba is great for new developers and maintenance, but has issues in community adoptability (folks aren't accustomed to depending on it, thoughts here). Numba also has issues if we're going to be doing a lot with dynamic data structures and want to use
std::map,std::vectorand friends.Having some familiarity with both Numba and Cython I personally also find the development cycle to be must faster with Numba than with Cython.
Suggestion
If I were doing this work I would stick to Numba until numba became an issue (either due to a need for dynamic data structures or downstream libraries being unwilling to depend on it) and then I would switch to something else. The cost of starting with Numba to me seems to be zero. We don't have to update our release mechanism, we don't have to have special documentation or field extra compilation help requests. The cost to switch to Cython if necessary is likely very small. We'll likely be writing exactly the same code we would write for Cython, just with less stuff around it.
Numba is also likely to be faster without us thinking as much, which I think is useful when experimenting with new algorithms, like in #125
However
However, I'm unlikely to do any of this work in the near future, and the preferences of those doing the work should have precedence.
mrocklin commentedon Mar 4, 2018
cc @rgommers @ogrisel @stefanv @shoyer for thoughts and hopefully critical feedback. In what system would you like to see low-level code in the pydata/sparse library written?
hameerabbasi commentedon Mar 4, 2018
In favor of Cython
DOKfaster is a priority for me for batch assignments (i.e.ndarrayandSparseArrayassignments), this needsstd::unordered_map.elemwisediscussed in Support Everything that XArray Expects #1 will come in useful forCSD. Since the passes in that algorithm are likely to be slow, Numba could be almost twice as slow asstd::vector. Other uses are very likely to pop up sooner or later.In favor of Numba
My Concerns
I guess it comes down to exactly two questions for me: Will we have to switch at some point (which I would like to avoid outright), or will Numba devs be willing to add support for accelerated
dictandlistoperations, and interleaving? If the answers to both these questions are in the favor of Numba, then it's a no-brainer for me.If, in the other hand, the answer to even one of these questions is "no"... Then my vote is for a mixture of Cython and Cython-wrapped C++.
I guess my main worries with Numba come down to the following:
mrocklin commentedon Mar 4, 2018
Some thoughts:
std::mapto be significantly faster than Python's dict. At best I would hope for 2-3x improvements, not 10x.numba.jit. I'd be curious to learn your debugging technique for CythonHowever, mostly this is me trying to convince you not to adopt Cython prematurely. At this point you're doing most of the work and should probably decide.
As a co-maintainer though I would appreciate it if, prior to actually writing algorithmic code in Cython you first to setup the build process, packaging support (we should consider conda-forge), and documentation. I don't expect this to be particularly difficult, I don't think we'll need any external libraries to speak of, but it's best to understand the costs ahead of time and build in solutions in case you leave this project in the future.
mrocklin commentedon Mar 4, 2018
FWIW while I'm probably biased towards numba due to professional association I also have a lot of respect for Cython. As someone who has used both in different projects though (numba for personal things, cython for geopandas) and as someone who maintains a number of projects, the added cost of maintaining a Cython project in terms of person-hours is high, and that cost tends to be focused on fewer maintainers. I think I tend to optimize these days more to reduce and diffuse maintenance costs than most other things.
I personally would probably stop spending as much personal time fielding maintenance questions or build/release issues if we go for Cython. This is something that the community would need to pick up long term.
hameerabbasi commentedon Mar 4, 2018
There is also
std::unordered_map, which is 2x-3x faster thanstd::map, and is the best equivalent todict.std::mapis more of a binary search tree.GDB supports python natively, and Cython has extensions for GDB. It's on the command line, though (I've tried and failed to find a good IDE solution), so it is a bit of a pain compared to your solution. Also, it freezes your interpreter in, so
conda envis a no-no.Interleaving Pure Python/optimized code. Other problems would be calling unoptimized code from optimized.
masterispip install -e(with or without Cython installed)setup.pyto eitherpip install CythonThere are also costs in terms of alienated developers we may have accrued that are used to Numba.
Of course, I understand the costs, and if the consensus is on Cython, then I would build these solutions in, along with docs, right at the very first check-in. I believe properly > quickly.
After your convincing and experience, I'm not too opposed to Numba, but would certainly like to wait for thoughts of others (particularly Scipy devs and whether they would consider it to be a blocker) before committing to it.
My vote is still slightly in favor of Cython, but I'm open to Numba as an option. I don't believe I should make the decision, I would really wait for community consensus.
albop commentedon Mar 4, 2018
I have started watching this project only recently and although I was super happy to finally a sparse multidimensional tensor lib that I could contribute to I'm just an observer here.
Funnily enough I spent some time yesterday trying to implement specialized tensor products for DOK with numba, and found out that dicts are not a recognized type yet. I used ordered list of tuples instead of dicts everywhere, which seems to be supported well but @mrocklin must know the details better than I do.
Here is one argument in favour of numba, which I haven't seen listed above. I have found very useful in other projects the ability to generate code, based on number of dimensions for instance, and jit-compile it to a fast, memory-savvy option. There are several instances, where I found this approach to be much faster than using numpy's generic functions, and I wouldn't know how to perform the same with Cython or C/C++ for that matter as dimension combinations are not known at compile-time.
hameerabbasi commentedon Mar 4, 2018
At this point, if Scipy devs came out and said they would be okay to depend on this project if Numba was used, I'd go for Numba. I just had a look at the docs and they're significantly better than Cython's (which alone makes them worth considering).
It'd be nice to have another contributor, @albop. :-)
albop commentedon Mar 4, 2018
@hameerabbasi : I was considering saying something about me contributing to the project, but wouldn't like to disappoint if it takes me a bit too long to start. I'll try to create some issues first ;-) (with the usecase I have in mind...)
hameerabbasi commentedon Mar 4, 2018
@albop If you have any questions about where something is handled, or how to implement something, don't hesitate to raise an issue and cc me on it. :-)
shoyer commentedon Mar 4, 2018
Numba actually does currently accelerated
listandset. I don't know if acceleratingdictis on their roadmap or not.ogrisel commentedon Mar 4, 2018
C++ is fine as long as the wrapper tool is modern enough to make it easy to package as a wheel (e.g. Cython or pybind11, no boost python please). I am fine with numba as a dependency as well. I can't speak for scipy developers.
hameerabbasi commentedon Mar 4, 2018
They do, it's even been labelled a high priority. xref numba/numba#2096
This makes me much more comfortable using Numba.
rgommers commentedon Mar 4, 2018
I can't speak for all SciPy developers, but if Numba continues to improve and be maintained (not sure of its status now with all the changes at Anaconda?) then at some point we'll likely adopt it. The concerns regarding build dependencies etc. are minor compared to all the C/C++/Fortran we have already. If you want me to start that discussion now on scipy-dev, I'd be happy to do so.
Re debugging: it depends on what you're debugging. If it's an issue with the logic of your code, then Numba is much better (just remove
@jit). If it's a problem with Numba or Cython behavior though, then Cython's debugging support is far better than Numba's. The latter case is probably less common, but not that uncommon - both due to changes in new releases and things that don't work identically to plain Python code in the first place. I'd say that as a contributor I would prefer Numba, and as a library maintainer I'd prefer Cython.We recently got a similar question from Pythran, and I came up with a list of factors for the decision at https://mail.python.org/pipermail/scipy-dev/2018-January/022334.html. The ones I'm not sure about for Numba are:
66 remaining items
seibert commentedon Apr 26, 2018
Some notes on issues raised above:
Being able to nest containers (like list[list[int]]) should be available in Numba 0.39 (scheduled for late June). However, we suspect that will reveal a new bottleneck for some use cases, where the unboxing cost of nested Python lists (when transitioning from Python to nopython mode) becomes prohibitive. For temporary lists, this won't matter.
The architecture support limitations should soon be resolved. By next release, ARMv7 will be working (except some issues that are also present in SciPy linalg functions), ARMv8 is working except for a few NaT conversion issues when working with datatime dtypes, and POWER8 will be solved with the future LLVM 6.0.1 release which contains some required PPC64 bug fixes.
We've had a request from HPAT for a typed dictionary that can be used as a temporary data structure inside algorithms. This is something we want to do for several reasons, but we're currently juggling development priorities between these sort of container features, refactoring our ufunc/gufunc compiler, and supporting the ability to jump back and forth between python and nopython mode in the same function. As a result I don't have an ETA, but it is a request we are aware of.
hameerabbasi commentedon Apr 26, 2018
@seibert Seems like you're well ahead of schedule. 😃 Thanks a lot. I'll try to juggle around things so I do things that don't need
list[list[int]]first. However, sorting (we need a counting/radix argsort) is the biggest bottleneck right now that needs it, the rest I think I can make work without this.shoyer commentedon Apr 26, 2018
I'm not entirely sure who would use sparse arrays in xarray, but my guess is that all-purpose flexibility of COO would suffice for most analytics purposes.
That said, for data analysis, I think the most useful capability would be support for fill-values other than zero (namely, NaN).
One intriguing usecase for sparse arrays in xarray is to reproduce the capabilities of the "multi-dimensional" databases used in business intelligence (something like Online analytical processing).
mrocklin commentedon Apr 27, 2018
Hrm, that's an interesting thought. Do we know people who care about OLAP cubes? Perhaps @Stiivi ? It would be useful to get an introduction to someone that might need an in-memory sparse multi-dimensional data structure and would be willing to try things out and provide feedback from concrete applications.
hameerabbasi commentedon Apr 27, 2018
Fill values are easy to build and I have them planned soon-ish. Most operations can be made to work with it, the only one I can think of that won't is
dot/tensordot.hameerabbasi commentedon Jun 1, 2018
I'm still waiting on numba/numba#2560, it's a blocker for a lot of features. Failing that in 0.39, I'd like to discuss Cython and/or C/C++ wrapping alternatives.
mrocklin commentedon Jun 1, 2018
JoElfner commentedon Jun 7, 2018
@hameerabbasi and @mrocklin It looks like the implementation will be coming in 0.39. It is labeled as ready for review: here
aldanor commentedon Sep 18, 2018
Refcounted lists seemed to have landed in numba indeed. Does anyone know what's the story with
dict?hameerabbasi commentedon Sep 18, 2018
See numba/numba#2096.
JoElfner commentedon Sep 18, 2018
Afaik numba version 1.0 release date is scheduled for december 2018. Dict support is marked as high priority for 1.0.
kiwi0fruit commentedon Mar 4, 2019
In the light of this question Why Julia? Will Python/Numba and Python/Cython lose to Julia? I suggest to go with Numba and actively contribute to it if needed.
rth commentedon Mar 10, 2020
Looks like the decisions was to go with numba, and given the amount of work invested there, this would be unlikely to change in the future? Should this be issue closed then?
Performance would be a significant argument for or against adoption (#331). For users who don't need much extra functionality outside of scipy.sparse (but just want a sparse COO/CSD 2D ndarray class), if this library can be made faster than
scipy.sparsethan accepting 2 extra dependencies (numba, llvmlite) can likely be acceptable. If the performance is similar or worse, that's going to be more difficult to sell (putting aside whether numba is generally faster than cython or not).Wrapping low level libraries is probably not doable now with numba, but it still might be worth following the work done in https://github.com/vbarrielle/sprs in Rust. So far it doesn't have the ND generalized data structures that pydata/sparse has, but some work has been done lately to benchmark against scipy.sparse in sparsemat/sprs#184 (comment) and to improve results.
hameerabbasi commentedon Aug 18, 2020
While I'm somewhat of a fan of Rust myself, it seems hard, due to the amount of work invested, as you say. Not to mention, some upcoming features absolutely need JIT, due to an "explosion of types".
If Rust or C++ JIT is a stable thing (better than Numba) I'd like to hear about it.
stuartarchibald commentedon Sep 15, 2020
Numba has
ctypesandcffisupport, it's possible to get a long way with these (void *!) and the extension API.