Skip to content

Commit

Permalink
Changes to run-docker and build_dataflow to enable YAML based configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bwintermann committed Jan 31, 2025
1 parent 76a8927 commit 125d00a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
16 changes: 13 additions & 3 deletions run-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,24 @@ elif [ "$1" = "build_dataflow" ]; then
FINN_DOCKER_EXTRA+="-v $BUILD_DATAFLOW_DIR:$BUILD_DATAFLOW_DIR "
#FINN_HOST_BUILD_DIR=$BUILD_DATAFLOW_DIR/build
gecho "Running build_dataflow for folder $BUILD_DATAFLOW_DIR"
DOCKER_CMD="build_dataflow $BUILD_DATAFLOW_DIR"
DOCKER_CMD="build_dataflow --dir $BUILD_DATAFLOW_DIR"
elif [ "$1" = "build_custom" ]; then
BUILD_CUSTOM_DIR=$(readlink -f "$2")
FLOW_NAME=${3:-build}
FINN_DOCKER_EXTRA+="-v $BUILD_CUSTOM_DIR:$BUILD_CUSTOM_DIR -w $BUILD_CUSTOM_DIR "
#FINN_HOST_BUILD_DIR=$BUILD_DATAFLOW_DIR/build
gecho "Running build_custom: $BUILD_CUSTOM_DIR/$FLOW_NAME.py"
DOCKER_CMD="python -mpdb -cc -cq $FLOW_NAME.py ${@:4}"
if [[ "$FLOW_NAME" == "build" ]]; then
if [[ -e "$FLOW_NAME.py "]]; then
DOCKER_CMD="build_custom $FLOW_NAME.py"
gecho "Running build_custom: $BUILD_CUSTOM_DIR/$FLOW_NAME.py"
elif [[ -e "$FLOW_NAME.yaml" ]]; then
DOCKER_CMD="build_custom $FLOW_NAME.yaml"
gecho "Running build_custom: $BUILD_CUSTOM_DIR/$FLOW_NAME.yaml"
elif [[ -e "$FLOW_NAME.yml" ]]; then
DOCKER_CMD="build_custom $FLOW_NAME.yml"
gecho "Running build_custom: $BUILD_CUSTOM_DIR/$FLOW_NAME.yml"
fi
fi
elif [ -z "$1" ]; then
gecho "Running container only"
DOCKER_CMD="bash"
Expand Down
34 changes: 33 additions & 1 deletion src/finn/builder/build_dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@
import logging
import os
import pdb # NOQA
import subprocess
import sys
import time
import traceback
from pathlib import Path
from qonnx.core.modelwrapper import ModelWrapper

from finn.builder.build_dataflow_config import (
DataflowBuildConfig,
default_build_dataflow_steps,
)
from finn.builder.build_dataflow_steps import build_dataflow_step_lookup
from finn.builder.yaml_to_cfg import run_finn_from_yaml


# adapted from https://stackoverflow.com/a/39215961
Expand Down Expand Up @@ -218,10 +221,39 @@ def build_dataflow_directory(path_to_cfg_dir: str):
return ret


def build(target: str, dir=False) -> None:
"""Start the FINN flow
:param target: The build target. Either a directory if using build_directory or
a yaml or py file
:param dir: Whether or not to use a directory with a build.py file"""
t = Path(target)
if dir and not t.is_dir():
print(f"Trying to start FINN with a build directory but {t} is not a directory")
sys.exit(1)
if not dir and not t.is_file():
print(f"Trying to start FINN with a non existing build file: {t}")
sys.exit(1)
if dir:
build_dataflow_directory(str(t.absolute()))
else:
if target.endswith(".yml") or target.endswith(".yaml"):
run_finn_from_yaml(target)
elif target.endswith(".py"):
subprocess.run(
f"python -mpdb -cc -cq {target}", stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
else:
print(f"Unknown file ending for buildfile: {target}")
print("Please specify either a YAML file or a python script")
sys.exit(1)


def main():
"""Entry point for dataflow builds. Invokes `build_dataflow_directory` using
command line arguments"""
clize.run(build_dataflow_directory)
clize.run(build)


if __name__ == "__main__":
Expand Down
11 changes: 6 additions & 5 deletions src/finn/builder/yaml_to_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def process_steps(
return cfg


def buildcfg_from_yaml(p: Path) -> DataflowBuildConfig | None:
def buildcfg_from_yaml(p: Path) -> tuple[DataflowBuildConfig, Path] | None:
"""Convert a YAML build file to a dataflow build config as well as possible. If the
given YAML file does not exist or is incorrect, this returns None"""
if not p.exists():
Expand All @@ -90,7 +90,7 @@ def buildcfg_from_yaml(p: Path) -> DataflowBuildConfig | None:
print('Missing "general" section in your build file')
return None
general = data["general"]
for key in ["output_dir", "synth_clk_period_ns", "generate_outputs"]:
for key in ["model", "output_dir", "synth_clk_period_ns", "generate_outputs"]:
if key not in general.keys():
print(f"Missing key {key} in your build file under the general section")
return None
Expand Down Expand Up @@ -158,10 +158,11 @@ def buildcfg_from_yaml(p: Path) -> DataflowBuildConfig | None:
# If the format is module_name.step_name, module_name is automatically imported
cfg = process_steps(p, data, cfg, "steps")
cfg = process_steps(p, data, cfg, "verify_steps")
return cfg
return cfg, general["model"]
return None


def run_finn_from_yaml(buildfile: str, modelfile: str) -> None:
def run_finn_from_yaml(buildfile: str) -> None:
"""Entrypoint for the FINN flow when using a YAML build file"""
build_dataflow.build_dataflow_cfg(modelfile, buildcfg_from_yaml(Path(buildfile)))
cfg, model = buildcfg_from_yaml(Path(buildfile))
build_dataflow.build_dataflow_cfg(str(model), cfg)

0 comments on commit 125d00a

Please sign in to comment.