tensorpack icon indicating copy to clipboard operation
tensorpack copied to clipboard

Using bitwise convolutions with negative weights?

Open the-bobo opened this issue 10 years ago • 1 comments

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:

  1. Add an additional sign bit to all M-bit fixed point integers xi ∈ x and all K-bit fixed point integers yi ∈ y.
  2. 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.
  3. Let bitwise_and(m, k) = and(cm(x), ck(y)), ∀(m, k), ignoring the sign bits.
  4. Let bitwise_sign = xor(xisigned bit, yisigned bit). This gives us the sign of the product of xi and yi
  5. ∀ 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).
  6. 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.
  7. 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) ].
  8. Use the negative and positive accumulations in 7 to get the signed contribution to the dot product.

the-bobo avatar Oct 17 '16 20:10 the-bobo

Reopen #27 .Closing this.

ppwwyyxx avatar Oct 20 '16 05:10 ppwwyyxx