Compute a pure function at the RVar of a reduction function
The following piece of code is meant to illustrate what I am trying to accomplish; it currently fails with an error "reduction domain referenced in pure function". In short, I need to compute a small lookup table in each iteration of a reduction variable and the lookup table references a piece of memory based on the reduction variable value itself. However since the lookup table itself is a pure function, it does not allow me to use the reduction variable. Any ideas how I can accomplish this?
Func LookupTableQuantizedMatrixVectorMultiply(const std::array<T, NumBits>& quantizationMeans, const Buffer
Func output("MatrixVectorMultiplyOutput");
output(outVar) = 0.0f;
// Throw an error if inputDim is not a multiple of lookupWidth
Expr val2 = 0.0f;
for (int i = 0; i < NumBits; ++i)
{
Expr currentLookupIdx = quantizedWeights(outVar, i, k);
val2 += lookupTable(currentLookupIdx) * quantizationMeans[i];
}
output(outVar) += val2;
if (schedule)
{
lookupTable.compute_at(output, k);
lookupTable.vectorize(lookupIdx, vectorizationWidth);
output.vectorize(outVar, vectorizationWidth);
output.update().reorder(outVar, k);
output.update().vectorize(outVar, vectorizationWidth);
}
return output;
}
Here's the C code for what I am trying to accomplish. Any ideas will be very helpful:
template <typename T, int NumBits>
void ManualLookupTableQuantizedMatrixVectorMultiply(const std::array<T, NumBits>& quantizationMeans, const Buffer
static T* lookupBuffer = new T[(1 << lookupWidth)];
for (int i = 0; i < outDim; ++i)
output(i) = 0.0f;
// 'i' is the reduction variable
for (int i = 0; i < inDim / lookupWidth; ++i)
{
for (int j = 0; j < (1 << lookupWidth); ++j)
lookupBuffer[j] = 0;
for (int k = 0; k < lookupWidth; ++k)
{
for (int j = 0; j < (1 << lookupWidth); ++j)
{
auto currentInput = input(lookupWidth * i + k);
auto bitVal = (j >> (lookupWidth - k - 1)) & 1;
lookupBuffer[j] += (bitVal == 0) ? -currentInput : currentInput;
}
}
for (int j = 0; j < outDim / vectorizationWidth; ++j)
for (int k = 0; k < vectorizationWidth; ++k)
{
auto flatIdx = (j * vectorizationWidth) + k;
for (int b = 0; b < NumBits; ++b)
output(flatIdx) += lookupBuffer[quantizedWeights(flatIdx, b, i)] * quantizationMeans[b];
}
}
}
I think you want your lookup table to be 2D, indexed by the existing variable, and also by a pure variable representing the axis you're reducing over. Let's call this new pure Variable v. You'd change the definition of the lookup table to use v instead of k, and then when you sample the lookup table, you'd say lookupTable(currentLookupIdx, k) to bind that pure variable to the current point in the reduction domain.
Thanks Andrew.
I have tried that but it appears to result in the entire lookup table (2D) to be recomputed during each iteration of the reduction. What I need is some way to tell Halide that the pure var and the reduction var are the same and that it should not redundantly compute the entire 2D lookup table during each iteration of the reduction (but just the required row). My assumption that the entire 2D lookup table is computed each time inside the reduction, is based on the loop nest dump and that this version runs ~8 times slower that the one where I compute the lookup table at root. The code below basically implements what you suggested but runs 8x slower compared to the compute_root schedule for the lookup table.
template <typename T, int NumBits, bool parallel = true>
Func LookupTableQuantizedMatrixVectorMultiply(const std::array<T, NumBits>& quantizationMeans, const Buffer
Func output("MatrixVectorMultiplyOutput");
output(outVar) = 0.0f;
// Throw an error if inputDim is not a multiple of lookupWidth
RDom k(0, inputDim / lookupWidth);
Expr val2 = 0.0f;
for (int i = 0; i < NumBits; ++i)
{
Expr currentLookupIdx = quantizedWeights(outVar, i, k);
val2 += lookupTable(currentLookupIdx, k) * quantizationMeans[i];
}
output(outVar) += val2;
if (schedule)
{
lookupTable.compute_at(output, k).vectorize(lookupIdx, vectorizationWidth);
lookupTable.store_root();
//lookupTable.compute_root();
output.vectorize(outVar, vectorizationWidth);
output.update().reorder(outVar, k);
output.update().vectorize(outVar, vectorizationWidth);
}
return output;
}
Try removing:
lookupTable.bound(inputVar_8, 0, inputDim / lookupWidth);
It's telling Halide to always compute the full extent of that dimension
That was it. Thanks.
For future, can you advise how could I have figured this myself?
I guess there are two rules of thumb that apply here:
-
Program as if everything was compute_root, and worry about fusion for locality later. This would have forced you to make the lut 2D with a pair of pure vars.
-
Only use Func::bound if you need an extent be a constant for unrolling or for promoting storage to the stack. It basically turns off Halide's bounds inference except as a safety check, and fixes the domain over which something is computed, regardless of where it is in a loop nest.