Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Open
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
103 changes: 48 additions & 55 deletions src/finn/util/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,81 +88,98 @@ def get_rtlsim_trace_depth():
"""

try:
return int(os.environ["RTLSIM_TRACE_DEPTH"])
except KeyError:
return int(os.getenv("RTLSIM_TRACE_DEPTH", default="1"))
except ValueError:
warnings.warn(
"Failed to convert the value of RTLSIM_TRACE_DEPTH"
" variable to integer. Using default value '1' instead."
)
return 1


def get_remote_vivado():
"""Return the address of the remote Vivado synthesis server as set by the,
REMOTE_VIVADO environment variable, otherwise return None"""

try:
return os.environ["REMOTE_VIVADO"]
except KeyError:
return None
return os.getenv("REMOTE_VIVADO", default=None)


def get_num_default_workers():
"""Return the number of workers for parallel transformations. Controllable
via the NUM_DEFAULT_WORKERS environment variable. If the env.var. is
undefined, the default value of 1 is returned.
"""

try:
return int(os.environ["NUM_DEFAULT_WORKERS"])
except KeyError:
return int(os.getenv("NUM_DEFAULT_WORKERS", default="1"))
except ValueError:
warnings.warn(
"Failed to convert the value of NUM_DEFAULT_WORKERS"
" variable to integer. Using default value '1' instead."
)
return 1


def get_finn_root():
"Return the root directory that FINN is cloned into."

try:
return os.environ["FINN_ROOT"]
except KeyError:
finn_root = os.getenv("FINN_ROOT", None)

if not finn_root:
raise Exception(
"""Environment variable FINN_ROOT must be set
correctly. Please ensure you have launched the Docker contaier correctly.
correctly. Please ensure you have launched the Docker container correctly.
"""
)

return finn_root


def get_execution_error_thresh():
"Return the max error that is allowed for rounding in FINN execution."

try:
return float(os.environ["ERROR_THRESH"])
except KeyError:
return float(os.getenv("ERROR_THRESH", default="1e-2"))
except ValueError:
warnings.warn(
"Failed to convert the value of ERROR_THRESH"
" variable to float. Using default value '1e-2' instead."
)
return 1e-2


def get_sanitize_quant_tensors():
"""Return whether tensors with quantization annotations should be sanitized.
Enabled by default, disabling will yield faster ONNX execution but may give
incorrect results. Use with caution."""

try:
return int(os.environ["SANITIZE_QUANT_TENSORS"])
except KeyError:
# enabled by default
return int(os.getenv("SANITIZE_QUANT_TENSORS", default="1"))
except ValueError:
warnings.warn(
"Failed to convert the value of SANITIZE_QUANT_TENSORS"
" variable to integer. Using default value '1' instead."
)
return 1


def make_build_dir(prefix=""):
"""Creates a 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."""
try:
tmpdir = tempfile.mkdtemp(prefix=prefix)
newdir = tmpdir.replace("/tmp", os.environ["FINN_BUILD_DIR"])
os.makedirs(newdir)
return newdir
except KeyError:

finn_build_dir = os.getenv("FINN_BUILD_DIR", None)
if finn_build_dir is None:
raise Exception(
"""Environment variable FINN_BUILD_DIR must be set
correctly. Please ensure you have launched the Docker contaier correctly.
"""
)

tmpdir = tempfile.mkdtemp(prefix=prefix)
newdir = tmpdir.replace("/tmp", os.environ["FINN_BUILD_DIR"])
os.makedirs(newdir)
return newdir


def get_by_name(container, name, name_field="name"):
"""Return item from container by .name field if it exists, None otherwise.
Expand All @@ -173,11 +190,8 @@ def get_by_name(container, name, name_field="name"):
inds = [i for i, e in enumerate(names) if e == name]
if len(inds) > 1:
raise Exception("Found multiple get_by_name matches, undefined behavior")
elif len(inds) == 0:
return None
else:
ind = inds[0]
return container[ind]

return None if len(inds) == 0 else container[inds[0]]


def remove_by_name(container, name, name_field="name"):
Expand All @@ -187,10 +201,10 @@ def remove_by_name(container, name, name_field="name"):
container.remove(item)


def random_string(stringLength=6):
def random_string(string_length=6):
"""Randomly generate a string of letters and digits."""
lettersAndDigits = string.ascii_letters + string.digits
return "".join(random.choice(lettersAndDigits) for i in range(stringLength))
letters_and_digits = string.ascii_letters + string.digits
return "".join(random.choice(letters_and_digits) for i in range(string_length))


def interleave_matrix_outer_dim_from_partitions(matrix, n_partitions):
Expand Down Expand Up @@ -457,8 +471,8 @@ def launch_process_helper(args, proc_env=None, cwd=None):
"""Helper function to launch a process in a way that facilitates logging
stdout/stderr with Python loggers.
Returns (cmd_out, cmd_err)."""
if proc_env is None:
proc_env = os.environ.copy()
proc_env = proc_env or os.environ.copy()

with subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=proc_env, cwd=cwd
) as proc:
Expand All @@ -470,24 +484,3 @@ def launch_process_helper(args, proc_env=None, cwd=None):
cmd_err = cmd_err.decode("utf-8")
sys.stderr.write(cmd_err)
return (cmd_out, cmd_err)


def which(program):
"Python equivalent of the shell cmd 'which'."

# source:
# https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file

return None
3 changes: 1 addition & 2 deletions src/finn/util/hls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

import os
import subprocess

from finn.util.basic import which
from shutil import which


class CallHLS:
Expand Down
3 changes: 2 additions & 1 deletion src/finn/util/vivado.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
from shutil import which

from finn.util.basic import launch_process_helper, which
from finn.util.basic import launch_process_helper


def out_of_context_synth(
Expand Down