1

我试图将我的卤化物程序编译为 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.

编辑:

这是我的运行功能代码:

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)
4

1 回答 1

2

通过使用,您在调用 时Halide::Param::get()从对象中提取(默认值为 0)值。如果你想使用调用生成函数时给出的参数值,直接使用而不调用,它应该被隐式转换为.Paramget()getExpr

由于Param不能转换为布尔值,因此 Halide 的做法ifHalide::select().

您没有使用Halide::clamp().

我看不到cS被卤化物代码使用,只有 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 编译重复使用:

  • 在意识到. ImageParam_ParamFunc
于 2016-08-22T19:29:56.193 回答