From 54657f9f8a0da5a5128eb500bf6a32e251cafb24 Mon Sep 17 00:00:00 2001 From: Jan-Frederik Schulte Date: Wed, 6 Nov 2024 09:24:51 -0500 Subject: [PATCH 1/2] make auto default precision for pytorch parser --- hls4ml/utils/config.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/hls4ml/utils/config.py b/hls4ml/utils/config.py index 1bd9ff25ef..14b8cf0b83 100644 --- a/hls4ml/utils/config.py +++ b/hls4ml/utils/config.py @@ -284,6 +284,7 @@ def config_from_pytorch_model( default_reuse_factor=1, channels_last_conversion='full', transpose_outputs=True, + max_precision=None, ): """Create an HLS conversion config given the PyTorch model. @@ -304,7 +305,8 @@ def config_from_pytorch_model( will generate config keys for every layer separately, allowing for highly specific configuration tweaks. backend(str, optional): Name of the backend to use - default_precision (str, optional): Default precision to use. Defaults to 'fixed<16,6>'. + default_precision (str, optional): Default precision to use. Defaults to 'fixed<16,6>'. Note, this must + be an explicit precision: 'auto' is not allowed. default_reuse_factor (int, optional): Default reuse factor. Defaults to 1. channels_last_conversion (string, optional): Configures the conversion of pytorch layers to 'channels_last' dataformate. Can be set to 'full', 'internal', or 'off'. If 'full', both the inputs @@ -313,6 +315,8 @@ def config_from_pytorch_model( transpose_outputs (bool, optional): Set to 'False' if the output should not be transposed from channels_last into channels_first data format. Defaults to 'False'. If False, outputs needs to be transposed manually. + max_precision (str or None, optional): Maximum width precision to use. Defaults to None, meaning no maximum. + Note: Only integer and fixed precisions are supported Raises: Exception: If PyTorch model has layers not supported by hls4ml. @@ -324,7 +328,10 @@ def config_from_pytorch_model( config = {} model_config = {} - model_config['Precision'] = default_precision + model_config['Precision'] = {} + model_config['Precision']['default'] = default_precision + if max_precision is not None: + model_config['Precision']['maximum'] = max_precision model_config['ReuseFactor'] = default_reuse_factor model_config['ChannelsLastConversion'] = channels_last_conversion model_config['TransposeOutputs'] = transpose_outputs @@ -372,7 +379,7 @@ def make_layer_config(layer): if name.endswith('_t'): name = name[:-2] if attr.default is None: - precision_cfg[name] = default_precision + precision_cfg[name] = 'auto' else: precision_cfg[name] = str(attr.default) elif attr.name == 'reuse_factor': From 01d4f793b4b62a09e8b930753063b12ea3cebe4d Mon Sep 17 00:00:00 2001 From: Jan-Frederik Schulte Date: Mon, 11 Nov 2024 12:40:03 -0500 Subject: [PATCH 2/2] more default settings suggested by Jovan --- hls4ml/utils/config.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hls4ml/utils/config.py b/hls4ml/utils/config.py index 14b8cf0b83..6938cf180a 100644 --- a/hls4ml/utils/config.py +++ b/hls4ml/utils/config.py @@ -336,6 +336,8 @@ def config_from_pytorch_model( model_config['ChannelsLastConversion'] = channels_last_conversion model_config['TransposeOutputs'] = transpose_outputs model_config['Strategy'] = 'Latency' + model_config['BramFactor'] = 1_000_000_000 + model_config['TraceOutput'] = False config['Model'] = model_config config['PytorchModel'] = model