BaseMatrix.h 25.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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
#include <stdint.h>
Y
Yu Yang 已提交
17
#include <cstddef>
H
hedaoyuan 已提交
18
#include "TensorExpression.h"
L
liaogang 已提交
19
#include "paddle/utils/Common.h"
Z
zhangjinchao01 已提交
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

namespace paddle {

/*
 * nvcc currently does not support C++11,
 * so I realized false_type and true_type.
 */
template <class T, T v>
struct bool_constant {
  static const T value = v;
};
typedef bool_constant<bool, false> false_type;
typedef bool_constant<bool, true> true_type;

/**
 * @brief   Calculate matrix element address.
 *
 * For instance, address of A[i][j] = i * ld + j.
 *
 */
#define CAL_MATRIX_START_ADDRESS(address, height, width, ld, col, row) \
  CHECK_LE(col, width);                                                \
  CHECK_LE(row, height);                                               \
  address += row * ld + col;

class MatrixOffset {
public:
  size_t aCol_;
  size_t aRow_;
  size_t bCol_;
  size_t bRow_;
  size_t cCol_;
  size_t cRow_;
  size_t dCol_;
  size_t dRow_;
55 56 57 58 59 60 61 62
  MatrixOffset(size_t aCol = 0,
               size_t aRow = 0,
               size_t bCol = 0,
               size_t bRow = 0,
               size_t cCol = 0,
               size_t cRow = 0,
               size_t dCol = 0,
               size_t dRow = 0)
Z
zhangjinchao01 已提交
63 64 65 66 67 68 69 70 71 72
      : aCol_(aCol),
        aRow_(aRow),
        bCol_(bCol),
        bRow_(bRow),
        cCol_(cCol),
        cRow_(cRow),
        dCol_(dCol),
        dRow_(dRow) {}
};

73
template <class T>
H
hedaoyuan 已提交
74
class BaseMatrixT : public TensorExpression<BaseMatrixT<T>, T> {
Z
zhangjinchao01 已提交
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
public:
  size_t height_, width_;
  size_t stride_;
  T* data_;
  bool trans_;
  bool useGpu_;

public:
  virtual ~BaseMatrixT() {}
  BaseMatrixT(size_t height, size_t width, T* data, bool trans, bool useGpu)
      : height_(height),
        width_(width),
        stride_(width),
        data_(data),
        trans_(trans),
        useGpu_(useGpu) {}

  /**
   * @note This constructor is for temporarily making a matrix with different
   *       useGpu flag as the original matrix so that mixed gpu/cpu operations
   *       can be performed successfully.
   */
  BaseMatrixT(BaseMatrixT& mat, bool useGpu)
      : height_(mat.height_),
        width_(mat.width_),
        stride_(mat.stride_),
        data_(mat.data_),
        trans_(mat.trans_),
        useGpu_(useGpu) {}

105 106 107 108 109 110
  BaseMatrixT(size_t height,
              size_t width,
              size_t stride,
              T* data,
              bool trans,
              bool use_gpu)
Z
zhangjinchao01 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      : height_(height),
        width_(width),
        stride_(stride),
        data_(data),
        trans_(trans),
        useGpu_(use_gpu) {
    /* CHECK_LE(width_, stride_); */
  }

  /// caller should make sure that the size of data is at least height*width
  void setData(T* data) { data_ = data; }

  /**
   * unary operator: element wise op(a).
   *
   * @code
   * for 0 <= i < this->height_ & for 0 <= j < this->width_.
   * @endcode
   */
  template <class Op>
  int applyUnary(Op op);

  /**
   * unary operator: element wise op(a).
   *
   * @code
   * for 0 <= i < numRows & for 0 <= j < numCols.
   * While matrix start address is:
   *  A = this->data_ + offset.aRow_*ld + offset.aCol_;
   * @endcode
   */
  template <class Op>
  int applyUnary(Op op, int numRows, int numCols, MatrixOffset& offset);

  /**
   * binary operator: element wise op(a, b).
   *
   * @code
   * for 0 <= i < this->height_ & for 0 <= j < this->width_.
   * While this->height_ == b.height_ && this->width_ == b.width_.
   * @endcode
   */
  template <class Op>
  int applyBinary(Op op, BaseMatrixT& b);

  /**
   * binary operator: element wise op(a, b)
   *
   * @code
   * for 0 <= i < numRows & for 0 <= j < numCols.
   * While matrix start address is:
   *   A = this->data_ + offset.aRow_*lda + offset.aCol_;
   *   B = b->data_ + offset.bRow_*ldb + offset.bCol_;
   *
   * if (bAsRowVector == false_type && bAsColVector == false_type)
   *   op(A[i * lda + j], B[i * ldb + j])
   *
   * if (bAsRowVector == true_type && bAsColVector == false_type)
   *   op(A[i * lda + j], B[j])
   *
   * if (bAsRowVector == false_type && bAsColVector == true_type)
   *   op(A[i * lda + j], B[i * ldb])
   *
   * if (bAsRowVector == true_type && bAsColVector == true_type)
   *   op(A[i * lda + j], B[0])
   * @endcode
   */
  template <class Op, class bAsRowVector, class bAsColVector>
179 180 181 182 183 184 185
  int applyBinary(Op op,
                  BaseMatrixT& b,
                  int numRows,
                  int numCols,
                  MatrixOffset& offset,
                  bAsRowVector,
                  bAsColVector);
Z
zhangjinchao01 已提交
186 187

  template <class Op>
188 189
  int applyBinary(
      Op op, BaseMatrixT& b, int numRows, int numCols, MatrixOffset& offset);
Z
zhangjinchao01 已提交
190 191 192 193 194 195 196 197 198 199 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

  /**
   * ternary operator: element wise op(a, b, c).
   *
   * @code
   * for 0 <= i < this->height_ & for 0 <= j < this->width_.
   *
   * While this->height_ == b.height_ && this->width_ == b.width_
   *    && this->height_ == c.height_ && this->width_ == c.width_
   * @endcode
   */
  template <class Op>
  int applyTernary(Op op, BaseMatrixT& b, BaseMatrixT& c);

  /**
   * ternary operator: element wise op(a, b, c).
   *
   * @code
   *  for 0 <= i < numRows & for 0 <= j < numCols.
   *  While matrix start address is:
   *
   *    A = this->data_ + offset.aRow_*lda + offset.aCol_;
   *    B = b->data_ + offset.bRow_*ldb + offset.bCol_;
   *    C = c->data_ + offset.cRow_*ldc + offset.cCol_;
   *
   *    if (cAsRowVector == false_type && cAsColVector == false_type)
   *      op(A[i*lda + j], B[i*ldb + j], C[i*ldc + j])
   *
   *    if (cAsRowVector == true_type && cAsColVector == false_type)
   *      op(A[i*lda + j], B[i*ldb + j], C[j])
   *
   *    if (cAsRowVector == false_type && cAsColVector == true_type)
   *      op(A[i*lda + j], B[i*ldb + j], C[i*ldc])
   *
   *    if (cAsRowVector == 1 && cAsColVector == 1)
   *      op(A[i*lda + j], B[i*ldb + j], C[0])
   * @endcode
   */
  template <class Op, class cAsRowVector, class cAsColVector>
229 230 231 232 233 234 235
  int applyTernary(Op op,
                   BaseMatrixT& b,
                   BaseMatrixT& c,
                   int numRows,
                   int numCols,
                   MatrixOffset& offset,
                   cAsRowVector,
Z
zhangjinchao01 已提交
236 237 238
                   cAsColVector);

  template <class Op>
239 240 241 242 243 244
  int applyTernary(Op op,
                   BaseMatrixT& b,
                   BaseMatrixT& c,
                   int numRows,
                   int numCols,
                   MatrixOffset& offset);
Z
zhangjinchao01 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

  /**
   * quaternary operator: element wise op(a, b, c, d).
   *
   * @code
   * for 0 <= i < this->height_ & for 0 <= j < this->width_.
   *
   * While this->height_ == b.height_ && this->width_ == b.width_
   *    && this->height_ == c.height_ && this->width_ == c.width_
   *    && this->height_ == d.height_ && this->width_ == d.width_
   * @endcode
   */
  template <class Op>
  int applyQuaternary(Op op, BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT& d);

  /**
   * quaternary operator: element wise op(a, b, c, d).
   *
   * @code
   * for 0 <= i < numRows & for 0 <= j < numCols.
   * While matrix start address is:
   *    A = this->data_ + offset.aRow_*lda + offset.aCol_;
   *    B = b->data_ + offset.bRow_*ldb + offset.bCol_;
   *    C = c->data_ + offset.cRow_*ldc + offset.cCol_;
   *    D = d->data_ + offset.dRow_*ldd + offset.dCol_;
   * @endcode
   */
  template <class Op>
273 274 275 276 277 278 279
  int applyQuaternary(Op op,
                      BaseMatrixT& b,
                      BaseMatrixT& c,
                      BaseMatrixT& d,
                      int numRows,
                      int numCols,
                      MatrixOffset& offset);
Z
zhangjinchao01 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

  /**
   * a aggregate expression that apply each row(or column) of matrix b.
   * op and sv is element wise operator.
   *
   * @code
   * if (aAsRowVector == true_type && aAsColVector == false_type)
   *  for each column j & 0 <= i < numRows, do:
   *    dst = agg(op(b[i*ldb + j]))
   *    a[j] = sv(a[j], dst)
   *
   * if (aAsRowVector == false_type && aAsColVector == true_type)
   *  for each row i & 0 <= j < numCols, do:
   *    dst = agg(op(b[i*ldb + j]))
   *    a[i] = sv(a[i], dst)
   * @endcode
   */
297 298 299 300
  template <class Agg,
            class Op,
            class Saver,
            class aAsRowVector,
Z
zhangjinchao01 已提交
301
            class aAsColVector>
302 303 304 305 306 307 308 309 310
  int aggregate(Agg agg,
                Op op,
                Saver sv,
                BaseMatrixT& b,
                int numRows,
                int numCols,
                MatrixOffset& offset,
                aAsRowVector,
                aAsColVector);
Z
zhangjinchao01 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

  /**
   * a aggregate expression that apply each row(or column) of matrix b and c.
   *
   * op and sv is element wise operator.
   *
   * @code
   * if (aAsRowVector == true_type && aAsColVector == false_type)
   *   for each column j & 0 <= i < numRows, do:
   *     dst = agg(op(b[i*ldb + j], c[i*ldc + j]))
   *     a[j] = sv(a[j], dst)
   *
   * if (aAsRowVector == false_type && aAsColVector == true_type)
   *   for each row i & 0 <= j < numCols, do:
   *     dst = agg(op(b[i*ldb + j], c[i*ldc + j]))
   *     a[i] = sv(a[i], dst)
   * @endcode
   */
329 330 331 332
  template <class Agg,
            class Op,
            class Saver,
            class aAsRowVector,
Z
zhangjinchao01 已提交
333
            class aAsColVector>
334 335 336 337 338 339 340 341 342
  int aggregate(Agg agg,
                Op op,
                Saver sv,
                BaseMatrixT& b,
                BaseMatrixT& c,
                int numRows,
                int numCols,
                MatrixOffset& offset,
                aAsRowVector,
Z
zhangjinchao01 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355
                aAsColVector);

  /**
   * a aggregate expression that apply each row of matrix b.
   *
   * @code
   * for each row i & 0 <= j < b.width_, do:
   *   this[i] = agg(b[i*ldb + j])
   * @endcode
   */
  template <class Agg>
  int applyRow(Agg agg, BaseMatrixT& b);

X
xuwei06 已提交
356 357 358 359 360 361 362 363 364 365 366 367
  /**
   * a aggregate expression that apply each row of matrix b.
   *
   * @code
   * for each row i & 0 <= j < b.width_, do:
   *   dst = agg(op(b[i*ldb + j], c[i*ldc + j])
   *   this[i] = sv(this[i], dst)
   * @endcode
   */
  template <class Agg, class Op, class Saver>
  int applyRow(Agg agg, Op op, Saver sv, BaseMatrixT& b, BaseMatrixT& c);

X
xuwei06 已提交
368 369
  // Same as the above with the special handing of sv=add2(scaleDest, scaleAgg)
  template <class Agg, class Op>
370 371 372 373 374 375
  int applyRow(Agg agg,
               Op op,
               real scaleDest,
               real scaleAgg,
               BaseMatrixT& b,
               BaseMatrixT& c);
X
xuwei06 已提交
376

Z
zhangjinchao01 已提交
377 378 379 380 381 382 383 384 385 386 387 388
  /**
   * a aggregate expression that apply each row of matrix b.
   *
   * @code
   * for each row i & 0 <= j < b.width_, do:
   *   dst = agg(b[i*ldb + j])
   *   this[i] = sv(this[i], dst)
   * @endcode
   */
  template <class Agg, class Saver>
  int applyRow(Agg agg, Saver sv, BaseMatrixT& b);

X
xuwei06 已提交
389 390 391 392
  // Same as the above with the special handing of sv=add2(scaleDest, scaleAgg)
  template <class Agg>
  int applyRow(Agg agg, real scaleDest, real scaleAgg, BaseMatrixT& b);

Z
zhangjinchao01 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  /**
   * a aggregate expression that apply each column of matrix b.
   *
   * @code
   * for each column j & 0 <= i < b.height_, do:
   *   this[j] = agg(b[i*ldb + j])
   * @endcode
   */
  template <class Agg>
  int applyCol(Agg agg, BaseMatrixT& b);

  /**
   * a aggregate expression that apply each column of matrix b.
   *
   * @code
   * for each column j & 0 <= i < b.height_, do:
   *   dst = agg(b[i*ldb + j])
   *   this[j] = sv(this[j], dst)
   * @endcode
   */
  template <class Agg, class Saver>
  int applyCol(Agg agg, Saver sv, BaseMatrixT& b);

X
xuwei06 已提交
416 417 418 419
  // Same as the above with the special handing of sv=add2(scaleDest, scaleAgg)
  template <class Agg>
  int applyCol(Agg agg, real scaleDest, real scaleAgg, BaseMatrixT& b);

Z
zhangjinchao01 已提交
420 421 422 423 424 425 426 427 428 429 430
  bool useGpu() const { return useGpu_; }

  const T* rowBuf(size_t row) const { return data_ + width_ * row; }

  T* rowBuf(size_t row) { return data_ + width_ * row; }

  /**
   * @brief   unary operator.
   *
   */
  void neg();
H
hedaoyuan 已提交
431 432 433 434 435 436 437 438
  void exp2();
  void pow2(T p);
  void log2();
  void sqrt2();
  void square2();
  void reciprocal2();
  void abs2();
  void sign2();
Z
zhangjinchao01 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  void zero();

  /**
   * @code
   * this(row, col + columnOffset) = 0 for 0 <= col < numColumns
   * @endcode
   */
  void zeroAtOffset(int64_t columnOffset, int64_t numColumns);
  void one();
  void subScalar(T p);
  void mulScalar(T p);
  void divScalar(T p);

  /**
   * @code
   * this = p
   * @endcode
   */
  void assign(T p);

X
xutianbing 已提交
459 460 461
  /**
   * @code
   * swap(this, b)
X
xutianbing 已提交
462 463 464 465
   * example: swap two Matrices
   * MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
   * MatrixPtr cpuB = std::make_shared<CpuMatrix>(height, width);
   * cpuA->deepSwap(*cpuB);
X
xutianbing 已提交
466 467 468 469
   * @endcode
   */
  void deepSwap(BaseMatrixT& b);

Z
zhangjinchao01 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  /**
   * @code
   * this = this + p
   * @endcode
   */
  void add(T p);

  /**
   * @code
   * this = this*p1 + p2
   * @endcode
   */
  void add(T p1, T p2);

  /**
   * this = this < low ? low : this
   *
   * this = this > high ? high : this
   */
  void clip(T p1, T p2);

G
guosheng 已提交
491 492 493 494 495 496 497
  /**
   * this = b < low ? 0 : 1
   *
   * this = b > high ? 0 : 1
   */
  void clipDerivative(BaseMatrixT& b, T p1, T p2);

Z
zhangjinchao01 已提交
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
  /**
   * @code
   * a = a > p ? 1.0f : 0.0f
   * @endcode
   */
  void biggerThanScalar(T p);

  /**
   * @code
   * a = a > p ? a : p
   * @endcode
   */
  void downClip(T p);

  /**
   * @code
   * this = b
   * @endcode
   */
  void assign(BaseMatrixT& b);

  /**
   * @code
   * If b.width + columOffset <= this.width
   *  this(row, col + columnOffset) = b(row, col) for 0 <= col < b.width
   *
   * If this.width + columnOffset <= b.width
   *  this(row, col) = b(row, col + columnOffset) for 0 <= col < this.width
   *
   * Otherwise, FATAL
   * @endcode
   */
  void assignAtOffset(BaseMatrixT& b, int64_t columnOffset);

  /// this = this + b
  void add(BaseMatrixT& b);

  /**
   * @code
   * If b.width + columOffset <= this.width
   *  this(row, col + columnOffset) += b(row, col) for 0 <= col < b.width
   *
   * If this.width + columnOffset <= b.width
   *  this(row, col) += b(row, col + columnOffset) for 0 <= col < this.width
   *
   * Otherwise, FATAL
   * @endcode
   */
  void addAtOffset(BaseMatrixT& b, int64_t columnOffset);

  void addColVector(BaseMatrixT& b);
  void addRowVector(BaseMatrixT& b);
  void addBias(BaseMatrixT& b, T scale);

  void mulRowVector(BaseMatrixT& b);
  void divRowVector(BaseMatrixT& b);

555 556 557
  void mulColVector(BaseMatrixT& b);
  void divColVector(BaseMatrixT& b);

Z
zhangjinchao01 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  void addP2P(BaseMatrixT& b);

  /**
   * @code
   * this = this + b*p
   * @endcode
   */
  void add(BaseMatrixT& b, T p);

  /**
   * @code
   * this = p1*this + p2*b
   * @endcode
   */
  void add(BaseMatrixT& b, T p1, T p2);

  /**
   * @code
   * this = this - b
   * @endcode
   */
  void sub(BaseMatrixT& b);

  /**
   * @code
   * this = this - b*p
   * @endcode
   */
  void sub(BaseMatrixT& b, T p);

  /**
   * @code
   * b = max(0, this)
   * @endcode
   */
  void relu(BaseMatrixT& b);
  void reluDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = log(1.0 + exp(this))
   * @endcode
   */
  void softrelu(BaseMatrixT& b);
  void softreluDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = min(max(this, p1), p2)
   * @endcode
   */
  void brelu(BaseMatrixT& b);
  void breluDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = this * this
   * @endcode
   */
H
hedaoyuan 已提交
617
  void square2(BaseMatrixT& b);
Z
zhangjinchao01 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  void squareDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = tanh(this)
   * @endcode
   */
  void tanh(BaseMatrixT& b);
  void tanhDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = p1 * tanh(p2 * this)
   * @endcode
   */
  void scaledTanh(BaseMatrixT& b, T p1, T p2);
  void scaledTanhDerivative(BaseMatrixT& b, T p1, T p2);

  /**
   * @code
   * b = 1.0f / this
   * @endcode
   */
H
hedaoyuan 已提交
641
  void reciprocal2(BaseMatrixT& b);
Z
zhangjinchao01 已提交
642 643 644 645 646 647 648
  void reciprocalDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = this > 0.0f ? this : -this
   * @endcode
   */
H
hedaoyuan 已提交
649
  void abs2(BaseMatrixT& b);
Z
zhangjinchao01 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  void absDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = 1.0f / (1.0f + exp(-this))
   * @endcode
   */
  void sigmoid(BaseMatrixT& b);
  void sigmoidDerivative(BaseMatrixT& b);

  /**
   * @code
   * b = a
   * @endcode
   */
  void expDerivative(BaseMatrixT& b);

H
hedaoyuan 已提交
667
  void sign2(BaseMatrixT& b);
Z
zhangjinchao01 已提交
668

H
hedaoyuan 已提交
669 670 671 672
  void exp2(BaseMatrixT& b);
  void pow2(BaseMatrixT& b, T p);
  void log2(BaseMatrixT& b);
  void sqrt2(BaseMatrixT& b);
Z
zhangjinchao01 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 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 736 737 738 739
  void addScalar(BaseMatrixT& b, T p);
  void subScalar(BaseMatrixT& b, T p);
  void mulScalar(BaseMatrixT& b, T p);
  void divScalar(BaseMatrixT& b, T p);
  void scalarDiv(BaseMatrixT& b, T p);

  /**
   * @code
   * this = 1.0f / sqrt(b)
   * @endcode
   */
  void invSqrt(BaseMatrixT& b);

  /// this = (b == value)
  void isEqualTo(BaseMatrixT& b, T value);

  /**
   * @brief   ternary operator.
   */
  void softCrossEntropy(BaseMatrixT& b, BaseMatrixT& c);
  void softCrossEntropyBp(BaseMatrixT& b, BaseMatrixT& c);
  void binaryLabelCrossEntropy(BaseMatrixT& b, BaseMatrixT& c);
  void binaryLabelCrossEntropyBp(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = b + c
   * @endcode
   */
  void add(BaseMatrixT& b, BaseMatrixT& c);
  /**
   * @code
   * this = b*p1 + c*p2
   * @endcode
   */
  void add(BaseMatrixT& b, T p1, BaseMatrixT& c, T p2);
  /**
   * @code
   * this = b - c
   * @endcode
   */
  void sub(BaseMatrixT& b, BaseMatrixT& c);
  /**
   * @code
   * this = b*p1 - c*p2
   * @endcode
   */
  void sub(BaseMatrixT& b, T p1, BaseMatrixT& c, T p2);

  /**
   * @code
   * this = this + b + c
   * @endcode
   */
  void add2(BaseMatrixT& b, BaseMatrixT& c);
  /**
   * @code
   * this = this*p1 + b*p2 + c*p3
   * @endcode
   */
  void add2(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2, T p3);

  /**
   * @code
   * this = a*p1 + b*p2 + c*p3
   * @endcode
   */
740
  void add3(BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT& d, T p1, T p2, T p3);
Z
zhangjinchao01 已提交
741 742 743 744 745 746 747 748 749

  /**
   * @code
   *   c = p2 * c - p1 *  (b + p3 * this)
   *   this += mom
   * @endcode
   */
  void sgdUpdate(BaseMatrixT& b,  //  grad
                 BaseMatrixT& c,  //  mom
750 751 752
                 T p1,            //  learningRate,
                 T p2,            //  momentum,
                 T p3);           //  decayRate
Z
zhangjinchao01 已提交
753 754 755 756 757 758 759 760 761 762

  /**
   * @code
   *   c = p2 * c - p1 * d * (b + p3 * this)
   *   this += mom
   * @endcode
   */
  void sgdUpdate(BaseMatrixT& b,  // grad,
                 BaseMatrixT& c,  // mom,
                 BaseMatrixT& d,  // lr,
763 764 765
                 T p1,            // learningRate,
                 T p2,            // momentum,
                 T p3);           // decayRate
Z
zhangjinchao01 已提交
766 767

  /// apply L1/L2 to *this*
768
  virtual void applyL1(T learningRate, T decayRate);
Z
zhangjinchao01 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  void applyL1(BaseMatrixT& lr, T learningRate, T decayRate);
  void applyL2(T learningRate, T decayRate);
  void applyL2(BaseMatrixT& lr, T learningRate, T decayRate);

  /**
   * @code
   * this *= b
   * @endcode
   */
  void dotMul(BaseMatrixT& b);

  /**
   * @code
   * this = b * c
   * @endcode
   */
  void dotMul(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = b / c
   * @endcode
   */
  void dotDiv(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = (b + p1) / (c + p2)
   * @endcode
   */
  void dotDiv(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this = log(1 + exp(b - c)) - d * (b - c)
   * @endcode
   */
  void rankLoss(BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT& d);
  void rankLossBp(BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT& d);

  /**
   * @code
   * this = log(1 + exp(b)) - c * b
   * @endcode
   */
  void logisticRegressionLoss(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this += exp(b)/(1+exp(b)) - c
   * @endcode
   */
  void logisticRegressionLossBp(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = b > c ? 1.0 : 0.0
   * @endcode
   */
  void biggerThan(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = ((b>c && d>0.5) || (b<c && d<0.5)) ? 1 : 0)
   * @endcode
   */
  void biggerThan(BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT& d);

  /**
   * @code
   * this = b>c ? b : c
   * @endcode
   */
H
hedaoyuan 已提交
842
  void max2(BaseMatrixT& b, BaseMatrixT& c);
Z
zhangjinchao01 已提交
843 844 845 846 847 848

  /**
   * @code
   * this[destCol] += (b>p1 == c>p1) ? 0 : 1)
   * @endcode
   */
849 850 851
  void binaryClassificationError(size_t destCol,
                                 BaseMatrixT& b,
                                 BaseMatrixT& c,
Z
zhangjinchao01 已提交
852
                                 T p);
853 854 855 856
  void binaryClassificationError2(size_t destCol,
                                  BaseMatrixT& b,
                                  BaseMatrixT& c,
                                  T p);
Z
zhangjinchao01 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

  /**
   * @code
   * this = this * b * b
   * @endcode
   */
  void dotMulSquare(BaseMatrixT& b);

  /**
   * @code
   * this = this * this * b
   * @endcode
   */
  void dotSquareMul(BaseMatrixT& b);

  /**
   * @code
   * this = b * c * c
   * @endcode
   */
  void dotMulSquare(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = b * b * c * c
   * @endcode
   */
  void dotSquareSquare(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = this * (p1*b + p2*c)^2
   * @endcode
   */
  void dotMulSquareSum(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this = (p1*b + p2*c)^2
   * @endcode
   */
  void dotSquareSum(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this=  this * (p1*b + p2*c)
   * @endcode
   */
  void dotMulSum(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this += sqr(p1*b + p2*c + p3*d)
   * @endcode
   */
912 913
  void addSquareSum(
      BaseMatrixT& b, BaseMatrixT& c, BaseMatrixT d, T p1, T p2, T p3);
Z
zhangjinchao01 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

  /**
   * @code
   * this += p * sqr(b)
   * @endcode
   */
  void addSquare(BaseMatrixT& b, T p);

  /**
   * @code
   * this = p1 * this + p2 * sqr(b)
   * @endcode
   */
  void decayAddSquare(BaseMatrixT& b, T p1, T p2);

  /**
   * @code
   * this = p1 * this + p2 * sqr(b * c)
   * @endcode
   */
  void decayAddSquareMul(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this = 1 / (p1 * b + p2)
   * @endcode
   */
H
hedaoyuan 已提交
941
  void reciprocal2(BaseMatrixT& b, T p1, T p2);
Z
zhangjinchao01 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 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

  /**
   * @code
   * this = 1 / (p1 * b + p2 * c + p3)
   * @endcode
   */
  void reciprocalSum(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2, T p3);

  /**
   * @code
   * b = this; this = 0
   * @endcode
   */
  void copyAndClear(BaseMatrixT& b);

  /**
   * @code
   * this_row[destCol] += dotprod(b_row, c_row)
   * @endcode
   */
  void rowDotMul(size_t destCol, BaseMatrixT& b, BaseMatrixT& c);
  void rowDotMul2(size_t destCol, BaseMatrixT& b, BaseMatrixT& c);

  /**
   * this is vector (one row matrix)
   *
   * @code
   *   for each row i, do:
   *      this_row += dotmul(b_row_i, c_row_i)
   * @endcode
   */
  void addDotMulVMM(BaseMatrixT& b, BaseMatrixT& c);
  void addDotMulVMM2(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * c is vector (one row matrix)
   *
   * @code
   * for each row i, do:
   *    this_row_i += dotmul(b_row_i, c_row)
   * @endcode
   */
  void addDotMulMMV(BaseMatrixT& b, BaseMatrixT& c);
  void addDotMulMMV2(BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this = p1 * this + p2 * b * c
   * @endcode
   */
  void addDotMul(BaseMatrixT& b, BaseMatrixT& c, T p1, T p2);

  /**
   * @code
   * this_row = b_row * c_row[cCol]
   * @endcode
   */
  void rowScale(size_t cCol, BaseMatrixT& b, BaseMatrixT& c);
  void rowScale2(size_t cCol, BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this_col = b_col * c_col[cRow]
   * @endcode
   */
  void colScale(size_t cRow, BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this_col += b_col * c_col[cRow]
   * @endcode
   */
  void addColScale(size_t cRow, BaseMatrixT& b, BaseMatrixT& c);

  /**
   * @code
   * this_row += b_row * c_row[cCol]
   * @endcode
   */
  void addRowScale(size_t cCol, BaseMatrixT& b, BaseMatrixT& c);

  /// calculate the sum of each row of the matrix b.
X
xuwei06 已提交
1024 1025 1026
  /// this_i = scaleDest * this_i + scaleSum * \sum_j b_{ij}
  void sumRows(BaseMatrixT& b, T scaleSum, T scaleDest);

Z
zhangjinchao01 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  /// calculate the maximum value of each row of the matrix b.
  void maxRows(BaseMatrixT& b);
  /// calculate the minimum value of each row of the matrix b.
  void minRows(BaseMatrixT& b);

  /// calculate the maximum value of each column of the matrix b.
  void maxCols(BaseMatrixT& b);
  /// calculate the minimum value of each column of the matrix b.
  void minCols(BaseMatrixT& b);

X
xuwei06 已提交
1037 1038 1039 1040 1041
  /// calculate the sum of each column of the matrix b.
  /// this_i = scaleDest * this_i + scaleSum * \sum_j b_{ji}
  void sumCols(BaseMatrixT& b, T scaleSum, T scaleDest);

  /// this_i = scaleDest * this_i + scaleSum * \sum_j (b_{ij} - c_{ij})^2
1042 1043 1044 1045
  void sumOfSquaredDiffs(BaseMatrixT& b,
                         BaseMatrixT& c,
                         T scaleSum,
                         T scaleDest);
X
xuwei06 已提交
1046 1047

  /// this_i = scaleDest * this_i + scaleSum * \sum_j b_{ij} * c_{ij}
1048
  void sumOfProducts(BaseMatrixT& b, BaseMatrixT& c, T scaleSum, T scaleDest);
Z
zhangjinchao01 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

  /**
   * @code
   * this_row = b_row + p * ones * c_row[cCol]
   * @endcode
   */
  void rowAdd(size_t cCol, BaseMatrixT& b, BaseMatrixT& c, T p);
  /**
   * @code
   * this_row = pow(b_row, c_row[cCol])
   * @endcode
   */
  void rowPow(size_t cCol, BaseMatrixT& b, BaseMatrixT& c);

1063
  virtual bool isSparse() const { return false; }
H
hedaoyuan 已提交
1064

1065
  template <typename ExpressionType>
H
hedaoyuan 已提交
1066 1067 1068 1069 1070 1071 1072 1073
  void operator=(const ExpressionType& expr) {
    if (useGpu_) {
      TensorGpuApply<T>(*this, expr);
    } else {
      TensorCpuApply<T>(*this, expr);
    }
  }

1074
  template <typename ExpressionType>
H
hedaoyuan 已提交
1075 1076 1077
  void operator+=(const ExpressionType& expr) {
    (*this) = (*this) + expr;
  }
1078
  template <typename ExpressionType>
H
hedaoyuan 已提交
1079 1080 1081
  void operator-=(const ExpressionType& expr) {
    (*this) = (*this) - expr;
  }
1082
  template <typename ExpressionType>
H
hedaoyuan 已提交
1083 1084 1085
  void operator*=(const ExpressionType& expr) {
    (*this) = (*this) * expr;
  }
1086
  template <typename ExpressionType>
H
hedaoyuan 已提交
1087 1088 1089
  void operator/=(const ExpressionType& expr) {
    (*this) = (*this) / expr;
  }
Z
zhangjinchao01 已提交
1090 1091 1092 1093 1094 1095
};

typedef BaseMatrixT<real> BaseMatrix;
typedef BaseMatrixT<int> IBaseMatrix;

}  // namespace paddle