diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd0197bbf8..8a149beefc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ exclude: (^hls4ml\/templates\/(vivado|quartus)\/(ap_types|ac_types)\/|^test/pyte repos: - repo: https://github.com/psf/black - rev: 22.12.0 + rev: 23.3.0 hooks: - id: black language_version: python3 @@ -30,7 +30,7 @@ repos: args: ["--profile", "black", --line-length=125] - repo: https://github.com/asottile/pyupgrade - rev: v3.3.1 + rev: v3.4.0 hooks: - id: pyupgrade args: ["--py36-plus"] @@ -41,7 +41,7 @@ repos: - id: setup-cfg-fmt - repo: https://github.com/pycqa/flake8 - rev: 5.0.4 + rev: 6.0.0 hooks: - id: flake8 exclude: docs/conf.py diff --git a/contrib/kl_layer/kl_layer.py b/contrib/kl_layer/kl_layer.py index ec2af1b797..52990fa35d 100644 --- a/contrib/kl_layer/kl_layer.py +++ b/contrib/kl_layer/kl_layer.py @@ -34,7 +34,6 @@ def build(self, input_shape): super().build(input_shape) def _merge_function(self, inputs): - mean = inputs[0] log_var = inputs[1] diff --git a/hls4ml/backends/fpga/fpga_backend.py b/hls4ml/backends/fpga/fpga_backend.py index ec496c2104..8cfaec8b3f 100644 --- a/hls4ml/backends/fpga/fpga_backend.py +++ b/hls4ml/backends/fpga/fpga_backend.py @@ -391,7 +391,6 @@ def product_type(self, data_T, weight_T): return product def compute_conv1d_instructions(self, in_W, in_C, kernel_size=3, stride=1, pad=0): - # Current limitations assert pad == 0 @@ -427,7 +426,6 @@ def compute_conv1d_instructions(self, in_W, in_C, kernel_size=3, stride=1, pad=0 return (min_W, windows_int) def compute_conv2d_instructions(self, in_H, in_W, in_C, kernel_size=3, stride=1, pad=0): - if isinstance(kernel_size, Iterable): kernel_height = kernel_size[0] kernel_width = kernel_size[1] diff --git a/hls4ml/backends/quartus/passes/convolution_winograd.py b/hls4ml/backends/quartus/passes/convolution_winograd.py index d28a82d231..9a66864129 100644 --- a/hls4ml/backends/quartus/passes/convolution_winograd.py +++ b/hls4ml/backends/quartus/passes/convolution_winograd.py @@ -82,7 +82,6 @@ def match(self, node): def transform(self, model, node): if isinstance(node, Conv1D): if node.get_attr('filt_width', 3) == 3: - # First, transpose to a format suitable for the Winograd algorithm (F, C, W) # Note, this assumes a format post-resource strategy optimizer, that is (F, W, C) # Therefore, (F, W, C) => (F, C, W) @@ -127,7 +126,6 @@ def transform(self, model, node): elif isinstance(node, Conv2D): if node.get_attr('filt_height', 3) == 3 and node.get_attr('filt_width', 3) == 3: - # First, transpose to a format suitable for the Winograd algorithm (F, C, H, W) # Note, this assumes a format post-resource strategy optimizer, that is (F, H, W, C) # Therefore, (F, H, W, C) => (F, C, H, W) diff --git a/hls4ml/backends/quartus/quartus_backend.py b/hls4ml/backends/quartus/quartus_backend.py index 55327ee978..f2b5fd9cec 100644 --- a/hls4ml/backends/quartus/quartus_backend.py +++ b/hls4ml/backends/quartus/quartus_backend.py @@ -133,7 +133,6 @@ def create_initial_config(self, part='Arria10', clock_period=5, io_type='io_para return config def build(self, model, synth=True, fpgasynth=False, log_level=1, cont_if_large_area=False): - """ Builds the project using Intel HLS compiler. diff --git a/hls4ml/backends/vivado/passes/recurrent_templates.py b/hls4ml/backends/vivado/passes/recurrent_templates.py index c2d665e4bb..aae806b35c 100644 --- a/hls4ml/backends/vivado/passes/recurrent_templates.py +++ b/hls4ml/backends/vivado/passes/recurrent_templates.py @@ -79,7 +79,6 @@ def __init__(self): self.mult2_template = recr_mult_config_template def format(self, node): - params = self._default_config_params(node) params['n_in'] = node.get_input_variable().dim_names[1] diff --git a/hls4ml/backends/vivado/passes/resource_strategy.py b/hls4ml/backends/vivado/passes/resource_strategy.py index 53b0e8661e..63e6e0b4db 100644 --- a/hls4ml/backends/vivado/passes/resource_strategy.py +++ b/hls4ml/backends/vivado/passes/resource_strategy.py @@ -8,7 +8,6 @@ class ApplyResourceStrategy(OptimizerPass): '''Transposes the weights to use the dense_resource matrix multiply routine''' def match(self, node): - node_matches = isinstance(node, (Dense, Conv1D, SeparableConv1D, Conv2D, SeparableConv2D, LSTM, GRU)) is_resource_strategy = node.get_attr('strategy', '').lower() == 'resource' already_transformed = node.get_attr('_weights_transposed', False) is True diff --git a/hls4ml/converters/__init__.py b/hls4ml/converters/__init__.py index 751f48b444..7ebc543b80 100644 --- a/hls4ml/converters/__init__.py +++ b/hls4ml/converters/__init__.py @@ -49,7 +49,6 @@ # and is defined in this module (i.e., not imported) if callable(func) and hasattr(func, 'handles') and func.__module__ == lib.__name__: for layer in func.handles: - if model_type == 'keras': register_keras_layer_handler(layer, func) elif model_type == 'pytorch': diff --git a/hls4ml/converters/keras/qkeras.py b/hls4ml/converters/keras/qkeras.py index 6bb88c7aa3..94b0ad5d00 100644 --- a/hls4ml/converters/keras/qkeras.py +++ b/hls4ml/converters/keras/qkeras.py @@ -20,7 +20,6 @@ def get_quantizer_from_config(keras_layer, quantizer_var): @keras_handler('QDense') def parse_qdense_layer(keras_layer, input_names, input_shapes, data_reader): - layer, output_shape = parse_dense_layer(keras_layer, input_names, input_shapes, data_reader) layer['weight_quantizer'] = get_quantizer_from_config(keras_layer, 'kernel') @@ -124,7 +123,6 @@ def parse_qactivation_layer(keras_layer, input_names, input_shapes, data_reader) @keras_handler('QBatchNormalization') def parse_qbatchnorm_layer(keras_layer, input_names, input_shapes, data_reader): - layer, output_shape = parse_batchnorm_layer(keras_layer, input_names, input_shapes, data_reader) layer['mean_quantizer'] = get_quantizer_from_config(keras_layer, 'mean') diff --git a/hls4ml/converters/keras_to_hls.py b/hls4ml/converters/keras_to_hls.py index 47232748ad..0e1e282bcb 100644 --- a/hls4ml/converters/keras_to_hls.py +++ b/hls4ml/converters/keras_to_hls.py @@ -191,7 +191,6 @@ def get_model_arch(config): def parse_keras_model(model_arch, reader): - # This is a list of dictionaries to hold all the layer info we need to generate HLS layer_list = [] diff --git a/hls4ml/converters/onnx/convolution.py b/hls4ml/converters/onnx/convolution.py index 368a849e4e..39b2232169 100644 --- a/hls4ml/converters/onnx/convolution.py +++ b/hls4ml/converters/onnx/convolution.py @@ -10,7 +10,6 @@ @onnx_handler('Conv') def parse_conv_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name layer['data_format'] = 'channels_first' # ONNX's default is channel first @@ -45,7 +44,6 @@ def parse_conv_layer(reader, node, inputs_map, input_shapes, graph, config): output_shape = [input_shapes[0][0], layer['n_filt'], layer['out_width']] elif len(input_shapes[0]) == 4: # Conv2D - layer['class_name'] = 'Conv2D' layer['in_height'] = input_shapes[0][2] diff --git a/hls4ml/converters/onnx/core.py b/hls4ml/converters/onnx/core.py index 8b41a6e92a..940b860870 100644 --- a/hls4ml/converters/onnx/core.py +++ b/hls4ml/converters/onnx/core.py @@ -3,7 +3,6 @@ @onnx_handler(*['Gemm', 'MatMul']) def parse_gemm_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['class_name'] = 'Dense' @@ -61,7 +60,6 @@ def parse_gemm_layer(reader, node, inputs_map, input_shapes, graph, config): @onnx_handler(*activation_layers) def parse_activation_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name @@ -70,7 +68,6 @@ def parse_activation_layer(reader, node, inputs_map, input_shapes, graph, config layer['inputs'] = get_onnx_input_name(node, graph) if layer['class_name'] != 'Activation': - if layer['class_name'] == 'Softmax': layer['activation'] = 'softmax' @@ -79,7 +76,6 @@ def parse_activation_layer(reader, node, inputs_map, input_shapes, graph, config layer['activ_param'] = get_onnx_attribute(node, 'alpha', 0.01) elif layer['class_name'] == 'Clip': - clip_min_node = [x for x in graph.initializer if x.name in node.input] clip_min = clip_min_node[0].float_data[0] @@ -99,7 +95,6 @@ def parse_activation_layer(reader, node, inputs_map, input_shapes, graph, config @onnx_handler('BatchNormalization') def parse_batchnorm_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['class_name'] = 'BatchNormalization' diff --git a/hls4ml/converters/onnx/merge.py b/hls4ml/converters/onnx/merge.py index e7337ac5bf..9ccd432d18 100644 --- a/hls4ml/converters/onnx/merge.py +++ b/hls4ml/converters/onnx/merge.py @@ -5,7 +5,6 @@ @onnx_handler(*merge_layers) def parse_merge_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['class_name'] = node.op_type layer['name'] = node.name diff --git a/hls4ml/converters/onnx/pooling.py b/hls4ml/converters/onnx/pooling.py index 7c11b3f4d9..67fa76c7c7 100644 --- a/hls4ml/converters/onnx/pooling.py +++ b/hls4ml/converters/onnx/pooling.py @@ -12,7 +12,6 @@ @onnx_handler(*pool_operations) def parse_pool_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name layer['inputs'] = get_onnx_input_name(node, graph) @@ -91,7 +90,6 @@ def parse_pool_layer(reader, node, inputs_map, input_shapes, graph, config): @onnx_handler(*global_pooling_layers) def parse_global_pooling_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name layer['inputs'] = get_onnx_input_name(node, graph) diff --git a/hls4ml/converters/onnx/reshape.py b/hls4ml/converters/onnx/reshape.py index a0d6807edd..5bbf58b079 100644 --- a/hls4ml/converters/onnx/reshape.py +++ b/hls4ml/converters/onnx/reshape.py @@ -5,7 +5,6 @@ @onnx_handler('Transpose') def parse_transpose_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name layer['class_name'] = 'Transpose' @@ -21,7 +20,6 @@ def parse_transpose_layer(reader, node, inputs_map, input_shapes, graph, config) @onnx_handler('Reshape') def parse_reshape_layer(reader, node, inputs_map, input_shapes, graph, config): - layer = {} layer['name'] = node.name layer['class_name'] = 'Reshape' diff --git a/hls4ml/converters/onnx_to_hls.py b/hls4ml/converters/onnx_to_hls.py index d57cbb8c4c..106daf62da 100644 --- a/hls4ml/converters/onnx_to_hls.py +++ b/hls4ml/converters/onnx_to_hls.py @@ -53,7 +53,6 @@ def get_weights_data(self, layer_name, var_name): tensor = next((x for x in self.model.graph.initializer if x.name == inputs['inputs'][inp_idx]), None) if tensor is not None: - data = numpy_helper.to_array(tensor) if inputs['transpose']: @@ -278,7 +277,6 @@ def onnx_to_hls(config): print('Topology:') for node in graph.node: - if node.op_type not in supported_layers: raise Exception(f'ERROR: Unsupported operation type: {node.op_type}') diff --git a/hls4ml/converters/pytorch/core.py b/hls4ml/converters/pytorch/core.py index fadd6fa9fa..fbf1e377e2 100644 --- a/hls4ml/converters/pytorch/core.py +++ b/hls4ml/converters/pytorch/core.py @@ -35,7 +35,6 @@ def parse_linear_layer(pytorch_layer, layer_name, input_shapes, data_reader, con @pytorch_handler(*activation_layers) def parse_activation_layer(pytorch_layer, layer_name, input_shapes, data_reader, config): - layer = {} layer['class_name'] = pytorch_layer.__class__.__name__ diff --git a/hls4ml/converters/pytorch_to_hls.py b/hls4ml/converters/pytorch_to_hls.py index d87961fd88..676e7e380c 100644 --- a/hls4ml/converters/pytorch_to_hls.py +++ b/hls4ml/converters/pytorch_to_hls.py @@ -159,7 +159,6 @@ def pytorch_to_hls(config): print("Input Shape: ", input_shapes) for layer_name, pytorch_layer in model.named_modules(): - pytorch_class = pytorch_layer.__class__.__name__ # First module is the whole model's class diff --git a/hls4ml/model/graph.py b/hls4ml/model/graph.py index 5616df3020..c44fd8f02e 100644 --- a/hls4ml/model/graph.py +++ b/hls4ml/model/graph.py @@ -647,7 +647,6 @@ def compile(self): lib_name = self.config.backend.compile(self) if self._top_function_lib is not None: - if platform.system() == "Linux": libdl_libs = ['libdl.so', 'libdl.so.2'] for libdl in libdl_libs: diff --git a/hls4ml/model/profiling.py b/hls4ml/model/profiling.py index bae80edb9f..467a914aef 100644 --- a/hls4ml/model/profiling.py +++ b/hls4ml/model/profiling.py @@ -567,7 +567,6 @@ def get_ymodel_keras(keras_model, X): isinstance(layer, keras.layers.Activation) or isinstance(layer, qkeras.qlayers.QActivation) ): if layer.activation: - if layer.activation.__class__.__name__ == "linear": ymodel[layer.name] = _get_output(layer, X, keras_model.input) diff --git a/hls4ml/model/types.py b/hls4ml/model/types.py index 8c6083e091..b6f2e42a01 100644 --- a/hls4ml/model/types.py +++ b/hls4ml/model/types.py @@ -408,7 +408,6 @@ class ExponentType(NamedType): """ def __init__(self, name, precision, **kwargs): - if not name.startswith('exponent_'): name = 'exponent_' + name super().__init__(name, precision, **kwargs) diff --git a/hls4ml/templates/vivado_accelerator/alveo/python_drivers/axi_stream_driver.py b/hls4ml/templates/vivado_accelerator/alveo/python_drivers/axi_stream_driver.py index c6dcb7688a..c589bcf057 100644 --- a/hls4ml/templates/vivado_accelerator/alveo/python_drivers/axi_stream_driver.py +++ b/hls4ml/templates/vivado_accelerator/alveo/python_drivers/axi_stream_driver.py @@ -6,7 +6,6 @@ class NeuralNetworkOverlay(Overlay): def __init__(self, xclbin_name, dtbo=None, download=True, ignore_version=False, device=None): - super().__init__(xclbin_name, dtbo=dtbo, download=download, ignore_version=ignore_version, device=device) self.input_buffer = None self.output_buffer = None diff --git a/hls4ml/utils/config.py b/hls4ml/utils/config.py index 4adaf2d3cb..0368d3276a 100644 --- a/hls4ml/utils/config.py +++ b/hls4ml/utils/config.py @@ -6,7 +6,6 @@ def create_config(output_dir='my-hls-test', project_name='myproject', backend='Vivado', **kwargs): - backend_list = hls4ml.backends.get_available_backends() if backend.lower() not in backend_list: raise Exception(f'Unknown backend: {backend}') @@ -25,7 +24,6 @@ def create_config(output_dir='my-hls-test', project_name='myproject', backend='V def _get_precision_from_quantizer(quantizer): - if isinstance(quantizer, str): quantizer_obj = qkeras.get_quantizer(quantizer) quantizer = {} diff --git a/hls4ml/utils/example_models.py b/hls4ml/utils/example_models.py index 640d47e41c..5fefbd662b 100644 --- a/hls4ml/utils/example_models.py +++ b/hls4ml/utils/example_models.py @@ -27,21 +27,18 @@ def _load_data_config_avai(model_name): def _data_is_available(model_name): - data = _load_data_config_avai(model_name) return data['example_data'] def _config_is_available(model_name): - data = _load_data_config_avai(model_name) return data['example_config'] def _create_default_config(model_name, model_config, backend): - # Initiate the configuration file config = create_config(backend=backend) @@ -69,7 +66,6 @@ def _filter_name(model_name): def _load_example_data(model_name): - print("Downloading input & output example files ...") filtered_name = _filter_name(model_name) @@ -89,7 +85,6 @@ def _load_example_data(model_name): def _load_example_config(model_name): - print("Downloading configuration files ...") filtered_name = _filter_name(model_name) @@ -179,7 +174,6 @@ def fetch_example_model(model_name, backend='Vivado'): def fetch_example_list(): - link_to_list = 'https://raw.githubusercontent.com/hls-fpga-machine-learning/example-models/master/available_models.json' temp_file, _ = urlretrieve(link_to_list) diff --git a/hls4ml/writer/quartus_writer.py b/hls4ml/writer/quartus_writer.py index 79ebf16e6c..a5ba6dc1c3 100644 --- a/hls4ml/writer/quartus_writer.py +++ b/hls4ml/writer/quartus_writer.py @@ -405,7 +405,6 @@ def write_defines(self, model): fout = open(f'{model.config.get_output_dir()}/firmware/defines.h', 'w') for line in f.readlines(): - # Insert numbers if '// hls-fpga-machine-learning insert numbers' in line: newline = line @@ -450,7 +449,6 @@ def write_parameters(self, model): fout = open(f'{model.config.get_output_dir()}/firmware/parameters.h', 'w') for line in f.readlines(): - if '// hls-fpga-machine-learning insert includes' in line: newline = line for include in sorted(set(sum((layer.get_attr('include_header', []) for layer in model.get_layers()), []))): @@ -760,7 +758,6 @@ def write_bridge(self, model): indent = ' ' for line in f.readlines(): - if 'MYPROJECT' in line: newline = line.replace('MYPROJECT', format(model.config.get_project_name().upper())) @@ -882,7 +879,6 @@ def write_build_script(self, model): fout = open(f'{model.config.get_output_dir()}/Makefile', 'w') for line in f.readlines(): - line = line.replace('myproject', model.config.get_project_name()) if 'DEVICE :=' in line: @@ -1054,7 +1050,6 @@ def __write_softsign_table(self, model, path): sep = '' for i in range(table_size): - in_val = ( i * (MAX_VALUE - MIN_VALUE) / float(table_size) + (MAX_VALUE - MIN_VALUE) / (float(table_size) * 2) diff --git a/hls4ml/writer/vivado_accelerator_writer.py b/hls4ml/writer/vivado_accelerator_writer.py index 46c193fdbd..98ac8f2dcd 100644 --- a/hls4ml/writer/vivado_accelerator_writer.py +++ b/hls4ml/writer/vivado_accelerator_writer.py @@ -279,7 +279,6 @@ def modify_build_script(self, model): fout.close() def write_wrapper_test(self, model): - ################### # write myproject_test_wrapper.cpp ################### diff --git a/hls4ml/writer/vivado_writer.py b/hls4ml/writer/vivado_writer.py index 7d22b0b64a..2fbe3d9438 100644 --- a/hls4ml/writer/vivado_writer.py +++ b/hls4ml/writer/vivado_writer.py @@ -239,7 +239,6 @@ def write_project_header(self, model): indent = ' ' for line in f.readlines(): - if 'MYPROJECT' in line: newline = line.replace('MYPROJECT', format(model.config.get_project_name().upper())) elif 'myproject' in line: @@ -273,7 +272,6 @@ def write_defines(self, model): fout = open(f'{model.config.get_output_dir()}/firmware/defines.h', 'w') for line in f.readlines(): - # Insert numbers if '// hls-fpga-machine-learning insert numbers' in line: newline = line @@ -318,7 +316,6 @@ def write_parameters(self, model): fout = open(f'{model.config.get_output_dir()}/firmware/parameters.h', 'w') for line in f.readlines(): - if '// hls-fpga-machine-learning insert includes' in line: newline = line for include in sorted(set(sum((layer.get_attr('include_header', []) for layer in model.get_layers()), []))): @@ -503,7 +500,6 @@ def write_bridge(self, model): indent = ' ' for line in f.readlines(): - if 'MYPROJECT' in line: newline = line.replace('MYPROJECT', format(model.config.get_project_name().upper())) elif 'myproject' in line: diff --git a/test/pytest/test_batchnorm.py b/test/pytest/test_batchnorm.py index 1556550a3c..c0ef0705ae 100644 --- a/test/pytest/test_batchnorm.py +++ b/test/pytest/test_batchnorm.py @@ -32,7 +32,6 @@ def model(request): @pytest.mark.parametrize('backend', ['Vivado', 'Vitis', 'Quartus']) @pytest.mark.parametrize('model', [True, False], indirect=True) def test_batchnorm(model, data, backend, io_type): - default_precision = 'ac_fixed<32, 1, true>' if backend == 'Quartus' else 'ac_fixed<32, 1>' center = model.layers[0].center diff --git a/test/pytest/test_causalpadding.py b/test/pytest/test_causalpadding.py index a9936dd054..c076c99987 100644 --- a/test/pytest/test_causalpadding.py +++ b/test/pytest/test_causalpadding.py @@ -15,7 +15,6 @@ @pytest.mark.parametrize('io_type', ['io_stream', 'io_parallel']) @pytest.mark.parametrize('backend', ['Vivado', 'Vitis', 'Quartus']) def test_causalpadding(io_type, backend): - model = Sequential() model.add(Conv1D(1, 5, padding="causal", input_shape=(100, 1))) model.compile() diff --git a/test/pytest/test_globalpooling.py b/test/pytest/test_globalpooling.py index ebbdb24195..c402a53cdf 100644 --- a/test/pytest/test_globalpooling.py +++ b/test/pytest/test_globalpooling.py @@ -51,7 +51,6 @@ def keras_model_1d(request): ) @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) def test_global_pool1d(backend, keras_model_1d, data_1d, io_type): - model, model_type, keepdims = keras_model_1d config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name') @@ -107,7 +106,6 @@ def keras_model_2d(request): ) @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) def test_global_pool2d(backend, keras_model_2d, data_2d, io_type): - model, model_type, keepdims = keras_model_2d config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name') diff --git a/test/pytest/test_pooling.py b/test/pytest/test_pooling.py index 5a2a737dfe..1f958696d8 100644 --- a/test/pytest/test_pooling.py +++ b/test/pytest/test_pooling.py @@ -51,7 +51,6 @@ def keras_model_1d(request): ) @pytest.mark.parametrize('io_type', ['io_parallel']) def test_pool1d(backend, keras_model_1d, data_1d, io_type): - model, model_type, padding = keras_model_1d config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name') @@ -107,7 +106,6 @@ def keras_model_2d(request): ) @pytest.mark.parametrize('io_type', ['io_parallel']) def test_pool2d(backend, keras_model_2d, data_2d, io_type): - model, model_type, padding = keras_model_2d config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,9>', granularity='name')