Halide Jit 编译

Halide Jit compilation

我正在尝试将我的卤化物程序编译为 jit,以便稍后在不同图像的代码中多次使用它。但是我想我做错了什么,有人可以纠正我吗? 首先我创建卤化物函数 运行:

void m_gammaFunctionTMOGenerate()
{
    Halide::ImageParam img(Halide::type_of<float>(), 3);
    img.set_stride(0, 4);
    img.set_stride(2, 1);
    Halide::Var x, y, c;
    Halide::Param<float> key, sat, clampMax, clampMin;
    Halide::Param<bool> cS;
    Halide::Func gamma;
    // algorytm
    //img.width() , img.height();
    if (cS.get())
    {
        float k1 = 1.6774;
        float k2 = 0.9925;
        sat.set((1 + k1) * pow(key.get(), k2) / (1 + k1 * pow(key.get(), k2)));
    }
    Halide::Expr luminance = img(x, y, 0) * 0.072186f + img(x, y, 1) * 0.715158f + img(x, y, 2) *  0.212656f;
    Halide::Expr ldr_lum = (luminance - clampMin) / (clampMax - clampMin);
    Halide::clamp(ldr_lum, 0.f, 1.f);
    ldr_lum = Halide::pow(ldr_lum, key);
    Halide::Expr imLum = img(x, y, c) / luminance;
    imLum = Halide::pow(imLum, sat) * ldr_lum;
    Halide::clamp(imLum, 0.f, 1.f);
    gamma(x, y, c) = imLum;
    // rozkład
    gamma.vectorize(x, 16).parallel(y);

    // kompilacja
    auto & obuff = gamma.output_buffer();
    obuff.set_stride(0, 4);
    obuff.set_stride(2, 1);
    obuff.set_extent(2, 3);
    std::vector<Halide::Argument> arguments = { img, key, sat, clampMax, clampMin, cS };
    m_gammaFunction = (gammafunction)(gamma.compile_jit());

}

将其存储在指针中:

typedef int(*gammafunction)(buffer_t*, float, float, float, float, bool, buffer_t*);
gammafunction m_gammaFunction;

然后我尝试 运行 它:

buffer_t  output_buf = { 0 };
//// The host pointers point to the start of the image data:
buffer_t buf = { 0 };
buf.host = (uint8_t *)data; // Might also need const_cast
float * output = new float[width * height * 4];
output_buf.host = (uint8_t*)(output);
//                                // If the buffer doesn't start at (0, 0), then assign mins
output_buf.extent[0] = buf.extent[0] = width; // In elements, not bytes
output_buf.extent[1] = buf.extent[1] = height; // In elements, not bytes
output_buf.extent[2] = buf.extent[2] = 4;    // Assuming RGBA
//                 // No need to assign additional extents as they were init'ed to zero above
output_buf.stride[0] = buf.stride[0] = 4; // RGBA interleaved
output_buf.stride[1] = buf.stride[1] = width * 4; // Assuming no line padding
output_buf.stride[2] = buf.stride[2] = 1; // Channel interleaved
output_buf.elem_size = buf.elem_size = sizeof(float);

// Run the pipeline
int error = m_photoFunction(&buf, params[0], &output_buf);

但是没用... 错误:

Exception thrown at 0x000002974F552DE0 in Viewer.exe: 0xC0000005: Access violation executing location 0x000002974F552DE0.

If there is a handler for this exception, the program may be safely continued.

编辑:

这是我的 运行ning 函数代码:

buffer_t  output_buf = { 0 };
//// The host pointers point to the start of the image data:
buffer_t buf = { 0 };
buf.host = (uint8_t *)data; // Might also need const_cast
float * output = new float[width * height * 4];
output_buf.host = (uint8_t*)(output);
//                                // If the buffer doesn't start at (0, 0), then assign mins
output_buf.extent[0] = buf.extent[0] = width; // In elements, not bytes
output_buf.extent[1] = buf.extent[1] = height; // In elements, not bytes
output_buf.extent[2] = buf.extent[2] = 3;    // Assuming RGBA
                                             //                // No need to assign additional extents as they were init'ed to zero above
output_buf.stride[0] = buf.stride[0] = 4; // RGBA interleaved
output_buf.stride[1] = buf.stride[1] = width * 4; // Assuming no line padding
output_buf.stride[2] = buf.stride[2] = 1; // Channel interleaved
output_buf.elem_size = buf.elem_size = sizeof(float);

// Run the pipeline
int error = m_gammaFunction(&buf, params[0], params[1], params[2], params[3], params[4] > 0.5 ? true : false, &output_buf);

if (error) {
    printf("Halide returned an error: %d\n", error);
    return -1;
}

memcpy(output, data, size * sizeof(float));

有人可以帮我吗?

编辑:

感谢@KhouriGiordano,我发现我做错了什么。事实上,我从 AOT 编译切换到这段代码。所以现在我的代码看起来像这样:

class GammaOperator
{
public:
    GammaOperator();

    int realize(buffer_t * input, float params[], buffer_t * output, int width);
private:

    HalideFloat m_key;
    HalideFloat m_sat;
    HalideFloat m_clampMax;
    HalideFloat m_clampMin;
    HalideBool  m_cS;

    Halide::ImageParam m_img;
    Halide::Var x, y, c;
    Halide::Func m_gamma;
};


GammaOperator::GammaOperator()
    : m_img( Halide::type_of<float>(), 3)
{

    Halide::Expr w = (1.f + 1.6774f) * pow(m_key.get(), 0.9925f) / (1.f + 1.6774f * pow(m_key.get(), 0.9925f));
    Halide::Expr sat = Halide::select(m_cS, m_sat, w);

    Halide::Expr luminance = m_img(x, y, 0) * 0.072186f + m_img(x, y, 1) * 0.715158f + m_img(x, y, 2) *  0.212656f;
    Halide::Expr ldr_lum = (luminance - m_clampMin) / (m_clampMax - m_clampMin);
    ldr_lum = Halide::clamp(ldr_lum, 0.f, 1.f);
    ldr_lum = Halide::pow(ldr_lum, m_key);
    Halide::Expr imLum = m_img(x, y, c) / luminance;
    imLum = Halide::pow(imLum, sat) * ldr_lum;
    imLum = Halide::clamp(imLum, 0.f, 1.f);
    m_gamma(x, y, c) = imLum;

}

int GammaOperator::realize(buffer_t * input, float params[], buffer_t * output, int width)
{
    m_img.set(Halide::Buffer(Halide::type_of<float>(), input));
    m_img.set_stride(0, 4);
    m_img.set_stride(1, width * 4);
    m_img.set_stride(2, 4);
    // algorytm
    m_gamma.vectorize(x, 16).parallel(y);

    //params[0], params[1], params[2], params[3], params[4] > 0.5 ? true : false
    //{ img, key, sat, clampMax, clampMin, cS };
    m_key.set(params[0]);
    m_sat.set(params[1]);
    m_clampMax.set(params[2]);
    m_clampMin.set(params[3]);
    m_cS.set(params[4] > 0.5f ? true : false);
    //// kompilacja
    m_gamma.realize(Halide::Buffer(Halide::type_of<float>(), output));
    return 0;
}

我是这样使用它的:

buffer_t  output_buf = { 0 };
    //// The host pointers point to the start of the image data:
    buffer_t buf = { 0 };
    buf.host = (uint8_t *)data; // Might also need const_cast
    float * output = new float[width * height * 4];
    output_buf.host = (uint8_t*)(output);
    //                                // If the buffer doesn't start at (0, 0), then assign mins
    output_buf.extent[0] = buf.extent[0] = width; // In elements, not bytes
    output_buf.extent[1] = buf.extent[1] = height; // In elements, not bytes
    output_buf.extent[2] = buf.extent[2] = 4;    // Assuming RGBA
                                                 //                // No need to assign additional extents as they were init'ed to zero above
    output_buf.stride[0] = buf.stride[0] = 4; // RGBA interleaved
    output_buf.stride[1] = buf.stride[1] = width * 4; // Assuming no line padding
    output_buf.stride[2] = buf.stride[2] = 1; // Channel interleaved
    output_buf.elem_size = buf.elem_size = sizeof(float);

    // Run the pipeline

    int error = s_gamma->realize(&buf, params, &output_buf, width);

但它仍然在 m_gamma.realize 功能上崩溃,控制台中有信息:

Error: Constraint violated: f0.stride.0 (4) == 1 (1)

通过使用 Halide::Param::get(),您可以在调用 get() 时从 Param 对象中提取(默认值为 0)值。如果要使用调用生成函数时给定的参数值,只需使用它而不调用 get 并且它应该被隐式转换为 Expr.

由于 Param 不可转换为布尔值,因此执行 if 的 Halide 方法是 Halide::select().

您没有使用 Halide::clamp() 的 return 值。

我没有看到 cS 被 Halide 代码使用,只有 C 代码。

现在解决您的 JIT 问题。看来你开始做AOT编译转JIT了

你制作了一个 std::vector<Halide::Argument> 但没有将它传递到任何地方。 Halide 怎么知道 Param 你想用什么?它查看 Func 并找到对 ImageParamParam 对象的引用。

您怎么知道它期望 Param 的顺序?你无法控制这个。我能够通过定义 HL_GENBITCODE=1 转储位码,然后使用 llvm-dis 查看它以查看您的函数:

int gamma
    ( buffer_t *img
    , float clampMax
    , float key
    , float clampMin
    , float sat
    , void *user_context
    , buffer_t *result
    );
  • 使用 gamma.realize(Halide::Buffer(Halide::type_of<float>(), &output_buf)) 而不是使用 gamma.compile_jit() 并尝试正确调用生成的函数。

一次性使用:

  • 使用 Image 代替 ImageParam
  • 使用 Expr 代替 Param

对于单个 JIT 编译的重复使用:

  • 保留ImageParamParam并在实现Func之前设置它们。