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 の方法で an を実行するのifHalide::select()です。

のクランプされた戻り値を使用していませんHalide::clamp()

Halide コードでは使用されていませんcS。C コードのみです。

今あなたのJITの問題に。AOT コンパイルを始めて、JIT に切り替えたようです。

を作成しますが、std::vector<Halide::Argument>どこにも渡しません。ParamHalide は、ユーザーが何を使用したいのかをどのように知ることができますか? を調べて、およびオブジェクトへの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()

1 回の使用の場合:

  • Imageの代わりに使用しImageParamます。
  • Exprの代わりに使用しParamます。

1 回の JIT コンパイルで繰り返し使用する場合:

  • ImageParamとを保持し、Paramに気付く前に設定しますFunc
于 2016-08-22T19:29:56.193 に答える