Skip to content

Fix SeLU lambda precision (Follow-up to #1287) #1298

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions hls4ml/templates/vivado/nnet_utils/nnet_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -717,19 +717,27 @@ template <class data_T, class res_T, typename CONFIG_T> void selu(data_T data[CO
initialized = true;
}

#pragma HLS PIPELINE
typedef ap_ufixed<16, 1> selu_const_t;
static const selu_const_t lambda = 1.0507009873554805;

data_T datareg;
// Index into the lookup table based on data
int index;
#pragma HLS PIPELINE
for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
datareg = data[ii];
data_T datareg = data[ii];

if (datareg >= 0) {
res[ii] = res_T(1.0507009873554804934193349852946) * datareg;
// Positive branch y = λ · x
res[ii] = lambda * datareg;
} else {
index = datareg * CONFIG_T::table_size / -8;
if (index > CONFIG_T::table_size - 1)
// Negative branch y = table(x)
int index = datareg * CONFIG_T::table_size / -8;

// clamp index to [0, table_size-1]
if (index < 0)
index = 0;
else if (index > CONFIG_T::table_size - 1) {
index = CONFIG_T::table_size - 1;
}

res[ii] = selu_table[index];
}
}
Expand Down