pnumpy icon indicating copy to clipboard operation
pnumpy copied to clipboard

numpy ufunc reduce should expect built in upcasting (remove setbufsize logic)

Open tdimitri opened this issue 5 years ago • 0 comments

When an .sum() operation is performed (which is a popular and important operation) on an int16 array it appears that numpy

  1. upcasts int16 to int32 based on setbufsize
  2. then sets the output value to 0
  3. then the reduce routine adds up all the int32 and produced a result
  4. another upcast from int16 to int32 occurs based on setbufsize
  5. the previous output value is expected as the first input to add up all the numbers

I presume this is done due to either handle overflow, or round off error in sum (when it is a float32).

I recommend that this change because a sum routine can internally upcast and return the result of the upcast. For instance int16 can be upcast to int64 (not sure why it is int32 on windows) at no cost since the int64 is held in registers.

Further the signature of the reduce for sum is coupled to the signature for add even though they should be different. For example the reduce signature of add on int16 is really one input: dtype: int16 and outputs dtype:int64.

tdimitri avatar Oct 01 '20 02:10 tdimitri