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

GarNet and GarNetStack in config.py #344

Merged
merged 11 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
10 changes: 8 additions & 2 deletions hls4ml/converters/keras/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ def parse_input_layer(keras_layer, input_names, input_shapes, data_reader, confi
layer = parse_default_keras_layer(keras_layer, input_names)

layer['input_shape'] = keras_layer['config']['batch_input_shape'][1:]
if keras_layer['config']['dtype'] == 'int32':

dtype = keras_layer['config']['dtype']
if dtype.startswith('int') or dtype.startswith('uint'):
layer['type_name'] = 'integer_input_t'
layer['precision'] = IntegerPrecisionType(width=32)
width = int(dtype[dtype.index('int') + 3:])
signed = (not dtype.startswith('u'))
layer['precision'] = IntegerPrecisionType(width=width, signed=signed)
# elif bool, q[u]int, ...

output_shape = keras_layer['config']['batch_input_shape']

return layer, output_shape
Expand Down
4 changes: 2 additions & 2 deletions hls4ml/converters/keras/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def parse_garnet_layer(keras_layer, input_names, input_shapes, data_reader, conf

if not keras_layer['config']['simplified']:
raise Exception('HLS GarNet is compatible only with keras GarNet with simplified=True')
if keras_layer['config']['output_activation'] is not None:
raise Exception('HLS GarNet cannot have output activation')
if keras_layer['config']['output_activation'] not in [None, 'linear']:
raise Exception('HLS GarNet cannot have nonlinear output activation')

layer = parse_default_keras_layer(keras_layer, input_names)

Expand Down
3 changes: 2 additions & 1 deletion hls4ml/templates/vivado/build_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if [[ "$OSTYPE" == "linux-gnu" ]]; then
elif [[ "$OSTYPE" == "darwin"* ]]; then
CFLAGS="-O3 -fPIC -std=c++11"
fi
CFLAGS="$CFLAGS -DNO_VIVADO"
LDFLAGS=
INCFLAGS="-Ifirmware/ap_types/"
PROJECT=myproject
Expand All @@ -14,4 +15,4 @@ LIB_STAMP=mystamp
${CC} ${CFLAGS} ${INCFLAGS} -c firmware/${PROJECT}.cpp -o ${PROJECT}.o
${CC} ${CFLAGS} ${INCFLAGS} -c ${PROJECT}_bridge.cpp -o ${PROJECT}_bridge.o
${CC} ${CFLAGS} ${INCFLAGS} -shared ${PROJECT}.o ${PROJECT}_bridge.o -o firmware/${PROJECT}-${LIB_STAMP}.so
rm -f *.o
rm -f *.o
8 changes: 8 additions & 0 deletions hls4ml/templates/vivado/nnet_utils/nnet_garnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

#include "nnet_common.h"
#include "hls_stream.h"
#ifdef NO_VIVADO
vloncar marked this conversation as resolved.
Show resolved Hide resolved
#include <cmath>
#else
#include "hls_math.h"
#endif

namespace nnet {
namespace garnet_utils {
Expand All @@ -45,7 +49,11 @@ namespace nnet {
for (unsigned iw = 1; iw < table_size; ++iw) {
index = iw;
distance.range(CONFIG_T::distance_width - 1, 0) = index.range(CONFIG_T::distance_width - 1, 0);
#ifdef NO_VIVADO
edge_weights_table[iw] = std::exp(-distance.to_double() * distance.to_double());
jmduarte marked this conversation as resolved.
Show resolved Hide resolved
#else
edge_weights_table[iw] = hls::exp(-distance * distance);
#endif
}
}

Expand Down
51 changes: 48 additions & 3 deletions hls4ml/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def config_from_keras_model(model, granularity='model', default_precision='ap_fi
qkeras_layers = ['QDense', 'QActivation', 'QConv1D', 'QConv2D', 'QBatchNormalization', 'QConv2DBatchnorm']
#Define layers to skip because they're not configurable or not converted to HLS
skip_layers = ['Dropout', 'Flatten', 'Reshape', 'Permute']
graph_layers = ['GarNet', 'GarNetStack']
#All supported layers
supported_layers = core_layers + dense_layers + conv_layers + pooling_layers + norm_layers + activation_layers + merge_layers + qkeras_layers + skip_layers
supported_layers = core_layers + dense_layers + conv_layers + pooling_layers + norm_layers + activation_layers + merge_layers + qkeras_layers + graph_layers + skip_layers

keras_layer_config = None
if model_arch['class_name'] == 'Sequential':
Expand Down Expand Up @@ -157,6 +158,17 @@ def config_from_keras_model(model, granularity='model', default_precision='ap_fi
precision = _get_precision_from_quantizer(qclass)
layer['precision']['result'] = precision

if layer['class_name'] in graph_layers:
# Graph layer config needs access to number of input vertices from the shape of the input tensor
# but to really compute it we'd have to track the full tensor flow as done in hls4ml.converters.keras_to_hls.
# So here we just assume that the first input layer of the model has shape [batch_size, n_vertices, n_features]
try:
first_input_layer = next(kl for kl in keras_layer_config if kl['class_name'] == 'InputLayer')
layer['n_vertices'] = first_input_layer['config']['batch_input_shape'][1]
except:
print(' Generating config for keras layer {}: could not estimate n_vertices. Defaulting to 128.')
layer['n_vertices'] = 128

print('Layer name: {}, layer type: {}'.format(layer['name'], layer['class_name']))
layer_list.append( layer )
if 'activation' in layer['config'] and layer['class_name'] not in activation_layers + qkeras_layers:
Expand Down Expand Up @@ -206,9 +218,42 @@ def make_layer_config(layer):
layer_config['Precision'] = default_precision
layer_config['ReuseFactor'] = default_reuse_factor

elif layer['class_name'] in ['GarNet', 'GarNetStack']:
## Following code copy-pasted from hls4ml.model.hls_layers - can we factor out commonalities between the two modules?

## Define default precisions for various internal arrays (can be overridden from the config file)
import math
log2_reuse = int(math.log(default_reuse_factor, 2.))
n_vertices_width = int(math.log(layer['n_vertices'], 2.))

# We always give 10 digits for the subintegral part
fwidth = 10
# Integral precision for aggr_t depends on how large the temporary sum for weighed feature mean will be
aggr_intw = max(log2_reuse, n_vertices_width - log2_reuse) + 3 # safety factor 2**3
aggr_w = aggr_intw + fwidth
# edge_weight_aggr_t does not need the safety factor
ew_aggr_intw = aggr_intw - 3
ew_aggr_w = ew_aggr_intw + fwidth

layer_config['Precision'] = {}
layer_config['Precision']['edge_weight'] = 'ap_ufixed<10,0,AP_TRN,AP_SAT>'
layer_config['Precision']['edge_weight_aggr'] = 'ap_ufixed<{},{},AP_TRN,AP_SAT>'.format(ew_aggr_w, ew_aggr_intw)
layer_config['Precision']['aggr'] = 'ap_fixed<{},{},AP_TRN,AP_SAT>'.format(aggr_w, aggr_intw)
layer_config['Precision']['norm'] = 'ap_ufixed<14,4,AP_TRN,AP_SAT>'

layer_config['ReuseFactor'] = default_reuse_factor

elif layer['class_name'] == 'Input':
layer_config['Precision'] = {}
layer_config['Precision']['result'] = default_precision

dtype = layer['config']['dtype']
if dtype.startswith('int') or dtype.startswith('uint'):
typename = dtype[:dtype.index('int') + 3]
width = int(dtype[dtype.index('int') + 3:])
layer_config['Precision']['result'] = 'ap_{}<{}>'.format(typename, width)
# elif bool, q[u]int, ...
else:
layer_config['Precision']['result'] = default_precision

else:
layer_config['Precision'] = default_precision
Expand Down Expand Up @@ -338,4 +383,4 @@ def config_from_onnx_model(model, granularity='model', default_precision='ap_fix

config['Model'] = model_config

return config
return config
Loading