提交 b6ec45d8 编写于 作者: A Abhinav Arora

Fix CPPLint warnings in tensor_util_test

上级 12ae354a
...@@ -105,16 +105,14 @@ TEST(TensorCopy, Tensor) { ...@@ -105,16 +105,14 @@ TEST(TensorCopy, Tensor) {
} }
TEST(TensorFromVector, Tensor) { TEST(TensorFromVector, Tensor) {
using namespace paddle::framework;
using namespace paddle::platform;
{ {
std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Tensor cpu_tensor; paddle::framework::Tensor cpu_tensor;
// Copy to CPU Tensor // Copy to CPU Tensor
cpu_tensor.Resize(make_ddim({3, 3})); cpu_tensor.Resize(paddle::framework::make_ddim({3, 3}));
auto cpu_place = new paddle::platform::CPUPlace(); auto cpu_place = new paddle::platform::CPUPlace();
TensorFromVector<int>(src_vec, &cpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, &cpu_tensor);
// Compare Tensors // Compare Tensors
const int* cpu_ptr = cpu_tensor.data<int>(); const int* cpu_ptr = cpu_tensor.data<int>();
...@@ -125,8 +123,8 @@ TEST(TensorFromVector, Tensor) { ...@@ -125,8 +123,8 @@ TEST(TensorFromVector, Tensor) {
} }
src_vec.erase(src_vec.begin(), src_vec.begin() + 5); src_vec.erase(src_vec.begin(), src_vec.begin() + 5);
cpu_tensor.Resize(make_ddim({2, 2})); cpu_tensor.Resize(paddle::framework::make_ddim({2, 2}));
TensorFromVector<int>(src_vec, &cpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, &cpu_tensor);
cpu_ptr = cpu_tensor.data<int>(); cpu_ptr = cpu_tensor.data<int>();
src_ptr = src_vec.data(); src_ptr = src_vec.data();
ASSERT_NE(src_ptr, cpu_ptr); ASSERT_NE(src_ptr, cpu_ptr);
...@@ -140,23 +138,23 @@ TEST(TensorFromVector, Tensor) { ...@@ -140,23 +138,23 @@ TEST(TensorFromVector, Tensor) {
#ifdef PADDLE_WITH_CUDA #ifdef PADDLE_WITH_CUDA
{ {
std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Tensor cpu_tensor; paddle::framework::Tensor cpu_tensor;
Tensor gpu_tensor; paddle::framework::Tensor gpu_tensor;
Tensor dst_tensor; paddle::framework::Tensor dst_tensor;
// Copy to CPU Tensor // Copy to CPU Tensor
cpu_tensor.Resize(make_ddim({3, 3})); cpu_tensor.Resize(make_ddim({3, 3}));
auto cpu_place = new paddle::platform::CPUPlace(); auto cpu_place = new paddle::platform::CPUPlace();
CPUDeviceContext cpu_ctx(*cpu_place); paddle::platform::CPUDeviceContext cpu_ctx(*cpu_place);
TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);
// Copy to GPUTensor // Copy to GPUTensor
gpu_tensor.Resize(make_ddim({3, 3})); gpu_tensor.Resize(paddle::framework::make_ddim({3, 3}));
auto gpu_place = new paddle::platform::CUDAPlace(); auto gpu_place = new paddle::platform::CUDAPlace();
CUDADeviceContext gpu_ctx(*gpu_place); paddle::platform::CUDADeviceContext gpu_ctx(*gpu_place);
TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
// Copy from GPU to CPU tensor for comparison // Copy from GPU to CPU tensor for comparison
TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor); paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
// Sync before Compare Tensors // Sync before Compare Tensors
gpu_ctx.Wait(); gpu_ctx.Wait();
...@@ -172,11 +170,11 @@ TEST(TensorFromVector, Tensor) { ...@@ -172,11 +170,11 @@ TEST(TensorFromVector, Tensor) {
src_vec.erase(src_vec.begin(), src_vec.begin() + 5); src_vec.erase(src_vec.begin(), src_vec.begin() + 5);
cpu_tensor.Resize(make_ddim({2, 2})); cpu_tensor.Resize(paddle::framework::make_ddim({2, 2}));
TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);
gpu_tensor.Resize(make_ddim({2, 2})); gpu_tensor.Resize(paddle::framework::make_ddim({2, 2}));
TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor); paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
// Sync before Compare Tensors // Sync before Compare Tensors
gpu_ctx.Wait(); gpu_ctx.Wait();
...@@ -197,18 +195,16 @@ TEST(TensorFromVector, Tensor) { ...@@ -197,18 +195,16 @@ TEST(TensorFromVector, Tensor) {
} }
TEST(TensorToVector, Tensor) { TEST(TensorToVector, Tensor) {
using namespace paddle::framework;
using namespace paddle::platform;
{ {
Tensor src; paddle::framework::Tensor src;
int* src_ptr = src.mutable_data<int>({3, 3}, CPUPlace()); int* src_ptr = src.mutable_data<int>({3, 3}, paddle::platform::CPUPlace());
for (int i = 0; i < 3 * 3; ++i) { for (int i = 0; i < 3 * 3; ++i) {
src_ptr[i] = i; src_ptr[i] = i;
} }
CPUPlace place; paddle::platform::CPUPlace place;
std::vector<int> dst; std::vector<int> dst;
TensorToVector<int>(src, &dst); paddle::framework::TensorToVector<int>(src, &dst);
for (int i = 0; i < 3 * 3; ++i) { for (int i = 0; i < 3 * 3; ++i) {
EXPECT_EQ(src_ptr[i], dst[i]); EXPECT_EQ(src_ptr[i], dst[i]);
...@@ -217,13 +213,13 @@ TEST(TensorToVector, Tensor) { ...@@ -217,13 +213,13 @@ TEST(TensorToVector, Tensor) {
#ifdef PADDLE_WITH_CUDA #ifdef PADDLE_WITH_CUDA
{ {
std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Tensor gpu_tensor; paddle::framework::Tensor gpu_tensor;
CUDAPlace place; paddle::platform::CUDAPlace place;
CUDADeviceContext gpu_ctx(place); paddle::platform::CUDADeviceContext gpu_ctx(place);
TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor); paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
std::vector<int> dst; std::vector<int> dst;
TensorToVector<int>(gpu_tensor, gpu_ctx, &dst); paddle::framework::TensorToVector<int>(gpu_tensor, gpu_ctx, &dst);
for (int i = 0; i < 3 * 3; ++i) { for (int i = 0; i < 3 * 3; ++i) {
EXPECT_EQ(src_vec[i], dst[i]); EXPECT_EQ(src_vec[i], dst[i]);
...@@ -233,54 +229,50 @@ TEST(TensorToVector, Tensor) { ...@@ -233,54 +229,50 @@ TEST(TensorToVector, Tensor) {
} }
TEST(TensorContainsNAN, CPU) { TEST(TensorContainsNAN, CPU) {
using namespace paddle::framework;
using namespace paddle::platform;
{ {
Tensor src; paddle::framework::Tensor src;
float* buf = src.mutable_data<float>({3}, CPUPlace()); float* buf = src.mutable_data<float>({3}, paddle::platform::CPUPlace());
buf[0] = 0.0; buf[0] = 0.0;
buf[1] = NAN; buf[1] = NAN;
buf[2] = 0.0; buf[2] = 0.0;
ASSERT_TRUE(TensorContainsNAN(src)); ASSERT_TRUE(paddle::framework::TensorContainsNAN(src));
buf[1] = 0.0; buf[1] = 0.0;
ASSERT_FALSE(TensorContainsNAN(src)); ASSERT_FALSE(paddle::framework::TensorContainsNAN(src));
} }
{ {
Tensor src; paddle::framework::Tensor src;
float16* buf = src.mutable_data<float16>({3}, CPUPlace()); float16* buf = src.mutable_data<float16>({3}, paddle::platform::CPUPlace());
buf[0] = 0.0; buf[0] = 0.0;
buf[1].x = 0x7fff; buf[1].x = 0x7fff;
buf[2] = 0.0; buf[2] = 0.0;
ASSERT_TRUE(TensorContainsNAN(src)); ASSERT_TRUE(paddle::framework::TensorContainsNAN(src));
buf[1] = 0.0; buf[1] = 0.0;
ASSERT_FALSE(TensorContainsNAN(src)); ASSERT_FALSE(paddle::framework::TensorContainsNAN(src));
} }
} }
TEST(TensorContainsInf, CPU) { TEST(TensorContainsInf, CPU) {
using namespace paddle::framework;
using namespace paddle::platform;
{ {
Tensor src; paddle::framework::Tensor src;
double* buf = src.mutable_data<double>({3}, CPUPlace()); double* buf = src.mutable_data<double>({3}, paddle::platform::CPUPlace());
buf[0] = 1.0; buf[0] = 1.0;
buf[1] = INFINITY; buf[1] = INFINITY;
buf[2] = 0.0; buf[2] = 0.0;
ASSERT_TRUE(TensorContainsInf(src)); ASSERT_TRUE(paddle::framework::TensorContainsInf(src));
buf[1] = 1.0; buf[1] = 1.0;
ASSERT_FALSE(TensorContainsInf(src)); ASSERT_FALSE(paddle::framework::TensorContainsInf(src));
} }
{ {
Tensor src; paddle::framework::Tensor src;
float16* buf = src.mutable_data<float16>({3}, CPUPlace()); float16* buf = src.mutable_data<float16>({3}, paddle::platform::CPUPlace());
buf[0] = 1.0; buf[0] = 1.0;
buf[1].x = 0x7c00; buf[1].x = 0x7c00;
buf[2] = 0.0; buf[2] = 0.0;
ASSERT_TRUE(TensorContainsInf(src)); ASSERT_TRUE(paddle::framework::TensorContainsInf(src));
buf[1] = 1.0; buf[1] = 1.0;
ASSERT_FALSE(TensorContainsInf(src)); ASSERT_FALSE(paddle::framework::TensorContainsInf(src));
} }
} }
......
...@@ -45,9 +45,8 @@ static __global__ void FillInf(platform::float16* buf) { ...@@ -45,9 +45,8 @@ static __global__ void FillInf(platform::float16* buf) {
} }
TEST(TensorContainsNAN, GPU) { TEST(TensorContainsNAN, GPU) {
using namespace paddle::platform; paddle::platform::CUDAPlace gpu(0);
CUDAPlace gpu(0); auto& pool = paddle::platform::DeviceContextPool::Instance();
auto& pool = DeviceContextPool::Instance();
auto* cuda_ctx = pool.GetByPlace(gpu); auto* cuda_ctx = pool.GetByPlace(gpu);
{ {
Tensor tensor; Tensor tensor;
...@@ -66,9 +65,8 @@ TEST(TensorContainsNAN, GPU) { ...@@ -66,9 +65,8 @@ TEST(TensorContainsNAN, GPU) {
} }
TEST(TensorContainsInf, GPU) { TEST(TensorContainsInf, GPU) {
using namespace paddle::platform; paddle::platform::CUDAPlace gpu(0);
CUDAPlace gpu(0); auto& pool = paddle::platform::DeviceContextPool::Instance();
auto& pool = DeviceContextPool::Instance();
auto* cuda_ctx = pool.GetByPlace(gpu); auto* cuda_ctx = pool.GetByPlace(gpu);
{ {
Tensor tensor; Tensor tensor;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册