micropython-ulab icon indicating copy to clipboard operation
micropython-ulab copied to clipboard

[FEATURE REQUEST] Implement `unpackbits()`

Open page200 opened this issue 1 year ago • 5 comments

Are there plans to implement unpackbits()?

And what are good practices for getting the same result fast while unpackbits() is not implemented?

I'd like to use the resulting bits as Boolean indices into a constant integer array X or do np.nditer(np.nonzero(X)) or whatever is fastest for achieving the same effect.

page200 avatar Nov 06 '24 16:11 page200

Here is unpack_bits() in numpy/numpy/_core/src/multiarray/compiled_base.c

page200 avatar Nov 06 '24 18:11 page200

Ha! I actually thought of this a while ago, but then I didn't see any immediate use, so I didn't pursue the idea. If you think it's useful, we could definitely add this.

v923z avatar Nov 06 '24 18:11 v923z

It would be useful.

Maybe we can even forgo the overhead of the parameter checks that NumPy does, or at least do "memoization" so that those checks are performed only once instead of in each call?

page200 avatar Nov 06 '24 19:11 page200

Maybe we can even forgo the overhead of the parameter checks that NumPy does, or at least do "memoization" so that those checks are performed only once instead of in each call?

That would break numpy-compatibility. If you're concerned about the keyword arguments, then we should do this in utils and not as a standard numpy function. We could simply define a class, and then unpack_bits could be a method of that class. That would save the time of the parameter checks.

v923z avatar Nov 06 '24 19:11 v923z

That would break numpy-compatibility. If you're concerned about the keyword arguments, then we should do this in utils and not as a standard numpy function.

That would save the time of the parameter checks.

Sounds good. Such "lean" versions of functions are particularly welcome in ulab for several reasons:

  • arrays are on average smaller in ulab than in NumPy, so parameter checks occupy a larger percentage of runtime,
  • parameter checks are on average slower on microprocessors, so forgoing them saves quite some time,
  • many other operations are also slower on microprocessors, so reducing the runtime makes the overall runtime more acceptable.

unpackbits isn't urgent for me at the moment.

page200 avatar Nov 22 '24 13:11 page200