|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | +from hls4ml.backends import FPGABackend |
| 6 | +from hls4ml.model.attributes import ChoiceAttribute |
| 7 | +from hls4ml.model.flow import register_flow |
| 8 | +from hls4ml.model.layers import Dense, Layer |
| 9 | +from hls4ml.model.optimizer import layer_optimizer |
| 10 | +from hls4ml.report import parse_libero_report |
| 11 | + |
| 12 | + |
| 13 | +class LiberoBackend(FPGABackend): |
| 14 | + def __init__(self): |
| 15 | + super().__init__(name='Libero') |
| 16 | + self._register_layer_attributes() |
| 17 | + self._register_flows() |
| 18 | + |
| 19 | + def _register_layer_attributes(self): |
| 20 | + strategy_layers = [ |
| 21 | + Dense, |
| 22 | + ] |
| 23 | + |
| 24 | + for layer in strategy_layers: |
| 25 | + attrs = self.attribute_map.get(layer, []) |
| 26 | + attrs.append( |
| 27 | + ChoiceAttribute( |
| 28 | + 'strategy', |
| 29 | + choices=['Latency', 'Resource'], |
| 30 | + default='Latency', |
| 31 | + ) |
| 32 | + ) |
| 33 | + self.attribute_map[layer] = attrs |
| 34 | + |
| 35 | + def _register_flows(self): |
| 36 | + initializers = self._get_layer_initializers() |
| 37 | + init_flow = register_flow('init_layers', initializers, requires=['optimize'], backend=self.name) |
| 38 | + |
| 39 | + libero_types = [ |
| 40 | + 'libero:transform_types', |
| 41 | + 'libero:set_pipeline_style', |
| 42 | + ] |
| 43 | + libero_types_flow = register_flow('specific_types', libero_types, requires=[init_flow], backend=self.name) |
| 44 | + |
| 45 | + template_flow = register_flow('apply_templates', self._get_layer_templates, requires=[init_flow], backend=self.name) |
| 46 | + |
| 47 | + writer_passes = ['make_stamp', 'libero:write_hls'] |
| 48 | + self._writer_flow = register_flow('write', writer_passes, requires=['libero:ip'], backend=self.name) |
| 49 | + |
| 50 | + ip_flow_requirements = [ |
| 51 | + 'optimize', |
| 52 | + init_flow, |
| 53 | + libero_types_flow, |
| 54 | + template_flow, |
| 55 | + ] |
| 56 | + |
| 57 | + self._default_flow = register_flow('ip', None, requires=ip_flow_requirements, backend=self.name) |
| 58 | + |
| 59 | + def get_default_flow(self): |
| 60 | + return self._default_flow |
| 61 | + |
| 62 | + def get_writer_flow(self): |
| 63 | + return self._writer_flow |
| 64 | + |
| 65 | + def create_initial_config( |
| 66 | + self, |
| 67 | + fpga_family='PolarFire', |
| 68 | + part='MPF300', |
| 69 | + board='hw_only', |
| 70 | + clock_period=5, |
| 71 | + clock_uncertainty='27%', |
| 72 | + io_type='io_parallel', |
| 73 | + namespace=None, |
| 74 | + write_weights_txt=True, |
| 75 | + write_tar=False, |
| 76 | + **_, |
| 77 | + ): |
| 78 | + """Create initial configuration of the Libero backend. |
| 79 | +
|
| 80 | + Args: |
| 81 | + part (str, optional): The FPGA part to be used. Defaults to 'MPF300'. |
| 82 | + clock_period (int, optional): The clock period. Defaults to 5. |
| 83 | + clock_uncertainty (str, optional): The clock uncertainty. Defaults to 27%. |
| 84 | + io_type (str, optional): Type of implementation used. One of |
| 85 | + 'io_parallel' or 'io_stream'. Defaults to 'io_parallel'. |
| 86 | + namespace (str, optional): If defined, place all generated code within a namespace. Defaults to None. |
| 87 | + write_weights_txt (bool, optional): If True, writes weights to .txt files which speeds up compilation. |
| 88 | + Defaults to True. |
| 89 | + write_tar (bool, optional): If True, compresses the output directory into a .tar.gz file. Defaults to False. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + dict: initial configuration. |
| 93 | + """ |
| 94 | + config = {} |
| 95 | + |
| 96 | + config['FPGAFamily'] = fpga_family if fpga_family is not None else 'PolarFire' |
| 97 | + config['Part'] = part if part is not None else 'MPF300' |
| 98 | + config['Board'] = board if board is not None else 'hw_only' |
| 99 | + config['ClockPeriod'] = clock_period if clock_period is not None else 5 |
| 100 | + config['IOType'] = io_type if io_type is not None else 'io_parallel' |
| 101 | + config['HLSConfig'] = {} |
| 102 | + config['WriterConfig'] = { |
| 103 | + 'Namespace': namespace, |
| 104 | + 'WriteWeightsTxt': write_weights_txt, |
| 105 | + 'WriteTar': write_tar, |
| 106 | + } |
| 107 | + |
| 108 | + return config |
| 109 | + |
| 110 | + def build( |
| 111 | + self, |
| 112 | + model, |
| 113 | + reset=False, |
| 114 | + skip_preqs=False, |
| 115 | + sw_compile=True, |
| 116 | + hw=True, |
| 117 | + cosim=False, |
| 118 | + rtl_synth=False, |
| 119 | + fpga=False, |
| 120 | + **kwargs, |
| 121 | + ): |
| 122 | + """Build the model using Libero suite and SmartHLS compiler. Additional arguments passed to the function in form of |
| 123 | + `<arg>=True` will be passed as an argument to the `shls` command. See SmartHLS user guide for list of possible |
| 124 | + command line options. |
| 125 | +
|
| 126 | + Args: |
| 127 | + model (ModelGraph): Model to build |
| 128 | + reset (bool, optional): Clean up any existing files. Defaults to False. |
| 129 | + skip_preqs(bool, optional): Skip any prerequisite step that is outdated. Defaults to False. |
| 130 | + sw_compile (bool, optional): Compile the generated HLS in software. Defaults to True. |
| 131 | + hw (bool, optional): Compile the software to hardware, producing a set of Verilog HDL files. Defaults to True. |
| 132 | + cosim (bool, optional): Run co-simulation. Defaults to False. |
| 133 | + rtl_synth (bool, optional): Run RTL synthesis for resource results. This will take less time than `fpga`. |
| 134 | + Defaults to False. |
| 135 | + fpga (bool, optional): Synthesize the generated hardware to target FPGA. This runs RTL synthesis and |
| 136 | + place-and-route for resource and timing results. Defaults to False. |
| 137 | +
|
| 138 | + Raises: |
| 139 | + Exception: Raised if the `shls` command has not been found |
| 140 | + CalledProcessError: Raised if SmartHLS returns non-zero code for any of the commands executed |
| 141 | +
|
| 142 | + Returns: |
| 143 | + dict: Detailed report produced by SmartHLS. |
| 144 | + """ |
| 145 | + if 'linux' in sys.platform: |
| 146 | + found = os.system('command -v shls > /dev/null') |
| 147 | + if found != 0: |
| 148 | + raise Exception('Libero/SmartHLS installation not found. Make sure "shls" is on PATH.') |
| 149 | + |
| 150 | + def run_shls_cmd(cmd_name): |
| 151 | + subprocess.run( |
| 152 | + ['shls', '-s', cmd_name], |
| 153 | + shell=False, |
| 154 | + check=True, |
| 155 | + stdout=sys.stdout, |
| 156 | + stderr=sys.stderr, |
| 157 | + cwd=model.config.get_output_dir(), |
| 158 | + ) |
| 159 | + |
| 160 | + if reset: |
| 161 | + run_shls_cmd('clean') |
| 162 | + if sw_compile: |
| 163 | + run_shls_cmd('sw_compile') |
| 164 | + if hw: |
| 165 | + run_shls_cmd('hw') |
| 166 | + if cosim: |
| 167 | + run_shls_cmd('cosim') |
| 168 | + if rtl_synth: |
| 169 | + run_shls_cmd('rtl_synth') |
| 170 | + if fpga: |
| 171 | + run_shls_cmd('fpga') |
| 172 | + |
| 173 | + for arg_name, arg_val in kwargs.items(): |
| 174 | + if arg_val: |
| 175 | + run_shls_cmd(arg_name) |
| 176 | + |
| 177 | + return parse_libero_report(model.config.get_output_dir()) |
| 178 | + |
| 179 | + @layer_optimizer(Layer) |
| 180 | + def init_base_layer(self, layer): |
| 181 | + reuse_factor = layer.model.config.get_reuse_factor(layer) |
| 182 | + layer.set_attr('reuse_factor', reuse_factor) |
| 183 | + |
| 184 | + @layer_optimizer(Dense) |
| 185 | + def init_dense(self, layer): |
| 186 | + if layer.model.config.is_resource_strategy(layer): |
| 187 | + n_in, n_out = self.get_layer_mult_size(layer) |
| 188 | + self.set_target_reuse_factor(layer) |
| 189 | + self.set_closest_reuse_factor(layer, n_in, n_out) |
| 190 | + layer.set_attr('strategy', 'resource') |
| 191 | + else: |
| 192 | + layer.set_attr('strategy', 'latency') |
0 commit comments