remove_padding_plugin.cu 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/inference/tensorrt/plugin/remove_padding_plugin.h"

namespace paddle {
namespace inference {
namespace tensorrt {
namespace plugin {

22 23
__global__ void RemovePaddingKernel(const float* input0,
                                    const int32_t* input1,
24 25 26 27 28 29 30 31 32 33 34 35
                                    float* output) {
  int word_id = blockIdx.x * gridDim.y + blockIdx.y;
  int32_t seqence_length = input1[blockIdx.x + 1] - input1[blockIdx.x];
  if (blockIdx.y < seqence_length) {
    output[(input1[blockIdx.x] + blockIdx.y) * gridDim.z * blockDim.x +
           blockIdx.z * blockDim.x + threadIdx.x] =
        input0[word_id * gridDim.z * blockDim.x + blockIdx.z * blockDim.x +
               threadIdx.x];
  }
}

nvinfer1::DataType RemovePaddingPlugin::getOutputDataType(
36 37
    int index,
    const nvinfer1::DataType* input_types,
38 39 40 41 42
    int nb_inputs) const TRT_NOEXCEPT {
  return input_types[0];
}

nvinfer1::DimsExprs RemovePaddingPlugin::getOutputDimensions(
43 44 45
    int outputIndex,
    const nvinfer1::DimsExprs* inputs,
    int nbInputs,
46 47 48 49 50 51 52 53 54 55 56 57
    nvinfer1::IExprBuilder& exprBuilder) TRT_NOEXCEPT {
  nvinfer1::DimsExprs output_dims{};
  output_dims.nbDims = 4;
  output_dims.d[0] = inputs[2].d[0];
  output_dims.d[1] = inputs[0].d[2];
  output_dims.d[2] = exprBuilder.constant(1);
  output_dims.d[3] = exprBuilder.constant(1);

  return output_dims;
}

bool RemovePaddingPlugin::supportsFormatCombination(
58 59 60
    int pos,
    const nvinfer1::PluginTensorDesc* inOut,
    int nbInputs,
61
    int nbOutputs) TRT_NOEXCEPT {
62 63
  PADDLE_ENFORCE_EQ(nbInputs,
                    3,
64 65 66
                    platform::errors::InvalidArgument("Must have 3 inputs, "
                                                      "but got %d input(s). ",
                                                      nbInputs));
67 68
  PADDLE_ENFORCE_EQ(nbOutputs,
                    getNbOutputs(),
69 70 71 72 73 74
                    platform::errors::InvalidArgument("Must have 1 output, "
                                                      "but got %d output(s). ",
                                                      nbOutputs));
  if (pos == 1 || pos == 2) {  // pos_id, work_id
    return inOut[pos].type == nvinfer1::DataType::kINT32 &&
           inOut[pos].format == nvinfer1::TensorFormat::kLINEAR;
75 76 77
  } else {
    return inOut[pos].type == nvinfer1::DataType::kFLOAT &&
           inOut[pos].format == nvinfer1::TensorFormat::kLINEAR;
78 79 80 81 82 83 84 85 86 87
  }
  // return (inOut[pos].type == nvinfer1::DataType::kFLOAT && inOut[pos].format
  // == nvinfer1::TensorFormat::kLINEAR)||
  // (inOut[pos].type == nvinfer1::DataType::kHALF && inOut[pos].format ==
  // nvinfer1::TensorFormat::kLINEAR)||
  // (inOut[pos].type == nvinfer1::DataType::kINT8 && inOut[pos].format ==
  // nvinfer1::TensorFormat::kCHW32);
}

void RemovePaddingPlugin::configurePlugin(
88 89
    const nvinfer1::DynamicPluginTensorDesc* inputs,
    int nbInputs,
90 91 92
    const nvinfer1::DynamicPluginTensorDesc* outputs,
    int nbOutputs) TRT_NOEXCEPT {}

93 94 95 96
void RemovePaddingPlugin::attachToContext(cudnnContext* cudnnContext,
                                          cublasContext* cublasContext,
                                          nvinfer1::IGpuAllocator* gpuAllocator)
    TRT_NOEXCEPT {}
97 98 99 100 101 102 103 104

void RemovePaddingPlugin::detachFromContext() TRT_NOEXCEPT {}

void RemovePaddingPlugin::terminate() TRT_NOEXCEPT {}

int RemovePaddingPlugin::enqueue(const nvinfer1::PluginTensorDesc* inputDesc,
                                 const nvinfer1::PluginTensorDesc* outputDesc,
                                 const void* const* inputs,
105 106
                                 void* const* outputs,
                                 void* workspace,
107 108 109 110 111 112 113 114 115 116 117
                                 cudaStream_t stream) TRT_NOEXCEPT {
  const auto input_desc = inputDesc[0];
  const float* input0 = static_cast<const float*>(inputs[0]);
  const int32_t* input1 =
      static_cast<const int32_t*>(inputs[1]);  // pos_id_tensor
  float* output = static_cast<float*>(outputs[0]);

  const auto input0_desc = inputDesc[0];

  const int32_t num_threads = 256;
  const dim3 num_blocks(
118 119
      input0_desc.dims.d[0],
      input0_desc.dims.d[1],
120 121 122
      input0_desc.dims.d[2] /
          num_threads);  //  batchs, max sequnce length, input.dims.d[2]/256

123 124
  RemovePaddingKernel<<<num_blocks, num_threads, 0, stream>>>(
      input0, input1, output);
125 126 127 128 129 130 131
  return cudaGetLastError() != cudaSuccess;
}

}  // namespace plugin
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle