batch_norm_op.cc 4.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// 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.

#include "lite/operators/batch_norm_op.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace operators {

bool BatchNormOp::CheckShape() const {
  CHECK_OR_FALSE(param_.x);
  CHECK_OR_FALSE(param_.bias);
  CHECK_OR_FALSE(param_.scale);
  CHECK_OR_FALSE(param_.mean);
  CHECK_OR_FALSE(param_.variance);
  CHECK_OR_FALSE(param_.y);
  if (!param_.is_test) {
    CHECK_OR_FALSE(param_.mean_out);
    CHECK_OR_FALSE(param_.variance_out);
    CHECK_OR_FALSE(param_.saved_mean);
    CHECK_OR_FALSE(param_.saved_variance);
  }
  auto x_dims = param_.x->dims();
  auto scale_dims = param_.scale->dims();
  auto bias_dims = param_.bias->dims();
  auto mean_dims = param_.mean->dims();
  auto variance_dims = param_.variance->dims();
  CHECK(x_dims.size() >= 2 && x_dims.size() <= 5)
      << "Input X must have 2 to 5 dimensions.";
  CHECK_EQ(scale_dims.size(), 1UL) << "Input Scale must have 1 dimensions.";
  CHECK_EQ(bias_dims.size(), 1UL) << "Input Bias must have 1 dimensions.";
  CHECK_EQ(mean_dims.size(), 1UL) << "Input Mean must have 1 dimensions.";
  CHECK_EQ(variance_dims.size(), 1UL)
      << "Input Variance must have 1 dimensions.";
  return true;
}

49
bool BatchNormOp::InferShapeImpl() const {
Y
Yan Chunwei 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  auto x_dims = param_.x->dims();
  int64_t channel_size = 0;
  switch (param_.data_layout) {
    case DATALAYOUT(kNCHW):
      channel_size = x_dims[1];
      break;
    // case DATALAYOUT(kNHWC):
    //   channel_size = x_dims[x_dims.size() - 1];
    //   break;
    default:
      LOG(FATAL) << "Unknown storage order: "
                 << DataLayoutToStr(param_.data_layout);
      break;
  }
  if (!param_.is_test) {
    param_.mean_out->Resize({channel_size});
    param_.variance_out->Resize({channel_size});
    param_.saved_mean->Resize({channel_size});
    param_.saved_variance->Resize({channel_size});
  }
  param_.y->Resize(x_dims);
X
xiaogang 已提交
71
  param_.y->set_lod(param_.x->lod());
Y
Yan Chunwei 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
  return true;
}

bool BatchNormOp::AttachImpl(const cpp::OpDesc &op_desc, lite::Scope *scope) {
  param_.x = scope->FindVar(op_desc.Input("X").front())->GetMutable<Tensor>();
  param_.bias =
      scope->FindVar(op_desc.Input("Bias").front())->GetMutable<Tensor>();
  param_.scale =
      scope->FindVar(op_desc.Input("Scale").front())->GetMutable<Tensor>();
  param_.mean =
      scope->FindVar(op_desc.Input("Mean").front())->GetMutable<Tensor>();
  param_.variance =
      scope->FindVar(op_desc.Input("Variance").front())->GetMutable<Tensor>();
  param_.y = scope->FindVar(op_desc.Output("Y").front())->GetMutable<Tensor>();
86 87 88 89 90 91 92 93 94 95 96 97 98 99

  auto is_test_type = op_desc.GetAttrType("is_test");
  switch (is_test_type) {
    case OpDescAPI::AttrType::INT:
      param_.is_test = op_desc.GetAttr<int>("is_test");
      break;
    case OpDescAPI::AttrType::BOOLEAN:
      param_.is_test = op_desc.GetAttr<bool>("is_test");
      break;
    default:
      LOG(FATAL) << "Unsupported attribute type: the type of attribute "
                    "`is_test` in BatchNormOP should be int or bool.";
  }

Y
Yan Chunwei 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  if (op_desc.HasAttr("use_global_stats")) {
    param_.use_global_stats = op_desc.GetAttr<bool>("use_global_stats");
  }
  if (!param_.is_test) {
    param_.mean_out =
        scope->FindVar(op_desc.Output("MeanOut").front())->GetMutable<Tensor>();
    param_.variance_out = scope->FindVar(op_desc.Output("VarianceOut").front())
                              ->GetMutable<Tensor>();
    param_.saved_mean = scope->FindVar(op_desc.Output("SavedMean").front())
                            ->GetMutable<Tensor>();
    param_.saved_variance =
        scope->FindVar(op_desc.Output("SavedVariance").front())
            ->GetMutable<Tensor>();
  }
  param_.epsilon = op_desc.GetAttr<float>("epsilon");
  param_.momentum = op_desc.GetAttr<float>("momentum");
  std::string data_layout = op_desc.GetAttr<std::string>("data_layout");
  CHECK_EQ(data_layout, "NCHW") << "TODO(hong19860320): Only support NCHW.";
  // param_.data_layout = StringToDataLayout(data_layout);
  return true;
}

}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(batch_norm, paddle::lite::operators::BatchNormOp);