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

namespace paddle {
namespace lite {

class CastComputeTester : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string input_ = "x";
  std::string output_ = "out";
28 29
  int in_dtype_;
  int out_dtype_;
30 31 32
  DDim x_dims_{{2, 2, 2, 2}};

 public:
33 34 35 36 37
  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) {}
38 39 40 41 42 43

  void RunBaseline(Scope* scope) override {
    auto* out = scope->NewTensor(output_);
    CHECK(out);
    out->Resize(x_dims_);

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    if (out_dtype_ == 5 && in_dtype_ == 21) {
      auto* output_data = out->mutable_data<float>();
      auto* x = scope->FindTensor(input_);
      auto* x_data = x->data<char>();
      auto* output_data_tmp = output_data;
      auto* x_data_tmp = x_data;
      for (int i = 0; i < x_dims_.production(); i++) {
        *output_data_tmp = static_cast<float>(*x_data_tmp);
        output_data_tmp++;
        x_data_tmp++;
      }
    } else if (out_dtype_ == 5 && in_dtype_ == 2) {
      auto* output_data = out->mutable_data<float>();
      auto* x = scope->FindTensor(input_);
      auto* x_data = x->data<int32_t>();
      auto* output_data_tmp = output_data;
      auto* x_data_tmp = x_data;
      for (int i = 0; i < x_dims_.production(); i++) {
        *output_data_tmp = static_cast<float>(*x_data_tmp);
        output_data_tmp++;
        x_data_tmp++;
      }
66 67 68 69 70 71 72 73 74 75 76 77
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType("cast");
    op_desc->SetInput("X", {input_});
    op_desc->SetOutput("Out", {output_});
    op_desc->SetAttr("in_dtype", in_dtype_);
    op_desc->SetAttr("out_dtype", out_dtype_);
  }

  void PrepareData() override {
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    if (in_dtype_ == 21) {
      std::vector<char> x_data(x_dims_.production());
      for (int i = 0; i < x_dims_.production(); i++) {
        float sign = i % 3 == 0 ? -1.0f : 1.0f;
        x_data[i] = sign * static_cast<char>(i % 128);
      }
      SetCommonTensor(input_, x_dims_, x_data.data());
    } else if (in_dtype_ == 2) {
      std::vector<int32_t> x_data(x_dims_.production());
      for (int i = 0; i < x_dims_.production(); i++) {
        int sign = i % 3 == 0 ? -1 : 1;
        x_data[i] = sign * static_cast<int32_t>(i % 128);
      }
      SetCommonTensor(input_, x_dims_, x_data.data());
    } else {
      LOG(FATAL) << "not implemented!";
94 95 96 97 98 99 100 101 102
    }
  }
};

TEST(Cast, precision) {
  LOG(INFO) << "test cast op";
#ifdef LITE_WITH_ARM
  Place place(TARGET(kARM));

103 104
  std::unique_ptr<arena::TestCase> tester(
      new CastComputeTester(place, "def", 21, 5));
105 106
  arena::Arena arena(std::move(tester), place, 2e-5);
  arena.TestPrecision();
107 108 109 110 111

// std::unique_ptr<arena::TestCase> tester1(
//    new CastComputeTester(place, "def", 2, 5));
// arena::Arena arena1(std::move(tester1), place, 2e-5);
// arena1.TestPrecision();
112 113 114 115 116
#endif
}

}  // namespace lite
}  // namespace paddle