Search code examples
halide

Setting Bounds on Vars in Halide Funcs


I have a 3x3 Convolution Function defined like this

conv(x, y) = 0;
conv(x, y) += kernel(r.x, r.y) * in(x + r.x - 1, y + r.y - 1);

Size of the input buffer is 16 x 16
If I want to execute it with padding I can directly do
in = Halide::BoundaryConditions::constant_exterior(in_buffer, 0, 0, 16, 0, 16)
But I have to execute without padding and so I am trying to manually set the bounds on the function like this

conv.bound(x, 1, 14);  
conv.bound(y, 1, 14);

This returns an error message

Error:
Bounds given for convolution in y (from 1 to 14) do not cover required region (from 0 to 15)

What should I do to set bounds on a Var in Func?


Solution

  • I think you need not to manually set the bounds using the *.bound function. Try this one:

    Halide::Func conv("conv"), kernelF("kernel"), in("in");
    Halide::Var x("x"), y("y");
    Halide::RDom r(0, 3, 0, 3,"r");
    
    in = Halide::BoundaryConditions::constant_exterior(in_buffer, 0, 
        0, 16, 0, 16);
    kernelF = Halide::BoundaryConditions::constant_exterior(kernel_buffer, 0, 
        0, 3, 0, 3);
    
    conv(x, y) = 0.0f;
    conv(x, y) += kernelF(r.x, r.y) * in(x + r.x, y + r.y);
    
    //conv.print_loop_nest();
    Halide::Buffer<float_t> outputBuf = conv.realize(14, 14);
    

    Look, we can set the bounds directly in *.realize() arguments, i.e. 14=16-3+1; Also, note that the convolution anchors are at the top-left of kernels.