Skip to content

Commit

Permalink
Fast tanh activvation(#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeoliphant authored Mar 4, 2023
1 parent d41d873 commit e1410f3
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions NeuralAmpModeler/dsp/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "numpy_util.h"
#include "util.h"

#define tanh_impl_ std::tanh
//#define tanh_impl_ fast_tanh_


constexpr auto _INPUT_BUFFER_SAFETY_FACTOR = 32;

DSP::DSP() { this->_stale_params = true; }
Expand Down Expand Up @@ -191,25 +195,38 @@ void sigmoid_(Eigen::MatrixXf &x, const long i_start, const long i_end,
x(i, j) = 1.0 / (1.0 + expf(-x(i, j)));
}

inline float fast_tanh_(const float x)
{
const float ax = fabs(x);
const float x2 = x * x;

return(x * (2.45550750702956f + 2.45550750702956f * ax +
(0.893229853513558f + 0.821226666969744f * ax) * x2) /
(2.44506634652299f + (2.44506634652299f + x2) *
fabs(x + 0.814642734961073f * x * ax)));
}


void tanh_(Eigen::MatrixXf &x, const long i_start, const long i_end,
const long j_start, const long j_end) {
for (long j = j_start; j < j_end; j++)
for (long i = i_start; i < i_end; i++)
x(i, j) = tanh(x(i, j));
x(i, j) = tanh_impl_(x(i, j));
}

void tanh_(Eigen::MatrixXf &x, const long j_start, const long j_end) {
tanh_(x, 0, x.rows(), j_start, j_end);
}

void tanh_(Eigen::MatrixXf& x) {

float* ptr = x.data();

long size = x.rows() * x.cols();

for (long pos = 0; pos < size; pos++)
{
ptr[pos] = tanh(ptr[pos]);
ptr[pos] = tanh_impl_(ptr[pos]);
}
}

Expand Down

0 comments on commit e1410f3

Please sign in to comment.