How do I keep passing the output of a Halide pipeline back into the pipeline until some condition is met?

174 views Asked by At

I have a Halide pipeline which takes in an image, and applies some filters to it. It works fine for a single pass. I pass in an image, and I get the processed image as the output. What I would like to do is implement multiple passes i.e., I want to keep passing the output image back into the pipeline for a number of steps until some condition on the image is met. How do I do this Halide? The only other way I could think of doing it was having an external C++ method that can invoke the pipeline in a loop. I was hoping if this is possible natively in Halide.

The code looks something like this. I want to consume output as input in a loop until some condition is met.

int main(int argc, char **argv) {
  Buffer<uint8_t> input = Tools::load_and_convert_image("img.png");
  int W = input.width();
  int H = input.height();
  Buffer<uint8_t> output(W, H);
  // after some Funcs. res is last Func of the pipeline
  res.realize(output);
  Tools::convert_and_save_image(output, "out.png");
  return 0;
}
1

There are 1 answers

0
jrk On BEST ANSWER

You can run the pipeline a static number of times, by unrolling it into a larger pipeline. If what you are computing is expressible as a single stage, you can run it a dynamic but unconditional number of times by using an RDom to iterate over successive applications. However, you can not express any loops with a dynamic termination condition (what you might think of as a while loop). That you have to do in the calling C++ code.

You've hit on quite an insightful question. This is a conscious constraint of the language: it's the difference between loop bounds being decidable, and becoming Turing complete. Future version of the language may extend this in targeted ways, but it's currently quite fundamental to how the compiler can infer all the bounds in your program.