cast_compute_test.cc 4.5 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
    // 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;
    }
122 123 124
  }
};

125
void TestCast(Place place, float abs_error, int in_dtype, int out_dtype) {
126
  std::unique_ptr<arena::TestCase> tester(
127 128
      new CastComputeTester(place, "def", in_dtype, out_dtype));
  arena::Arena arena(std::move(tester), place, abs_error);
129
  arena.TestPrecision();
130
}
131

132 133 134 135 136
TEST(Cast, precision) {
  LOG(INFO) << "test cast op";
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
137
  place = TARGET(kARM);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#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);
153 154 155 156 157
#endif
}

}  // namespace lite
}  // namespace paddle