Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does having inconsistent input and output dimensions in a Halide generator significantly slow down performance? #8218

Open
mhua97 opened this issue May 10, 2024 · 4 comments

Comments

@mhua97
Copy link

mhua97 commented May 10, 2024

`class Example1Generator : public Generator
{
public:
Input<Buffer<uint8_t, 2>> input{"input"};
Output<Buffer<uint8_t, 3>> output{"output"};

void generate()
{
    Var x("x"), y("y"), c("c");

    Func f1;
    f1(x, y) = input(x, y) + 10;
    Func f2;
    f2(x, y) = input(x, y) + 20;
    Func f3;
    f3(x, y) = input(x, y) + 30;
    Func f4;
    f4(x, y) = input(x, y) + 40;
    Func f5;
    f5(x, y) = input(x, y) + 50;

    output(c, x, y) = mux(c, {f1(x, y), f2(x, y), f3(x, y), f4(x, y), f5(x, y)});

    if (using_autoscheduler())
    {
        input.set_estimates({{1024, 16384}, {1024, 8192}});
        output.set_estimates({{0, 10}, {1024, 16384}, {1024, 8192}});
    }
}

};

class Example2Generator : public Generator
{
public:
Input<Buffer<uint8_t, 2>> input{"input"};
Output<Buffer<uint8_t, 2>> output{"output"};

void generate()
{
    Var x("x"), y("y");

    Func f1;
    f1(x, y) = input(x, y) + 10;

    output(x, y) = f1(x, y);

    if (using_autoscheduler())
    {
        input.set_estimates({{1024, 16384}, {1024, 8192}});
        output.set_estimates({{1024, 16384}, {1024, 8192}});
    }
}

};
`
Consider this: calling Example2Generator five times takes several times longer than calling Example1Generator once. Is this because the input to output dimensionality increased? What if my requirement necessitates going from low-dimensional input to high-dimensional output?

@abadams
Copy link
Member

abadams commented May 10, 2024

It's nothing to do with dimensionality. It's because calling Example2Generator five times reads the input from memory five times, and calling Example1Generator once reads the input from memory once (and then does five things with it). You'd see the same effect in C.

@mhua97
Copy link
Author

mhua97 commented May 10, 2024

Calling Example2Generator once takes about 3ms, but calling Example1Generator once takes 160ms. This is the confusing part for me.

@abadams
Copy link
Member

abadams commented May 10, 2024

In Example1Generator the innermost dimension in memory of the output buffer is 'c'. That's a small dimension - probably length five in practice given that you're muxing together five things. That means any use of SIMD (vectorization) is going to need to do a strided store to memory instead of a dense vector store, which is very bad for performance.

So if Example1Generator is slow it's probably due to the memory layout of your output buffer. It looks like you're using an autoscheduler, but this is not something an autoscheduler can help with because the memory layout of the output buffer is controlled by the caller.

A fast version of Example1Generator would look like:

    Var x("x"), y("y"), c("c");

    Func f1;
    f1(x, y) = input(x, y) + 10;
    Func f2;
    f2(x, y) = input(x, y) + 20;
    Func f3;
    f3(x, y) = input(x, y) + 30;
    Func f4;
    f4(x, y) = input(x, y) + 40;
    Func f5;
    f5(x, y) = input(x, y) + 50;

    output(x, y, c) = mux(c, {f1(x, y), f2(x, y), f3(x, y), f4(x, y), f5(x, y)});

    output.reorder(c, x, y).bound(c, 0, 5).unroll(c).vectorize(x, 16).parallel(y, 8);

Note that 'reorder' just reorders the loops, not the memory layout.

@mhua97
Copy link
Author

mhua97 commented May 10, 2024

Thank you very much for your suggestion, it's very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants