Skip to content

activation: use dedicated fixed-point λ for SeLU (fixes #1287) #1296

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 4 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,2> selu_const_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would just 1 integer bit be enough?

Copy link
Author

@valerioedu valerioedu May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, kept one more just to be sure, do you want me to bring it to 1?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 1 makes sense, since the number is in the range.

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
Loading