Skip to content

Commit

Permalink
A few updates (fastmachinelearning#29)
Browse files Browse the repository at this point in the history
* Increase precision of weight printing

* Get class name differently for profiling. Import profiling in model/__init__ for easier import elsewhere

* Make optimizer passes configurable. API is with list in config file. Splitting hls_model.py into hls_model.py and hls_layers.py was necessary to remove circular import dependency from optimizers importing Layers and utilities, while hls_model now needs to import optimizer

* QKeras use of integer bits seems to differ a bit from ap_fixed. e.g. quantized_bits(4,0).max() is 1.0, whereas it would be 0.5 with ap_fixed. So, add 1 bit to the integer for ap_fixed types

* Add 1 bit elsewhere for QKeras, since they don't count the sign bit
  • Loading branch information
thesps authored May 6, 2020
1 parent f403be3 commit 3907d9c
Show file tree
Hide file tree
Showing 12 changed files with 888 additions and 876 deletions.
4 changes: 3 additions & 1 deletion hls4ml/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def convert_from_yaml_config(yamlConfig):

def convert_from_keras_model(model, output_dir='my-hls-test', project_name='myproject',
fpga_part='xcku115-flvb2104-2-i', clock_period=5, hls_config={}):

config = create_vivado_config(output_dir=output_dir,
project_name=project_name, fpga_part=fpga_part, clock_period=clock_period)
config['KerasModel'] = model
Expand All @@ -82,5 +81,8 @@ def convert_from_keras_model(model, output_dir='my-hls-test', project_name='mypr

if 'LayerType' in hls_config:
config['HLSConfig']['LayerType'] = hls_config['LayerType']

if 'Optimizers' in hls_config:
config['HLSConfig']['Optimizers'] = hls_config['Optimizers']

return keras_to_hls(config)
6 changes: 3 additions & 3 deletions hls4ml/converters/keras/qkeras.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_type(quantizer_config):
else:
return IntegerPrecisionType(width=width, signed=True)
else:
return FixedPrecisionType(width=width, integer=integer, signed=True)
return FixedPrecisionType(width=width+1, integer=integer+1, signed=True)

def get_quantizer_from_config(keras_layer, quantizer_var):
quantizer_config = keras_layer['config']['{}_quantizer'.format(quantizer_var)]
Expand Down Expand Up @@ -81,8 +81,8 @@ def parse_qactivation_layer(keras_layer, input_names, input_shapes, data_reader,

layer['class_name'] = 'Activation'
layer['activation'] = act_class.replace('quantized_', '')
layer['bits'] = activation_config['config']['bits']
layer['integer'] = activation_config['config']['integer']
layer['bits'] = activation_config['config']['bits'] + 1
layer['integer'] = activation_config['config']['integer'] + 1
#TODO this needs extra work in HLS model and HLS templates

return layer, [shape for shape in input_shapes[0]]
Expand Down
5 changes: 0 additions & 5 deletions hls4ml/converters/keras_to_hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import math

from hls4ml.model import HLSModel
from hls4ml.model.optimizer import optimize_model

MAXMULT = 4096

Expand Down Expand Up @@ -478,8 +477,6 @@ def keras_to_hls_old(yamlConfig):

print('Creating HLS model')
hls_model = HLSModel(yamlConfig, reader, layer_list, input_layers, output_layers)
optimizers = ['eliminate_linear_activation', 'merge_batch_norm_quantized_tanh', 'quantize_dense_output', 'fuse_dense_batch_norm']
optimize_model(hls_model, optimizers)
return hls_model

def parse_default_keras_layer(keras_layer, input_names):
Expand Down Expand Up @@ -638,6 +635,4 @@ def keras_to_hls(config):

print('Creating HLS model')
hls_model = HLSModel(config, reader, layer_list, input_layers, output_layers)
optimizers = ['eliminate_linear_activation', 'merge_batch_norm_quantized_tanh', 'quantize_dense_output', 'fuse_dense_batch_norm']
optimize_model(hls_model, optimizers)
return hls_model
3 changes: 0 additions & 3 deletions hls4ml/converters/onnx_to_hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from onnx import optimizer, helper, numpy_helper, shape_inference

from hls4ml.model import HLSModel
from hls4ml.model.optimizer import optimize_model

MAXMULT = 4096

Expand Down Expand Up @@ -374,6 +373,4 @@ def onnx_to_hls(yamlConfig):

print('Creating HLS model')
hls_model = HLSModel(yamlConfig, reader, layer_list, input_layers, output_layers)
optimizers = ['eliminate_linear_activation', 'merge_batch_norm_quantized_tanh', 'quantize_dense_output', 'fuse_dense_batch_norm']
optimize_model(hls_model, optimizers)
return hls_model
3 changes: 0 additions & 3 deletions hls4ml/converters/pytorch_to_hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import re

from hls4ml.model import HLSModel
from hls4ml.model.optimizer import optimize_model

class PyTorchDataReader:
def __init__(self, config):
Expand Down Expand Up @@ -101,6 +100,4 @@ def pytorch_to_hls(yamlConfig):
reader = PyTorchDataReader(yamlConfig)
print('Creating HLS model')
hls_model = HLSModel(yamlConfig, reader, layer_list)
optimizers = ['eliminate_linear_activation', 'merge_batch_norm_quantized_tanh', 'quantize_dense_output', 'fuse_dense_batch_norm']
optimize_model(hls_model, optimizers)
return hls_model
3 changes: 0 additions & 3 deletions hls4ml/converters/tf_to_hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from tensorflow.python.framework import tensor_util

from hls4ml.model import HLSModel
from hls4ml.model.optimizer import optimize_model

MAXMULT = 4096

Expand Down Expand Up @@ -371,6 +370,4 @@ def tf_to_hls(yamlConfig):
reader = TFDataReader(graph)
print('Creating HLS model')
hls_model = HLSModel(yamlConfig, reader, layer_list, input_layers, output_layers)
optimizers = ['eliminate_linear_activation', 'merge_batch_norm_quantized_tanh', 'quantize_dense_output', 'fuse_biasadd', 'fuse_dense_batch_norm']
optimize_model(hls_model, optimizers)
return hls_model
3 changes: 2 additions & 1 deletion hls4ml/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import

from .hls_model import HLSModel, HLSConfig
from .hls_model import HLSModel, HLSConfig
from . import profiling
Loading

0 comments on commit 3907d9c

Please sign in to comment.