-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto compare cpu and gpu function. #643
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff7b428
Add a auto compare for BaseMatrix
hedaoyuan 85e0cd7
move some BaseMatrix test from test_matrixCompare.cpp to test_BaseMat…
hedaoyuan 409a8a1
Merge branch 'develop' of https://github.com/baidu/Paddle into auto_c…
hedaoyuan 1bac8e6
modify the file name test_matrix.cpp to test_SparseMatrix.cpp
hedaoyuan 1873945
Add test_Matrix.cpp for auto compare member functions of class Matrix
hedaoyuan f70fc4a
move some test from test_matrixCompare.cpp to test_BaseMatrix.cpp and…
hedaoyuan 1df826e
Add a AutoCompare and move some test form test_matrixCompare.cpp to t…
hedaoyuan c410382
add some comments
hedaoyuan ee52c75
delete BaseMatrixCompare, and add AutoCompare::cmpWithoutArg
hedaoyuan 956b661
Merge branch 'master' into auto_compare
hedaoyuan 1147990
some bug fix
hedaoyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/* 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 | ||
|
||
/** | ||
* This file provides a TensorCheck template function, which can be used to | ||
* compare CpuMatrix and GpuMatrix, CpuVector and GpuVector, and so on. | ||
*/ | ||
|
||
#include <cmath> | ||
#include "paddle/math/Matrix.h" | ||
|
||
namespace autotest { | ||
|
||
using paddle::Matrix; | ||
using paddle::CpuMatrix; | ||
using paddle::GpuMatrix; | ||
using paddle::VectorT; | ||
using paddle::CpuVectorT; | ||
using paddle::GpuVectorT; | ||
|
||
class AssertEqual { | ||
public: | ||
AssertEqual(real err = 0) : err_(err) {} | ||
|
||
inline bool operator()(real a, real b) { | ||
if (err_ == 0) { | ||
if (a != b) { | ||
return false; | ||
} | ||
} else { | ||
if (std::fabs(a - b) > err_) { | ||
if ((std::fabs(a - b) / std::fabs(a)) > (err_ / 10.0f)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private: | ||
real err_; | ||
}; | ||
|
||
template <typename Tensor> | ||
class CopyToCpu; | ||
|
||
template <> | ||
class CopyToCpu<CpuMatrix> { | ||
public: | ||
explicit CopyToCpu(const CpuMatrix& arg) : arg_(arg) {} | ||
const CpuMatrix& copiedArg() const { return arg_; } | ||
|
||
private: | ||
const CpuMatrix& arg_; | ||
}; | ||
|
||
template <> | ||
class CopyToCpu<GpuMatrix> { | ||
public: | ||
explicit CopyToCpu(const GpuMatrix& arg) | ||
: arg_(arg.getHeight(), arg.getWidth()) { | ||
arg_.copyFrom(arg); | ||
} | ||
CpuMatrix& copiedArg() { return arg_; } | ||
|
||
private: | ||
CpuMatrix arg_; | ||
}; | ||
|
||
template <> | ||
class CopyToCpu<Matrix> { | ||
public: | ||
explicit CopyToCpu(const Matrix& arg) | ||
: arg_(arg.getHeight(), arg.getWidth()) { | ||
arg_.copyFrom(arg); | ||
} | ||
CpuMatrix& copiedArg() { return arg_; } | ||
|
||
private: | ||
CpuMatrix arg_; | ||
}; | ||
|
||
template <typename T> | ||
class CopyToCpu<CpuVectorT<T>> { | ||
public: | ||
explicit CopyToCpu(const CpuVectorT<T>& arg) : arg_(arg) {} | ||
const CpuVectorT<T>& copiedArg() const { return arg_; } | ||
|
||
private: | ||
const CpuVectorT<T>& arg_; | ||
}; | ||
|
||
template <typename T> | ||
class CopyToCpu<GpuVectorT<T>> { | ||
public: | ||
explicit CopyToCpu(const GpuVectorT<T>& arg) : arg_(arg.getSize()) { | ||
arg_.copyFrom(arg); | ||
} | ||
CpuVectorT<T>& copiedArg() { return arg_; } | ||
|
||
private: | ||
CpuVectorT<T> arg_; | ||
}; | ||
|
||
template <typename T> | ||
class CopyToCpu<VectorT<T>> { | ||
public: | ||
explicit CopyToCpu(const VectorT<T>& arg) : arg_(arg.getSize()) { | ||
arg_.copyFrom(arg); | ||
} | ||
CpuVectorT<T>& copiedArg() { return arg_; } | ||
|
||
private: | ||
CpuVectorT<T> arg_; | ||
}; | ||
|
||
template <typename AssertEq> | ||
void TensorCheck(AssertEq compare, | ||
const CpuMatrix& matrix1, | ||
const CpuMatrix& matrix2) { | ||
CHECK(matrix1.getHeight() == matrix2.getHeight()); | ||
CHECK(matrix1.getWidth() == matrix2.getWidth()); | ||
|
||
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]; | ||
if (!compare(a, b)) { | ||
count++; | ||
} | ||
} | ||
} | ||
EXPECT_EQ(count, 0) << "There are " << count << " different element."; | ||
} | ||
|
||
template <typename AssertEq, class T> | ||
void TensorCheck(AssertEq compare, | ||
const CpuVectorT<T>& vector1, | ||
const CpuVectorT<T>& vector2) { | ||
CHECK(vector1.getSize() == vector2.getSize()); | ||
|
||
const T* data1 = vector1.getData(); | ||
const T* data2 = vector2.getData(); | ||
size_t size = vector1.getSize(); | ||
int count = 0; | ||
for (size_t i = 0; i < size; i++) { | ||
real a = data1[i]; | ||
real b = data2[i]; | ||
if (!compare(a, b)) { | ||
count++; | ||
} | ||
} | ||
EXPECT_EQ(count, 0) << "There are " << count << " different element."; | ||
} | ||
|
||
template <typename AssertEq, typename Tensor1, typename Tensor2> | ||
void TensorCheck(AssertEq compare, | ||
const Tensor1& tensor1, | ||
const Tensor2& tensor2) { | ||
TensorCheck(compare, | ||
CopyToCpu<Tensor1>(tensor1).copiedArg(), | ||
CopyToCpu<Tensor2>(tensor2).copiedArg()); | ||
} | ||
|
||
template <typename AssertEq> | ||
void TensorCheck(AssertEq compare, real args1, real args2) { | ||
EXPECT_EQ(compare(args1, args2), true) << "[Test error] args1 = " << args1 | ||
<< ", args2 = " << args2; | ||
} | ||
|
||
template <typename AssertEq> | ||
void TensorCheck(AssertEq compare, size_t args1, size_t args2) { | ||
EXPECT_EQ(args1, args2) << "[Test error] args1 = " << args1 | ||
<< ", args2 = " << args2; | ||
} | ||
|
||
template <typename Tensor1, typename Tensor2> | ||
void TensorCheckEqual(const Tensor1& tensor1, const Tensor2& tensor2) { | ||
AssertEqual compare(0); | ||
TensorCheck(compare, | ||
CopyToCpu<Tensor1>(tensor1).copiedArg(), | ||
CopyToCpu<Tensor2>(tensor2).copiedArg()); | ||
} | ||
|
||
template <typename Tensor1, typename Tensor2> | ||
void TensorCheckErr(const Tensor1& tensor1, const Tensor2& tensor2) { | ||
#ifndef PADDLE_TYPE_DOUBLE | ||
AssertEqual compare(1e-3); | ||
#else | ||
AssertEqual compare(1e-10); | ||
#endif | ||
TensorCheck(compare, | ||
CopyToCpu<Tensor1>(tensor1).copiedArg(), | ||
CopyToCpu<Tensor2>(tensor2).copiedArg()); | ||
} | ||
|
||
} // namespace autotest |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 header file 里没有 include guard 也没有
pragma once
吗?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done