-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Generator iterator fails from within NPM function if created outside the NPM function #3061
Comments
Thanks for the report. This isn't yet supported. Also, of concern is that it segfaults for me.
|
@stuartarchibald, has there been and update on this or is there a workaround so a generator can be passed to a njit function? |
@Jul3k just from memory, I'm pretty sure this still won't work. |
@stuartarchibald this explains why my code also fails with
|
It's because Numba doesn't know about generator objects, it has no way to translate them from python into a native representation. Even if you spelled it out Numba wouldn't know what to do with it. Is there a specific need for this? Or is it just convenient? |
Well I am generating pulse sequences for stepper motors on a Raspberry Pi. Those pulse sequences are very long (>200.000) so I used generators to calculate chunks of 20 ms sequences with around 2000 pulses and write them to a buffer. The first type of generators calculate the pulse times and directions for different types of movements. The second type of generator calculates the delays and breaks them into chunks. Not being able to pass a generator means that I have to write a implementation of the second type of generator for every first type of generator. It would say passing a generator makes sense in cases where a generator acts on another group of generators. I also found a unsolved stackoverflow question on that topic generator argument in numba |
I see, thanks. This would be useful to have working for the above. As an interim, does writing factory functions help? i.e. generate the generator? |
I am not quite sure what you mean by a factory function. Can you please explain how that would look? As generators work when they are created inside the function this worked for me as a workaround:
|
Do you mean something like the following?
This code fails with: |
I was thinking along the lines of basically creating specialisations via some "factory" to avoid some issues/unimplemented things in Numba. I had a play about with this and I've found at least two new issues with creating escaping functions from closures, but no way better than the workaround you describe, so I think that's the best option for now. |
The workaround is fine for me. I just want to point out that for other use cases, e.g. when a generator should be passed to two subsequent generators acting on it, this will not work, as the generator is created inside the generator and it's state cannot be passed to the subsequent one. |
Feature request (but possibly a bug?)
Note: I'm using numba 0.38.0 in Python 2.7.12
Creating the generator iterator from a generator within the nopython function, then calling it within that function, works:
But creating the generator iterator from the generator outside the nopython function that calls it fails:
With error message:
this use case would be helpful for being able to do costly (CPU and/or memory) initialization within the function using arguments that are difficult for numba to handle, and I think it makes the code quite clean.
My specific use case is that I want to initialize a fixed number of nopython-mode generators where the user can provide these to my function (note that each generator requires different arguments for its initialization, so their arguments have to be handled outside of nopython code). These generators are then handed to my nopython function that just calls
next(gen0)
,next(gen1)
, etc.The best alternative I see would be to use a factory function to define a nopython mode function after the initialization is performed in "regular" Python that then bakes-in the (potentially large) results of the initialization at the time it is compiled. While this works, it would be nice to be able to achieve this the more "straightforward" way.
The text was updated successfully, but these errors were encountered: