Skip to content

Commit

Permalink
Configure tests with CTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Sep 1, 2023
1 parent 0d0adf9 commit 2609ecc
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:

- name: Build
shell: bash
run: cmake --build build --config Release --parallel
run: ctest -C Release --test-dir build --parallel

- name: Test
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ include(cmake/ChooseBackend.cmake)
option(BUILD_TESTS "Build RTNeural accuracy tests" OFF)
if(BUILD_TESTS)
message(STATUS "RTNeural -- Configuring tests...")
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()

Expand Down
19 changes: 14 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ include_directories(../RTNeural)

add_executable(rtneural_tests tests.cpp)
target_link_libraries(rtneural_tests LINK_PUBLIC RTNeural)
target_compile_definitions(rtneural_tests PRIVATE RTNEURAL_ROOT_DIR="${CMAKE_SOURCE_DIR}/")

add_custom_command(TARGET rtneural_tests
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "copying $<TARGET_FILE:rtneural_tests> to ${PROJECT_BINARY_DIR}/rtneural_tests"
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:rtneural_tests> ${PROJECT_BINARY_DIR}/rtneural_tests)
add_test(NAME "RTNeural_Approx_Test" COMMAND $<TARGET_FILE:rtneural_tests> approx)
add_test(NAME "RTNeural_Bad_Model_Test" COMMAND $<TARGET_FILE:rtneural_tests> bad_model)
add_test(NAME "RTNeural_Model_Test" COMMAND $<TARGET_FILE:rtneural_tests> model)
add_test(NAME "RTNeural_Sample_Rate_RNN_Test" COMMAND $<TARGET_FILE:rtneural_tests> sample_rate_rnn)
add_test(NAME "RTNeural_Torch_Test" COMMAND $<TARGET_FILE:rtneural_tests> torch)
add_test(NAME "RTNeural_Util_Test" COMMAND $<TARGET_FILE:rtneural_tests> util)
add_test(NAME "RTNeural_Conv1D_Test" COMMAND $<TARGET_FILE:rtneural_tests> conv1d)
add_test(NAME "RTNeural_Conv2D_Test" COMMAND $<TARGET_FILE:rtneural_tests> conv2d)
add_test(NAME "RTNeural_Dense_Test" COMMAND $<TARGET_FILE:rtneural_tests> dense)
add_test(NAME "RTNeural_GRU_Test" COMMAND $<TARGET_FILE:rtneural_tests> gru)
add_test(NAME "RTNeural_GRU_1D_Test" COMMAND $<TARGET_FILE:rtneural_tests> gru_1d)
add_test(NAME "RTNeural_LSTM_Test" COMMAND $<TARGET_FILE:rtneural_tests> lstm)
add_test(NAME "RTNeural_LSTM_1D_Test" COMMAND $<TARGET_FILE:rtneural_tests> lstm_1d)

option(RTNEURAL_CODE_COVERAGE "Build RTNeural tests with code coverage flags" OFF)
if(RTNEURAL_CODE_COVERAGE)
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/EnableCoverageFlags.cmake)
enable_coverage_flags(rtneural_tests)
endif()

3 changes: 2 additions & 1 deletion tests/bad_model_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ int badModelTest()
RTNeural::LSTMLayerT<float, 1, 16>,
RTNeural::DenseT<float, 16, 1>> lstm_16;

std::ifstream jsonStream1("models/bad_lstm.json", std::ifstream::binary);
const auto file_path = std::string { RTNEURAL_ROOT_DIR } + "models/bad_lstm.json";
std::ifstream jsonStream1(file_path, std::ifstream::binary);

try
{
Expand Down
6 changes: 3 additions & 3 deletions tests/conv2d_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ int conv2d_test()
#else
std::cout << "TESTING CONV2D MODEL..." << std::endl;

const std::string model_file = "models/conv2d.json";
const std::string data_file = "test_data/conv2d_x_python.csv";
const std::string data_file_y = "test_data/conv2d_y_python.csv";
const auto model_file = std::string { RTNEURAL_ROOT_DIR } + "models/conv2d.json";
const auto data_file = std::string { RTNEURAL_ROOT_DIR } + "test_data/conv2d_x_python.csv";
const auto data_file_y = std::string { RTNEURAL_ROOT_DIR } + "test_data/conv2d_y_python.csv";

constexpr double threshold = 1.0e-6;

Expand Down
4 changes: 2 additions & 2 deletions tests/model_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ int model_test()
{
std::cout << "TESTING FULL MODEL..." << std::endl;

const std::string model_file = "models/full_model.json";
const std::string data_file = "test_data/dense_x_python.csv";
const std::string model_file = std::string { RTNEURAL_ROOT_DIR } + "models/full_model.json";
const std::string data_file = std::string { RTNEURAL_ROOT_DIR } + "test_data/dense_x_python.csv";
constexpr double threshold = 1.0e-12;

std::ifstream pythonX(data_file);
Expand Down
4 changes: 2 additions & 2 deletions tests/sample_rate_rnn_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ int runModelTest(const std::string& modelFile, MultType sampleRateMult)
static constexpr auto baseSampleRate = 48000.0;

ModelType<RTNeural::SampleRateCorrectionMode::None> baseSampleRateModel;
std::ifstream jsonStream1("models/" + modelFile, std::ifstream::binary);
std::ifstream jsonStream1(std::string { RTNEURAL_ROOT_DIR } + "models/" + modelFile, std::ifstream::binary);
baseSampleRateModel.parseJson(jsonStream1);
baseSampleRateModel.reset();
auto baseSampleRateSignal = getSampleRateVector(baseSampleRate);
for(auto& sample : baseSampleRateSignal)
sample = baseSampleRateModel.forward(&sample);

ModelType<mode> testSampleRateModel;
std::ifstream jsonStream2("models/" + modelFile, std::ifstream::binary);
std::ifstream jsonStream2(std::string { RTNEURAL_ROOT_DIR } + "models/" + modelFile, std::ifstream::binary);
testSampleRateModel.parseJson(jsonStream2);
testSampleRateModel.reset();
testSampleRateModel.template get<RLayerIdx>().prepare(sampleRateMult);
Expand Down
6 changes: 3 additions & 3 deletions tests/templated_tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ int runTestTemplated(const TestConfig& test)
{
std::cout << "TESTING " << test.name << " TEMPLATED IMPLEMENTATION..." << std::endl;

std::ifstream jsonStream(test.model_file, std::ifstream::binary);
std::ifstream jsonStream(std::string { RTNEURAL_ROOT_DIR } + test.model_file, std::ifstream::binary);
ModelType model;
model.parseJson(jsonStream, true);
model.reset();

std::ifstream pythonX(test.x_data_file);
std::ifstream pythonX(std::string { RTNEURAL_ROOT_DIR } + test.x_data_file);
auto xData = load_csv::loadFile<T>(pythonX);

std::ifstream pythonY(test.y_data_file);
std::ifstream pythonY(std::string { RTNEURAL_ROOT_DIR } + test.y_data_file);
const auto yRefData = load_csv::loadFile<T>(pythonY);

std::vector<T> yData(xData.size(), (T)0);
Expand Down
6 changes: 3 additions & 3 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ int runTest(const TestConfig& test)
{
std::cout << "TESTING " << test.name << " IMPLEMENTATION..." << std::endl;

std::ifstream jsonStream(test.model_file, std::ifstream::binary);
std::ifstream jsonStream(std::string { RTNEURAL_ROOT_DIR } + test.model_file, std::ifstream::binary);
auto model = RTNeural::json_parser::parseJson<T>(jsonStream, true);
model->reset();

std::ifstream pythonX(test.x_data_file);
std::ifstream pythonX(std::string { RTNEURAL_ROOT_DIR } + test.x_data_file);
auto xData = load_csv::loadFile<T>(pythonX);

std::ifstream pythonY(test.y_data_file);
std::ifstream pythonY(std::string { RTNEURAL_ROOT_DIR } + test.y_data_file);
const auto yRefData = load_csv::loadFile<T>(pythonY);

std::vector<T> yData(xData.size(), (T)0);
Expand Down
7 changes: 4 additions & 3 deletions tests/torch_conv1d_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ int testTorchConv1DModel()
std::cout << "TESTING TORCH/CONV1D MODEL WITH DATA TYPE: FLOAT" << std::endl;
else
std::cout << "TESTING TORCH/CONV1D MODEL WITH DATA TYPE: DOUBLE" << std::endl;
std::ifstream jsonStream("models/conv1d_torch.json", std::ifstream::binary);
const auto model_file = std::string { RTNEURAL_ROOT_DIR } + "models/conv1d_torch.json";
std::ifstream jsonStream(model_file, std::ifstream::binary);
nlohmann::json modelJson;
jsonStream >> modelJson;

RTNeural::ModelT<T, 1, 12, RTNeural::Conv1DT<T, 1, 12, 5, 1>> model;
RTNeural::torch_helpers::loadConv1D<T> (modelJson, "", model.template get<0>());
model.reset();

std::ifstream modelInputsFile { "test_data/conv1d_torch_x_python.csv" };
std::ifstream modelInputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/conv1d_torch_x_python.csv" };
const auto inputs = load_csv::loadFile<T>(modelInputsFile);
std::vector<std::array<T, 12>> outputs {};
outputs.resize(inputs.size(), {});
Expand All @@ -61,7 +62,7 @@ int testTorchConv1DModel()
std::copy(model.getOutputs(), model.getOutputs() + 12, outputs[i].begin());
}

std::ifstream modelOutputsFile { "test_data/conv1d_torch_y_python.csv" };
std::ifstream modelOutputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/conv1d_torch_y_python.csv" };
const auto expected_y = loadFile2D<T> (modelOutputsFile);

size_t nErrs = 0;
Expand Down
17 changes: 9 additions & 8 deletions tests/torch_gru_test.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "load_csv.hpp"
#include "RTNeural/RTNeural.h"
#include "load_csv.hpp"

namespace torch_gru_test
{
Expand All @@ -16,23 +16,24 @@ int testTorchGRUModel()
jsonStream >> modelJson;

auto& gru = model.template get<0>();
RTNeural::torch_helpers::loadGRU<T> (modelJson, "gru.", gru);
RTNeural::torch_helpers::loadGRU<T>(modelJson, "gru.", gru);

auto& dense = model.template get<1>();
RTNeural::torch_helpers::loadDense<T> (modelJson, "dense.", dense);
RTNeural::torch_helpers::loadDense<T>(modelJson, "dense.", dense);
};

if (std::is_same<T, float>::value)
if(std::is_same<T, float>::value)
std::cout << "TESTING TORCH/GRU MODEL WITH DATA TYPE: FLOAT" << std::endl;
else
std::cout << "TESTING TORCH/GRU MODEL WITH DATA TYPE: DOUBLE" << std::endl;
std::ifstream jsonStream("models/gru_torch.json", std::ifstream::binary);
const auto model_file = std::string { RTNEURAL_ROOT_DIR } + "models/gru_torch.json";
std::ifstream jsonStream(model_file, std::ifstream::binary);

ModelType model;
loadModel(jsonStream, model);
model.reset();

std::ifstream modelInputsFile { "test_data/gru_torch_x_python.csv" };
std::ifstream modelInputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/gru_torch_x_python.csv" };
const auto inputs = load_csv::loadFile<T>(modelInputsFile);
std::vector<T> outputs {};
outputs.resize(inputs.size(), {});
Expand All @@ -42,15 +43,15 @@ int testTorchGRUModel()
outputs[i] = model.forward(&inputs[i]);
}

std::ifstream modelOutputsFile { "test_data/gru_torch_y_python.csv" };
std::ifstream modelOutputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/gru_torch_y_python.csv" };
const auto expected_y = load_csv::loadFile<T>(modelOutputsFile);

size_t nErrs = 0;
T max_error = (T)0;
for(size_t n = 0; n < inputs.size(); ++n)
{
auto err = std::abs(outputs[n] - expected_y[n]);
if(err > (T) 1.0e-6)
if(err > (T)1.0e-6)
{
max_error = std::max(err, max_error);
nErrs++;
Expand Down
17 changes: 9 additions & 8 deletions tests/torch_lstm_test.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "load_csv.hpp"
#include "RTNeural/RTNeural.h"
#include "load_csv.hpp"

namespace torch_lstm_test
{
Expand All @@ -16,23 +16,24 @@ int testTorchLSTMModel()
jsonStream >> modelJson;

auto& lstm = model.template get<0>();
RTNeural::torch_helpers::loadLSTM<T> (modelJson, "lstm.", lstm);
RTNeural::torch_helpers::loadLSTM<T>(modelJson, "lstm.", lstm);

auto& dense = model.template get<1>();
RTNeural::torch_helpers::loadDense<T> (modelJson, "dense.", dense);
RTNeural::torch_helpers::loadDense<T>(modelJson, "dense.", dense);
};

if (std::is_same<T, float>::value)
if(std::is_same<T, float>::value)
std::cout << "TESTING TORCH/LSTM MODEL WITH DATA TYPE: FLOAT" << std::endl;
else
std::cout << "TESTING TORCH/LSTM MODEL WITH DATA TYPE: DOUBLE" << std::endl;
std::ifstream jsonStream("models/lstm_torch.json", std::ifstream::binary);
const auto model_file = std::string { RTNEURAL_ROOT_DIR } + "models/lstm_torch.json";
std::ifstream jsonStream(model_file, std::ifstream::binary);

ModelType model;
loadModel(jsonStream, model);
model.reset();

std::ifstream modelInputsFile { "test_data/lstm_torch_x_python.csv" };
std::ifstream modelInputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/lstm_torch_x_python.csv" };
const auto inputs = load_csv::loadFile<T>(modelInputsFile);
std::vector<T> outputs {};
outputs.resize(inputs.size(), {});
Expand All @@ -42,15 +43,15 @@ int testTorchLSTMModel()
outputs[i] = model.forward(&inputs[i]);
}

std::ifstream modelOutputsFile { "test_data/lstm_torch_y_python.csv" };
std::ifstream modelOutputsFile { std::string { RTNEURAL_ROOT_DIR } + "test_data/lstm_torch_y_python.csv" };
const auto expected_y = load_csv::loadFile<T>(modelOutputsFile);

size_t nErrs = 0;
T max_error = (T)0;
for(size_t n = 0; n < inputs.size(); ++n)
{
auto err = std::abs(outputs[n] - expected_y[n]);
if(err > (T) 1.0e-6)
if(err > (T)1.0e-6)
{
max_error = std::max(err, max_error);
nErrs++;
Expand Down

0 comments on commit 2609ecc

Please sign in to comment.