layer_norm_compute_test.cc 5.8 KB
Newer Older
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
// 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 <gtest/gtest.h>
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/arena/framework.h"
#include "lite/tests/utils/fill_data.h"

namespace paddle {
namespace lite {

class LayerNormComputeTest : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string op_type_ = "layer_norm";
28
  std::string x_ = "x";
29 30
  std::string scale_ = "scale";
  std::string bias_ = "bias";
31
  std::string y_ = "y";
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  std::string mean_ = "mean";
  std::string variance_ = "variance";
  DDim dims_{{4, 5, 19, 19}};
  float epsilon_ = 1e-5f;
  int begin_norm_axis_ = 1;
  bool has_bias_ = true;
  bool has_scale_ = true;

 public:
  LayerNormComputeTest(const Place& place,
                       const std::string& alias,
                       DDim dims,
                       float epsilon,
                       int begin_norm_axis,
                       bool has_bias,
                       bool has_scale)
      : TestCase(place, alias),
        dims_(dims),
        epsilon_(epsilon),
        begin_norm_axis_(begin_norm_axis),
        has_bias_(has_bias),
        has_scale_(has_scale) {}

  void RunBaseline(Scope* scope) override {
56
    auto x = scope->FindTensor(x_);
57 58 59
    auto scale = scope->FindTensor(scale_);
    auto bias = scope->FindTensor(bias_);

60
    auto y = scope->NewTensor(y_);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    auto mean = scope->NewTensor(mean_);
    auto variance = scope->NewTensor(variance_);
    CHECK(y);
    CHECK(mean);
    CHECK(variance);
    y->Resize(dims_);

    auto matrix_dim = dims_.Flatten2D(begin_norm_axis_);
    int batch_size = matrix_dim[0];
    int feature_size = matrix_dim[1];
    mean->Resize(std::vector<int64_t>{batch_size});
    variance->Resize(std::vector<int64_t>{batch_size});

    auto* x_data = x->data<float>();
    auto* scale_data = (scale == nullptr ? nullptr : scale->data<float>());
    auto* bias_data = (bias == nullptr ? nullptr : bias->data<float>());
77
    auto* y_data = y->mutable_data<float>();
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    auto* mean_data = mean->mutable_data<float>();
    auto* variance_data = variance->mutable_data<float>();

    for (int i = 0; i < batch_size; ++i) {
      int start = i * feature_size;
      int end = start + feature_size;

      float mean_t = 0;
      float variance_t = 0;
      for (int j = start; j < end; ++j) {
        mean_t += x_data[j];
        variance_t += x_data[j] * x_data[j];
      }
      mean_t /= feature_size;
      variance_t = variance_t / feature_size - mean_t * mean_t;
      mean_data[i] = mean_t;
      variance_data[i] = variance_t;
      variance_t = sqrt(variance_t + epsilon_);
      for (int j = start; j < end; ++j) {
97
        y_data[j] = (x_data[j] - mean_t) / variance_t;
98
        if (scale_data) {
99
          y_data[j] *= scale_data[j - start];
100 101
        }
        if (bias_data) {
102
          y_data[j] += bias_data[j - start];
103 104 105 106 107 108 109
        }
      }
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType(op_type_);
110 111 112 113 114 115 116 117
    op_desc->SetInput("X", {x_});
    if (has_scale_) {
      op_desc->SetInput("Scale", {scale_});
    }
    if (has_bias_) {
      op_desc->SetInput("Bias", {bias_});
    }
    op_desc->SetOutput("Y", {y_});
118 119 120 121 122 123 124
    op_desc->SetOutput("Mean", {mean_});
    op_desc->SetOutput("Variance", {variance_});
    op_desc->SetAttr("epsilon", epsilon_);
    op_desc->SetAttr("begin_norm_axis", begin_norm_axis_);
  }

  void PrepareData() override {
125 126 127 128 129 130 131 132 133 134
    std::vector<float> x(dims_.production());
    fill_data_rand(x.data(), -1.f, 1.f, dims_.production());
    SetCommonTensor(x_, dims_, x.data());

    auto scale_bias_size =
        dims_.Slice(begin_norm_axis_, dims_.size()).production();
    if (has_scale_) {
      DDim scale_dims({scale_bias_size});
      std::vector<float> scale(scale_bias_size);
      fill_data_rand(scale.data(), -1.f, 1.f, scale_bias_size);
135
      SetCommonTensor(scale_, scale_dims, scale.data(), {}, true);
136 137 138 139 140
    }
    if (has_bias_) {
      DDim bias_dims({scale_bias_size});
      std::vector<float> bias(scale_bias_size);
      fill_data_rand(bias.data(), -1.f, 1.f, scale_bias_size);
141
      SetCommonTensor(bias_, bias_dims, bias.data(), {}, true);
142 143 144 145 146 147 148 149
    }
  }
};

TEST(LayerNorm, precision) {
  LOG(INFO) << "test layer_norm op";
  float abs_error = 2e-5;
  Place place;
H
hong19860320 已提交
150
#if defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
151
  place = TARGET(kXPU);
152 153 154
#elif defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;
155 156 157
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
  place = TARGET(kHuaweiAscendNPU);
  abs_error = 1e-2;  // precision_mode default is force_fp16
158 159 160 161 162 163 164
#elif defined(LITE_WITH_ARM)
  place = TARGET(kARM);
  abs_error = 6e-5;
#else
  return;
#endif

165
  for (auto dims :
166
       std::vector<std::vector<int64_t>>{{2, 3, 4, 5}, {3, 4, 5}, {4, 5}}) {
167
    for (auto epsilon : {1e-5f}) {
168
      for (auto axis : {1, 2, 3}) {
169 170
        for (bool has_bias : {true, false}) {
          for (bool has_scale : {true, false}) {
171 172 173
            if (axis >= dims.size()) continue;
            std::unique_ptr<arena::TestCase> tester(new LayerNormComputeTest(
                place, "def", DDim(dims), epsilon, axis, has_bias, has_scale));
174 175 176 177 178 179 180 181 182 183 184
            arena::Arena arena(std::move(tester), place, abs_error);
            arena.TestPrecision({"mean", "variance"});
          }
        }
      }
    }
  }
}

}  // namespace lite
}  // namespace paddle