test_matrixUtil.h 6.2 KB
Newer Older
Z
zhangjinchao01 已提交
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/* 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. */

#pragma once
#include <gtest/gtest.h>
#include <paddle/utils/Util.h>
#include "paddle/math/SparseMatrix.h"

namespace paddle {

void checkMatrixEqual(const MatrixPtr& a, const MatrixPtr& b) {
  ASSERT_EQ(a->getWidth(), b->getWidth());
  ASSERT_EQ(a->getHeight(), b->getHeight());
  ASSERT_EQ(a->isTransposed(), b->isTransposed());
  for (size_t r = 0; r < a->getHeight(); ++r) {
    for (size_t c = 0; c < a->getWidth(); ++c) {
      ASSERT_FLOAT_EQ(a->getElement(r, c), b->getElement(r, c));
    }
  }
}

void checkSMatrixEqual(const CpuSparseMatrixPtr& a,
                       const CpuSparseMatrixPtr& b) {
  ASSERT_EQ(a->getWidth(), b->getWidth());
  ASSERT_EQ(a->getHeight(), b->getHeight());
  ASSERT_EQ(a->isTransposed(), b->isTransposed());
  ASSERT_EQ(a->getFormat(), b->getFormat());
  ASSERT_EQ(a->getElementCnt(), b->getElementCnt());
  for (size_t r = 0; r < a->getElementCnt(); ++r) {
    ASSERT_FLOAT_EQ(a->getValue()[r], b->getValue()[r]);
  }
}

void checkSMatrixEqual2(const CpuSparseMatrixPtr& a,
                        const CpuSparseMatrixPtr& b) {
  ASSERT_EQ(a->getWidth(), b->getWidth());
  ASSERT_EQ(a->getHeight(), b->getHeight());
  ASSERT_EQ(a->isTransposed(), b->isTransposed());
  ASSERT_EQ(a->getFormat(), b->getFormat());
  ASSERT_EQ(a->getValueType(), b->getValueType());
  ASSERT_EQ(a->getElementCnt(), b->getElementCnt());
  if (a->getFormat() == SPARSE_CSR) {
    for (size_t r = 0; r < a->getElementCnt(); ++r) {
      ASSERT_EQ(a->getCols()[r], b->getCols()[r]);
      if (a->getValueType() == FLOAT_VALUE) {
        ASSERT_FLOAT_EQ(a->getValue()[r], b->getValue()[r]);
      }
    }
    for (size_t r = 0; r <= a->getHeight(); r++) {
      ASSERT_EQ(a->getRows()[r], b->getRows()[r]);
    }
  } else {
    for (size_t r = 0; r < a->getElementCnt(); ++r) {
      ASSERT_EQ(a->getRows()[r], b->getRows()[r]);
      if (a->getValueType() == FLOAT_VALUE) {
        ASSERT_FLOAT_EQ(a->getValue()[r], b->getValue()[r]);
      }
    }
    for (size_t r = 0; r <= a->getWidth(); r++) {
      ASSERT_EQ(a->getCols()[r], b->getCols()[r]);
    }
  }
}

void checkSMatrixEqual2Dense(const CpuSparseMatrixPtr& a,
                             const CpuMatrixPtr& b) {
  ASSERT_EQ(a->getWidth(), b->getWidth());
  ASSERT_EQ(a->getHeight(), b->getHeight());
  ASSERT_EQ(a->isTransposed(), b->isTransposed());

  if (a->getFormat() == SPARSE_CSC) {
    int* rows = a->getRows();
    for (size_t i = 0; i < a->getWidth(); i++) {
      for (size_t j = a->getColStartIdx(i); j < a->getColStartIdx(i + 1); j++) {
        if (a->getValueType() == FLOAT_VALUE) {
          ASSERT_FLOAT_EQ(a->getValue()[j], b->getElement(rows[j], i));
        } else {
          ASSERT_FLOAT_EQ(1.0, b->getElement(rows[j], i));
        }
      }
    }
  } else {
    int* cols = a->getCols();
    for (size_t i = 0; i < a->getHeight(); i++) {
      for (size_t j = a->getRowStartIdx(i); j < a->getRowStartIdx(i + 1); j++) {
        if (a->getValueType() == FLOAT_VALUE) {
          ASSERT_FLOAT_EQ(a->getValue()[j], b->getElement(i, cols[j]));
        } else {
          ASSERT_FLOAT_EQ(1.0, b->getElement(i, cols[j]));
        }
      }
    }
  }
}

void checkSMatrixErr(const CpuSparseMatrixPtr& a,
                     const CpuSparseMatrixPtr& b) {
#ifndef PADDLE_TYPE_DOUBLE
  real err = 1e-3;
#else
  real err = 1e-10;
#endif
  ASSERT_EQ(a->getWidth(), b->getWidth());
  ASSERT_EQ(a->getHeight(), b->getHeight());
  ASSERT_EQ(a->isTransposed(), b->isTransposed());
  ASSERT_EQ(a->getFormat(), b->getFormat());
  ASSERT_EQ(a->getValueType(), b->getValueType());
  ASSERT_EQ(a->getElementCnt(), b->getElementCnt());
  int count = 0;
  if (a->getFormat() == SPARSE_CSR) {
    for (size_t r = 0; r < a->getElementCnt(); ++r) {
      ASSERT_EQ(a->getCols()[r], b->getCols()[r]);
      if (a->getValueType() == FLOAT_VALUE) {
        real aVal = a->getValue()[r];
        real bVal = b->getValue()[r];
L
liaogang 已提交
127 128
        if (std::abs(aVal - bVal) > err) {
          if ((std::abs(aVal - bVal) / std::abs(aVal)) > (err / 10.0f)) {
Z
zhangjinchao01 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            LOG(INFO) << "a=" << aVal << "\t" << "b=" << bVal;
            count++;
          }
        }
      }
    }
    for (size_t r = 0; r <= a->getHeight(); r++) {
      ASSERT_EQ(a->getRows()[r], b->getRows()[r]);
    }
  } else {
    for (size_t r = 0; r < a->getElementCnt(); ++r) {
      ASSERT_EQ(a->getRows()[r], b->getRows()[r]);
      if (a->getValueType() == FLOAT_VALUE) {
        real aVal = a->getValue()[r];
        real bVal = b->getValue()[r];
L
liaogang 已提交
144 145
        if (std::abs(aVal - bVal) > err) {
          if ((std::abs(aVal - bVal) / std::abs(aVal)) > (err / 10.0f)) {
Z
zhangjinchao01 已提交
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
            count++;
          }
        }
      }
    }
    for (size_t r = 0; r <= a->getWidth(); r++) {
      ASSERT_EQ(a->getCols()[r], b->getCols()[r]);
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

void checkMatrixErr(const Matrix& matrix1, const Matrix& matrix2) {
  CHECK(matrix1.getHeight() == matrix2.getHeight());
  CHECK(matrix1.getWidth() == matrix2.getWidth());
#ifndef PADDLE_TYPE_DOUBLE
  real err = 1e-3;
#else
  real err = 1e-10;
#endif

  int height = matrix1.getHeight();
  int width = matrix1.getWidth();
  const real* data1 = matrix1.getData();
  const real* data2 = matrix2.getData();
  int count = 0;
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      real a = data1[i * width + j];
      real b = data2[i * width + j];
L
liaogang 已提交
176 177
      if (std::abs(a - b) > err) {
        if ((std::abs(a - b) / std::abs(a)) > (err / 10.0f)) {
Z
zhangjinchao01 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
          count++;
        }
      }
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

void checkDataEqual(const real* a, const real* b, size_t size) {
  for (size_t i = 0; i < size; ++i) {
    ASSERT_FLOAT_EQ(a[i], b[i]);
  }
}

}  //  namespace paddle