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

Optimized look-up table #527

Merged
merged 5 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 40 additions & 16 deletions hls4ml/templates/quartus/firmware/nnet_utils/nnet_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,28 @@ void relu1(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
template<class data_T, class res_T, typename CONFIG_T>
void sigmoid(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
{
#include "activation_tables/sigmoid_table.tb"
// Index into the lookup table based on data
#pragma unroll
for (int ii=0; ii<CONFIG_T::n_in; ii++) {
int data_round = (data[ii]*CONFIG_T::table_size/16).to_int();
int index = data_round + 8*CONFIG_T::table_size/16;
if (index < 0) index = 0;
if (index > CONFIG_T::table_size-1) index = CONFIG_T::table_size-1;
res[ii] = (res_T) sigmoid_table[index];
}
static const int MAX_VALUE=8;
#include "activation_tables/sigmoid_table.tb"
#pragma unroll
for (int ii=0; ii < CONFIG_T::n_in; ii++) {
data_T absoluteValue hls_register;
res_T temp2 hls_register;
if(data[ii] < 0 ){
absoluteValue = - data[ii];
}
else{
absoluteValue = data[ii];
}
int index = ( absoluteValue *( CONFIG_T::table_size / MAX_VALUE)).to_int();
if (absoluteValue > MAX_VALUE ) index = CONFIG_T::table_size - 1;
temp2 = (res_T) sigmoid_table[index];
if(data[ii] < 0 ){
res[ii] = 1-temp2;
}
else{
res[ii] = temp2;
}
}
}

// *************************************************
Expand Down Expand Up @@ -163,17 +175,29 @@ void softmax( data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
template<class data_T, class res_T, typename CONFIG_T>
void dense_tanh(data_T data[CONFIG_T::n_in], res_T res[CONFIG_T::n_in])
{
static const int MAX_VALUE=4;
// Initialize the lookup table
#include "activation_tables/tanh_table.tb"
// Index into the lookup table based on data
#pragma unroll
for (int ii=0; ii<CONFIG_T::n_in; ii++) {
ac_int<16> data_round = (data[ii]*CONFIG_T::table_size/8).to_int();
ac_int<16> index = data_round + 4*CONFIG_T::table_size/8;
//std::cout << "Input: " << data[ii] << " Round: " << data_round << " Index: " << index << std::endl;
if (index < 0) index = 0;
if (index > CONFIG_T::table_size-1) index = CONFIG_T::table_size-1;
res[ii] = (res_T) tanh_table[index];
data_T temp hls_register;
res_T temp2 hls_register;
if(data[ii] < 0 ){
temp = -data[ii];
}
else{
temp = data[ii];
}
ac_int<16> index = ( temp *(CONFIG_T::table_size/MAX_VALUE)).to_int();
if (temp > MAX_VALUE ) index = CONFIG_T::table_size-1;
temp2 = (res_T) tanh_table[index];
if(data[ii] < 0 ){
res[ii] = -temp2;
}
else{
res[ii] = temp2;
}
}
}

Expand Down
51 changes: 33 additions & 18 deletions hls4ml/writer/quartus_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,18 +509,21 @@ def write_activation_tables(self, model):
h_file.close()

###################
## sigmoid_table
# sigmoid_table
###################
CENTERED = True
nemerchiedde marked this conversation as resolved.
Show resolved Hide resolved
MAX_VALUE = 8
MIN_VALUE = 0
for layer in model.get_layers():
if(layer.get_attr('activation') == 'sigmoid'):
if layer.get_attr('activation') == 'sigmoid' and layer.get_attr('table_size') is not None:
nemerchiedde marked this conversation as resolved.
Show resolved Hide resolved
table_size = layer.get_attr('table_size')
else:
table_size = 1024
if table_size is None:
table_size = 1024

table_name = 'sigmoid_table'
h_file = open("{}/{}.tb".format(dstpath, table_name),"w")
h_file = open("{}/{}.tb".format(dstpath, table_name), "w")

#meta data
# meta data
h_file.write("#ifndef {}_H_\n".format(table_name.upper()))
h_file.write("#define {}_H_\n".format(table_name.upper()))
h_file.write("\n")
Expand All @@ -534,28 +537,35 @@ def write_activation_tables(self, model):

sep = ''
for i in range(table_size):
in_val = 2*8.0*(i-float(table_size)/2.0)/float(table_size)
if CENTERED:
in_val = i * (MAX_VALUE-MIN_VALUE)/float(table_size) + (MAX_VALUE-MIN_VALUE)/(float(table_size)*2) + MIN_VALUE
else:
in_val = (i * (MAX_VALUE-MIN_VALUE)/float(table_size)) + MIN_VALUE
real_val = 1.0 / (1 + np.exp(-in_val))
h_file.write(sep + str(real_val))
sep = ", "
if(real_val >= 0.5):
h_file.write(sep + str(real_val))
sep = ", "

h_file.write("};\n")
h_file.write("\n#endif\n")
h_file.close()

###################
## tanh_table
# tanh_table
###################
CENTERED = True
MAX_VALUE = 4
MIN_VALUE = 0
for layer in model.get_layers():
if(layer.get_attr('activation') == 'dense_tanh'):
if layer.get_attr('activation') == 'dense_tanh' and layer.get_attr('table_size') is not None:
nemerchiedde marked this conversation as resolved.
Show resolved Hide resolved
table_size = layer.get_attr('table_size')
else:
table_size = 1024
if table_size is None:
table_size = 1024

table_name = 'tanh_table'
h_file = open("{}/{}.tb".format(dstpath, table_name),"w")
h_file = open("{}/{}.tb".format(dstpath, table_name), "w")

#meta data
# meta data
h_file.write("#ifndef {}_H_\n".format(table_name.upper()))
h_file.write("#define {}_H_\n".format(table_name.upper()))
h_file.write("\n")
Expand All @@ -569,10 +579,15 @@ def write_activation_tables(self, model):

sep = ''
for i in range(table_size):
in_val = 2*4.0*(i-float(table_size)/2.0)/float(table_size)
if CENTERED:
in_val = i*(MAX_VALUE-MIN_VALUE)/float(table_size) + (MAX_VALUE-MIN_VALUE)/(float(table_size)*2) + MIN_VALUE
else:
in_val = (i*(MAX_VALUE-MIN_VALUE)/float(table_size)) + MIN_VALUE

real_val = np.tanh(in_val)
h_file.write(sep + str(real_val))
sep = ", "
if(real_val >= 0):
h_file.write(sep + str(real_val))
sep = ", "

h_file.write("};\n")
h_file.write("\n#endif\n")
Expand Down