[Numpy-discussion] ANN: numexpr 2.4.3 released
mistersheik at gmail.com
Mon Apr 27 20:29:53 EDT 2015
On Mon, Apr 27, 2015 at 7:42 PM, Nathaniel Smith <njs at pobox.com> wrote:
> On Mon, Apr 27, 2015 at 4:23 PM, Neil Girdhar <mistersheik at gmail.com>
> > I was told that numba did similar ast parsing, but maybe that's not true.
> > Regarding the ast, I don't know about reliability, but take a look at
> > get_ast in pyautodiff:
> > It looks up the __file__ attribute and passes that through compile to get
> > the ast. Of course that won't work when you don't have source code (a
> > only module, or when else?)
> > Since I'm looking into this kind of solution for the future of my code,
> > curious if you think that's too unreliable for some reason?
> I'd certainly hesitate to rely on it for anything I cared about or
> would be used by a lot of people... it's just intrinsically pretty
> hacky. No guarantee that the source code you find via __file__ will
> match what was used to compile the function, doesn't work when working
> interactively or from the ipython notebook, etc. Or else you have to
> trust a decompiler, which is a pretty serious complex chunk of code
> just to avoid typing quote marks.
Those are all good points. However, it's more than just typing quote
marks. The code might have non-numpy things mixed in. It might have
context managers and function calls and so on. More comments below.
> > From a
> > usability standpoint, I do think that's better than feeding in strings,
> > which:
> > * are not syntax highlighted, and
> > * require porting code from regular numpy expressions to numexpr strings
> > (applying a decorator is so much easier).
> Yes, but then you have to write a program that knows how to port code
> from numpy expressions to numexpr strings :-). numexpr only knows a
> tiny restricted subset of Python...
> The general approach I'd take to solve these kinds of problems would
> be similar to that used by Theano or dask -- use regular python source
> code that generates an expression graph in memory. E.g. this could
> look like
> def do_stuff(arr1, arr2):
> arr1 = deferred(arr1)
> arr2 = deferred(arr2)
> arr3 = np.sum(arr1 + (arr2 ** 2))
> return force(arr3 / np.sum(arr3))
Right, there are three basic approaches: string processing, AST
processing, and compile-time expression graphs.
The big advantage to AST processing over the other two is that you can
write and test your code as regular numpy code along with regular tests.
Then, with the application of a decorator, you get the speedup you're
looking for. The problem with porting the numpy code to numexpr strings or
Theano-like expression-graphs is that porting can introduce bugs, and even
if you're careful, every time you make a change to the numpy version of the
code, you have port it again.
Also, I personally want to do more than just AST transformations of the
numpy code. For example, I have some methods that call super. The super
calls can be collapsed since the mro is known at compile time.
> Nathaniel J. Smith -- http://vorpus.org
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion