From 7e0b51f28f475c52764b055b183fd780e3e240ac Mon Sep 17 00:00:00 2001 From: hedaoyuan Date: Mon, 24 Oct 2016 11:51:30 +0800 Subject: [PATCH] some bugs fix --- paddle/cuda/include/hl_tensor_ops.h | 22 ++-- paddle/math/TensorApply.h | 22 ++-- paddle/math/TensorEvaluate.h | 22 ++-- paddle/math/TensorExpression.h | 22 ++-- paddle/math/TrainingAlgorithmOp.cu | 22 ++-- paddle/math/TrainingAlgorithmOp.h | 22 ++-- paddle/math/tests/OriginalOptimizerApi.h | 21 ++-- paddle/math/tests/TensorCheck.h | 44 +------ paddle/math/tests/test_Tensor.cu | 116 +++---------------- paddle/math/tests/test_TrainingAlgorithm.cpp | 52 +++++++-- paddle/math/tests/test_lazyAssign.cu | 11 +- 11 files changed, 161 insertions(+), 215 deletions(-) diff --git a/paddle/cuda/include/hl_tensor_ops.h b/paddle/cuda/include/hl_tensor_ops.h index 0912083ba0a..28defec103b 100644 --- a/paddle/cuda/include/hl_tensor_ops.h +++ b/paddle/cuda/include/hl_tensor_ops.h @@ -1,12 +1,16 @@ -/** - * hl_tensor_ops.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-06 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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. */ #ifndef HL_TENSOR_OPS_H_ #define HL_TENSOR_OPS_H_ diff --git a/paddle/math/TensorApply.h b/paddle/math/TensorApply.h index 4f19d5232a4..87948beb287 100644 --- a/paddle/math/TensorApply.h +++ b/paddle/math/TensorApply.h @@ -1,12 +1,16 @@ -/** - * TensorApply.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-06 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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 diff --git a/paddle/math/TensorEvaluate.h b/paddle/math/TensorEvaluate.h index 1abfee602fa..cf4894e2cba 100644 --- a/paddle/math/TensorEvaluate.h +++ b/paddle/math/TensorEvaluate.h @@ -1,12 +1,16 @@ -/** - * TensorEvaluate.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-06 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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 diff --git a/paddle/math/TensorExpression.h b/paddle/math/TensorExpression.h index a5d4fa9d72e..a98b6bb0d5e 100644 --- a/paddle/math/TensorExpression.h +++ b/paddle/math/TensorExpression.h @@ -1,12 +1,16 @@ -/** - * TensorExpression.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-06 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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 diff --git a/paddle/math/TrainingAlgorithmOp.cu b/paddle/math/TrainingAlgorithmOp.cu index 4698fb179bd..864ed0840b9 100644 --- a/paddle/math/TrainingAlgorithmOp.cu +++ b/paddle/math/TrainingAlgorithmOp.cu @@ -1,12 +1,16 @@ -/** - * TrainingAlgorithmOp.cu - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-29 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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. */ #include "paddle/utils/Logging.h" #include "BaseMatrix.h" diff --git a/paddle/math/TrainingAlgorithmOp.h b/paddle/math/TrainingAlgorithmOp.h index 5c413328a0e..2b8bd01aa54 100644 --- a/paddle/math/TrainingAlgorithmOp.h +++ b/paddle/math/TrainingAlgorithmOp.h @@ -1,12 +1,16 @@ -/** - * TrainingAlgorithmOp.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-29 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - * - */ +/* 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 diff --git a/paddle/math/tests/OriginalOptimizerApi.h b/paddle/math/tests/OriginalOptimizerApi.h index 9a2e38246f5..799dc89dd9a 100644 --- a/paddle/math/tests/OriginalOptimizerApi.h +++ b/paddle/math/tests/OriginalOptimizerApi.h @@ -1,11 +1,16 @@ -/** - * OriginalOptimizerApi.h - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-29 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - */ +/* 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 diff --git a/paddle/math/tests/TensorCheck.h b/paddle/math/tests/TensorCheck.h index d11e1376e4e..8ffe1ea0759 100644 --- a/paddle/math/tests/TensorCheck.h +++ b/paddle/math/tests/TensorCheck.h @@ -9,6 +9,7 @@ #include #include "paddle/math/Matrix.h" + using namespace paddle; // NOLINT using namespace std; // NOLINT @@ -105,50 +106,9 @@ void TensorCheckEqual(const GpuVectorT& vector1, TensorCheckEqual(cpu1, cpu2); } -int VectorCheckErr(const Vector& vector1, const Vector& vector2) { - CHECK(vector1.getSize() == vector2.getSize()); - - const real* data1 = vector1.getData(); - const real* 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 (fabs(a - b) > FLAGS_max_diff) { - if ((fabsf(a - b) / fabsf(a)) > (FLAGS_max_diff / 10.0f)) { - count++; - } - } - } - - return count; -} - -#define INIT_UNARY(A1, A2) \ - Tensor A1(height, width); \ - Tensor A2(height, width); \ - A1.randomizeUniform(); \ - A2.copyFrom(A1) -#define INIT_BINARY(A1, A2, B) \ - INIT_UNARY(A1, A2); \ - Tensor B(height, width); \ - B.randomizeUniform() -#define INIT_TERNARY(A1, A2, B, C) \ - INIT_BINARY(A1, A2, B); \ - Tensor C(height, width); \ - C.randomizeUniform() -#define INIT_QUATERNARY(A1, A2, B, C, D) \ - INIT_TERNARY(A1, A2, B, C); \ - Tensor D(height, width); \ - D.randomizeUniform() - // Performance Check #ifdef PADDLE_DISABLE_TIMER -#define CHECK_VECTORPTR(vector1, vector2) \ - EXPECT_EQ(VectorCheckErr(vector1, vector2), 0) - #define EXPRESSION_PERFORMANCE(expression) \ expression; @@ -156,8 +116,6 @@ int VectorCheckErr(const Vector& vector1, const Vector& vector2) { #include "paddle/utils/Stat.h" -#define CHECK_VECTORPTR(vector1, vector2) - #define EXPRESSION_PERFORMANCE(expression) \ do {\ char expr[30];\ diff --git a/paddle/math/tests/test_Tensor.cu b/paddle/math/tests/test_Tensor.cu index 34a13c2e9c9..bd406da8451 100644 --- a/paddle/math/tests/test_Tensor.cu +++ b/paddle/math/tests/test_Tensor.cu @@ -1,109 +1,23 @@ -/** - * test_Tensor.cpp - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-06 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - */ +/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve. -#include -#include "paddle/math/Matrix.h" -using namespace paddle; // NOLINT -using namespace std; // NOLINT - -template -extern void TensorCheckEqual(const Tensor& tensor1, const Tensor& tensor2); - -void TensorCheckEqual(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++) { - if (data1[i * width + j] != data2[i * width + j]) { - count++; - } - } - } - EXPECT_EQ(count, 0) << "There are " << count << " different element."; -} - -void TensorCheckEqual(const GpuMatrix& matrix1, const GpuMatrix& matrix2) { - CpuMatrix cpu1(matrix1.getHeight(), matrix1.getWidth()); - CpuMatrix cpu2(matrix2.getHeight(), matrix2.getWidth()); - cpu1.copyFrom(matrix1); - cpu2.copyFrom(matrix2); - TensorCheckEqual(cpu1, cpu2); -} - -void TensorCheckErr(const CpuMatrix& matrix1, const CpuMatrix& matrix2) { - CHECK(matrix1.getHeight() == matrix2.getHeight()); - CHECK(matrix1.getWidth() == matrix2.getWidth()); -#ifndef PADDLE_TYPE_DOUBLE - real err = 1e-5; -#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]; - if (fabs(a - b) > err) { - if ((fabsf(a - b) / fabsf(a)) > (err / 10.0f)) { - count++; - } - } - } - } - EXPECT_EQ(count, 0) << "There are " << count << " different element."; -} +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 -void TensorCheckErr(const GpuMatrix& matrix1, const GpuMatrix& matrix2) { - CpuMatrix cpu1(matrix1.getHeight(), matrix1.getWidth()); - CpuMatrix cpu2(matrix2.getHeight(), matrix2.getWidth()); - cpu1.copyFrom(matrix1); - cpu2.copyFrom(matrix2); - TensorCheckErr(cpu1, cpu2); -} + http://www.apache.org/licenses/LICENSE-2.0 -template -void TensorCheckEqual(const CpuVectorT& vector1, - const CpuVectorT& vector2) { - CHECK(vector1.getSize() == vector2.getSize()); +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. */ - 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++) { - if (data1[i] != data2[i]) { - count++; - } - } - EXPECT_EQ(count, 0) << "There are " << count << " different element."; -} +#include +#include "paddle/math/Matrix.h" +#include "TensorCheck.h" -template -void TensorCheckEqual(const GpuVectorT& vector1, - const GpuVectorT& vector2) { - CpuVectorT cpu1(vector1.getSize()); - CpuVectorT cpu2(vector2.getSize()); - cpu1.copyFrom(vector1); - cpu2.copyFrom(vector2); - TensorCheckEqual(cpu1, cpu2); -} +using namespace paddle; // NOLINT +using namespace std; // NOLINT #define INIT_UNARY(A1, A2) \ Tensor A1(height, width); \ diff --git a/paddle/math/tests/test_TrainingAlgorithm.cpp b/paddle/math/tests/test_TrainingAlgorithm.cpp index a4218118b81..2c5807d9661 100644 --- a/paddle/math/tests/test_TrainingAlgorithm.cpp +++ b/paddle/math/tests/test_TrainingAlgorithm.cpp @@ -1,11 +1,16 @@ -/** - * test_TrainingAlgorithm.cpp - * - * Author: hedaoyuan (hedaoyuan@baidu.com) - * Created on: 2016-06-29 - * - * Copyright (c) Baidu.com, Inc. All Rights Reserved - */ +/* 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. */ #include #include "paddle/utils/Util.h" @@ -44,6 +49,26 @@ private: }\ } while (0) +int VectorCheckErr(const Vector& vector1, const Vector& vector2) { + CHECK(vector1.getSize() == vector2.getSize()); + + const real* data1 = vector1.getData(); + const real* 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 (fabs(a - b) > FLAGS_max_diff) { + if ((fabsf(a - b) / fabsf(a)) > (FLAGS_max_diff / 10.0f)) { + count++; + } + } + } + + return count; +} + int VectorCheckErr(const VectorPtr& vector1, const VectorPtr& vector2) { VectorPtr tmp1; VectorPtr tmp2; @@ -52,6 +77,17 @@ int VectorCheckErr(const VectorPtr& vector1, const VectorPtr& vector2) { return VectorCheckErr(*tmp1, *tmp2); } +#ifdef PADDLE_DISABLE_TIMER + +#define CHECK_VECTORPTR(vector1, vector2) \ + EXPECT_EQ(VectorCheckErr(vector1, vector2), 0) + +#else + +#define CHECK_VECTORPTR(vector1, vector2) + +#endif + typedef std::function testMatrixFunc; void testCase(testMatrixFunc matrixFunc) { diff --git a/paddle/math/tests/test_lazyAssign.cu b/paddle/math/tests/test_lazyAssign.cu index 070a9a92dd6..51cc874019d 100644 --- a/paddle/math/tests/test_lazyAssign.cu +++ b/paddle/math/tests/test_lazyAssign.cu @@ -27,7 +27,16 @@ void testMatrixCase(testMatrixFunc matrixFunc) { template void testLazyAssign(int height, int width) { - INIT_QUATERNARY(A1, A2, B, C, D); + Tensor A1(height, width); + Tensor A2(height, width); + Tensor B(height, width); + Tensor C(height, width); + Tensor D(height, width); + A1.randomizeUniform(); + B.randomizeUniform(); + C.randomizeUniform(); + D.randomizeUniform(); + A2.copyFrom(A1); EXPRESSION_PERFORMANCE(A1 = B + C; A1 = A1 * D;); -- GitLab