1

I have three Halide functions which have the following output dimensions:
40 x 40 x 64
40 x 40 x 128
40 x 40 x 64

I want to combine them into a single function so that I get a function handle for later use. So for here , the resulting function should have a dimension of

40 x 40 x 256

I am using Halide::select but it results in 4 dimensions

concat(x,y,z,c)=Halide::select(c == 0, func_1(x, y, z), c == 1, func_2(x, y, z), func_3(x, y, z));

Is there any way to produce a merged 3D function?

Arnab Mitra
  • 127
  • 1
  • 6

2 Answers2

1

You can use a Tuple. It's a little complicated by the different size of the 3rd dimension since the members of a Tuple have to be the same size. This complication exists for your 4d solution as well.

result(x, y, z) = Tuple
    ( func_1(x, y, z)
    , func_2(x, y, z * 2 + 0) // Even z
    , func_2(x, y, z * 2 + 1) // Odd z
    , func_3(x, y, z)
    );

If you keep the 4d solution, add unroll(c) to the schedule and the 3 (now 4) functions will be evaluated sequentially inside the innermost loop.

Rather than concatenating the three functions in the third dimension, adding another dimension or using a Tuple is the better way to go, IMHO.

Edit: Besides unroll(c), you'll need reorder(c,x,y,z) to change the loop order.

Khouri Giordano
  • 796
  • 3
  • 9
1

You may want to return a Pipeline object instead of a Func. Pipelines compile to functions with multiple output parameters, which can be buffers of different shapes.

If you do want a single Func, you want something like:

concat(x, y, z) = 
  Halide::select(z < 64, func_1(x, y, clamp(z, 0, 63)), 
                 z < 192, func_2(x, y, clamp(z - 64, 0, 127)), 
                 func_3(x, y, clamp(z - 192, 0, 63)));
Andrew Adams
  • 1,396
  • 7
  • 3