blas_impl.h 45.6 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2018 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.
#pragma once
15 16 17
#ifdef PADDLE_WITH_MKLML
#include <mkl.h>
#endif
S
ShenLiang 已提交
18
#include <algorithm>
T
tensor-tang 已提交
19
#include <cmath>
T
tensor-tang 已提交
20
#include <limits>
Y
Yu Yang 已提交
21
#include <vector>
22

Y
Yu Yang 已提交
23
#include "paddle/fluid/operators/math/math_function.h"
24
#include "paddle/fluid/platform/bfloat16.h"
25 26
#include "paddle/fluid/platform/complex128.h"
#include "paddle/fluid/platform/complex64.h"
Y
Yu Yang 已提交
27 28 29 30 31 32 33 34

namespace paddle {
namespace operators {
namespace math {

template <typename T>
struct CBlas;

35 36 37 38
template <>
struct CBlas<int8_t> {
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
39 40
    PADDLE_THROW(platform::errors::Unimplemented(
        "Blas VCOPY do not supported on CPU, please check your code"));
41 42 43
  }
};

44 45 46 47 48 49 50 51 52 53
template <>
struct CBlas<platform::bfloat16> {
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "Blas VCOPY do not supported on CPU with bfloat16,"
        " please check your code"));
  }
};

54
#ifdef PADDLE_WITH_MKLML
Y
Yu Yang 已提交
55 56
template <>
struct CBlas<float> {
Y
Yu Yang 已提交
57 58
  template <typename... ARGS>
  static void GEMM(ARGS... args) {
59
    platform::dynload::cblas_sgemm(args...);
Y
Yu Yang 已提交
60
  }
Y
Yu Yang 已提交
61

T
tensor-tang 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  template <typename... ARGS>
  static float *GEMM_ALLOC(ARGS... args) {
    return platform::dynload::cblas_sgemm_alloc(args...);
  }

  template <typename... ARGS>
  static void GEMM_PACK(ARGS... args) {
    platform::dynload::cblas_sgemm_pack(args...);
  }

  template <typename... ARGS>
  static void GEMM_COMPUTE(ARGS... args) {
    platform::dynload::cblas_sgemm_compute(args...);
  }

  template <typename... ARGS>
  static void GEMM_FREE(ARGS... args) {
    platform::dynload::cblas_sgemm_free(args...);
  }

T
tensor-tang 已提交
82 83 84 85 86 87
#ifdef PADDLE_WITH_LIBXSMM
  template <typename... ARGS>
  static void SMM_GEMM(ARGS... args) {
    libxsmm_sgemm(args...);
  }
#endif
T
tensor-tang 已提交
88

Y
Yu Yang 已提交
89 90
  template <typename... ARGS>
  static void AXPY(ARGS... args) {
91 92 93 94 95 96 97 98 99 100 101 102 103
    platform::dynload::cblas_saxpy(args...);
  }

  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    platform::dynload::cblas_scopy(args...);
  }

  template <typename... ARGS>
  static void GEMV(ARGS... args) {
    platform::dynload::cblas_sgemv(args...);
  }

T
tensor-tang 已提交
104 105 106 107 108
  template <typename... ARGS>
  static float DOT(ARGS... args) {
    return platform::dynload::cblas_sdot(args...);
  }

T
tensor-tang 已提交
109 110 111 112 113
  template <typename... ARGS>
  static void SCAL(ARGS... args) {
    platform::dynload::cblas_sscal(args...);
  }

J
Jacek Czaja 已提交
114 115 116 117 118
  template <typename... ARGS>
  static float ASUM(ARGS... args) {
    return platform::dynload::cblas_sasum(args...);
  }

119 120 121
  template <typename... ARGS>
  static void GEMM_BATCH(ARGS... args) {
    platform::dynload::cblas_sgemm_batch(args...);
Y
Yu Yang 已提交
122 123
  }

124 125
  template <typename... ARGS>
  static void VADD(ARGS... args) {
126 127
    platform::dynload::vsAdd(args...);
  }
T
tensor-tang 已提交
128

129 130 131 132 133
  template <typename... ARGS>
  static void VSUB(ARGS... args) {
    platform::dynload::vsSub(args...);
  }

T
tensor-tang 已提交
134 135 136 137
  template <typename... ARGS>
  static void VMUL(ARGS... args) {
    platform::dynload::vsMul(args...);
  }
T
tensor-tang 已提交
138

139 140 141 142 143
  template <typename... ARGS>
  static void VDIV(ARGS... args) {
    platform::dynload::vsDiv(args...);
  }

T
tensor-tang 已提交
144 145 146 147
  template <typename... ARGS>
  static void VEXP(ARGS... args) {
    platform::dynload::vsExp(args...);
  }
T
tensor-tang 已提交
148 149

  template <typename... ARGS>
T
tensor-tang 已提交
150
  static void VSQUARE(ARGS... args) {
T
tensor-tang 已提交
151 152 153 154 155 156 157
    platform::dynload::vsSqr(args...);
  }

  template <typename... ARGS>
  static void VPOW(ARGS... args) {
    platform::dynload::vsPowx(args...);
  }
Y
Use mkl  
Yu Yang 已提交
158 159 160 161 162

  template <typename... ARGS>
  static void VINV(ARGS... args) {
    platform::dynload::vsInv(args...);
  }
Y
Yihua Xu 已提交
163 164 165 166 167

  template <typename... ARGS>
  static void VMERF(ARGS... args) {
    platform::dynload::vmsErf(args...);
  }
168
#if !defined(_WIN32)
169 170 171 172
  template <typename... ARGS>
  static void CSRMM(ARGS... args) {
    platform::dynload::mkl_scsrmm(args...);
  }
173
#endif
G
Guo Sheng 已提交
174 175 176 177 178

  template <typename... ARGS>
  static void TRSM(ARGS... args) {
    platform::dynload::cblas_strsm(args...);
  }
179 180 181 182 183 184 185 186 187
};

template <>
struct CBlas<double> {
  template <typename... ARGS>
  static void GEMM(ARGS... args) {
    platform::dynload::cblas_dgemm(args...);
  }

T
tensor-tang 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  template <typename... ARGS>
  static double *GEMM_ALLOC(ARGS... args) {
    return platform::dynload::cblas_dgemm_alloc(args...);
  }

  template <typename... ARGS>
  static void GEMM_PACK(ARGS... args) {
    platform::dynload::cblas_dgemm_pack(args...);
  }

  template <typename... ARGS>
  static void GEMM_COMPUTE(ARGS... args) {
    platform::dynload::cblas_dgemm_compute(args...);
  }

  template <typename... ARGS>
  static void GEMM_FREE(ARGS... args) {
    platform::dynload::cblas_dgemm_free(args...);
  }

T
tensor-tang 已提交
208 209 210 211 212 213
#ifdef PADDLE_WITH_LIBXSMM
  template <typename... ARGS>
  static void SMM_GEMM(ARGS... args) {
    libxsmm_dgemm(args...);
  }
#endif
T
tensor-tang 已提交
214

215 216 217
  template <typename... ARGS>
  static void AXPY(ARGS... args) {
    platform::dynload::cblas_daxpy(args...);
218 219 220 221
  }

  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
222
    platform::dynload::cblas_dcopy(args...);
223 224
  }

Y
Yu Yang 已提交
225 226
  template <typename... ARGS>
  static void GEMV(ARGS... args) {
227
    platform::dynload::cblas_dgemv(args...);
Y
Yu Yang 已提交
228 229
  }

T
tensor-tang 已提交
230 231 232 233 234
  template <typename... ARGS>
  static double DOT(ARGS... args) {
    return platform::dynload::cblas_ddot(args...);
  }

T
tensor-tang 已提交
235 236 237 238 239
  template <typename... ARGS>
  static void SCAL(ARGS... args) {
    platform::dynload::cblas_dscal(args...);
  }

J
Jacek Czaja 已提交
240 241 242 243 244
  template <typename... ARGS>
  static double ASUM(ARGS... args) {
    return platform::dynload::cblas_dasum(args...);
  }

Y
Yu Yang 已提交
245 246
  template <typename... ARGS>
  static void GEMM_BATCH(ARGS... args) {
247 248 249 250 251 252 253
    platform::dynload::cblas_dgemm_batch(args...);
  }

  template <typename... ARGS>
  static void VADD(ARGS... args) {
    platform::dynload::vdAdd(args...);
  }
T
tensor-tang 已提交
254

255 256 257 258 259
  template <typename... ARGS>
  static void VSUB(ARGS... args) {
    platform::dynload::vdSub(args...);
  }

T
tensor-tang 已提交
260 261 262 263
  template <typename... ARGS>
  static void VMUL(ARGS... args) {
    platform::dynload::vdMul(args...);
  }
T
tensor-tang 已提交
264

265 266 267 268 269
  template <typename... ARGS>
  static void VDIV(ARGS... args) {
    platform::dynload::vdDiv(args...);
  }

T
tensor-tang 已提交
270 271 272 273
  template <typename... ARGS>
  static void VEXP(ARGS... args) {
    platform::dynload::vdExp(args...);
  }
T
tensor-tang 已提交
274 275

  template <typename... ARGS>
T
tensor-tang 已提交
276
  static void VSQUARE(ARGS... args) {
T
tensor-tang 已提交
277 278 279 280 281 282 283
    platform::dynload::vdSqr(args...);
  }

  template <typename... ARGS>
  static void VPOW(ARGS... args) {
    platform::dynload::vdPowx(args...);
  }
Y
Use mkl  
Yu Yang 已提交
284 285 286 287 288

  template <typename... ARGS>
  static void VINV(ARGS... args) {
    platform::dynload::vdInv(args...);
  }
Y
Yihua Xu 已提交
289 290 291 292 293

  template <typename... ARGS>
  static void VMERF(ARGS... args) {
    platform::dynload::vmdErf(args...);
  }
294
#if !defined(_WIN32)
295 296 297 298
  template <typename... ARGS>
  static void CSRMM(ARGS... args) {
    platform::dynload::mkl_dcsrmm(args...);
  }
299
#endif
G
Guo Sheng 已提交
300 301 302 303 304

  template <typename... ARGS>
  static void TRSM(ARGS... args) {
    platform::dynload::cblas_dtrsm(args...);
  }
305 306
};

307 308
template <>
struct CBlas<platform::complex64> {
309 310 311 312 313 314 315
  template <typename... ARGS>
  static void AXPY(int n, const paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *X, const int incX,
                   paddle::platform::complex64 *Y, const int incY) {
    platform::dynload::cblas_caxpy(n, &alpha, X, incX, Y, incY);
  }

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    platform::dynload::cblas_ccopy(args...);
  }

  // the libmklml_intel.so paddle used has no vcAdd, vcSub,
  // vcMul, vcDiv apis before rebuild from source
  // so replace with the raw operator methods
  /*
  template <typename... ARGS>
  static void VADD(ARGS... args) {
    platform::dynload::vcAdd(args...);
  }

  template <typename... ARGS>
  static void VSUB(ARGS... args) {
    platform::dynload::vcSub(args...);
  }

  template <typename... ARGS>
  static void VMUL(ARGS... args) {
    platform::dynload::vcMul(args...);
  }

  template <typename... ARGS>
  static void VDIV(ARGS... args) {
    platform::dynload::vcDiv(args...);
  }
  */

  template <typename... ARGS>
  static void VADD(int n, const paddle::platform::complex64 *a,
                   const paddle::platform::complex64 *b,
                   paddle::platform::complex64 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] + b[i];
    }
  }

  template <typename... ARGS>
  static void VSUB(int n, const paddle::platform::complex64 *a,
                   const paddle::platform::complex64 *b,
                   paddle::platform::complex64 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] - b[i];
    }
  }

  template <typename... ARGS>
  static void VMUL(int n, const paddle::platform::complex64 *a,
                   const paddle::platform::complex64 *b,
                   paddle::platform::complex64 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] * b[i];
    }
  }
  template <typename... ARGS>
  static void VDIV(int n, const paddle::platform::complex64 *a,
                   const paddle::platform::complex64 *b,
                   paddle::platform::complex64 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] / b[i];
    }
  }

  template <typename... ARGS>
  static void GEMV(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE trans, int M, int N,
                   paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *A, int lda,
                   const paddle::platform::complex64 *X, int incx,
                   paddle::platform::complex64 beta,
                   paddle::platform::complex64 *Y, int incy) {
    const void *a_ = (const void *)(A);
    const void *x_ = (const void *)(X);
    void *y_ = static_cast<void *>(Y);
    platform::dynload::cblas_cgemv(layout, trans, M, N, &alpha, a_, lda, x_,
                                   incx, &beta, y_, incy);
  }

  template <typename... ARGS>
  static void GEMM(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE trans_a,
                   CBLAS_TRANSPOSE trans_b, int M, int N, int K,
                   paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *A, int lda,
                   const paddle::platform::complex64 *B, int ldb,
                   paddle::platform::complex64 beta,
                   paddle::platform::complex64 *C, int ldc) {
    const void *a_ = (const void *)(A);
    const void *b_ = (const void *)(B);
    void *c_ = static_cast<void *>(C);
    platform::dynload::cblas_cgemm(layout, trans_a, trans_b, M, N, K, &alpha,
                                   a_, lda, b_, ldb, &beta, c_, ldc);
  }

  template <typename... ARGS>
  static void GEMM_BATCH(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE *trans_a,
                         CBLAS_TRANSPOSE *trans_b, int *M, int *N, int *K,
                         paddle::platform::complex64 *alpha,
                         const paddle::platform::complex64 **A, const int *lda,
                         const paddle::platform::complex64 **B, const int *ldb,
                         paddle::platform::complex64 *beta,
                         paddle::platform::complex64 **C, const int *ldc,
                         int group_count, int *group_size) {
    const void **A_void = (const void **)(&(*A));
    const void **B_void = (const void **)(&(*B));
    void **C_void = reinterpret_cast<void **>(C);

    platform::dynload::cblas_cgemm_batch(layout, trans_a, trans_b, M, N, K,
                                         alpha, A_void, lda, B_void, ldb, beta,
                                         C_void, ldc, group_count, group_size);
  }

  template <typename... ARGS>
  static void GEMM_EX(ARGS... args) {
    platform::dynload::cblas_cgemm_batch(args...);
  }
};

template <>
struct CBlas<platform::complex128> {
436 437 438 439 440 441 442
  template <typename... ARGS>
  static void AXPY(int n, const paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *X, const int incX,
                   paddle::platform::complex128 *Y, const int incY) {
    platform::dynload::cblas_zaxpy(n, &alpha, X, incX, Y, incY);
  }

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    platform::dynload::cblas_zcopy(args...);
  }

  // the libmklml_intel.so paddle used has no vzAdd, vzSub,
  // vzMul, vzDiv apis before rebuild from source
  // so replace with the raw operator methods
  /*
  template <typename... ARGS>
  static void VADD(ARGS... args) {
    platform::dynload::vzAdd(args...);
  }

  template <typename... ARGS>
  static void VSUB(ARGS... args) {
    platform::dynload::vzSub(args...);
  }

  template <typename... ARGS>
  static void VMUL(ARGS... args) {
    platform::dynload::vzMul(args...);
  }

  template <typename... ARGS>
  static void VDIV(ARGS... args) {
    platform::dynload::vzDiv(args...);
  }
  */

  template <typename... ARGS>
  static void VADD(int n, const paddle::platform::complex128 *a,
                   const paddle::platform::complex128 *b,
                   paddle::platform::complex128 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] + b[i];
    }
  }

  template <typename... ARGS>
  static void VSUB(int n, const paddle::platform::complex128 *a,
                   const paddle::platform::complex128 *b,
                   paddle::platform::complex128 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] - b[i];
    }
  }

  template <typename... ARGS>
  static void VMUL(int n, const paddle::platform::complex128 *a,
                   const paddle::platform::complex128 *b,
                   paddle::platform::complex128 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] * b[i];
    }
  }
  template <typename... ARGS>
  static void VDIV(int n, const paddle::platform::complex128 *a,
                   const paddle::platform::complex128 *b,
                   paddle::platform::complex128 *y) {
    for (int i = 0; i < n; ++i) {
      y[i] = a[i] / b[i];
    }
  }

  template <typename... ARGS>
  static void GEMV(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE trans, int M, int N,
                   paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *A, int lda,
                   const paddle::platform::complex128 *X, int incx,
                   paddle::platform::complex128 beta,
                   paddle::platform::complex128 *Y, int incy) {
    const void *a_ = (const void *)(A);
    const void *x_ = (const void *)(X);
    void *y_ = static_cast<void *>(Y);
    platform::dynload::cblas_zgemv(layout, trans, M, N, &alpha, a_, lda, x_,
                                   incx, &beta, y_, incy);
  }

  template <typename... ARGS>
  static void GEMM(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE trans_a,
                   CBLAS_TRANSPOSE trans_b, int M, int N, int K,
                   paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *A, int lda,
                   const paddle::platform::complex128 *B, int ldb,
                   paddle::platform::complex128 beta,
                   paddle::platform::complex128 *C, int ldc) {
    const void *a_ = (const void *)(A);
    const void *b_ = (const void *)(B);
    void *c_ = static_cast<void *>(C);
    platform::dynload::cblas_zgemm(layout, trans_a, trans_b, M, N, K, &alpha,
                                   a_, lda, b_, ldb, &beta, c_, ldc);
  }

  template <typename... ARGS>
  static void GEMM_BATCH(CBLAS_LAYOUT layout, CBLAS_TRANSPOSE *trans_a,
                         CBLAS_TRANSPOSE *trans_b, int *M, int *N, int *K,
                         paddle::platform::complex128 *alpha,
                         const paddle::platform::complex128 **A, const int *lda,
                         const paddle::platform::complex128 **B, const int *ldb,
                         paddle::platform::complex128 *beta,
                         paddle::platform::complex128 **C, const int *ldc,
                         int group_count, int *group_size) {
    const void **A_void = (const void **)(&(*A));
    const void **B_void = (const void **)(&(*B));
    void **C_void = reinterpret_cast<void **>(C);

    platform::dynload::cblas_zgemm_batch(layout, trans_a, trans_b, M, N, K,
                                         alpha, A_void, lda, B_void, ldb, beta,
                                         C_void, ldc, group_count, group_size);
  }

  template <typename... ARGS>
  static void GEMM_EX(ARGS... args) {
    platform::dynload::cblas_zgemm_batch(args...);
  }
};

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
#else

template <>
struct CBlas<float> {
  template <typename... ARGS>
  static void GEMM(ARGS... args) {
    cblas_sgemm(args...);
  }

  template <typename... ARGS>
  static void AXPY(ARGS... args) {
    cblas_saxpy(args...);
  }

  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    cblas_scopy(args...);
  }

  template <typename... ARGS>
  static void GEMV(ARGS... args) {
    cblas_sgemv(args...);
Y
Yu Yang 已提交
583
  }
G
Guo Sheng 已提交
584 585 586 587 588

  template <typename... ARGS>
  static void TRSM(ARGS... args) {
    cblas_strsm(args...);
  }
Y
Yu Yang 已提交
589 590 591 592
};

template <>
struct CBlas<double> {
Y
Yu Yang 已提交
593 594 595 596
  template <typename... ARGS>
  static void GEMM(ARGS... args) {
    cblas_dgemm(args...);
  }
Y
Yu Yang 已提交
597 598 599 600 601 602

  template <typename... ARGS>
  static void AXPY(ARGS... args) {
    cblas_daxpy(args...);
  }

603 604 605 606 607
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    cblas_dcopy(args...);
  }

Y
Yu Yang 已提交
608 609 610 611
  template <typename... ARGS>
  static void GEMV(ARGS... args) {
    cblas_dgemv(args...);
  }
G
Guo Sheng 已提交
612 613 614 615 616

  template <typename... ARGS>
  static void TRSM(ARGS... args) {
    cblas_dtrsm(args...);
  }
Y
Yu Yang 已提交
617
};
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

template <>
struct CBlas<platform::complex64> {
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    cblas_ccopy(args...);
  }

  template <typename... ARGS>
  static void AXPY(int n, const paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *X, const int incX,
                   paddle::platform::complex64 *Y, const int incY) {
    cblas_caxpy(n, &alpha, X, incX, Y, incY);
  }

  template <typename... ARGS>
  static void GEMV(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE TransA,
                   const int M, const int N,
                   const paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *A, const int lda,
                   const paddle::platform::complex64 *X, const int incX,
                   const paddle::platform::complex64 beta,
                   paddle::platform::complex64 *Y, const int incY) {
    cblas_cgemv(layout, TransA, M, N, &alpha, A, lda, X, incX, &beta, Y, incY);
  }

  template <typename... ARGS>
  static void GEMM(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE TransA,
                   const CBLAS_TRANSPOSE TransB, const int M, const int N,
                   const int K, const paddle::platform::complex64 alpha,
                   const paddle::platform::complex64 *A, const int lda,
                   const paddle::platform::complex64 *B, const int ldb,
                   const paddle::platform::complex64 beta,
                   paddle::platform::complex64 *C, const int ldc) {
    cblas_cgemm(layout, TransA, TransB, M, N, K, &alpha, A, lda, B, ldb, &beta,
                C, ldc);
  }
};

template <>
struct CBlas<platform::complex128> {
  template <typename... ARGS>
  static void VCOPY(ARGS... args) {
    cblas_zcopy(args...);
  }

  template <typename... ARGS>
  static void AXPY(int n, const paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *X, const int incX,
                   paddle::platform::complex128 *Y, const int incY) {
    cblas_zaxpy(n, &alpha, X, incX, Y, incY);
  }

  template <typename... ARGS>
  static void GEMV(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE TransA,
                   const int M, const int N,
                   const paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *A, const int lda,
                   const paddle::platform::complex128 *X, const int incX,
                   const paddle::platform::complex128 beta,
                   paddle::platform::complex128 *Y, const int incY) {
    cblas_zgemv(layout, TransA, M, N, &alpha, A, lda, X, incX, &beta, Y, incY);
  }

  template <typename... ARGS>
  static void GEMM(const CBLAS_LAYOUT layout, const CBLAS_TRANSPOSE TransA,
                   const CBLAS_TRANSPOSE TransB, const int M, const int N,
                   const int K, const paddle::platform::complex128 alpha,
                   const paddle::platform::complex128 *A, const int lda,
                   const paddle::platform::complex128 *B, const int ldb,
                   const paddle::platform::complex128 beta,
                   paddle::platform::complex128 *C, const int ldc) {
    cblas_zgemm(layout, TransA, TransB, M, N, K, &alpha, A, lda, B, ldb, &beta,
                C, ldc);
  }
};

695
#endif
T
tensor-tang 已提交
696

Y
Yu Yang 已提交
697 698
template <>
struct CBlas<platform::float16> {
699 700 701 702 703
  static void GEMM(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 GEMM not supported on CPU, please check your code"));
  }

T
tensor-tang 已提交
704
  static void SMM_GEMM(...) {
705 706
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 SMM_GEMM not supported on CPU, please check your code"));
T
tensor-tang 已提交
707
  }
708 709 710
  static void VMUL(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 VMUL not supported on CPU, please check your code"));
T
tensor-tang 已提交
711
  }
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  static void VEXP(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 VEXP not supported on CPU, please check your code"));
  }
  static void VSQUARE(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 VSQUARE not supported on CPU, please check your code"));
  }
  static void VPOW(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 VPOW not supported on CPU, please check your code"));
  }
  static void DOT(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 DOT not supported on CPU, please check your code"));
  };
  static void SCAL(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 SCAL not supported on CPU, please check your code"));
  };
  static void ASUM(...) {
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 ASUM not supported on CPU, please check your code"));
  };
Y
Yu Yang 已提交
736 737
#ifdef PADDLE_WITH_MKLML
  static void GEMM_BATCH(...) {
738 739
    PADDLE_THROW(platform::errors::Unimplemented(
        "float16 GEMM_BATCH not supported on CPU, please check your code"));
Y
Yu Yang 已提交
740 741
  }
#endif
Y
Yu Yang 已提交
742
};
T
tensor-tang 已提交
743

T
tensor-tang 已提交
744
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
template <>
template <typename T>
T *Blas<platform::CPUDeviceContext>::GEMM_ALLOC(const CBLAS_IDENTIFIER id,
                                                const int M, const int N,
                                                const int K) const {
  return CBlas<T>::GEMM_ALLOC(id, M, N, K);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMM_PACK(const CBLAS_IDENTIFIER id,
                                                 const CBLAS_TRANSPOSE trans,
                                                 int M, int N, int K,
                                                 const T alpha, const T *src,
                                                 const int ld, T *dst) const {
  CBlas<T>::GEMM_PACK(CblasRowMajor, id, trans, M, N, K, alpha, src, ld, dst);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMM_COMPUTE(
    int transA, int transB, int M, int N, int K, const T *A, const int lda,
    const T *B, const int ldb, T beta, T *C, const int ldc) const {
  CBlas<T>::GEMM_COMPUTE(CblasRowMajor, transA, transB, M, N, K, A, lda, B, ldb,
                         beta, C, ldc);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMM_FREE(T *data) const {
  CBlas<T>::GEMM_FREE(data);
}
T
tensor-tang 已提交
777
#endif
T
tensor-tang 已提交
778

T
tensor-tang 已提交
779 780 781 782 783 784 785 786 787
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMM(CBLAS_TRANSPOSE transA,
                                            CBLAS_TRANSPOSE transB, int M,
                                            int N, int K, T alpha, const T *A,
                                            const T *B, T beta, T *C) const {
  int lda = (transA == CblasNoTrans) ? K : M;
  int ldb = (transB == CblasNoTrans) ? N : K;
  int ldc = N;
T
tensor-tang 已提交
788 789
  CBlas<T>::GEMM(CblasRowMajor, transA, transB, M, N, K, alpha, A, lda, B, ldb,
                 beta, C, ldc);
Y
Yu Yang 已提交
790 791 792 793
}

template <>
template <typename T>
Y
Yu Yang 已提交
794 795 796 797
void Blas<platform::CPUDeviceContext>::GEMM(bool transA, bool transB, int M,
                                            int N, int K, T alpha, const T *A,
                                            int lda, const T *B, int ldb,
                                            T beta, T *C, int ldc) const {
T
tensor-tang 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811
  CBlas<T>::GEMM(CblasRowMajor, transA == false ? CblasNoTrans : CblasTrans,
                 transB == false ? CblasNoTrans : CblasTrans, M, N, K, alpha, A,
                 lda, B, ldb, beta, C, ldc);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMM(CBLAS_TRANSPOSE transA,
                                            CBLAS_TRANSPOSE transB, int M,
                                            int N, int K, T alpha, const T *A,
                                            int lda, const T *B, int ldb,
                                            T beta, T *C, int ldc) const {
  CBlas<T>::GEMM(CblasRowMajor, transA, transB, M, N, K, alpha, A, lda, B, ldb,
                 beta, C, ldc);
Y
Yu Yang 已提交
812 813
}

Y
Yu Yang 已提交
814 815 816 817 818 819 820 821 822
template <typename DeviceContext>
template <typename T>
void Blas<DeviceContext>::MatMul(const framework::Tensor &mat_a, bool trans_a,
                                 const framework::Tensor &mat_b, bool trans_b,
                                 T alpha, framework::Tensor *mat_out,
                                 T beta) const {
  auto dim_a = mat_a.dims();
  auto dim_b = mat_b.dims();
  auto dim_out = mat_out->dims();
823 824 825 826 827 828 829 830 831 832 833 834
  PADDLE_ENFORCE_EQ(
      dim_a.size() == 2 && dim_b.size() == 2 && dim_out.size() == 2, true,
      platform::errors::InvalidArgument(
          "The input and output of matmul should be matrix, the dim size must "
          "be 2,"
          "but received dim size input_a:%d, input_b:%d, output:%d",
          dim_a.size(), dim_b.size(), dim_out.size()));
  PADDLE_ENFORCE_EQ(
      mat_a.place() == mat_b.place() && mat_a.place() == mat_out->place(), true,
      platform::errors::InvalidArgument("The places of matrices in the matmul "
                                        "should be same, please check your "
                                        "code."));
Y
Yu Yang 已提交
835 836 837 838 839 840 841 842 843 844 845 846

  int M = dim_out[0];
  int N = dim_out[1];
  int K = !trans_a ? dim_a[1] : dim_a[0];

  CBLAS_TRANSPOSE transA = !trans_a ? CblasNoTrans : CblasTrans;
  CBLAS_TRANSPOSE transB = !trans_b ? CblasNoTrans : CblasTrans;

  this->GEMM(transA, transB, M, N, K, alpha, mat_a.data<T>(), mat_b.data<T>(),
             beta, mat_out->data<T>());
}

Y
Yu Yang 已提交
847 848 849 850 851 852 853
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::AXPY(int n, T alpha, const T *x,
                                            T *y) const {
  CBlas<T>::AXPY(n, alpha, x, 1, y, 1);
}

854 855 856 857 858 859 860 861 862 863 864 865 866
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VCOPY(int n, const T *x, T *y) const {
  CBlas<T>::VCOPY(n, x, 1, y, 1);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VADD(int n, const T *x, const T *y,
                                            T *z) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VADD(n, x, y, z);
#else
867
  if (x == z) {
868
    this->template AXPY<T>(n, (T)(1.), y, z);
869 870
  } else {
    this->template VCOPY<T>(n, y, z);
871
    this->template AXPY<T>(n, (T)(1.), x, z);
872
  }
873 874 875
#endif
}

876 877 878 879 880 881 882 883 884 885 886 887 888 889
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VSUB(int n, const T *x, const T *y,
                                            T *z) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VSUB(n, x, y, z);
#else
  // try to find if openblas support vsub
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] - y[i];
  }
#endif
}

T
tensor-tang 已提交
890 891 892 893 894 895 896 897 898 899
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VMUL(int n, const T *x, const T *y,
                                            T *z) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VMUL(n, x, y, z);
#else
  // try to find if openblas support vmul
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] * y[i];
900 901 902 903 904 905 906 907 908 909 910 911 912 913
  }
#endif
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VDIV(int n, const T *x, const T *y,
                                            T *z) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VDIV(n, x, y, z);
#else
  // try to find if openblas support vdiv
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] / y[i];
T
tensor-tang 已提交
914 915 916 917
  }
#endif
}

T
tensor-tang 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VEXP(int n, const T *x, T *y) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VEXP(n, x, y);
#else
  // try to find if openblas support vexp
  for (int i = 0; i < n; ++i) {
    y[i] = std::exp(x[i]);
  }
#endif
}

T
tensor-tang 已提交
931 932
template <>
template <typename T>
T
tensor-tang 已提交
933
void Blas<platform::CPUDeviceContext>::VSQUARE(int n, const T *x, T *y) const {
T
tensor-tang 已提交
934
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
935
  CBlas<T>::VSQUARE(n, x, y);
T
tensor-tang 已提交
936 937
#else
  for (int i = 0; i < n; ++i) {
T
tensor-tang 已提交
938
    y[i] = x[i] * x[i];
T
tensor-tang 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
  }
#endif
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VPOW(int n, const T *x, T a,
                                            T *y) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VPOW(n, x, a, y);
#else
  for (int i = 0; i < n; ++i) {
    y[i] = std::pow(x[i], a);
  }
#endif
}

T
tensor-tang 已提交
956 957 958 959
template <>
template <typename T>
T Blas<platform::CPUDeviceContext>::DOT(int n, const T *x, const T *y) const {
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
960
  return CBlas<T>::DOT(n, x, 1, y, 1);
T
tensor-tang 已提交
961 962 963 964 965 966 967 968 969 970
#else
  // try to find if openblas support cblas_dot
  T sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += x[i] * y[i];
  }
  return sum;
#endif
}

T
tensor-tang 已提交
971 972
template <>
template <typename T>
T
tensor-tang 已提交
973
void Blas<platform::CPUDeviceContext>::SCAL(int n, const T a, T *x) const {
T
tensor-tang 已提交
974 975 976 977 978 979 980 981 982 983
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::SCAL(n, a, x, 1);
#else
  // try to find if openblas support cblas_scal
  for (int i = 0; i < n; ++i) {
    x[i] = a * x[i];
  }
#endif
}

J
Jacek Czaja 已提交
984 985 986 987 988
template <>
template <typename T>
T Blas<platform::CPUDeviceContext>::ASUM(int n, T *x, int inc) const {
  auto sum = static_cast<T>(0.0);
#ifdef PADDLE_WITH_MKLML
989
  sum = CBlas<T>::ASUM(n, x, inc);
J
Jacek Czaja 已提交
990
#else
J
Jacek Czaja 已提交
991
  // TODO(jczaja): check if openblas does provide cblas_sasum/cblas_dasum
J
Jacek Czaja 已提交
992 993 994 995 996 997 998
  for (int c = 0; c < n; ++c) {
    sum += x[c];
  }
#endif
  return sum;
}

Y
Yu Yang 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::GEMV(bool trans_a, int M, int N, T alpha,
                                            const T *A, const T *B, T beta,
                                            T *C) const {
  CBLAS_TRANSPOSE transA = !trans_a ? CblasNoTrans : CblasTrans;
  CBlas<T>::GEMV(CblasRowMajor, transA, M, N, alpha, A, N, B, 1, beta, C, 1);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::BatchedGEMM(
    CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB, int M, int N, int K,
    T alpha, const T *A, const T *B, T beta, T *C, int batchCount,
    int64_t strideA, int64_t strideB) const {
#ifdef PADDLE_WITH_MKLML
  int lda = (transA == CblasNoTrans) ? K : M;
  int ldb = (transB == CblasNoTrans) ? N : K;
  int ldc = N;
  auto a_array = std::vector<const T *>(batchCount);
  auto b_array = std::vector<const T *>(batchCount);
  auto c_array = std::vector<T *>(batchCount);
  for (int k = 0; k < batchCount; ++k) {
    a_array[k] = &A[k * strideA];
    b_array[k] = &B[k * strideB];
    c_array[k] = &C[k * M * N];
  }

  CBlas<T>::GEMM_BATCH(CblasRowMajor, &transA, &transB, &M, &N, &K, &alpha,
                       a_array.data(), &lda, b_array.data(), &ldb, &beta,
                       c_array.data(), &ldc, 1 /* group_count */, &batchCount);
#else
  for (int k = 0; k < batchCount; ++k) {
Y
yuyang18 已提交
1032 1033 1034
    auto *Ak = &A[k * strideA];
    auto *Bk = &B[k * strideB];
    auto *Ck = &C[k * M * N];
Y
Yu Yang 已提交
1035 1036 1037 1038 1039
    this->template GEMM<T>(transA, transB, M, N, K, alpha, Ak, Bk, beta, Ck);
  }
#endif
}

S
ShenLiang 已提交
1040 1041 1042 1043 1044 1045
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::BatchedGEMM(
    CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB, int M, int N, int K,
    T alpha, const T **A, const T **B, T beta, T **C, int batchCount) const {
#ifdef PADDLE_WITH_MKLML
W
wanghuancoder 已提交
1046 1047 1048
  const int lda = (std::max)((transA == CblasNoTrans) ? K : M, 1);
  const int ldb = (std::max)((transB == CblasNoTrans) ? N : K, 1);
  const int ldc = (std::max)(N, 1);
S
ShenLiang 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
  CBlas<T>::GEMM_BATCH(CblasRowMajor, &transA, &transB, &M, &N, &K, &alpha, A,
                       &lda, B, &ldb, &beta, C, &ldc, 1 /* group_count */,
                       &batchCount);
#else
  for (int k = 0; k < batchCount; ++k) {
    this->template GEMM<T>(transA, transB, M, N, K, alpha, A[k], B[k], beta,
                           C[k]);
  }
#endif
}

1060 1061
#if defined(PADDLE_WITH_MKLML) && !defined(PADDLE_WITH_CUDA) && \
    !defined(PADDLE_WITH_HIP)  // @{ Group Blas MKLML: BatchedGEMMWithHead
1062 1063 1064
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::BatchedGEMMWithHead(
1065 1066 1067 1068 1069 1070
    CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB, int W1, int H1, int W2,
    int H2, T alpha, const T *A, const T *B, T beta, T *C, int batchCount,
    int64_t strideA, int64_t strideB, int64_t head_number,
    bool split_b_vertical) const {
  int lda = (transA == CblasNoTrans) ? W1 : H1;
  int ldb = (transB == CblasNoTrans) ? W2 : H2;
1071 1072 1073 1074
  auto a_array = std::vector<const T *>(batchCount);
  auto b_array = std::vector<const T *>(batchCount);
  auto c_array = std::vector<T *>(batchCount);

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  if (split_b_vertical) {
    int ldc = W2;
    int sub_width = W2 / head_number;

    for (int i = 0; i < head_number; i++) {
      int sub_matA_offset = (transA == CblasNoTrans)
                                ? i * (W1 / head_number)
                                : i * (W1 / head_number) * H1;
      int sub_matB_offset = (transB == CblasNoTrans)
                                ? i * (W2 / head_number)
                                : i * (W2 / head_number) * H2;
      int sub_matC_offset = i * W2 / head_number;
      for (int k = 0; k < batchCount; ++k) {
        a_array[k] = &A[k * strideA] + sub_matA_offset;
        b_array[k] = &B[k * strideB] + sub_matB_offset;
        c_array[k] = &C[k * H1 * W2] + sub_matC_offset;
      }

      CBlas<T>::GEMM_BATCH(CblasRowMajor, &transA, &transB, &H1, &sub_width,
                           &H2, &alpha, a_array.data(), &lda, b_array.data(),
                           &ldb, &beta, c_array.data(), &ldc,
                           1 /* group_count */, &batchCount);
1097 1098
    }

1099
  } else {
1100 1101 1102 1103 1104 1105 1106
    PADDLE_ENFORCE_EQ(
        W1, H2,
        platform::errors::InvalidArgument(
            "The fisrt matrix width should be same as second matrix height,"
            "but received fisrt matrix width %d"
            ", second matrix height %d",
            W1, H2));
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    int ldc = W2 * head_number;
    int sub_width = W1 / head_number;

    for (int i = 0; i < head_number; i++) {
      int sub_matA_offset = (transA == CblasNoTrans)
                                ? i * (W1 / head_number)
                                : i * (W1 / head_number) * H1;
      int sub_matB_offset = (transB == CblasNoTrans)
                                ? i * (W1 / head_number) * W2
                                : i * (W1 / head_number);
      int sub_matC_offset = i * W2;
      for (int k = 0; k < batchCount; ++k) {
        a_array[k] = &A[k * strideA] + sub_matA_offset;
        b_array[k] = &B[k * strideB] + sub_matB_offset;
        c_array[k] = &C[k * H1 * head_number * W2] + sub_matC_offset;
      }

      CBlas<T>::GEMM_BATCH(CblasRowMajor, &transA, &transB, &H1, &W2,
                           &sub_width, &alpha, a_array.data(), &lda,
                           b_array.data(), &ldb, &beta, c_array.data(), &ldc,
                           1 /* group_count */, &batchCount);
    }
1129 1130
  }
}
1131
#endif  // @} End Group Blas MKLML: BatchedGEMMWithHead
1132

T
tensor-tang 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
template <typename DeviceContext>
template <typename T>
void Blas<DeviceContext>::MatMul(const int M, const int N, const int K,
                                 const T *A, const T *B, T *C) const {
  this->template GEMM<T>(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K,
                         static_cast<T>(1), A, K, B, N, static_cast<T>(0), C,
                         N);
}

template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::MatMul(const int M, const int N,
                                              const int K, const T *A,
                                              const T *B, T *C) const {
#ifdef PADDLE_WITH_LIBXSMM
  // Refer to https://github.com/hfp/libxsmm/blob/master/README.md
  // But the threshold is custom constexpr int LIBXSMM_THRESHOLD = 20 * 20 * 20;

  // Since the matrix is very small,
  // so the unit of calculation is already very fast,
  // and the if( M*N*K < LIBXSMM_THRESHOLD) would be overhead,
  // use xsmm directly.
  // Note: SMM use ColMajor
  const char transa = 'N';
  const char transb = 'N';
  const T alpha = static_cast<T>(1);
  const T beta = static_cast<T>(0);
  CBlas<T>::SMM_GEMM(&transa, &transb, &N, &M, &K, &alpha, B, &N, A, &K, &beta,
                     C, &N);
  return;
#endif

  CBlas<T>::GEMM(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K,
                 static_cast<T>(1), A, K, B, N, static_cast<T>(0), C, N);
}

Y
Yu Yang 已提交
1169 1170 1171 1172 1173 1174 1175
template <typename DeviceContext>
template <typename T>
void Blas<DeviceContext>::MatMul(const framework::Tensor &mat_a,
                                 const MatDescriptor &dim_a,
                                 const framework::Tensor &mat_b,
                                 const MatDescriptor &dim_b, T alpha,
                                 framework::Tensor *mat_out, T beta) const {
1176 1177 1178 1179 1180 1181 1182 1183
  PADDLE_ENFORCE_EQ(
      dim_a.width_, dim_b.height_,
      platform::errors::InvalidArgument(
          "The fisrt matrix width should be same as second matrix height,"
          "but received fisrt matrix width %d"
          ", second matrix height %d",
          dim_a.width_, dim_b.height_));

Y
Yu Yang 已提交
1184 1185 1186 1187 1188 1189 1190
  CBLAS_TRANSPOSE transA = !dim_a.trans_ ? CblasNoTrans : CblasTrans;
  CBLAS_TRANSPOSE transB = !dim_b.trans_ ? CblasNoTrans : CblasTrans;
  if (dim_a.batch_size_ == 0 && dim_b.batch_size_ == 0) {
    this->template GEMM<T>(transA, transB, dim_a.height_, dim_b.width_,
                           dim_a.width_, alpha, mat_a.data<T>(),
                           mat_b.data<T>(), beta, mat_out->data<T>());
  } else {
1191 1192 1193 1194 1195 1196 1197 1198
    PADDLE_ENFORCE_EQ(
        dim_a.batch_size_ == dim_b.batch_size_ || dim_a.batch_size_ == 0 ||
            dim_b.batch_size_ == 0,
        true, platform::errors::InvalidArgument(
                  "dim_a.batch_size should be equal to dim_b.batch_size, or "
                  "one of dim_a.batch_size and dim_b.batch_size should be 0. "
                  "But got dim_a.batch_size = %d, dim_b.batch_size = %d.",
                  dim_a.batch_size_, dim_b.batch_size_));
Y
Yu Yang 已提交
1199 1200 1201 1202 1203 1204 1205
    this->template BatchedGEMM<T>(
        transA, transB, dim_a.height_, dim_b.width_, dim_a.width_, alpha,
        mat_a.data<T>(), mat_b.data<T>(), beta, mat_out->data<T>(),
        dim_a.batch_size_ == 0 ? dim_b.batch_size_ : dim_a.batch_size_,
        dim_a.stride_, dim_b.stride_);
  }
}
1206

1207 1208 1209
#if defined(PADDLE_WITH_MKLML) && !defined(PADDLE_WITH_CUDA) && \
    !defined(PADDLE_WITH_HIP)
// @{ Group Blas MKLML: MatMulWithHead
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
/*
 * Multiple two matrixes with multiple heads
 *
 * A new parameter, i.e head_number is added compared to normal MatMul.
 * The head_number describes the number of heads a matrix is vertically
 * split.
 *
 * When user calls this API, the multiplication of two big matrixes is split
 * into multiplication of several (head_number_) small matrixes. e.g. if Mat A
 * is [3, 24] and Mat B is [24, 4], when multiple A and B with head_number as
T
tianshuo78520a 已提交
1220 1221
 * 4, Mat A will be split as 4 matrix of [3, 6] and Mat B will be
 * (horizontally) split as 4 matrix of [6, 4]. The result of final matrix
1222 1223
 * will be 4 matrix of [3, 4], i.e. [3, 16].
 * Another example is A is [3, 8], B is [2, 16], head_number is 4. In this
T
tianshuo78520a 已提交
1224
 * case, A will be split as [3, 2], B will be (vertically) split as
1225
 * [2, 4]. The final result will be 4 matrix of 4 matrix of [3,4], i.e. [3, 16]
1226 1227 1228
 */
template <typename DeviceContext>
template <typename T>
1229 1230 1231 1232 1233 1234 1235
void Blas<DeviceContext>::MatMulWithHead(const framework::Tensor &mat_a,
                                         const MatDescriptor &dim_a,
                                         const framework::Tensor &mat_b,
                                         const MatDescriptor &dim_b, T alpha,
                                         int head_number,
                                         framework::Tensor *mat_out, T beta,
                                         bool mat_b_split_vertical) const {
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  PADDLE_ENFORCE_EQ(
      dim_a.width_ % head_number, 0,
      platform::errors::InvalidArgument(
          "The first input width must be some times the head number"
          "but received first input width %d"
          ",  head_number %d",
          dim_a.width_, head_number));
  PADDLE_ENFORCE_GE(head_number, 1,
                    platform::errors::InvalidArgument(
                        "The head number should be greater equal 1,"
                        "but received head number %d",
                        head_number));
  PADDLE_ENFORCE_LE(
      head_number, dim_a.width_,
      platform::errors::InvalidArgument(
          "The head number should be less equal first input width,"
          "but received first input width %d"
          ",  head_number %d",
          dim_a.width_, head_number));
1255 1256 1257
  CBLAS_TRANSPOSE transA = !dim_a.trans_ ? CblasNoTrans : CblasTrans;
  CBLAS_TRANSPOSE transB = !dim_b.trans_ ? CblasNoTrans : CblasTrans;

1258
  if (mat_b_split_vertical) {
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    PADDLE_ENFORCE_EQ(
        dim_b.height_, dim_a.width_ / head_number,
        platform::errors::InvalidArgument(
            "The second input height should be equal than first input width,"
            "but received second input height %d, first input width %d",
            dim_b.height_, dim_a.width_ / head_number));
    PADDLE_ENFORCE_EQ(
        dim_a.width_ % head_number, 0,
        platform::errors::InvalidArgument(
            "The second input width should be some times the head number"
            "but received second input width %d"
            ",  head_number %d",
            dim_b.width_, head_number));
1272 1273
  }

1274
  if (dim_a.batch_size_ == 0 && dim_b.batch_size_ == 0) {
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    int lda = !dim_a.trans_ ? dim_a.width_ : dim_a.height_;
    int ldb = !dim_b.trans_ ? dim_b.width_ : dim_b.height_;
    int sub_matA_offset;
    int sub_matB_offset;
    int sub_matC_offset;
    int sub_mat_M = dim_a.height_;
    int sub_mat_N;
    int sub_mat_K;
    int ldc;

1285
    for (int i = 0; i < head_number; i++) {
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
      sub_matA_offset = dim_a.trans_
                            ? i * (dim_a.width_ / head_number) * dim_a.height_
                            : i * (dim_a.width_ / head_number);
      if (mat_b_split_vertical) {
        sub_matB_offset = dim_b.trans_
                              ? i * (dim_b.width_ / head_number) * dim_b.height_
                              : i * (dim_b.width_ / head_number);
        sub_matC_offset = i * dim_b.width_ / head_number;

        sub_mat_N = dim_b.width_ / head_number;
        sub_mat_K = dim_b.height_;

        ldc = dim_b.width_;
      } else {
        sub_matB_offset =
            dim_b.trans_ ? i * (dim_b.height_ / head_number)
                         : i * (dim_b.height_ / head_number) * dim_b.width_;
        sub_matC_offset = i * dim_b.width_;

        sub_mat_N = dim_b.width_;
        sub_mat_K = dim_a.width_ / head_number;

        ldc = head_number * dim_b.width_;
      }

      this->template GEMM<T>(transA, transB, sub_mat_M, sub_mat_N, sub_mat_K,
                             alpha, mat_a.data<T>() + sub_matA_offset, lda,
1313 1314 1315 1316
                             mat_b.data<T>() + sub_matB_offset, ldb, beta,
                             mat_out->data<T>() + sub_matC_offset, ldc);
    }
  } else {
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    PADDLE_ENFORCE_EQ(
        (dim_a.batch_size_ == dim_b.batch_size_ || dim_a.batch_size_ == 0 ||
         dim_b.batch_size_ == 0),
        true,
        platform::errors::InvalidArgument(
            "The first input batch size should be equal than second input,"
            "either two input batch size is 0, but received first input batch "
            "size"
            " %d, second input batch size %d",
            dim_a.batch_size_, dim_b.batch_size_));
1327 1328

    this->template BatchedGEMMWithHead<T>(
1329 1330 1331
        transA, transB, dim_a.width_, dim_a.height_, dim_b.width_,
        dim_b.height_, alpha, mat_a.data<T>(), mat_b.data<T>(), beta,
        mat_out->data<T>(),
1332
        dim_a.batch_size_ == 0 ? dim_b.batch_size_ : dim_a.batch_size_,
1333
        dim_a.stride_, dim_b.stride_, head_number, mat_b_split_vertical);
1334 1335
  }
}
1336
#endif  // @} End Group Blas MKLML: MatMulWithHead
1337

Y
Use mkl  
Yu Yang 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
template <typename DeviceContext>
template <typename T>
void Blas<DeviceContext>::VINV(int n, const T *a, T *y) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VINV(n, a, y);
#else
  for (int i = 0; i < n; ++i) {
    y[i] = 1.0 / a[i];
  }
#endif
}
Y
Yu Yang 已提交
1349

Y
Yihua Xu 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::VMERF(int n, const T *a, T *y,
                                             int64_t mode) const {
#ifdef PADDLE_WITH_MKLML
  CBlas<T>::VMERF(n, a, y, mode);
#else
  for (int i = 0; i < n; ++i) {
    y[i] = std::erf(a[i]);
  }
#endif
}

1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
#ifdef PADDLE_WITH_MKLML
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::CSRMM(
    const char *transa, const int *m, const int *n, const int *k,
    const T *alpha, const char *matdescra, const T *val, const int *indx,
    const int *pntrb, const int *pntre, const T *b, const int *ldb,
    const T *beta, T *c, const int *ldc) const {
  CBlas<T>::CSRMM(transa, m, n, k, alpha, matdescra, val, indx, pntrb, pntre, b,
                  ldb, beta, c, ldc);
}
#endif

G
Guo Sheng 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
template <>
template <typename T>
void Blas<platform::CPUDeviceContext>::TRSM(CBLAS_SIDE side, CBLAS_UPLO uplo,
                                            CBLAS_TRANSPOSE transA,
                                            CBLAS_DIAG diag, int M, int N,
                                            T alpha, const T *A, int lda, T *B,
                                            int ldb) const {
  CBlas<T>::TRSM(CblasRowMajor, side, uplo, transA, diag, M, N, alpha, A, lda,
                 B, ldb);
}

Y
Yu Yang 已提交
1387 1388 1389
}  // namespace math
}  // namespace operators
}  // namespace paddle