Live data from Hacker News

Hope – A Python JIT for astrophysical computations

github.com

11–20 of 25 posts

Re: Hope – A Python JIT for astrophysical computations

#11
post #6
post #2

Assuming this is Python 2.x (doesn't seem to be specified in the benchmark results), then the use of "range" is problematic (should be "xrange").

Not really. "range" returns a list, "xrange" returns an iterator. I assume that the list is much easier to reason about in a JIT. In python3, range is a type of its own and certainly more useful for in a JIT. https://docs.python.org/3/library/stdtypes.html#typesseq-ran... https://docs.python.org/2/library/functions.html#range

How is allocating and populating a Python list order to do a simple for loop in a tight inner loop not problematic? Neither CPython nor PyPy can optimize that out.

As I said, this is assuming we're on Python 2.x.

Re: Hope – A Python JIT for astrophysical computations

#13
post #11
post #6

Earlier quoted context omitted.

Not really. "range" returns a list, "xrange" returns an iterator. I assume that the list is much easier to reason about in a JIT. In python3, range is a type of its own and certainly more useful for in a JIT. https://docs.python.org/3/library/stdtypes.html#typesseq-ran... https://docs.python.org/2/library/functions.html#range

How is allocating and populating a Python list order to do a simple for loop in a tight inner loop not problematic? Neither CPython nor PyPy can optimize that out. As I said, this is assuming we're on Python 2.x.

PyPy optimizes that out, you have a thing that looks exactly like a list, but is implemented differently under the hood

Re: Hope – A Python JIT for astrophysical computations

#15
I found it interesting to note that one of their use case is numerical integration. As of now Hope does method by method JIT. Numerical integration is one of those things where just "method by method" JIT leaves substantial room for improvement. It is the poster child used to showcase the strength of Stalin, the lisp compiler that substantially outperforms numerical integrator written in C. The main feature that Stalin uses to achieve that impressive performance is inlining of the function that needs to be integrated, right into the integration code. Such inlining is hard to do when code reside in separate compilation unit, separate functions and loaded dynamically, especially when the caller is only aware of a function pointer and not much else. The later the integration routine is compiled, more chance would there be for the compiler to know more about the function pointer, and JIT could potentially help a lot here.

It is indeed possible to do these inlining in C++ using its template machinery but it seems Hope does not to this yet. I am really curious about what Hope does and what plans do they have for the future. Inlining does give huge speedup for this application, more so if one is performing multidimensional numeric integration.

Another thing that I was curious about was how does Hope optimize away the dynamic semantics of Python. If one where to isolate one aspect of Python that makes it ridiculously hard to speed it up, it is its dynamic nature. It seems that Hope removes the dynamic aspect by analyzing the AST of the Python code. Since Python was never intended to be type inferred, it surely poses a substantial obstacle. I wonder whether Hope devs intend to add the Cython functionality of selectively removing dynamic nature of specific functions via annotations. Given the difficulty of automatic static typing of a piece of Python code, any little help afforded to the compiler should go a long way.

The other thing that I am really curious about is what does Hope do about Numpy vector expressions. Cython for example calls back into Numpy, so there is not much speed benefit to be had. In fact if this is being done in a loop, this hurts performance because of the overhead of the call. So if one really wants performance one needs to write the low level indexing code in the Cython syntax. I think it is a matter of personal taste, but if I were to write low-level C,C++; I would rather write it in C,C++ itself. The latter has the additional benefit of mature toolchains that target C, C++.

Finally, a number of tools aimed at similar use cases have been mentioned in the post and in the comments. I will add a shout out for copperhead. It isolates a subset of Python over which one can define a ML like semantics which is then used to aid compilation and parallelization, all hiding behind the familiar python syntax.

Re: Hope – A Python JIT for astrophysical computations

#16
post #7

This is crazy! It actually generates C++ code on the fly by translating AST nodes one to one https://github.com/cosmo-ethz/hope/blob/master/hope/_generat...

This precisely what I would a physicist expect to do, Cern has >16 million lines of C++ and you can be sure that the analysis code written by non-programmers barely works and not nescessarily uses sane programming principles.

Re: Hope – A Python JIT for astrophysical computations

#17
post #3

Does anybody know how this compares performance-wise to Numba ( http://numba.pydata.org/ ) which seems like a pretty similar effort? Numba translates to LLVM bytecode rather than C++.

Good question. When I saw the @jit decorator, I thought it was a numba clone.

Re: Hope – A Python JIT for astrophysical computations

#18
post #15

I found it interesting to note that one of their use case is numerical integration. As of now Hope does method by method JIT. Numerical integration is one of those things where just "method by method" JIT leaves substantial room for improvement. It is the poster child used to showcase the strength of Stalin, the lisp compiler that substantially outperforms numerical integrator written in C. The main feature that Stal…

Just a note - if this use case is important, I've observed that Julia will often properly inline numerical integrals - even some fairly tricky ones. But you do generally have to make sure your loops are striding the quadrature in the memory correct order - Julia doesn't seem to reorder them.

I wonder whether Hope devs intend to add the Cython functionality of selectively removing dynamic nature of specific functions via annotations.

You should definitely look at Julia.

Re: Hope – A Python JIT for astrophysical computations

#19
post #7

This is crazy! It actually generates C++ code on the fly by translating AST nodes one to one https://github.com/cosmo-ethz/hope/blob/master/hope/_generat...

It's basically the same strategy rperl uses, compiling a restricted and typed subset of perl5 to C++11 on the fly, via Inline::CPP http://rperl.org

I wouldn't call that JIT, because the term JIT is normally used for direct compilation to machine code, and all those other compile-to-c|c++-on-the-fly languages don't call it JIT neither.

Re: Hope – A Python JIT for astrophysical computations

#20
post #15

I found it interesting to note that one of their use case is numerical integration. As of now Hope does method by method JIT. Numerical integration is one of those things where just "method by method" JIT leaves substantial room for improvement. It is the poster child used to showcase the strength of Stalin, the lisp compiler that substantially outperforms numerical integrator written in C. The main feature that Stal…

Just a note - if this use case is important, I've observed that Julia will often properly inline numerical integrals - even some fairly tricky ones. But you do generally have to make sure your loops are striding the quadrature in the memory correct order - Julia doesn't seem to reorder them. I wonder whether Hope devs intend to add the Cython functionality of selectively removing dynamic nature of specific functions…

[deleted]
Post reply on HN