batch_norm_op.cc 3.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 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.

15
#include "lite/kernels/npu/bridges/graph.h"
Z
zhupengyang 已提交
16
#include "lite/kernels/npu/bridges/registry.h"
17
#include "lite/kernels/npu/bridges/utility.h"
Y
Yan Chunwei 已提交
18 19 20

namespace paddle {
namespace lite {
21
namespace subgraph {
Y
Yan Chunwei 已提交
22 23
namespace npu {

24 25 26 27 28
int BatchNormConverter(void* ctx, OpLite* op) {
  CHECK(ctx != nullptr);
  CHECK(op != nullptr);
  auto graph = static_cast<Graph*>(ctx);
  auto op_info = op->op_info();
29
  auto op_type = op_info->Type();
30 31
  auto scope = op->scope();
  VLOG(3) << "[NPU] Converting " + op_type + "...";
Y
Yan Chunwei 已提交
32 33

  auto x_var_name = op_info->Input("X").front();
34 35 36
  auto y_var_name = op_info->Output("Y").front();
  auto batch_norm_node = graph->AddNode<ge::op::BatchNormExt2>(y_var_name);
  batch_norm_node->set_input_x(*graph->GetNode(x_var_name));
Y
Yan Chunwei 已提交
37 38

  auto scale_var_name = op_info->Input("Scale").front();
39 40
  auto scale = scope->FindVar(scale_var_name)->GetMutable<Tensor>();
  auto scale_const_node = graph->AddNode(scale_var_name, *scale);
Y
Yan Chunwei 已提交
41 42

  auto bias_var_name = op_info->Input("Bias").front();
43 44
  auto bias = scope->FindVar(bias_var_name)->GetMutable<Tensor>();
  auto bias_const_node = graph->AddNode(bias_var_name, *bias);
Y
Yan Chunwei 已提交
45 46

  auto mean_var_name = op_info->Input("Mean").front();
47 48
  auto mean = scope->FindVar(mean_var_name)->GetMutable<Tensor>();
  auto mean_const_node = graph->AddNode(mean_var_name, *mean);
Y
Yan Chunwei 已提交
49 50

  auto variance_var_name = op_info->Input("Variance").front();
51 52
  auto variance = scope->FindVar(variance_var_name)->GetMutable<Tensor>();
  auto variance_const_node = graph->AddNode(variance_var_name, *variance);
Y
Yan Chunwei 已提交
53

54 55 56 57
  float momentum = op_info->GetAttr<float>("momentum");
  float epsilon = op_info->GetAttr<float>("epsilon");
  int mode = 1;  // bnScale, bnBias tensor dims are 1xCx1x1
  bool use_global_stats = op_info->GetAttr<bool>("use_global_stats");
Y
Yan Chunwei 已提交
58

59 60 61 62 63 64 65 66 67
  batch_norm_node->set_input_scale(*scale_const_node);
  batch_norm_node->set_input_offset(*bias_const_node);
  batch_norm_node->set_input_mean(*mean_const_node);
  batch_norm_node->set_input_variance(*variance_const_node);
  batch_norm_node->set_attr_momentum(momentum);
  batch_norm_node->set_attr_epsilon(epsilon);
  batch_norm_node->set_attr_mode(mode);
  batch_norm_node->set_attr_use_global_stats(use_global_stats);
  return SUCCESS;
Y
Yan Chunwei 已提交
68 69 70
}

}  // namespace npu
71
}  // namespace subgraph
Y
Yan Chunwei 已提交
72 73 74
}  // namespace lite
}  // namespace paddle

75 76 77
REGISTER_SUBGRAPH_BRIDGE(NPU,
                         batch_norm,
                         paddle::lite::subgraph::npu::BatchNormConverter);