-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexpy.py
More file actions
executable file
·146 lines (122 loc) · 5.27 KB
/
flexpy.py
File metadata and controls
executable file
·146 lines (122 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
"""Flexpy - FPGA Logic from EXpressions
Copyright 2025 - Mirko Mariotti - https://www.mirkomariotti.it
Usage:
flexpy -e <expression> -o <outputfile> (--basm | --hls) [-d] [--config-file <config>] [-r <registersize>] [-t <type>] [--build-app] [--app-file <appfile>] [--emit-bmapi-maps] [--bmapi-maps-file <bmapi-maps-file>] [--io-mode <iomode>] [--neuron-statistics <neuron-statistics>] [--devices <devices>]
flexpy -e <expression> --iomap-only --basm [-d] [--config-file <config>]
flexpy -h | --help
Options:
-d Debug mode.
-h --help Show this screen.
-e <expression> The expression to convert.
-o <outputfile> The output file.
-r <registersize>, --register-size <registersize> The size of the registers, only needed if the data type is variable size.
-t <type>, --data-type <type> The type of the numbers, if not specified it is set to float32.
--basm Convert the expression to BASM.
--hls Convert the expression to HLS.
--config-file <config> The JSON configuration file to use.
--build-app Build the application
--app-file <appfile> The application file to generate.
--emit-bmapi-maps Emit the bmapi maps.
--bmapi-maps-file <bmapi-maps-file> The file where to save the bmapi maps.
--io-mode <iomode> The iomode to use [default: sync].
--iomap-only Generate only the iomap file.
--neuron-statistics <neuron-statistics> Save the neuron statistics to a file.
--devices <devices> Comma separated list of devices to target.
"""
from docopt import docopt
import sympy as sp
import numpy as np
import json
import dill
from flexpyengine import flexpyEngine
from jinja2 import Environment, DictLoader
from files_cpynqapi import cpynqapi
def main():
arguments = docopt(__doc__, version='Flexpy 0.0')
# Create the expression
exprFile = arguments["-e"]
# Read the content of the file and read it
f = open(exprFile, "r")
expr = f.read()
f.close()
deviceList = []
deviceExpr = []
devices = arguments["--devices"]
if devices and devices!="none":
deviceList = devices.split(",")
# Load the configuration file by executing the code
localParams = {'spExpr': None, 'testRanges': None}
globalParams = {'sp': sp, 'np': np}
for device in deviceList:
exec(device+"=sp.Function('"+device+"')", globalParams, localParams)
exec(expr, globalParams, localParams)
spExpr = localParams['spExpr']
testRanges = localParams['testRanges']
for device in deviceList:
deviceExpr.append(localParams[device])
if spExpr is None:
print("Error: The expression is not valid")
return
config = None
# Eventually load the JSON configuration file
if arguments["--config-file"]:
configFile = arguments["--config-file"]
with open(configFile) as json_file:
config = json.load(json_file)
debugMode = False
if arguments["-d"]:
debugMode = True
neuronStatistics = None
if arguments["--neuron-statistics"]:
neuronStatistics = arguments["--neuron-statistics"]
eng=flexpyEngine(config, spExpr, regsize=arguments["--register-size"], type=arguments["--data-type"], debug=debugMode, neuronStatistics=neuronStatistics, deviceExpr=deviceExpr)
if arguments["--io-mode"] == "async":
eng.basm += "%meta bmdef global iomode: async\n"
else:
eng.basm += "%meta bmdef global iomode: sync\n"
if arguments["--basm"]:
outbasm=eng.to_basm()
if not arguments["--iomap-only"]:
# Save to a file the basm code
with open(arguments["-o"], "w") as text_file:
text_file.write(eng.basm)
else:
print(eng.inputs)
print(eng.outputs)
if arguments["--neuron-statistics"]:
# Save the neuron statistics to a JSON file
with open(arguments["--neuron-statistics"], "w") as json_file:
json.dump(eng.neurons, json_file, indent=4)
if arguments["--build-app"]:
if arguments["--app-file"]:
items = [{"bminputs": str(len(eng.inputs)), "bmoutputs": str(len(eng.outputs))}]
# Save to a file the app code
with open(arguments["--app-file"], "w") as appFile:
env = Environment(loader=DictLoader({'cpynqapi': cpynqapi}))
template = env.get_template('cpynqapi')
appFile.write(template.render(items=items))
else:
print("Error: The app file is not specified")
return
if arguments["--emit-bmapi-maps"]:
if arguments["--bmapi-maps-file"]:
# Save to a file the bmapi maps
with open(arguments["--bmapi-maps-file"], "w") as bmapiFile:
assoc={}
for i in range(len(eng.inputs)):
assoc["i"+str(i)]=str(i)
for i in range(len(eng.outputs)):
assoc["o"+str(i)]=str(i)
mapFile= {"Assoc": assoc}
json.dump(mapFile, bmapiFile)
else:
print("Error: The bmapi maps file is not specified")
return
elif arguments["--hls"]:
outhls=eng.to_hls()
# Save to a file the hls code
with open(arguments["-o"], "w") as text_file:
text_file.write(eng.hls)
if __name__ == '__main__':
main()