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

Add fast tanh approximation #95

Merged
merged 1 commit into from
Mar 4, 2023
Merged
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
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