Skip to content

Commit

Permalink
Merge pull request fastmachinelearning#781 from vloncar/top_pipeline
Browse files Browse the repository at this point in the history
Decouple pipeline style from strategy
  • Loading branch information
jmitrevs authored May 14, 2023
2 parents 618064f + 744f78b commit e89e6fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
6 changes: 3 additions & 3 deletions hls4ml/backends/vivado/vivado_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ def build(
return parse_vivado_report(model.config.get_output_dir())

def _validate_conv_strategy(self, layer):
if layer.model.config.model_strategy.lower() != 'resource':
print(f'WARNING: Cannot use "Latency" model strategy for {layer.name} layer. Switching to "Resource" strategy.')
layer.model.config.model_strategy = 'Resource'
if layer.model.config.pipeline_style.lower() != 'dataflow':
print(f'WARNING: Layer {layer.name} requires "dataflow" pipeline style. Switching to "dataflow" pipeline style.')
layer.model.config.pipeline_style = 'dataflow'

@layer_optimizer(Layer)
def init_base_layer(self, layer):
Expand Down
44 changes: 25 additions & 19 deletions hls4ml/model/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def __init__(self, config):

self.trace_output = self.get_config_value('TraceOutput', False)

self.pipeline_style = 'pipeline'

self._parse_hls_config()
self._validate_hls_config()

Expand Down Expand Up @@ -212,6 +214,7 @@ def _parse_hls_config(self):
self.model_conv_implementation = model_cfg.get('ConvImplementation', 'LineBuffer')
self.model_strategy = model_cfg.get('Strategy', 'Latency')
self.model_compression = bool(model_cfg.get('Compression', 0))
self.pipeline_style = model_cfg.get('PipelineStyle', 'pipeline')

layer_type_cfg = hls_config.get('LayerType')
if layer_type_cfg is not None:
Expand Down Expand Up @@ -274,45 +277,48 @@ def _parse_hls_config(self):
self.layer_name_compression[layer_name.lower()] = bool(compression)

def _validate_hls_config(self):
use_resource = False
if self.model_strategy.lower() == 'latency' and self.model_compression:
print('WARNING: Compression enabled while model strategy set to "Latency".')
use_resource = True
use_dataflow = False
if self.pipeline_style.lower() == 'pipeline' and self.model_compression:
print('WARNING: Compression enabled while pipeline style set to "pipeline".')
use_dataflow = True
for layer_type, strategy in self.layer_type_strategy.items():
if strategy.lower() == 'resource' and self.model_strategy.lower() == 'latency':
if strategy.lower() == 'resource' and self.pipeline_style.lower() == 'pipeline':
print(
'WARNING: Strategy for layer type {} set to "Resource", while model strategy set to "Latency".'.format(
'WARNING: Strategy for layer type {} set to "Resource", while pipeline style set to "pipeline".'.format(
layer_type
)
)
use_resource = True
use_dataflow = True

for layer_name, strategy in self.layer_name_strategy.items():
if strategy.lower() == 'resource' and self.model_strategy.lower() == 'latency':
if strategy.lower() == 'resource' and self.pipeline_style.lower() == 'pipeline':
print(
'WARNING: Strategy for layer {} set to "Resource", while model strategy set to "Latency".'.format(
'WARNING: Strategy for layer {} set to "Resource", while pipeline style set to "pipeline".'.format(
layer_name
)
)
use_resource = True
use_dataflow = True

for layer_type, compression in self.layer_type_compression.items():
if compression and self.model_strategy.lower() == 'latency':
if compression and self.pipeline_style.lower() == 'pipeline':
print(
'WARNING: Compression enabled for layer type {}, while model strategy set to "Latency".'.format(
'WARNING: Compression enabled for layer type {}, while pipeline style set to "pipeline".'.format(
layer_type
)
)
use_resource = True
use_dataflow = True

for layer_name, compression in self.layer_name_compression.items():
if compression and self.model_strategy.lower() == 'latency':
print(f'WARNING: Compression enabled for layer {layer_name}, while model strategy set to "Latency".')
use_resource = True
if compression and self.pipeline_style.lower() == 'pipeline':
print(f'WARNING: Compression enabled for layer {layer_name}, while pipeline style set to "pipeline".')
use_dataflow = True

if self.model_strategy.lower() == 'resource':
use_dataflow = True

if use_resource:
print('WARNING: Changing model strategy to "Resource"')
self.model_strategy = 'Resource'
if use_dataflow:
print('WARNING: Changing pipeline style to "dataflow".')
self.pipeline_style = 'dataflow'


class ModelGraph:
Expand Down
2 changes: 1 addition & 1 deletion hls4ml/writer/vivado_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def write_project_cpp(self, model):
newline += indent + '#pragma HLS INTERFACE ap_vld port={},{} \n'.format(
','.join(all_inputs), ','.join(all_outputs)
)
if model.config.model_strategy.lower() == 'resource':
if model.config.pipeline_style.lower() == 'dataflow':
newline += indent + '#pragma HLS DATAFLOW \n'
else:
newline += indent + '#pragma HLS PIPELINE \n'
Expand Down

0 comments on commit e89e6fb

Please sign in to comment.