-
Notifications
You must be signed in to change notification settings - Fork 462
FIFO depth optimization #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3559053
FIFO depth optimization almost adapted, wrapper fifos have to be handled
nghielme 118fbe0
FIFO depth optimization adapted and tested with the new flow framework
nghielme 505a818
Added some improvements - from github PR discussion
nghielme 0f39da7
Copy flows to config
thesps f4b5c87
Added some missing modifications
nghielme 2f7a586
Improved from SE perspective
nghielme 57485ca
Merge branch 'master' of https://github.com/fastmachinelearning/hls4m…
thesps e9e9c14
Fix setup.py after merge from master
thesps e6bf861
`build(...)` function fixed in vivado_accelerator_backend.py
nghielme 86f72b9
Merge branch 'master' into fifo_depth_merge
nghielme 7da98cf
Merge remote-tracking branch 'upstream/master' into fifo_opt_flow
vloncar 62cf4bc
Update for the new flows ordering
vloncar 9014136
Follow up in VivadoAcc backend
vloncar d072262
Cleanups on FIFO depth optimization
thesps 290f2c8
Merge branch 'main' of https://github.com/fastmachinelearning/hls4ml …
thesps d0ec0f8
Use name instead of cppname
thesps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import json | ||
|
||
from pyDigitalWaveTools.vcd.parser import VcdParser | ||
|
||
from hls4ml.model.optimizer.optimizer import ConfigurableOptimizerPass, ModelOptimizerPass | ||
|
||
|
||
def populate_values(values, name, data, depth): | ||
values.append({'name': name, 'data': [], 'max': 0, 'depth': 0}) | ||
get_values = lambda x: int(x[1][1:], 2) | ||
values[-1]['data'] = [get_values(x) for x in data] | ||
values[-1]['max'] = max(values[-1]['data']) | ||
values[-1]['depth'] = int(depth[0][1][1:], 2) | ||
return values | ||
|
||
|
||
def set_big_fifos(vars_to_profile, profiling_fifo_depth): | ||
for k, v in vars_to_profile.items(): | ||
v.pragma = (v.pragma[0], profiling_fifo_depth) | ||
|
||
|
||
def get_vcd_data(model): | ||
model.write() | ||
model.build(reset=False, csim=True, synth=True, cosim=True, validation=False, export=False, vsynth=False, | ||
fifo_opt=True) | ||
|
||
with open( | ||
model.config.get_output_dir() + '/' + model.config.get_project_name() + '_prj' + '/solution1/sim/verilog/fifo_opt.vcd') as vcd_file: | ||
vcd = VcdParser() | ||
vcd.parse(vcd_file) | ||
data = vcd.scope.toJson() | ||
return data | ||
|
||
|
||
def generate_max_depth_file(model, maxs): | ||
with open(model.config.get_output_dir() + '/max_depth.json', 'w') as f: | ||
json.dump(maxs, f, indent=4) | ||
|
||
|
||
def set_fifo_depth(model, maxs): | ||
for k, v in model.output_vars.items(): | ||
filtered_max = [x['max'] for x in maxs if v.name in x['name']] | ||
if len(filtered_max) == 0: | ||
continue | ||
if len(filtered_max) > 1: | ||
print('WARNING! Check names of FIFOs') | ||
v.pragma = (v.pragma[0], filtered_max[0] + 1) | ||
|
||
|
||
class FifoDepthOptimization(ConfigurableOptimizerPass, ModelOptimizerPass): | ||
def __init__(self): | ||
self.values = [] | ||
|
||
def transform(self, model): | ||
# use `large_fifo_depth = 0` to keep the default fifo depth | ||
profiling_fifo_depth = getattr(self, 'profiling_fifo_depth', 100_000) | ||
|
||
# check axi-stream or io-stream, if not one the 2 exit | ||
if not (model.config.get_config_value('IOType') == 'io_stream'): | ||
raise Exception('To use this optimization you have to set `IOType` field to `io_stream` in the HLS config') | ||
|
||
# initialize all the fifos to `profiling_fifo_depth` so that they will be automatically implemented in BRAMs | ||
# and so they will be profiled | ||
if profiling_fifo_depth: | ||
vars_to_profile = {k: v for k, v in model.output_vars.items() if v != model.get_output_variables()[0] and | ||
v != model.get_input_variables()[0]} | ||
|
||
set_big_fifos(vars_to_profile, profiling_fifo_depth) | ||
|
||
data = get_vcd_data(model) | ||
|
||
if len(data['children']) == 0: | ||
print("FIFO depth optimization found no FIFOs implemented using BRAMs in the design, no optimization is possible. Consider increasing profiling_fifo_depth.") | ||
return False | ||
|
||
n_elem = len(data['children'][0]['children'][0]['children']) | ||
thesps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for i in range(n_elem): | ||
name = data['children'][0]['children'][0]['children'][i]['name'] | ||
data_p = data['children'][0]['children'][0]['children'][i]['children'][0]['data'] | ||
depth = data['children'][0]['children'][0]['children'][i]['children'][1]['data'] | ||
populate_values(self.values, name, data_p, depth) | ||
|
||
maxs = [{'name': i['name'], 'max': i['max'], 'depth': i['depth']} for i in self.values] | ||
|
||
generate_max_depth_file(model, maxs) | ||
|
||
set_fifo_depth(model, maxs) | ||
|
||
print('[hls4ml] - FIFO optimization completed') | ||
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
58 changes: 58 additions & 0 deletions
58
hls4ml/backends/vivado_accelerator/passes/fifo_depth_optimization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from hls4ml.backends.vivado.passes.fifo_depth_optimization import set_big_fifos, get_vcd_data, populate_values, \ | ||
generate_max_depth_file, set_fifo_depth | ||
from hls4ml.model.optimizer.optimizer import ConfigurableOptimizerPass, ModelOptimizerPass | ||
|
||
|
||
class FifoDepthOptimization(ConfigurableOptimizerPass, ModelOptimizerPass): | ||
def __init__(self): | ||
self.values = [] | ||
|
||
def transform(self, model): | ||
# use `large_fifo_depth = 0` to keep the default fifo depth | ||
profiling_fifo_depth = getattr(self, 'profiling_fifo_depth', 100_000) | ||
|
||
# check axi-stream or io-stream, if not one the 2 exit | ||
if not(model.config.get_config_value('IOType') == 'io_stream' or | ||
model.config.get_config_value('AcceleratorConfig')['Interface'] == 'axi_stream' or | ||
model.config.get_config_value('AcceleratorConfig')['Interface'] == 'axi_master'): | ||
raise Exception('To use this optimization you have to set `IOType` field to `io_stream` in the HLS config ' | ||
'or `axi_stream` or `axi_master` in `AcceleratorConfig` interface field') | ||
|
||
# initialize all the fifos to 10000 so that they will be automatically implemented in BRAMs and so they will be | ||
# profiled | ||
|
||
if profiling_fifo_depth: | ||
set_big_fifos(model.output_vars, profiling_fifo_depth) | ||
|
||
data = get_vcd_data(model) | ||
|
||
for i in range(1, len(data['children'][0]['children'][0]['children'])): | ||
# wrapper fifos | ||
populate_values(self.values, | ||
data['children'][0]['children'][0]['children'][i]['name'], | ||
data['children'][0]['children'][0]['children'][i]['children'][0]['data'], | ||
data['children'][0]['children'][0]['children'][i]['children'][1]['data']) | ||
|
||
n_elem = len(data['children'][0]['children'][0]['children'][0]['children']) | ||
for i in range(n_elem): | ||
name = data['children'][0]['children'][0]['children'][0]['children'][i]['name'] | ||
data_p = data['children'][0]['children'][0]['children'][0]['children'][i]['children'][0]['data'] | ||
depth = data['children'][0]['children'][0]['children'][0]['children'][i]['children'][1]['data'] | ||
populate_values(self.values, name, data_p, depth) | ||
|
||
maxs = [{'name': i['name'], 'max': i['max'], 'depth': i['depth']} for i in self.values] | ||
|
||
generate_max_depth_file(model, maxs) | ||
|
||
set_fifo_depth(model, maxs) | ||
|
||
inp = model.get_input_variables()[0] | ||
out = model.get_output_variables()[0] | ||
for x in maxs: | ||
if 'in_local' in x['name']: | ||
inp.pragma = (inp.pragma[0], x['max'] + 1) | ||
elif 'out_local' in x['name']: | ||
out.pragma = (out.pragma[0], x['max'] + 1) | ||
|
||
print('[hls4ml] - FIFO optimization completed') | ||
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.