slice_op.cc 5.4 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. */

#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
#include "paddle/fluid/inference/tensorrt/plugin/slice_op_plugin.h"
17
#include "paddle/fluid/inference/tensorrt/plugin/special_slice_plugin.h"
18 19 20 21 22 23 24 25 26

namespace paddle {
namespace inference {
namespace tensorrt {

class SliceOpConverter : public OpConverter {
 public:
  void operator()(const framework::proto::OpDesc& op,
                  const framework::Scope& scope, bool test_mode) override {
27 28
    // This OP is implemented by trt dynamic shpae plugin.
    // Dynamic shape plugin requires TRT version greater than 6.0.
29 30 31 32
    VLOG(4) << "convert slice op to tensorrt layer";
    framework::OpDesc op_desc(op, nullptr);
    // Declare inputs
    auto* input = engine_->GetITensor(op_desc.Input("Input")[0]);
33
    auto output_name = op_desc.Output("Out")[0];
34

35
    float out_scale = 1;
36
    if (op_desc.HasAttr("out_threshold")) {
37
      out_scale = BOOST_GET_CONST(float, op_desc.GetAttr("out_threshold"));
38 39 40
      engine_->SetTensorDynamicRange(input, out_scale);
    }

41
    std::vector<int> axes =
42
        BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("axes"));
43
    std::vector<int> starts =
44
        BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("starts"));
45
    std::vector<int> ends =
46
        BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("ends"));
47 48
    std::vector<int> decrease_axises =
        BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("decrease_axis"));
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    auto input_dims = input->getDimensions();
    if (!engine_->with_dynamic_shape()) {
      // notice that input shape is [CHW] without batch axis when input has
      // static shape
      for (size_t i = input_dims.nbDims; i > 0; i--) {
        input_dims.d[i] = input_dims.d[i - 1];
      }
      input_dims.d[0] = 1;  // fake batchsize, not useful here
      for (size_t i = 0; i < axes.size(); i++) {
        if (starts[i] < 0) {
          starts[i] = std::max(starts[i] + input_dims.d[axes[i]], 0);
        }
        if (ends[i] < 0) {
          ends[i] = std::max(ends[i] + input_dims.d[axes[i]], 0);
        }
        ends[i] = std::min(ends[i], input_dims.d[axes[i]]);
        PADDLE_ENFORCE_GT(
            ends[i], starts[i],
            platform::errors::InvalidArgument(
                "Attr(ends) should be greater than attr(starts) in "
                "slice op. But received ends = %d, starts = %d.",
                ends[i], starts[i]));
      }
    }

75 76
    nvinfer1::ILayer* layer = nullptr;
    if (engine_->with_dynamic_shape()) {
77 78
      if (engine_->use_oss() && engine_->with_ernie() &&
          input_dims.nbDims == 4) {
79
        std::vector<nvinfer1::ITensor*> plugin_inputs;
80 81 82 83 84 85 86
        if (engine_->with_interleaved()) {
          auto* shuffler_slice = TRT_ENGINE_ADD_LAYER(engine_, Shuffle, *input);
          nvinfer1::Permutation transpose_embed{2, 1, 0, 3};
          shuffler_slice->setSecondTranspose(transpose_embed);
          engine_->SetTensorDynamicRange(shuffler_slice->getOutput(0),
                                         out_scale);
          shuffler_slice->setName(
87
              ("SpecialSlice_interleaved: transpose: (Output: " + output_name +
88 89 90 91 92 93
               ")")
                  .c_str());
          plugin_inputs.emplace_back(shuffler_slice->getOutput(0));
        } else {
          plugin_inputs.emplace_back(input);
        }
94 95 96 97 98 99 100 101 102
        std::string pos_name;
        if (engine_->Has("ernie_pos_name")) {
          pos_name = engine_->Get<std::string>("ernie_pos_name");
        } else {
          // hard code for compatibility
          pos_name = engine_->network()->getInput(2)->getName();
        }
        plugin_inputs.emplace_back(
            engine_->GetITensor(pos_name));  // cu_seqlens, eval_placeholder_2
103 104 105 106

        // bool ban_fp16 = engine_->disable_trt_plugin_fp16();
        plugin::SpecialSlicePluginDynamic* plugin =
            new plugin::SpecialSlicePluginDynamic();
107 108
        layer = engine_->AddDynamicPlugin(plugin_inputs.data(),
                                          plugin_inputs.size(), plugin);
109
      } else {
110 111
        bool with_fp16 =
            engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
112 113 114 115
        int decrease_axis =
            decrease_axises.size() == 0 ? -1 : decrease_axises[0];
        plugin::SlicePluginDynamic* plugin = new plugin::SlicePluginDynamic(
            starts, ends, axes, decrease_axis, with_fp16);
116
        layer = engine_->AddDynamicPlugin(&input, 1, plugin);
117
      }
118
    } else {
119 120
      bool with_fp16 =
          engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
121
      plugin::SlicePlugin* plugin =
122
          new plugin::SlicePlugin(starts, ends, axes, with_fp16);
123
      layer = engine_->AddPlugin(&input, 1, plugin);
124
    }
125
    RreplenishLayerAndOutput(layer, "slice", {output_name}, test_mode);
126 127 128 129 130 131 132 133
  }
};

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

REGISTER_TRT_OP_CONVERTER(slice, SliceOpConverter);