cast_compute_test.cc 5.2 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
// 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"

namespace paddle {
namespace lite {

class CastComputeTester : public arena::TestCase {
 protected:
25 26 27 28
  std::string x_ = "x";
  std::string out_ = "out";
  // BOOL = 0;INT16 = 1;INT32 = 2;INT64 = 3;FP16 = 4;FP32 = 5;FP64 = 6;
  // SIZE_T = 19;UINT8 = 20;INT8 = 21;
29 30
  int in_dtype_;
  int out_dtype_;
31
  DDim dims_{{2, 2}};
32 33

 public:
34 35 36 37 38
  CastComputeTester(const Place& place,
                    const std::string& alias,
                    int in_dtype,
                    int out_dtype)
      : TestCase(place, alias), in_dtype_(in_dtype), out_dtype_(out_dtype) {}
39

40 41 42 43 44
  template <typename T1, typename T2>
  void RunBaselineHelper(Scope* scope) {
    auto* x = scope->FindTensor(x_);
    auto* x_data = x->data<T1>();
    auto* out = scope->NewTensor(out_);
45
    CHECK(out);
46 47 48 49 50 51 52 53
    out->Resize(dims_);
    auto* out_data = out->mutable_data<T2>();
    for (int i = 0; i < dims_.production(); i++) {
      *out_data = static_cast<T2>(*x_data);
      out_data++;
      x_data++;
    }
  }
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  void RunBaseline(Scope* scope) override {
    if (in_dtype_ == 20 && out_dtype_ == 5) {
      RunBaselineHelper<uint8_t, float>(scope);
    } else if (in_dtype_ == 2 && out_dtype_ == 5) {
      RunBaselineHelper<int32_t, float>(scope);
    } else if (in_dtype_ == 3 && out_dtype_ == 5) {
      RunBaselineHelper<int64_t, float>(scope);
    } else if (in_dtype_ == 5 && out_dtype_ == 3) {
      RunBaselineHelper<float, int64_t>(scope);
    } else if (in_dtype_ == 21 && out_dtype_ == 5) {
      RunBaselineHelper<int8_t, float>(scope);
    } else if (in_dtype_ == 5 && out_dtype_ == 21) {
      RunBaselineHelper<float, int8_t>(scope);
    } else {
      LOG(FATAL) << "unsupported";
70 71 72 73 74
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType("cast");
75 76
    op_desc->SetInput("X", {x_});
    op_desc->SetOutput("Out", {out_});
77 78 79 80
    op_desc->SetAttr("in_dtype", in_dtype_);
    op_desc->SetAttr("out_dtype", out_dtype_);
  }

81 82 83 84 85 86 87 88 89
  template <typename T1>
  void PrepareDataHelper() {
    std::vector<T1> x_data(dims_.production());
    for (int i = 0; i < dims_.production(); i++) {
      x_data[i] = static_cast<T1>(i % 128);
    }
    SetCommonTensor(x_, dims_, x_data.data());
  }

90
  void PrepareData() override {
91 92 93 94 95 96 97 98 99 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    // BOOL = 0;INT16 = 1;INT32 = 2;INT64 = 3;FP16 = 4;FP32 = 5;FP64 = 6;
    // SIZE_T = 19;UINT8 = 20;INT8 = 21;
    switch (in_dtype_) {
      case 20:
        PrepareDataHelper<uint8_t>();
        break;
      case 21:
        PrepareDataHelper<int8_t>();
        break;
      case 1:
        PrepareDataHelper<int16_t>();
        break;
      case 2:
        PrepareDataHelper<int32_t>();
        break;
      case 3:
        PrepareDataHelper<int64_t>();
        break;
      case 5:
        PrepareDataHelper<float>();
        break;
      case 6:
        PrepareDataHelper<double>();
        break;
      case 19:
        PrepareDataHelper<size_t>();
        break;
      default:
        LOG(FATAL) << "unsupported data type: " << in_dtype_;
        break;
    }

    PrecisionType out_ptype;
    switch (out_dtype_) {
      case 0:
        out_ptype = PRECISION(kBool);
        break;
      case 21:
        out_ptype = PRECISION(kInt8);
        break;
      case 1:
        out_ptype = PRECISION(kInt16);
        break;
      case 2:
        out_ptype = PRECISION(kInt32);
        break;
      case 3:
        out_ptype = PRECISION(kInt64);
        break;
      case 4:
        out_ptype = PRECISION(kFP16);
        break;
      case 5:
        out_ptype = PRECISION(kFloat);
        break;
      default:
        LOG(FATAL) << "unsupported data type: " << out_dtype_;
        break;
149
    }
150
    SetPrecisionType(out_, out_ptype);
151 152 153
  }
};

154
void TestCast(Place place, float abs_error, int in_dtype, int out_dtype) {
155
  std::unique_ptr<arena::TestCase> tester(
156 157
      new CastComputeTester(place, "def", in_dtype, out_dtype));
  arena::Arena arena(std::move(tester), place, abs_error);
158
  arena.TestPrecision();
159
}
160

161 162 163 164 165
TEST(Cast, precision) {
  LOG(INFO) << "test cast op";
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
166
  place = {TARGET(kARM), PRECISION(kAny)};
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#elif defined(LITE_WITH_XPU)
  place = TARGET(kXPU);
#else
  return;
#endif

// BOOL = 0;INT16 = 1;INT32 = 2;INT64 = 3;FP16 = 4;FP32 = 5;FP64 = 6;
// SIZE_T = 19;UINT8 = 20;INT8 = 21;
#ifndef LITE_WITH_XPU
  TestCast(place, abs_error, 20, 5);
#endif
  TestCast(place, abs_error, 2, 5);
#ifdef LITE_WITH_XPU
  TestCast(place, abs_error, 3, 5);
  TestCast(place, abs_error, 5, 3);
182 183 184 185 186
#endif
}

}  // namespace lite
}  // namespace paddle