transpose_compute_test.cc 5.6 KB
Newer Older
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"
20 21 22 23

namespace paddle {
namespace lite {

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
std::vector<int> CalStrides(const DDim& dims) {
  int dsize = dims.size();
  std::vector<int> strides(dsize, 1);
  for (int i = dsize - 2; i >= 0; i--) {
    strides[i] = strides[i + 1] * dims[i + 1];
  }
  return strides;
}

std::vector<int> CalIndex(const std::vector<int>& strides, int offset) {
  int dsize = strides.size();
  std::vector<int> index(dsize, 0);
  for (int i = 0; i < dsize; i++) {
    index[i] = offset / strides[i];
    offset %= strides[i];
  }
  return index;
41 42
}

43 44 45
std::vector<int> TransIndex(const std::vector<int>& in_index,
                            const std::vector<int>& axis) {
  std::vector<int> out_index(in_index.size(), 0);
46
  for (int i = 0; i < axis.size(); i++) {
47 48 49 50 51 52 53 54 55
    out_index[i] = in_index[axis[i]];
  }
  return out_index;
}

int CalOffset(const std::vector<int>& strides, const std::vector<int>& index) {
  int offset = 0;
  for (int i = 0; i < index.size(); i++) {
    offset += strides[i] * index[i];
56
  }
57
  return offset;
58 59 60 61 62 63 64 65 66
}

class TransposeComputeTester : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string op_type_ = "transpose2";
  std::string input_ = "x";
  std::string output_ = "out";
  std::string xshape_ = "xshape";
67
  DDim dims_;
68 69 70 71 72
  std::vector<int> axis_;

 public:
  TransposeComputeTester(const Place& place,
                         const std::string& alias,
73
                         DDim dims,
74
                         std::vector<int> axis)
75
      : TestCase(place, alias), dims_(dims), axis_(axis) {}
76 77 78 79 80 81 82

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

    auto* x = scope->FindTensor(input_);

83 84 85
    std::vector<int64_t> out_shape(dims_.size(), 0);
    for (size_t i = 0; i < dims_.size(); i++) {
      out_shape[i] = dims_[axis_[i]];
86 87
    }
    out->Resize(out_shape);
88 89 90 91 92 93 94
    auto out_dims = out->dims();

    std::vector<int> x_strides = CalStrides(dims_);
    std::vector<int> out_strides = CalStrides(out_dims);

    auto x_data = x->data<float>();
    auto out_data = out->mutable_data<float>();
95

96 97 98 99 100
    for (int i = 0; i < dims_.production(); i++) {
      std::vector<int> x_index = CalIndex(x_strides, i);
      std::vector<int> out_index = TransIndex(x_index, axis_);
      int out_offset = CalOffset(out_strides, out_index);
      out_data[out_offset] = x_data[i];
101 102 103 104
    }

    if (op_type_ == "transpose2") {
      auto* xshape = scope->NewTensor(xshape_);
105
      auto xshape_dims = dims_.Vectorize();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      xshape_dims.insert(xshape_dims.begin(), 0);
      xshape->Resize(xshape_dims);
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType(op_type_);
    op_desc->SetInput("X", {input_});
    op_desc->SetOutput("Out", {output_});
    if (op_type_ == "transpose2") {
      op_desc->SetOutput("XShape", {xshape_});
    }
    op_desc->SetAttr("axis", axis_);
  }

  void PrepareData() override {
122 123 124
    std::vector<float> din(dims_.production());
    fill_data_rand(din.data(), -1.f, 1.f, dims_.production());
    SetCommonTensor(input_, dims_, din.data());
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 160 161 162
void TestTranspose2D(Place place, float abs_error) {
  DDim x_dims{{4, 5}};
  std::vector<std::vector<int>> axes{{0, 1}, {1, 0}};
  for (auto axis : axes) {
    std::unique_ptr<arena::TestCase> tester(
        new TransposeComputeTester(place, "def", x_dims, axis));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision({"xshape"});
  }
}

void TestTranspose3D(Place place, float abs_error) {
  DDim x_dims{{3, 4, 5}};
  std::vector<std::vector<int>> axes{
      {0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {2, 1, 0}};
  for (auto axis : axes) {
    std::unique_ptr<arena::TestCase> tester(
        new TransposeComputeTester(place, "def", x_dims, axis));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision({"xshape"});
  }
}

void TestTranspose4D(Place place, float abs_error) {
  DDim x_dims{{2, 3, 4, 5}};
  std::vector<std::vector<int>> axes{
      {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {3, 1, 2, 0}, {3, 1, 0, 2}};
  for (auto axis : axes) {
    std::unique_ptr<arena::TestCase> tester(
        new TransposeComputeTester(place, "def", x_dims, axis));
    arena::Arena arena(std::move(tester), place, abs_error);
    arena.TestPrecision({"xshape"});
  }
}

163 164 165 166
TEST(Transpose, precision) {
  LOG(INFO) << "test Transpose op";
  float abs_error = 2e-5;
  Place place;
167
#if defined(LITE_WITH_NPU)
168 169
  place = TARGET(kNPU);
  abs_error = 1e-2;  // Using fp16 in NPU
170 171 172
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
  place = TARGET(kHuaweiAscendNPU);
  abs_error = 1e-2;  // precision_mode default is force_fp16
173 174
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL) // NOLINT
//  place = TARGET(kXPU);
175 176 177 178
#else
  return;
#endif

179 180 181
  TestTranspose2D(place, abs_error);
  TestTranspose3D(place, abs_error);
  TestTranspose4D(place, abs_error);
182 183 184 185
}

}  // namespace lite
}  // namespace paddle