From c410382145db79ea1cb77b0995ac189264529299 Mon Sep 17 00:00:00 2001 From: hedaoyuan Date: Wed, 30 Nov 2016 23:53:12 +0800 Subject: [PATCH] add some comments --- paddle/math/tests/TensorCheck.h | 11 +++++- paddle/math/tests/TestUtils.h | 63 ++++++++----------------------- paddle/math/tests/test_Matrix.cpp | 9 +++-- 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/paddle/math/tests/TensorCheck.h b/paddle/math/tests/TensorCheck.h index 796f2fce64..956bcf61a4 100644 --- a/paddle/math/tests/TensorCheck.h +++ b/paddle/math/tests/TensorCheck.h @@ -12,9 +12,18 @@ 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 #include "paddle/math/Matrix.h" +namespace autotest { + using paddle::Matrix; using paddle::CpuMatrix; using paddle::GpuMatrix; @@ -22,8 +31,6 @@ using paddle::VectorT; using paddle::CpuVectorT; using paddle::GpuVectorT; -namespace autotest { - class AssertEqual { public: AssertEqual(real err = 0) : err_(err) {} diff --git a/paddle/math/tests/TestUtils.h b/paddle/math/tests/TestUtils.h index fe78f7bf09..85a582258a 100644 --- a/paddle/math/tests/TestUtils.h +++ b/paddle/math/tests/TestUtils.h @@ -12,8 +12,13 @@ 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 + /** * TestUtils.h is used to automatically compare CPU and GPU code is consistent. + * This file provides a class(AutoCompare) and a template + * function(BaseMatrixCompare) to simplify the comparison + * of CPU and GPU member functions. * Refer test_Matrix.cpp and test_BaseMatrix.cpp for how to use autotest. */ @@ -22,14 +27,14 @@ limitations under the License. */ #include "paddle/math/SparseMatrix.h" #include "TensorCheck.h" +namespace autotest { + using paddle::BaseMatrix; using paddle::CpuIVector; using paddle::GpuIVector; using paddle::CpuSparseMatrix; using paddle::GpuSparseMatrix; -namespace autotest { - template class ReplaceType { public: @@ -66,7 +71,7 @@ T construct(int height, int width); template <> float construct(int height, int width) { - return 0.0; + return 0.5; } template <> @@ -89,15 +94,7 @@ GpuMatrix construct(int height, int width) { // init a argument template -void init(T& v); - -template <> -void init(float& v) { - v = 0.5; -} - -template <> -void init(size_t& v) { +void init(T& v) { return; } @@ -125,15 +122,7 @@ template // copy a argument, copy src to dest template -void copy(T1& dest, T2& src); - -template <> -void copy(float& dest, float& src) { - dest = src; -} - -template <> -void copy(size_t& dest, size_t& src) { +void copy(T1& dest, T2& src) { dest = src; } @@ -155,28 +144,6 @@ template copyTuple(dest, src); } -// Compare output -template -inline typename std::enable_if::type checkTuple( - std::tuple& args1, - std::tuple& args2, - AssertEq compare) {} - -template - inline typename std::enable_if < - I::type checkTuple(std::tuple& args1, - std::tuple& args2, - AssertEq compare) { - TensorCheck(compare, std::get(args1), std::get(args2)); - checkTuple(args1, args2, compare); -} - // call member function template class ReturnType { public: @@ -252,32 +220,31 @@ public: }; template -typename ReturnType::type autoArgs(T v) { +typename ReturnType::type autoArgs(T& v) { return v; } template <> -GpuMatrix autoArgs(CpuMatrix v) { +GpuMatrix autoArgs(CpuMatrix& v) { GpuMatrix a(v.getHeight(), v.getWidth()); a.copyFrom(v); return a; } template <> -GpuIVector autoArgs(CpuIVector v) { +GpuIVector autoArgs(CpuIVector& v) { GpuIVector a(v.getSize()); a.copyFrom(v); return a; } template <> -GpuSparseMatrix autoArgs(CpuSparseMatrix v) { +GpuSparseMatrix autoArgs(CpuSparseMatrix& v) { GpuSparseMatrix a(v.getHeight(), v.getWidth(), v.getElementCnt(), v.getValueType(), v.getFormat()); - a.copyFrom(v, HPPL_STREAM_DEFAULT); hl_stream_synchronize(HPPL_STREAM_DEFAULT); return a; diff --git a/paddle/math/tests/test_Matrix.cpp b/paddle/math/tests/test_Matrix.cpp index b766e5ebe2..b2b6aa5b5f 100644 --- a/paddle/math/tests/test_Matrix.cpp +++ b/paddle/math/tests/test_Matrix.cpp @@ -14,7 +14,7 @@ limitations under the License. */ #ifndef PADDLE_ONLY_CPU /** - * This test file use AutoCompare to compares the implementation + * This test file use autotest::AutoCompare to compares the implementation * of CPU and GPU member function in Matrix.cpp. * * 1. Constructs an AutoCompare object, a AutoCompare object contains @@ -28,20 +28,21 @@ limitations under the License. */ * AutoCompare test(...); * Init Argument arg1,arg2... * test(function, arg1, arg2....) - * */ #include #include "TestUtils.h" +using paddle::BaseMatrix; +using paddle::Matrix; using paddle::CpuMatrix; +using paddle::CpuIVector; +using paddle::CpuSparseMatrix; using paddle::SparseValueType; using paddle::SparseFormat; using paddle::NO_VALUE; using paddle::SPARSE_CSR; using paddle::initMain; -using autotest::TensorCheckEqual; -using autotest::TensorCheckErr; using autotest::AutoCompare; void testBilinearFwdBwd(int numSamples, -- GitLab