gpt4 book ai didi

c++ - Halide Jit 编译

转载 作者:行者123 更新时间:2023-11-30 03:39:01 25 4
gpt4 key购买 nike

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

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)

最佳答案

通过使用 Halide::Param::get() ,您正在从 Param 中提取(默认值为 0)值你打电话时的对象 get() .如果要使用调用生成函数时给定的参数值,直接使用,不用调用get它应该隐式转换为 Expr .

Param不可转换为 bool 值,Halide 执行 if 的方式是Halide::select() .

您没有使用 Halide::clamp() 的限制返回值.

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

现在解决您的 JIT 问题。看起来您开始进行 AOT 编译并切换到 JIT。

你做了一个std::vector<Halide::Argument>但不要在任何地方传递它。 Halide怎么会知道什么Param你想用吗?它查看 Func并找到对 ImageParam 的引用和 Param对象。

你怎么知道它期望 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 之前设置它们.

关于c++ - Halide Jit 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39085251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com