Skip to content

Commit

Permalink
add back tests folder
Browse files Browse the repository at this point in the history
  • Loading branch information
lidanqing-intel committed May 17, 2022
1 parent 447dfca commit 7ec293a
Show file tree
Hide file tree
Showing 10 changed files with 1,952 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/api_cc/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CTestTestfile.cmake
Makefile
cmake_install.cmake
runUnitTests
version.h
108 changes: 108 additions & 0 deletions source/api_cc/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
cmake_minimum_required(VERSION 3.9)
project(deepmd_api_test)
set(CMAKE_LINK_WHAT_YOU_USE TRUE)

if (NOT DEFINED BUILD_CPP_IF)
set(BUILD_CPP_IF TRUE)
endif (NOT DEFINED BUILD_CPP_IF)
add_definitions ("-DHIGH_PREC")

enable_testing()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# model version
file(READ ${PROJECT_SOURCE_DIR}/../../config/MODEL_VER MODEL_VERSION)
string(REPLACE "\n" " " MODEL_VERSION ${MODEL_VERSION})
message(STATUS "Supported model version: ${MODEL_VERSION}")

set(libname "deepmd")
set(LIB_BASE_DIR ${CMAKE_SOURCE_DIR}/../../lib)
include_directories(${LIB_BASE_DIR}/include)
file(GLOB LIB_SRC ${LIB_BASE_DIR}/src/*.cc ${LIB_BASE_DIR}/src/*.cpp)
add_library(${libname} SHARED ${LIB_SRC})

set(apiname "deepmd_api")
set(API_BASE_DIR ${CMAKE_SOURCE_DIR}/../)
include_directories(${API_BASE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR})
file(GLOB API_SRC ${API_BASE_DIR}/src/*.cc ${API_BASE_DIR}/src/*.cpp)
add_library(${apiname} SHARED ${API_SRC})
configure_file(
${API_BASE_DIR}/include/version.h.in
${CMAKE_SOURCE_DIR}/version.h
@ONLY
)

set(opname "deepmd_op")
set(OP_BASE_DIR ${CMAKE_SOURCE_DIR}/../../op)
# file(GLOB OP_SRC ${OP_BASE_DIR}/*.cc)
file(GLOB OP_SRC ${OP_BASE_DIR}/prod_force.cc ${OP_BASE_DIR}/prod_virial.cc ${OP_BASE_DIR}/descrpt.cc ${OP_BASE_DIR}/descrpt_se_a_ef.cc ${OP_BASE_DIR}/descrpt_se_a_ef.cc ${OP_BASE_DIR}/descrpt_se_a_ef_para.cc ${OP_BASE_DIR}/descrpt_se_a_ef_vert.cc ${OP_BASE_DIR}/pair_tab.cc ${OP_BASE_DIR}/prod_force_multi_device.cc ${OP_BASE_DIR}/prod_virial_multi_device.cc ${OP_BASE_DIR}/soft_min.cc ${OP_BASE_DIR}/soft_min_force.cc ${OP_BASE_DIR}/soft_min_virial.cc ${OP_BASE_DIR}/ewald_recp.cc ${OP_BASE_DIR}/gelu_multi_device.cc ${OP_BASE_DIR}/map_aparam.cc ${OP_BASE_DIR}/neighbor_stat.cc ${OP_BASE_DIR}/unaggregated_grad.cc ${OP_BASE_DIR}/tabulate_multi_device.cc ${OP_BASE_DIR}/prod_env_mat_multi_device.cc)
add_library(${opname} SHARED ${OP_SRC})

list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/../../cmake/)
find_package(tensorflow REQUIRED)
include_directories(${TensorFlow_INCLUDE_DIRS})

find_package(Threads)
# find openmp
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# define USE_CUDA_TOOLKIT
if (DEFINED USE_CUDA_TOOLKIT)
if (USE_CUDA_TOOLKIT)
find_package(CUDA REQUIRED)
else()
message(STATUS "Will not build nv GPU support")
endif()
else()
find_package(CUDA QUIET)
if (CUDA_FOUND)
set(USE_CUDA_TOOLKIT TRUE)
message(STATUS "Found CUDA in ${CUDA_TOOLKIT_ROOT_DIR}, build nv GPU support")
else()
set(USE_CUDA_TOOLKIT FALSE)
message(STATUS "No cuda support found, will not build nv GPU support")
endif()
endif()

if (USE_CUDA_TOOLKIT)
add_definitions("-D GOOGLE_CUDA")
include_directories(${CUDA_INCLUDE_DIRS})
add_subdirectory(${LIB_BASE_DIR}/src/cuda cuda_binary_dir)
endif()

file(GLOB TEST_SRC test_*.cc)
add_executable( runUnitTests ${TEST_SRC} )

if (USE_CUDA_TOOLKIT)
target_link_libraries(runUnitTests gtest gtest_main ${libname} pthread deepmd_op_cuda)
else()
target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} ${opname} pthread ${TensorFlow_LIBRARY})
endif()
add_test( runUnitTests runUnitTests )

find_package(GTest)
if(NOT GTEST_LIBRARY)
configure_file(../../cmake/googletest.cmake.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
else ()
include_directories(${GTEST_INCLUDE_DIRS})
endif ()
105 changes: 105 additions & 0 deletions source/api_cc/tests/test_deepdipole.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <gtest/gtest.h>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <vector>
#include "DeepTensor.h"
#include "neighbor_list.h"
#include "test_utils.h"

#include "google/protobuf/text_format.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

class TestInferDeepDipole : public ::testing::Test
{
protected:
std::vector<double> coord = {
12.83, 2.56, 2.18,
12.09, 2.87, 2.74,
00.25, 3.32, 1.68,
3.36, 3.00, 1.81,
3.51, 2.51, 2.60,
4.27, 3.22, 1.56
};
std::vector<int> atype = {
0, 1, 1, 0, 1, 1
};
std::vector<double> box = {
13., 0., 0., 0., 13., 0., 0., 0., 13.
};
std::vector<double> expected_d = {
-9.274180565967479195e-01,2.698028341272042496e+00,2.521268387140979117e-01,2.927260638453461628e+00,-8.571926301526779923e-01,1.667785136187720063e+00
};
int natoms;

deepmd::DeepTensor dp;

void SetUp() override {
std::string file_name = "../../tests/infer/deepdipole.pbtxt";
int fd = open(file_name.c_str(), O_RDONLY);
tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd);
tensorflow::GraphDef graph_def;
tensorflow::protobuf::TextFormat::Parse(input, &graph_def);
delete input;
std::fstream output("deepdipole.pb", std::ios::out | std::ios::trunc | std::ios::binary);
graph_def.SerializeToOstream(&output);
// check the string by the following commands
// string txt;
// tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt);

dp.init("deepdipole.pb");

natoms = expected_d.size();
};

void TearDown() override {
remove( "deepdipole.pb" ) ;
};
};


TEST_F(TestInferDeepDipole, cpu_build_nlist)
{
EXPECT_EQ(dp.cutoff(), 4.);
EXPECT_EQ(dp.numb_types(), 2);
EXPECT_EQ(dp.output_dim(), 3);
std::vector<int> sel_types = dp.sel_types();
EXPECT_EQ(sel_types.size(), 1);
EXPECT_EQ(sel_types[0], 0);

std::vector<double> value;
dp.compute(value, coord, atype, box);

EXPECT_EQ(value.size(), expected_d.size());
for(int ii = 0; ii < expected_d.size(); ++ii){
EXPECT_LT(fabs(value[ii] - expected_d[ii]), 1e-10);
}
}

TEST_F(TestInferDeepDipole, cpu_lmp_nlist)
{
float rc = dp.cutoff();
int nloc = coord.size() / 3;
std::vector<double> coord_cpy;
std::vector<int> atype_cpy, mapping;
std::vector<int> ilist(nloc), numneigh(nloc);
std::vector<int*> firstneigh(nloc);
std::vector<std::vector<int > > nlist_data;
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
_build_nlist(nlist_data, coord_cpy, atype_cpy, mapping,
coord, atype, box, rc);
int nall = coord_cpy.size() / 3;
convert_nlist(inlist, nlist_data);

std::vector<double> value;
dp.compute(value, coord_cpy, atype_cpy, box, nall-nloc, inlist);

EXPECT_EQ(value.size(), expected_d.size());
for(int ii = 0; ii < expected_d.size(); ++ii){
EXPECT_LT(fabs(value[ii] - expected_d[ii]), 1e-10);
}
}

105 changes: 105 additions & 0 deletions source/api_cc/tests/test_deeppolar.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <gtest/gtest.h>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <vector>
#include "DeepTensor.h"
#include "neighbor_list.h"
#include "test_utils.h"

#include "google/protobuf/text_format.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

class TestInferDeepPolar : public ::testing::Test
{
protected:
std::vector<double> coord = {
12.83, 2.56, 2.18,
12.09, 2.87, 2.74,
00.25, 3.32, 1.68,
3.36, 3.00, 1.81,
3.51, 2.51, 2.60,
4.27, 3.22, 1.56
};
std::vector<int> atype = {
0, 1, 1, 0, 1, 1
};
std::vector<double> box = {
13., 0., 0., 0., 13., 0., 0., 0., 13.
};
std::vector<double> expected_d = {
1.061407927405987051e-01,-3.569013342133873778e-01,-2.862108976089940138e-02,-3.569013342133875444e-01,1.304367268874677244e+00,1.037647501453442256e-01,-2.862108976089940138e-02,1.037647501453441284e-01,8.100521520762453409e-03,1.236797829492216616e+00,-3.717307430531632262e-01,7.371515676976750919e-01,-3.717307430531630041e-01,1.127222682121889058e-01,-2.239181552775717510e-01,7.371515676976746478e-01,-2.239181552775717787e-01,4.448255365635306879e-01
};
int natoms;

deepmd::DeepTensor dp;

void SetUp() override {
std::string file_name = "../../tests/infer/deeppolar.pbtxt";
int fd = open(file_name.c_str(), O_RDONLY);
tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd);
tensorflow::GraphDef graph_def;
tensorflow::protobuf::TextFormat::Parse(input, &graph_def);
delete input;
std::fstream output("deeppolar.pb", std::ios::out | std::ios::trunc | std::ios::binary);
graph_def.SerializeToOstream(&output);
// check the string by the following commands
// string txt;
// tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt);

dp.init("deeppolar.pb");

natoms = expected_d.size();
};

void TearDown() override {
remove( "deeppolar.pb" ) ;
};
};


TEST_F(TestInferDeepPolar, cpu_build_nlist)
{
EXPECT_EQ(dp.cutoff(), 6.);
EXPECT_EQ(dp.numb_types(), 2);
EXPECT_EQ(dp.output_dim(), 9);
std::vector<int> sel_types = dp.sel_types();
EXPECT_EQ(sel_types.size(), 1);
EXPECT_EQ(sel_types[0], 0);

std::vector<double> value;
dp.compute(value, coord, atype, box);

EXPECT_EQ(value.size(), expected_d.size());
for(int ii = 0; ii < expected_d.size(); ++ii){
EXPECT_LT(fabs(value[ii] - expected_d[ii]), 1e-10);
}
}

TEST_F(TestInferDeepPolar, cpu_lmp_nlist)
{
float rc = dp.cutoff();
int nloc = coord.size() / 3;
std::vector<double> coord_cpy;
std::vector<int> atype_cpy, mapping;
std::vector<int> ilist(nloc), numneigh(nloc);
std::vector<int*> firstneigh(nloc);
std::vector<std::vector<int > > nlist_data;
deepmd::InputNlist inlist(nloc, &ilist[0], &numneigh[0], &firstneigh[0]);
_build_nlist(nlist_data, coord_cpy, atype_cpy, mapping,
coord, atype, box, rc);
int nall = coord_cpy.size() / 3;
convert_nlist(inlist, nlist_data);

std::vector<double> value;
dp.compute(value, coord_cpy, atype_cpy, box, nall-nloc, inlist);

EXPECT_EQ(value.size(), expected_d.size());
for(int ii = 0; ii < expected_d.size(); ++ii){
EXPECT_LT(fabs(value[ii] - expected_d[ii]), 1e-10);
}
}

Loading

0 comments on commit 7ec293a

Please sign in to comment.