fill_constant_op.cc 3.0 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 {

class FillConstantOpConverter : public OpConverter {
 public:
  void operator()(const framework::proto::OpDesc& op,
                  const framework::Scope& scope,
                  bool test_mode) override {
    VLOG(4)
        << "convert a fluid fill_constant op to tensorrt fill_constant layer";

    framework::OpDesc op_desc(op, nullptr);
R
Ruibiao Chen 已提交
30
    int dtype = PADDLE_GET_CONST(int, op_desc.GetAttr("dtype"));
31
    std::string str_value =
R
Ruibiao Chen 已提交
32
        PADDLE_GET_CONST(std::string, op_desc.GetAttr("str_value"));
33
    std::vector<int64_t> shape =
R
Ruibiao Chen 已提交
34
        PADDLE_GET_CONST(std::vector<int64_t>, op_desc.GetAttr("shape"));
35 36 37 38
    if (str_value == "") {
      float value = PADDLE_GET_CONST(float, op_desc.GetAttr("value"));
      str_value = std::to_string(value);
    }
39 40 41 42 43 44 45 46 47 48 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 75
    std::unique_ptr<framework::Tensor> out_tensor(new framework::Tensor());
    out_tensor->Resize(phi::make_ddim(shape));
    nvinfer1::DataType trt_dtype = nvinfer1::DataType::kFLOAT;
    void* trt_data = nullptr;
    size_t trt_num;
    if (dtype == 2 || dtype == 3) {  // int,int64
      auto* tmp_ptr = out_tensor->mutable_data<int>(platform::CPUPlace());
      for (int64_t i = 0; i < out_tensor->numel(); i++)
        tmp_ptr[i] = std::stoi(str_value);
      trt_dtype = nvinfer1::DataType::kINT32;
      trt_data = static_cast<void*>(tmp_ptr);
    } else if (dtype == 5) {  // float
      auto* tmp_ptr = out_tensor->mutable_data<float>(platform::CPUPlace());
      for (int64_t i = 0; i < out_tensor->numel(); i++)
        tmp_ptr[i] = std::stof(str_value);
      trt_data = static_cast<void*>(tmp_ptr);
    }

    trt_num = static_cast<size_t>(out_tensor->numel());
    engine_->SetWeights("fill_constant_value", std::move(out_tensor));
    TensorRTEngine::Weight weight{trt_dtype, trt_data, trt_num};

    nvinfer1::Dims trt_in_shape;
    trt_in_shape.nbDims = shape.size();
    for (size_t i = 0; i < shape.size(); i++) trt_in_shape.d[i] = shape[i];
    nvinfer1::ILayer* layer =
        TRT_ENGINE_ADD_LAYER(engine_, Constant, trt_in_shape, weight.get());
    auto output_name = op_desc.Output("Out")[0];
    RreplenishLayerAndOutput(layer, "fill_constant", {output_name}, test_mode);
  }
};

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

REGISTER_TRT_OP_CONVERTER(fill_constant, FillConstantOpConverter);