test_matrixUtil.h 7.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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 <gtest/gtest.h>
X
Xin Pan 已提交
17
#include <paddle/legacy/utils/Util.h>
X
Xin Pan 已提交
18
#include "paddle/legacy/math/SparseMatrix.h"
Z
zhangjinchao01 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32

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));
    }
  }
}

33 34 35 36 37 38 39 40 41 42 43
void checkSMatrixEqual(const CpuSparseMatrix& a, const CpuSparseMatrix& 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]);
  }
}

Z
zhangjinchao01 已提交
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
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]);
    }
  }
}

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
void checkSMatrixEqual2Dense(const CpuSparseMatrix& a, const CpuMatrix& 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]));
        }
      }
    }
  }
}

Z
zhangjinchao01 已提交
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
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]));
        }
      }
    }
  }
}

148
void checkSMatrixErr(const CpuSparseMatrixPtr& a, const CpuSparseMatrixPtr& b) {
Z
zhangjinchao01 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#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 已提交
167 168
        if (std::abs(aVal - bVal) > err) {
          if ((std::abs(aVal - bVal) / std::abs(aVal)) > (err / 10.0f)) {
169 170
            LOG(INFO) << "a=" << aVal << "\t"
                      << "b=" << bVal;
Z
zhangjinchao01 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
            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 已提交
185 186
        if (std::abs(aVal - bVal) > err) {
          if ((std::abs(aVal - bVal) / std::abs(aVal)) > (err / 10.0f)) {
Z
zhangjinchao01 已提交
187 188 189 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
            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 已提交
217 218
      if (std::abs(a - b) > err) {
        if ((std::abs(a - b) / std::abs(a)) > (err / 10.0f)) {
Z
zhangjinchao01 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
          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