Skip to content
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

Hardcore weight txt path #1089

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions hls4ml/model/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,32 +763,24 @@ def predict(self, x):
n_inputs = len(self.get_input_variables())
n_outputs = len(self.get_output_variables())

curr_dir = os.getcwd()
os.chdir(self.config.get_output_dir() + '/firmware')

output = []
if n_samples == 1 and n_inputs == 1:
x = [x]

try:
for i in range(n_samples):
predictions = [np.zeros(yj.size(), dtype=ctype) for yj in self.get_output_variables()]
if n_inputs == 1:
inp = [np.asarray(x[i])]
else:
inp = [np.asarray(xj[i]) for xj in x]
argtuple = inp
argtuple += predictions
argtuple = tuple(argtuple)
top_function(*argtuple)
output.append(predictions)

# Convert to list of numpy arrays (one for each output)
output = [
np.asarray([output[i_sample][i_output] for i_sample in range(n_samples)]) for i_output in range(n_outputs)
]
finally:
os.chdir(curr_dir)
for i in range(n_samples):
predictions = [np.zeros(yj.size(), dtype=ctype) for yj in self.get_output_variables()]
if n_inputs == 1:
inp = [np.asarray(x[i])]
else:
inp = [np.asarray(xj[i]) for xj in x]
argtuple = inp
argtuple += predictions
argtuple = tuple(argtuple)
top_function(*argtuple)
output.append(predictions)

# Convert to list of numpy arrays (one for each output)
output = [np.asarray([output[i_sample][i_output] for i_sample in range(n_samples)]) for i_output in range(n_outputs)]

if n_samples == 1 and n_outputs == 1:
return output[0][0]
Expand Down
2 changes: 1 addition & 1 deletion hls4ml/templates/catapult/myproject_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <algorithm>
#include <map>

static std::string s_weights_dir = "weights";
// hls-fpga-machine-learning insert weights dir

const char *get_weights_dir() { return s_weights_dir.c_str(); }

Expand Down
3 changes: 2 additions & 1 deletion hls4ml/templates/vivado/build_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ LDFLAGS=
INCFLAGS="-Ifirmware/ap_types/"
PROJECT=myproject
LIB_STAMP=mystamp
WEIGHTS_DIR="\"weights\""
BASEDIR="$(cd "$(dirname "$0")" && pwd)"
WEIGHTS_DIR="\"${BASEDIR}/firmware/weights\""

${CC} ${CFLAGS} ${INCFLAGS} -D WEIGHTS_DIR=${WEIGHTS_DIR} -c firmware/${PROJECT}.cpp -o ${PROJECT}.o
${CC} ${CFLAGS} ${INCFLAGS} -D WEIGHTS_DIR=${WEIGHTS_DIR} -c ${PROJECT}_bridge.cpp -o ${PROJECT}_bridge.o
Expand Down
3 changes: 3 additions & 0 deletions hls4ml/writer/catapult_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,9 @@ def write_bridge(self, model):
newline = line.replace('MYPROJECT', format(model.config.get_project_name().upper()))
elif 'myproject' in line:
newline = line.replace('myproject', format(model.config.get_project_name()))
elif '// hls-fpga-machine-learning insert weights dir' in line:
weights_dir = (Path(fout.name).parent / 'firmware/weights').resolve()
newline = f'static std::string s_weights_dir = "{weights_dir}";\n'
elif '// hls-fpga-machine-learning insert bram' in line:
newline = line
for bram in model_brams:
Expand Down
Loading