|
| 1 | +/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +/** |
| 18 | + * This file provides a TensorCheck template function, which can be used to |
| 19 | + * compare CpuMatrix and GpuMatrix, CpuVector and GpuVector, and so on. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <cmath> |
| 23 | +#include "paddle/math/Matrix.h" |
| 24 | + |
| 25 | +namespace autotest { |
| 26 | + |
| 27 | +using paddle::Matrix; |
| 28 | +using paddle::CpuMatrix; |
| 29 | +using paddle::GpuMatrix; |
| 30 | +using paddle::VectorT; |
| 31 | +using paddle::CpuVectorT; |
| 32 | +using paddle::GpuVectorT; |
| 33 | + |
| 34 | +class AssertEqual { |
| 35 | +public: |
| 36 | + AssertEqual(real err = 0) : err_(err) {} |
| 37 | + |
| 38 | + inline bool operator()(real a, real b) { |
| 39 | + if (err_ == 0) { |
| 40 | + if (a != b) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + } else { |
| 44 | + if (std::fabs(a - b) > err_) { |
| 45 | + if ((std::fabs(a - b) / std::fabs(a)) > (err_ / 10.0f)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | +private: |
| 55 | + real err_; |
| 56 | +}; |
| 57 | + |
| 58 | +template <typename Tensor> |
| 59 | +class CopyToCpu; |
| 60 | + |
| 61 | +template <> |
| 62 | +class CopyToCpu<CpuMatrix> { |
| 63 | +public: |
| 64 | + explicit CopyToCpu(const CpuMatrix& arg) : arg_(arg) {} |
| 65 | + const CpuMatrix& copiedArg() const { return arg_; } |
| 66 | + |
| 67 | +private: |
| 68 | + const CpuMatrix& arg_; |
| 69 | +}; |
| 70 | + |
| 71 | +template <> |
| 72 | +class CopyToCpu<GpuMatrix> { |
| 73 | +public: |
| 74 | + explicit CopyToCpu(const GpuMatrix& arg) |
| 75 | + : arg_(arg.getHeight(), arg.getWidth()) { |
| 76 | + arg_.copyFrom(arg); |
| 77 | + } |
| 78 | + CpuMatrix& copiedArg() { return arg_; } |
| 79 | + |
| 80 | +private: |
| 81 | + CpuMatrix arg_; |
| 82 | +}; |
| 83 | + |
| 84 | +template <> |
| 85 | +class CopyToCpu<Matrix> { |
| 86 | +public: |
| 87 | + explicit CopyToCpu(const Matrix& arg) |
| 88 | + : arg_(arg.getHeight(), arg.getWidth()) { |
| 89 | + arg_.copyFrom(arg); |
| 90 | + } |
| 91 | + CpuMatrix& copiedArg() { return arg_; } |
| 92 | + |
| 93 | +private: |
| 94 | + CpuMatrix arg_; |
| 95 | +}; |
| 96 | + |
| 97 | +template <typename T> |
| 98 | +class CopyToCpu<CpuVectorT<T>> { |
| 99 | +public: |
| 100 | + explicit CopyToCpu(const CpuVectorT<T>& arg) : arg_(arg) {} |
| 101 | + const CpuVectorT<T>& copiedArg() const { return arg_; } |
| 102 | + |
| 103 | +private: |
| 104 | + const CpuVectorT<T>& arg_; |
| 105 | +}; |
| 106 | + |
| 107 | +template <typename T> |
| 108 | +class CopyToCpu<GpuVectorT<T>> { |
| 109 | +public: |
| 110 | + explicit CopyToCpu(const GpuVectorT<T>& arg) : arg_(arg.getSize()) { |
| 111 | + arg_.copyFrom(arg); |
| 112 | + } |
| 113 | + CpuVectorT<T>& copiedArg() { return arg_; } |
| 114 | + |
| 115 | +private: |
| 116 | + CpuVectorT<T> arg_; |
| 117 | +}; |
| 118 | + |
| 119 | +template <typename T> |
| 120 | +class CopyToCpu<VectorT<T>> { |
| 121 | +public: |
| 122 | + explicit CopyToCpu(const VectorT<T>& arg) : arg_(arg.getSize()) { |
| 123 | + arg_.copyFrom(arg); |
| 124 | + } |
| 125 | + CpuVectorT<T>& copiedArg() { return arg_; } |
| 126 | + |
| 127 | +private: |
| 128 | + CpuVectorT<T> arg_; |
| 129 | +}; |
| 130 | + |
| 131 | +template <typename AssertEq> |
| 132 | +void TensorCheck(AssertEq compare, |
| 133 | + const CpuMatrix& matrix1, |
| 134 | + const CpuMatrix& matrix2) { |
| 135 | + CHECK(matrix1.getHeight() == matrix2.getHeight()); |
| 136 | + CHECK(matrix1.getWidth() == matrix2.getWidth()); |
| 137 | + |
| 138 | + int height = matrix1.getHeight(); |
| 139 | + int width = matrix1.getWidth(); |
| 140 | + const real* data1 = matrix1.getData(); |
| 141 | + const real* data2 = matrix2.getData(); |
| 142 | + int count = 0; |
| 143 | + for (int i = 0; i < height; i++) { |
| 144 | + for (int j = 0; j < width; j++) { |
| 145 | + real a = data1[i * width + j]; |
| 146 | + real b = data2[i * width + j]; |
| 147 | + if (!compare(a, b)) { |
| 148 | + count++; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + EXPECT_EQ(count, 0) << "There are " << count << " different element."; |
| 153 | +} |
| 154 | + |
| 155 | +template <typename AssertEq, class T> |
| 156 | +void TensorCheck(AssertEq compare, |
| 157 | + const CpuVectorT<T>& vector1, |
| 158 | + const CpuVectorT<T>& vector2) { |
| 159 | + CHECK(vector1.getSize() == vector2.getSize()); |
| 160 | + |
| 161 | + const T* data1 = vector1.getData(); |
| 162 | + const T* data2 = vector2.getData(); |
| 163 | + size_t size = vector1.getSize(); |
| 164 | + int count = 0; |
| 165 | + for (size_t i = 0; i < size; i++) { |
| 166 | + real a = data1[i]; |
| 167 | + real b = data2[i]; |
| 168 | + if (!compare(a, b)) { |
| 169 | + count++; |
| 170 | + } |
| 171 | + } |
| 172 | + EXPECT_EQ(count, 0) << "There are " << count << " different element."; |
| 173 | +} |
| 174 | + |
| 175 | +template <typename AssertEq, typename Tensor1, typename Tensor2> |
| 176 | +void TensorCheck(AssertEq compare, |
| 177 | + const Tensor1& tensor1, |
| 178 | + const Tensor2& tensor2) { |
| 179 | + TensorCheck(compare, |
| 180 | + CopyToCpu<Tensor1>(tensor1).copiedArg(), |
| 181 | + CopyToCpu<Tensor2>(tensor2).copiedArg()); |
| 182 | +} |
| 183 | + |
| 184 | +template <typename AssertEq> |
| 185 | +void TensorCheck(AssertEq compare, real args1, real args2) { |
| 186 | + EXPECT_EQ(compare(args1, args2), true) << "[Test error] args1 = " << args1 |
| 187 | + << ", args2 = " << args2; |
| 188 | +} |
| 189 | + |
| 190 | +template <typename AssertEq> |
| 191 | +void TensorCheck(AssertEq compare, size_t args1, size_t args2) { |
| 192 | + EXPECT_EQ(args1, args2) << "[Test error] args1 = " << args1 |
| 193 | + << ", args2 = " << args2; |
| 194 | +} |
| 195 | + |
| 196 | +template <typename Tensor1, typename Tensor2> |
| 197 | +void TensorCheckEqual(const Tensor1& tensor1, const Tensor2& tensor2) { |
| 198 | + AssertEqual compare(0); |
| 199 | + TensorCheck(compare, |
| 200 | + CopyToCpu<Tensor1>(tensor1).copiedArg(), |
| 201 | + CopyToCpu<Tensor2>(tensor2).copiedArg()); |
| 202 | +} |
| 203 | + |
| 204 | +template <typename Tensor1, typename Tensor2> |
| 205 | +void TensorCheckErr(const Tensor1& tensor1, const Tensor2& tensor2) { |
| 206 | +#ifndef PADDLE_TYPE_DOUBLE |
| 207 | + AssertEqual compare(1e-3); |
| 208 | +#else |
| 209 | + AssertEqual compare(1e-10); |
| 210 | +#endif |
| 211 | + TensorCheck(compare, |
| 212 | + CopyToCpu<Tensor1>(tensor1).copiedArg(), |
| 213 | + CopyToCpu<Tensor2>(tensor2).copiedArg()); |
| 214 | +} |
| 215 | + |
| 216 | +} // namespace autotest |
0 commit comments