I’m looking for a fast operation than can separate the positive and negative values of a tensor X to two tensors of the same size.
One way to do this is using maximum and minimum like below:
In the above solution, each element of X gets compared to 0 twice. But really the comparison only needs to happen once, because if you know X < 0 is true then you already know X > 0 is False.
So you can cut the computation in half.
If we call that operation sieve, then it would look like this: pos, neg = torch.sieve(X, condition=(X > 0))
Is there an operation in torch that can achieve that?
However, I’m not familiar with the implementation details/potential performance tradeoffs, so you might want to check the final speed of whatever approach you go with (e.g., just because one approach only does a single read of data/comparison doesn’t mean it is guaranteed to be faster).
Good catch, in that case I’m not sure there is a clear alternative other than computing a mask and then reusing it for indexing (which would require more reads than the min/max approach anyway).
Is this a performance bottleneck in your application?