Tensorflow 累积张量流GPU算子的设计
我正在设计一个GPU操作内核,它在GPU内存的缓冲区中迭代累积数据。 数据保留在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
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)
我想了解我认为可以用来实现这一目标的三种方法:
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),
输出);
}
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,真));
}
<>我不太清楚你想用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
中以避免获取更新的值)