Using bitwise convolutions with negative weights?
Could not reopen the issue, please see here for more context: https://github.com/ppwwyyxx/tensorpack/issues/27#issuecomment-249977898
The sign-vs-unsign problem is more relevant in FPGA. But as we are only doing summation, unsign numbers should be fine.
How do you implement this bitwise dot product kernel (equation 3 in section 2.1, DoReFa v2 paper) for negative weights?
The quantizek function defined in Section 2.2 as Equation 5 outputs a number ro ∈ [0, 1]. The affine transform on Fwk(ri) in Equation 9 takes the output of a quantizek function and multiplies by 2 and subtracts 1: Fwk(ri) = 2 * quantizek(stuff) - 1.
Thus Fwk(ri) ∈ [-1, 1].
However, the procedure you define in Equation 3 only works for unsigned values. If some values xi in the sequence x or yi in the sequence y are negative, then their contribution to the dot product is a subtraction, not an addition, so the simple bitcount(and()) operation no longer suffices.
How did you change the bitwise dot product procedure to account for negative weights?
One possibility:
- Add an additional sign bit to all M-bit fixed point integers xi ∈ x and all K-bit fixed point integers yi ∈ y.
- This bit is 1 if the number xi is negative, and 0 if xi is positive (likewise for the yi), but does not count as a place-value bit for multiplication.
- Let bitwise_and(m, k) = and(cm(x), ck(y)), ∀(m, k), ignoring the sign bits.
- Let bitwise_sign = xor(xisigned bit, yisigned bit). This gives us the sign of the product of xi and yi
- ∀ bitwise_and(m, k) ∀(m, k), note that bitwise_sign(xi, yi) is a vector giving the sign for each element in bitwise_and(m, k).
- For each pair of vectors ( bitwise_and(m, k), bitwise_sign(xi, yi) ) ∀(m,k), drop all members of bitwise_and(m, k) and their corresponding signs in bitwise_sign(xi, yi) where bitwise_and(m, k) == 0. This leaves us with the cases where the bitwise multiplication produced a 1, along with their signs.
- For each pair of vectors ( bitwise_and(m, k), bitwise_sign(xi, yi) ) ∀(m,k), compute bitcount[ bitwise_sign(xi, yi) ] to get the total number of negatives for the (m*k) place-value. The total number of positives is given by len(bitwise_sign(xi, yi)) - bitcount[ bitwise_sign(xi, yi) ].
- Use the negative and positive accumulations in 7 to get the signed contribution to the dot product.
Reopen #27 .Closing this.