test_FPException.cpp 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */


/**
 * This test is about floating point calculation exception.
 * Paddle catches FE_INVALID, FE DIVBYZERO and FE_OVERFLOW exceptions.
 *
 * Some exceptions occur in the middle of a set of formulas, 
 * that can be circumvented by some tricks.
 * For example, 
 * calculate tanh
 *   b = 2.0 / (1.0 + exp(-2 * a)) - 1.0
 *
 * If the result of (-2 * a) is too large,
 * a FE_OVERFLOW exception occurs when calculating exp.
 * But the result of tanh is no overflow problem,
 * so we can add some tricks to prevent exp calculate an excessive value.
 *
 */
#include <fenv.h>
#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
#include "paddle/utils/Excepts.h"

using namespace paddle;     // NOLINT

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 {
      LOG(FATAL) << "should not reach here";
    }
  }
}

template<typename Matrix>
void testTanh(real illegal) {
  MatrixPtr A = std::make_shared<Matrix>(10, 10);
  MatrixPtr B = std::make_shared<Matrix>(10, 10);
  A->randomizeUniform();
  B->randomizeUniform();

  SetTensorValue(*A, illegal);

  A->tanh(*B);
}

template<typename Matrix>
void testSigmoid(real illegal) {
  MatrixPtr A = std::make_shared<Matrix>(10, 10);
  MatrixPtr B = std::make_shared<Matrix>(10, 10);
  A->randomizeUniform();
  B->randomizeUniform();

  SetTensorValue(*A, illegal);

  A->sigmoid(*B);
}

TEST(fp, overflow) {
  for (auto illegal : {-90.0, 90.0}) {
    LOG(INFO) << " illegal=" << illegal;
    testTanh<CpuMatrix>(illegal);
    testSigmoid<CpuMatrix>(illegal);
  }
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);

  feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
  return RUN_ALL_TESTS();
}