Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 累积张量流GPU算子的设计_Tensorflow - Fatal编程技术网

Tensorflow 累积张量流GPU算子的设计

Tensorflow 累积张量流GPU算子的设计,tensorflow,Tensorflow,我正在设计一个GPU操作内核,它在GPU内存的缓冲区中迭代累积数据。 数据保留在GPU内存中很重要。因此,大致如下: with tf.device('/gpu:0'): buffer = tf.zeros(...) buffer = accumulate(param11, param12, buffer) buffer = accumulate(param21, param22, buffer) buffer = accumulate(param31, para

我正在设计一个GPU操作内核,它在GPU内存的缓冲区中迭代累积数据。 数据保留在GPU内存中很重要。因此,大致如下:

with tf.device('/gpu:0'):
    buffer = tf.zeros(...)

    buffer = accumulate(param11, param12, buffer)
    buffer = accumulate(param21, param22, buffer)
    buffer = accumulate(param31, param32, buffer)

with tf.device('/cpu:0'):
    A = do_some_more_stuff(buffer)
我想了解我认为可以用来实现这一目标的三种方法:

  • 在每次调用中分配输出张量,并将其用作输入张量 下一个电话。这很容易实现,但我担心 持续分配GPU内存将是一个问题。 tensorflow会将现在未使用的分配释放到GPU内存池中吗

    REGISTER_OP("Accumulate")
        .Input("param1: T")
        .Input("param2: T")
        .Input("buffer_in: T")
        .Output("buffer_out: T")
    
    void Compute(tensorflow::OpKernelContext * ctx) override
    {
        TensorShape output_shape{...};
        Tensor * output_ptr = nullptr;
        OP_REQUIRES_OK(ctx, ctx->allocate_output(
            0, output_shape, &output_ptr))
    
        kernel<<<grid, blocks, 0, stream>>>(
            ctx->input(0), ctx->input(1),
            output);
    }    
    
    寄存器操作(“累积”)
    .输入(“参数1:T”)
    .输入(“参数2:T”)
    .输入(“缓冲区输入:T”)
    .输出(“缓冲区输出:T”)
    void计算(tensorflow::OpKernelContext*ctx)覆盖
    {
    张量形输出_形{…};
    张量*output_ptr=nullptr;
    OP_需要_OK(ctx,ctx->分配输出(
    0,输出(形状和输出)
    内核(
    ctx->输入(0),ctx->输入(1),
    输出);
    }    
    
  • 引用输入和输出张量,确保它们引用 相同的数据。据我所知,标准ops和OpKernelContext 在文档中,这需要像其他操作一样使用互斥锁进行保护 也可能正在访问基础的引用张量

    REGISTER_OP("Accumulate")
        .Input("param1: T")
        .Input("param2: T")
        .Input("buffer_in: Ref(T)")
        .Output("buffer_out: Ref(T)")
    
    void Compute(tensorflow::OpKernelContext * ctx) override
    {
        mutex_lock(mu_);
    
        ctx->forward_ref_input_to_ref_output(2, 0);
    
        kernel<<<grid, blocks, 0, stream>>>(
            ctx->input(0), ctx->input(1),
            ctx->mutable_input(2, true));
    }
    
    寄存器操作(“累积”)
    .输入(“参数1:T”)
    .输入(“参数2:T”)
    .输入(“缓冲区输入:参考(T)”)
    .Output(“缓冲区输出:Ref(T)”)
    void计算(tensorflow::OpKernelContext*ctx)覆盖
    {
    互斥锁;
    ctx->将\u ref\u输入\u转发到\u ref\u输出(2,0);
    内核(
    ctx->输入(0),ctx->输入(1),
    ctx->可变_输入(2,真));
    }
    
  • 将allocate_persistent()与OpKernelConstruction上下文结合使用 为积累提供持久缓冲区。我不想这样做,因为 我正在处理可变的缓冲区大小,它们可能会相当大


  • <>我不太清楚你想用C++代码做什么,但是从Python片段中看,我认为<>代码> Tf。赋值可能会有帮助。它允许您执行以下操作:

    buffer = tf.Variable(...)
    param = tf.Variable(...)
    accumulate_op = buffer.assign(expr<param, buffer>)
    
    ...
    
    sess.run(accumulate_op)
    
    buffer=tf.Variable(…)
    param=tf.变量(…)
    累积=缓冲区分配(expr)
    ...
    分段运行(累积操作)
    
    运行
    accumulate\u op
    应更新gpu上的缓冲区(您可能必须将其包装在
    tf.group
    中以避免获取更新的值)