Skip to content

Commit

Permalink
add several base unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwhql committed Oct 22, 2021
1 parent 7b7e988 commit 52fead0
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 40 deletions.
2 changes: 2 additions & 0 deletions paddle/pten/hapi/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ limitations under the License. */
namespace paddle {
namespace experimental {

// TODO(chenweihang): add scale API
// TODO(chenweihang): move mean API into stat.h/cc
Tensor mean(const Tensor& x);

} // namespace experimental
Expand Down
3 changes: 3 additions & 0 deletions paddle/pten/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
cc_test(pten_backend_test SRCS backend_test.cc DEPS gtest)
cc_test(pten_data_layout_test SRCS data_layout_test.cc DEPS gtest)
cc_test(pten_data_type_test SRCS data_type_test.cc DEPS gtest)
cc_test(dense_tensor_test SRCS dense_tensor_test.cc DEPS dense_tensor)
cc_test(kernel_factory_test SRCS kernel_factory_test.cc DEPS kernel_factory)
cc_test(test_mean_api SRCS test_mean_api.cc DEPS math_api)
Expand Down
32 changes: 32 additions & 0 deletions paddle/pten/tests/backend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,35 @@ limitations under the License. */
#include "paddle/pten/common/backend.h"

#include <gtest/gtest.h>
#include <iostream>

TEST(Backend, OStream) {
std::ostringstream oss;
oss << pten::Backend::UNDEFINED;
EXPECT_EQ(oss.str(), "Undefined");
oss.str("");
oss << pten::Backend::CPU;
EXPECT_EQ(oss.str(), "CPU");
oss.str("");
oss << pten::Backend::CUDA;
EXPECT_EQ(oss.str(), "CUDA");
oss.str("");
oss << pten::Backend::XPU;
EXPECT_EQ(oss.str(), "XPU");
oss.str("");
oss << pten::Backend::NPU;
EXPECT_EQ(oss.str(), "NPU");
oss.str("");
oss << pten::Backend::MKLDNN;
EXPECT_EQ(oss.str(), "MKLDNN");
oss.str("");
oss << pten::Backend::CUDNN;
EXPECT_EQ(oss.str(), "CUDNN");
oss.str("");
try {
oss << pten::Backend::NUM_BACKENDS;
} catch (paddle::platform::EnforceNotMet &exception) {
std::string ex_msg = exception.what();
EXPECT_TRUE(ex_msg.find("Invalid enum backend type") != std::string::npos);
}
}
44 changes: 44 additions & 0 deletions paddle/pten/tests/data_layout_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
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 <gtest/gtest.h>
#include <iostream>
#include <sstream>
#include "paddle/pten/common/layout.h"

TEST(DataLayout, OStream) {
std::ostringstream oss;
oss << pten::DataLayout::UNDEFINED;
EXPECT_EQ(oss.str(), "Undefined");
oss.str("");
oss << pten::DataLayout::ANY;
EXPECT_EQ(oss.str(), "Any");
oss.str("");
oss << pten::DataLayout::NHWC;
EXPECT_EQ(oss.str(), "NHWC");
oss.str("");
oss << pten::DataLayout::NCHW;
EXPECT_EQ(oss.str(), "NCHW");
oss.str("");
oss << pten::DataLayout::MKLDNN;
EXPECT_EQ(oss.str(), "MKLDNN");
oss.str("");
try {
oss << pten::DataLayout::NUM_DATA_LAYOUTS;
} catch (paddle::platform::EnforceNotMet &exception) {
std::string ex_msg = exception.what();
EXPECT_TRUE(ex_msg.find("Invalid enum data layout type") !=
std::string::npos);
}
}
68 changes: 68 additions & 0 deletions paddle/pten/tests/data_type_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
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/pten/common/data_type.h"

#include <gtest/gtest.h>
#include <iostream>
#include <sstream>

TEST(DataType, OStream) {
std::ostringstream oss;
oss << pten::DataType::UNDEFINED;
EXPECT_EQ(oss.str(), "Undefined");
oss.str("");
oss << pten::DataType::BOOL;
EXPECT_EQ(oss.str(), "bool");
oss.str("");
oss << pten::DataType::INT8;
EXPECT_EQ(oss.str(), "int8");
oss.str("");
oss << pten::DataType::UINT8;
EXPECT_EQ(oss.str(), "uint8");
oss.str("");
oss << pten::DataType::INT16;
EXPECT_EQ(oss.str(), "int16");
oss.str("");
oss << pten::DataType::INT32;
EXPECT_EQ(oss.str(), "int32");
oss.str("");
oss << pten::DataType::INT64;
EXPECT_EQ(oss.str(), "int64");
oss.str("");
oss << pten::DataType::BFLOAT16;
EXPECT_EQ(oss.str(), "bfloat16");
oss.str("");
oss << pten::DataType::FLOAT16;
EXPECT_EQ(oss.str(), "float16");
oss.str("");
oss << pten::DataType::FLOAT32;
EXPECT_EQ(oss.str(), "float32");
oss.str("");
oss << pten::DataType::FLOAT64;
EXPECT_EQ(oss.str(), "float64");
oss.str("");
oss << pten::DataType::COMPLEX64;
EXPECT_EQ(oss.str(), "complex64");
oss.str("");
oss << pten::DataType::COMPLEX128;
EXPECT_EQ(oss.str(), "complex128");
oss.str("");
try {
oss << pten::DataType::NUM_DATA_TYPES;
} catch (paddle::platform::EnforceNotMet &exception) {
std::string ex_msg = exception.what();
EXPECT_TRUE(ex_msg.find("Invalid enum data type") != std::string::npos);
}
}
12 changes: 0 additions & 12 deletions paddle/pten/tests/dense_tensor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,3 @@ TEST(DenseTensor, Constructor) {
ASSERT_EQ(tensor.data_type(), pten::DataType::FLOAT32);
ASSERT_EQ(tensor.layout(), pten::DataLayout::NCHW);
}

TEST(DenseTensor, Dims) {
// impl later
}

TEST(DenseTensor, Place) {
// impl later
}

TEST(DenseTensor, Data) {
// impl later
}
13 changes: 0 additions & 13 deletions paddle/pten/tests/dtype_test.cc

This file was deleted.

28 changes: 26 additions & 2 deletions paddle/pten/tests/kernel_factory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,36 @@ 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 <iostream>
#include <sstream>

#include "paddle/pten/core/kernel_factory.h"

#include "gtest/gtest.h"

TEST(KernelFactory, KernelKey) {
// TODO(chenweihang): add more unittests later

TEST(KernelName, ConstructAndOStream) {
std::ostringstream oss;
oss << pten::KernelName("scale", "host");
EXPECT_EQ(oss.str(), "scale.host");
pten::KernelName kernel_name1("scale.host");
EXPECT_EQ(kernel_name1.name(), "scale");
EXPECT_EQ(kernel_name1.overload_name(), "host");
pten::KernelName kernel_name2("scale.host");
EXPECT_EQ(kernel_name2.name(), "scale");
EXPECT_EQ(kernel_name2.overload_name(), "host");
}

TEST(KernelKey, ConstructAndOStream) {
pten::KernelKey key(
pten::Backend::CPU, pten::DataLayout::NCHW, pten::DataType::FLOAT32);
std::cout << key;
EXPECT_EQ(key.backend(), pten::Backend::CPU);
EXPECT_EQ(key.layout(), pten::DataLayout::NCHW);
EXPECT_EQ(key.dtype(), pten::DataType::FLOAT32);
std::ostringstream oss;
oss << key;
std::cout << oss.str();
// EXPECT_EQ(oss.str(), "scale.host");
oss.flush();
}
13 changes: 0 additions & 13 deletions paddle/pten/tests/layout_test.cc

This file was deleted.

1 change: 1 addition & 0 deletions paddle/pten/tests/test_dot_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PT_DECLARE_MODULE(LinalgCUDA);
namespace framework = paddle::framework;
using DDim = paddle::framework::DDim;

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, dot) {
// 1. create tensor
auto dense_x = std::make_shared<pten::DenseTensor>(
Expand Down
1 change: 1 addition & 0 deletions paddle/pten/tests/test_fill_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PT_DECLARE_MODULE(CreationCUDA);
namespace framework = paddle::framework;
using DDim = paddle::framework::DDim;

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, full_like) {
// 1. create tensor
auto dense_x = std::make_shared<pten::DenseTensor>(
Expand Down
1 change: 1 addition & 0 deletions paddle/pten/tests/test_flatten_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PT_DECLARE_MODULE(ManipulationCUDA);
namespace framework = paddle::framework;
using DDim = paddle::framework::DDim;

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, flatten) {
// 1. create tensor
auto dense_x = std::make_shared<pten::DenseTensor>(
Expand Down
1 change: 1 addition & 0 deletions paddle/pten/tests/test_mean_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PT_DECLARE_MODULE(MathCUDA);
namespace framework = paddle::framework;
using DDim = paddle::framework::DDim;

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, mean) {
// 1. create tensor
auto dense_x = std::make_shared<pten::DenseTensor>(
Expand Down

0 comments on commit 52fead0

Please sign in to comment.