prelu_op_plugin.h 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#pragma once

N
nhzlx 已提交
17
#include <algorithm>
18
#include <string>
N
nhzlx 已提交
19 20 21 22
#include <vector>
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/tensor_util.h"

23 24 25 26 27 28
#include "paddle/fluid/inference/tensorrt/engine.h"
#include "paddle/fluid/inference/tensorrt/plugin/trt_plugin.h"

namespace paddle {
namespace inference {
namespace tensorrt {
29
namespace plugin {
30 31

class PReluPlugin : public PluginTensorRT {
N
nhzlx 已提交
32
  std::vector<float> weight_;
33
  float* p_gpu_weight_;
34 35
  std::string mode_;

36 37
 public:
  size_t getSerializationSize() const override {
N
nhzlx 已提交
38
    return getBaseSerializationSize() + SerializedSize(mode_.c_str()) +
39
           SerializedSize(weight_);
40 41 42 43 44
  }

  // TRT will call this func when we need to serialize the configuration of
  // tensorrt.
  // It should not be called by users.
45
  void serialize(void* buffer) const override {
N
nhzlx 已提交
46 47 48
    serializeBase(buffer);
    SerializeValue(&buffer, weight_);
    SerializeValue(&buffer, mode_.c_str());
49 50
  }

51 52
  PReluPlugin(const float* weight, const int weight_num,
              std::string const& mode)
N
nhzlx 已提交
53 54 55 56
      : mode_(mode) {
    weight_.resize(weight_num);
    std::copy(weight, weight + weight_num, weight_.data());
  }
57 58 59

  // It was used for tensorrt deserialization.
  // It should not be called by users.
60
  PReluPlugin(void const* serialData, size_t serialLength) {
N
nhzlx 已提交
61 62
    deserializeBase(serialData, serialLength);
    DeserializeValue(&serialData, &serialLength, &weight_);
63
    const char* prelu_mode;
N
nhzlx 已提交
64 65
    DeserializeValue(&serialData, &serialLength, &prelu_mode);
    mode_ = std::string(prelu_mode);
66
  }
67
  ~PReluPlugin() {}
N
nhzlx 已提交
68
  int initialize() override;
69
  void terminate() override;
70

71
  PReluPlugin* clone() const override {
72 73 74
    auto* ptr = new PReluPlugin(weight_.data(), weight_.size(), mode_);
    ptr->p_gpu_weight_ = p_gpu_weight_;
    return ptr;
N
nhzlx 已提交
75
  }
76

77
  const char* getPluginType() const override { return "prelu_plugin"; }
78
  int getNbOutputs() const override { return 1; }
79
  nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims* inputs,
80
                                     int nbInputDims) override;
81
#if IS_TRT_VERSION_LT(8000)
82
  int enqueue(int batchSize, const void* const* inputs, void** outputs,
83 84 85
#else
  int enqueue(int batchSize, const void* const* inputs, void* const* outputs,
#endif
86
              void* workspace, cudaStream_t stream) override;
87 88
};

89 90 91 92 93 94 95 96 97 98 99 100 101 102
class PReluPluginCreator : public TensorRTPluginCreator {
 public:
  const char* getPluginName() const override { return "prelu_plugin"; }

  const char* getPluginVersion() const override { return "1"; }

  nvinfer1::IPluginV2* deserializePlugin(const char* name,
                                         const void* serial_data,
                                         size_t serial_length) override {
    return new PReluPlugin(serial_data, serial_length);
  }
};
REGISTER_TRT_PLUGIN_V2(PReluPluginCreator);

103 104 105 106 107 108 109 110 111 112
#if IS_TRT_VERSION_GE(6000)
class PReluPluginDynamic : public DynamicPluginTensorRT {
 public:
  PReluPluginDynamic(const float* weight, const int weight_num,
                     std::string const& mode)
      : mode_(mode) {
    weight_.resize(weight_num);
    std::copy(weight, weight + weight_num, weight_.data());
  }

113
  PReluPluginDynamic(void const* serialData, size_t serialLength);
114
  ~PReluPluginDynamic() {}
115
  nvinfer1::IPluginV2DynamicExt* clone() const override {
116 117 118
    auto ptr = new PReluPluginDynamic(weight_.data(), weight_.size(), mode_);
    ptr->p_gpu_weight_ = p_gpu_weight_;
    return ptr;
119 120
  }

121
  const char* getPluginType() const override { return "prelu_plugin_dynamic"; }
122 123
  int getNbOutputs() const override { return 1; }
  int initialize() override;
124
  void terminate() override;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  size_t getSerializationSize() const override;
  void serialize(void* buffer) const override;

  nvinfer1::DimsExprs getOutputDimensions(
      int output_index, const nvinfer1::DimsExprs* inputs, int nb_inputs,
      nvinfer1::IExprBuilder& expr_builder) override;

  bool supportsFormatCombination(int pos,
                                 const nvinfer1::PluginTensorDesc* inOut,
                                 int nbInputs, int nbOutputs) override;

  void configurePlugin(const nvinfer1::DynamicPluginTensorDesc* in,
                       int nbInputs,
                       const nvinfer1::DynamicPluginTensorDesc* out,
                       int nbOutputs) override {}

  size_t getWorkspaceSize(const nvinfer1::PluginTensorDesc* inputs,
                          int nbInputs,
                          const nvinfer1::PluginTensorDesc* outputs,
                          int nbOutputs) const override {
    return 0;
  }

  int enqueue(const nvinfer1::PluginTensorDesc* inputDesc,
              const nvinfer1::PluginTensorDesc* outputDesc,
              const void* const* inputs, void* const* outputs, void* workspace,
              cudaStream_t stream) override;
  nvinfer1::DataType getOutputDataType(int index,
                                       const nvinfer1::DataType* inputTypes,
                                       int nbInputs) const override;

  void destroy() override { delete this; }

 private:
  std::vector<float> weight_;
  float* p_gpu_weight_;
  std::string mode_;
};
#endif

166 167 168 169 170 171 172 173 174 175 176 177 178 179
class PReluPluginDynamicCreator : public TensorRTPluginCreator {
 public:
  const char* getPluginName() const override { return "prelu_plugin_dynamic"; }

  const char* getPluginVersion() const override { return "1"; }

  nvinfer1::IPluginV2* deserializePlugin(const char* name,
                                         const void* serial_data,
                                         size_t serial_length) override {
    return new PReluPluginDynamic(serial_data, serial_length);
  }
};
REGISTER_TRT_PLUGIN_V2(PReluPluginDynamicCreator);

180
}  // namespace plugin
181 182 183
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle