diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py index 13cf2e77c94fb803b04ba71adffadc9fc4c46237..e036c91348510e8708d86bc5b5070fb421cae6f9 100644 --- a/src/finn/core/utils.py +++ b/src/finn/core/utils.py @@ -194,8 +194,8 @@ def gen_finn_dt_tensor(finn_dt, tensor_shape): # always use float type as container return tensor_values.astype(np.float32) -class CallCppCompiler(): - + +class CallCppCompiler: def __init__(self): self.include_paths = [] self.cpp_files = [] @@ -212,11 +212,13 @@ class CallCppCompiler(): raise ValueError( """There is no generated code to compile for node of op type {}""".format( - node.op_type - ) + node.op_type ) + ) else: - self.cpp_files.append(str(self.code_gen_dir) + "/execute_" + str(node.op_type) + ".cpp") + self.cpp_files.append( + str(self.code_gen_dir) + "/execute_" + str(node.op_type) + ".cpp" + ) for lib in self.include_paths: if "cnpy" in lib: self.cpp_files.append("/workspace/cnpy/cnpy.cpp") @@ -227,15 +229,19 @@ class CallCppCompiler(): raise ValueError( """There is no generated code to compile for node of op type {}""".format( - node.op_type - ) + node.op_type ) + ) else: - self.executable_path = str(self.code_gen_dir) + "/execute_" + str(node.op_type) + self.executable_path = ( + str(self.code_gen_dir) + "/execute_" + str(node.op_type) + ) def build(self, node): # raise error if includes are empty - self.code_gen_dir = (get_by_name(node.attribute, "code_gen_dir")).s.decode("UTF-8") + self.code_gen_dir = (get_by_name(node.attribute, "code_gen_dir")).s.decode( + "UTF-8" + ) self.prepare_cpp_files(node) self.set_executable_path(node) self.compile_components.append("g++ -o " + str(self.executable_path)) @@ -249,10 +255,8 @@ class CallCppCompiler(): for component in self.compile_components: bash_compile += str(component) + " " - self.compile_script = str(self.code_gen_dir)+"/compile.sh" - f = open(self.compile_script,"w") + self.compile_script = str(self.code_gen_dir) + "/compile.sh" + f = open(self.compile_script, "w") f.write("#!/bin/sh \n") f.write(bash_compile) f.close() - - diff --git a/src/finn/transformation/fpgadataflow/code_gen_transformation.py b/src/finn/transformation/fpgadataflow/code_gen_transformation.py index 437870dba1f2b20174ccc9a04b43689ce6c1c235..313112690e773fd060b465d2a2cb1d87cd7801a0 100644 --- a/src/finn/transformation/fpgadataflow/code_gen_transformation.py +++ b/src/finn/transformation/fpgadataflow/code_gen_transformation.py @@ -2,8 +2,8 @@ import os import tempfile as tmp import finn.custom_op.registry as registry -from finn.transformation import Transformation from finn.core.utils import get_by_name +from finn.transformation import Transformation def code_gen_transformation(node, model): @@ -67,7 +67,7 @@ class CodeGen(Transformation): for node in model.graph.node: if node.domain == "finn": backend_attribute = get_by_name(node.attribute, "backend") - backend_value = backend_attribute.s.decode('UTF-8') + backend_value = backend_attribute.s.decode("UTF-8") if backend_value == "fpgadataflow": code_gen_transformation(node, model) return (model, False) diff --git a/src/finn/transformation/fpgadataflow/compilation_transformation.py b/src/finn/transformation/fpgadataflow/compilation_transformation.py index 9649985b162d146787e98e3a0a43152d38a05e7c..d06af8682a81ed951c6f9e3554bf63dd1c3e457a 100644 --- a/src/finn/transformation/fpgadataflow/compilation_transformation.py +++ b/src/finn/transformation/fpgadataflow/compilation_transformation.py @@ -1,12 +1,13 @@ import subprocess import finn.core.utils as util -from finn.transformation import Transformation from finn.core.utils import CallCppCompiler +from finn.transformation import Transformation class Compilation(Transformation): """Compilation for all nodes in model""" + def __init__(self): super().__init__() self.compiler_call = CallCppCompiler() @@ -19,14 +20,11 @@ class Compilation(Transformation): self.compiler_call.append_includes("-I/workspace/vivado-hlslib") self.compiler_call.append_includes("--std=c++11") - def prepare_bash_command(self, node): self.get_includes() self.compiler_call.build(node) bash_command = "chmod +x " + str(self.compiler_call.compile_script) - process_compile = subprocess.Popen( - bash_command.split(), stdout=subprocess.PIPE - ) + process_compile = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE) process_compile.communicate() print(self.compiler_call.code_gen_dir) @@ -35,7 +33,7 @@ class Compilation(Transformation): for node in model.graph.node: if node.domain == "finn": backend_attribute = util.get_by_name(node.attribute, "backend") - backend_value = backend_attribute.s.decode('UTF-8') + backend_value = backend_attribute.s.decode("UTF-8") if backend_value == "fpgadataflow": self.prepare_bash_command(node) bash_command = self.compiler_call.compile_script @@ -44,6 +42,8 @@ class Compilation(Transformation): ) process_compile.communicate() - model.set_attribute(node, "executable_path", self.compiler_call.executable_path) + model.set_attribute( + node, "executable_path", self.compiler_call.executable_path + ) return (model, False) diff --git a/tests/test_set_attribute.py b/tests/test_set_attribute.py index fe67d18bc2ab332fa878052be6a229eb48a1b593..43688f212c612959291938e44d34f6badedb6396 100644 --- a/tests/test_set_attribute.py +++ b/tests/test_set_attribute.py @@ -1,12 +1,10 @@ -import numpy as np from onnx import TensorProto, helper -import finn.core.onnx_exec as oxe -import finn.custom_op.xnorpopcount as xp from finn.core.datatype import DataType from finn.core.modelwrapper import ModelWrapper from finn.core.utils import get_by_name + def test_set_attribute(): mw = 8 mh = 8 @@ -20,7 +18,7 @@ def test_set_attribute(): inp = helper.make_tensor_value_info("inp", TensorProto.FLOAT, [1, sf, simd]) outp = helper.make_tensor_value_info("outp", TensorProto.FLOAT, [1, nf, pe]) node_inp_list = ["inp", "weights"] - + FCLayer_node = helper.make_node( "StreamingFCLayer_Batch", node_inp_list, @@ -50,7 +48,7 @@ def test_set_attribute(): value_to_set = "fpgadataflow" model.set_attribute(FCLayer_node, "backend", value_to_set) value = get_by_name(FCLayer_node.attribute, "backend") - assert value.s.decode('UTF-8') == value_to_set + assert value.s.decode("UTF-8") == value_to_set value_to_set = mw model.set_attribute(FCLayer_node, "MW", value_to_set) @@ -60,8 +58,4 @@ def test_set_attribute(): value_to_set = idt.name model.set_attribute(FCLayer_node, "inputDataType", value_to_set) value = get_by_name(FCLayer_node.attribute, "inputDataType") - assert value.s.decode('UTF-8') == value_to_set - - - - + assert value.s.decode("UTF-8") == value_to_set