batch_norm.cc 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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/anakin/convert/batch_norm.h"
#include <math.h>
17
#include <algorithm>
18 19 20
#include <map>
#include <string>
#include <vector>
21
#include "paddle/fluid/inference/anakin/convert/helper.h"
22 23 24 25 26

namespace paddle {
namespace inference {
namespace anakin {

27 28
template <typename TargetT, ::anakin::Precision PrecisionT>
void BatchNormOpConverter<TargetT, PrecisionT>::operator()(
29 30
    const framework::proto::OpDesc &op, const framework::BlockDesc &block_desc,
    const framework::Scope &scope, bool test_mode) {
31 32 33 34 35 36 37
  framework::OpDesc op_desc(op, nullptr);
  PADDLE_ENFORCE_EQ(op_desc.Output("Y").size(), 1);
  std::map<std::string, std::string> inputs;
  for (auto k : {"X", "Scale", "Bias", "Mean", "Variance"}) {
    PADDLE_ENFORCE_EQ(op_desc.Input(k).size(), 1UL);
  }

38
  auto input = op_desc.Input("X").front();
39 40
  auto output = op_desc.Output("Y").front();
  auto op_name = op_desc.Type() + ":" + op_desc.Output("Y").front();
41 42 43 44
  auto epsilon = boost::get<float>(op_desc.GetAttr("epsilon"));

  auto bn_op_name = op_name + ":bn";
  auto bn_output = bn_op_name + "_output";
45
  this->engine_->AddOp(bn_op_name, "BatchNorm", {input}, {bn_output});
46 47
  this->engine_->AddOpAttr(bn_op_name, "epsilon", epsilon);
  this->engine_->AddOpAttr(bn_op_name, "momentum", static_cast<float>(1.0));
48

49
  auto scale_op_name = op_name + ":scale";
50 51 52 53
  this->engine_->AddOp(scale_op_name, "Scale", {bn_output}, {output});
  this->engine_->AddOpAttr(scale_op_name, "axis", 1);
  this->engine_->AddOpAttr(scale_op_name, "num_axes", 1);
  this->engine_->AddOpAttr(scale_op_name, "bias_term", true);
54

55 56
  auto *mean_v = scope.FindVar(op_desc.Input("Mean").front());
  PADDLE_ENFORCE_NOT_NULL(mean_v);
57
  auto weight1 = pblock_from_var<TargetT, PrecisionT>(*mean_v, this->engine_);
58
  this->engine_->AddOpAttr(bn_op_name, "weight_1", *weight1);
59

60 61
  auto *variance_v = scope.FindVar(op_desc.Input("Variance").front());
  PADDLE_ENFORCE_NOT_NULL(variance_v);
62 63
  auto weight2 =
      pblock_from_var<TargetT, PrecisionT>(*variance_v, this->engine_);
64
  this->engine_->AddOpAttr(bn_op_name, "weight_2", *weight2);
65

66 67
  auto *weight3 = pblock_from_vector<TargetT, PrecisionT>(
      std::vector<float>({1}), this->engine_);
68
  this->engine_->AddOpAttr(bn_op_name, "weight_3", *weight3);
69

70 71
  auto *scale_v = scope.FindVar(op_desc.Input("Scale").front());
  PADDLE_ENFORCE_NOT_NULL(scale_v);
72
  auto scale = pblock_from_var<TargetT, PrecisionT>(*scale_v, this->engine_);
73
  this->engine_->AddOpAttr(scale_op_name, "weight_1", *scale);
74 75 76

  auto *bias_v = scope.FindVar(op_desc.Input("Bias").front());
  PADDLE_ENFORCE_NOT_NULL(bias_v);
77
  auto bias = pblock_from_var<TargetT, PrecisionT>(*bias_v, this->engine_);
78
  this->engine_->AddOpAttr(scale_op_name, "weight_2", *bias);
79 80 81 82 83 84
}

}  // namespace anakin
}  // namespace inference
}  // namespace paddle

85
#ifdef PADDLE_WITH_CUDA
86 87 88 89 90 91
using bn_nv_fp32 = ::paddle::inference::anakin::BatchNormOpConverter<
    ::anakin::saber::NV, ::anakin::Precision::FP32>;
using bn_nv_int8 = ::paddle::inference::anakin::BatchNormOpConverter<
    ::anakin::saber::NV, ::anakin::Precision::INT8>;
REGISTER_CUDA_ANAKIN_OP_CONVERTER(batch_norm, bn_nv_fp32);
REGISTER_CUDA_INT8_ANAKIN_OP_CONVERTER(batch_norm, bn_nv_int8);
92 93
#endif

94 95 96 97 98 99
using bn_cpu_fp32 = ::paddle::inference::anakin::BatchNormOpConverter<
    ::anakin::saber::X86, ::anakin::Precision::FP32>;
using bn_cpu_int8 = ::paddle::inference::anakin::BatchNormOpConverter<
    ::anakin::saber::X86, ::anakin::Precision::INT8>;
REGISTER_CPU_ANAKIN_OP_CONVERTER(batch_norm, bn_cpu_fp32);
REGISTER_CPU_INT8_ANAKIN_OP_CONVERTER(batch_norm, bn_cpu_int8);