我有一个像这样定义的3x3卷积函数
conv(x, y) = 0;
conv(x, y) += kernel(r.x, r.y) * in(x + r.x - 1, y + r.y - 1);
输入缓冲区的大小为16 x 16
如果我想用填充执行它,我可以直接执行
in = Halide::BoundaryConditions::constant_exterior(in_buffer, 0, 0, 16, 0, 16)
但我必须在没有填充的情况下执行,因此我尝试手动设置函数的边界,如此
conv.bound(x, 1, 14);
conv.bound(y, 1, 14);
这会返回错误消息
Error:
Bounds given for convolution in y (from 1 to 14) do not cover required region (from 0 to 15)
如何在Func中设置Var的界限?
答案 0 :(得分:1)
我认为您不需要使用* .bound函数手动设置边界。试试这个:
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);
看,我们可以直接在* .realize()参数中设置边界,即14 = 16-3 + 1;另请注意,卷积锚点位于内核的左上角。