Skip to content

Commit 12cbee4

Browse files
committed
rest of #1287
1 parent 3c8c568 commit 12cbee4

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

hls4ml/templates/vivado/nnet_utils/nnet_activation.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -717,22 +717,27 @@ template <class data_T, class res_T, typename CONFIG_T> void selu(data_T data[CO
717717
initialized = true;
718718
}
719719

720-
typedef ap_ufixed<16, 1> selu_const_t;
720+
typedef ap_ufixed<16,1> selu_const_t;
721721
static const selu_const_t lambda = 1.0507009873554805;
722722

723723
#pragma HLS PIPELINE
724-
725-
data_T datareg;
726-
// Index into the lookup table based on data
727-
int index;
728724
for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
729-
datareg = data[ii];
725+
data_T datareg = data[ii];
726+
730727
if (datareg >= 0) {
731-
res[ii] = res_T(1.0507009873554804934193349852946) * datareg;
728+
// Positive branch y = λ · x
729+
res[ii] = lambda * datareg;
732730
} else {
733-
index = datareg * CONFIG_T::table_size / -8;
734-
if (index > CONFIG_T::table_size - 1)
731+
// Negative branch y = table(x)
732+
int index = datareg * CONFIG_T::table_size / -8;
733+
734+
// clamp index to [0, table_size-1]
735+
if (index < 0)
736+
index = 0;
737+
else if (index > CONFIG_T::table_size - 1) {
735738
index = CONFIG_T::table_size - 1;
739+
}
740+
736741
res[ii] = selu_table[index];
737742
}
738743
}

0 commit comments

Comments
 (0)