Home | History | Annotate | Download | only in custom_plugin_examples
      1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #if GOOGLE_CUDA
     17 #if GOOGLE_TENSORRT
     18 
     19 #include "tensorflow/contrib/tensorrt/custom_plugin_examples/inc_op_kernel.h"
     20 
     21 #include <vector>
     22 
     23 #define EIGEN_USE_GPU
     24 #include "cuda/include/cuda_runtime_api.h"
     25 #include "tensorflow/core/framework/op_kernel.h"
     26 #include "tensorflow/core/platform/stream_executor.h"
     27 #include "tensorflow/core/util/cuda_launch_config.h"
     28 
     29 namespace tensorflow {
     30 namespace tensorrt {
     31 
     32 __global__ void VecInc(const float* vec, float inc, float* dest, int n) {
     33   int i = blockDim.x * blockIdx.x + threadIdx.x;
     34   if (i < n) dest[i] = vec[i] + inc;
     35 }
     36 
     37 void IncrementKernel(const float* d_input, float inc, float* d_output,
     38                      int count, cudaStream_t stream) {
     39   int threads_per_block = 256;
     40   int blocks_per_grid = (count + threads_per_block - 1) / threads_per_block;
     41 
     42   TF_CHECK_OK(CudaLaunchKernel(VecInc, threads_per_block, blocks_per_grid, 0,
     43                                stream, d_input, inc, d_output, count));
     44 }
     45 
     46 // Note: this kernel definition is not needed in the plugin_test rule, but it is
     47 // required for correctness of the TF program, i.e. if not using plugin or when
     48 // run with trt optimization pass, the test should work.
     49 class IncPluginTRT : public OpKernel {
     50  public:
     51   explicit IncPluginTRT(OpKernelConstruction* context) : OpKernel(context) {
     52     std::vector<float> inc_list;
     53     OP_REQUIRES_OK(context, context->GetAttr("inc", &inc_list));
     54     OP_REQUIRES(context, inc_list.size() == 1,
     55                 errors::InvalidArgument(
     56                     "The increment list should contain single element."));
     57     inc_ = inc_list[0];
     58   }
     59 
     60   void Compute(OpKernelContext* context) override {
     61     const Tensor& input_tensor = context->input(0);
     62     const TensorShape& input_shape = input_tensor.shape();
     63     Tensor* output_tensor = nullptr;
     64     OP_REQUIRES_OK(context,
     65                    context->allocate_output(0, input_shape, &output_tensor));
     66     const cudaStream_t* stream = CHECK_NOTNULL(
     67         reinterpret_cast<const cudaStream_t*>(context->op_device_context()
     68                                                   ->stream()
     69                                                   ->implementation()
     70                                                   ->GpuStreamMemberHack()));
     71     IncrementKernel(input_tensor.flat<float>().data(), inc_,
     72                     output_tensor->flat<float>().data(),
     73                     input_shape.num_elements(), *stream);
     74   }
     75 
     76  private:
     77   float inc_;
     78 };
     79 
     80 REGISTER_KERNEL_BUILDER(Name("IncPluginTRT").Device(DEVICE_GPU), IncPluginTRT);
     81 
     82 }  // namespace tensorrt
     83 }  // namespace tensorflow
     84 
     85 #endif  // GOOGLE_TENSORRT
     86 #endif  // GOOGLE_CUDA
     87