Halide icon indicating copy to clipboard operation
Halide copied to clipboard

Compute a pure function at the RVar of a reduction function

Open amitaga opened this issue 9 years ago • 6 comments

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& quantizedWeights, Func& input, int inputDim, const Var& outVar, bool schedule = true) { int lookupWidth = sizeof(unsigned char) * 8; Var lookupIdx("LookupIdx"); RDom k(0, inputDim / lookupWidth); Expr val1 = 0.0f; for (int i = 0; i < lookupWidth; ++i) { Expr currentInput = input(lookupWidth * k + i); Expr bitVal = (lookupIdx >> (lookupWidth - i - 1)) & 1; val1 += select(bitVal == 0, -currentInput, currentInput); } Func lookupTable("lookupTable"); lookupTable(lookupIdx) = val1; lookupTable.bound(lookupIdx, 0, 1 << lookupWidth);

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;

}

amitaga avatar Aug 30 '17 00:08 amitaga

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& quantizedWeights, const Buffer& input, Buffer& output) { const int lookupWidth = 8; int inDim = input.width(); int outDim = output.width();

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];
        }
}

}

amitaga avatar Aug 30 '17 15:08 amitaga

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.

abadams avatar Aug 31 '17 21:08 abadams

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& quantizedWeights, Func& input, int inputDim, const Var& outVar, bool schedule = true) { int lookupWidth = sizeof(unsigned char) * 8; Var lookupIdx("LookupIdx"), inputVar_8("inputVar_8"); Expr val1 = 0.0f; for (int i = 0; i < lookupWidth; ++i) { Expr currentInput = input(lookupWidth * inputVar_8 + i); Expr bitVal = (lookupIdx >> (lookupWidth - i - 1)) & 1; val1 += select(bitVal == 0, -currentInput, currentInput); } Func lookupTable("lookupTable"); lookupTable(lookupIdx, inputVar_8) = val1; lookupTable.bound(lookupIdx, 0, 1 << lookupWidth); lookupTable.bound(inputVar_8, 0, inputDim / lookupWidth);

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;

}

amitaga avatar Aug 31 '17 22:08 amitaga

Try removing:

lookupTable.bound(inputVar_8, 0, inputDim / lookupWidth);

It's telling Halide to always compute the full extent of that dimension

abadams avatar Aug 31 '17 22:08 abadams

That was it. Thanks.

For future, can you advise how could I have figured this myself?

amitaga avatar Aug 31 '17 23:08 amitaga

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.

abadams avatar Aug 31 '17 23:08 abadams