Skip to content
Snippets Groups Projects
Commit f7915ed7 authored by auphelia's avatar auphelia
Browse files

[fpgadataflow] Newly formatted files

parent ea16f33b
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ class HLSCustomOp(CustomOp):
def __init__(self):
super().__init__()
# template for single node execution
self.docompute_template= """
self.docompute_template = """
#include "cnpy.h"
#include <vector>
#include "bnn-library.h"
......@@ -20,19 +20,19 @@ class HLSCustomOp(CustomOp):
int main(){
$STREAMDECLARATIONS$
$READNPYDATA$
$DOCOMPUTE$
$DATAOUTSTREAM$
$SAVEASCNPY$
}
"""
@abstractmethod
def get_attributes(self, node):
pass
......@@ -40,7 +40,7 @@ class HLSCustomOp(CustomOp):
@abstractmethod
def code_generation(self, node):
pass
@abstractmethod
def global_includes(self, node, code_gen_dict):
pass
......@@ -60,7 +60,7 @@ class HLSCustomOp(CustomOp):
@abstractmethod
def docompute(self, node, code_gen_dict):
pass
@abstractmethod
def dataoutstrm(self, node, code_gen_dict):
pass
......@@ -68,4 +68,3 @@ class HLSCustomOp(CustomOp):
@abstractmethod
def save_as_npy(self, node, code_gen_dict):
pass
import numpy as np
import onnx.helper as helper
import sys
import os
import subprocess
from finn.core.datatype import DataType
import numpy as np
from finn.core.utils import get_by_name
from finn.custom_op.fpgadataflow import HLSCustomOp
......@@ -25,7 +23,9 @@ class StreamingMaxPool(HLSCustomOp):
in_ind += 1
self.code_generation(node)
temp_files.append("execute_StreamingMaxPool.cpp")
bash_compile = """g++ -o execute_StreamingMaxPool execute_StreamingMaxPool.cpp /workspace/finn/cnpy/cnpy.cpp -I/workspace/finn/cnpy/ -I/workspace/finn/finn-hlslib -I/workspace/vivado-hlslib --std=c++11 -lz"""
bash_compile = """g++ -o execute_StreamingMaxPool execute_StreamingMaxPool.cpp
/workspace/finn/cnpy/cnpy.cpp -I/workspace/finn/cnpy/
-I/workspace/finn/finn-hlslib -I/workspace/vivado-hlslib --std=c++11 -lz"""
process_compile = subprocess.Popen(bash_compile.split(), stdout=subprocess.PIPE)
process_compile.communicate()
bash_execute = "./execute_StreamingMaxPool"
......@@ -34,18 +34,18 @@ class StreamingMaxPool(HLSCustomOp):
temp_files.append("execute_StreamingMaxPool")
temp_files.append("output.npy")
output = np.load("output.npy")
context[node.output[0]]=output
context[node.output[0]] = output
# deleting temporary files
for temp_file in temp_files:
os.remove(temp_file)
def get_attributes(self, node):
self.ImgDim = get_by_name(node.attribute, 'ImgDim').i
self.PoolDim = get_by_name(node.attribute, 'PoolDim').i
self.NumChannels = get_by_name(node.attribute, 'NumChannels').i
self.ImgDim = get_by_name(node.attribute, "ImgDim").i
self.PoolDim = get_by_name(node.attribute, "PoolDim").i
self.NumChannels = get_by_name(node.attribute, "NumChannels").i
def code_generation(self, node):
code_gen_dict={}
code_gen_dict = {}
self.get_attributes(node)
self.global_includes(node, code_gen_dict)
self.defines(node, code_gen_dict)
......@@ -54,27 +54,30 @@ class StreamingMaxPool(HLSCustomOp):
self.docompute(node, code_gen_dict)
self.dataoutstrm(node, code_gen_dict)
self.save_as_npy(node, code_gen_dict)
template = self.docompute_template
for key in code_gen_dict:
#transform list into long string separated by '\n'
# transform list into long string separated by '\n'
code_gen_line = "\n".join(code_gen_dict[key])
template = template.replace(key, code_gen_line)
f = open("execute_StreamingMaxPool.cpp", "w")
f.write(template)
f.close()
def global_includes(self, node, code_gen_dict):
code_gen_dict["$GLOBALS$"]=['#include "maxpool.h"']
code_gen_dict["$GLOBALS$"] = ['#include "maxpool.h"']
def defines(self, node, code_gen_dict):
code_gen_dict["$DEFINES$"]=['#define ImgDim {}\n #define PoolDim {}\n #define NumChannels {}'.format(self.ImgDim, self.PoolDim, self.NumChannels)]
code_gen_dict["$DEFINES$"] = [
"#define ImgDim {}\n #define PoolDim {}\n #define NumChannels {}".format(
self.ImgDim, self.PoolDim, self.NumChannels
)
]
def read_npy_data(self, node, code_gen_dict):
code_gen_dict["$READNPYDATA$"]=[]
code_gen_dict["$READNPYDATA$"] = []
input_ind = 0
input_file_names = []
for inputs in node.input:
......@@ -83,39 +86,88 @@ class StreamingMaxPool(HLSCustomOp):
input_ind = 0
for input_file in input_file_names:
code_gen_dict["$READNPYDATA$"].append('cnpy::NpyArray arr = cnpy::npy_load("{}");\n float* loaded_data{} = arr.data<float>();'.format(input_file, input_ind))
code_gen_dict["$READNPYDATA$"].append('int num_values = 1; \n for(int i = 0; i < arr.shape.size(); i++){\n num_values *= arr.shape[i]; \n }')
code_gen_dict["$READNPYDATA$"].append('ap_uint<{}> dat;'.format(self.NumChannels))
code_gen_dict["$READNPYDATA$"].append('for(int i=0; i < num_values/{}; i++){{'.format(self.NumChannels))
code_gen_dict["$READNPYDATA$"].append(
"""cnpy::NpyArray arr = cnpy::npy_load("{}");\n
float* loaded_data{} = arr.data<float>();""".format(
input_file, input_ind
)
)
code_gen_dict["$READNPYDATA$"].append(
""""int num_values = 1; \n
for(int i = 0; i < arr.shape.size(); i++){\n
num_values *= arr.shape[i]; \n }"""
)
code_gen_dict["$READNPYDATA$"].append(
"ap_uint<{}> dat;".format(self.NumChannels)
)
code_gen_dict["$READNPYDATA$"].append(
"for(int i=0; i < num_values/{}; i++){{".format(self.NumChannels)
)
for channel in range(self.NumChannels):
code_gen_dict["$READNPYDATA$"].append('dat.range({},{}) = loaded_data{}[i+((num_values/{})*{})];'.format(channel, channel, input_ind, self.NumChannels, channel))
code_gen_dict["$READNPYDATA$"].append('in{} << dat;'.format(input_ind))
code_gen_dict["$READNPYDATA$"].append('}')
input_ind+=1
code_gen_dict["$READNPYDATA$"].append(
"dat.range({},{}) = loaded_data{}[i+((num_values/{})*{})];".format(
channel, channel, input_ind, self.NumChannels, channel
)
)
code_gen_dict["$READNPYDATA$"].append("in{} << dat;".format(input_ind))
code_gen_dict["$READNPYDATA$"].append("}")
input_ind += 1
def strm_decl(self, node, code_gen_dict):
code_gen_dict["$STREAMDECLARATIONS$"]=[]
code_gen_dict["$STREAMDECLARATIONS$"] = []
input_ind = 0
for inputs in node.input:
code_gen_dict["$STREAMDECLARATIONS$"].append('hls::stream<ap_uint<{}>> in{} ("in{}");'.format(self.NumChannels, input_ind, input_ind))
code_gen_dict["$STREAMDECLARATIONS$"].append(
'hls::stream<ap_uint<{}>> in{} ("in{}");'.format(
self.NumChannels, input_ind, input_ind
)
)
input_ind += 1
code_gen_dict["$STREAMDECLARATIONS$"].append('hls::stream<ap_uint<{}>> out ("out");'.format(self.NumChannels))
code_gen_dict["$STREAMDECLARATIONS$"].append(
'hls::stream<ap_uint<{}>> out ("out");'.format(self.NumChannels)
)
def docompute(self, node, code_gen_dict):
code_gen_dict["$DOCOMPUTE$"] = ['{}<ImgDim, PoolDim, NumChannels>(in0, out);'.format(node.op_type)]
code_gen_dict["$DOCOMPUTE$"] = [
"{}<ImgDim, PoolDim, NumChannels>(in0, out);".format(node.op_type)
]
def dataoutstrm(self, node, code_gen_dict):
code_gen_dict["$DATAOUTSTREAM$"]=['ap_uint<{}> out_data;\n std::vector<ap_uint<{}>> out_data_vector;'.format(self.NumChannels, self.NumChannels)]
code_gen_dict["$DATAOUTSTREAM$"].append('while(out.read_nb(out_data)){')
code_gen_dict["$DATAOUTSTREAM$"].append('out_data_vector.push_back(out_data);\n}')
code_gen_dict["$DATAOUTSTREAM$"].append('std::vector<float> output_data_vector;')
code_gen_dict["$DATAOUTSTREAM$"].append('for(std::vector<ap_uint<{}>>::iterator it = out_data_vector.begin(); it != out_data_vector.end(); ++it){{'.format(self.NumChannels))
code_gen_dict["$DATAOUTSTREAM$"].append('ap_uint<{}> output_data = *it;'.format(self.NumChannels))
code_gen_dict["$DATAOUTSTREAM$"] = [
"ap_uint<{}> out_data;\n std::vector<ap_uint<{}>> out_data_vector;".format(
self.NumChannels, self.NumChannels
)
]
code_gen_dict["$DATAOUTSTREAM$"].append("while(out.read_nb(out_data)){")
code_gen_dict["$DATAOUTSTREAM$"].append(
"out_data_vector.push_back(out_data);\n}"
)
code_gen_dict["$DATAOUTSTREAM$"].append(
"std::vector<float> output_data_vector;"
)
code_gen_dict["$DATAOUTSTREAM$"].append(
"""for(std::vector<ap_uint<{}>>::iterator it = out_data_vector.begin();
it != out_data_vector.end(); ++it){{""".format(
self.NumChannels
)
)
code_gen_dict["$DATAOUTSTREAM$"].append(
"ap_uint<{}> output_data = *it;".format(self.NumChannels)
)
for channel in range(self.NumChannels):
code_gen_dict["$DATAOUTSTREAM$"].append('output_data_vector.push_back(output_data.range({},{}));'.format(channel, channel))
code_gen_dict["$DATAOUTSTREAM$"].append('}')
code_gen_dict["$DATAOUTSTREAM$"].append(
"output_data_vector.push_back(output_data.range({},{}));".format(
channel, channel
)
)
code_gen_dict["$DATAOUTSTREAM$"].append("}")
def save_as_npy(self, node, code_gen_dict):
code_gen_dict["$SAVEASCNPY$"]=['cnpy::npy_save("output.npy",&output_data_vector[0],{{{},{},{}}},"w");'.format(self.NumChannels, int(self.ImgDim/self.PoolDim), int(self.ImgDim/self.PoolDim))]
code_gen_dict["$SAVEASCNPY$"] = [
"""cnpy::npy_save("output.npy",&output_data_vector[0],
{{{},{},{}}},"w");""".format(
self.NumChannels,
int(self.ImgDim / self.PoolDim),
int(self.ImgDim / self.PoolDim),
)
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment