pool_op_plugin.cu 3.0 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2018 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.

15
#include "paddle/fluid/inference/tensorrt/plugin/pool_op_plugin.h"
N
nhzlx 已提交
16
#include "paddle/fluid/inference/tensorrt/plugin/trt_plugin_factory.h"
N
nhzlx 已提交
17 18 19 20 21
#include "paddle/fluid/operators/math/pooling.h"

namespace paddle {
namespace inference {
namespace tensorrt {
N
nhzlx 已提交
22
namespace plugin {
N
nhzlx 已提交
23

24 25
PoolPlugin* CreatePoolPluginDeserialize(const void* buffer, size_t length) {
  return new PoolPlugin(buffer, length);
N
nhzlx 已提交
26
}
27
REGISTER_TRT_PLUGIN("pool_plugin", CreatePoolPluginDeserialize);
N
nhzlx 已提交
28

29 30 31
nvinfer1::Dims PoolPlugin::getOutputDimensions(int index,
                                               const nvinfer1::Dims* inputDims,
                                               int nbInputs) {
N
nhzlx 已提交
32 33 34 35 36 37 38 39 40 41 42 43
  assert(nbInputs == 1);
  assert(index == 0);
  assert(inputDims[0].nbDims == 3);
  nvinfer1::Dims const& input_dims = inputDims[0];

  nvinfer1::Dims output_dims = input_dims;

  output_dims.d[1] = output_shape_[1];
  output_dims.d[2] = output_shape_[2];
  return output_dims;
}

44 45
int PoolPlugin::enqueue(int batchSize, const void* const* inputs,
                        void** outputs, void* workspace, cudaStream_t stream) {
N
nhzlx 已提交
46 47 48 49 50 51 52 53 54 55
  auto const& input_dims = this->getInputDims(0);
  int input_size = 0;
  float const* idata = reinterpret_cast<float const*>(inputs[0]);
  float** odatas = reinterpret_cast<float**>(outputs);

  std::vector<int> input_shape = input_shape_;
  std::vector<int> output_shape = output_shape_;
  input_shape.insert(input_shape.begin(), batchSize);
  output_shape.insert(output_shape.begin(), batchSize);

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  if (pool_type_ == PoolType::max) {
    paddle::operators::math::MaxPool<float> pool_process;
    paddle::operators::math::Pool2dDirectCUDAFunctor<
        paddle::operators::math::MaxPool<float>, float>
        pool2d_forward;
    pool2d_forward(idata, input_shape, output_shape, ksize_, strides_,
                   paddings_, pool_process, true, adaptive_, odatas[0], stream);
  } else if (pool_type_ == PoolType::avg) {
    paddle::operators::math::AvgPool<float> pool_process;
    paddle::operators::math::Pool2dDirectCUDAFunctor<
        paddle::operators::math::AvgPool<float>, float>
        pool2d_forward;
    pool2d_forward(idata, input_shape, output_shape, ksize_, strides_,
                   paddings_, pool_process, true, adaptive_, odatas[0], stream);
  }
N
nhzlx 已提交
71 72 73 74

  return cudaGetLastError() != cudaSuccess;
}

N
nhzlx 已提交
75
}  // namespace plugin
N
nhzlx 已提交
76 77 78
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle