emb_eltwise_layernorm.cc 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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/helper.h"
#include "paddle/fluid/inference/tensorrt/plugin/emb_eltwise_layernorm_plugin.h"

W
wanghuancoder 已提交
16 17 18 19 20 21 22 23 24
namespace paddle {
namespace framework {
class Scope;
namespace proto {
class OpDesc;
}  // namespace proto
}  // namespace framework
}  // namespace paddle

25 26 27 28 29 30 31 32 33
namespace paddle {
namespace inference {
namespace tensorrt {

class EmbEltwiseLayerNormOpConverter : public OpConverter {
 public:
  void operator()(const framework::proto::OpDesc& op,
                  const framework::Scope& scope, bool test_mode) override {
#if IS_TRT_VERSION_GE(6000)
34
    VLOG(4) << "convert fluid EmbEltwiseLayerNorm op to tensorrt layer";
35 36

    framework::OpDesc op_desc(op, nullptr);
37 38
    auto word_id_name = op_desc.Input("WordId").front();
    auto pos_id_name = op_desc.Input("PosId").front();
39 40
    engine_->Set("ernie_pos_name", new std::string(pos_id_name));

41 42 43 44
    auto sent_id_name = op_desc.Input("SentId").front();
    auto word_emb_name = op_desc.Input("WordEmbedding").front();
    auto pos_emb_name = op_desc.Input("PosEmbedding").front();
    auto sent_emb_name = op_desc.Input("SentEmbedding").front();
45 46 47 48 49 50 51 52 53 54 55 56 57

    std::vector<std::string> id_names;
    std::vector<std::string> emb_names;

    if (engine_->use_oss()) {
      id_names =
          std::vector<std::string>{word_id_name, pos_id_name, sent_id_name};
      emb_names =
          std::vector<std::string>{word_emb_name, pos_emb_name, sent_emb_name};
    } else {
      id_names = op_desc.Input("Ids");
      emb_names = op_desc.Input("Embs");
    }
58

59 60 61 62 63 64 65 66
    int input_num = id_names.size();

    // Declare inputs
    std::vector<nvinfer1::ITensor*> input_ids;
    for (int i = 0; i < input_num; i++) {
      input_ids.push_back(engine_->GetITensor(id_names[i]));
    }

67 68 69
    // input_embs[0]: word_embedding
    // input_embs[1]: pos_embedding
    // input_embs[2]: sent_embedding
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    std::vector<float*> input_embs;
    std::vector<int> emb_sizes;

    // get the presistable var's data
    auto get_persistable_data = [&](const std::string& var_name,
                                    framework::DDim* dims) -> float* {
      auto* temp_var = scope.FindVar(var_name);
      auto* temp_tensor = temp_var->GetMutable<framework::LoDTensor>();
      (*dims) = temp_tensor->dims();

      auto* temp_data = engine_->GetWeightCPUData(var_name, temp_tensor, false);
      return temp_data;
    };

    int hidden = 0;
    for (int i = 0; i < input_num; i++) {
      framework::DDim emb_dims;
      float* emb_data = get_persistable_data(emb_names[i], &emb_dims);
      int64_t emb_size = framework::product(emb_dims);
      input_embs.push_back(emb_data);
      emb_sizes.push_back(emb_size);
      PADDLE_ENFORCE_EQ(
          emb_dims.size(), 2,
          platform::errors::InvalidArgument(
              "The fused EmbEltwiseLayerNorm's emb should be 2 dims."));
      hidden = emb_dims[1];
    }

    framework::DDim bias_dims, scale_dims;

    auto* bias =
        get_persistable_data(op_desc.Input("Bias").front(), &bias_dims);
    auto* scale =
        get_persistable_data(op_desc.Input("Scale").front(), &scale_dims);
    int64_t bias_size = framework::product(bias_dims);
    int64_t scale_size = framework::product(scale_dims);
    nvinfer1::ILayer* layer = nullptr;
107
    bool enable_int8 = op_desc.HasAttr("enable_int8");
108

109 110 111 112
    if (engine_->use_oss()) {
      int output_fp16 = static_cast<int>((engine_->WithFp16() == 1) ? 1 : 0);
      if (enable_int8) {
        output_fp16 = 1;
113
      }
114 115 116 117 118 119
      PADDLE_ENFORCE_EQ(
          input_num, 3,
          platform::errors::InvalidArgument(
              "When using oss and var-len, embedding_eltwise_layernorm op"
              "should have 3 inputs only, but got %d.",
              input_num));
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      PADDLE_ENFORCE_EQ(
          output_fp16, 1,
          platform::errors::InvalidArgument(
              "Only Precision::KHalf(fp16) is supported when infering "
              "ernie(bert) model with config.EnableTensorRtOSS(). "
              "But Precision::KFloat32 is setted."));
      const std::vector<nvinfer1::PluginField> fields{
          {"bert_embeddings_layernorm_beta", bias,
           nvinfer1::PluginFieldType::kFLOAT32,
           static_cast<int32_t>(bias_size)},
          {"bert_embeddings_layernorm_gamma", scale,
           nvinfer1::PluginFieldType::kFLOAT32,
           static_cast<int32_t>(scale_size)},
          {"bert_embeddings_word_embeddings", input_embs[0],
           nvinfer1::PluginFieldType::kFLOAT32,
           static_cast<int32_t>(emb_sizes[0])},
          {"bert_embeddings_token_type_embeddings", input_embs[2],
           nvinfer1::PluginFieldType::kFLOAT32,
           static_cast<int32_t>(emb_sizes[2])},
          {"bert_embeddings_position_embeddings", input_embs[1],
           nvinfer1::PluginFieldType::kFLOAT32,
           static_cast<int32_t>(emb_sizes[1])},
          {"output_fp16", &output_fp16, nvinfer1::PluginFieldType::kINT32, 1},
      };

      // remember to free
      nvinfer1::PluginFieldCollection* plugin_ptr =
          static_cast<nvinfer1::PluginFieldCollection*>(
              malloc(sizeof(*plugin_ptr) +
                     fields.size() * sizeof(nvinfer1::PluginField)));
      plugin_ptr->nbFields = static_cast<int>(fields.size());
      plugin_ptr->fields = fields.data();

      std::vector<nvinfer1::ITensor*> plugin_inputs;
154 155 156 157 158 159 160 161 162
      plugin_inputs.emplace_back(
          engine_->GetITensor(word_id_name));  // word_embedding,
                                               // eval_placeholder_0
      plugin_inputs.emplace_back(
          engine_->GetITensor(sent_id_name));  // sent_embedding,
                                               // eval_placeholder_1
      plugin_inputs.emplace_back(
          engine_->GetITensor(pos_id_name));  // cu_seqlens,
                                              // eval_placeholder_2
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      auto max_seqlen_tensor =
          engine_->GetITensor(engine_->network()->getInput(3)->getName());
      auto* shuffle_layer =
          TRT_ENGINE_ADD_LAYER(engine_, Shuffle, *max_seqlen_tensor);
      nvinfer1::Dims shape_dim;
      shape_dim.nbDims = 1;
      shape_dim.d[0] = -1;
      shuffle_layer->setReshapeDimensions(shape_dim);
      plugin_inputs.emplace_back(
          shuffle_layer->getOutput(0));  // max_seqlen, eval_placeholder_3

      auto creator = GetPluginRegistry()->getPluginCreator(
          "CustomEmbLayerNormPluginDynamic", "2");

      auto plugin_obj =
          creator->createPlugin("CustomEmbLayerNormPluginDynamic", plugin_ptr);
      auto plugin_layer = engine_->network()->addPluginV2(
          plugin_inputs.data(), plugin_inputs.size(), *plugin_obj);
      layer = plugin_layer;
      free(plugin_ptr);
      auto output_name = op_desc.Output("Out")[0];
      RreplenishLayerAndOutput(layer, "emb_eltwise_layernorm",
                               {output_name, std::string("qkv_plugin_mask")},
                               test_mode);
187
    } else {
188 189 190 191 192 193 194 195 196 197 198
      bool with_fp16 =
          engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
      float eps = BOOST_GET_CONST(float, op_desc.GetAttr("epsilon"));
      plugin::DynamicPluginTensorRT* plugin = nullptr;
      plugin = new plugin::EmbEltwiseLayernormPluginDynamic(
          input_embs, bias, scale, emb_sizes, bias_size, scale_size, hidden,
          eps, with_fp16);
      layer = engine_->AddDynamicPlugin(input_ids.data(), input_num, plugin);
      auto output_name = op_desc.Output("Out")[0];
      RreplenishLayerAndOutput(layer, "emb_eltwise_layernorm", {output_name},
                               test_mode);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }

#else
    PADDLE_THROW(platform::errors::Fatal(
        "You are running the TRT Dynamic Shape mode, need to confirm that "
        "your TRT version is no less than 6.0"));
#endif
  }
};

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

REGISTER_TRT_OP_CONVERTER(fused_embedding_eltwise_layernorm,
                          EmbEltwiseLayerNormOpConverter);