Skip to content

Commit a07d4f1

Browse files
authored
Merge pull request #1298 from valerioedu/selu-lambda-clean
Fix SeLU lambda precision (Follow-up to #1287)
2 parents 6cdf842 + 770dc44 commit a07d4f1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

hls4ml/templates/vivado/nnet_utils/nnet_activation.h

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

720-
#pragma HLS PIPELINE
720+
typedef ap_ufixed<16, 1> selu_const_t;
721+
static const selu_const_t lambda = 1.0507009873554805;
721722

722-
data_T datareg;
723-
// Index into the lookup table based on data
724-
int index;
723+
#pragma HLS PIPELINE
725724
for (int ii = 0; ii < CONFIG_T::n_in; ii++) {
726-
datareg = data[ii];
725+
data_T datareg = data[ii];
726+
727727
if (datareg >= 0) {
728-
res[ii] = res_T(1.0507009873554804934193349852946) * datareg;
728+
// Positive branch y = λ · x
729+
res[ii] = lambda * datareg;
729730
} else {
730-
index = datareg * CONFIG_T::table_size / -8;
731-
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) {
732738
index = CONFIG_T::table_size - 1;
739+
}
740+
733741
res[ii] = selu_table[index];
734742
}
735743
}

0 commit comments

Comments
 (0)