scale_compute_test.cc 6.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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"
19
#include "lite/tests/utils/fill_data.h"
Y
Yan Chunwei 已提交
20 21 22 23 24 25 26

namespace paddle {
namespace lite {

class ScaleComputeTester : public arena::TestCase {
 protected:
  // common attributes for this op.
27 28 29
  std::string x_ = "x";
  std::string out_ = "out";
  DDim x_dims_{{100, 20}};
Y
Yan Chunwei 已提交
30 31
  float scale_ = 0.;
  float bias_ = 0.;
Z
zhupengyang 已提交
32 33
  bool bias_after_scale_ = true;
  PrecisionType x_dtype_ = PRECISION(kFloat);
Y
Yan Chunwei 已提交
34 35 36 37

 public:
  ScaleComputeTester(const Place& place,
                     const std::string& alias,
38
                     const DDim& x_dims,
Y
Yan Chunwei 已提交
39 40
                     float scale,
                     float bias,
Z
zhupengyang 已提交
41 42
                     bool bias_after_scale = true,
                     PrecisionType x_dtype = PRECISION(kFloat))
Y
Yan Chunwei 已提交
43
      : TestCase(place, alias),
44
        x_dims_(x_dims),
Y
Yan Chunwei 已提交
45 46
        scale_(scale),
        bias_(bias),
Z
zhupengyang 已提交
47 48
        bias_after_scale_(bias_after_scale),
        x_dtype_(x_dtype) {}
Y
Yan Chunwei 已提交
49

Z
zhupengyang 已提交
50 51 52 53
  template <typename T>
  void RunBaselineHelper(Scope* scope) {
    auto* x = scope->FindTensor(x_);
    auto* x_data = x->data<T>();
54 55
    auto* out = scope->NewTensor(out_);
    out->Resize(x_dims_);
Y
Yan Chunwei 已提交
56

Z
zhupengyang 已提交
57 58
    T scale = static_cast<T>(scale_);
    T bias = static_cast<T>(bias_);
Y
Yan Chunwei 已提交
59
    if (!bias_after_scale_) {
Z
zhupengyang 已提交
60
      bias *= scale;
Y
Yan Chunwei 已提交
61 62
    }

Z
zhupengyang 已提交
63
    auto out_data = out->mutable_data<T>();
64
    for (int i = 0; i < x_dims_.production(); i++) {
Z
zhupengyang 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      out_data[i] = x_data[i] * scale + bias;
    }
  }

  void RunBaseline(Scope* scope) override {
    switch (x_dtype_) {
      case PRECISION(kFloat):
        RunBaselineHelper<float>(scope);
        break;
      case PRECISION(kInt32):
        RunBaselineHelper<int>(scope);
        break;
      default:
        LOG(FATAL) << "unsupported data type: " << PrecisionToStr(x_dtype_);
        break;
Y
Yan Chunwei 已提交
80 81 82 83 84
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType("scale");
85 86
    op_desc->SetInput("X", {x_});
    op_desc->SetOutput("Out", {out_});
Y
Yan Chunwei 已提交
87 88 89 90 91
    op_desc->SetAttr("scale", scale_);
    op_desc->SetAttr("bias", bias_);
    op_desc->SetAttr("bias_after_scale", bias_after_scale_);
  }

Z
zhupengyang 已提交
92 93 94 95 96 97 98
  template <typename T>
  void PrepareDataHelper() {
    std::vector<T> dx(x_dims_.production());
    fill_data_rand<T>(dx.data(), -10, 10, x_dims_.production());
    SetCommonTensor(x_, x_dims_, dx.data());
  }

Y
Yan Chunwei 已提交
99
  void PrepareData() override {
Z
zhupengyang 已提交
100 101 102 103 104 105 106 107 108 109 110
    switch (x_dtype_) {
      case PRECISION(kFloat):
        PrepareDataHelper<float>();
        break;
      case PRECISION(kInt32):
        PrepareDataHelper<int>();
        break;
      default:
        LOG(FATAL) << "unsupported data type: " << PrecisionToStr(x_dtype_);
        break;
    }
Y
Yan Chunwei 已提交
111 112 113
  }
};

Z
zhupengyang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
void TestScaleShape(Place place, float abs_error) {
  for (auto x_dims :
       std::vector<std::vector<int64_t>>{{5, 2, 3, 4}, {8, 3, 5}, {12, 3}}) {
    std::unique_ptr<arena::TestCase> tester(
        new ScaleComputeTester(place, "def", DDim(x_dims), 1.5f, 0.2f));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision();
  }
}

void TestScaleValue(Place place, float abs_error) {
  for (float scale : {0.123, 0., -1.2}) {
    for (float bias : {1., 0., -1.2331}) {
      std::unique_ptr<arena::TestCase> tester(new ScaleComputeTester(
          place, "def", DDim({5, 2, 3, 4}), scale, bias));
      arena::Arena arena(std::move(tester), place, abs_error);
      arena.TestPrecision();
    }
  }
}

void TestScaleOrder(Place place, float abs_error) {
  for (bool bias_after_scale : {true, false}) {
    std::unique_ptr<arena::TestCase> tester(new ScaleComputeTester(
        place, "def", DDim({2, 3, 4, 5}), 1.5f, 0.2f, bias_after_scale));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision();
  }
}

void TestScaleDtype(Place place, float abs_error) {
  for (PrecisionType x_dtype : {PRECISION(kFloat), PRECISION(kInt32)}) {
    if (x_dtype == PRECISION(kFloat)) {
      place.precision = PRECISION(kFloat);
    } else if (x_dtype == PRECISION(kInt32)) {
      place.precision = PRECISION(kInt32);
    } else {
      LOG(FATAL) << "fatal";
    }
    std::unique_ptr<arena::TestCase> tester(new ScaleComputeTester(
        place, "def", DDim({2, 3, 4, 5}), 2.f, 1.f, true, x_dtype));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision();
  }
}

Y
Yan Chunwei 已提交
160
TEST(Scale, precision) {
161 162
  Place place;
  float abs_error = 2e-5;
163 164 165 166
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 4e-3;  // Using fp16 in NPU
#elif defined(LITE_WITH_ARM)
167
  place = TARGET(kARM);
H
hong19860320 已提交
168
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
169 170
  place = TARGET(kXPU);
  abs_error = 3e-4;  // Some operations use fp16 in XPU
171 172 173
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
  place = TARGET(kHuaweiAscendNPU);
  abs_error = 1e-2;  // precision_mode default is force_fp16
174 175 176 177
#elif defined(LITE_WITH_X86)
  place = TARGET(kX86);
#else
  return;
Y
Yan Chunwei 已提交
178 179
#endif

Z
zhupengyang 已提交
180 181 182 183 184 185
  TestScaleShape(place, abs_error);
  TestScaleValue(place, abs_error);
  TestScaleOrder(place, abs_error);
#ifdef LITE_WITH_ARM
  TestScaleDtype(place, abs_error);
#endif
Y
Yan Chunwei 已提交
186 187 188
}

TEST(Scale, performance) {
189 190 191 192 193 194 195
  Place place;
#if defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#elif defined(LITE_WITH_X86)
  place = TARGET(kX86);
#else
  return;
Y
Yan Chunwei 已提交
196 197
#endif

198 199
  std::unique_ptr<arena::TestCase> tester(new ScaleComputeTester(
      place, "def", DDim(std::vector<int64_t>{5, 2, 3, 4}), 1.2, 1.1, true));
Y
Yan Chunwei 已提交
200 201 202 203 204 205 206 207 208 209 210 211

  // To modify the arm context, one can retrive the context as follows.
  // #ifdef LITE_WITH_ARM
  //   tester->context()->As<ARMContext>();
  // #endif

  arena::Arena arena(std::move(tester), place, 2e-5);
  arena.TestPerformance(100);
}

}  // namespace lite
}  // namespace paddle