slice_op_plugin.h 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// 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

#include <algorithm>
#include <string>
#include <vector>

#include "paddle/fluid/inference/tensorrt/engine.h"
#include "paddle/fluid/inference/tensorrt/plugin/trt_plugin.h"

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

29 30
class SlicePlugin : public PluginTensorRT {
 public:
31 32 33 34
  explicit SlicePlugin(std::vector<int> starts,
                       std::vector<int> ends,
                       std::vector<int> axes,
                       bool with_fp16);
35 36 37 38 39

  // It was used for tensorrt deserialization.
  // It should not be called by users.
  SlicePlugin(void const* serial_data, size_t serial_length);
  ~SlicePlugin();
40
  SlicePlugin* clone() const TRT_NOEXCEPT override;
41

42 43 44 45 46 47 48
  const char* getPluginType() const TRT_NOEXCEPT override {
    return "slice_plugin";
  }
  int getNbOutputs() const TRT_NOEXCEPT override { return 1; }
  int initialize() TRT_NOEXCEPT override { return 0; }
  bool supportsFormat(nvinfer1::DataType type, nvinfer1::PluginFormat format)
      const TRT_NOEXCEPT override;
49 50
  nvinfer1::Dims getOutputDimensions(int index,
                                     const nvinfer1::Dims* inputs,
51
                                     int nb_input_dims) TRT_NOEXCEPT override;
52
#if IS_TRT_VERSION_LT(8000)
53 54 55
  int enqueue(int batch_size,
              const void* const* inputs,
              void** outputs,
56
#else
57 58 59
  int enqueue(int batch_size,
              const void* const* inputs,
              void* const* outputs,
60
#endif
61 62
              void* workspace,
              cudaStream_t stream) TRT_NOEXCEPT override;
63

64
  size_t getSerializationSize() const TRT_NOEXCEPT override;
65 66 67

  // TRT will call this func  to serialize the configuration of TRT
  // It should not be called by users.
68
  void serialize(void* buffer) const TRT_NOEXCEPT override;
69 70 71 72 73 74

 private:
  std::vector<int> starts_;
  std::vector<int> ends_;
  std::vector<int> axes_;
  int* offset_temp_data_{nullptr};
W
Wilber 已提交
75
  std::vector<int> offset_info_;
76 77
};

78 79
class SlicePluginCreator : public TensorRTPluginCreator {
 public:
80 81 82
  const char* getPluginName() const TRT_NOEXCEPT override {
    return "slice_plugin";
  }
83

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

86 87 88 89
  nvinfer1::IPluginV2* deserializePlugin(const char* name,
                                         const void* serial_data,
                                         size_t serial_length)
      TRT_NOEXCEPT override {
90 91 92 93 94
    return new SlicePlugin(serial_data, serial_length);
  }
};
REGISTER_TRT_PLUGIN_V2(SlicePluginCreator);

95 96 97
#if IS_TRT_VERSION_GE(6000)
class SlicePluginDynamic : public DynamicPluginTensorRT {
 public:
98 99 100 101
  explicit SlicePluginDynamic(std::vector<int> starts,
                              std::vector<int> ends,
                              std::vector<int> axes,
                              int decrease_axis,
102
                              bool with_fp16);
103

104
  nvinfer1::IPluginV2DynamicExt* clone() const TRT_NOEXCEPT override {
105 106
    return new SlicePluginDynamic(
        starts_, ends_, axes_, decrease_axis_, with_fp16_);
107 108
  }

109 110
  SlicePluginDynamic(void const* serialData, size_t serialLength);

111 112 113 114 115
  const char* getPluginType() const TRT_NOEXCEPT override {
    return "slice_plugin_dynamic";
  }
  int getNbOutputs() const TRT_NOEXCEPT override { return 1; }
  int initialize() TRT_NOEXCEPT override;
116

117 118
  size_t getSerializationSize() const TRT_NOEXCEPT override;
  void serialize(void* buffer) const TRT_NOEXCEPT override;
119

120 121 122 123 124
  nvinfer1::DimsExprs getOutputDimensions(int output_index,
                                          const nvinfer1::DimsExprs* inputs,
                                          int nb_inputs,
                                          nvinfer1::IExprBuilder& expr_builder)
      TRT_NOEXCEPT override;
125 126 127

  bool supportsFormatCombination(int pos,
                                 const nvinfer1::PluginTensorDesc* inOut,
128 129
                                 int nbInputs,
                                 int nbOutputs) TRT_NOEXCEPT override;
130 131 132 133

  void configurePlugin(const nvinfer1::DynamicPluginTensorDesc* in,
                       int nbInputs,
                       const nvinfer1::DynamicPluginTensorDesc* out,
134
                       int nbOutputs) TRT_NOEXCEPT override {}
135 136 137 138

  size_t getWorkspaceSize(const nvinfer1::PluginTensorDesc* inputs,
                          int nbInputs,
                          const nvinfer1::PluginTensorDesc* outputs,
139
                          int nbOutputs) const TRT_NOEXCEPT override {
140 141 142 143 144
    return 0;
  }

  int enqueue(const nvinfer1::PluginTensorDesc* inputDesc,
              const nvinfer1::PluginTensorDesc* outputDesc,
145 146 147
              const void* const* inputs,
              void* const* outputs,
              void* workspace,
148
              cudaStream_t stream) TRT_NOEXCEPT override;
149 150 151 152
  nvinfer1::DataType getOutputDataType(int index,
                                       const nvinfer1::DataType* inputTypes,
                                       int nbInputs) const
      TRT_NOEXCEPT override;
153

154
  void destroy() TRT_NOEXCEPT override;
155 156 157 158 159

 private:
  std::vector<int> starts_;
  std::vector<int> ends_;
  std::vector<int> axes_;
160
  int decrease_axis_;
161
  int* offset_temp_data_{nullptr};
W
Wilber 已提交
162
  std::vector<int> offset_info_;
163
};
164

165
class SlicePluginDynamicCreator : public TensorRTPluginCreator {
166
 public:
167 168 169
  const char* getPluginName() const TRT_NOEXCEPT override {
    return "slice_plugin_dynamic";
  }
170

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

173 174 175 176
  nvinfer1::IPluginV2* deserializePlugin(const char* name,
                                         const void* serialData,
                                         size_t serialLength)
      TRT_NOEXCEPT override {
177
    return new SlicePluginDynamic(serialData, serialLength);
178 179
  }
};
W
Wilber 已提交
180
REGISTER_TRT_PLUGIN_V2(SlicePluginDynamicCreator);
181

182 183 184 185 186 187
#endif

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