未验证 提交 1a941b71 编写于 作者: B bukejiyu 提交者: GitHub

[inference][trt]Layer norm rollback 2 plugin when trt<8.6 (#54679)

* layer_norm op with dynamic shape support INormalizationLayer in TRT8.6

* Using trt layer to make layers_norm op in lower than trt8.6
layer_norm op with dynamic shape support INormalizationLayer in TRT8.6

* ROLLBACK to layer_norm plugin when trt<8.6
* Update layer_norm_op.cc delete log

* Update layer_norm_op.cc code style
上级 73fa98ed
...@@ -24,19 +24,19 @@ class LayerNormOpConverter : public OpConverter { ...@@ -24,19 +24,19 @@ class LayerNormOpConverter : public OpConverter {
void operator()(const framework::proto::OpDesc& op, void operator()(const framework::proto::OpDesc& op,
const framework::Scope& scope, const framework::Scope& scope,
bool test_mode) override { bool test_mode) override {
VLOG(4) << "convert a layer_norm op with dynamic shape to Normalization " VLOG(4) << "convert a layer_norm op to INormalization layer or "
"layer or Static shape tensorrt layer_norm plugin"; "layer_norm plugin";
framework::OpDesc op_desc(op, nullptr); framework::OpDesc op_desc(op, nullptr);
auto* X = engine_->GetITensor(op_desc.Input("X")[0]); auto* X = engine_->GetITensor(op_desc.Input("X")[0]);
auto rank = X->getDimensions().nbDims;
std::string output_name = op_desc.Output("Y")[0]; std::string output_name = op_desc.Output("Y")[0];
const float eps = op_desc.HasAttr("epsilon") const float eps = op_desc.HasAttr("epsilon")
? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon"))
: 1e-5f; : 1e-5f;
if (engine_->with_dynamic_shape()) { if (engine_->with_dynamic_shape()) {
#if IS_TRT_VERSION_GE(8600)
auto* Scale = engine_->GetITensor(op_desc.Input("Scale")[0]); auto* Scale = engine_->GetITensor(op_desc.Input("Scale")[0]);
auto* Bias = engine_->GetITensor(op_desc.Input("Bias")[0]); auto* Bias = engine_->GetITensor(op_desc.Input("Bias")[0]);
auto rank = X->getDimensions().nbDims;
int32_t begin_axis = int32_t begin_axis =
op_desc.HasAttr("begin_norm_axis") op_desc.HasAttr("begin_norm_axis")
? PADDLE_GET_CONST(int, op_desc.GetAttr("begin_norm_axis")) ? PADDLE_GET_CONST(int, op_desc.GetAttr("begin_norm_axis"))
...@@ -67,61 +67,54 @@ class LayerNormOpConverter : public OpConverter { ...@@ -67,61 +67,54 @@ class LayerNormOpConverter : public OpConverter {
Scale, Scale,
concat_shape_tensor, concat_shape_tensor,
("layer_norm Scale: reshape: (Output(" + output_name + ")").c_str()); ("layer_norm Scale: reshape: (Output(" + output_name + ")").c_str());
#if IS_TRT_VERSION_GE(8600)
auto layer = TRT_ENGINE_ADD_LAYER( auto layer = TRT_ENGINE_ADD_LAYER(
engine_, Normalization, *X, *Scale_reshape, *Bias_reshape, axisMask); engine_, Normalization, *X, *Scale_reshape, *Bias_reshape, axisMask);
layer->setEpsilon(eps); layer->setEpsilon(eps);
RreplenishLayerAndOutput(layer, "layer_norm", {output_name}, test_mode); RreplenishLayerAndOutput(layer, "layer_norm", {output_name}, test_mode);
#else #endif
// μ #if IS_TRT_VERSION_LT(8600)
auto miu_layer = TRT_ENGINE_ADD_LAYER( // For dynamic shape & trt<8.6,
engine_, Reduce, *X, nvinfer1::ReduceOperation::kAVG, axisMask, true); // the shape of mean and variance will be determine in configuPlugin.
miu_layer->setName((output_name + "_miu").c_str()); auto* X = engine_->GetITensor(op_desc.Input("X").front());
auto miu_output = miu_layer->getOutput(0); auto* Bias_v = scope.FindVar(op_desc.Input("Bias").front());
// x−μ auto* Scale_v = scope.FindVar(op_desc.Input("Scale").front());
auto xsubmiu_output = Sub(X, miu_output); const int begin_norm_axis =
// σ op_desc.HasAttr("begin_norm_axis")
// pow(x−μ,2) ? PADDLE_GET_CONST(int, op_desc.GetAttr("begin_norm_axis"))
auto pow_tensor = Add1DConstantLayer(static_cast<float>(2)); : 1;
auto xsubmiu_pow_out = Pow( PADDLE_ENFORCE_NOT_NULL(
xsubmiu_output, Bias_v,
BroadcastTensors(xsubmiu_output, platform::errors::InvalidArgument(
pow_tensor, "Input(Bias) of layer_norm should not be null."));
("layer_norm_pow: reshape_for_broadcast: (Output(" + PADDLE_ENFORCE_NOT_NULL(
output_name + ")") Scale_v,
.c_str())); platform::errors::InvalidArgument(
// mean_var "Input(Scale) of layer_norm should not be null."));
auto mean_var_layer = auto* Bias_t = Bias_v->GetMutable<phi::DenseTensor>();
TRT_ENGINE_ADD_LAYER(engine_, auto* Scale_t = Scale_v->GetMutable<phi::DenseTensor>();
Reduce, auto bias_weight =
*xsubmiu_pow_out, engine_->GetFp32TrtWeight(op_desc.Input("Bias").front(), *Bias_t);
nvinfer1::ReduceOperation::kAVG, auto scale_weight =
axisMask, engine_->GetFp32TrtWeight(op_desc.Input("Scale").front(), *Scale_t);
true); nvinfer1::ILayer* layernorm_layer = nullptr;
mean_var_layer->setName((output_name + "_sigma").c_str()); std::vector<int64_t> mean_shape{1};
auto mean_var_out = mean_var_layer->getOutput(0); std::vector<int64_t> variance_shape{1};
// sigma bool with_fp16 =
auto eps_tensor = Add1DConstantLayer(eps); engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
auto sum_out = Sum( plugin::LayerNormPluginDynamic* plugin =
mean_var_out, new plugin::LayerNormPluginDynamic(
BroadcastTensors(mean_var_out, static_cast<const float*>(bias_weight.get().values),
eps_tensor, bias_weight.get().count,
("layer_norm_eps: reshape_for_broadcast: (Output(" + static_cast<const float*>(scale_weight.get().values),
output_name + ")") scale_weight.get().count,
.c_str())); begin_norm_axis,
auto sigma_layer = TRT_ENGINE_ADD_LAYER( eps,
engine_, Unary, *sum_out, nvinfer1::UnaryOperation::kSQRT); mean_shape,
auto sigma_output = sigma_layer->getOutput(0); variance_shape,
// σ/sigma with_fp16);
auto div_out = Div(xsubmiu_output, sigma_output); layernorm_layer = engine_->AddDynamicPlugin(&X, 1, plugin);
// (σ/sigma)*g+b RreplenishLayerAndOutput(
auto scale_out = Prod(div_out, Scale_reshape); layernorm_layer, "layer_norm", {output_name}, test_mode);
auto layer = TRT_ENGINE_ADD_LAYER(engine_,
ElementWise,
*scale_out,
*Bias_reshape,
nvinfer1::ElementWiseOperation::kSUM);
RreplenishLayerAndOutput(layer, "layer_norm", {output_name}, test_mode);
#endif #endif
} else { } else {
auto* Bias_v = scope.FindVar(op_desc.Input("Bias")[0]); auto* Bias_v = scope.FindVar(op_desc.Input("Bias")[0]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册