I'm working on some high-dimensional integer-based data, and for memory reasons we have to use int8s for everything. The issue I'm running into is that I need to do matrix multiplications with this int8 data, but if the result goes higher than 127, I need it to just return 127 instead of whatever the overflow is.
For example:
import numpy as np
a = np.random.choice([0,1], size=[128,2000]).astype(np.int8)
b = np.random.randint(0,128, size=[2000,2000]).astype(np.int8)
c = np.matmul(a, b)
# c returns values between -128 and 127, instead of the desired 0 to 127
To clarify, I'm not just looking for the extra space unsigned ints would afford - values in b can sometimes be negative. (I just made b all positive to further illustrate my point.)
Is there an algorithm or Numpy trick that would allow me to cap these operations instead of having them overflow? I've perused the Numpy documentation and asked some friends of mine in the CS department, but I've yet to find any leads on this.
Lastly, I know that I could do some of this in C with bit-twiddling, but the rest of our project is unavoidably in Python with little chance of expansion (and given Python, I'd prefer to use Numpy for the matrix multiplication if at all possible).
Thanks, all!
a? This does not work as written. Otherwise thanks for the nice verifiable example