Skip to content

Commit

Permalink
support multiple architectures (e.g. SSE, AVX) in the same executable… (
Browse files Browse the repository at this point in the history
#118)

* support multiple architectures (e.g. SSE, AVX) in the same executable via a macro

* fix CI tests

* fix makefile

* define RTNEURAL_NAMESPACE (if not already) as a safety net

* RTNEURAL_NAMESPACE is configurable in cmake
  • Loading branch information
JeffMcClintock committed Nov 23, 2023
1 parent 37fde44 commit 1e7b570
Show file tree
Hide file tree
Showing 61 changed files with 114 additions and 105 deletions.
5 changes: 5 additions & 0 deletions RTNeural/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ target_include_directories(RTNeural
INTERFACE
..
)
set(RTNEURAL_NAMESPACE "RTNeural" CACHE STRING "Namespace to use for RTNeural code")
target_compile_definitions(RTNeural
PUBLIC
RTNEURAL_NAMESPACE=${RTNEURAL_NAMESPACE}
)
4 changes: 2 additions & 2 deletions RTNeural/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstddef>
#include <string>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/** Virtual base class for a generic neural network layer. */
Expand Down Expand Up @@ -34,6 +34,6 @@ class Layer
const int out_size;
};

} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // LAYER_H_INCLUDED
4 changes: 2 additions & 2 deletions RTNeural/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "lstm/lstm.h"
#include "lstm/lstm.tpp"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/**
Expand Down Expand Up @@ -112,6 +112,6 @@ class Model
std::vector<vec_type> outs;
};

} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // MODEL_H_INCLUDED
4 changes: 2 additions & 2 deletions RTNeural/ModelT.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "model_loader.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

#ifndef DOXYGEN
Expand Down Expand Up @@ -575,4 +575,4 @@ class ModelT2D
static constexpr size_t n_layers = sizeof...(Layers);
};
#endif // RTNEURAL_USE_XSIMD
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE
8 changes: 4 additions & 4 deletions RTNeural/RTNeural.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "RTNeural.h"

// forward declare some template classes
template class RTNeural::Model<float>;
template class RTNeural::Model<double>;
template class RTNeural::Layer<float>;
template class RTNeural::Layer<double>;
template class RTNEURAL_NAMESPACE::Model<float>;
template class RTNEURAL_NAMESPACE::Model<double>;
template class RTNEURAL_NAMESPACE::Layer<float>;
template class RTNEURAL_NAMESPACE::Layer<double>;
4 changes: 4 additions & 0 deletions RTNeural/RTNeural.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// C++ STL includes
#include <limits>

#ifndef RTNEURAL_NAMESPACE
#define RTNEURAL_NAMESPACE RTNeural
#endif

// Handle default RTNeural defines
#ifndef RTNEURAL_DEFAULT_ALIGNMENT
#if _MSC_VER
Expand Down
8 changes: 4 additions & 4 deletions RTNeural/activation/activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <functional>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/** Base class for activation layers. */
Expand Down Expand Up @@ -35,7 +35,7 @@ class Activation : public Layer<T>
const std::function<T(T)> func;
};

} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#if RTNEURAL_USE_EIGEN
#include "activation_eigen.h"
Expand All @@ -48,7 +48,7 @@ class Activation : public Layer<T>
#include "../maths/maths_stl.h"
#include <cmath>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/** Dynamic implementation of a tanh activation layer. */
Expand Down Expand Up @@ -424,7 +424,7 @@ class PReLUActivationT
T outs[size];
T alpha[size];
};
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // RTNEURAL_USE_EIGEN

Expand Down
4 changes: 2 additions & 2 deletions RTNeural/activation/activation_eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../common.h"
#include "../maths/maths_eigen.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/** Dynamic implementation of a tanh activation layer. */
Expand Down Expand Up @@ -463,6 +463,6 @@ class PReLUActivationT
T outs_internal alignas(RTNEURAL_DEFAULT_ALIGNMENT)[out_size];
v_type alpha;
};
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // ACTIVATIONEIGEN_H_INCLUDED
4 changes: 2 additions & 2 deletions RTNeural/activation/activation_xsimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../common.h"
#include "../maths/maths_xsimd.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/** Dynamic implementation of a tanh activation layer. */
Expand Down Expand Up @@ -430,6 +430,6 @@ class PReLUActivationT
v_type outs[v_io_size];
v_type alpha[v_io_size];
};
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // ACTIVATIONXSIMD_H_INCLUDED
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "../common.h"
#include <vector>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "batchnorm.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
#if !RTNEURAL_USE_EIGEN && !RTNEURAL_USE_XSIMD

Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#else
#include "../Layer.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#if !RTNEURAL_USE_EIGEN && !RTNEURAL_USE_XSIMD

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
BatchNorm2DLayer<T>::BatchNorm2DLayer(int in_num_filters, int in_num_features)
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d_eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <Eigen/Dense>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d_eigen.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "batchnorm2d_eigen.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
BatchNorm2DLayer<T>::BatchNorm2DLayer(int in_num_filters, int in_num_features)
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d_xsimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <xsimd/xsimd.hpp>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm2d_xsimd.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "batchnorm2d_xsimd.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
BatchNorm2DLayer<T>::BatchNorm2DLayer(int in_num_filters, int in_num_features)
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm_eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <Eigen/Dense>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm_eigen.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "batchnorm_eigen.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
BatchNorm1DLayer<T>::BatchNorm1DLayer(int size)
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm_xsimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <xsimd/xsimd.hpp>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
/** Dynamic batch normalization layer. */
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion RTNeural/batchnorm/batchnorm_xsimd.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "batchnorm_xsimd.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
BatchNorm1DLayer<T>::BatchNorm1DLayer(int size)
Expand Down
16 changes: 8 additions & 8 deletions RTNeural/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/**
Expand Down Expand Up @@ -29,26 +29,26 @@ constexpr T ceil_div(T num, T den)
{
return (num + den - 1) / den;
}
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#if RTNEURAL_USE_EIGEN
#include <Eigen/Dense>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
#if RTNEURAL_DEFAULT_ALIGNMENT == 32
constexpr auto RTNeuralEigenAlignment = Eigen::Aligned32;
#else
constexpr auto RTNeuralEigenAlignment = Eigen::Aligned16;
#endif
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#elif RTNEURAL_USE_XSIMD
#include <xsimd/xsimd.hpp>

#include "xsimd-legacy/algorithms/algorithms.hpp"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

template <typename T>
Expand Down Expand Up @@ -236,20 +236,20 @@ static inline void elu(const T* in, T* out, int dim, T alpha) noexcept
for(auto i = vec_size; i < dim; ++i)
out[i] = in[i] > (T)0 ? in[i] : (alpha * (MathsProvider::exp(in[i]) - (T)1));
}
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#else // STL backend
#include <algorithm>
#include <cmath>
#include <numeric>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{
template <typename T>
static inline T vMult(const T* arg1, const T* arg2, int dim) noexcept
{
return std::inner_product(arg1, arg1 + dim, arg2, (T)0);
}
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif
4 changes: 2 additions & 2 deletions RTNeural/conv1d/conv1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "../common.h"
#include <vector>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/**
Expand Down Expand Up @@ -241,6 +241,6 @@ class Conv1DT
state_ptrs[k] = (state_ptr + state_size - k * dilation_rate) % state_size;
}
};
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE
#endif
#endif // CONV1D_H_INCLUDED
4 changes: 2 additions & 2 deletions RTNeural/conv1d/conv1d.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "conv1d.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

#if !RTNEURAL_USE_EIGEN && !RTNEURAL_USE_XSIMD
Expand Down Expand Up @@ -166,4 +166,4 @@ void Conv1DT<T, in_sizet, out_sizet, kernel_size, dilation_rate, dynamic_state>:

#endif

} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE
4 changes: 2 additions & 2 deletions RTNeural/conv1d/conv1d_eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Layer.h"
#include <Eigen/Dense>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/**
Expand Down Expand Up @@ -214,6 +214,6 @@ class Conv1DT
}
};

} // RTNeural
} // RTNEURAL_NAMESPACE

#endif // CONV1DEIGEN_H_INCLUDED
4 changes: 2 additions & 2 deletions RTNeural/conv1d/conv1d_eigen.tpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "conv1d_eigen.h"

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

template <typename T>
Expand Down Expand Up @@ -105,4 +105,4 @@ void Conv1DT<T, in_sizet, out_sizet, kernel_size, dilation_rate, dynamic_state>:
bias(i) = biasVals[i];
}

} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE
4 changes: 2 additions & 2 deletions RTNeural/conv1d/conv1d_xsimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <numeric>
#include <vector>

namespace RTNeural
namespace RTNEURAL_NAMESPACE
{

/**
Expand Down Expand Up @@ -313,6 +313,6 @@ class Conv1DT
state_ptrs[k] = (state_ptr + state_size - k * dilation_rate) % state_size;
}
};
} // namespace RTNeural
} // namespace RTNEURAL_NAMESPACE

#endif // CONV1DXSIMD_H_INCLUDED
Loading

0 comments on commit 1e7b570

Please sign in to comment.