6

I am have implemented a rather complex new Op in Tensorflow with a GPU CUDA kernel. This Op requires a lot of dynamic memory allocation of variables which are not tensors and are deallocated after the op is done, more specifically it involves using a hash table.

Right now I am using cudaMalloc() and cudaFree() but I have noticed Tensorflow has its own type called Eigen::GPUDevice which has the ability to allocate and deallocate memory on the GPU.

My questions:

  1. Is it best practice to use Eigen::GPUDevice to manage GPU memory;
  2. By using Eigen::GPUDevice instead of the CUDA API I am "automatically" enabling multi-GPU support since different GPUDevices can be passed to the Op;
  3. Should I extend this idea to the CPU kernel and see if there is a CPUDevice type which also manages the memory instead of using C++ syntax (i.e. auto var = new int[100]; delete[] var)

2 Answers 2

9
+50

The is no direct public guideline for this issue. I usually just let the TensorFlow allocate this information by

template<typename Device, typename Dtype>
class MyOp: public OpKernel {
{
public:
  explicit MyOp(OpKernelConstruction *context) :
      OpKernel(context)
  {
    // ...
  }

  void Compute(OpKernelContext *context) override
  {
    Tensor* tmp_var = nullptr;
    Tensor* output = nullptr;

    TensorShape some_shape, some_shape2;

    // temparily use this space
    OP_REQUIRES_OK(ctx, ctx->allocate_temp(DT_FLOAT, some_shape, &tmp_var));
    // allocate memory for output tensor
    OP_REQUIRES_OK(ctx, ctx->allocate_output(0, some_shape2, &output));
  1. whatever needs memory, should be allocated by the TensorFlow context and not by custom cudaMalloc or new type[num] calls.
  2. the context should provide the information for the Allocator
  3. see below

Consider, for the sake of simplicity just adding two matrices (full example). TensorFlow-Operations usually contain the following structure:

Op description having REGISTER_OP, which is responsible for shape-checking, and setting the output shape (example)

OpKernel responsible for allocating memory, getting pointer to the inputs and setup stuff, (see above or this )

Functor for the implementation itself, like

Tensor* output = nullptr;
Tensor* tmp_var = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, output_shape, &output));
OP_REQUIRES_OK(ctx, ctx->allocate_temp(0, some_shape, &tmp_var));
// the function does not need to care about the memory allocation as everything is already setup at this point
::tensorflow::functor::MyFunctor<Device, Dtype>()(ctx, inputA, inputB, tmp_var, output);

You are just left by implementing

    // gpu version
    template <typename Dtype>
    struct MyFunctor<GPUDevice, Dtype> {
      void operator ()(::tensorflow::OpKernelContext* ctx,...)

    // cpu version
    template <typename Dtype>
    struct MyFunctor<CPUDevice, Dtype> {
      void operator ()(::tensorflow::OpKernelContext* ctx,...)

edit

  • allocate_persistent: use this if you need your data between Op invocations like one-time index structures.[example]
  • allocate_temp just tmp memory which will be not retained at the end of the Compute method lifetime. [example]

But I highly recommend reading the comment in the source-code here and then decided depending on your use case.

4
  • Thanks for the answer and all the examples. Can you comment on why you use allocate_temp() instead of allocate_persistent() as suggested by mrry's answer? Feb 5, 2018 at 20:09
  • 1
    It depends on your use case and if you are willing to free memory. See the comments in the TensorFlow repo. I suggest to use allocate_output in the kernel if the Op is stateless (which most Ops are). GPU memory is a rare resource (for most people), so I usually free my stuff.
    – Patwie
    Feb 6, 2018 at 12:42
  • Sorry to bother again, but I have some followup questions that you might be able to help with. 1. How do you go about allocating a user defined C++ types in this manner? For example an array of structs. 2. How about static memory e.g. int a[3] = {1,2,3}, should I be concerned about this? Feb 7, 2018 at 15:00
  • 1
    I do not allocate structs this way. But I would probably serialize the stuff into byte-arrays. The last example is placed on the stack, so there should be usually no problems.
    – Patwie
    Feb 7, 2018 at 15:54
2

The best practice is to use the OpKernelContext::allocate_persistent() method to allocate memory, in the form of a tensorflow::Tensor, that outlives a single call to OpKernel::Compute(). It uses the appropriate Allocator* for the device, so if the kernel runs on a GPU device, it will allocate GPU memory for that particular device, and if it runs on a CPU device it will allocate CPU memory.

1
  • Thanks for the answer. Perhaps I am over-reaching, but can you give a brief comment on static memory allocation (compile time)? For example, declaration of static shape arrays inside CUDA kernels, will this cause issues in a multi-GPU scenario? Feb 5, 2018 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.