Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/testing framework dedicated target #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/radsan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: RADSan Real-Time Safety

on: [push]

#on:
# push:
# branches:
# - main
# - develop
# pull_request:
# branches:
# - main
# - develop

jobs:
build_and_test:
name: Check real-time safety with RADSan
runs-on: ubuntu-latest
container: realtimesanitizer/radsan-clang:latest
steps:
- name: Install CMake and Git
run: apt-get update && apt-get install -y cmake git

- name: Checkout code
uses: actions/checkout@v2
with:
submodules: true

- name: Configure
run: cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -DRTNEURAL_ENABLE_RADSAN=ON

- name: Build
run: cmake --build build --config Release --parallel --target rtneural_test_realtime

- name: Test
run: cd build && ./tests/realtime/rtneural_test_realtime
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(RTNeural VERSION 1.0.0)
include(cmake/CXXStandard.cmake)
include(cmake/RADSan.cmake)

add_subdirectory(RTNeural)

Expand All @@ -27,3 +28,7 @@ if(BUILD_EXAMPLES)
message(STATUS "RTNeural -- Configuring examples...")
add_subdirectory(examples)
endif()

if(RTNEURAL_ENABLE_RADSAN)
rtneural_radsan_configure(RTNeural)
endif()
7 changes: 7 additions & 0 deletions cmake/RADSan.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
option(RTNEURAL_ENABLE_RADSAN "Enable RADSan checks (requires RADSan clang)" OFF)

function(rtneural_radsan_configure target)
target_compile_definitions(${target} PUBLIC RTNEURAL_RADSAN_ENABLED)
target_compile_options(${target} PUBLIC -fsanitize=realtime)
target_link_options(${target} PUBLIC -fsanitize=realtime)
endfunction()
2 changes: 1 addition & 1 deletion cmake/Testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ option(RTNEURAL_TEST_REPORTS "Output test reports to XML files" OFF)
macro(rtneural_setup_testing)
include(CTest)
enable_testing()
add_custom_target(rtneural_test COMMAND ctest -C ${Configuration} --output-on-failure)
add_custom_target(rtneural_test COMMAND ctest --output-on-failure)
CPMAddPackage("gh:google/googletest@1.14.0")
endmacro()

Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
add_subdirectory(unit)
add_subdirectory(functional)

if(RTNEURAL_ENABLE_RADSAN)
add_subdirectory(realtime)
endif()

option(RTNEURAL_CODE_COVERAGE "Build RTNeural tests with code coverage flags" OFF)
if(RTNEURAL_CODE_COVERAGE)
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/EnableCoverageFlags.cmake)
Expand Down
4 changes: 4 additions & 0 deletions tests/realtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rtneural_add_test(
TARGET rtneural_test_realtime
SOURCES modelt_test.cpp
DEPENDENCIES PRIVATE RTNeural)
15 changes: 15 additions & 0 deletions tests/realtime/assertions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#ifdef RTNEURAL_RADSAN_ENABLED

extern "C" void radsan_realtime_enter();
extern "C" void radsan_realtime_exit();
#define EXPECT_REAL_TIME_SAFE(statement) \
radsan_realtime_enter(); \
statement; \
radsan_realtime_exit();
#else
#define EXPECT_REAL_TIME_SAFE(statement) \
#error EXPECT_REAL_TIME_SAFE \
requires RTNEURAL_RADSAN_ENABLED
#endif
111 changes: 111 additions & 0 deletions tests/realtime/modelt_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <gmock/gmock.h>

#include "../functional/load_csv.hpp"
#include "../functional/test_configs.hpp"
#include "assertions.hpp"

#include <RTNeural.h>

namespace
{
using TestType = double;
using namespace RTNeural;

template <typename T, typename StaticModelType>
void checkRealTimeSafety(const TestConfig& test)
{
std::ifstream jsonStream(std::string { RTNEURAL_ROOT_DIR } + test.model_file, std::ifstream::binary);
StaticModelType static_model;
static_model.parseJson(jsonStream, true);
static_model.reset();

jsonStream.seekg(0);
auto dynamic_model = RTNeural::json_parser::parseJson<T>(jsonStream, true);
dynamic_model->reset();

T input[] = { 0.5f };
EXPECT_REAL_TIME_SAFE(static_model.forward(input));
EXPECT_REAL_TIME_SAFE(dynamic_model->forward(input));
}
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithDense)
{
using ModelType = ModelT<TestType, 1, 1,
DenseT<TestType, 1, 8>,
TanhActivationT<TestType, 8>,
DenseT<TestType, 8, 8>,
ReLuActivationT<TestType, 8>,
DenseT<TestType, 8, 8>,
ELuActivationT<TestType, 8>,
DenseT<TestType, 8, 8>,
SoftmaxActivationT<TestType, 8>,
DenseT<TestType, 8, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("dense"));
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithConv1D)
{
using ModelType = ModelT<TestType, 1, 1,
DenseT<TestType, 1, 8>,
TanhActivationT<TestType, 8>,
Conv1DT<TestType, 8, 4, 3, 1, true>,
TanhActivationT<TestType, 4>,
BatchNorm1DT<TestType, 4>,
PReLUActivationT<TestType, 4>,
Conv1DT<TestType, 4, 4, 1, 1>,
TanhActivationT<TestType, 4>,
Conv1DT<TestType, 4, 4, 3, 2>,
TanhActivationT<TestType, 4>,
BatchNorm1DT<TestType, 4, false>,
PReLUActivationT<TestType, 4>,
DenseT<TestType, 4, 1>,
SigmoidActivationT<TestType, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("conv1d"));
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithGRU)
{
using ModelType = ModelT<TestType, 1, 1,
DenseT<TestType, 1, 8>,
TanhActivationT<TestType, 8>,
GRULayerT<TestType, 8, 8>,
DenseT<TestType, 8, 8>,
SigmoidActivationT<TestType, 8>,
DenseT<TestType, 8, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("gru"));
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithGRU1D)
{
using ModelType = ModelT<TestType, 1, 1,
GRULayerT<TestType, 1, 8>,
DenseT<TestType, 8, 8>,
SigmoidActivationT<TestType, 8>,
DenseT<TestType, 8, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("gru_1d"));
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithLSTM)
{
using ModelType = ModelT<TestType, 1, 1,
DenseT<TestType, 1, 8>,
TanhActivationT<TestType, 8>,
LSTMLayerT<TestType, 8, 8>,
DenseT<TestType, 8, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("lstm"));
}

TEST(TestTemplatedModels, doesNotCauseRADSanErrorWithLSTM1D)
{
using ModelType = ModelT<TestType, 1, 1,
LSTMLayerT<TestType, 1, 8>,
DenseT<TestType, 8, 1>>;

checkRealTimeSafety<TestType, ModelType>(tests.at("lstm_1d"));
}
Loading