当前位置:   article > 正文

【Paddle2ONNX】为Paddle2ONNX适配swish算子_cannot found attribute beta in op: swish

cannot found attribute beta in op: swish

简介

在PaddlePaddle2.6中,swish算子在PaddleInference上发生了变化,删除掉了beta这个Attr,因此我们需要想办法自行适配它。

适配过程

原解析relu6算子的核心代码如下:

void SwishMapper::Opset7() {
  auto input_info = GetInput("X");
  auto output_info = GetOutput("Out");

  std::string beta_node =
      helper_->Constant({}, GetOnnxDtype(input_info[0].dtype), beta_);
  // TODO(jiangjiajun) eliminate multiply with a constant of value 1
  // TODO(jiangjiajun) eliminate add with a constant of value 0
  auto beta_x_node = helper_->MakeNode("Mul", {input_info[0].name, beta_node});
  auto sigmod_node = helper_->MakeNode("Sigmoid", {beta_x_node->output(0)});
  helper_->MakeNode("Mul", {input_info[0].name, sigmod_node->output(0)},
                    {output_info[0].name});
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

如果仅需要适配PaddlePaddle2.6,只需要改动为(同时还需要在类的构造函数中删除对beta参数的读取):

void SwishMapper::Opset7() {
  auto input_info = GetInput("X");
  auto output_info = GetOutput("Out");
  auto sigmod_node = helper_->MakeNode("Sigmoid", {input_info[0].name});
  helper_->MakeNode("Mul", {input_info[0].name, sigmod_node->output(0)},
                    {output_info[0].name});
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

考虑到要兼容PaddlePaddle2.5之前的用户,因此不能直接删除掉beta这个参数,进一步修改如下:

void SwishMapper::Opset7() {
  auto input_info = GetInput("X");
  auto output_info = GetOutput("Out");
  std::shared_ptr<paddle2onnx::NodeProto> sigmod_node = nullptr;

  if (HasAttr("beta")) {
    float temp_beta = 1.0;
    GetAttr("beta", &temp_beta);
    std::string beta_node = helper_->Constant({}, GetOnnxDtype(input_info[0].dtype), temp_beta);
    auto beta_x_node = helper_->MakeNode("Mul", {input_info[0].name, beta_node});
    sigmod_node = helper_->MakeNode("Sigmoid", {beta_x_node->output(0)});
  } else {
    sigmod_node = helper_->MakeNode("Sigmoid", {input_info[0].name});
  }

  helper_->MakeNode("Mul", {input_info[0].name, sigmod_node->output(0)},
                    {output_info[0].name});
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

参考链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/670467
推荐阅读
相关标签
  

闽ICP备14008679号