matmul_compute_test.cc 18.1 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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

namespace paddle {
namespace lite {

void matrix_mul(int m_,
                int k_,
                int n_,
                float alpha,
                const float* x,
                const float* y,
                float* out) {
  for (int m = 0; m < m_; ++m) {
    for (int n = 0; n < n_; ++n) {
      out[m * n_ + n] = 0;
      for (int k = 0; k < k_; ++k) {
        out[m * n_ + n] += x[m * k_ + k] * y[k * n_ + n] * alpha;
      }
    }
  }
}

void transpose(int m, int n, const float* src, float* dst) {
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      dst[j * m + i] = src[i * n + j];
    }
  }
}

void mul_low_efficiency(DDim x_dims_,
                        DDim y_dims_,
                        bool x_transpose_,
                        bool y_transpose_,
                        float alpha_,
                        const float* x_data,
                        const float* y_data,
                        float* out_data) {
  if (!x_transpose_ && !y_transpose_) {
    CHECK_EQ(x_dims_[1], y_dims_[0])
        << "not supported x_dims(" << x_dims_ << ") and y_dims(" << y_dims_
        << "), x_transpose is " << x_transpose_ << ", y_transpose is "
        << y_transpose_;
    matrix_mul(
        x_dims_[0], y_dims_[0], y_dims_[1], alpha_, x_data, y_data, out_data);
  } else if (!x_transpose_ && y_transpose_) {
    CHECK_EQ(x_dims_[1], y_dims_[1])
        << "not supported x_dims(" << x_dims_ << ") and y_dims(" << y_dims_
        << "), x_transpose is " << x_transpose_ << ", y_transpose is "
        << y_transpose_;
    float* y_data_trans =
        static_cast<float*>(malloc(sizeof(float) * y_dims_[0] * y_dims_[1]));
    transpose(y_dims_[0], y_dims_[1], y_data, y_data_trans);
    matrix_mul(x_dims_[0],
               x_dims_[1],
               y_dims_[0],
               alpha_,
               x_data,
               y_data_trans,
               out_data);
    free(y_data_trans);
  } else if (x_transpose_ && !y_transpose_) {
    CHECK_EQ(x_dims_[0], y_dims_[0])
        << "not supported x_dims(" << x_dims_ << ") and y_dims(" << y_dims_
        << "), x_transpose is " << x_transpose_ << ", y_transpose is "
        << y_transpose_;
    float* x_data_trans =
        static_cast<float*>(malloc(sizeof(float) * x_dims_[0] * x_dims_[1]));
    transpose(x_dims_[0], x_dims_[1], x_data, x_data_trans);
    matrix_mul(x_dims_[1],
               x_dims_[0],
               y_dims_[1],
               alpha_,
               x_data_trans,
               y_data,
               out_data);
    free(x_data_trans);
  } else {
    CHECK_EQ(x_dims_[0], y_dims_[1])
        << "not supported x_dims(" << x_dims_ << ") and y_dims(" << y_dims_
        << "), x_transpose is " << x_transpose_ << ", y_transpose is "
        << y_transpose_;
    float* x_data_trans =
        static_cast<float*>(malloc(sizeof(float) * x_dims_[0] * x_dims_[1]));
    float* y_data_trans =
        static_cast<float*>(malloc(sizeof(float) * y_dims_[0] * y_dims_[1]));
    transpose(x_dims_[0], x_dims_[1], x_data, x_data_trans);
    transpose(y_dims_[0], y_dims_[1], y_data, y_data_trans);
    matrix_mul(x_dims_[1],
               x_dims_[0],
               y_dims_[0],
               alpha_,
               x_data_trans,
               y_data_trans,
               out_data);
    free(x_data_trans);
    free(y_data_trans);
  }
}

class MatMulComputeTester : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string x_ = "X";
  std::string y_ = "Y";
124 125 126
  std::string out_ = "Out";
  DDim x_dims_;
  DDim y_dims_;
127 128 129
  bool x_transpose_;
  bool y_transpose_;
  float alpha_;
Y
Yan Chunwei 已提交
130 131 132 133 134

 public:
  MatMulComputeTester(const Place& place,
                      const std::string& alias,
                      const DDim& x_dims,
135 136 137 138
                      const DDim& y_dims,
                      bool x_transpose = false,
                      bool y_transpose = false,
                      float alpha = 1.f)
Y
Yan Chunwei 已提交
139
      : TestCase(place, alias),
140 141
        x_dims_(x_dims),
        y_dims_(y_dims),
Y
Yan Chunwei 已提交
142 143
        x_transpose_(x_transpose),
        y_transpose_(y_transpose),
144
        alpha_(alpha) {}
Y
Yan Chunwei 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

  void RunBaseline(Scope* scope) override {
    auto* x = scope->FindTensor(x_);
    auto* y = scope->FindTensor(y_);
    CHECK(x);
    CHECK(y);
    const auto* x_data = x->data<float>();
    const auto* y_data = y->data<float>();
    auto* out = scope->NewTensor(out_);
    CHECK(out);

    std::vector<int64_t> dim_out_vec;
    if (x_dims_.size() > 2 && y_dims_.size() >= 2) {
      // x: [B, ..., M, K], y: [B, ..., K, N], out: [B, ..., M, N]
      // x: [B, M, K], y: [K, N], out: [B, M, N]
      dim_out_vec.resize(x_dims_.size());
W
Wilber 已提交
161
      for (size_t i = 0; i < x_dims_.size() - 2; ++i) {
Y
Yan Chunwei 已提交
162 163
        dim_out_vec[i] = x_dims_[i];
      }
W
Wilber 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
      if (!x_transpose_ && !y_transpose_) {
        dim_out_vec[x_dims_.size() - 2] = x_dims_[x_dims_.size() - 2];
        dim_out_vec[x_dims_.size() - 1] = y_dims_[y_dims_.size() - 1];
      } else if (!x_transpose_ && y_transpose_) {
        dim_out_vec[x_dims_.size() - 2] = x_dims_[x_dims_.size() - 2];
        dim_out_vec[x_dims_.size() - 1] = y_dims_[y_dims_.size() - 2];
      } else if (x_transpose_ && !y_transpose_) {
        dim_out_vec[x_dims_.size() - 2] = x_dims_[x_dims_.size() - 1];
        dim_out_vec[x_dims_.size() - 1] = y_dims_[y_dims_.size() - 1];
      } else {
        dim_out_vec[x_dims_.size() - 2] = x_dims_[x_dims_.size() - 1];
        dim_out_vec[x_dims_.size() - 1] = y_dims_[y_dims_.size() - 2];
      }

Y
Yan Chunwei 已提交
178 179 180 181 182 183
      out->Resize(dim_out_vec);
      auto* out_data = out->mutable_data<float>();
      int x_inner = x_dims_[x_dims_.size() - 2] * x_dims_[x_dims_.size() - 1];

      if (y_dims_.size() > 2) {
        int y_inner = y_dims_[y_dims_.size() - 2] * y_dims_[y_dims_.size() - 1];
W
Wilber 已提交
184 185
        int o_inner =
            dim_out_vec[x_dims_.size() - 2] * dim_out_vec[x_dims_.size() - 1];
Y
Yan Chunwei 已提交
186 187 188 189 190 191 192 193 194 195 196 197
        for (size_t i = 0; i < x_dims_.count(0, x_dims_.size() - 2); ++i) {
          mul_low_efficiency(
              DDim({x_dims_[x_dims_.size() - 2], x_dims_[x_dims_.size() - 1]}),
              DDim({y_dims_[y_dims_.size() - 2], y_dims_[y_dims_.size() - 1]}),
              x_transpose_,
              y_transpose_,
              alpha_,
              x_data + i * x_inner,
              y_data + i * y_inner,
              out_data + i * o_inner);
        }
      } else {
W
Wilber 已提交
198 199
        int o_inner =
            dim_out_vec[x_dims_.size() - 2] * dim_out_vec[x_dims_.size() - 1];
Y
Yan Chunwei 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        for (size_t i = 0; i < x_dims_.count(0, x_dims_.size() - 2); ++i) {
          mul_low_efficiency(
              DDim({x_dims_[x_dims_.size() - 2], x_dims_[x_dims_.size() - 1]}),
              y_dims_,
              x_transpose_,
              y_transpose_,
              alpha_,
              x_data + i * x_inner,
              y_data,
              out_data + i * o_inner);
        }
      }
    } else if (x_dims_.size() == 2 && y_dims_.size() == 2) {
      // x: [M, K], y: [K, N], out: [M, N]
      dim_out_vec.resize(x_dims_.size());
      if (x_transpose_) {
        dim_out_vec[0] = x_dims_[1];
      } else {
        dim_out_vec[0] = x_dims_[0];
      }
      if (y_transpose_) {
        dim_out_vec[1] = y_dims_[0];
      } else {
        dim_out_vec[1] = y_dims_[1];
      }
      out->Resize(dim_out_vec);
      auto* out_data = out->mutable_data<float>();
      mul_low_efficiency(x_dims_,
                         y_dims_,
                         x_transpose_,
                         y_transpose_,
                         alpha_,
                         x_data,
                         y_data,
                         out_data);
    } else if (x_dims_.size() > 2 && y_dims_.size() == 1) {
      // x: [B, M, K], y: [K], out: [B, M]
      CHECK_EQ(x_dims_[x_dims_.size() - 1], y_dims_[0])
          << "not supported x_dims(" << x_dims_ << ") and y_dims(" << y_dims_
          << ")";
      dim_out_vec.resize(x_dims_.size() - 1);
      for (size_t i = 0; i < dim_out_vec.size(); ++i) {
        dim_out_vec[i] = x_dims_[i];
      }
      out->Resize(dim_out_vec);
      auto* out_data = out->mutable_data<float>();
      for (size_t i = 0; i < x_dims_.count(0, x_dims_.size() - 1); ++i) {
        out_data[i] = 0;
        for (size_t j = 0; j < y_dims_[0]; ++j) {
          out_data[i] += x_data[i * y_dims_[0] + j] * y_data[j] * alpha_;
        }
      }
W
Wilber 已提交
252
    } else if (x_dims_.size() == 1 && y_dims_.size() == 1) {
Y
Yan Chunwei 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      // x: [K], y: [K], out: [1]
      if (x_dims_[0] == y_dims_[0] && x_transpose_ == false &&
          y_transpose_ == false) {
        dim_out_vec.resize(1);
        dim_out_vec[0] = 1;

        out->Resize(dim_out_vec);
        auto* out_data = out->mutable_data<float>();
        out_data[0] = 0.f;
        for (size_t i = 0; i < x_dims_[0]; ++i) {
          out_data[0] += x_data[i] * y_data[i] * alpha_;
        }
      }
      // x: [M], y: [N], x_transpose: true, y_transpose: true, out: [M, N]
      if (x_transpose_ == true && y_transpose_ == true) {
        dim_out_vec.resize(2);
        dim_out_vec[0] = x_dims_[0];
        dim_out_vec[1] = y_dims_[0];
        out->Resize(dim_out_vec);
        auto* out_data = out->mutable_data<float>();
        mul_low_efficiency(DDim({x_dims_[0], 1}),
                           DDim({1, y_dims_[0]}),
                           false,
                           false,
                           alpha_,
                           x_data,
                           y_data,
                           out_data);
      }
    } else {
      LOG(FATAL) << "not supported x_dims(" << x_dims_ << ") and y_dims("
                 << y_dims_ << ")";
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType("matmul");
    op_desc->SetInput("X", {x_});
    op_desc->SetInput("Y", {y_});
    op_desc->SetOutput("Out", {out_});
    op_desc->SetAttr("transpose_X", x_transpose_);
    op_desc->SetAttr("transpose_Y", y_transpose_);
    op_desc->SetAttr("alpha", alpha_);
  }

  void PrepareData() override {
299 300 301
    std::vector<float> x(x_dims_.production());
    fill_data_rand(x.data(), -1.f, 1.f, x_dims_.production());
    SetCommonTensor(x_, x_dims_, x.data());
Y
Yan Chunwei 已提交
302

303 304 305
    std::vector<float> y(y_dims_.production());
    fill_data_rand(y.data(), -1.f, 1.f, y_dims_.production());
    SetCommonTensor(y_, y_dims_, y.data(), {}, true);
Y
Yan Chunwei 已提交
306 307 308
  }
};

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
void test_matmul_helper(Place place,
                        float abs_error,
                        std::vector<int64_t> x_dims,
                        std::vector<int64_t> y_dims,
                        bool x_transpose,
                        bool y_transpose,
                        float alpha) {
  std::unique_ptr<arena::TestCase> tester(new MatMulComputeTester(place,
                                                                  "def",
                                                                  DDim(x_dims),
                                                                  DDim(y_dims),
                                                                  x_transpose,
                                                                  y_transpose,
                                                                  alpha));
  arena::Arena arena(std::move(tester), place, abs_error);
  arena.TestPrecision();
}

void test_matmul2x2(Place place, float abs_error) {
  for (int64_t m : {1, 2, 8}) {
    for (int64_t k : {1, 3, 5}) {
      for (int64_t n : {1, 4, 6}) {
Y
Yan Chunwei 已提交
331
        for (float alpha : {1., 2.}) {
332 333
          test_matmul_helper(
              place, abs_error, {m, k}, {k, n}, false, false, alpha);
Y
Yan Chunwei 已提交
334 335 336 337 338 339
        }
      }
    }
  }
}

340 341 342 343
void test_matmul2x2_xtranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(place, abs_error, {3, 4}, {3, 2}, true, false, alpha);
    test_matmul_helper(place, abs_error, {2, 5}, {2, 1}, true, false, alpha);
W
Wilber 已提交
344 345 346
  }
}

347 348 349 350
void test_matmul2x2_ytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(place, abs_error, {5, 2}, {3, 2}, false, true, alpha);
    test_matmul_helper(place, abs_error, {2, 5}, {1, 5}, false, true, alpha);
W
Wilber 已提交
351 352 353
  }
}

354 355 356 357
void test_matmul2x2_xytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(place, abs_error, {6, 2}, {3, 6}, true, true, alpha);
    test_matmul_helper(place, abs_error, {5, 3}, {1, 5}, true, true, alpha);
W
Wilber 已提交
358
  }
Y
Yan Chunwei 已提交
359 360
}

361 362 363 364
void test_matmul1x1(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(place, abs_error, {3}, {3}, false, false, alpha);
  }
Y
Yan Chunwei 已提交
365 366
}

367 368 369 370
void test_matmul1x1_xytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(place, abs_error, {3}, {5}, true, true, alpha);
  }
Y
Yan Chunwei 已提交
371 372
}

373 374 375 376 377
void test_matmulnx1(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 2, 5}, {5}, false, false, alpha);
  }
Y
Yan Chunwei 已提交
378 379
}

380 381 382 383 384 385 386
void test_matmulnx2(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {1, 2, 2, 3}, {3, 1}, false, false, alpha);
    test_matmul_helper(
        place, abs_error, {1, 2, 2, 3}, {3, 4}, false, false, alpha);
  }
Y
Yan Chunwei 已提交
387 388
}

389 390 391 392 393 394
void test_matmulnx2_xtranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 6, 2}, {6, 2}, true, false, alpha);
    test_matmul_helper(
        place, abs_error, {5, 3, 5, 2}, {5, 1}, true, false, alpha);
W
Wilber 已提交
395 396 397
  }
}

398 399 400 401 402 403
void test_matmulnx2_ytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 6, 2}, {5, 2}, false, true, alpha);
    test_matmul_helper(
        place, abs_error, {5, 3, 5, 2}, {1, 2}, false, true, alpha);
W
Wilber 已提交
404 405 406
  }
}

407 408 409 410 411 412
void test_matmulnx2_xytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 4, 3}, {2, 4}, true, true, alpha);
    test_matmul_helper(
        place, abs_error, {5, 3, 3, 2}, {1, 3}, true, true, alpha);
W
Wilber 已提交
413 414 415
  }
}

416 417 418 419 420 421 422
void test_matmulnxn(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 6, 2}, {3, 4, 2, 5}, false, false, alpha);
    test_matmul_helper(
        place, abs_error, {5, 3, 4}, {5, 4, 6}, false, false, alpha);
  }
Y
Yan Chunwei 已提交
423 424
}

425 426 427 428 429 430
void test_matmulnxn_xtranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 2, 6}, {3, 4, 2, 5}, true, false, alpha);
    test_matmul_helper(
        place, abs_error, {5, 4, 2}, {5, 4, 6}, true, false, alpha);
W
Wilber 已提交
431 432 433
  }
}

434 435 436 437 438 439
void test_matmulnxn_ytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 6, 2}, {3, 4, 5, 2}, false, true, alpha);
    test_matmul_helper(
        place, abs_error, {5, 3, 4}, {5, 6, 4}, false, true, alpha);
W
Wilber 已提交
440 441 442
  }
}

443 444 445 446 447 448
void test_matmulnxn_xytranspose(Place place, float abs_error) {
  for (float alpha : {1.f, 2.f}) {
    test_matmul_helper(
        place, abs_error, {3, 4, 2, 6}, {3, 4, 5, 2}, true, true, alpha);
    test_matmul_helper(
        place, abs_error, {5, 4, 3}, {5, 6, 4}, true, true, alpha);
W
Wilber 已提交
449 450 451
  }
}

Y
Yan Chunwei 已提交
452
TEST(Matmul2x2, precision) {
453
  Place place;
454 455 456 457 458
  float abs_error = 2e-5;
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;  // use fp16 in npu
#elif defined(LITE_WITH_ARM)
459 460 461 462 463
  place = TARGET(kARM);
#elif defined(LITE_WITH_XPU)
  place = TARGET(kXPU);
#else
  return;
Y
Yan Chunwei 已提交
464
#endif
465

466
  test_matmul2x2(place, abs_error);
Y
Yan Chunwei 已提交
467 468
}

W
Wilber 已提交
469
TEST(Matmul2x2_x_transpose, precision) {
470 471 472 473 474 475 476 477 478
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;  // use fp16 in npu
#elif defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
W
Wilber 已提交
479
#endif
480 481

  test_matmul2x2_xtranspose(place, abs_error);
W
Wilber 已提交
482
}
483

W
Wilber 已提交
484
TEST(Matmul2x2_y_transpose, precision) {
485
  Place place;
486 487 488 489 490
  float abs_error = 2e-5;
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;  // use fp16 in npu
#elif defined(LITE_WITH_ARM)
491 492 493 494 495
  place = TARGET(kARM);
#elif defined(LITE_WITH_XPU)
  place = TARGET(kXPU);
#else
  return;
W
Wilber 已提交
496
#endif
497

498
  test_matmul2x2_ytranspose(place, abs_error);
W
Wilber 已提交
499 500 501
}

TEST(Matmul2x2_transpose, precision) {
502 503 504 505 506 507 508 509 510
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;  // use fp16 in npu
#elif defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
W
Wilber 已提交
511
#endif
512 513

  test_matmul2x2_xytranspose(place, abs_error);
W
Wilber 已提交
514 515
}

Y
Yan Chunwei 已提交
516
TEST(Matmul1x1, precision) {
517 518 519 520 521 522
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
Y
Yan Chunwei 已提交
523
#endif
524 525 526

  test_matmul1x1(place, abs_error);
  test_matmul1x1_xytranspose(place, abs_error);
Y
Yan Chunwei 已提交
527 528 529
}

TEST(Matmulnx1, precision) {
530 531 532 533 534 535
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
Y
Yan Chunwei 已提交
536
#endif
537 538

  test_matmulnx1(place, abs_error);
Y
Yan Chunwei 已提交
539 540 541
}

TEST(Matmulnx2, precision) {
542 543 544 545 546 547
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
Y
Yan Chunwei 已提交
548
#endif
549 550 551 552 553

  test_matmulnx2(place, abs_error);
  test_matmulnx2_xtranspose(place, abs_error);
  test_matmulnx2_ytranspose(place, abs_error);
  test_matmulnx2_xytranspose(place, abs_error);
Y
Yan Chunwei 已提交
554 555 556
}

TEST(Matmulnxn, precision) {
557 558 559 560 561 562 563 564 565
  Place place;
  float abs_error = 2e-5;
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 1e-2;  // use fp16 in npu
#elif defined(LITE_WITH_ARM)
  place = TARGET(kARM);
#else
  return;
Y
Yan Chunwei 已提交
566
#endif
567 568 569 570 571

  test_matmulnxn(place, abs_error);
  test_matmulnxn_xtranspose(place, abs_error);
  test_matmulnxn_ytranspose(place, abs_error);
  test_matmulnxn_xytranspose(place, abs_error);
Y
Yan Chunwei 已提交
572 573 574 575
}

}  // namespace lite
}  // namespace paddle