fc_op.cc 5.7 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 29
/* 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"

namespace paddle {
namespace inference {
namespace tensorrt {

// Reorder the elements from istrides to ostrides, borrowed from TRT convert in
// tensorflow.
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/tensorrt/convert/convert_nodes.cc#L318
template <typename T>
void Reorder2(nvinfer1::DimsHW shape, const T* idata, nvinfer1::DimsHW istrides,
              T* odata, nvinfer1::DimsHW ostrides) {
  for (int h = 0; h < shape.h(); ++h) {
    for (int w = 0; w < shape.w(); ++w) {
      odata[h * ostrides.h() + w * ostrides.w()] =
30
          idata[h * istrides.h() + w * istrides.w()];
31 32 33
    }
  }
}
34
// indata c * k
35
// Reorder the data layout from CK to KC.
G
gongweibao 已提交
36
void ReorderCKtoKC(TensorRTEngine::Weight& iweights,  // NOLINT
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
                   TensorRTEngine::Weight* oweights) {
  int c = iweights.dims[0];
  int k = iweights.dims[1];
  oweights->dims.assign({k, c});
  nvinfer1::DimsHW istrides = {1, k};
  nvinfer1::DimsHW ostrides = {c, 1};
  Reorder2({k, c}, static_cast<float const*>(iweights.get().values), istrides,
           static_cast<float*>(const_cast<void*>(oweights->get().values)),
           ostrides);
}

/*
 * FC converter convert a MUL op in Fluid to a FC layer in TRT.
 */
class FcOpConverter : public OpConverter {
 public:
  void operator()(const framework::proto::OpDesc& op,
54
                  const framework::Scope& scope, bool test_mode) override {
55
    VLOG(3) << "convert a fluid fc op to tensorrt fc layer without bias";
Y
Yan Chunwei 已提交
56
    framework::OpDesc op_desc(op, nullptr);
57 58 59 60 61 62 63 64 65

    auto input_names = op_desc.InputNames();
    bool with_bias = input_names.size() >= 3;
    std::string w_name = "Y";
    std::string i_name = "X";
    if (with_bias) {
      w_name = "W";
      i_name = "Input";
    }
66 67

    // Declare inputs
68
    auto* X = engine_->GetITensor(op_desc.Input(i_name).front());
69 70

    // Declare weights
71
    auto* Y_v = scope.FindVar(op_desc.Input(w_name).front());
72 73 74
    PADDLE_ENFORCE_NOT_NULL(Y_v);
    auto* Y_t = Y_v->GetMutable<framework::LoDTensor>();
    // This may trigger a GPU->CPU copy, because TRT's weight can only be
75
    // assigned from CPU memory, which can't be avoided.
76 77 78 79
    float* weight_data = nullptr;
    bool enable_int8 = boost::get<bool>(op_desc.HasAttr("enable_int8"));
    if (enable_int8) {
#if IS_TRT_VERSION_GE(5000)
80 81
      CHECK(op_desc.HasAttr(i_name + "_scale"));
      float in_scale = boost::get<float>(op_desc.GetAttr(i_name + "_scale"));
82 83 84 85 86 87 88 89 90 91
      auto weight_scale =
          boost::get<std::vector<float>>(op_desc.GetAttr("weight_scale"));
      weight_data = engine_->GetWeightCPUData(op_desc.Input(w_name).front(),
                                              Y_t, true, weight_scale);
      engine_->SetTensorDynamicRange(X, in_scale);
#endif
    } else {
      weight_data =
          engine_->GetWeightCPUData(op_desc.Input(w_name).front(), Y_t, false);
    }
N
nhzlx 已提交
92

93 94
    PADDLE_ENFORCE_EQ(Y_t->dims().size(), 2UL);  // a matrix
    size_t n_output = Y_t->dims()[1];
N
nhzlx 已提交
95

N
nhzlx 已提交
96
    std::unique_ptr<framework::Tensor> tmp(new framework::LoDTensor());
97
    tmp->Resize(Y_t->dims());
N
nhzlx 已提交
98 99

    memcpy(tmp->mutable_data<float>(platform::CPUPlace()), weight_data,
100
           Y_t->dims()[0] * Y_t->dims()[1] * sizeof(float));
101 102
    TensorRTEngine::Weight weight{nvinfer1::DataType::kFLOAT,
                                  static_cast<void*>(weight_data),
N
nhzlx 已提交
103
                                  static_cast<size_t>(Y_t->numel())};
104
    TensorRTEngine::Weight tmp_weight(nvinfer1::DataType::kFLOAT,
N
nhzlx 已提交
105
                                      static_cast<void*>(tmp->data<float>()),
N
nhzlx 已提交
106
                                      static_cast<size_t>(Y_t->numel()));
107 108 109 110 111
    weight.dims.assign({Y_t->dims()[0], Y_t->dims()[1]});
    tmp_weight.dims = weight.dims;

    // The data layout of TRT FC layer's weight is different from fluid's FC,
    // need to reorder the elements.
112
    ReorderCKtoKC(weight, &tmp_weight);
113 114 115 116 117

    // Currently, the framework can only handle one fluid op -> one TRT layer,
    // but fc fuses `mul` and `bias` (2 fluid ops), so here is a trick, just
    // handle `mul`, leave `add` as another layer.
    // DEBUG
118 119 120 121 122 123 124 125 126 127 128 129
    float* bias_data = nullptr;
    int bias_num = 0;
    if (with_bias) {
      auto* b_v = scope.FindVar(op_desc.Input("Bias").front());
      auto* b_t = b_v->GetMutable<framework::LoDTensor>();
      bias_data =
          engine_->GetWeightCPUData(op_desc.Input("Bias").front(), b_t, false);
      bias_num = b_t->numel();
    }
    TensorRTEngine::Weight bias{nvinfer1::DataType::kFLOAT,
                                static_cast<void*>(bias_data),
                                static_cast<size_t>(bias_num)};
130 131 132

    auto* layer = TRT_ENGINE_ADD_LAYER(engine_, FullyConnected,
                                       *const_cast<nvinfer1::ITensor*>(X),
133
                                       n_output, tmp_weight.get(), bias.get());
134

135
    engine_->SetWeights(op_desc.Input(w_name).front(), std::move(tmp));
136
    auto output_name = op_desc.Output("Out").front();
137 138

    RreplenishLayerAndOutput(layer, "fc", {output_name}, test_mode);
139 140 141 142 143 144 145
  }
};

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

N
nhzlx 已提交
146
REGISTER_TRT_OP_CONVERTER(fc, FcOpConverter);