prelu_op_plugin.h 2.9 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 33
  std::vector<float> weight_;
  float *p_gpu_weight_;
34 35 36 37
  std::string mode_;

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

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

 public:
N
nhzlx 已提交
53 54 55 56 57 58
  PReluPlugin(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());
  }
59 60 61 62

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

N
nhzlx 已提交
72 73 74
  PReluPlugin *clone() const override {
    return new PReluPlugin(weight_.data(), weight_.size(), mode_);
  }
75

N
nhzlx 已提交
76
  const char *getPluginType() const override { return "prelu_plugin"; }
77 78 79 80 81 82 83
  int getNbOutputs() const override { return 1; }
  nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
                                     int nbInputDims) override;
  int enqueue(int batchSize, const void *const *inputs, void **outputs,
              void *workspace, cudaStream_t stream) override;
};

84
}  // namespace plugin
85 86 87
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle