test_Tensor.cu 31.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2

H
hedaoyuan 已提交
3 4 5
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
H
hedaoyuan 已提交
6

H
hedaoyuan 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
H
hedaoyuan 已提交
8

H
hedaoyuan 已提交
9 10 11 12 13
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. */
H
hedaoyuan 已提交
14

H
hedaoyuan 已提交
15 16
#include <gtest/gtest.h>
#include "TensorCheck.h"
X
Xin Pan 已提交
17
#include "paddle/legacy/math/Matrix.h"
H
hedaoyuan 已提交
18

19 20 21 22 23 24 25
using paddle::Matrix;
using paddle::CpuMatrix;
using paddle::GpuMatrix;
using paddle::CpuVector;
using paddle::GpuVector;
using paddle::CpuIVector;
using paddle::GpuIVector;
H
hedaoyuan 已提交
26 27
using autotest::TensorCheckEqual;
using autotest::TensorCheckErr;
H
hedaoyuan 已提交
28

L
liaogang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#define INIT_UNARY(A1, A2)  \
  Tensor A1(height, width); \
  Tensor A2(height, width); \
  A1.randomizeUniform();    \
  A2.copyFrom(A1)
#define INIT_BINARY(A1, A2, B) \
  INIT_UNARY(A1, A2);          \
  Tensor B(height, width);     \
  B.randomizeUniform()
#define INIT_TERNARY(A1, A2, B, C) \
  INIT_BINARY(A1, A2, B);          \
  Tensor C(height, width);         \
  C.randomizeUniform()
#define INIT_QUATERNARY(A1, A2, B, C, D) \
  INIT_TERNARY(A1, A2, B, C);            \
  Tensor D(height, width);               \
  D.randomizeUniform()

template <typename Tensor>
H
hedaoyuan 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
struct TestUnaryMatrix {
  typedef std::function<void(Tensor& A1, Tensor& A2)> UnaryFunc;

  explicit TestUnaryMatrix(UnaryFunc testUnaryFunc) {
    for (auto height : {1, 11, 73, 128, 200, 330}) {
      for (auto width : {1, 32, 100, 512, 1000, 3210}) {
        LOG(INFO) << " height=" << height << " width=" << width;
        INIT_UNARY(A1, A2);
        testUnaryFunc(A1, A2);
      }
    }
  }
};

L
liaogang 已提交
62
template <typename Tensor>
H
hedaoyuan 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
struct TestBinaryMatrix {
  typedef std::function<void(Tensor& A1, Tensor& A2, Tensor& B)> BinaryFunc;

  explicit TestBinaryMatrix(BinaryFunc testBinaryFunc) {
    for (auto height : {1, 11, 73, 128, 200, 330}) {
      for (auto width : {1, 32, 100, 512, 1000, 3210}) {
        LOG(INFO) << " height=" << height << " width=" << width;
        INIT_BINARY(A1, A2, B);
        testBinaryFunc(A1, A2, B);
      }
    }
  }
};

L
liaogang 已提交
77
template <typename Tensor>
H
hedaoyuan 已提交
78
struct TestTernaryMatrix {
L
liaogang 已提交
79 80
  typedef std::function<void(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C)>
      TernaryFunc;
H
hedaoyuan 已提交
81 82 83 84 85 86 87 88 89 90 91 92

  explicit TestTernaryMatrix(TernaryFunc testTernaryFunc) {
    for (auto height : {1, 11, 73, 128, 200, 330}) {
      for (auto width : {1, 32, 100, 512, 1000, 3210}) {
        LOG(INFO) << " height=" << height << " width=" << width;
        INIT_TERNARY(A1, A2, B, C);
        testTernaryFunc(A1, A2, B, C);
      }
    }
  }
};

L
liaogang 已提交
93
template <typename Tensor>
H
hedaoyuan 已提交
94 95
struct TestQuaternaryMatrix {
  typedef std::function<void(
L
liaogang 已提交
96 97
      Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D)>
      QuaternaryFunc;
H
hedaoyuan 已提交
98 99 100 101 102 103 104 105 106 107 108 109

  explicit TestQuaternaryMatrix(QuaternaryFunc testQuaternaryFunc) {
    for (auto height : {1, 11, 73, 128, 200, 330}) {
      for (auto width : {1, 32, 100, 512, 1000, 3210}) {
        LOG(INFO) << " height=" << height << " width=" << width;
        INIT_QUATERNARY(A1, A2, B, C, D);
        testQuaternaryFunc(A1, A2, B, C, D);
      }
    }
  }
};

L
liaogang 已提交
110
template <typename Tensor, class T>
H
hedaoyuan 已提交
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
struct TestUnaryVectorT {
  typedef std::function<void(Tensor& A1, Tensor& A2)> UnaryFunc;

  explicit TestUnaryVectorT(UnaryFunc testUnaryFunc) {
    for (auto size : {1, 11, 73, 128, 200, 330, 512, 1000, 4210}) {
      LOG(INFO) << " size=" << size;
      Tensor A1(size);
      Tensor A2(size);
      if (typeid(T) == typeid(real)) {
        A1.rand();
      } else {
        A1.rand(1000);
      }
      A2.copyFrom(A1);
      testUnaryFunc(A1, A2);
    }
  }
};

void SetTensorValue(Matrix& matrix, real value) {
  int height = matrix.getHeight();
  int width = matrix.getWidth();
  int stride = matrix.getStride();
  real* data = matrix.getData();
  for (int i = 0; i < height; i++) {
    int j = rand() % width;  // NOLINT
    if (typeid(matrix) == typeid(CpuMatrix)) {
      data[i * stride + j] = value;
    } else if (typeid(matrix) == typeid(GpuMatrix)) {
      hl_memcpy(&data[i * stride + j], &value, sizeof(real));
    } else {
    }
  }
}

L
liaogang 已提交
146
template <typename Tensor>
H
hedaoyuan 已提交
147 148 149
void testTensorAddScalar(Tensor& A1, Tensor& A2) {
  real p1 = 2.5;
  real p2 = 3.0;
L
liaogang 已提交
150
  A1.add(p1);  // a += p
H
hedaoyuan 已提交
151 152 153 154 155 156 157 158
  A2 += p1;
  TensorCheckEqual(A1, A2);

  A1.add(p1, p2);  // a = a * p1 + p2
  A2 = A2 * p1 + p2;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
159
template <typename Tensor>
H
hedaoyuan 已提交
160 161 162 163 164 165 166
void testTensorSubScalar(Tensor& A1, Tensor& A2) {
  real p = 2.5;
  A1.subScalar(p);  // a -= p
  A2 -= p;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
167
template <typename Tensor>
H
hedaoyuan 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180
void testTensorMulScalar(Tensor& A1, Tensor& A2) {
  real p = 2.5;
  A1.mulScalar(p);  // a *= p
  A2 *= p;
  TensorCheckEqual(A1, A2);

  real learningRate = 0.7f;
  real decayRate = 1.2f;
  A1.applyL2(learningRate, decayRate);
  A2 = A2 * (1.0f / (1.0f + learningRate * decayRate));
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
181
template <typename Tensor>
H
hedaoyuan 已提交
182 183 184 185 186 187 188
void testTensorDivScalar(Tensor& A1, Tensor& A2) {
  real p = 2.5;
  A1.divScalar(p);  // a /= p
  A2 /= p;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
189
template <typename Tensor>
H
hedaoyuan 已提交
190 191 192 193 194 195
void testTensorNeg(Tensor& A1, Tensor& A2) {
  A1.neg();  // a = -a
  A2 = -A2;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
196
template <typename Tensor>
H
hedaoyuan 已提交
197 198 199 200 201 202
void testTensorAbs(Tensor& A1, Tensor& A2) {
  A1.abs2();  // a = a > 0 ? a : -a
  A2 = A2.abs();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
203
template <typename Tensor>
H
hedaoyuan 已提交
204 205 206 207 208 209
void testTensorSquare(Tensor& A1, Tensor& A2) {
  A1.square2();  // a = a * a
  A2 = A2.square();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
210
template <typename Tensor>
H
hedaoyuan 已提交
211 212 213 214 215 216
void testTensorReciprocal(Tensor& A1, Tensor& A2) {
  A1.reciprocal2();  // a = 1.0f / a
  A2 = A2.reciprocal();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
217
template <typename Tensor>
H
hedaoyuan 已提交
218 219 220 221 222 223
void testTensorSign(Tensor& A1, Tensor& A2) {
  A1.sign2();  // a = (a > 0) - (a < 0)
  A2 = A2.sign();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
224
template <typename Tensor>
H
hedaoyuan 已提交
225
void testTensorAssign(Tensor& A1, Tensor& A2) {
L
liaogang 已提交
226
  A1.assign(1.5);  // a = p
H
hedaoyuan 已提交
227 228 229 230 231 232 233 234 235 236 237 238
  A2 = A2.constant(1.5);
  TensorCheckEqual(A1, A2);

  A1.one();  // a = 1
  A2 = A2.constant(1.0);
  TensorCheckEqual(A1, A2);

  A1.zero();  // a = 0
  A2 = A2.constant(0.0);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
239
template <typename Tensor>
H
hedaoyuan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252
void testUnaryBaseOp(Tensor& A1, Tensor& A2) {
  testTensorAddScalar(A1, A2);
  testTensorSubScalar(A1, A2);
  testTensorMulScalar(A1, A2);
  testTensorDivScalar(A1, A2);
  testTensorNeg(A1, A2);
  testTensorAbs(A1, A2);
  testTensorSquare(A1, A2);
  testTensorReciprocal(A1, A2);
  testTensorSign(A1, A2);
  testTensorAssign(A1, A2);
}

L
liaogang 已提交
253
template <typename Tensor>
H
hedaoyuan 已提交
254
void testUnaryBaseOpInt(Tensor& A1, Tensor& A2) {
L
liaogang 已提交
255
  A1.add(2);  // a += p
H
hedaoyuan 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269
  A2 += 2;
  TensorCheckEqual(A1, A2);

  A1.add(3, 2);  // a = a * p1 + p2
  A2 = A2 * 3 + 2;
  TensorCheckEqual(A1, A2);

  testTensorNeg(A1, A2);
  testTensorAbs(A1, A2);
}

TEST(Unary, BaseOp) {
  TestUnaryMatrix<CpuMatrix> testCpuMatrix(testUnaryBaseOp<CpuMatrix>);
  TestUnaryVectorT<CpuVector, real> testCpuVector(testUnaryBaseOp<CpuVector>);
L
liaogang 已提交
270 271
  TestUnaryVectorT<CpuIVector, int> testCpuIVector(
      testUnaryBaseOpInt<CpuIVector>);
H
hedaoyuan 已提交
272

273
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
274 275
  TestUnaryMatrix<GpuMatrix> testGpuMatrix(testUnaryBaseOp<GpuMatrix>);
  TestUnaryVectorT<GpuVector, real> testGpuVector(testUnaryBaseOp<GpuVector>);
L
liaogang 已提交
276 277
  TestUnaryVectorT<GpuIVector, int> testGpuIVector(
      testUnaryBaseOpInt<GpuIVector>);
H
hedaoyuan 已提交
278 279 280
#endif
}

L
liaogang 已提交
281
template <typename Tensor>
H
hedaoyuan 已提交
282 283 284 285 286 287
void testTensorExp(Tensor& A1, Tensor& A2) {
  A1.exp2();  // a = exp(a)
  A2 = A2.exp();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
288
template <typename Tensor>
H
hedaoyuan 已提交
289 290 291 292 293 294
void testTensorLog(Tensor& A1, Tensor& A2) {
  A1.log2();  // a = log(a)
  A2 = A2.log();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
295
template <typename Tensor>
H
hedaoyuan 已提交
296 297 298 299 300 301
void testTensorSqrt(Tensor& A1, Tensor& A2) {
  A1.sqrt2();  // a = sqrt(a)
  A2 = A2.sqrt();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
302
template <typename Tensor>
H
hedaoyuan 已提交
303 304 305 306 307 308
void testTensorPow(Tensor& A1, Tensor& A2) {
  A1.pow2(3.2);  // a = pow(a, p)
  A2 = A2.pow(3.2);
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
309
template <typename Tensor>
H
hedaoyuan 已提交
310 311 312 313 314 315 316 317 318 319
void testUnayrMathOp(Tensor& A1, Tensor& A2) {
  testTensorExp(A1, A2);
  testTensorLog(A1, A2);
  testTensorSqrt(A1, A2);
  testTensorPow(A1, A2);
}

TEST(Unary, MathOp) {
  TestUnaryMatrix<CpuMatrix> testCpu(testUnayrMathOp<CpuMatrix>);

320
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
321 322 323 324
  TestUnaryMatrix<GpuMatrix> testGpu(testUnayrMathOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
325
template <typename Tensor>
H
hedaoyuan 已提交
326 327 328 329 330 331 332 333 334
void testTensorClip(Tensor& A1, Tensor& A2) {
  real p1 = 0.003f;
  real p2 = 0.877f;
  A1.clip(p1, p2);  // a = a < p1 ? p1 : (a > p2 ? p2 : a)
  // A2 = A2.min(0.877f).max(0.003f);
  A2 = (A2 < p1).condition(p1, (A2 > p2).condition(p2, A2));
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
335
template <typename Tensor>
H
hedaoyuan 已提交
336 337 338 339 340 341 342
void testTensorBiggerThanScalar(Tensor& A1, Tensor& A2) {
  real p = 0.5f;
  A1.biggerThanScalar(p);  // a = a > p ? 1.0f : 0.0f
  A2 = (A2 > p).condition((real)1.0, (real)0.0);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
343
template <typename Tensor>
H
hedaoyuan 已提交
344 345 346 347 348 349 350 351 352 353 354
void testTensorapplyL1(Tensor& A1, Tensor& A2) {
  /**
   * T lambda = p;
   * a = (a > lambda) ? (a - lambda)
   *                  : (a < -lambda) ? (a + lambda) : 0
   *
   * p = learningRate * decayRate;
   */
  real learningRate = 0.7f;
  real decayRate = 0.6f;
  A1.applyL1(learningRate, decayRate);
L
liaogang 已提交
355 356 357 358 359
  A2 = (A2 > (learningRate * decayRate))
           .condition(
               (A2 - (learningRate * decayRate)),
               (A2 < -(learningRate * decayRate))
                   .condition((A2 + (learningRate * decayRate)), (real)0.0));
H
hedaoyuan 已提交
360 361 362
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
363
template <typename Tensor>
H
hedaoyuan 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376
void testUnayrCompareOp(Tensor& A1, Tensor& A2) {
  testTensorClip(A1, A2);
  testTensorBiggerThanScalar(A1, A2);

  A1.randomizeUniform();
  A1.subScalar(0.5f);
  A2.copyFrom(A1);
  testTensorapplyL1(A1, A2);
}

TEST(Unary, CompareOp) {
  TestUnaryMatrix<CpuMatrix> testCpu(testUnayrCompareOp<CpuMatrix>);

377
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
378 379 380 381
  TestUnaryMatrix<GpuMatrix> testGpu(testUnayrCompareOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
382
template <typename Tensor>
H
hedaoyuan 已提交
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
void testTensorAdd(Tensor& A1, Tensor& A2, Tensor& B) {
  real p1 = 2.5;
  real p2 = 3.2;
  A1.add(B);  // a += b
  A2 += B;
  TensorCheckEqual(A1, A2);

  A1.add(B, p1);  // a += b * p
  A2 += B * p1;
  TensorCheckEqual(A1, A2);

  A1.add(B, p1, p2);  // a = p1 * a + p2 * b
  A2 = A2 * p1 + B * p2;
  TensorCheckEqual(A1, A2);

  A1.addScalar(B, p1);  // a = b + p
  A2 = B + p1;
  TensorCheckEqual(A1, A2);

  A1.addSquare(B, p1);  // a += p * b * b
  A2 += B.constant(p1) * B * B;
  TensorCheckEqual(A1, A2);

  A1.decayAddSquare(B, p1, p2);  // a = p1 * a + p2 * b * b
  A2 = A2 * p1 + B.constant(p2) * B * B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
411
template <typename Tensor>
H
hedaoyuan 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
void testTensorSub(Tensor& A1, Tensor& A2, Tensor& B) {
  real p = 2.5;
  A1.sub(B);  // a -= b
  A2 -= B;
  TensorCheckEqual(A1, A2);

  A1.sub(B, p);  // a -= b * p
  A2 -= B * p;
  TensorCheckEqual(A1, A2);

  A1.subScalar(B, p);  // a = b - p
  A2 = B - p;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
427
template <typename Tensor>
H
hedaoyuan 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
void testTensorMul(Tensor& A1, Tensor& A2, Tensor& B) {
  real p = 2.5;
  A1.mulScalar(B, p);  // a = b * p
  A2 = B * p;
  TensorCheckEqual(A1, A2);

  A1.dotMulSquare(B);  // a *= b * b
  A2 *= B * B;
  TensorCheckEqual(A1, A2);

  A1.dotSquareMul(B);  // a = a * a * b
  A2 = A2 * A2 * B;
  TensorCheckEqual(A1, A2);

  A1.dotMul(B);  // a *= b
  A2 *= B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
447
template <typename Tensor>
H
hedaoyuan 已提交
448 449 450 451 452 453 454 455 456 457 458
void testTensorDiv(Tensor& A1, Tensor& A2, Tensor& B) {
  real p = 2.5;
  A1.divScalar(B, p);  // a = b / p
  A2 = B / p;
  TensorCheckEqual(A1, A2);

  A1.scalarDiv(B, p);  // a = p / b
  A2 = B.constant(p) / B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
459
template <typename Tensor>
H
hedaoyuan 已提交
460 461 462 463 464 465
void testTensorAssign(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.assign(B);  // a = b
  A2 = B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
466
template <typename Tensor>
H
hedaoyuan 已提交
467
void testTensorSquare(Tensor& A1, Tensor& A2, Tensor& B) {
L
liaogang 已提交
468
  B.square2(A1);  // b = a * a
H
hedaoyuan 已提交
469 470 471 472
  A2 = B.square();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
473
template <typename Tensor>
H
hedaoyuan 已提交
474 475 476 477 478 479
void testTensorSquareDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.squareDerivative(B);  // a *= 2.0 * b
  A2 = A2 * (real)2.0 * B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
480
template <typename Tensor>
H
hedaoyuan 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494
void testTensorReciprocal(Tensor& A1, Tensor& A2, Tensor& B) {
  B.reciprocal2(A1);  // b = 1.0f / a
  A2 = B.reciprocal();
  TensorCheckEqual(A1, A2);

  real p1 = 0.58;
  real p2 = 0.32;
  A1.reciprocal2(B, p1, p2);  // a = 1 / (p1 * b + p2)
  A2 = (B * p1 + p2).reciprocal();
  TensorCheckEqual(A1, A2);

  real learningRate = 0.7f;
  real decayRate = 1.2f;
  A1.applyL2(B, learningRate, decayRate);  // a *= (1.0f / (1.0f + p * b))
L
liaogang 已提交
495 496
  A2 *= (B.constant(1.0f) + B.constant(learningRate * decayRate) * B)
            .reciprocal();
H
hedaoyuan 已提交
497 498 499
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
500
template <typename Tensor>
H
hedaoyuan 已提交
501 502 503 504 505 506
void testTensorReciprocalDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.reciprocalDerivative(B);  // a *= -b * b
  A2 *= (-B) * B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
507
template <typename Tensor>
H
hedaoyuan 已提交
508 509 510 511 512 513
void testTensorSign(Tensor& A1, Tensor& A2, Tensor& B) {
  B.sign2(A1);  // b = a > 0.0f ? 1.0f : -1.0f
  A2 = B.sign();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
514
template <typename Tensor>
H
hedaoyuan 已提交
515 516 517 518 519 520
void testTensorAbs(Tensor& A1, Tensor& A2, Tensor& B) {
  B.abs2(A1);  // b = a > 0.0f ? a : -a
  A2 = B.abs();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
521
template <typename Tensor>
H
hedaoyuan 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
void testBinaryBaseOp(Tensor& A1, Tensor& A2, Tensor& B) {
  testTensorAdd(A1, A2, B);
  testTensorSub(A1, A2, B);
  testTensorMul(A1, A2, B);
  testTensorDiv(A1, A2, B);
  testTensorSquare(A1, A2, B);
  testTensorSquareDerivative(A1, A2, B);
  testTensorReciprocal(A1, A2, B);
  testTensorReciprocalDerivative(A1, A2, B);
  testTensorAbs(A1, A2, B);
  testTensorSign(A1, A2, B);
  testTensorAssign(A1, A2, B);
}

TEST(Binary, BaseOp) {
  TestBinaryMatrix<CpuMatrix> testCpu(testBinaryBaseOp<CpuMatrix>);

539
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
540 541 542 543
  TestBinaryMatrix<GpuMatrix> testGpu(testBinaryBaseOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
544
template <typename Tensor>
H
hedaoyuan 已提交
545 546 547 548 549 550 551
void testTensorExp(Tensor& A1, Tensor& A2, Tensor& B) {
  // a = exp(b)
  A1.exp2(B);
  A2 = B.exp();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
552
template <typename Tensor>
H
hedaoyuan 已提交
553 554 555 556 557 558
void testTensorExpDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.expDerivative(B);  // a *= b
  A2 *= B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
559
template <typename Tensor>
H
hedaoyuan 已提交
560 561 562 563 564 565 566
void testTensorLog(Tensor& A1, Tensor& A2, Tensor& B) {
  // a = log(b)
  A1.log2(B);
  A2 = B.log();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
567
template <typename Tensor>
H
hedaoyuan 已提交
568 569 570 571 572 573 574
void testTensorSqrt(Tensor& A1, Tensor& A2, Tensor& B) {
  // a = sqrt(b)
  A1.sqrt2(B);
  A2 = B.sqrt();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
575
template <typename Tensor>
H
hedaoyuan 已提交
576 577 578 579 580 581 582
void testTensorInvSqrt(Tensor& A1, Tensor& A2, Tensor& B) {
  // a = 1.0f / sqrt(b)
  A1.invSqrt(B);
  A2 = B.sqrt().reciprocal();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
583
template <typename Tensor>
H
hedaoyuan 已提交
584 585 586 587 588 589
void testTensorPow(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.pow2(B, 2.5f);  // a = pow(b, p)
  A2 = B.pow(2.5f);
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
590
template <typename Tensor>
H
hedaoyuan 已提交
591 592 593 594 595 596 597 598 599 600 601
void testTensorSoftrelu(Tensor& A1, Tensor& A2, Tensor& B) {
  /*
   * const T THRESHOLD = 40.0;
   * b = log(1.0 +
   *         exp((a > THRESHOLD) ? THRESHOLD
   *             : ((a < -THRESHOLD) ? (-THRESHOLD) : a)))
   */
  B.softrelu(A1);

  real THRESHOLD = 40.0;
  A2 = (B.constant(1.0f) +
L
liaogang 已提交
602 603 604 605
        (B > THRESHOLD)
            .condition(THRESHOLD, (B < -THRESHOLD).condition(-THRESHOLD, B))
            .exp())
           .log();
H
hedaoyuan 已提交
606 607 608
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
609
template <typename Tensor>
H
hedaoyuan 已提交
610 611 612 613 614 615 616 617 618
void testTensorSoftreluDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  /*
   * const T THRESHOLD = 40.0;
   * a *= (1.0 - exp(-1.0 * ((b > THRESHOLD)
   *                             ? THRESHOLD
   *                             : ((b < -THRESHOLD) ? (-THRESHOLD) : b)))));
   */
  A1.softreluDerivative(B);
  real THRESHOLD = 40.0;
L
liaogang 已提交
619 620 621 622 623 624
  A2 = A2 *
       (B.constant(1.0f) -
        (B.constant(-1.0f) *
         (B > THRESHOLD)
             .condition(THRESHOLD, (B < -THRESHOLD).condition(-THRESHOLD, B)))
            .exp());
H
hedaoyuan 已提交
625 626 627
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
628
template <typename Tensor>
H
hedaoyuan 已提交
629 630 631 632 633 634 635 636 637 638 639 640
void testTensorSigmoid(Tensor& A1, Tensor& A2, Tensor& B) {
  /*
    const T THRESHOLD_MIN = -40.0;
    const T THRESHOLD_MAX = 13.0;
    T tmp = (a < THRESHOLD_MIN) ? THRESHOLD_MIN
            : ((a > THRESHOLD_MAX) ? THRESHOLD_MAX : a);
    b = 1.0f / (1.0f + exp(-tmp)))
   */
  B.sigmoid(A1);

  const real THRESHOLD_MIN = -40.0;
  const real THRESHOLD_MAX = 13.0;
L
liaogang 已提交
641 642 643
  auto tmp = (B < THRESHOLD_MIN)
                 .condition(THRESHOLD_MIN,
                            (B > THRESHOLD_MAX).condition(THRESHOLD_MAX, B));
H
hedaoyuan 已提交
644 645 646 647
  A2 = (B.constant(1.0f) + (-tmp).exp()).reciprocal();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
648
template <typename Tensor>
H
hedaoyuan 已提交
649 650 651 652 653 654
void testTensorSigmoidDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.sigmoidDerivative(B);  // a *= b * (1 - b)
  A2 *= B * (B.constant(1.0f) - B);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
655
template <typename Tensor>
H
hedaoyuan 已提交
656 657 658 659 660 661
void testTensorTanh(Tensor& A1, Tensor& A2, Tensor& B) {
  B.tanh(A1);  // b = 2.0 / (1.0 + exp(-2 * a)) - 1.0
  A2 = B.constant(2.0f) / ((B * ((real)-2.0f)).exp() + (real)1.0f) - (real)1.0f;
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
662
template <typename Tensor>
H
hedaoyuan 已提交
663 664 665 666 667 668
void testTensorTanhDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.tanhDerivative(B);  // a *= 1 - b * b
  A2 *= B.constant(1.0f) - B * B;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
669
template <typename Tensor>
H
hedaoyuan 已提交
670 671 672 673 674 675
void testTensorScaledTanh(Tensor& A1, Tensor& A2, Tensor& B) {
  real p1 = 2.5;
  real p2 = 3.1;
  // b = p1 * (2.0 / (1.0 + exp(-2 * p2 * a)) - 1.0)
  B.scaledTanh(A1, p1, p2);
  A2 = B.constant(p1) *
L
liaogang 已提交
676 677
       (B.constant(2.0f) / ((B.constant(-2.0f) * p2 * B).exp() + (real)1.0) -
        (real)1.0);
H
hedaoyuan 已提交
678 679 680
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
681
template <typename Tensor>
H
hedaoyuan 已提交
682 683 684 685 686 687 688 689 690
void testTensorScaledTanhDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  real p1 = 2.5;
  real p2 = 3.1;
  // a *= (p2 / p1) * (p1 * p1 - b * b));
  A1.scaledTanhDerivative(B, p1, p2);
  A2 = A2 * (B.constant(p2 / p1) * (B.constant(p1 * p1) - B * B));
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
691
template <typename Tensor>
H
hedaoyuan 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
void testBinaryMathOp(Tensor& A1, Tensor& A2, Tensor& B) {
  testTensorTanhDerivative(A1, A2, B);
  testTensorScaledTanhDerivative(A1, A2, B);
  testTensorSigmoidDerivative(A1, A2, B);
  testTensorExpDerivative(A1, A2, B);
  testTensorScaledTanh(A1, A2, B);
  testTensorTanh(A1, A2, B);
  testTensorExp(A1, A2, B);
  testTensorLog(A1, A2, B);
  testTensorSqrt(A1, A2, B);
  testTensorInvSqrt(A1, A2, B);
  testTensorPow(A1, A2, B);

  testTensorSoftrelu(A1, A2, B);
  testTensorSoftreluDerivative(A1, A2, B);
  testTensorSigmoid(A1, A2, B);
}

TEST(Binary, MathOp) {
  TestBinaryMatrix<CpuMatrix> testCpu(testBinaryMathOp<CpuMatrix>);

713
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
714 715 716 717
  TestBinaryMatrix<GpuMatrix> testGpu(testBinaryMathOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
718
template <typename Tensor>
H
hedaoyuan 已提交
719 720 721 722 723 724
void testTensorRelu(Tensor& A1, Tensor& A2, Tensor& B) {
  B.relu(A1);  // b = a > 0.0f ? a : 0.0f
  A2 = (B > (real)0.0f).condition(B, (real)0.0f);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
725
template <typename Tensor>
H
hedaoyuan 已提交
726 727 728 729 730 731
void testTensorReluDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.reluDerivative(B);  // a *= (b > 0.0f ? 1.0f : 0.0f)
  A2 *= (B > (real)0.0).condition((real)1.0, (real)0.0);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
732
template <typename Tensor>
H
hedaoyuan 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745
void testTensorBrelu(Tensor& A1, Tensor& A2, Tensor& B) {
  /*
   * b = a > p1 ? a : p1
   * b = b < p2 ? b : p2
   * int p1 = 0, p2 = 24;
   */
  SetTensorValue(B, 32.0f);
  B.brelu(A1);
  auto tmp = (B > (real)0.0f).condition(B, (real)0.0f);
  A2 = (tmp < (real)24.0f).condition(tmp, (real)24.0f);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
746
template <typename Tensor>
H
hedaoyuan 已提交
747 748 749 750 751 752 753 754 755 756 757
void testTensorBreluDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  SetTensorValue(B, 32.0f);
  /*
   * a *= (b > p1 && b < p2) ? 1.0 : 0.0
   * int p1 = 0, p2 = 24;
   */
  A1.breluDerivative(B);
  A2 *= (B > (real)0.0f && B < (real)24.0f).condition((real)1.0f, (real)0.0f);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
758
template <typename Tensor>
H
hedaoyuan 已提交
759 760
void testTensorAbsDerivative(Tensor& A1, Tensor& A2, Tensor& B) {
  A1.absDerivative(B);  // a = (b > 0) ? a : (b < 0) ? -a : 0
L
liaogang 已提交
761 762
  A2 = (B > (real)0.0f)
           .condition(A2, (B < (real)0.0f).condition(-A2, (real)0.0f));
H
hedaoyuan 已提交
763 764 765
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
766
template <typename Tensor>
H
hedaoyuan 已提交
767 768 769 770 771 772 773 774
void testTensorIsEqualTo(Tensor& A1, Tensor& A2, Tensor& B) {
  real p = 0.613;
  SetTensorValue(B, p);
  A1.isEqualTo(B, p);  // a = (b == p)
  A2 = (B == p);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
775
template <typename Tensor>
H
hedaoyuan 已提交
776 777 778 779 780 781 782 783 784 785 786 787
void testTensorapplyL1(Tensor& A1, Tensor& A2, Tensor& B) {
  /**
   * T lambda = p * b;
   * a = (a > lambda) ? (a - lambda)
   *                  : (a < -lambda) ? (a + lambda) : 0
   *
   * p = learningRate * decayRate;
   */
  real learningRate = 0.7f;
  real decayRate = 0.6f;
  A1.applyL1(B, learningRate, decayRate);
  auto lambda = B.constant(learningRate * decayRate) * B;
L
liaogang 已提交
788 789 790
  A2 = (A2 > lambda)
           .condition((A2 - lambda),
                      (A2 < -lambda).condition((A2 + lambda), (real)0.0f));
H
hedaoyuan 已提交
791 792 793
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
794
template <typename Tensor>
H
hedaoyuan 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
void testBinaryCompareOp(Tensor& A1, Tensor& A2, Tensor& B) {
  B.subScalar(0.5f);
  SetTensorValue(B, 0.0f);
  testTensorReluDerivative(A1, A2, B);

  A1.randomizeUniform();
  A2.copyFrom(A1);
  testTensorBreluDerivative(A1, A2, B);

  testTensorAbsDerivative(A1, A2, B);
  testTensorRelu(A1, A2, B);
  testTensorBrelu(A1, A2, B);
  testTensorIsEqualTo(A1, A2, B);
}

TEST(Binary, CompareOp) {
  TestBinaryMatrix<CpuMatrix> testCpu(testBinaryCompareOp<CpuMatrix>);

813
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
814 815 816 817
  TestBinaryMatrix<GpuMatrix> testGpu(testBinaryCompareOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
818
template <typename Tensor>
H
hedaoyuan 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
void testTensorAdd(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.add(B, C);  // a = b + c
  A2 = B + C;
  TensorCheckEqual(A1, A2);

  real p1 = 1.5;
  real p2 = 2.5;
  real p3 = 3.8;
  A1.add(B, p1, C, p2);  // a = p1 * b + p2 * c
  A2 = B * p1 + C * p2;
  TensorCheckEqual(A1, A2);

  A1.add2(B, C);  // a = a + b + c
  A2 = A2 + B + C;
  TensorCheckEqual(A1, A2);

  A1.add2(B, C, p1, p2, p3);  // a = p1 * a + p2 * b + p3 * c
  A2 = A2 * p1 + B * p2 + C * p3;
  TensorCheckEqual(A1, A2);

  A1.decayAddSquareMul(B, C, p1, p2);  // a = p1 * a + p2 * b * b * c * c
  A2 = A2 * p1 + B.constant(p2) * B * B * C * C;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
844
template <typename Tensor>
H
hedaoyuan 已提交
845 846 847 848 849 850 851 852 853 854 855 856
void testTensorSub(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.sub(B, C);  // a = b - c
  A2 = B - C;
  TensorCheckEqual(A1, A2);

  real p1 = 1.5;
  real p2 = 2.5;
  A1.sub(B, p1, C, p2);  // a = p1 * b - p2 * c
  A2 = B * p1 - C * p2;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
857
template <typename Tensor>
H
hedaoyuan 已提交
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
void testTensorMul(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.dotMul(B, C);  // a = b * c
  A2 = B * C;
  TensorCheckEqual(A1, A2);

  A1.dotMulSquare(B, C);  // a = b * c * c
  A2 = B * C * C;
  TensorCheckEqual(A1, A2);

  A1.dotSquareSquare(B, C);  // a = b * b * c * c
  A2 = B * B * C * C;
  TensorCheckEqual(A1, A2);

  real p1 = 1.5;
  real p2 = 2.5;

  /*
   * T tmp = p1 * b + p2 * c;
   * a *= tmp * tmp
   */
  A1.dotMulSquareSum(B, C, p1, p2);
  auto tmp = B * p1 + C * p2;
  A2 *= tmp * tmp;
  TensorCheckEqual(A1, A2);

  /*
   * T tmp = p1 * b + p2 * c;
   * a = tmp * tmp
   */
  A1.dotSquareSum(B, C, p1, p2);
  auto tmp2 = B * p1 + C * p2;
  A2 = tmp2 * tmp2;
  TensorCheckEqual(A1, A2);

  // a *= p1 * b + p2 * c
  A1.dotMulSum(B, C, p1, p2);
  A2 *= B * p1 + C * p2;
  TensorCheckEqual(A1, A2);

  // a = p1 * a + p2 * b * c
  A1.addDotMul(B, C, p1, p2);
  A2 = A2 * p1 + B.constant(p2) * B * C;
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
903
template <typename Tensor>
H
hedaoyuan 已提交
904 905 906 907 908 909 910 911 912 913 914 915
void testTensorDiv(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.dotDiv(B, C);  // a = (b == 0.0) ? 0.0 : b / c
  A2 = (B == (real)0.0).condition((real)0.0, B / C);
  TensorCheckEqual(A1, A2);

  real p1 = 1.5;
  real p2 = 2.5;
  A1.dotDiv(B, C, p1, p2);  // a = (b + p1) / (c + p2)
  A2 = (B + p1) / (C + p2);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
916
template <typename Tensor>
H
hedaoyuan 已提交
917 918 919 920 921 922 923 924 925
void testTensorReciprocal(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  real p1 = 1.5;
  real p2 = 2.5;
  real p3 = 3.5;
  A1.reciprocalSum(B, C, p1, p2, p3);  // a = 1 / (p1 * b + p2 * c + p3)
  A2 = (B * p1 + C * p2 + p3).reciprocal();
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
926
template <typename Tensor>
H
hedaoyuan 已提交
927 928 929 930 931 932
void testTensorSoftCrossEntropy(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.softCrossEntropy(B, C);  // a = -c * log(b) - (1 - c) * log(1 - b)
  A2 = -C * B.log() - (C.constant(1.0f) - C) * (B.constant(1.0f) - B).log();
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
933
template <typename Tensor>
H
hedaoyuan 已提交
934 935 936 937 938 939 940 941 942
void testTensorSoftCrossEntropyBp(Tensor& A1,
                                  Tensor& A2,
                                  Tensor& B,
                                  Tensor& C) {
  A1.softCrossEntropyBp(B, C);  // a += (b - c) / (b * (1 - b))
  A2 += (B - C) / (B * (B.constant(1.0f) - B));
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
943
template <typename Tensor>
H
hedaoyuan 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957
void testTernaryBaseOp(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  testTensorAdd(A1, A2, B, C);
  testTensorSub(A1, A2, B, C);
  testTensorMul(A1, A2, B, C);
  testTensorDiv(A1, A2, B, C);
  testTensorReciprocal(A1, A2, B, C);
  testTensorSoftCrossEntropyBp(A1, A2, B, C);

  testTensorSoftCrossEntropy(A1, A2, B, C);
}

TEST(Ternary, BaseOp) {
  TestTernaryMatrix<CpuMatrix> testCpu(testTernaryBaseOp<CpuMatrix>);

958
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
959 960 961 962
  TestTernaryMatrix<GpuMatrix> testGpu(testTernaryBaseOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
963
template <typename Tensor>
H
hedaoyuan 已提交
964 965 966 967 968
void testTensorBinaryLabelCrossEntropy(Tensor& A1,
                                       Tensor& A2,
                                       Tensor& B,
                                       Tensor& C) {
  A1.binaryLabelCrossEntropy(B, C);  // a = c > 0.5 ? -log(b) : -log(1.0 - b)
L
liaogang 已提交
969
  A2 = (C > (real)0.5).condition(-(B.log()), -((B.constant(1.0f) - B).log()));
H
hedaoyuan 已提交
970 971 972
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
973
template <typename Tensor>
H
hedaoyuan 已提交
974 975 976 977 978 979
void testTensorBinaryLabelCrossEntropyBp(Tensor& A1,
                                         Tensor& A2,
                                         Tensor& B,
                                         Tensor& C) {
  // a += c > 0.5 ? -1.0 / b : 1.0 / (1.0 - b)
  A1.binaryLabelCrossEntropyBp(B, C);
L
liaogang 已提交
980 981 982
  A2 += (C > (real)0.5)
            .condition((B.constant(-1.0f) / B),
                       (B.constant(1.0f) - B).reciprocal());
H
hedaoyuan 已提交
983 984 985
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
986
template <typename Tensor>
H
hedaoyuan 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
void testTensorLogisticRegressionLoss(Tensor& A1,
                                      Tensor& A2,
                                      Tensor& B,
                                      Tensor& C) {
  SetTensorValue(B, 50.0f);
  SetTensorValue(B, -50.0f);
  /**
   * const T THRESHOLD = 40.0;
   * T x = (b > THRESHOLD) ? THRESHOLD : (b < -THRESHOLD)
   *                                        ? -THRESHOLD
   *                                        : b;
   * a = log(1 + exp(x)) - c * x
   */
  A1.logisticRegressionLoss(B, C);
  real THRESHOLD = 40.0;
L
liaogang 已提交
1002 1003 1004
  auto tmp =
      (B > THRESHOLD)
          .condition(THRESHOLD, (B < -THRESHOLD).condition(-THRESHOLD, B));
H
hedaoyuan 已提交
1005 1006 1007 1008
  A2 = (C.constant(1.0f) + tmp.exp()).log() - C * tmp;
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
1009
template <typename Tensor>
H
hedaoyuan 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
void testTensorLogisticRegressionLossBp(Tensor& A1,
                                        Tensor& A2,
                                        Tensor& B,
                                        Tensor& C) {
  SetTensorValue(B, 50.0f);
  SetTensorValue(B, -50.0f);
  /**
   * const T THRESHOLD = 40.0;
   * T x = (b > THRESHOLD) ? THRESHOLD : (b < -THRESHOLD)
   *                                        ? -THRESHOLD
   *                                        : b;
   * x = exp(x); a = x / (1 + x) - c
   */
  A1.logisticRegressionLossBp(B, C);
  real THRESHOLD = 40.0;
L
liaogang 已提交
1025 1026 1027
  auto tmp =
      (B > THRESHOLD)
          .condition(THRESHOLD, (B < -THRESHOLD).condition(-THRESHOLD, B));
H
hedaoyuan 已提交
1028 1029 1030 1031 1032
  auto tmp2 = tmp.exp();
  A2 = tmp2 / (C.constant(1.0) + tmp2) - C;
  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
1033
template <typename Tensor>
H
hedaoyuan 已提交
1034 1035 1036 1037 1038 1039
void testTensorBiggerThan(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.biggerThan(B, C);  // a = (b > c) ? 1.0f : 0.0f
  A2 = (B > C).condition((real)1.0f, (real)0.0f);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
1040
template <typename Tensor>
H
hedaoyuan 已提交
1041 1042 1043 1044 1045 1046
void testTensorMax(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  A1.max2(B, C);  // a = (b > c) ? b : c
  A2 = (B > C).condition(B, C);
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
1047
template <typename Tensor>
H
hedaoyuan 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
void testTernaryCompareOp(Tensor& A1, Tensor& A2, Tensor& B, Tensor& C) {
  testTensorBinaryLabelCrossEntropyBp(A1, A2, B, C);
  testTensorBinaryLabelCrossEntropy(A1, A2, B, C);
  testTensorBiggerThan(A1, A2, B, C);
  testTensorMax(A1, A2, B, C);

  testTensorLogisticRegressionLoss(A1, A2, B, C);
  testTensorLogisticRegressionLossBp(A1, A2, B, C);
}

TEST(Ternary, CompareOp) {
  TestTernaryMatrix<CpuMatrix> testCpu(testTernaryCompareOp<CpuMatrix>);

1061
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
1062 1063 1064 1065
  TestTernaryMatrix<GpuMatrix> testGpu(testTernaryCompareOp<GpuMatrix>);
#endif
}

L
liaogang 已提交
1066 1067 1068
template <typename Tensor>
void testQuaternaryAdd(
    Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D) {
H
hedaoyuan 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  // A1.add3(B, C, D, 1.5f, 2.5f, 3.5f);  // a = p1 * b + p2 * c + p3 * d
  // A2 = B * 1.5f + C * 2.5f + D * 3.5f;
  // TensorCheckEqual(A1, A2);

  /*
   * T tmp = p1 * b + p2 * c + p3 * d;
   * a += tmp * tmp
   */
  real p1 = 1.5f;
  real p2 = 2.5f;
  real p3 = 3.5f;
  A1.addSquareSum(B, C, D, p1, p2, p3);
  auto tmp = B * p1 + C * p2 + D * p3;
  A2 += tmp * tmp;
  TensorCheckEqual(A1, A2);
}

TEST(Quaternary, BaseOp) {
  TestQuaternaryMatrix<CpuMatrix> testCpu(testQuaternaryAdd<CpuMatrix>);

1089
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
1090 1091 1092 1093
  TestQuaternaryMatrix<GpuMatrix> testGpu(testQuaternaryAdd<GpuMatrix>);
#endif
}

L
liaogang 已提交
1094 1095 1096
template <typename Tensor>
void testTensorBiggerThan(
    Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D) {
H
hedaoyuan 已提交
1097 1098
  // a = ((b > c && d > 0.5f) || (b < c && d < 0.5f)) ? 1.0f : 0.0f);
  A1.biggerThan(B, C, D);
L
liaogang 已提交
1099 1100
  A2 = ((B > C && D > (real)0.5) || (B < C && D < (real)0.5))
           .condition((real)1.0, (real)0.0);
H
hedaoyuan 已提交
1101 1102 1103
  TensorCheckEqual(A1, A2);
}

L
liaogang 已提交
1104 1105 1106
template <typename Tensor>
void testTensorRankLoss(
    Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D) {
H
hedaoyuan 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  /**
   * const T THRESHOLD = 40.0; a = b - c;
   * a = (a > THRESHOLD)
   *         ? THRESHOLD
   *         : ((a < -THRESHOLD) ? (-THRESHOLD) : a);
   * a = log(1 + exp(a)) - a * d
   */
  A1.rankLoss(B, C, D);

  real THRESHOLD = 40.0;
  auto tmp = B - C;
L
liaogang 已提交
1118 1119 1120
  auto tmp2 =
      (tmp > THRESHOLD)
          .condition(THRESHOLD, (tmp < -THRESHOLD).condition(-THRESHOLD, tmp));
H
hedaoyuan 已提交
1121 1122 1123 1124 1125
  A2 = (D.constant(1.0f) + tmp2.exp()).log() - tmp2 * D;

  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
1126 1127 1128
template <typename Tensor>
void testTensorRankLossBp(
    Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D) {
H
hedaoyuan 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  /**
   * const T THRESHOLD = 40.0; a = b - c;
   * a = (a > THRESHOLD)
   *         ? THRESHOLD
   *         : ((a < -THRESHOLD) ? (-THRESHOLD) : a);
   * a = exp(a); a = (a / (1 + a) - d)
   */
  A1.rankLossBp(B, C, D);
  real THRESHOLD = 40.0;
  auto tmp = B - C;
L
liaogang 已提交
1139 1140 1141
  auto tmp2 =
      (tmp > THRESHOLD)
          .condition(THRESHOLD, (tmp < -THRESHOLD).condition(-THRESHOLD, tmp));
H
hedaoyuan 已提交
1142 1143 1144 1145 1146 1147
  auto tmp3 = tmp2.exp();
  A2 = tmp3 / (D.constant(1.0f) + tmp3) - D;

  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
1148 1149 1150
template <typename Tensor>
void testQuaternaryCompareOp(
    Tensor& A1, Tensor& A2, Tensor& B, Tensor& C, Tensor& D) {
H
hedaoyuan 已提交
1151 1152 1153 1154 1155 1156 1157 1158
  testTensorBiggerThan(A1, A2, B, C, D);
  testTensorRankLoss(A1, A2, B, C, D);
  testTensorRankLossBp(A1, A2, B, C, D);
}

TEST(Quaternary, CompareOp) {
  TestQuaternaryMatrix<CpuMatrix> testCpu(testQuaternaryCompareOp<CpuMatrix>);

1159
#ifdef PADDLE_WITH_GPU
H
hedaoyuan 已提交
1160 1161 1162
  TestQuaternaryMatrix<GpuMatrix> testGpu(testQuaternaryCompareOp<GpuMatrix>);
#endif
}