conv2d_op.cc 4.4 KB
Newer Older
L
Luo Tao 已提交
1 2 3 4 5 6
/* 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

L
Luo Tao 已提交
7
http://www.apache.org/licenses/LICENSE-2.0
L
Luo Tao 已提交
8 9 10 11 12 13 14

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. */

L
Luo Tao 已提交
15
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
L
Luo Tao 已提交
16 17 18 19 20

namespace paddle {
namespace inference {
namespace tensorrt {

N
nhzlx 已提交
21
bool to_skip_merging_optimize(TensorRTEngine* engine_,
N
nhzlx 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
                              const std::vector<int>& filters,
                              const std::vector<int>& strides,
                              const std::vector<int>& paddings,
                              std::string input_name) {
  if (engine_->itensor_quote_num[input_name] > 0) {
    return true;
  }
  if (filters[0] == 1 && filters[1] == 1 && strides[0] == 1 &&
      strides[1] == 1 && paddings[0] == 0 && paddings[1] == 0)
    engine_->itensor_quote_num[input_name] += 1;

  return false;
}

L
Luo Tao 已提交
36 37
class Conv2dOpConverter : public OpConverter {
 public:
38
  void operator()(const framework::proto::OpDesc& op,
39
                  const framework::Scope& scope, bool test_mode) override {
40
    VLOG(3) << "convert a fluid conv2d op to tensorrt conv layer without bias";
N
nhzlx 已提交
41 42 43 44 45 46 47

    framework::OpDesc op_desc(op, nullptr);
    PADDLE_ENFORCE_EQ(op_desc.Input("Input").size(), 1);
    PADDLE_ENFORCE_EQ(op_desc.Input("Filter").size(), 1);  // Y is a weight
    PADDLE_ENFORCE_EQ(op_desc.Output("Output").size(), 1);

    auto* X = engine_->GetITensor(op_desc.Input("Input").front());
N
nhzlx 已提交
48

N
nhzlx 已提交
49 50 51 52 53
    // Declare weights
    auto* Y_v = scope.FindVar(op_desc.Input("Filter").front());
    PADDLE_ENFORCE_NOT_NULL(Y_v);
    auto* Y_t = Y_v->GetMutable<framework::LoDTensor>();

N
nhzlx 已提交
54
    platform::CPUPlace cpu_place;
N
nhzlx 已提交
55 56
    std::unique_ptr<framework::LoDTensor> weight_tensor(
        new framework::LoDTensor());
N
nhzlx 已提交
57
    weight_tensor->Resize(Y_t->dims());
N
nhzlx 已提交
58 59
    TensorCopySync((*Y_t), cpu_place, weight_tensor.get());

N
nhzlx 已提交
60 61 62 63 64 65 66
    auto* weight_data =
        weight_tensor->mutable_data<float>(platform::CPUPlace());

    PADDLE_ENFORCE_EQ(weight_tensor->dims().size(), 4UL);
    const int n_output = weight_tensor->dims()[0];
    const int filter_h = weight_tensor->dims()[2];
    const int filter_w = weight_tensor->dims()[3];
N
nhzlx 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    const int groups = boost::get<int>(op_desc.GetAttr("groups"));
    const std::vector<int> dilations =
        boost::get<std::vector<int>>(op_desc.GetAttr("dilations"));
    const std::vector<int> strides =
        boost::get<std::vector<int>>(op_desc.GetAttr("strides"));
    const std::vector<int> paddings =
        boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));

    nvinfer1::DimsHW nv_ksize(filter_h, filter_w);
    nvinfer1::DimsHW nv_dilations(dilations[0], dilations[1]);
    nvinfer1::DimsHW nv_strides(strides[0], strides[1]);
    nvinfer1::DimsHW nv_paddings(paddings[0], paddings[1]);

    TensorRTEngine::Weight weight{nvinfer1::DataType::kFLOAT,
                                  static_cast<void*>(weight_data),
N
nhzlx 已提交
83
                                  weight_tensor->memory_size() / sizeof(float)};
N
nhzlx 已提交
84 85 86 87 88 89 90 91 92 93 94 95

    TensorRTEngine::Weight bias{nvinfer1::DataType::kFLOAT, nullptr, 0};
    auto* layer = TRT_ENGINE_ADD_LAYER(
        engine_, Convolution, *const_cast<nvinfer1::ITensor*>(X), n_output,
        nv_ksize, weight.get(), bias.get());
    PADDLE_ENFORCE(layer != nullptr);
    layer->setStride(nv_strides);
    layer->setPadding(nv_paddings);
    layer->setDilation(nv_dilations);
    layer->setNbGroups(groups);

    auto output_name = op_desc.Output("Output").front();
96
    layer->setName(("conv2d (Output: " + output_name + ")").c_str());
N
nhzlx 已提交
97 98
    engine_->weight_map[op_desc.Input("Filter").front()] =
        std::move(weight_tensor);
99
    layer->getOutput(0)->setName(output_name.c_str());
N
nhzlx 已提交
100
    engine_->SetITensor(output_name, layer->getOutput(0));
N
nhzlx 已提交
101 102

    if (test_mode ||
N
nhzlx 已提交
103
        to_skip_merging_optimize(engine_, {filter_h, filter_w}, strides,
N
nhzlx 已提交
104
                                 paddings, op_desc.Input("Input").front())) {
N
nhzlx 已提交
105 106
      engine_->DeclareOutput(output_name);
    }
L
Luo Tao 已提交
107 108
  }
};
L
Luo Tao 已提交
109

L
Luo Tao 已提交
110 111 112
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle
113 114

REGISTER_TRT_OP_CONVERTER(conv2d, Conv2dOpConverter);