From 79fa6fd4e4e88cce5005c98bdb88dca844873a22 Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <yamanu@xilinx.com>
Date: Thu, 23 Jan 2020 18:10:04 +0000
Subject: [PATCH] [Util] provide own util fxn to create tmp dirs, mount on host

---
 run-docker.sh                                            | 6 ++++++
 src/finn/core/utils.py                                   | 9 +++++++--
 src/finn/transformation/fpgadataflow/codegen_ipgen.py    | 5 ++---
 src/finn/transformation/fpgadataflow/codegen_ipstitch.py | 5 ++---
 src/finn/transformation/fpgadataflow/codegen_npysim.py   | 5 ++---
 src/finn/transformation/fpgadataflow/make_pynq_proj.py   | 5 ++---
 tests/fpgadataflow/test_npy2hls.py                       | 3 +--
 7 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/run-docker.sh b/run-docker.sh
index 5fc4c2eda..7dcabb20c 100755
--- a/run-docker.sh
+++ b/run-docker.sh
@@ -30,6 +30,7 @@ CNPY_LOCAL=$SCRIPTPATH/cnpy
 FINN_HLS_LOCAL=$SCRIPTPATH/finn-hlslib
 PYVERILATOR_LOCAL=$SCRIPTPATH/pyverilator
 PYNQSHELL_LOCAL=$SCRIPTPATH/PYNQ-HelloWorld
+BUILD_LOCAL=/tmp/finn
 VIVADO_HLS_LOCAL=$VIVADO_PATH
 
 # clone dependency repos
@@ -40,6 +41,9 @@ git clone $FINN_HLS_REPO $FINN_HLS_LOCAL; git -C "$FINN_HLS_LOCAL" checkout b5dc
 git clone $PYVERILATOR_REPO $PYVERILATOR_LOCAL ||  git -C "$PYVERILATOR_LOCAL" pull
 git clone $PYNQSHELL_REPO $PYNQSHELL_LOCAL ||  git -C "$PYNQSHELL_LOCAL" pull
 
+# ensure build dir exists locally
+mkdir -p $BUILD_LOCAL
+
 echo "Mounting $SCRIPTPATH into /workspace/finn"
 echo "Mounting $SCRIPTPATH/brevitas into /workspace/brevitas"
 echo "Mounting $SCRIPTPATH/brevitas_cnv_lfc into /workspace/brevitas_cnv_lfc"
@@ -47,6 +51,7 @@ echo "Mounting $SCRIPTPATH/cnpy into /workspace/cnpy"
 echo "Mounting $SCRIPTPATH/finn-hlslib into /workspace/finn-hlslib"
 echo "Mounting $SCRIPTPATH/pyverilator into /workspace/pyverilator"
 echo "Mounting $SCRIPTPATH/PYNQ-HelloWorld into /workspace/PYNQ-HelloWorld"
+echo "Mounting $BUILD_LOCAL into $BUILD_LOCAL"
 echo "Mounting $VIVADO_PATH into $VIVADO_PATH"
 
 if [ "$1" = "test" ]; then
@@ -78,6 +83,7 @@ docker run --rm --name finn_dev -it \
 -v $SCRIPTPATH/finn-hlslib:/workspace/finn-hlslib \
 -v $SCRIPTPATH/pyverilator:/workspace/pyverilator \
 -v $SCRIPTPATH/PYNQ-HelloWorld:/workspace/PYNQ-HelloWorld \
+-v $BUILD_LOCAL:$BUILD_LOCAL \
 -v $VIVADO_PATH:$VIVADO_PATH \
 -e VIVADO_PATH=$VIVADO_PATH \
 -p 8888:8888 -p 8081:8081 \
diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py
index 7f85b2445..7f39c6475 100644
--- a/src/finn/core/utils.py
+++ b/src/finn/core/utils.py
@@ -2,13 +2,19 @@ import random
 import string
 import subprocess
 import os
-
 import numpy as np
 import onnx
+import tempfile
 from bitstring import BitArray
 
 from finn.core.datatype import DataType
 
+def make_build_dir(prefix=""):
+    """Creates a temporary folder with given prefix to be used as a build dir.
+    Use this function instead of tempfile.mkdtemp to ensure any generated files
+    will survive on the host after the FINN Docker container exits."""
+
+    return tempfile.mkdtemp(prefix="finn/"+prefix)
 
 def valueinfo_to_tensor(vi):
     """Creates an all-zeroes numpy tensor from a ValueInfoProto."""
@@ -276,4 +282,3 @@ class IPGenBuilder:
         bash_command = ["bash", self.ipgen_script]
         process_compile = subprocess.Popen(bash_command, stdout=subprocess.PIPE)
         process_compile.communicate()
-        
diff --git a/src/finn/transformation/fpgadataflow/codegen_ipgen.py b/src/finn/transformation/fpgadataflow/codegen_ipgen.py
index 0159873a3..13686f846 100644
--- a/src/finn/transformation/fpgadataflow/codegen_ipgen.py
+++ b/src/finn/transformation/fpgadataflow/codegen_ipgen.py
@@ -1,8 +1,7 @@
 import os
-import tempfile as tmp
 
 import finn.custom_op.registry as registry
-from finn.core.utils import get_by_name
+from finn.core.utils import get_by_name, make_build_dir
 from finn.transformation import Transformation
 
 
@@ -17,7 +16,7 @@ def _codegen_single_node(node, model, fpgapart, clk):
         code_gen_dir = inst.get_nodeattr("code_gen_dir_ipgen")
         # ensure that there is a directory
         if code_gen_dir == "" or not os.path.isdir(code_gen_dir):
-            code_gen_dir = tmp.mkdtemp(
+            code_gen_dir = make_build_dir(
                 prefix="code_gen_ipgen_" + str(node.op_type) + "_"
             )
             inst.set_nodeattr("code_gen_dir_ipgen", code_gen_dir)
diff --git a/src/finn/transformation/fpgadataflow/codegen_ipstitch.py b/src/finn/transformation/fpgadataflow/codegen_ipstitch.py
index 138556ce8..e4c303f84 100644
--- a/src/finn/transformation/fpgadataflow/codegen_ipstitch.py
+++ b/src/finn/transformation/fpgadataflow/codegen_ipstitch.py
@@ -1,8 +1,7 @@
 import os
 import subprocess
-import tempfile as tmp
 
-from finn.core.utils import get_by_name
+from finn.core.utils import get_by_name, make_build_dir
 from finn.transformation import Transformation
 
 
@@ -90,7 +89,7 @@ class CodeGen_ipstitch(Transformation):
                 )
 
         # create a temporary folder for the project
-        vivado_stitch_proj_dir = tmp.mkdtemp(prefix="vivado_stitch_proj_")
+        vivado_stitch_proj_dir = make_build_dir(prefix="vivado_stitch_proj_")
         model.set_metadata_prop("vivado_stitch_proj", vivado_stitch_proj_dir)
         # start building the tcl script
         tcl = []
diff --git a/src/finn/transformation/fpgadataflow/codegen_npysim.py b/src/finn/transformation/fpgadataflow/codegen_npysim.py
index 0d5a42dfe..51a7c5bb8 100644
--- a/src/finn/transformation/fpgadataflow/codegen_npysim.py
+++ b/src/finn/transformation/fpgadataflow/codegen_npysim.py
@@ -1,8 +1,7 @@
 import os
-import tempfile as tmp
 
 import finn.custom_op.registry as registry
-from finn.core.utils import get_by_name
+from finn.core.utils import get_by_name, make_build_dir
 from finn.transformation import Transformation
 
 
@@ -17,7 +16,7 @@ def _codegen_single_node(node, model):
         code_gen_dir = inst.get_nodeattr("code_gen_dir_npysim")
         # ensure that there is a directory
         if code_gen_dir == "" or not os.path.isdir(code_gen_dir):
-            code_gen_dir = tmp.mkdtemp(
+            code_gen_dir = make_build_dir(
                 prefix="code_gen_npysim_" + str(node.op_type) + "_"
             )
             inst.set_nodeattr("code_gen_dir_npysim", code_gen_dir)
diff --git a/src/finn/transformation/fpgadataflow/make_pynq_proj.py b/src/finn/transformation/fpgadataflow/make_pynq_proj.py
index 5358053a7..ac9ff67db 100644
--- a/src/finn/transformation/fpgadataflow/make_pynq_proj.py
+++ b/src/finn/transformation/fpgadataflow/make_pynq_proj.py
@@ -1,8 +1,7 @@
 import os
 import subprocess
-import tempfile as tmp
 
-from finn.core.utils import get_by_name
+from finn.core.utils import get_by_name, make_build_dir
 from finn.transformation import Transformation
 
 
@@ -58,7 +57,7 @@ class MakePYNQProject(Transformation):
         nrst_name = "ap_rst_n_0"
 
         # create a temporary folder for the project
-        vivado_pynq_proj_dir = tmp.mkdtemp(prefix="vivado_pynq_proj_")
+        vivado_pynq_proj_dir = make_build_dir(prefix="vivado_pynq_proj_")
         model.set_metadata_prop("vivado_pynq_proj", vivado_pynq_proj_dir)
 
         ip_config_tcl = """
diff --git a/tests/fpgadataflow/test_npy2hls.py b/tests/fpgadataflow/test_npy2hls.py
index 47b4eb410..775b486c0 100644
--- a/tests/fpgadataflow/test_npy2hls.py
+++ b/tests/fpgadataflow/test_npy2hls.py
@@ -1,6 +1,5 @@
 import shutil
 import subprocess
-import tempfile as tmp
 
 import numpy as np
 
@@ -10,7 +9,7 @@ from finn.core.datatype import DataType
 
 
 def make_npy2apintstream_testcase(ndarray, dtype):
-    test_dir = tmp.mkdtemp(prefix="test_npy2apintstream_")
+    test_dir = make_build_dir(prefix="test_npy2apintstream_")
     shape = ndarray.shape
     elem_bits = dtype.bitwidth()
     packed_bits = shape[-1] * elem_bits
-- 
GitLab