EwBinaryMat kernel mishandles broadcasting where `lhs` is broadcast (and not scalar)
#924 opened on 2024/11/29
Repository metrics
- Stars
- (80 個のスター)
- PR merge metrics
- (PR metrics pending)
説明
The current implementation of EwBinaryMat.h for DenseMatrix assumes that the operand that is broadcast is the rhs operand. However, if both operands are matrices there is no canonicalizer or logic elsewhere to guarantee this. Thus, the following script mishandles the broadcasting and returns a wrong result:
lhsMat = [1, 2, 3, 4, 5, 6](2,); // 2x3
rhsMat = [10, 20, 30](1,); // 1x3
print(rhsMat + lhsMat); // 1x3 matrix instead of 2x3
print(lhsMat + rhsMat); // correct result
Since rhs is assumed to be broadcast and the kernel only checks whether broadcasting is generally allowed, the inferred dimension for the result is wrong and the kernel broadcasts the wrong matrix (see lines 72-97).
Dimensions are not always known at compile time, so instead of/complementary to a canonicalizer pass that shifts the broadcast matrix to be the rhs operand, a new utility function to handle broadcasting in general would be very helpful and could be reused by other kernels that support similar broadcasting I believe. E.g. it could be run at the beginning of the kernel call and verify whether the dimensions are valid, then swap lhs and rhs if necessary.