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

Fixes: quantized_relu & unsigned profiling #405

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
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
23 changes: 11 additions & 12 deletions hls4ml/model/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,49 +159,48 @@ def types_histogram(data, fmt='longform'):
types_plots = {'boxplot' : types_boxplot,
'histogram' : types_histogram}

def ap_fixed_WIF(dtype):
def ap_fixed_WIFS(dtype):
jmduarte marked this conversation as resolved.
Show resolved Hide resolved
from hls4ml.templates.vivado_template import VivadoBackend
dtype = VivadoBackend.convert_precision_string(None, dtype)
W, I, F = dtype.width, dtype.integer, dtype.fractional
return W, I, F
W, I, F, S = dtype.width, dtype.integer, dtype.fractional, dtype.signed
return W, I, F, S

def types_hlsmodel(model):
suffix = ['w', 'b']
data = {'layer' : [], 'low' : [], 'high' : []}
# Plot the default precision
default_precision = model.config.model_precision['default']
# assumes ap_fixed
W, I, F = ap_fixed_WIF(default_precision)
W, I, F, S = ap_fixed_WIFS(default_precision)
data['layer'].append('model')
data['low'].append(-F)
data['high'].append(I-1)
data['high'].append(I-1 if S else I)

for layer in model.get_layers():
for iw, weight in enumerate(layer.get_weights()):
wname = '{}/{}'.format(layer.name, suffix[iw])
T = weight.type
if T.name != 'model':
W, I, F = ap_fixed_WIF(T.precision)
W, I, F, S = ap_fixed_WIFS(T.precision)
data['layer'].append(wname)
data['low'].append(-F)
data['high'].append(I-1)
data['high'].append(I-1 if S else I)
data = pandas.DataFrame(data)
return data

def activation_types_hlsmodel(model):
data = {'layer' : [], 'low' : [], 'high' : []}
# Get the default precision
default_precision = model.config.model_precision['default']
W, I, F = ap_fixed_WIF(default_precision)
W, I, F, S = ap_fixed_WIFS(default_precision)
data['layer'].append('model')
data['low'].append(-F)
data['high'].append(I-1)
data['high'].append(I-1 if S else I)
for layer in model.get_layers():
T = layer.get_output_variable().type.precision
W, I, F = ap_fixed_WIF(T)
W, I, F, S = ap_fixed_WIFS(T)
data['layer'].append(layer.name)
data['low'].append(-F)
data['high'].append(I-1)
data['high'].append(I-1 if S else I)
data = pandas.DataFrame(data)
return data

Expand Down
10 changes: 7 additions & 3 deletions hls4ml/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def _get_precision_from_quantizer(quantizer):
quantizer['class_name'] = quantizer_obj.__name__

supported_quantizers = ['quantized_bits', 'quantized_relu', 'quantized_tanh', 'quantized_po2', 'quantized_relu_po2']
signed = True
if quantizer['class_name'] in supported_quantizers:
bits = int(quantizer['config']['bits'])
# if integer isn't specified, it should be the same as bits
integer = int(quantizer['config'].get('integer', bits-1)) + 1

if quantizer['class_name'] == 'quantized_relu':
signed = False
integer -= 1
elif quantizer['class_name'] in ['binary', 'stochastic_binary', 'binary_tanh']:
bits = 2
integer = 2
Expand All @@ -53,10 +56,11 @@ def _get_precision_from_quantizer(quantizer):
raise Exception('ERROR: Unsupported quantizer: {}'.format(quantizer['class_name']))

decimal = bits - integer
signed = '' if signed else 'u'
if decimal > 0:
return 'ap_fixed<{},{}>'.format(bits, integer)
return 'ap_{}fixed<{},{}>'.format(signed, bits, integer)
else:
return 'ap_int<{}>'.format(bits)
return 'ap_{}int<{}>'.format(signed, bits)

def config_from_keras_model(model, granularity='model', default_precision='ap_fixed<16,6>', default_reuse_factor=1):
"""Create an HLS conversion config given the Keras model.
Expand Down
10 changes: 7 additions & 3 deletions test/pytest/test_qkeras.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ def randX_1000_1():
(quantized_bits(8,4)),
(quantized_bits(4,2)),
(quantized_bits(4,0)),
(quantized_bits(10,0)),])
#(quantized_relu(4)),
#(quantized_relu(10))])
(quantized_bits(10,0)),
(quantized_relu(4)),
(quantized_relu(4,2)),
(quantized_relu(8)),
(quantized_relu(8,4)),
(quantized_relu(10)),
(quantized_relu(10,5))])
def test_quantizer(randX_1000_1, quantizer):
'''
Test a single quantizer as an Activation function.
Expand Down