diff --git a/.gitignore b/.gitignore index 225fb5cfa3df45124797da425df14974308b90c2..126321cf4deccaa01ab0f2025460e53519d4c06f 100644 --- a/.gitignore +++ b/.gitignore @@ -96,3 +96,6 @@ MANIFEST # generated files as part of end2end notebooks /notebooks/end2end_example/**/*.onnx + +# downloaded dep repos +/deps/ diff --git a/README.md b/README.md index 331c405e8230695e97f701b8682e6f4d07727670..4cc995fc8c991ccc851e95fd30897aeea8ca266a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ <img align="left" src="https://raw.githubusercontent.com/Xilinx/finn/github-pages/docs/img/finn-stack.png" alt="drawing" style="margin-right: 20px" width="250"/> -[](https://gitter.im/xilinx-finn/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +[](https://github.com/Xilinx/finn/discussions) [](http://finn.readthedocs.io/) FINN is an experimental framework from Xilinx Research Labs to explore deep neural network diff --git a/docker/Dockerfile.finn b/docker/Dockerfile.finn index 54b6c353fdda23a9156be5e523093474de2bf731..71f41acbb618a8cde9bb8ab07cb2cf5a3be90544 100644 --- a/docker/Dockerfile.finn +++ b/docker/Dockerfile.finn @@ -39,24 +39,29 @@ WORKDIR /workspace ENV TZ="Europe/Dublin" RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -RUN apt-get update -RUN apt-get -y upgrade -RUN apt-get install -y build-essential -RUN apt-get install -y libglib2.0-0 -RUN apt-get install -y libsm6 -RUN apt-get install -y libxext6 -RUN apt-get install -y libxrender-dev -RUN apt-get install -y verilator -RUN apt-get install -y nano -RUN apt-get install -y zsh -RUN apt-get install -y rsync -RUN apt-get install -y git -RUN apt-get install -y sshpass -RUN apt-get install -y wget -RUN apt-get install -y sudo -RUN apt-get install -y unzip -RUN apt-get install -y zip +RUN apt-get update && \ + apt-get install -y \ + build-essential \ + libc6-dev-i386 \ + libglib2.0-0 \ + libsm6 \ + libxext6 \ + libxrender-dev \ + verilator \ + nano \ + zsh \ + rsync \ + git \ + openssh-client \ + sshpass \ + wget \ + sudo \ + unzip \ + zip \ + locales \ + lsb-core RUN echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config +RUN locale-gen "en_US.UTF-8" # install XRT RUN wget https://www.xilinx.com/bin/public/openDownload?filename=$XRT_DEB_VERSION.deb -O /tmp/$XRT_DEB_VERSION.deb @@ -76,7 +81,7 @@ RUN pip install matplotlib==3.3.1 --ignore-installed RUN pip install pytest-dependency==0.5.1 RUN pip install sphinx==3.1.2 RUN pip install sphinx_rtd_theme==0.5.0 -RUN pip install pytest-xdist==2.0.0 +RUN pip install pytest-xdist[setproctitle]==2.4.0 RUN pip install pytest-parallel==0.1.0 RUN pip install "netron>=5.0.0" RUN pip install pandas==1.1.5 @@ -86,14 +91,14 @@ RUN pip install -e git+https://github.com/fbcotter/dataset_loading.git@0.0.4#egg # git-based Python repo dependencies # these are installed in editable mode for easier co-development -ARG FINN_BASE_COMMIT="7cd7e00ba6709a85073ba22beeb5827e684fe085" -ARG QONNX_COMMIT="76c165fe7656d9bb3b826e98ac452085f1544f54" +ARG FINN_BASE_COMMIT="585bccad29ba6416511256c732a2c1da21d00bdf" +ARG QONNX_COMMIT="9f9eff95227cc57aadc6eafcbd44b7acda89f067" ARG FINN_EXP_COMMIT="af6102769226b82b639f243dc36f065340991513" ARG BREVITAS_COMMIT="a5b71d6de1389d3e7db898fef72e014842670f03" ARG PYVERILATOR_COMMIT="0c3eb9343500fc1352a02c020a736c8c2db47e8e" ARG CNPY_COMMIT="4e8810b1a8637695171ed346ce68f6984e585ef4" -ARG HLSLIB_COMMIT="bcca5d2b69c88e9ad7a86581ec062a9756966367" -ARG OMX_COMMIT="1dfc4aa2f2895632742cd5751520c6b472feb74e" +ARG HLSLIB_COMMIT="269410aa217389fc02e69bd7de210cd026f10971" +ARG OMX_COMMIT="a97f0bf145a2f7e57ca416ea76c9e45df4e9aa37" ARG AVNET_BDF_COMMIT="2d49cfc25766f07792c0b314489f21fe916b639b" # finn-base @@ -124,7 +129,7 @@ RUN git -C /workspace/cnpy checkout $CNPY_COMMIT RUN git clone https://github.com/Xilinx/finn-hlslib.git /workspace/finn-hlslib RUN git -C /workspace/finn-hlslib checkout $HLSLIB_COMMIT # oh-my-xilinx -RUN git clone https://bitbucket.org/maltanar/oh-my-xilinx.git /workspace/oh-my-xilinx +RUN git clone https://github.com/maltanar/oh-my-xilinx.git /workspace/oh-my-xilinx RUN git -C /workspace/oh-my-xilinx checkout $OMX_COMMIT # board files RUN cd /tmp; \ diff --git a/docker/finn_entrypoint.sh b/docker/finn_entrypoint.sh index a2312d025b616acd285b94f1b56b83f0c35cc0ae..788e6bf51b4c0748883be371f4dd77941ef2c99d 100644 --- a/docker/finn_entrypoint.sh +++ b/docker/finn_entrypoint.sh @@ -28,9 +28,12 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -export FINN_ROOT=/workspace/finn +export FINN_ROOT=/workspace export HOME=/tmp/home_dir export SHELL=/bin/bash +export LANG="en_US.UTF-8" +export LC_ALL="en_US.UTF-8" +export LANGUAGE="en_US:en" # colorful terminal output export PS1='\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] ' @@ -51,11 +54,11 @@ recho () { echo -e "${RED}ERROR: $1${NC}" } -if [ -f "$FINN_ROOT/setup.py" ];then +if [ -f "$FINN_ROOT/finn/setup.py" ];then # run pip install for finn - pip install --user -e $FINN_ROOT + pip install --user -e $FINN_ROOT/finn else - recho "Unable to find FINN source code in /workspace/finn" + recho "Unable to find FINN source code in $FINN_ROOT/finn" recho "Ensure you have passed -v <path-to-finn-repo>:/workspace/finn to the docker run command" exit -1 fi @@ -90,5 +93,16 @@ else fi fi +if [ -f "$HLS_PATH/settings64.sh" ];then + # source Vitis HLS env.vars + source $HLS_PATH/settings64.sh + gecho "Found Vitis HLS at $HLS_PATH" +else + yecho "Unable to find $HLS_PATH/settings64.sh" + yecho "Functionality dependent on Vitis HLS will not be available." + yecho "Please note that FINN needs at least version 2020.2 for Vitis HLS support." + yecho "If you need Vitis HLS, ensure HLS_PATH is set correctly and mounted into the Docker container." +fi + # execute the provided command(s) as root exec "$@" diff --git a/docker/jenkins/Jenkinsfile b/docker/jenkins/Jenkinsfile index f3211941890d634b12142ed13c0f0cf49a9003d8..dab0833166234fc8ec9f123adf8c6157acdf5d5d 100644 --- a/docker/jenkins/Jenkinsfile +++ b/docker/jenkins/Jenkinsfile @@ -1,108 +1,46 @@ -pipeline { - agent any - parameters { - string(name: 'FINN_CI_BRANCH', defaultValue: '', description: 'FINN branch to build') - string(name: 'FINN_XILINX_PATH', defaultValue: '', description: 'Path to Xilinx tool installation') - string(name: 'FINN_XILINX_VERSION', defaultValue: '2020.1', description: 'Xilinx tool version') - string(name: 'PYNQ_BOARD', defaultValue: 'Pynq-Z1', description: 'PYNQ board type') - string(name: 'PYNQ_IP', defaultValue: '', description: 'PYNQ board IP address') - string(name: 'PYNQ_USERNAME', defaultValue: 'xilinx', description: 'PYNQ board username') - string(name: 'PYNQ_PASSWORD', defaultValue: 'xilinx', description: 'PYNQ board password') - string(name: 'PYNQ_TARGET_DIR', defaultValue: '/home/xilinx/finn', description: 'PYNQ board target deployment directory') - string(name: 'NUM_DEFAULT_WORKERS', defaultValue: '1', description: 'Number of cores for parallel transformations') - // main test: everything except rtlsim and end2end tests, parallel run with xdist, no parallel transformations to save on memory - string(name: 'DOCKER_CMD_MAIN', defaultValue: """python setup.py test --addopts "-k 'not (rtlsim or end2end)' --dist=loadfile -n auto" """, description: 'Main test command') - // rtlsim tests: parallel run with pytest-parallel, no parallel transformations to save on memory - string(name: 'DOCKER_CMD_RTLSIM', defaultValue: """python setup.py test --addopts "-k rtlsim --workers auto" """, description: 'rtlsim test command') - // end2end tests: no parallel testing, use NUM_DEFAULT_WORKERS for parallel transformations - string(name: 'DOCKER_CMD_END2END', defaultValue: """python setup.py test --addopts "-k end2end" """, description: 'end2end test command') - // allow specifying where to mount the cloned folder from, since Jenkins and FINN may be running in separate containers - string(name: 'WORKSPACE_MOUNT', defaultValue: '/var/jenkins_home/workspace/finn', description: 'Path to Jenkins workspace mount') +node { + def app + stage('Clone repository') { + /* Let's make sure we have the repository cloned to our workspace */ + checkout scm } - environment { - DOCKER_TAG='finn_ci:$BUILD_ID' - DOCKER_INST_NAME='finn_ci' - BUILD_PATH='/tmp/finn_ci' - VIVADO_PATH=${params.FINN_XILINX_PATH}/Vivado/${params.FINN_XILINX_VERSION} - VITIS_PATH=${params.FINN_XILINX_PATH}/Vitis/${params.FINN_XILINX_VERSION} - } - stages { - stage("Clone") { - steps { - git branch: "${params.FINN_CI_BRANCH}", url: 'https://github.com/Xilinx/finn.git' - } - } - stage('Build') { - steps { - sh """ - docker build -t $DOCKER_TAG -f docker/Dockerfile.finn_ci \ - --build-arg BUILD_PATH=$BUILD_PATH \ - . - """ + withEnv([ + "FINN_XILINX_PATH=/proj/xbuilds/SWIP/2022.1_0420_0327/installs/lin64", + "FINN_XILINX_VERSION=2022.1", + "FINN_DOCKER_TAG=xilinx/finn:jenkins", + "FINN_HOST_BUILD_DIR=/scratch/users/finn_ci", + "PLATFORM_REPO_PATHS=/opt/xilinx/dsa" + ]){ + parallel firstBranch: { + stage('Brevitas export') { + dir("${env.WORKSPACE}") { + sh("bash run-docker.sh python setup.py test --addopts -mbrevitas_export") + } } - } - stage('test-main') { - steps { - catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { - sh """ - docker run --init \ - --hostname $DOCKER_INST_NAME \ - -v ${params.WORKSPACE_MOUNT}:/workspace/finn \ - -v ${params.FINN_XILINX_PATH}:${params.FINN_XILINX_PATH}:ro \ - -e NUM_DEFAULT_WORKERS=1 \ - -e FINN_INST_NAME=$DOCKER_INST_NAME \ - -e VIVADO_PATH=$VIVADO_PATH \ - -e VITIS_PATH=$VITIS_PATH \ - -e PYNQ_BOARD=${params.PYNQ_BOARD} \ - -e PYNQ_IP=${params.PYNQ_IP} \ - -e PYNQ_USERNAME=${params.PYNQ_USERNAME} \ - -e PYNQ_PASSWORD=${params.PYNQ_PASSWORD} \ - -e PYNQ_TARGET_DIR=${params.PYNQ_TARGET_DIR} \ - $DOCKER_TAG ${params.DOCKER_CMD_MAIN} - """} + }, secondBranch: { + stage('Streamlining transformations') { + dir("${env.WORKSPACE}") { + sh("bash run-docker.sh python setup.py test --addopts -mstreamline") + } + } + }, thirdBranch: { + stage('Util functions') { + dir("${env.WORKSPACE}") { + sh("bash run-docker.sh python setup.py test --addopts -mutil") + } } - } - stage('test-rtlsim') { - steps { - catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { - sh """ - docker run --init \ - --hostname $DOCKER_INST_NAME \ - -v ${params.WORKSPACE_MOUNT}:/workspace/finn \ - -v $VIVADO_PATH:$VIVADO_PATH:ro \ - -e NUM_DEFAULT_WORKERS=1 \ - -e FINN_INST_NAME=$DOCKER_INST_NAME \ - -e VIVADO_PATH=$VIVADO_PATH \ - -e VITIS_PATH=$VITIS_PATH \ - -e PYNQ_BOARD=${params.PYNQ_BOARD} \ - -e PYNQ_IP=${params.PYNQ_IP} \ - -e PYNQ_USERNAME=${params.PYNQ_USERNAME} \ - -e PYNQ_PASSWORD=${params.PYNQ_PASSWORD} \ - -e PYNQ_TARGET_DIR=${params.PYNQ_TARGET_DIR} \ - $DOCKER_TAG ${params.DOCKER_CMD_RTLSIM} - """} + }, fourthBranch: { + stage('General transformations') { + dir("${env.WORKSPACE}") { + sh("bash run-docker.sh python setup.py test --addopts -mtransform") + } } - } - stage('test-end2end') { - steps { - catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { - sh """ - docker run --init \ - --hostname $DOCKER_INST_NAME \ - -v ${params.WORKSPACE_MOUNT}:/workspace/finn \ - -v $VIVADO_PATH:$VIVADO_PATH:ro \ - -e NUM_DEFAULT_WORKERS=${params.NUM_DEFAULT_WORKERS} \ - -e FINN_INST_NAME=$DOCKER_INST_NAME \ - -e VIVADO_PATH=$VIVADO_PATH \ - -e VITIS_PATH=$VITIS_PATH \ - -e PYNQ_BOARD=${params.PYNQ_BOARD} \ - -e PYNQ_IP=${params.PYNQ_IP} \ - -e PYNQ_USERNAME=${params.PYNQ_USERNAME} \ - -e PYNQ_PASSWORD=${params.PYNQ_PASSWORD} \ - -e PYNQ_TARGET_DIR=${params.PYNQ_TARGET_DIR} \ - $DOCKER_TAG ${params.DOCKER_CMD_END2END} - """ } + }, fifthBranch: { + stage('Fpgadataflow transformations and simulations') { + dir("${env.WORKSPACE}") { + sh("bash run-docker.sh python setup.py test --addopts -mfpgadataflow") + } } - } + } } } diff --git a/docker/quicktest.sh b/docker/quicktest.sh index b4ad37232fa69754a86e9064d7592d7474e8617e..f625f2b1ef722f386180a8409a9eb9e759a2f3b6 100755 --- a/docker/quicktest.sh +++ b/docker/quicktest.sh @@ -2,7 +2,7 @@ : ${PYTEST_PARALLEL=auto} -cd $FINN_ROOT +cd $FINN_ROOT/finn # check if command line argument is empty or not present if [ -z $1 ]; then echo "Running quicktest: not (vivado or slow or board) with pytest-xdist" diff --git a/docs/finn/command_line.rst b/docs/finn/command_line.rst index ccb891a0ab42eebdd85f10c14384aaa217e8ed8b..54ffca9430a57ed4513ce822afbe0f1642b77404 100644 --- a/docs/finn/command_line.rst +++ b/docs/finn/command_line.rst @@ -186,20 +186,23 @@ This is possible by using the `build_custom` entry as follows: outside the FINN repo folder for cleaner separation. Let's call this folder ``custom_build_dir``. -2. Create a ``custom_build_dir/build.py`` file that will perform the build when -executed. You should also put any ONNX model(s) or other Python modules you -may want to include in your build flow in this folder (so that they get mounted -into the Docker container while building). Besides the filename and data placement, +2. Create one or more Python files under this directory that perform the build(s) +you would like when executed, for instance ``custom_build_dir/build.py`` and +``custom_build_dir/build_quick.py``. +You should also put any ONNX model(s) or other +Python modules you may want to include in your build flow in this folder (so that they get +mounted into the Docker container while building). Besides the data placement, you have complete freedom on how to implement the build flow here, including calling the steps from the simple dataflow build mode above, making calls to FINN library functions, preprocessing and altering models, building several variants etc. -You can find a basic example of build.py under ``src/finn/qnn-data/build_dataflow/build.py``. +You can find a basic example of a build flow under ``src/finn/qnn-data/build_dataflow/build.py``. -You can launch the custom build flow using: +You can launch the desired custom build flow using: :: - ./run-docker.sh build_custom <path/to/custom_build_dir/> + ./run-docker.sh build_custom <path/to/custom_build_dir> <name-of-build-flow> This will mount the specified folder into the FINN Docker container and launch -your ``build.py``. +the build flow. If ``<name-of-build-flow>`` is not specified it will default to ``build`` +and thus execute ``build.py``. If it is specified, it will be ``<name-of-build-flow>.py``. diff --git a/fetch-repos.sh b/fetch-repos.sh new file mode 100755 index 0000000000000000000000000000000000000000..50ca89e459b3d93c835049fe9e9c1b45571b5a52 --- /dev/null +++ b/fetch-repos.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# Copyright (c) 2020-2022, Advanced Micro Devices +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# * Neither the name of FINN nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +FINN_BASE_COMMIT="585bccad29ba6416511256c732a2c1da21d00bdf" +QONNX_COMMIT="9f9eff95227cc57aadc6eafcbd44b7acda89f067" +FINN_EXP_COMMIT="af6102769226b82b639f243dc36f065340991513" +BREVITAS_COMMIT="a5b71d6de1389d3e7db898fef72e014842670f03" +PYVERILATOR_COMMIT="0c3eb9343500fc1352a02c020a736c8c2db47e8e" +CNPY_COMMIT="4e8810b1a8637695171ed346ce68f6984e585ef4" +HLSLIB_COMMIT="c6cd928bc6f7e2e41c4d6a0376ad5c3ebe9d2d82" +OMX_COMMIT="a97f0bf145a2f7e57ca416ea76c9e45df4e9aa37" +AVNET_BDF_COMMIT="2d49cfc25766f07792c0b314489f21fe916b639b" + +FINN_BASE_URL="https://github.com/Xilinx/finn-base.git" +QONNX_URL="https://github.com/fastmachinelearning/qonnx.git" +FINN_EXP_URL="https://github.com/Xilinx/finn-experimental.git" +BREVITAS_URL="https://github.com/Xilinx/brevitas.git" +PYVERILATOR_URL="https://github.com/maltanar/pyverilator.git" +CNPY_URL="https://github.com/rogersce/cnpy.git" +HLSLIB_URL="https://github.com/Xilinx/finn-hlslib.git" +OMX_URL="https://github.com/maltanar/oh-my-xilinx.git" +AVNET_BDF_URL="https://github.com/Avnet/bdf.git" + +FINN_BASE_DIR="finn-base" +QONNX_DIR="qonnx" +FINN_EXP_DIR="finn-experimental" +BREVITAS_DIR="brevitas" +PYVERILATOR_DIR="pyverilator" +CNPY_DIR="cnpy" +HLSLIB_DIR="finn-hlslib" +OMX_DIR="oh-my-xilinx" +AVNET_BDF_DIR="avnet-bdf" + +# absolute path to this script, e.g. /home/user/bin/foo.sh +SCRIPT=$(readlink -f "$0") +# absolute path this script is in, thus /home/user/bin +SCRIPTPATH=$(dirname "$SCRIPT") + +fetch_repo() { + # URL for git repo to be cloned + REPO_URL=$1 + # commit hash for repo + REPO_COMMIT=$2 + # directory to clone to under deps/ + REPO_DIR=$3 + # absolute path for the repo local copy + CLONE_TO=$SCRIPTPATH/deps/$REPO_DIR + + # clone repo if dir not found + if [ ! -d "$CLONE_TO" ]; then + git clone $REPO_URL $CLONE_TO + fi + # verify and try to pull repo if not at correct commit + CURRENT_COMMIT=$(git -C $CLONE_TO rev-parse HEAD) + if [ $CURRENT_COMMIT != $REPO_COMMIT ]; then + git -C $CLONE_TO pull + # checkout the expected commit + git -C $CLONE_TO checkout $REPO_COMMIT + fi + # verify one last time + CURRENT_COMMIT=$(git -C $CLONE_TO rev-parse HEAD) + if [ $CURRENT_COMMIT == $REPO_COMMIT ]; then + echo "Successfully checked out $REPO_DIR at commit $CURRENT_COMMIT" + else + echo "Could not check out $REPO_DIR. Check your internet connection and try again." + fi +} + +fetch_repo $FINN_BASE_URL $FINN_BASE_COMMIT $FINN_BASE_DIR +fetch_repo $QONNX_URL $QONNX_COMMIT $QONNX_DIR +fetch_repo $FINN_EXP_URL $FINN_EXP_COMMIT $FINN_EXP_DIR +fetch_repo $BREVITAS_URL $BREVITAS_COMMIT $BREVITAS_DIR +fetch_repo $PYVERILATOR_URL $PYVERILATOR_COMMIT $PYVERILATOR_DIR +fetch_repo $CNPY_URL $CNPY_COMMIT $CNPY_DIR +fetch_repo $HLSLIB_URL $HLSLIB_COMMIT $HLSLIB_DIR +fetch_repo $OMX_URL $OMX_COMMIT $OMX_DIR +fetch_repo $AVNET_BDF_URL $AVNET_BDF_COMMIT $AVNET_BDF_DIR + +# TODO download extra Pynq board files and extract if needed diff --git a/finn-rtllib/memstream/component.xml b/finn-rtllib/memstream/component.xml index 1e5b710dc86bde4d442ce9e83b188aeed24388c5..63a8540a76a100201c67d7a1dcbaec15f10e1c0e 100644 --- a/finn-rtllib/memstream/component.xml +++ b/finn-rtllib/memstream/component.xml @@ -1677,6 +1677,7 @@ <xilinx:family xilinx:lifeCycle="Production">qzynq</xilinx:family> <xilinx:family xilinx:lifeCycle="Production">qzynqplus</xilinx:family> <xilinx:family xilinx:lifeCycle="Production">versal</xilinx:family> + <xilinx:family xilinx:lifeCycle="Production">versalprime</xilinx:family> <xilinx:family xilinx:lifeCycle="Production">virtex7</xilinx:family> <xilinx:family xilinx:lifeCycle="Production">virtexu</xilinx:family> <xilinx:family xilinx:lifeCycle="Production">virtexuplus</xilinx:family> diff --git a/run-docker.sh b/run-docker.sh index 2abd67f0679b32a09e51d03efe548bdc095c11a0..c804b8aa7b03d87309ba71443610ec4844fb123e 100755 --- a/run-docker.sh +++ b/run-docker.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (c) 2020, Xilinx +# Copyright (c) 2020-2022, Advanced Micro Devices # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -118,24 +118,29 @@ elif [ "$1" = "notebook" ]; then FINN_DOCKER_EXTRA+="-p $NETRON_PORT:$NETRON_PORT " elif [ "$1" = "build_dataflow" ]; then BUILD_DATAFLOW_DIR=$(readlink -f "$2") - FINN_DOCKER_EXTRA="-v $BUILD_DATAFLOW_DIR:$BUILD_DATAFLOW_DIR " + FINN_DOCKER_EXTRA+="-v $BUILD_DATAFLOW_DIR:$BUILD_DATAFLOW_DIR " DOCKER_INTERACTIVE="-it" #FINN_HOST_BUILD_DIR=$BUILD_DATAFLOW_DIR/build gecho "Running build_dataflow for folder $BUILD_DATAFLOW_DIR" DOCKER_CMD="build_dataflow $BUILD_DATAFLOW_DIR" elif [ "$1" = "build_custom" ]; then BUILD_CUSTOM_DIR=$(readlink -f "$2") - FINN_DOCKER_EXTRA="-v $BUILD_CUSTOM_DIR:$BUILD_CUSTOM_DIR -w $BUILD_CUSTOM_DIR " + FLOW_NAME=${3:-build} + FINN_DOCKER_EXTRA+="-v $BUILD_CUSTOM_DIR:$BUILD_CUSTOM_DIR -w $BUILD_CUSTOM_DIR " DOCKER_INTERACTIVE="-it" #FINN_HOST_BUILD_DIR=$BUILD_DATAFLOW_DIR/build - gecho "Running build_custom: $BUILD_CUSTOM_DIR/build.py" - DOCKER_CMD="python -mpdb -cc -cq build.py" + gecho "Running build_custom: $BUILD_CUSTOM_DIR/$FLOW_NAME.py" + DOCKER_CMD="python -mpdb -cc -cq $FLOW_NAME.py" +elif [ -z "$1" ]; then + gecho "Running container only" + DOCKER_CMD="bash" + DOCKER_INTERACTIVE="-it" else - gecho "Running container only" - DOCKER_CMD="bash" - DOCKER_INTERACTIVE="-it" + gecho "Running container with passed arguments" + DOCKER_CMD="$@" fi + if [ "$FINN_DOCKER_GPU" != 0 ];then gecho "nvidia-docker detected, enabling GPUs" if [ ! -z "$NVIDIA_VISIBLE_DEVICES" ];then @@ -178,7 +183,7 @@ DOCKER_EXEC+="-e SHELL=/bin/bash " DOCKER_EXEC+="-v $SCRIPTPATH:/workspace/finn " DOCKER_EXEC+="-v $FINN_HOST_BUILD_DIR:$FINN_HOST_BUILD_DIR " DOCKER_EXEC+="-e FINN_BUILD_DIR=$FINN_HOST_BUILD_DIR " -DOCKER_EXEC+="-e FINN_ROOT="/workspace/finn" " +DOCKER_EXEC+="-e FINN_ROOT="/workspace" " DOCKER_EXEC+="-e LOCALHOST_URL=$LOCALHOST_URL " DOCKER_EXEC+="-e VIVADO_IP_CACHE=$VIVADO_IP_CACHE " DOCKER_EXEC+="-e PYNQ_BOARD=$PYNQ_BOARD " @@ -204,11 +209,15 @@ fi if [ ! -z "$FINN_XILINX_PATH" ];then VIVADO_PATH="$FINN_XILINX_PATH/Vivado/$FINN_XILINX_VERSION" VITIS_PATH="$FINN_XILINX_PATH/Vitis/$FINN_XILINX_VERSION" + HLS_PATH="$FINN_XILINX_PATH/Vitis_HLS/$FINN_XILINX_VERSION" DOCKER_EXEC+="-v $FINN_XILINX_PATH:$FINN_XILINX_PATH " if [ -d "$VIVADO_PATH" ];then DOCKER_EXEC+="-e "XILINX_VIVADO=$VIVADO_PATH" " DOCKER_EXEC+="-e VIVADO_PATH=$VIVADO_PATH " fi + if [ -d "$HLS_PATH" ];then + DOCKER_EXEC+="-e HLS_PATH=$HLS_PATH " + fi if [ -d "$VITIS_PATH" ];then DOCKER_EXEC+="-e VITIS_PATH=$VITIS_PATH " fi diff --git a/setup.cfg b/setup.cfg index c1dff9bd9b44fc7ca7a02ad0891fd75f10009530..bcf5364b782447d21eea553ddcc2a6fc9b2636c0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -119,6 +119,11 @@ markers = vivado: mark tests that require Vivado or Vivado HLS vitis: mark tests that require Vitis board: mark tests that require a PYNQ board + brevitas_export : mark tests that test brevitas export functionality + streamline: mark tests that test streamlining functionality + util: mark tests that test util functions + transform: mark tests that test transformations (before hls layers) + fpgadataflow: mark tests related to hls layers norecursedirs = dist build diff --git a/src/finn/builder/build_dataflow_config.py b/src/finn/builder/build_dataflow_config.py index cb7ad10761852fd0cc2f10a64fc16bd73a08e55e..6abac895a9ba647d3fd3733fda4b337f3b05dca6 100644 --- a/src/finn/builder/build_dataflow_config.py +++ b/src/finn/builder/build_dataflow_config.py @@ -389,6 +389,6 @@ class DataflowBuildConfig: ) verify_expected_output_npy = np.load(self.verify_expected_output_npy) return ( - verify_input_npy.astype(np.float32), - verify_expected_output_npy.astype(np.float32), + verify_input_npy, + verify_expected_output_npy, ) diff --git a/src/finn/builder/build_dataflow_steps.py b/src/finn/builder/build_dataflow_steps.py index 7748626f0794a283262ea5283a550416b7489f26..85f150dcf83972a0d14cef959235965b5cf30fe6 100644 --- a/src/finn/builder/build_dataflow_steps.py +++ b/src/finn/builder/build_dataflow_steps.py @@ -55,6 +55,7 @@ from finn.builder.build_dataflow_config import ( ) from finn.core.modelwrapper import ModelWrapper from finn.core.onnx_exec import execute_onnx +from finn.core.rtlsim_exec import rtlsim_exec from finn.core.throughput_test import throughput_test_rtlsim from finn.custom_op.registry import getCustomOp from finn.transformation.bipolar_to_xnor import ConvertBipolarMatMulToXnorPopcount @@ -108,7 +109,11 @@ from finn.util.test import execute_parent def verify_step( - model: ModelWrapper, cfg: DataflowBuildConfig, step_name: str, need_parent: bool + model: ModelWrapper, + cfg: DataflowBuildConfig, + step_name: str, + need_parent: bool, + rtlsim_pre_hook=None, ): print("Running verification for " + step_name) verify_out_dir = cfg.output_dir + "/verification_output" @@ -131,7 +136,10 @@ def verify_step( inp_tensor_name = model.graph.input[0].name out_tensor_name = model.graph.output[0].name inp_dict = {inp_tensor_name: in_npy} - out_dict = execute_onnx(model, inp_dict, True) + if rtlsim_pre_hook is not None: + out_dict = rtlsim_exec(model, inp_dict, pre_hook=rtlsim_pre_hook) + else: + out_dict = execute_onnx(model, inp_dict, True) out_npy = out_dict[out_tensor_name] res = np.isclose(exp_out_npy, out_npy, atol=1e-3).all() res_to_str = {True: "SUCCESS", False: "FAIL"} diff --git a/src/finn/custom_op/fpgadataflow/addstreams_batch.py b/src/finn/custom_op/fpgadataflow/addstreams_batch.py index fa80e47485eef4f289b0272fd73ac185bd1c2c5e..7ba67247a37b790eb14f12948613eb975105cd84 100644 --- a/src/finn/custom_op/fpgadataflow/addstreams_batch.py +++ b/src/finn/custom_op/fpgadataflow/addstreams_batch.py @@ -340,14 +340,22 @@ class AddStreams_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=in1") - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=in1 name=in1_" + self.hls_sname() + ) + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) def get_verilog_top_module_intf_names(self): intf_names = super().get_verilog_top_module_intf_names() - intf_names["s_axis"].append(("in1_V_V", self.get_instream_width_padded())) + sname = self.hls_sname() + swidth = self.get_instream_width_padded() + intf_names["s_axis"] = [(x + "_" + sname, swidth) for x in ["in0", "in1"]] return intf_names diff --git a/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py b/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py index 4961f6148231252d255c1830ced418308032ce41..f6c5624543e47f488e5c42983a324ff48b43decd 100644 --- a/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py +++ b/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py @@ -350,13 +350,13 @@ class ChannelwiseOp_Batch(HLSCustomOp): # get desired function func = self.get_nodeattr("Func") if func == "cmp_le": - func_str = "comp::less_equal" + func_str = "comp::less_equal<%s, %s>" % (idt_hls, pdt_hls) elif func == "cmp_ge": - func_str = "std::greater_equal" + func_str = "comp::greater_equal<%s, %s>" % (idt_hls, pdt_hls) elif func == "add": - func_str = "std::plus" + func_str = "comp::add<%s, %s, %s>" % (odt_hls, odt_hls, odt_hls) elif func == "mul": - func_str = "std::multiplies" + func_str = "comp::mul<%s, %s, %s>" % (odt_hls, odt_hls, odt_hls) else: raise Exception( """Invalid value for attribute Func! Is currently set to: {} @@ -373,7 +373,7 @@ class ChannelwiseOp_Batch(HLSCustomOp): idt_hls, pdt_hls, odt_hls, - "%s<%s>" % (func_str, odt_hls), + func_str, ) ) f_params.write(parameters_hls_code) @@ -514,18 +514,15 @@ class ChannelwiseOp_Batch(HLSCustomOp): # should ImgDim be defined or just filled in here like we do now? ishape = self.get_folded_input_shape() if len(ishape) == 3: - imgdim_h = 1 - imgdim_w = 1 + spatial_dim = 1 elif len(ishape) == 5: - imgdim_h = ishape[1] - imgdim_w = ishape[2] + spatial_dim = ishape[1] * ishape[2] else: raise Exception("""Unexpeted input shape""") self.code_gen_dict["$DOCOMPUTE$"] = [ - """Thresholding_Batch<{}, {}, NumChannels1, PE1, {}, {}> + """Thresholding_Batch<{}, NumChannels1, PE1, {}, {}> (in0, out, threshs, numReps);""".format( - imgdim_h, - imgdim_w, + spatial_dim, tmpl_args["TSrcI"], tmpl_args["TDstI"], ) @@ -574,8 +571,12 @@ class ChannelwiseOp_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/concat.py b/src/finn/custom_op/fpgadataflow/concat.py index 3d61d3abc2b0411e107271586fba7a2c29b5fce5..ee8a2c323238c4e4f91b76c91d1445c69e3cdaa0 100644 --- a/src/finn/custom_op/fpgadataflow/concat.py +++ b/src/finn/custom_op/fpgadataflow/concat.py @@ -348,9 +348,14 @@ class StreamingConcat(HLSCustomOp): n_inputs = self.get_n_inputs() pragmas = [] for i in range(n_inputs): - pragmas.append("#pragma HLS INTERFACE axis port=in%d" % i) + pragmas.append( + "#pragma HLS INTERFACE axis port=in%d name=in%d_%s" + % (i, i, self.hls_sname()) + ) self.code_gen_dict["$PRAGMAS$"] = pragmas - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) @@ -362,9 +367,10 @@ class StreamingConcat(HLSCustomOp): def get_verilog_top_module_intf_names(self): intf_names = super().get_verilog_top_module_intf_names() n_inputs = self.get_n_inputs() + sname = self.hls_sname() intf_names["s_axis"] = [] for i in range(n_inputs): intf_names["s_axis"].append( - ("in%d_V_V" % i, self.get_instream_width_padded(i)) + ("in%d_%s" % (i, sname), self.get_instream_width_padded(i)) ) return intf_names diff --git a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py index a4018836846257c15ad203b1cef54c03cd081e45..e27b46b11ca43a804ea01571b0e1604e8e3e16a1 100644 --- a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py +++ b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py @@ -489,8 +489,12 @@ class ConvolutionInputGenerator(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py index 6347c9e9e6923cff6c1d02d272030fbdb100604a..5cb9bce0c86b6be533527a58ecbd8d08f82fa59b 100644 --- a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py +++ b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py @@ -29,6 +29,7 @@ import math import numpy as np import os +import warnings from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -85,6 +86,7 @@ class ConvolutionInputGenerator1D(HLSCustomOp): "distributed", {"auto", "block", "distributed", "ultra"}, ), + "parallel_window": ("i", False, 0, {0, 1}), } my_attrs.update(super().get_nodeattr_types()) return my_attrs @@ -181,18 +183,36 @@ class ConvolutionInputGenerator1D(HLSCustomOp): num_output_elems = np.prod(folded_oshape[:-1]) return num_output_elems + def get_swu_variant(self): + # checks which variant of the 1D ConvolutionInputGenerator (SWU) can be used + # We have 5 variants: ConvolutionInputGenerator_1D_parallel, + # ConvolutionInputGenerator_1D_dws_naive, ConvolutionInputGenerator_1D, + # ConvolutioninputGenerator_1D_dws, ConvolutionInputGenerator_1D_dws_stride + is_dws = self.get_nodeattr("depthwise") + is_strided = np.prod(self.get_nodeattr("Stride")) > 1 + is_stride_2 = np.prod(self.get_nodeattr("Stride")) == 2 + is_dilated = np.prod(self.get_nodeattr("Dilation")) > 1 + if self.use_parallel_window_output(): + return "ConvolutionInputGenerator_1D_parallel" + if not is_dws: + return "ConvolutionInputGenerator_1D" + if is_dws: + if (is_strided and not is_stride_2) or (is_dilated): + return "ConvolutionInputGenerator_1D_dws_naive" + elif is_stride_2: + return "ConvolutionInputGenerator_1D_dws_stride" + else: + return "ConvolutionInputGenerator_1D_dws" + def get_1d_conv_attrs_normalized(self): # support both (1, D) and (D, 1) cases transparently: # For the kernel, presenting the input data of size D as # [H, W] = [Y, X] = [1, D] or [D, 1] - # effectively gives the same result. Because the - # ConvolutionInputGenerator_NonSquare_Dilated(_dws) kernel currently only - # supports dilation>1 along the X-axis and the - # ConvolutionInputGenerator_NonSquare only works for stride>1 along the - # X-axis, we are working with the following assumption: - # the dummy ('1') dimension is the Y-dimension, i.e. - # images and kernels (and their attributes) of dimension - # [H, W] = [Y, X] = [D, 1] or [1, D] are always mapped to [1, D] + # effectively gives the same result. + # For consistency and ease of programming, this function + # returns the attributes of the layer as follows: + # [H, W] = [Y, X] = [1, D] or [D, 1] are always mapped to [1, D]. + # The dummy ('1') dimension is the Y-dimension. ifm_ch = self.get_nodeattr("IFMChannels") k = self.get_nodeattr("ConvKernelDim") ifm_dim = self.get_nodeattr("IFMDim") @@ -219,57 +239,92 @@ class ConvolutionInputGenerator1D(HLSCustomOp): dilation_h, dilation_w = dilation ram_style = self.get_nodeattr("ram_style") - if self.get_nodeattr("SIMD") == self.get_nodeattr("IFMChannels"): - if self.get_nodeattr("depthwise") == 0: - if stride_h == 1 and stride_w == 1: - if dilation_h == 1 and dilation_w == 1: - return ram_style in ["auto", "distributed"] - - return False + fully_unfolded = self.get_nodeattr("SIMD") == self.get_nodeattr("IFMChannels") + non_dws = self.get_nodeattr("depthwise") == 0 + no_stride = stride_h == 1 and stride_w == 1 + no_dilation = dilation_h == 1 and dilation_w == 1 + supported_ram_style = ram_style in ["auto", "distributed"] + if self.get_nodeattr("parallel_window") == 1: + if ( + fully_unfolded + and non_dws + and no_stride + and no_dilation + and supported_ram_style + ): + return True + else: + warnings.warn( + "{}: Parallel window output variant is not supported for this node,\ + please inspect requirements in use_parallel_window_output method\ + of the custom_op".format( + self.onnx_node.name + ) + ) + return False + else: + return False def get_exp_cycles(self): simd = self.get_nodeattr("SIMD") ( ifm_ch, - ifm_dim, - ofm_dim, - k, - stride, - dilation, + [ifm_dim_h, ifm_dim_w], + [ofm_dim_h, ofm_dim_w], + [k_h, k_w], + [stride_h, stride_w], + [dilation_h, dilation_w], ) = self.get_1d_conv_attrs_normalized() - ifm_dim_h, ifm_dim_w = ifm_dim - ofm_dim_h, ofm_dim_w = ofm_dim - k_h, k_w = k - stride_h, stride_w = stride - dilation_h, dilation_w = dilation # since mmv != 1 is not supported yet, we set mmv for now to 1 - mmv = 1 + # mmv = 1 # see https://github.com/Xilinx/finn-hlslib/blob/master/slidingwindow.h - if self.use_parallel_window_output(): + swu_variant = self.get_swu_variant() + if swu_variant == "ConvolutionInputGenerator_1D_parallel": exp_cycles = k_w + ofm_dim_w - else: - cycles_write_block = (ofm_dim_w * k_w * k_h * (ifm_ch / simd)) / mmv - cycles_read_block = stride_w * ifm_dim_w * (ifm_ch / simd) - max_cycles = max(cycles_write_block, cycles_read_block) + elif swu_variant == "ConvolutionInputGenerator_1D": + exp_cycles = 1 + ofm_dim_w * k_w * ifm_ch / simd + elif swu_variant in [ + "ConvolutionInputGenerator_1D_dws", + "ConvolutionInputGenerator_1D_dws_stride", + ]: exp_cycles = ( - ifm_dim_w * k_h * dilation_h * (ifm_ch / simd) + ofm_dim_h * max_cycles + 1 + + ofm_dim_w * k_w * ifm_ch / simd + + (ifm_ch / simd) * (k_w - 1) + - (k_w - 1) ) + elif swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + cycles_read_block = ifm_dim_w * ifm_ch / simd + cycles_write_block = ofm_dim_w * k_w * ifm_ch / simd + exp_cycles = cycles_read_block + cycles_write_block return int(exp_cycles) def bram_estimation(self): - # NOTE: not tested for correctness simd = self.get_nodeattr("SIMD") - ifm_ch = self.get_nodeattr("IFMChannels") - ifm_dim = np.prod(self.get_nodeattr("IFMDim")) - k = np.prod(self.get_nodeattr("ConvKernelDim")) - stride = np.prod(self.get_nodeattr("Stride")) + ( + ifm_ch, + [ifm_dim_h, ifm_dim_w], + [ofm_dim_h, ofm_dim_w], + [k_h, k_w], + [stride_h, stride_w], + [dilation_h, dilation_w], + ) = self.get_1d_conv_attrs_normalized() ram_style = self.get_nodeattr("ram_style") - if self.use_parallel_window_output(): + swu_variant = self.get_swu_variant() + if swu_variant == "ConvolutionInputGenerator_1D_parallel": return 0 if ram_style == "block" or ram_style == "auto": - ram_depth = ifm_dim * ifm_ch / simd + if swu_variant == "ConvolutionInputGenerator_1D": + ram_depth = (k_w - 1) * ifm_ch / simd + elif swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + ram_depth = ifm_dim_w * ifm_ch / simd + elif swu_variant in [ + "ConvolutionInputGenerator_1D_dws", + "ConvolutionInputGenerator_1D_dws_stride", + ]: + ram_depth = k_w * ifm_ch / simd if ram_depth <= 512: ram_width = 36 elif ram_depth <= 1024: @@ -282,63 +337,80 @@ class ConvolutionInputGenerator1D(HLSCustomOp): ram_width = 2 else: ram_width = 1 - return int( - (k + stride) - * ( - math.ceil(simd * self.get_input_datatype().bitwidth() / ram_width) - * math.ceil(ifm_dim * ifm_ch / simd / ram_depth) - ) + width_mul = math.ceil( + simd * self.get_input_datatype().bitwidth() / ram_width ) + depth_mul = math.ceil(ram_depth / 18432) + return width_mul * depth_mul else: return 0 def lut_estimation(self): - # NOTE: not tested for correctness simd = self.get_nodeattr("SIMD") - ifm_ch = self.get_nodeattr("IFMChannels") - ifm_dim = np.prod(self.get_nodeattr("IFMDim")) - k = np.prod(self.get_nodeattr("ConvKernelDim")) - stride = np.prod(self.get_nodeattr("Stride")) + ( + ifm_ch, + [ifm_dim_h, ifm_dim_w], + [ofm_dim_h, ofm_dim_w], + [k_h, k_w], + [stride_h, stride_w], + [dilation_h, dilation_w], + ) = self.get_1d_conv_attrs_normalized() ram_style = self.get_nodeattr("ram_style") - if self.use_parallel_window_output(): + swu_variant = self.get_swu_variant() + if swu_variant == "ConvolutionInputGenerator_1D_parallel": ram_luts = math.ceil( - (simd * self.get_input_datatype().bitwidth() * (k + 1)) / 64 + simd * self.get_input_datatype().bitwidth() * (k_w + 1) / 64 ) elif ram_style == "distributed": - ram_luts = int( - (k + stride) - * ( - simd - * self.get_input_datatype().bitwidth() - * math.ceil(ifm_dim * ifm_ch / simd / 64) + if swu_variant == "ConvolutionInputGenerator_1D": + ram_luts = math.ceil( + self.get_input_datatype().bitwidth() * (k_w - 1) * ifm_ch / 64 + ) + elif swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + ram_luts = math.ceil( + self.get_input_datatype().bitwidth() * ifm_dim_w * ifm_ch / 64 + ) + elif swu_variant in [ + "ConvolutionInputGenerator_1D_dws", + "ConvolutionInputGenerator_1D_dws_stride", + ]: + ram_luts = math.ceil( + self.get_input_datatype().bitwidth() * k_w * ifm_ch / 64 ) - ) else: ram_luts = 0 return 300 + ram_luts def uram_estimation(self): - # NOTE: not tested for correctness + simd = self.get_nodeattr("SIMD") ( ifm_ch, - ifm_dim, - ofm_dim, - k, - stride, - dilation, + [ifm_dim_h, ifm_dim_w], + [ofm_dim_h, ofm_dim_w], + [k_h, k_w], + [stride_h, stride_w], + [dilation_h, dilation_w], ) = self.get_1d_conv_attrs_normalized() - ifm_dim_y, ifm_dim_x = ifm_dim - k_y, k_x = k - stride_y, stride_x = stride ram_style = self.get_nodeattr("ram_style") - simd = self.get_nodeattr("SIMD") - if self.use_parallel_window_output(): + swu_variant = self.get_swu_variant() + if swu_variant == "ConvolutionInputGenerator_1D_parallel": return 0 elif ram_style == "ultra": - block_mul = 2 - width_mul = math.ceil(simd * self.get_input_datatype().bitwidth() / 64) - depth_mul = math.ceil(stride_x * ifm_dim_x * (ifm_ch // simd) / 4096) - return block_mul * width_mul * depth_mul + if swu_variant == "ConvolutionInputGenerator_1D": + width_mul = math.ceil(simd * self.get_input_datatype().bitwidth() / 72) + depth_mul = math.ceil((k_w - 1) * ifm_ch / simd / 4096) + return width_mul * depth_mul + elif swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + width_mul = math.ceil(simd * self.get_input_datatype().bitwidth() / 72) + depth_mul = math.ceil(ifm_dim_w * ifm_ch / simd / 4096) + return width_mul * depth_mul + elif swu_variant in [ + "ConvolutionInputGenerator_1D_dws", + "ConvolutionInputGenerator_1D_dws_stride", + ]: + width_mul = math.ceil(simd * self.get_input_datatype().bitwidth() / 72) + depth_mul = math.ceil(k_w * ifm_ch / simd / 4096) + return width_mul * depth_mul else: return 0 @@ -436,89 +508,83 @@ class ConvolutionInputGenerator1D(HLSCustomOp): numReps = 1 ( ifm_ch, - ifm_dim, - ofm_dim, - k, - stride, - dilation, + [ifm_dim_h, ifm_dim_w], + [ofm_dim_h, ofm_dim_w], + [k_h, k_w], + [stride_h, stride_w], + [dilation_h, dilation_w], ) = self.get_1d_conv_attrs_normalized() simd = self.get_nodeattr("SIMD") ifm_precision = self.get_input_datatype().bitwidth() - ifm_dim_y, ifm_dim_x = ifm_dim - ofm_dim_y, ofm_dim_x = ofm_dim - k_y, k_x = k - dilation_y, dilation_x = dilation - # For a 1d convolution with stride=[S,1] or [1,S], the finn-hlslib function - # of ConvInpGen must be created with [stride_y, stride_x] = [S, S]. - # TODO: changes in finn-hlslib (slidingwindow.h) - stride_y = np.prod(stride) - stride_x = np.prod(stride) - - if dilation_x > 1: - assert ( - dilation_y == 1 - ), "Dilation value greater than 1 along y-axis is not yet supported" + swu_variant = self.get_swu_variant() + + if swu_variant in [ + "ConvolutionInputGenerator_1D_parallel", + "ConvolutionInputGenerator_1D", + "ConvolutionInputGenerator_1D_dws_stride", + ]: self.code_gen_dict["$DEFINES$"] = [ """ #define ConvKernelDim1_x {}\n - #define ConvKernelDim1_y {}\n #define IFMChannels1 {}\n #define Input_precision1 {}\n #define IFMDim1_x {}\n - #define IFMDim1_y {}\n #define OFMDim1_x {}\n - #define OFMDim1_y {}\n - #define SIMD1 {}\n #define Stride1_x {}\n - #define Stride1_y {}\n - #define Dilation1_x {}\n - #define Dilation1_y {}\n + #define SIMD1 {}\n #define numReps {} """.format( - k_x, - k_y, + k_w, ifm_ch, ifm_precision, - ifm_dim_x, - ifm_dim_y, - ofm_dim_x, - ofm_dim_y, + ifm_dim_w, + ofm_dim_w, + stride_w, simd, - stride_x, - stride_y, - dilation_x, - dilation_y, numReps, ) ] - else: - ofm_dim = self.get_nodeattr("OFMDim") + if swu_variant == "ConvolutionInputGenerator_1D_dws": self.code_gen_dict["$DEFINES$"] = [ """ #define ConvKernelDim1_x {}\n - #define ConvKernelDim1_y {}\n #define IFMChannels1 {}\n #define Input_precision1 {}\n #define IFMDim1_x {}\n - #define IFMDim1_y {}\n #define OFMDim1_x {}\n - #define OFMDim1_y {}\n #define SIMD1 {}\n + #define numReps {} + """.format( + k_w, + ifm_ch, + ifm_precision, + ifm_dim_w, + ofm_dim_w, + simd, + numReps, + ) + ] + if swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + self.code_gen_dict["$DEFINES$"] = [ + """ + #define ConvKernelDim1_x {}\n + #define IFMChannels1 {}\n + #define Input_precision1 {}\n + #define IFMDim1_x {}\n + #define OFMDim1_x {}\n #define Stride1_x {}\n - #define Stride1_y {}\n + #define Dilation1_x {}\n + #define SIMD1 {}\n #define numReps {} """.format( - k_x, - k_y, + k_w, ifm_ch, ifm_precision, - ifm_dim_x, - ifm_dim_y, - ofm_dim_x, - ofm_dim_y, + ifm_dim_w, + ofm_dim_w, + stride_w, + dilation_w, simd, - stride_x, - stride_y, numReps, ) ] @@ -559,49 +625,49 @@ class ConvolutionInputGenerator1D(HLSCustomOp): "ultra": "ap_resource_uram()", } hls_ram_style = map_to_hls_ram_style[ram_style] + swu_variant = self.get_swu_variant() # check which ConvolutionInputGenerator is needed - if self.use_parallel_window_output(): - hls_call = "ConvolutionInputGenerator_1D_parallel" + if swu_variant == "ConvolutionInputGenerator_1D_parallel": self.code_gen_dict["$DOCOMPUTE$"] = [ """{}<ConvKernelDim1_x, IFMChannels1, Input_precision1, - IFMDim1_x, OFMDim1_x, SIMD1, Stride1_x> + IFMDim1_x, OFMDim1_x, Stride1_x, SIMD1> (in0, out, numReps, {});""".format( - hls_call, hls_ram_style + swu_variant, hls_ram_style + ) + ] + if swu_variant == "ConvolutionInputGenerator_1D": + self.code_gen_dict["$DOCOMPUTE$"] = [ + """{}<ConvKernelDim1_x, IFMChannels1, Input_precision1, + IFMDim1_x, OFMDim1_x, Stride1_x, SIMD1> + (in0, out, numReps, {});""".format( + swu_variant, hls_ram_style + ) + ] + if swu_variant == "ConvolutionInputGenerator_1D_dws": + self.code_gen_dict["$DOCOMPUTE$"] = [ + """{}<ConvKernelDim1_x, IFMChannels1, Input_precision1, + IFMDim1_x, OFMDim1_x, SIMD1> + (in0, out, numReps, {});""".format( + swu_variant, hls_ram_style + ) + ] + if swu_variant == "ConvolutionInputGenerator_1D_dws_stride": + self.code_gen_dict["$DOCOMPUTE$"] = [ + """{}<ConvKernelDim1_x, IFMChannels1, Input_precision1, + IFMDim1_x, OFMDim1_x, Stride1_x, SIMD1> + (in0, out, numReps, {});""".format( + swu_variant, hls_ram_style + ) + ] + if swu_variant == "ConvolutionInputGenerator_1D_dws_naive": + self.code_gen_dict["$DOCOMPUTE$"] = [ + """{}<ConvKernelDim1_x, IFMChannels1, Input_precision1, + IFMDim1_x, OFMDim1_x, Stride1_x, Dilation1_x, SIMD1> + (in0, out, numReps, {});""".format( + swu_variant, hls_ram_style ) ] - else: - hls_call = "ConvolutionInputGenerator_NonSquare" - dilation_h, dilation_w = self.get_nodeattr("Dilation") - if dilation_h > 1 or dilation_w > 1: - hls_call += "_Dilated" - if self.get_nodeattr("depthwise") == 1: - hls_call += "_dws" - self.code_gen_dict["$DOCOMPUTE$"] = [ - """{}<ConvKernelDim1_x, ConvKernelDim1_y, IFMChannels1, - Input_precision1, IFMDim1_x, IFMDim1_y, OFMDim1_x, OFMDim1_y, - SIMD1, Stride1_x, Stride1_y, Dilation1_x, Dilation1_y> - (in0, out, numReps, {});""".format( - hls_call, hls_ram_style - ) - ] - elif self.get_nodeattr("depthwise") == 1: - hls_call += "_dws" - self.code_gen_dict["$DOCOMPUTE$"] = [ - """{}<ConvKernelDim1_x, ConvKernelDim1_y, IFMChannels1, - Input_precision1, IFMDim1_x, IFMDim1_y, OFMDim1_x, OFMDim1_y, - SIMD1, Stride1_x, Stride1_y> (in0, out, numReps, {});""".format( - hls_call, hls_ram_style - ) - ] - else: - self.code_gen_dict["$DOCOMPUTE$"] = [ - """{}<ConvKernelDim1_x, ConvKernelDim1_y, IFMChannels1, - Input_precision1, IFMDim1_x, IFMDim1_y, OFMDim1_x, OFMDim1_y, - SIMD1, Stride1_x, Stride1_y> (in0, out, numReps, {});""".format( - hls_call, hls_ram_style - ) - ] def dataoutstrm(self): code_gen_dir = self.get_nodeattr("code_gen_dir_cppsim") @@ -659,8 +725,12 @@ class ConvolutionInputGenerator1D(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/downsampler.py b/src/finn/custom_op/fpgadataflow/downsampler.py index 124b3e4645caa63a2590d91c58f430f8d56bb6a0..789d6ece9ad1d8cfc16353b638df31a422cffb72 100644 --- a/src/finn/custom_op/fpgadataflow/downsampler.py +++ b/src/finn/custom_op/fpgadataflow/downsampler.py @@ -248,8 +248,12 @@ class DownSampler(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/duplicatestreams_batch.py b/src/finn/custom_op/fpgadataflow/duplicatestreams_batch.py index 51c8e4aea34b22a6b509eea65e9ebacaa640f234..d6f9f3bd3baf7a2961e8fe4447a4f86a6458a664 100644 --- a/src/finn/custom_op/fpgadataflow/duplicatestreams_batch.py +++ b/src/finn/custom_op/fpgadataflow/duplicatestreams_batch.py @@ -384,10 +384,13 @@ class DuplicateStreams_Batch(HLSCustomOp): def pragmas(self): n_outputs = self.get_num_output_streams() - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] for i in range(n_outputs): self.code_gen_dict["$PRAGMAS$"].append( - "#pragma HLS INTERFACE axis port=out%d" % i + "#pragma HLS INTERFACE axis port=out%d name=out%d_%s" + % (i, i, self.hls_sname()) ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" @@ -396,9 +399,10 @@ class DuplicateStreams_Batch(HLSCustomOp): def get_verilog_top_module_intf_names(self): intf_names = super().get_verilog_top_module_intf_names() n_outputs = self.get_num_output_streams() + sname = self.hls_sname() intf_names["m_axis"] = [] for i in range(n_outputs): intf_names["m_axis"].append( - ("out%d_V_V" % i, self.get_outstream_width_padded()) + ("out%d_%s" % (i, sname), self.get_outstream_width_padded()) ) return intf_names diff --git a/src/finn/custom_op/fpgadataflow/fmpadding_batch.py b/src/finn/custom_op/fpgadataflow/fmpadding_batch.py index 8ac30524ebee6f503e34f6d92408f3f137a59c72..a2d42f63a8f5c9cf997ad540040daa21cb3d39d1 100644 --- a/src/finn/custom_op/fpgadataflow/fmpadding_batch.py +++ b/src/finn/custom_op/fpgadataflow/fmpadding_batch.py @@ -312,8 +312,12 @@ class FMPadding_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py b/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py index 6d4a55ee5c86b68776f4c7c2e58930034bb0be02..7812b9531ed742ae00bb4383f5dc77c84c77f4c4 100644 --- a/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py +++ b/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py @@ -331,8 +331,12 @@ class GlobalAccPool_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/hlscustomop.py b/src/finn/custom_op/fpgadataflow/hlscustomop.py index 3aac7f6b451ed12ab265a20a7df1bfa6c1d7b4c7..b4da42a5e715c835f9c289c76179cb0efe7edf9d 100644 --- a/src/finn/custom_op/fpgadataflow/hlscustomop.py +++ b/src/finn/custom_op/fpgadataflow/hlscustomop.py @@ -112,13 +112,22 @@ class HLSCustomOp(CustomOp): # input and output FIFO depths "inFIFODepth": ("i", False, 2), "outFIFODepth": ("i", False, 2), + # HLS version to be used for IP synthesis + "hls_version": ("s", False, "vitis_hls", {"vivado_hls", "vitis_hls"}), } def get_verilog_top_module_name(self): "Return the Verilog top module name for this node." node = self.onnx_node - prefixed_top_name = "%s_%s" % (node.name, node.name) + hls_version = self.get_nodeattr("hls_version") + if hls_version == "vivado_hls": + prefixed_top_name = "%s_%s" % (node.name, node.name) + elif hls_version == "vitis_hls": + prefixed_top_name = node.name + else: + raise Exception("Unknown hls_version: %s" % hls_version) + return prefixed_top_name def get_verilog_top_module_intf_names(self): @@ -133,8 +142,9 @@ class HLSCustomOp(CustomOp): intf_names = {} intf_names["clk"] = ["ap_clk"] intf_names["rst"] = ["ap_rst_n"] - intf_names["s_axis"] = [("in0_V_V", self.get_instream_width_padded())] - intf_names["m_axis"] = [("out_V_V", self.get_outstream_width_padded())] + sname = self.hls_sname() + intf_names["s_axis"] = [("in0_" + sname, self.get_instream_width_padded())] + intf_names["m_axis"] = [("out_" + sname, self.get_outstream_width_padded())] intf_names["aximm"] = [] intf_names["axilite"] = [] return intf_names @@ -290,10 +300,9 @@ class HLSCustomOp(CustomOp): self.code_gen_dict["$PROJECTNAME$"] = ["project_{}".format(node.name)] self.code_gen_dict["$HWSRCDIR$"] = [code_gen_dir] self.code_gen_dict["$FPGAPART$"] = [fpgapart] - self.code_gen_dict["$FINNHLSLIBDIR$"] = ["/workspace/finn-hlslib"] - self.code_gen_dict["$FINNHLSCUSTOMDIR$"] = ["/workspace/finn/custom_hls"] self.code_gen_dict["$TOPFXN$"] = [node.name] self.code_gen_dict["$CLKPERIOD$"] = [str(clk)] + self.code_gen_dict["$DEFAULT_DIRECTIVES$"] = self.ipgen_default_directives() self.code_gen_dict["$EXTRA_DIRECTIVES$"] = self.ipgen_extra_directives() template = self.ipgentcl_template @@ -308,16 +317,37 @@ class HLSCustomOp(CustomOp): f.close() self.code_gen_dict.clear() + def ipgen_default_directives(self): + """Return list of default HLS synthesis directives, which differ + slightly between vivado_hls and vitis_hls""" + + hls_version = self.get_nodeattr("hls_version") + default_directives = { + "vivado_hls": [ + "config_compile -ignore_long_run_time -disable_unroll_code_size_check", + "config_interface -m_axi_addr64", + "config_rtl -auto_prefix", + ], + "vitis_hls": [ + "set_param hls.enable_hidden_option_error false", + "config_compile -disable_unroll_code_size_check", + "config_interface -m_axi_addr64", + "config_rtl -auto_prefix", + "config_export -disable_deadlock_detection", + ], + } + return default_directives[hls_version] + def ipgen_extra_directives(self): "Return a list of extra tcl directives for HLS synthesis." return [] def ipgen_singlenode_code(self): - """Builds the bash script for ip generation using the CallHLS from - finn.util.hls.""" + """Builds the bash script for IP generation using the CallHLS utility.""" node = self.onnx_node code_gen_dir = self.get_nodeattr("code_gen_dir_ipgen") - builder = CallHLS() + hls_version = self.get_nodeattr("hls_version") + builder = CallHLS(backend=hls_version) builder.append_tcl(code_gen_dir + "/hls_syn_{}.tcl".format(node.name)) builder.set_ipgen_path(code_gen_dir + "/project_{}".format(node.name)) builder.build(code_gen_dir) @@ -372,15 +402,15 @@ class HLSCustomOp(CustomOp): builder = CppBuilder() # to enable additional debug features please uncommand the next line # builder.append_includes("-DDEBUG") - builder.append_includes("-I/workspace/finn/src/finn/qnn-data/cpp") - builder.append_includes("-I/workspace/cnpy/") - builder.append_includes("-I/workspace/finn-hlslib") - builder.append_includes("-I/workspace/finn/custom_hls") - builder.append_includes("-I{}/include".format(os.environ["VIVADO_PATH"])) - builder.append_includes("--std=c++11") + builder.append_includes("-I$FINN_ROOT/finn/src/finn/qnn-data/cpp") + builder.append_includes("-I$FINN_ROOT/cnpy/") + builder.append_includes("-I$FINN_ROOT/finn-hlslib") + builder.append_includes("-I$FINN_ROOT/finn/custom_hls") + builder.append_includes("-I{}/include".format(os.environ["HLS_PATH"])) + builder.append_includes("--std=c++14") builder.append_includes("-O3") builder.append_sources(code_gen_dir + "/*.cpp") - builder.append_sources("/workspace/cnpy/cnpy.cpp") + builder.append_sources("$FINN_ROOT/cnpy/cnpy.cpp") builder.append_includes("-lz") builder.set_executable_path(code_gen_dir + "/node_model") builder.build(code_gen_dir) @@ -453,6 +483,17 @@ compilation transformations? sim.io.ap_clk = 1 sim.io.ap_clk = 0 + def hls_sname(self): + """Get the naming convention used by chosen HLS version for stream signals, + decided by the hls_version node attribute. + Example: the TDATA for a stream called "out" would be out_V_V_TDATA + in vivado_hls and out_V_TDATA in vitis_hls. + """ + hls_version = self.get_nodeattr("hls_version") + sname_dict = {"vivado_hls": "V_V", "vitis_hls": "V"} + sname = sname_dict[hls_version] + return sname + def rtlsim(self, sim, inp, inp2=None): """Runs the pyverilator simulation by passing the input values to the simulation, toggle the clock and observing the execution time. Function contains also an @@ -466,7 +507,18 @@ compilation transformations? sim.start_vcd_trace(trace_file) inputs = inp outputs = [] - sim.io.out_V_V_TREADY = 1 + sname = self.hls_sname() + o_ready = "out_" + sname + "_TREADY" + o_valid = "out_" + sname + "_TVALID" + o_data = "out_" + sname + "_TDATA" + in0_ready = "in0_" + sname + "_TREADY" + in0_valid = "in0_" + sname + "_TVALID" + in0_data = "in0_" + sname + "_TDATA" + in1_ready = "in1_" + sname + "_TREADY" + in1_valid = "in1_" + sname + "_TVALID" + in1_data = "in1_" + sname + "_TDATA" + + sim.io[o_ready] = 1 # observe if output is completely calculated # observation_count will contain the number of cycles the calculation ran @@ -481,19 +533,19 @@ compilation transformations? liveness_threshold = pyverilate_get_liveness_threshold_cycles() while not (output_observed): - sim.io.in0_V_V_TVALID = 1 if len(inputs) > 0 else 0 - sim.io.in0_V_V_TDATA = inputs[0] if len(inputs) > 0 else 0 - if sim.io.in0_V_V_TREADY == 1 and sim.io.in0_V_V_TVALID == 1: + sim.io[in0_valid] = 1 if len(inputs) > 0 else 0 + sim.io[in0_data] = inputs[0] if len(inputs) > 0 else 0 + if sim.io[in0_ready] == 1 and sim.io[in0_valid] == 1: inputs = inputs[1:] if inp2 is not None: - sim.io.in1_V_V_TVALID = 1 if len(inp2) > 0 else 0 - sim.io.in1_V_V_TDATA = inp2[0] if len(inp2) > 0 else 0 - if sim.io.in1_V_V_TREADY == 1 and sim.io.in1_V_V_TVALID == 1: + sim.io[in1_valid] = 1 if len(inp2) > 0 else 0 + sim.io[in1_data] = inp2[0] if len(inp2) > 0 else 0 + if sim.io[in1_ready] == 1 and sim.io[in1_valid] == 1: inp2 = inp2[1:] - if sim.io.out_V_V_TVALID == 1 and sim.io.out_V_V_TREADY == 1: - outputs = outputs + [sim.io.out_V_V_TDATA] + if sim.io[o_valid] == 1 and sim.io[o_ready] == 1: + outputs = outputs + [sim.io[o_data]] sim.io.ap_clk = 1 sim.io.ap_clk = 0 @@ -525,11 +577,16 @@ compilation transformations? def rtlsim_multi_io(self, sim, io_dict): "Run rtlsim for this node, supports multiple i/o streams." + # signal naming differs slightly between vivado_hls/vitis_hls + sname = "_" + self.hls_sname() + "_" + trace_file = self.get_nodeattr("rtlsim_trace") if trace_file == "default": trace_file = self.onnx_node.name + ".vcd" num_out_values = self.get_number_output_values() - total_cycle_count = rtlsim_multi_io(sim, io_dict, num_out_values, trace_file) + total_cycle_count = rtlsim_multi_io( + sim, io_dict, num_out_values, trace_file, sname=sname + ) self.set_nodeattr("cycles_rtlsim", total_cycle_count) def execute_node(self, context, graph): diff --git a/src/finn/custom_op/fpgadataflow/iodma.py b/src/finn/custom_op/fpgadataflow/iodma.py index 802c7e78515336ef884e5ff09356085b5cc6069f..a331caee0193e101dd108299c159dfd97c893cfa 100644 --- a/src/finn/custom_op/fpgadataflow/iodma.py +++ b/src/finn/custom_op/fpgadataflow/iodma.py @@ -83,11 +83,14 @@ class IODMA(HLSCustomOp): "NumChannels": ("i", True, 0), # FINN input datatype "dataType": ("s", True, ""), - # Stream parameters + # Width of input or output stream "streamWidth": ("i", False, 32), # DMA-specific parameters + # width of axi-mm interface "intfWidth": ("i", False, 32), + # burst mode for axi-mm interface (wrap used for DRAM weights) "burstMode": ("s", False, "increment", {"wrap", "increment"}), + # IODMA direction: in = read from DRAM, out = write to DRAM "direction": ("s", False, "in", {"in", "out"}), # shape describing input vecs per execution "numInputVectors": ("ints", False, [1]), @@ -224,20 +227,19 @@ class IODMA(HLSCustomOp): def docompute(self): direction = self.get_nodeattr("direction") mode = self.get_nodeattr("burstMode") + dwc_func = "StreamingDataWidthConverter_Batch" if direction == "in": if mode == "wrap": func = "Mem2Stream_Batch_external_wmem" else: func = "Mem2Stream_Batch" - dwc_func = "WidthAdjustedOutputStream" elif direction == "out": func = "Stream2Mem_Batch" - dwc_func = "WidthAdjustedInputStream" else: raise ValueError("Invalid IODMA direction, please set to in or out") # define templates for instantiation dma_inst_template = func + "<DataWidth1, NumBytes1>(%s, %s, numReps);" - dwc_inst_template = dwc_func + "<%d, %d, %d> %s(%s, numReps);" + dwc_inst_template = dwc_func + "<%d, %d, %d>(%s, %s, numReps);" # do stream infrastructure and instantiations intfw = self.get_nodeattr("intfWidth") strmw = self.get_nodeattr("streamWidth") @@ -246,22 +248,65 @@ class IODMA(HLSCustomOp): # because we use WidthAdjustedInputStream, dtype_bits = self.get_input_datatype().bitwidth() total_bits = dtype_bits * np.prod(self.get_normal_input_shape()) + if direction == "in": - self.code_gen_dict["$DOCOMPUTE$"] = [ - dwc_inst_template - % (width_lcm, strmw, total_bits // width_lcm, "dwc_lcm", "out"), - dwc_inst_template - % (intfw, width_lcm, total_bits // intfw, "dwc_intfw", "dwc_lcm"), - dma_inst_template % ("in0", "dwc_intfw"), - ] + # AXI MM -> IODMA -> (DWCs) -> out + # DWCs depend on AXI MM and out interface width + if strmw == intfw: + # case 0: AXI MM width = out width, no DWCs needed + self.code_gen_dict["$DOCOMPUTE$"] = [dma_inst_template % ("in0", "out")] + elif (strmw % intfw == 0) or (intfw % strmw == 0): + # case 1: AXI MM width divisible by out width or vice versa + # single DWC + single extra stream needed + self.code_gen_dict["$DOCOMPUTE$"] = [ + "hls::stream<ap_uint<%d> > dma2dwc;" % intfw, + dma_inst_template % ("in0", "dma2dwc"), + dwc_inst_template + % (intfw, strmw, total_bits // intfw, "dma2dwc", "out"), + ] + else: + # case 2: AXI MM width not divisible by out width or vice versa + # need 2 DWCs (going through the least common multiple width) + # and 2 streams + self.code_gen_dict["$DOCOMPUTE$"] = [ + "hls::stream<ap_uint<%d> > dma2lcm;" % intfw, + "hls::stream<ap_uint<%d> > lcm2out;" % width_lcm, + dma_inst_template % ("in0", "dma2lcm"), + dwc_inst_template + % (intfw, width_lcm, total_bits // intfw, "dma2lcm", "lcm2out"), + dwc_inst_template + % (width_lcm, strmw, total_bits // width_lcm, "lcm2out", "out"), + ] + elif direction == "out": + # in0 -> (DWCs) -> IODMA -> AXI MM + # DWCs depend on AXI MM and out interface width + if strmw == intfw: + # case 0: in width = AXI MM width, no DWCs needed + self.code_gen_dict["$DOCOMPUTE$"] = [dma_inst_template % ("in0", "out")] + elif (strmw % intfw == 0) or (intfw % strmw == 0): + # case 1: AXI MM width divisible by in width or vice versa + # single DWC + single extra stream needed + self.code_gen_dict["$DOCOMPUTE$"] = [ + "hls::stream<ap_uint<%d> > dwc2dma;" % intfw, + dwc_inst_template + % (strmw, intfw, total_bits // strmw, "in0", "dwc2dma"), + dma_inst_template % ("dwc2dma", "out"), + ] + else: + # case 2: AXI MM width not divisible by out width or vice versa + # need 2 DWCs (going through the least common multiple width) + # and 2 streams + self.code_gen_dict["$DOCOMPUTE$"] = [ + "hls::stream<ap_uint<%d> > in2lcm;" % width_lcm, + "hls::stream<ap_uint<%d> > lcm2dma;" % intfw, + dwc_inst_template + % (strmw, width_lcm, total_bits // strmw, "in0", "in2lcm"), + dwc_inst_template + % (width_lcm, intfw, total_bits // width_lcm, "in2lcm", "lcm2dma"), + dma_inst_template % ("lcm2dma", "out"), + ] else: - self.code_gen_dict["$DOCOMPUTE$"] = [ - dwc_inst_template - % (strmw, width_lcm, total_bits // strmw, "dwc_lcm", "in0"), - dwc_inst_template - % (width_lcm, intfw, total_bits // width_lcm, "dwc_intfw", "dwc_lcm"), - dma_inst_template % ("dwc_intfw", "out"), - ] + raise Exception("Unknown IODMA direction: %s" % direction) def blackboxfunction(self): packed_ibits = self.get_instream_width() @@ -304,11 +349,11 @@ class IODMA(HLSCustomOp): "#pragma HLS INTERFACE s_axilite port=in0 bundle=control" ) self.code_gen_dict["$PRAGMAS$"].append( - "#pragma HLS INTERFACE axis port=out" + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() ) elif direction == "out": self.code_gen_dict["$PRAGMAS$"].append( - "#pragma HLS INTERFACE axis port=in0" + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() ) if intfname == "": self.code_gen_dict["$PRAGMAS$"].append( diff --git a/src/finn/custom_op/fpgadataflow/labelselect_batch.py b/src/finn/custom_op/fpgadataflow/labelselect_batch.py index 1eb5962fdbc54092eaeb4796806b3a623c65aea8..da994fb13971e2066c9838b8c963372ab1ee0d92 100644 --- a/src/finn/custom_op/fpgadataflow/labelselect_batch.py +++ b/src/finn/custom_op/fpgadataflow/labelselect_batch.py @@ -342,8 +342,12 @@ class LabelSelect_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/lookup.py b/src/finn/custom_op/fpgadataflow/lookup.py index 27be06bdfa3ce3d980a139ec91385c7fe85afab3..dcf67e4c4338b8a903fefd7a83a96331d0a5c8e9 100644 --- a/src/finn/custom_op/fpgadataflow/lookup.py +++ b/src/finn/custom_op/fpgadataflow/lookup.py @@ -29,13 +29,14 @@ import numpy as np import os import warnings -from math import ceil +from math import ceil, log2 from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp from finn.util.data_packing import ( npy_to_rtlsim_input, numpy_to_hls_code, + pack_innermost_dim_as_hex_string, rtlsim_output_to_npy, ) @@ -58,6 +59,13 @@ class Lookup(HLSCustomOp): "InputType": ("s", True, ""), # Input shape "InputShape": ("ints", False, [1]), + # Memory mode + # const : parameters baked into bitfile (BRAM) + # external : lookup performed in external memory over AXI MM + "mem_mode": ("s", False, "const", ["const", "external"]), + # Width for AXI-MM interface + # only relevant when mem_mode="external" + "ext_mem_width": ("i", False, 32), } my_attrs.update(super().get_nodeattr_types()) return my_attrs @@ -72,7 +80,8 @@ class Lookup(HLSCustomOp): def get_normal_output_shape(self): ishape = self.get_normal_input_shape() - oshape = list(ishape) + [self.get_nodeattr("EmbeddingDim")] + emb_dim = self.get_nodeattr("EmbeddingDim") + oshape = list(ishape) + [emb_dim] return tuple(oshape) def get_folded_input_shape(self): @@ -81,7 +90,23 @@ class Lookup(HLSCustomOp): return tuple(folded_ishape) def get_folded_output_shape(self): - return self.get_normal_output_shape() + ishape = self.get_normal_input_shape() + mem_mode = self.get_nodeattr("mem_mode") + emb_dim = self.get_nodeattr("EmbeddingDim") + if mem_mode == "const": + oshape = list(ishape) + [emb_dim] + elif mem_mode == "external": + ext_mem_width = self.get_nodeattr("ext_mem_width") + bits_per_emb_elem = self.get_output_datatype().bitwidth() + assert ext_mem_width % bits_per_emb_elem == 0 + emb_elems_per_ext_mem_width = ext_mem_width // bits_per_emb_elem + oshape = list(ishape) + [ + emb_dim // emb_elems_per_ext_mem_width, + emb_elems_per_ext_mem_width, + ] + else: + raise Exception("Unrecognized mem_mode:" + mem_mode) + return tuple(oshape) def make_shape_compatible_op(self, model): exp_ishape = tuple(self.get_normal_input_shape()) @@ -123,17 +148,20 @@ class Lookup(HLSCustomOp): return ibits def get_outstream_width(self): + folded_oshape = self.get_folded_output_shape() obits = self.get_output_datatype().bitwidth() - ofm_ch = self.get_nodeattr("EmbeddingDim") - return obits * ofm_ch + return obits * folded_oshape[-1] def get_number_output_values(self): folded_oshape = self.get_folded_output_shape() return np.prod(folded_oshape[:-1]) def global_includes(self): - global_incls = ['#include "lookup.hpp"'] - global_incls.append('#include "embeddings.hpp"') + mem_mode = self.get_nodeattr("mem_mode") + global_incls = [] + if mem_mode == "const": + global_incls.append('#include "lookup.hpp"') + global_incls.append('#include "embeddings.hpp"') self.code_gen_dict["$GLOBALS$"] = global_incls def defines(self, var): @@ -142,14 +170,26 @@ class Lookup(HLSCustomOp): elem_hls_type = dtype.get_hls_datatype_str() emb_type = DataType[self.get_nodeattr("EmbeddingType")] emb_hls_type = emb_type.get_hls_datatype_str() + emb_dim = self.get_nodeattr("EmbeddingDim") + mem_mode = self.get_nodeattr("mem_mode") my_defines = [] - my_defines.append( - "#define NumEmbeddings %d" % self.get_nodeattr("NumEmbeddings") - ) - my_defines.append("#define EmbeddingDim %d" % self.get_nodeattr("EmbeddingDim")) my_defines.append("#define NumInputs %d" % n_inputs) - my_defines.append("#define InputType %s" % elem_hls_type) - my_defines.append("#define EmbeddingType %s" % emb_hls_type) + if mem_mode == "external": + ext_mem_width = self.get_nodeattr("ext_mem_width") + ext_mem_emb_size = self.get_folded_output_shape()[-2] + ext_mem_emb_align = ceil(log2(ext_mem_emb_size)) + my_defines.append("#define MemBits %d" % ext_mem_width) + my_defines.append("#define EmbeddingSize %d" % ext_mem_emb_size) + my_defines.append("#define EmbeddingAlign %d" % ext_mem_emb_align) + my_defines.append("#define T_SRC %s" % elem_hls_type) + my_defines.append("#define T_DST ap_uint<MemBits>") + elif mem_mode == "const": + my_defines.append( + "#define NumEmbeddings %d" % self.get_nodeattr("NumEmbeddings") + ) + my_defines.append("#define EmbeddingDim %d" % emb_dim) + my_defines.append("#define InputType %s" % elem_hls_type) + my_defines.append("#define EmbeddingType %s" % emb_hls_type) self.code_gen_dict["$DEFINES$"] = my_defines def read_npy_data(self): @@ -186,7 +226,7 @@ class Lookup(HLSCustomOp): oshape_cpp_str = str(oshape).replace("(", "{").replace(")", "}") self.code_gen_dict["$DATAOUTSTREAM$"] = [ - 'apintstream2npy<%s, %s, %d, %s>(out, %s, "%s");' + 'apintstream2npy<%s, %s, %d, %s>(out, %s, "%s", %s);' % ( packed_hls_type, elem_hls_type, @@ -194,6 +234,7 @@ class Lookup(HLSCustomOp): npy_type, oshape_cpp_str, npy_out, + "false", ) ] @@ -210,43 +251,115 @@ class Lookup(HLSCustomOp): ) def docompute(self): - self.code_gen_dict["$DOCOMPUTE$"] = [ - """StreamingLookup<NumEmbeddings, EmbeddingDim, NumInputs, - InputType, EmbeddingType >(in0, out, embeddings);""" - ] + mem_mode = self.get_nodeattr("mem_mode") + if mem_mode == "const": + self.code_gen_dict["$DOCOMPUTE$"] = [ + """StreamingLookup<NumEmbeddings, EmbeddingDim, NumInputs, + InputType, EmbeddingType >(in0, out, embeddings);""" + ] + elif mem_mode == "external": + hls_impl = """ + if(!in0.empty()) { + ap_uint<T_SRC::width+EmbeddingAlign> const base = + (in0.read(), ap_uint<EmbeddingAlign>(0)); + for(unsigned j = 0; j < EmbeddingSize; j++) { +#pragma HLS PIPELINE II=1 + out.write(mem[base+j]); + } + } + """ + self.code_gen_dict["$DOCOMPUTE$"] = [hls_impl] def blackboxfunction(self): + mem_mode = self.get_nodeattr("mem_mode") ibits = self.get_instream_width() packed_input_hls_type = "ap_uint<%d>" % ibits obits = self.get_outstream_width() packed_output_hls_type = "ap_uint<%d>" % obits - self.code_gen_dict["$BLACKBOXFUNCTION$"] = [ - "void %s(hls::stream<%s > &in0, hls::stream<%s > &out)" - % (self.onnx_node.name, packed_input_hls_type, packed_output_hls_type) - ] + if mem_mode == "const": + self.code_gen_dict["$BLACKBOXFUNCTION$"] = [ + "void %s(hls::stream<%s > &in0, hls::stream<%s > &out)" + % (self.onnx_node.name, packed_input_hls_type, packed_output_hls_type) + ] + elif mem_mode == "external": + self.code_gen_dict["$BLACKBOXFUNCTION$"] = [ + "void " + + self.onnx_node.name + + "(hls::stream<T_SRC> &in0, hls::stream<T_DST> &out, " + + "T_DST const *const mem)" + ] def pragmas(self): - my_pragmas = ["#pragma HLS INTERFACE axis port=in0"] - my_pragmas.append("#pragma HLS INTERFACE axis port=out") + mem_mode = self.get_nodeattr("mem_mode") + my_pragmas = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + my_pragmas.append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) my_pragmas.append("#pragma HLS INTERFACE ap_ctrl_none port=return") + if mem_mode == "const": + my_pragmas.append( + "#pragma HLS BIND_STORAGE variable=embeddings type=ROM_2P impl=BRAM" + ) + elif mem_mode == "external": + my_pragmas.append("#pragma HLS INTERFACE m_axi offset=slave port=mem") + my_pragmas.append("#pragma HLS INTERFACE s_axilite port=mem bundle=control") + else: + raise Exception("Unrecognized mem_mode: " + mem_mode) self.code_gen_dict["$PRAGMAS$"] = my_pragmas def generate_params(self, model, path): - code_gen_dir = path + mem_mode = self.get_nodeattr("mem_mode") embeddings = model.get_initializer(self.onnx_node.input[1]) - weight_filename = "{}/embeddings.hpp".format(code_gen_dir) - edt = DataType[self.get_nodeattr("EmbeddingType")] - # obits = self.get_outstream_width() - # packed_output_hls_type = "ap_uint<%d>" % obits - assert np.vectorize(edt.allowed)( - embeddings - ).all(), "Embeddings can't be expressed with type %s" % str(edt) - embeddings_hls_code = numpy_to_hls_code( - embeddings, edt, "embeddings", True, False - ) - f_thresh = open(weight_filename, "w") - f_thresh.write(embeddings_hls_code) - f_thresh.close() + if mem_mode == "const": + code_gen_dir = path + weight_filename = "{}/embeddings.hpp".format(code_gen_dir) + edt = DataType[self.get_nodeattr("EmbeddingType")] + # obits = self.get_outstream_width() + # packed_output_hls_type = "ap_uint<%d>" % obits + assert np.vectorize(edt.allowed)( + embeddings + ).all(), "Embeddings can't be expressed with type %s" % str(edt) + # reverse innertmost dim in embeddings to remain compatible with + # how we normally encode the data in FINN + embeddings_rev = np.flip(embeddings, -1) + embeddings_hls_code = numpy_to_hls_code( + embeddings_rev, edt, "embeddings", True, False + ) + f_thresh = open(weight_filename, "w") + f_thresh.write(embeddings_hls_code) + f_thresh.close() + elif mem_mode == "external": + edt = DataType[self.get_nodeattr("EmbeddingType")] + ext_mem_width = self.get_nodeattr("ext_mem_width") + assert edt.bitwidth() == 8, ( + "Lookup with mem_mode=external " + + "only works with 8-bit embeddings but found " + + str(edt) + ) + emb_dim = self.get_nodeattr("EmbeddingDim") + # need to zero-pad embeddings in external mode for burst alignment + # compute how much padding we need + emb_elems_per_ext_mem_width = self.get_folded_output_shape()[-1] + ext_mem_emb_size = self.get_folded_output_shape()[-2] + ext_mem_emb_align = ceil(log2(ext_mem_emb_size)) + align_factor = int((ext_mem_width / 8) * 2**ext_mem_emb_align) + pad_amount = align_factor - emb_dim + embeddings_padded = np.pad(embeddings, [(0, 0), (0, pad_amount)]) + # reshape for packing the innermost dim + embeddings_padded = embeddings_padded.reshape( + -1, emb_elems_per_ext_mem_width + ) + weight_filename = "%s/%s.dat" % (path, self.onnx_node.name) + ret = pack_innermost_dim_as_hex_string( + embeddings_padded, edt, ext_mem_width, True, prefix="" + ) + with open(weight_filename, "w") as f: + for current_line in ret: + f.write(current_line + "\n") + else: + raise Exception("Unrecognized mem_mode: " + mem_mode) def execute_node(self, context, graph): mode = self.get_nodeattr("exec_mode") @@ -255,6 +368,10 @@ class Lookup(HLSCustomOp): exp_oshape = tuple(self.get_normal_output_shape()) folded_ishape = tuple(self.get_folded_input_shape()) folded_oshape = tuple(self.get_folded_output_shape()) + mem_mode = self.get_nodeattr("mem_mode") + assert ( + mem_mode == "const" + ), "Only mem_mode=const is supported for simulation of Lookup layer" if mode == "cppsim": code_gen_dir = self.get_nodeattr("code_gen_dir_cppsim") @@ -306,7 +423,7 @@ class Lookup(HLSCustomOp): out_shape, packed_bits, target_bits, - reverse_inner=False, + reverse_inner=True, ) # load and reshape output output = np.load(out_npy_path) @@ -324,10 +441,16 @@ class Lookup(HLSCustomOp): ), """Output shape doesn't match expected shape.""" def bram_estimation(self): - # current calculation assumes embeddings always stored in BRAM_18Ks - width_factor = ceil(self.get_outstream_width() / 16) - depth_factor = ceil(self.get_nodeattr("NumEmbeddings") / 1024) - return width_factor * depth_factor + mem_mode = self.get_nodeattr("mem_mode") + if mem_mode == "const": + # current calculation assumes embeddings always stored in BRAM_18Ks + # when mem_mode is const + width_factor = ceil(self.get_outstream_width() / 16) + depth_factor = ceil(self.get_nodeattr("NumEmbeddings") / 1024) + return width_factor * depth_factor + else: + # TODO can we estimate BRAMs for the DMA engine? + return 0 def bram_efficiency_estimation(self): bram16_est = self.bram_estimation() @@ -336,3 +459,18 @@ class Lookup(HLSCustomOp): ebits = self.get_outstream_width() * self.get_nodeattr("NumEmbeddings") bram16_est_capacity = bram16_est * 18 * 1024 return ebits / bram16_est_capacity + + def get_ap_int_max_w(self): + parent_max = super().get_ap_int_max_w() + mem_mode = self.get_nodeattr("mem_mode") + ext_mem_width = self.get_nodeattr("ext_mem_width") + if mem_mode == "external": + return max(ext_mem_width, parent_max) + else: + return parent_max + + def get_verilog_top_module_intf_names(self): + intf_names = super().get_verilog_top_module_intf_names() + intf_names["axilite"] = ["s_axi_control"] + intf_names["aximm"] = [("m_axi_gmem", self.get_nodeattr("ext_mem_width"))] + return intf_names diff --git a/src/finn/custom_op/fpgadataflow/pool_batch.py b/src/finn/custom_op/fpgadataflow/pool_batch.py index 708a3a149abe268d122d339a5c25648630a01ff6..7d0ad43107d53168f441d1513c3855300cfdf4f8 100644 --- a/src/finn/custom_op/fpgadataflow/pool_batch.py +++ b/src/finn/custom_op/fpgadataflow/pool_batch.py @@ -200,7 +200,8 @@ class Pool_Batch(HLSCustomOp): return info_messages def global_includes(self): - self.code_gen_dict["$GLOBALS$"] = ['#include "maxpool.h"'] + self.code_gen_dict["$GLOBALS$"] = ['#include "activations.hpp"'] + self.code_gen_dict["$GLOBALS$"] += ['#include "maxpool.h"'] self.code_gen_dict["$GLOBALS$"] += ['#include "pool.hpp"'] def defines(self, var): @@ -326,8 +327,12 @@ class Pool_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py b/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py index 1791706afa217d5eb453064547c1ea66b306d227..5fabef57be3675c38fcfd74c0db99f50d98340f4 100644 --- a/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py @@ -76,24 +76,30 @@ class StreamingDataWidthConverter_Batch(HLSCustomOp): oshape = self.get_nodeattr("shape") return oshape + def check_divisible_iowidths(self): + impl_style = self.get_nodeattr("impl_style") + if impl_style == "hls": + # when using impl_style = hls must have the following + # if inWidth > outWidth: inWidth % outWidth = 0 + # if inWidth < outWidth: outWidth % inWidth = 0 + iwidth = self.get_nodeattr("inWidth") + owidth = self.get_nodeattr("outWidth") + if iwidth > owidth: + assert ( + iwidth % owidth == 0 + ), """DWC InWidth is bigger than OutWidth and is not divisible by it. + Please adjust PE and SIMD values so that InWidth % OutWidth = 0 + or alternatively use impl_style = vivado""" + else: + assert ( + owidth % iwidth == 0 + ), """DWC OutWidth is bigger than InWidth and is not divisible by it. + Please adjust PE and SIMD values so that OutWidth % InWidth = 0 + or alternatively use impl_style = vivado""" + def get_folded_input_shape(self): - # for correct functionality of the dwc node the - # following must apply: - # if inWidth > outWidth: inWidth % outWidth = 0 - # if inWidth < outWidth: outWidth % inWidth = 0 + self.check_divisible_iowidths() iwidth = self.get_nodeattr("inWidth") - owidth = self.get_nodeattr("outWidth") - if iwidth > owidth: - assert ( - iwidth % owidth == 0 - ), """InWidth is bigger than OutWidth and is not divisible by it. - Please adjust PE and SIMD values so that InWidth % OutWidth = 0""" - else: - assert ( - owidth % iwidth == 0 - ), """OutWidth is bigger than InWidth and is not divisible by it. - Please adjust PE and SIMD values so that OutWidth % InWidth = 0""" - ishape = self.get_normal_input_shape() dummy_t = np.random.randn(*ishape) ibits = self.get_input_datatype().bitwidth() @@ -112,23 +118,8 @@ class StreamingDataWidthConverter_Batch(HLSCustomOp): return dummy_t.shape def get_folded_output_shape(self): - # for correct functionality of the dwc node the - # following must apply: - # if inWidth > outWidth: inWidth % outWidth = 0 - # if inWidth < outWidth: outWidth % inWidth = 0 - iwidth = self.get_nodeattr("inWidth") + self.check_divisible_iowidths() owidth = self.get_nodeattr("outWidth") - if iwidth > owidth: - assert ( - iwidth % owidth == 0 - ), """InWidth is bigger than OutWidth and is not divisible by it. - Please adjust PE and SIMD values so that InWidth % OutWidth = 0""" - else: - assert ( - owidth % iwidth == 0 - ), """OutWidth is bigger than InWidth and is not divisible by it. - Please adjust PE and SIMD values so that OutWidth % InWidth = 0""" - oshape = self.get_normal_output_shape() dummy_t = np.random.randn(*oshape) obits = self.get_output_datatype().bitwidth() @@ -287,22 +278,29 @@ class StreamingDataWidthConverter_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) def execute_node(self, context, graph): mode = self.get_nodeattr("exec_mode") + impl_style = self.get_nodeattr("impl_style") node = self.onnx_node exp_shape = self.get_normal_input_shape() folded_ishape = self.get_folded_input_shape() # TODO ensure codegen dir exists if mode == "cppsim": + assert impl_style == "hls", "DWC cppsim only possible when impl_style==hls" code_gen_dir = self.get_nodeattr("code_gen_dir_cppsim") elif mode == "rtlsim": + assert impl_style == "hls", "DWC rtlsim only possible when impl_style==hls" code_gen_dir = self.get_nodeattr("code_gen_dir_ipgen") else: raise Exception( diff --git a/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py b/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py index 896e7c2925e340455f98344d1275d9368f701ed9..3f4103b4380f8d1838910b37e966e8363891d39f 100644 --- a/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py @@ -393,9 +393,16 @@ class StreamingFCLayer_Batch(HLSCustomOp): exp_cycles = (mh / pe) * (mw / simd) * np.prod(num_inp_vec) / mmv return int(exp_cycles) - def get_input_datatype(self): + def get_input_datatype(self, ind=0): """Returns FINN DataType of input.""" - return DataType[self.get_nodeattr("inputDataType")] + # when performing FIFO insertion on an FC layer with ext weights, the ind + # parameter can be > 0 (referring to the weights) so handle that here + if ind == 0: + return DataType[self.get_nodeattr("inputDataType")] + elif ind == 1: + return DataType[self.get_nodeattr("weightDataType")] + else: + raise Exception("Undefined input ind for this layer type") def get_weight_datatype(self): """Returns FINN DataType of weights.""" @@ -871,7 +878,7 @@ class StreamingFCLayer_Batch(HLSCustomOp): tdt_hls, odt_hls, self.get_nodeattr("ActVal"), - "comp::less_equal<%s>" % tdt_hls, + "comp::less_equal<%s, %s>" % (tdt_hls, tdt_hls), ) ) f_thresh.write(thresholds_hls_code) @@ -1210,8 +1217,12 @@ class StreamingFCLayer_Batch(HLSCustomOp): def pragmas(self): mem_mode = self.get_nodeattr("mem_mode") ram_style_thresholds = self.get_nodeattr("ram_style_thresholds") - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) in_fifo_depth = self.get_nodeattr("inFIFODepth") out_fifo_depth = self.get_nodeattr("outFIFODepth") # insert depth pragmas only if specified @@ -1239,7 +1250,8 @@ class StreamingFCLayer_Batch(HLSCustomOp): ) elif mem_mode == "decoupled" or mem_mode == "external": self.code_gen_dict["$PRAGMAS$"].append( - "#pragma HLS INTERFACE axis port=weights" + "#pragma HLS INTERFACE axis port=weights name=weights_" + + self.hls_sname() ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS stream depth=8 variable=weights" @@ -1302,6 +1314,7 @@ class StreamingFCLayer_Batch(HLSCustomOp): runtime_writable == 1 ), "Layer with URAM weights must have runtime_writeable_weights=1" node_name = self.onnx_node.name + sname = self.hls_sname() # create a hierarchy for this layer, with the same port names clk_name = self.get_verilog_top_module_intf_names()["clk"][0] rst_name = self.get_verilog_top_module_intf_names()["rst"][0] @@ -1355,8 +1368,8 @@ class StreamingFCLayer_Batch(HLSCustomOp): ) cmd.append( "connect_bd_intf_net [get_bd_intf_pins %s/%s/m_axis_0] " - "[get_bd_intf_pins %s/%s/weights_V_V]" - % (node_name, strm_inst, node_name, node_name) + "[get_bd_intf_pins %s/%s/weights_%s]" + % (node_name, strm_inst, node_name, node_name, sname) ) cmd.append( "connect_bd_net [get_bd_pins %s/%s] [get_bd_pins %s/%s/aresetn]" @@ -1410,9 +1423,10 @@ class StreamingFCLayer_Batch(HLSCustomOp): def get_verilog_top_module_intf_names(self): intf_names = super().get_verilog_top_module_intf_names() mem_mode = self.get_nodeattr("mem_mode") + sname = self.hls_sname() if mem_mode == "external": intf_names["s_axis"].append( - ("weights_V_V", self.get_weightstream_width_padded()) + ("weights_" + sname, self.get_weightstream_width_padded()) ) if mem_mode == "decoupled": # only expose axilite interface if attribute is set diff --git a/src/finn/custom_op/fpgadataflow/streamingfifo.py b/src/finn/custom_op/fpgadataflow/streamingfifo.py index 91f6ed5b8d29fd72ea1fbb8a3da94cfc103af88e..923081ecdeb65e829a59c4c9bfdc67fc03a82ccc 100644 --- a/src/finn/custom_op/fpgadataflow/streamingfifo.py +++ b/src/finn/custom_op/fpgadataflow/streamingfifo.py @@ -128,6 +128,7 @@ class StreamingFIFO(HLSCustomOp): self.code_gen_dict["$OUT_RANGE$"] = ["[{}:0]".format(in_width - 1)] self.code_gen_dict["$WIDTH$"] = [str(in_width)] self.code_gen_dict["$DEPTH$"] = [str(self.get_nodeattr("depth"))] + self.code_gen_dict["$HLS_SNAME$"] = [self.hls_sname()] template = self.strm_fifo_wrapper @@ -152,6 +153,7 @@ class StreamingFIFO(HLSCustomOp): # note: setting the root dir as absolute can cause path problems # the ipgen script will be invoked from the sources dir so root_dir=. is OK self.code_gen_dict["$VERILOG_DIR$"] = ["."] + self.code_gen_dict["$HLS_SNAME$"] = [self.hls_sname()] for key in self.code_gen_dict: # transform list into long string separated by '\n' code_gen_line = "\n".join(self.code_gen_dict[key]) diff --git a/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py b/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py old mode 100644 new mode 100755 index 1e66a5c204cc62bb7620907f82fcd5b2072bc184..6fbf176d4c80d5b5cd6caac294e131ec1a515438 --- a/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py @@ -32,9 +32,12 @@ import warnings from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp -from finn.custom_op.general.im2col import compute_conv_output_dim +from finn.custom_op.general.maxpoolnhwc import compute_pool_output_dim from finn.util.data_packing import npy_to_rtlsim_input, rtlsim_output_to_npy +# TODO: consider splitting this into separate implementations for 1D and 2D +# similar to what we do for ConvolutionInputGenerator + class StreamingMaxPool_Batch(HLSCustomOp): """Class that corresponds to finn-hlslib StreamingMaxPool_batch function.""" @@ -44,6 +47,10 @@ class StreamingMaxPool_Batch(HLSCustomOp): "ImgDim": ("ints", True, []), # [H, W] = [Y, X] "PoolDim": ("ints", True, []), # [H, W] = [Y, X] "NumChannels": ("i", True, 0), + # parallelism control - only supported for 1D maxpool + "PE": ("i", False, 0), + # round up (instead of down) output size - only supported for 1D maxpool + "CeilMode": ("i", False, 0), # FINN DataTypes for inputs/outputs "dataType": ("s", True, ""), } @@ -82,24 +89,30 @@ class StreamingMaxPool_Batch(HLSCustomOp): return ishape def get_folded_input_shape(self): - # even though there is no folding in the current hlslib op, - # insert a time multiplexing axis to remain compatible with the - # shapes produced by the rest of the dataflow pipeline - ret = list(self.get_normal_input_shape()) - ret.insert(-1, 1) - return tuple(ret) + ifm_dim_h, ifm_dim_w = self.get_nodeattr("ImgDim") + ifm_ch = self.get_nodeattr("NumChannels") + pe = self.get_nodeattr("PE") + nf = int(ifm_ch / pe) + if self.is_1d(): + folded_ishape = (1, ifm_dim_h, ifm_dim_w, nf, pe) + else: + folded_ishape = (1, ifm_dim_h, ifm_dim_w, 1, ifm_ch) + return folded_ishape def get_normal_output_shape(self): ifm_dim_h, ifm_dim_w = self.get_nodeattr("ImgDim") k_h, k_w = tuple(self.get_nodeattr("PoolDim")) ifm_ch = self.get_nodeattr("NumChannels") - stride_h = k_h - stride_w = k_w - pad = 0 - assert ifm_dim_h % k_h == 0, "StreamingMaxPool needs ImgDim_h % PoolDim_h == 0" - assert ifm_dim_w % k_w == 0, "StreamingMaxPool needs ImgDim_w % PoolDim_w == 0" - ofm_dim_h = compute_conv_output_dim(ifm_dim_h, k_h, stride_h, pad) - ofm_dim_w = compute_conv_output_dim(ifm_dim_w, k_w, stride_w, pad) + ceil_mode = self.get_nodeattr("CeilMode") + if not self.is_1d(): + assert ( + ifm_dim_h % k_h == 0 + ), "StreamingMaxPool needs ImgDim_h % PoolDim_h == 0" + assert ( + ifm_dim_w % k_w == 0 + ), "StreamingMaxPool needs ImgDim_w % PoolDim_w == 0" + ofm_dim_h = compute_pool_output_dim(ifm_dim_h, k_h, k_h, 0, ceil_mode) + ofm_dim_w = compute_pool_output_dim(ifm_dim_w, k_w, k_w, 0, ceil_mode) oshape = (1, ofm_dim_h, ofm_dim_w, ifm_ch) return oshape @@ -107,8 +120,15 @@ class StreamingMaxPool_Batch(HLSCustomOp): # even though there is no folding in the current hlslib op, # insert a time multiplexing axis to remain compatible with the # shapes produced by the rest of the dataflow pipeline + ifm_ch = self.get_nodeattr("NumChannels") + pe = self.get_nodeattr("PE") + nf = int(ifm_ch / pe) ret = list(self.get_normal_output_shape()) - ret.insert(-1, 1) + if self.is_1d(): + ret[-1] = nf + ret.append(pe) + else: + ret.insert(-1, 1) return tuple(ret) def get_number_output_values(self): @@ -118,20 +138,27 @@ class StreamingMaxPool_Batch(HLSCustomOp): def get_exp_cycles(self): # derived from StreamingMaxPool_Batch loop nest ifm_dim, k, ifm_ch = self.get_1d_attrs_normalized() + _, _, ofm_dim_w, nf, _ = self.get_folded_output_shape() + if self.is_1d(): - return int(ifm_dim[1] + k[1]) + exp_cycles = ofm_dim_w * nf * (k[1] + 1) + return int(exp_cycles) else: # TODO: adjust inaccurate formula return int(ifm_dim[1] * (ifm_dim[1] + (ifm_dim[1] / k[1]))) def get_instream_width(self): dt_bits = self.get_input_datatype().bitwidth() + pe = self.get_nodeattr("PE") ifm_ch = self.get_nodeattr("NumChannels") - in_width = int(dt_bits * ifm_ch) + if self.is_1d(): + in_width = int(dt_bits * pe) + else: + in_width = int(dt_bits * ifm_ch) return in_width def get_outstream_width(self): - """For streaming maxpool out stream with is the same as in stream width""" + """For streaming maxpool out stream width is the same as in stream width""" return self.get_instream_width() def make_shape_compatible_op(self, model): @@ -176,18 +203,34 @@ class StreamingMaxPool_Batch(HLSCustomOp): self.code_gen_dict["$GLOBALS$"] = ['#include "maxpool.h"'] def defines(self, var): - numReps = 2 + numReps = 1 ifm_dim, k, ifm_ch = self.get_1d_attrs_normalized() + ceil_mode = self.get_nodeattr("CeilMode") + output_size = compute_pool_output_dim(ifm_dim[1], k[1], k[1], 0, ceil_mode) - self.code_gen_dict["$DEFINES$"] = [ - """#define ImgDim {}\n #define PoolDim {}\n - #define NumChannels {}\n #define numReps {}""".format( - ifm_dim[1], - k[1], - self.get_nodeattr("NumChannels"), - numReps, - ) - ] + if self.is_1d(): + self.code_gen_dict["$DEFINES$"] = [ + """#define ImgDim {}\n #define PoolDim {}\n + #define NumChannels {}\n #define PE {}\n #define OutputSize {} + \n #define numReps {}""".format( + ifm_dim[1], + k[1], + self.get_nodeattr("NumChannels"), + self.get_nodeattr("PE"), + output_size, + numReps, + ) + ] + else: + self.code_gen_dict["$DEFINES$"] = [ + """#define ImgDim {}\n #define PoolDim {}\n + #define NumChannels {}\n #define numReps {}""".format( + ifm_dim[1], + k[1], + self.get_nodeattr("NumChannels"), + numReps, + ) + ] def read_npy_data(self): code_gen_dir = self.get_nodeattr("code_gen_dir_cppsim") @@ -222,22 +265,27 @@ class StreamingMaxPool_Batch(HLSCustomOp): if self.is_1d(): raise Exception("Binary 1d MaxPool not implemented on HLS backend") else: - op = "StreamingMaxPool_Batch" + op = "StreamingMaxPool" self.code_gen_dict["$DOCOMPUTE$"] = [ - "%s<ImgDim, PoolDim, NumChannels>(in0, out, numReps);" % (op) + "%s<ImgDim, PoolDim, NumChannels>(in0, out);" % (op) ] else: - if self.is_1d(): - op = "StreamingMaxPool_Precision_Batch_1d" - else: - op = "StreamingMaxPool_Precision_Batch" dtype = self.get_input_datatype() dtype_hls = dtype.get_hls_datatype_str() minval_str = str(int(dtype.min())) - self.code_gen_dict["$DOCOMPUTE$"] = [ - "%s<ImgDim, PoolDim, NumChannels, %s, %s>(in0, out, numReps);" - % (op, dtype_hls, minval_str) - ] + if self.is_1d(): + op = "StreamingMaxPool_Precision_1d" + self.code_gen_dict["$DOCOMPUTE$"] = [ + """%s<ImgDim, PoolDim, NumChannels, PE, + OutputSize, %s, %s>(in0, out);""" + % (op, dtype_hls, minval_str) + ] + else: + op = "StreamingMaxPool_Precision" + self.code_gen_dict["$DOCOMPUTE$"] = [ + "%s<ImgDim, PoolDim, NumChannels, %s, %s>(in0, out);" + % (op, dtype_hls, minval_str) + ] def dataoutstrm(self): code_gen_dir = self.get_nodeattr("code_gen_dir_cppsim") @@ -278,8 +326,12 @@ class StreamingMaxPool_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) @@ -289,6 +341,7 @@ class StreamingMaxPool_Batch(HLSCustomOp): node = self.onnx_node exp_ishape = self.get_normal_input_shape() exp_oshape = self.get_normal_output_shape() + folded_ishape = self.get_folded_input_shape() folded_oshape = self.get_folded_output_shape() # TODO ensure codegen dir exists @@ -316,9 +369,8 @@ class StreamingMaxPool_Batch(HLSCustomOp): export_idt = DataType["BINARY"] else: export_idt = self.get_input_datatype() - # no reshaping for input since assuming no folding on input - # make copy before saving array - reshaped_input = inp.copy() + + reshaped_input = inp.reshape(folded_ishape) np.save(os.path.join(code_gen_dir, "input_0.npy"), reshaped_input) if mode == "cppsim": @@ -329,7 +381,7 @@ class StreamingMaxPool_Batch(HLSCustomOp): assert ( context[node.output[0]].shape == folded_oshape ), "cppsim \ - did not produce expected ofolded utput shape" + did not produce expected folded output shape" context[node.output[0]] = context[node.output[0]].reshape(*exp_oshape) elif mode == "rtlsim": sim = self.get_rtlsim() @@ -367,4 +419,4 @@ class StreamingMaxPool_Batch(HLSCustomOp): assert ( context[node.output[0]].shape == exp_oshape ), """Output - shape doesn't match expected shape (1, ofm_dim, ofm_dim, k*k*ifm_ch).""" + shape doesn't match expected shape (1, ofm_dim, ofm_dim, ifm_ch).""" diff --git a/src/finn/custom_op/fpgadataflow/templates.py b/src/finn/custom_op/fpgadataflow/templates.py index e253348598d72897c2a8f83f5bee04351eb43d32..d33a7b54b8e96c5e63aa8b77743e83e7460715a6 100644 --- a/src/finn/custom_op/fpgadataflow/templates.py +++ b/src/finn/custom_op/fpgadataflow/templates.py @@ -86,23 +86,21 @@ puts "HLS project: $config_proj_name" set config_hwsrcdir "$HWSRCDIR$" puts "HW source dir: $config_hwsrcdir" set config_proj_part "$FPGAPART$" - -set config_bnnlibdir "$FINNHLSLIBDIR$" -set config_customhlsdir "$FINNHLSCUSTOMDIR$" - +set config_bnnlibdir "$::env(FINN_ROOT)/finn-hlslib" +puts "finn-hlslib dir: $config_bnnlibdir" +set config_customhlsdir "$::env(FINN_ROOT)/finn/custom_hls" +puts "custom HLS dir: $config_customhlsdir" set config_toplevelfxn "$TOPFXN$" set config_clkperiod $CLKPERIOD$ open_project $config_proj_name -add_files $config_hwsrcdir/top_$TOPFXN$.cpp -cflags "-std=c++0x -I$config_bnnlibdir -I$config_customhlsdir" +add_files $config_hwsrcdir/top_$TOPFXN$.cpp -cflags "-std=c++14 -I$config_bnnlibdir -I$config_customhlsdir" set_top $config_toplevelfxn open_solution sol1 set_part $config_proj_part -config_compile -ignore_long_run_time -disable_unroll_code_size_check -config_interface -m_axi_addr64 -config_rtl -auto_prefix +$DEFAULT_DIRECTIVES$ $EXTRA_DIRECTIVES$ create_clock -period $config_clkperiod -name default @@ -116,22 +114,22 @@ decoupled_wrapper = """ module $TOPNAME$( ap_clk, ap_rst_n, -in0_V_V_TDATA, -in0_V_V_TVALID, -in0_V_V_TREADY, -out_V_V_TDATA, -out_V_V_TVALID, -out_V_V_TREADY +in0_$HLS_SNAME$_TDATA, +in0_$HLS_SNAME$_TVALID, +in0_$HLS_SNAME$_TREADY, +out_$HLS_SNAME$_TDATA, +out_$HLS_SNAME$_TVALID, +out_$HLS_SNAME$_TREADY ); input ap_clk; input ap_rst_n; -input $IN_RANGE$ in0_V_V_TDATA; -input in0_V_V_TVALID; -output in0_V_V_TREADY; -output $OUT_RANGE$ out_V_V_TDATA; -output out_V_V_TVALID; -input out_V_V_TREADY; +input $IN_RANGE$ in0_$HLS_SNAME$_TDATA; +input in0_$HLS_SNAME$_TVALID; +output in0_$HLS_SNAME$_TREADY; +output $OUT_RANGE$ out_$HLS_SNAME$_TDATA; +output out_$HLS_SNAME$_TVALID; +input out_$HLS_SNAME$_TREADY; reg [31:0] config_address = 0; reg config_ce = 0; @@ -198,15 +196,15 @@ MVA_Stream_U ( .ap_clk(ap_clk), //input .ap_rst_n(ap_rst_n), //input -.in0_V_V_TDATA(in0_V_V_TDATA), //$IN_RANGE$ input -.in0_V_V_TVALID(in0_V_V_TVALID), //input -.in0_V_V_TREADY(in0_V_V_TREADY), //output -.weights_V_V_TDATA(m_axis_0_tdata), //$WEIGHT_RANGE$ input -.weights_V_V_TVALID(m_axis_0_tvalid), //input -.weights_V_V_TREADY(m_axis_0_tready), //output -.out_V_V_TDATA(out_V_V_TDATA), //$OUT_RANGE$ output -.out_V_V_TVALID(out_V_V_TVALID), //output -.out_V_V_TREADY(out_V_V_TREADY) //input +.in0_$HLS_SNAME$_TDATA(in0_$HLS_SNAME$_TDATA), //$IN_RANGE$ input +.in0_$HLS_SNAME$_TVALID(in0_$HLS_SNAME$_TVALID), //input +.in0_$HLS_SNAME$_TREADY(in0_$HLS_SNAME$_TREADY), //output +.weights_$HLS_SNAME$_TDATA(m_axis_0_tdata), //$WEIGHT_RANGE$ input +.weights_$HLS_SNAME$_TVALID(m_axis_0_tvalid), //input +.weights_$HLS_SNAME$_TREADY(m_axis_0_tready), //output +.out_$HLS_SNAME$_TDATA(out_$HLS_SNAME$_TDATA), //$OUT_RANGE$ output +.out_$HLS_SNAME$_TVALID(out_$HLS_SNAME$_TVALID), //output +.out_$HLS_SNAME$_TREADY(out_$HLS_SNAME$_TREADY) //input ); endmodule @@ -248,6 +246,8 @@ set_property supported_families { \ kintex7l Production \ kintexu Production \ kintexuplus Production \ + versal Production \ + versalprime Production \ virtex7 Production \ virtexu Production \ virtexuplus Production \ @@ -301,10 +301,10 @@ ipx::add_ports_from_hdl \ ## Infer interfaces ipx::infer_bus_interface ap_clk xilinx.com:signal:clock_rtl:1.0 [ipx::current_core] ipx::infer_bus_interface ap_rst_n xilinx.com:signal:reset_rtl:1.0 [ipx::current_core] -ipx::infer_bus_interface {in0_V_V_TDATA in0_V_V_TVALID in0_V_V_TREADY} xilinx.com:interface:axis_rtl:1.0 [ipx::current_core] -ipx::infer_bus_interface {out_V_V_TREADY out_V_V_TDATA out_V_V_TVALID} xilinx.com:interface:axis_rtl:1.0 [ipx::current_core] -ipx::associate_bus_interfaces -busif in0_V_V -clock ap_clk [ipx::current_core] -ipx::associate_bus_interfaces -busif out_V_V -clock ap_clk [ipx::current_core] +ipx::infer_bus_interface {in0_$HLS_SNAME$_TDATA in0_$HLS_SNAME$_TVALID in0_$HLS_SNAME$_TREADY} xilinx.com:interface:axis_rtl:1.0 [ipx::current_core] +ipx::infer_bus_interface {out_$HLS_SNAME$_TREADY out_$HLS_SNAME$_TDATA out_$HLS_SNAME$_TVALID} xilinx.com:interface:axis_rtl:1.0 [ipx::current_core] +ipx::associate_bus_interfaces -busif in0_$HLS_SNAME$ -clock ap_clk [ipx::current_core] +ipx::associate_bus_interfaces -busif out_$HLS_SNAME$ -clock ap_clk [ipx::current_core] ## Finalize set_property core_revision 2 [ipx::current_core] @@ -319,23 +319,23 @@ module $TOPNAME$( ap_clk, ap_rst_n, count, -in0_V_V_TDATA, -in0_V_V_TVALID, -in0_V_V_TREADY, -out_V_V_TDATA, -out_V_V_TVALID, -out_V_V_TREADY +in0_$HLS_SNAME$_TDATA, +in0_$HLS_SNAME$_TVALID, +in0_$HLS_SNAME$_TREADY, +out_$HLS_SNAME$_TDATA, +out_$HLS_SNAME$_TVALID, +out_$HLS_SNAME$_TREADY ); input ap_clk; input ap_rst_n; output $COUNT_RANGE$ count; -input $IN_RANGE$ in0_V_V_TDATA; -input in0_V_V_TVALID; -output in0_V_V_TREADY; -output $OUT_RANGE$ out_V_V_TDATA; -output out_V_V_TVALID; -input out_V_V_TREADY; +input $IN_RANGE$ in0_$HLS_SNAME$_TDATA; +input in0_$HLS_SNAME$_TVALID; +output in0_$HLS_SNAME$_TREADY; +output $OUT_RANGE$ out_$HLS_SNAME$_TDATA; +output out_$HLS_SNAME$_TVALID; +input out_$HLS_SNAME$_TREADY; Q_srl #( .depth($DEPTH$), @@ -346,12 +346,12 @@ $LAYER_NAME$ .clock(ap_clk), .reset(!ap_rst_n), .count(count), - .i_d(in0_V_V_TDATA), - .i_v(in0_V_V_TVALID), - .i_r(in0_V_V_TREADY), - .o_d(out_V_V_TDATA), - .o_v(out_V_V_TVALID), - .o_r(out_V_V_TREADY) + .i_d(in0_$HLS_SNAME$_TDATA), + .i_v(in0_$HLS_SNAME$_TVALID), + .i_r(in0_$HLS_SNAME$_TREADY), + .o_d(out_$HLS_SNAME$_TDATA), + .o_v(out_$HLS_SNAME$_TVALID), + .o_r(out_$HLS_SNAME$_TREADY) ); endmodule diff --git a/src/finn/custom_op/fpgadataflow/thresholding_batch.py b/src/finn/custom_op/fpgadataflow/thresholding_batch.py index 610139f44ee7e8be1320b47c99222667fa6ed850..173882bf929611f6cd9f560f48a46dfe09430622 100644 --- a/src/finn/custom_op/fpgadataflow/thresholding_batch.py +++ b/src/finn/custom_op/fpgadataflow/thresholding_batch.py @@ -389,7 +389,7 @@ class Thresholding_Batch(HLSCustomOp): tdt_hls, odt_hls, self.get_nodeattr("ActVal"), - "comp::less_equal<%s>" % tdt_hls, + "comp::less_equal<%s, %s>" % (tdt_hls, tdt_hls), ) ) f_thresh.write(thresholds_hls_code) @@ -589,7 +589,7 @@ class Thresholding_Batch(HLSCustomOp): # TODO check and add whatever missing def defines(self, var): numInputVectors = list(self.get_nodeattr("numInputVectors")) - numReps = numInputVectors[0] + numReps = int(np.prod(numInputVectors)) self.code_gen_dict["$DEFINES$"] = [ """#define NumChannels1 {}\n #define PE1 {}\n #define numReps {}""".format( self.get_nodeattr("NumChannels"), @@ -660,34 +660,25 @@ class Thresholding_Batch(HLSCustomOp): # TODO: why put some template parameters into defines and not others? # should ImgDim be defined or just filled in here like we do now? node = self.onnx_node - ishape = self.get_folded_input_shape() - if len(ishape) == 3: - imgdimh = 1 - imgdimw = 1 - elif len(ishape) == 5: - imgdimh = ishape[1] - imgdimw = ishape[2] - else: - raise Exception("""Unexpected input shape""") + inp_vecs = self.get_nodeattr("numInputVectors") + total_spatial_size = int(np.prod(inp_vecs)) mem_mode = self.get_nodeattr("mem_mode") if mem_mode == "const": self.code_gen_dict["$DOCOMPUTE$"] = [ - """{}<{}, {}, NumChannels1, PE1, {}, {}> + """{}<{}, NumChannels1, PE1, {}, {}> (in0, out, threshs, numReps);""".format( node.op_type, - imgdimh, - imgdimw, + total_spatial_size, tmpl_args["TSrcI"], tmpl_args["TDstI"], ) ] elif mem_mode == "decoupled": self.code_gen_dict["$DOCOMPUTE$"] = [ - """{}<{}, {}, NumChannels1, PE1, {}, {}, ActVal1, ThresType1, NumSteps1> + """{}<{}, NumChannels1, PE1, {}, {}, ActVal1, ThresType1, NumSteps1> (in0, out, weights, numReps);""".format( "Thresholding_Stream_Batch", - imgdimh, - imgdimw, + total_spatial_size, tmpl_args["TSrcI"], tmpl_args["TDstI"], ) @@ -753,8 +744,12 @@ class Thresholding_Batch(HLSCustomOp): raise Exception("Unrecognized mem_mode") def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) @@ -805,7 +800,8 @@ class Thresholding_Batch(HLSCustomOp): ) elif self.get_nodeattr("mem_mode") == "decoupled": self.code_gen_dict["$PRAGMAS$"].append( - "#pragma HLS INTERFACE axis port=weights" + "#pragma HLS INTERFACE axis port=weights name=weights_" + + self.hls_sname() ) def code_generation_ipi(self): @@ -815,6 +811,7 @@ class Thresholding_Batch(HLSCustomOp): if mem_mode == "decoupled": node_name = self.onnx_node.name runtime_writable = self.get_nodeattr("runtime_writeable_weights") == 1 + sname = self.hls_sname() # create a hierarchy for this layer, with the same port names clk_name = self.get_verilog_top_module_intf_names()["clk"][0] rst_name = self.get_verilog_top_module_intf_names()["rst"][0] @@ -868,8 +865,8 @@ class Thresholding_Batch(HLSCustomOp): ) cmd.append( "connect_bd_intf_net [get_bd_intf_pins %s/%s/m_axis_0] " - "[get_bd_intf_pins %s/%s/weights_V_V]" - % (node_name, strm_inst, node_name, node_name) + "[get_bd_intf_pins %s/%s/weights_%s]" + % (node_name, strm_inst, node_name, node_name, sname) ) cmd.append( "connect_bd_net [get_bd_pins %s/%s] [get_bd_pins %s/%s/aresetn]" @@ -940,3 +937,8 @@ class Thresholding_Batch(HLSCustomOp): thres_count = out_features * num_steps ret_dict[thres_param_type] = thres_count return ret_dict + + def ipgen_extra_directives(self): + "Return a list of extra tcl directives for HLS synthesis." + + return ["config_compile -pipeline_style frp"] diff --git a/src/finn/custom_op/fpgadataflow/tlastmarker.py b/src/finn/custom_op/fpgadataflow/tlastmarker.py index 70edaee9cfc0662411d005325e781f13b4f1b510..7386aa7e6311754b653e94f8d2e9b2a910a1370b 100644 --- a/src/finn/custom_op/fpgadataflow/tlastmarker.py +++ b/src/finn/custom_op/fpgadataflow/tlastmarker.py @@ -198,8 +198,12 @@ class TLastMarker(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) dyn_iters = self.get_nodeattr("DynIters") if dyn_iters == 1: @@ -244,12 +248,9 @@ class TLastMarker(HLSCustomOp): def get_verilog_top_module_intf_names(self): intf_names = super().get_verilog_top_module_intf_names() stream_width = self.get_nodeattr("StreamWidth") - if self.get_nodeattr("Direction") == "in": - intf_names["s_axis"] = [("in0", stream_width)] - intf_names["m_axis"] = [("out_V_V", stream_width)] - else: - intf_names["s_axis"] = [("in0_V_V", stream_width)] - intf_names["m_axis"] = [("out_r", stream_width)] + sname = self.hls_sname() + intf_names["s_axis"] = [("in0_" + sname, stream_width)] + intf_names["m_axis"] = [("out_" + sname, stream_width)] if self.get_nodeattr("DynIters") == 1: intf_names["axilite"] = ["s_axi_control"] return intf_names diff --git a/src/finn/custom_op/fpgadataflow/upsampler.py b/src/finn/custom_op/fpgadataflow/upsampler.py index 7114cd83ed08b53eab2cfe38d98d84944d537168..221725d49440653c5e56287f0d910848ec0b24c5 100644 --- a/src/finn/custom_op/fpgadataflow/upsampler.py +++ b/src/finn/custom_op/fpgadataflow/upsampler.py @@ -231,8 +231,12 @@ class UpsampleNearestNeighbour_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) self.code_gen_dict["$PRAGMAS$"].append( "#pragma HLS INTERFACE ap_ctrl_none port=return" ) diff --git a/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py b/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py index f50c5d1ef61d1677d2c2e394c43ebd6354a5331e..3d8dcaf2fca52b6c23b10322e0061b580807e0bc 100644 --- a/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py +++ b/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py @@ -379,7 +379,7 @@ class Vector_Vector_Activate_Batch(HLSCustomOp): tdt_hls, odt_hls, self.get_nodeattr("ActVal"), - "comp::less_equal<%s>" % tdt_hls, + "comp::less_equal<%s, %s>" % (tdt_hls, tdt_hls), ) ) f_thresh.write(thresholds_hls_code) @@ -579,8 +579,12 @@ class Vector_Vector_Activate_Batch(HLSCustomOp): ] def pragmas(self): - self.code_gen_dict["$PRAGMAS$"] = ["#pragma HLS INTERFACE axis port=in0"] - self.code_gen_dict["$PRAGMAS$"].append("#pragma HLS INTERFACE axis port=out") + self.code_gen_dict["$PRAGMAS$"] = [ + "#pragma HLS INTERFACE axis port=in0 name=in0_" + self.hls_sname() + ] + self.code_gen_dict["$PRAGMAS$"].append( + "#pragma HLS INTERFACE axis port=out name=out_" + self.hls_sname() + ) in_fifo_depth = self.get_nodeattr("inFIFODepth") out_fifo_depth = self.get_nodeattr("outFIFODepth") # insert depth pragmas only if specified diff --git a/src/finn/qnn-data/mdd-data/finn_design.mdd b/src/finn/qnn-data/mdd-data/finn_design.mdd new file mode 100644 index 0000000000000000000000000000000000000000..517180fa94079ad3e04d3a45776f165fd82cc483 --- /dev/null +++ b/src/finn/qnn-data/mdd-data/finn_design.mdd @@ -0,0 +1,36 @@ +# Copyright (c) 2022 Advanced Micro Devices, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# * Neither the name of Advanced Micro Devices nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +OPTION psf_version = 2.1; + +BEGIN driver finn_design + OPTION supported_peripherals = (finn_design); + OPTION driver_state = ACTIVE; + OPTION VERSION = 1.0; + OPTION NAME = finn_design; +END driver diff --git a/src/finn/qnn-data/mdd-data/finn_design.tcl b/src/finn/qnn-data/mdd-data/finn_design.tcl new file mode 100644 index 0000000000000000000000000000000000000000..b8c55e12b22a2152157cbecd2b0b4bf061e9918a --- /dev/null +++ b/src/finn/qnn-data/mdd-data/finn_design.tcl @@ -0,0 +1,58 @@ +# Copyright (c) 2022 Advanced Micro Devices, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# * Neither the name of Advanced Micro Devices nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# @brief Address range defines for FINN IP. +# @author Thomas B. Preußer <thomas.preusser@amd.com> +## + +proc generate {drv_handle} { + # Bounds of all exposed slave address ranges to xparameters.h + set file_handle [hsi::utils::open_include_file "xparameters.h"] + generate_memrange_parameters $drv_handle $file_handle + close $file_handle +} + +proc generate_memrange_parameters {drv_handle file_handle} { + # Collect unique slave interfaces to custom module + array unset ranges + foreach mem_range [hsi::get_mem_ranges -of_object [hsi::get_cells -hier [hsi::get_sw_processor]] $drv_handle] { + set ranges([common::get_property SLAVE_INTERFACE $mem_range]) [list \ + [common::get_property BASE_NAME $mem_range] \ + [common::get_property BASE_VALUE $mem_range] \ + [common::get_property HIGH_NAME $mem_range] \ + [common::get_property HIGH_VALUE $mem_range] \ + ] + } + + # Produce defines for the address range bounds + set prefix "XPAR_[string toupper $drv_handle]" + foreach {key val} [array get ranges] { + puts $file_handle "#define [format "%s_%s_%s" $prefix $key [lindex $val 0]] [lindex $val 1]" + puts $file_handle "#define [format "%s_%s_%s" $prefix $key [lindex $val 2]] [lindex $val 3]" + } + puts $file_handle "" +} diff --git a/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py b/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py index b2f50b1a23f85bf782c553057148173b6f94dde4..4ea5abdc0d142eb510ada8de83bfb61a84287352 100644 --- a/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py +++ b/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py @@ -197,15 +197,15 @@ class InferConvInpGen(Transformation): depthwise=depthwise, name="ConvolutionInputGenerator_" + n.name, ) - else: # non-square images and/or kernels + else: # 1D images and/or kernels assert is_1d_convolution, ( "%s: ConvolutionInputGenerator1D works only for 1D convs" % n.name ) if dilation_h > 1 or dilation_w > 1: - assert stride_h == 1 and stride_w == 1, ( - """%s: Stride value of greater than 1 is not supported for convolutions - with dilation value greater than 1""" + assert depthwise == 1, ( + """%s: Dilation value > 1 is only supported for + 1D depthwise separable convolutions""" % n.name ) ConvInpGen_node = helper.make_node( @@ -339,20 +339,27 @@ class InferStreamingMaxPool(Transformation): graph = model.graph node_ind = 0 graph_modified = False - for n in graph.node: + for node in graph.node: node_ind += 1 - if n.op_type == "MaxPoolNHWC": - mp_input = n.input[0] - mp_output = n.output[0] + if node.op_type == "MaxPoolNHWC": + mp_input = node.input[0] + mp_output = node.output[0] mp_in_shape = model.get_tensor_shape(mp_input) # mp_out_shape = model.get_tensor_shape(mp_output) dt = model.get_tensor_datatype(mp_input) - mp_inst = getCustomOp(n) + mp_inst = getCustomOp(node) k_h, k_w = mp_inst.get_nodeattr("kernel_shape") ifm_ch = mp_in_shape[-1] ifm_dim_h = mp_in_shape[1] ifm_dim_w = mp_in_shape[2] - if ifm_dim_h % k_h == 0 and ifm_dim_w % k_w == 0: + pe = 1 + ceil_mode = mp_inst.get_nodeattr("ceil_mode") + is_1d = (ifm_dim_h == 1 and k_h == 1) or (ifm_dim_w == 1 and k_w == 1) + is_divisable = (ifm_dim_h % k_h == 0) or (ifm_dim_w % k_w == 0) + is_bipolar = dt == DataType["BIPOLAR"] + pass_1d = is_1d and (not is_bipolar) + pass_2d = (not is_1d) and is_divisable + if pass_1d or pass_2d: # create equivalent StreamingMaxPool_Batch node new_node = helper.make_node( "StreamingMaxPool_Batch", @@ -364,12 +371,16 @@ class InferStreamingMaxPool(Transformation): NumChannels=ifm_ch, ImgDim=(ifm_dim_h, ifm_dim_w), dataType=dt.name, - name="StreamingMaxPool_Batch_" + n.name, + PE=pe, + CeilMode=ceil_mode, + name="StreamingMaxPool_Batch_" + node.name, ) graph.node.insert(node_ind, new_node) # remove old nodes - graph.node.remove(n) + graph.node.remove(node) graph_modified = True + else: + warnings.warn(node.name + ": could not convert to HLS") if graph_modified: model = model.transform(InferShapes()) model = model.transform(InferDataTypes()) diff --git a/src/finn/transformation/fpgadataflow/create_stitched_ip.py b/src/finn/transformation/fpgadataflow/create_stitched_ip.py index ecaf4f4d194e57f20a6af186dfaccdad5ab2a686..b19ef170f4547747d196978d08b8eacc7963d1ce 100644 --- a/src/finn/transformation/fpgadataflow/create_stitched_ip.py +++ b/src/finn/transformation/fpgadataflow/create_stitched_ip.py @@ -26,11 +26,14 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pkg_resources as pk + import json import multiprocessing as mp import os import subprocess import warnings +from shutil import copytree from finn.custom_op.registry import getCustomOp from finn.transformation.base import Transformation @@ -61,7 +64,9 @@ def is_external_output(model, node, i): # indicate whether output i of node should be made external # True only if output is unconnected consumers = model.find_consumers(node.output[i]) - if consumers is None: + if consumers == []: + # TODO should ideally check if tensor is in top-level + # outputs return True return False @@ -160,6 +165,16 @@ class CreateStitchedIP(Transformation): self.connect_cmds.append( "set_property name m_axi_gmem0 [get_bd_intf_ports m_axi_gmem_0]" ) + self.connect_cmds.append("assign_bd_address") + seg_name = "%s/Data_m_axi_gmem/SEG_m_axi_gmem0_Reg" % (inst_name) + self.connect_cmds.append( + "set_property offset 0 [get_bd_addr_segs {%s}]" % (seg_name) + ) + # TODO should propagate this information from the node instead of 4G + self.connect_cmds.append( + "set_property range 4G [get_bd_addr_segs {%s}]" % (seg_name) + ) + self.intf_names["aximm"] = [("m_axi_gmem0", aximm_intf_name[0][1])] assert self.has_aximm is False, "Currently limited to one AXI-MM interface" self.has_aximm = True @@ -215,7 +230,7 @@ class CreateStitchedIP(Transformation): model = model.transform(ReplaceVerilogRelPaths()) ip_dirs = ["list"] # add RTL streamer IP - ip_dirs.append("/workspace/finn/finn-rtllib/memstream") + ip_dirs.append("$::env(FINN_ROOT)/finn/finn-rtllib/memstream") if model.graph.node[0].op_type not in ["StreamingFIFO", "IODMA"]: warnings.warn( """First node is not StreamingFIFO or IODMA. @@ -257,7 +272,7 @@ class CreateStitchedIP(Transformation): for input in model.graph.input: inp_name = input.name inp_cons = model.find_consumers(inp_name) - assert inp_cons is not None, "No consumer for input " + inp_name + assert inp_cons != [], "No consumer for input " + inp_name assert len(inp_cons) == 1, "Multiple consumers for input " + inp_name node = inp_cons[0] node_inst = getCustomOp(node) @@ -432,6 +447,21 @@ class CreateStitchedIP(Transformation): "ipx::add_file dcp/%s.dcp " "[ipx::get_file_groups xilinx_simulationcheckpoint]" % block_name ) + # add a rudimentary driver mdd to get correct ranges in xparameters.h later on + example_data_dir = pk.resource_filename("finn.qnn-data", "mdd-data/") + copytree(example_data_dir, vivado_stitch_proj_dir + "/data") + tcl.append("file copy -force data ip/") + tcl.append("ipx::add_file_group -type software_driver {} [ipx::current_core]") + tcl.append( + "set_property type mdd [ipx::add_file data/finn_design.mdd " + "[ipx::get_file_groups xilinx_softwaredriver -of_objects " + "[ipx::current_core]]]" + ) + tcl.append( + "set_property type tclSource [ipx::add_file data/finn_design.tcl " + "[ipx::get_file_groups xilinx_softwaredriver -of_objects " + "[ipx::current_core]]]" + ) tcl.append("ipx::update_checksums [ipx::find_open_core %s]" % block_vlnv) tcl.append("ipx::save_core [ipx::find_open_core %s]" % block_vlnv) # export list of used Verilog files (for rtlsim later on) @@ -459,4 +489,13 @@ class CreateStitchedIP(Transformation): bash_command = ["bash", make_project_sh] process_compile = subprocess.Popen(bash_command, stdout=subprocess.PIPE) process_compile.communicate() + # wrapper may be created in different location depending on Vivado version + if not os.path.isfile(wrapper_filename): + # check in alternative location (.gen instead of .srcs) + wrapper_filename_alt = wrapper_filename.replace(".srcs", ".gen") + if os.path.isfile(wrapper_filename_alt): + model.set_metadata_prop("wrapper_filename", wrapper_filename_alt) + else: + raise Exception("CreateStitchedIP failed, no wrapper HDL found.") + return (model, False) diff --git a/src/finn/transformation/fpgadataflow/insert_dwc.py b/src/finn/transformation/fpgadataflow/insert_dwc.py index 4a0d0a89c4a6bb5809887ffcfffb2068ccebaa48..afc889f5bc90a551efddc6232f5689504fe1bb29 100644 --- a/src/finn/transformation/fpgadataflow/insert_dwc.py +++ b/src/finn/transformation/fpgadataflow/insert_dwc.py @@ -45,7 +45,7 @@ class InsertDWC(Transformation): if _suitable_node(n): for output_name in n.output: consumers = model.find_consumers(output_name) - if consumers is None: + if consumers == []: continue assert len(consumers) == 1, ( n.name diff --git a/src/finn/transformation/fpgadataflow/insert_fifo.py b/src/finn/transformation/fpgadataflow/insert_fifo.py index b5ae2da47a19af5b6bbf44a2a65cbef4c3bbc4dd..26613849060e361a6bc93483e3e1d8416e1fd97f 100644 --- a/src/finn/transformation/fpgadataflow/insert_fifo.py +++ b/src/finn/transformation/fpgadataflow/insert_fifo.py @@ -62,7 +62,7 @@ class InsertFIFO(Transformation): if _suitable_node(first_node): for n_output in first_node.output: consumers = model.find_consumers(n_output) - if consumers is None: + if consumers == []: continue if len(consumers) > 1: warnings.warn( diff --git a/src/finn/transformation/fpgadataflow/insert_tlastmarker.py b/src/finn/transformation/fpgadataflow/insert_tlastmarker.py index 34cb61346dcd5bcd6f41a4272748764cf385a524..a33cee4640a7498f478962767ac4260d9c2bed90 100644 --- a/src/finn/transformation/fpgadataflow/insert_tlastmarker.py +++ b/src/finn/transformation/fpgadataflow/insert_tlastmarker.py @@ -97,7 +97,7 @@ class InsertTLastMarker(Transformation): first_node = model.find_consumers(graph_in_name) # skip if no consumers (this may be the case for unused initializers) # TODO: fix this with a cleanup transform - if first_node is None: + if first_node == []: continue assert len(first_node) == 1, "Input fans out to multiple nodes" first_node = first_node[0] diff --git a/src/finn/transformation/fpgadataflow/make_zynq_proj.py b/src/finn/transformation/fpgadataflow/make_zynq_proj.py index 84d587b6cecea63cb3be41a4a73bcc24aeb822f3..0b92f1777373a78cf09466dc3aea6a2802ec98fe 100644 --- a/src/finn/transformation/fpgadataflow/make_zynq_proj.py +++ b/src/finn/transformation/fpgadataflow/make_zynq_proj.py @@ -68,7 +68,7 @@ def collect_ip_dirs(model, ipstitch_path): ip_dirs += [ipstitch_path + "/ip"] if need_memstreamer: # add RTL streamer IP - ip_dirs.append("/workspace/finn/finn-rtllib/memstream") + ip_dirs.append("$::env(FINN_ROOT)/finn/finn-rtllib/memstream") return ip_dirs @@ -152,11 +152,13 @@ class MakeZYNQProject(Transformation): # define kernel instances # name kernels connected to graph inputs as idmaxx # name kernels connected to graph outputs as odmaxx - if producer is None or consumer is None: + if (producer is None) or (consumer == []): + # TODO not a good way of checking for external inp&out + # should look at the list of top-level in/out instead if producer is None: instance_names[node.name] = "idma" + str(idma_idx) idma_idx += 1 - elif consumer is None: + elif consumer == []: instance_names[node.name] = "odma" + str(odma_idx) odma_idx += 1 config.append( @@ -279,10 +281,16 @@ class MakeZYNQProject(Transformation): copy(bitfile_name, deploy_bitfile_name) # set bitfile attribute model.set_metadata_prop("bitfile", deploy_bitfile_name) - hwh_name = ( + hwh_name_alts = [ vivado_pynq_proj_dir - + "/finn_zynq_link.srcs/sources_1/bd/top/hw_handoff/top.hwh" - ) + + "/finn_zynq_link.srcs/sources_1/bd/top/hw_handoff/top.hwh", + vivado_pynq_proj_dir + + "/finn_zynq_link.gen/sources_1/bd/top/hw_handoff/top.hwh", + ] + hwh_name = None + for hwh_name_cand in hwh_name_alts: + if os.path.isfile(hwh_name_cand): + hwh_name = hwh_name_cand if not os.path.isfile(hwh_name): raise Exception( "Synthesis failed, no bitfile found. Check logs under %s" diff --git a/src/finn/transformation/fpgadataflow/set_fifo_depths.py b/src/finn/transformation/fpgadataflow/set_fifo_depths.py index ce7cf7bc589fae7fb6c8785b51cf45514f49c5a0..28f74b5292d14947f4f8a27f2723af4f04590ec8 100644 --- a/src/finn/transformation/fpgadataflow/set_fifo_depths.py +++ b/src/finn/transformation/fpgadataflow/set_fifo_depths.py @@ -99,7 +99,7 @@ class RemoveShallowFIFOs(Transformation): # bypass shallow fifos shallow_fifos.append(node) consumers = model.find_consumers(node.output[0]) - if consumers is None: + if consumers == []: producer = model.find_producer(node.input[0]) for idx, inp in enumerate(producer.output): if inp == node.input[0]: diff --git a/src/finn/transformation/fpgadataflow/templates.py b/src/finn/transformation/fpgadataflow/templates.py index a12f359c7d3f1c29a17694ef4987a1a349286234..ba1d757b75ff46ef1f78075bc8f3fe07c11551c8 100644 --- a/src/finn/transformation/fpgadataflow/templates.py +++ b/src/finn/transformation/fpgadataflow/templates.py @@ -103,8 +103,8 @@ create_project finn_zynq_link ./ -part $FPGA_PART # set board part repo paths to find PYNQ-Z1/Z2 set paths_prop [get_property BOARD_PART_REPO_PATHS [current_project]] set paths_param [get_param board.repoPaths] -lappend paths_prop /workspace/board_files -lappend paths_param /workspace/board_files +lappend paths_prop $::env(FINN_ROOT)/board_files +lappend paths_param $::env(FINN_ROOT)/board_files set_property BOARD_PART_REPO_PATHS $paths_prop [current_project] set_param board.repoPaths $paths_param diff --git a/src/finn/transformation/fpgadataflow/vitis_build.py b/src/finn/transformation/fpgadataflow/vitis_build.py index 365632cd5a02eae6e19e670e0b676c521e460507..4dce3ab16c38bfe5dd43f3e23b14ea2ec571f68c 100644 --- a/src/finn/transformation/fpgadataflow/vitis_build.py +++ b/src/finn/transformation/fpgadataflow/vitis_build.py @@ -213,11 +213,13 @@ class VitisLink(Transformation): # define kernel instances # name kernels connected to graph inputs as idmaxx # name kernels connected to graph inputs as odmaxx + # TODO not a good way of checking for external in/out + # check top-level in/out list instead if producer is None: instance_names[node.name] = "idma" + str(idma_idx) config.append("nk=%s:1:%s" % (node.name, instance_names[node.name])) idma_idx += 1 - elif consumer is None: + elif consumer == []: instance_names[node.name] = "odma" + str(odma_idx) config.append("nk=%s:1:%s" % (node.name, instance_names[node.name])) odma_idx += 1 diff --git a/src/finn/transformation/qonnx/fold_quant_weights.py b/src/finn/transformation/qonnx/fold_quant_weights.py index 12c854d3bab2b762abc3649e15beff29ff8de3ac..e8a0f418ae5eb587d6aabae57d8b379357d3a0ca 100644 --- a/src/finn/transformation/qonnx/fold_quant_weights.py +++ b/src/finn/transformation/qonnx/fold_quant_weights.py @@ -103,7 +103,7 @@ class FoldQuantWeights(Transformation): model.set_initializer(node_out, q_node_output) else: # Check next operator type - mul_like_nodes = ["Mul", "Div", "Conv", "MatMul"] + mul_like_nodes = ["Mul", "Div", "Conv", "MatMul", "Gather"] add_like_nodes = ["Add", "Sub"] all_supported_ops = mul_like_nodes.copy() all_supported_ops.extend(add_like_nodes) @@ -146,11 +146,14 @@ class FoldQuantWeights(Transformation): model.set_initializer(mul_tensor.name, scale) successor = model.find_consumers(node_out) - if successor is None: + if successor == []: raise RuntimeError( "Can only constant fold scaled Quant weights " "if a successor exists." ) + assert ( + len(successor) == 1 + ), "Only implemented for a single consumer" successor = successor[0] succ_output_name = successor.output[0] diff --git a/src/finn/transformation/streamline/absorb.py b/src/finn/transformation/streamline/absorb.py index 97ae3b51a849a4174c9853cb41c0d6d72bdf8dad..32e539d87045520044378b94fd0e3c71486990c7 100644 --- a/src/finn/transformation/streamline/absorb.py +++ b/src/finn/transformation/streamline/absorb.py @@ -627,10 +627,9 @@ class AbsorbTransposeIntoResize(Transformation): graph.node.insert(node_ind + 1, new_transpose) # rewire nodes final_t_cands = model.find_consumers(mt_cand.output[0]) - if final_t_cands is not None: - # rewire next nodes' inputs - for final_t_cand in final_t_cands: - final_t_cand.input[0] = trans_output + # rewire next nodes' inputs + for final_t_cand in final_t_cands: + final_t_cand.input[0] = trans_output mt_cand.output[0] = trans_input graph_modified = True if graph_modified: diff --git a/src/finn/transformation/streamline/reorder.py b/src/finn/transformation/streamline/reorder.py index 0cdd6651d982426b1d81d7313346dcd899294bf7..e922dffe37691a39434e9ebafa5df6d1a11d389e 100644 --- a/src/finn/transformation/streamline/reorder.py +++ b/src/finn/transformation/streamline/reorder.py @@ -670,6 +670,13 @@ class MakeMaxPoolNHWC(Transformation): if consumer is not None and consumer.op_type == "Transpose": perms = list(get_by_name(consumer.attribute, "perm").ints) if perms == [0, 2, 3, 1]: + ceil_mode = get_by_name(n.attribute, "ceil_mode") + if ceil_mode is not None: + ceil_mode = ceil_mode.i + else: + ceil_mode = ( + 0 # default to ceil_mode=0 (equivalent to np.floor) + ) n.op_type = "MaxPoolNHWC" n.domain = "finn.custom_op.general" start_name = n.input[0] @@ -683,12 +690,20 @@ class MakeMaxPoolNHWC(Transformation): n.output[0] = end_name model.set_tensor_shape(mid_name, (b, hi, wi, c)) model.set_tensor_shape(end_name, (b, ho, wo, c)) + getCustomOp(n).set_nodeattr("ceil_mode", ceil_mode) graph.node.remove(consumer) graph.node.insert(node_ind - 1, consumer) graph_modified = True elif producer is not None and producer.op_type == "Transpose": perms = list(get_by_name(producer.attribute, "perm").ints) if perms == [0, 3, 1, 2]: + ceil_mode = get_by_name(n.attribute, "ceil_mode") + if ceil_mode is not None: + ceil_mode = ceil_mode.i + else: + ceil_mode = ( + 0 # default to ceil_mode=0 (equivalent to np.floor) + ) n.op_type = "MaxPoolNHWC" n.domain = "finn.custom_op.general" start_name = producer.input[0] @@ -702,6 +717,7 @@ class MakeMaxPoolNHWC(Transformation): n.output[0] = mid_name model.set_tensor_shape(mid_name, (b, ho, wo, c)) model.set_tensor_shape(end_name, (b, c, ho, wo)) + getCustomOp(n).set_nodeattr("ceil_mode", ceil_mode) graph.node.remove(producer) graph.node.insert(node_ind, producer) graph_modified = True @@ -739,6 +755,7 @@ class MoveOpPastFork(Transformation): # Check case when branches are empty and go # to the same node consumers = model.find_consumers(n.output[0]) + assert len(consumers) > 1, "Must have >1 consumer" unique_consumer = True for consum_node in consumers[1:]: if consumers[0] != consum_node: diff --git a/tests/brevitas/test_brevitas_avg_pool_export.py b/tests/brevitas/test_brevitas_avg_pool_export.py index 1b38914a83e7c5d68bb004df7545b518d6a93ddd..6d0c68f0f456c05ab60ffa043277409730b695ce 100644 --- a/tests/brevitas/test_brevitas_avg_pool_export.py +++ b/tests/brevitas/test_brevitas_avg_pool_export.py @@ -47,6 +47,7 @@ from finn.util.basic import gen_finn_dt_tensor base_export_onnx_path = "test_brevitas_avg_pool_export.onnx" +@pytest.mark.brevitas_export @pytest.mark.parametrize("QONNX_export", [False, True]) @pytest.mark.parametrize("kernel_size", [2, 3]) @pytest.mark.parametrize("stride", [1, 2]) diff --git a/tests/brevitas/test_brevitas_cnv.py b/tests/brevitas/test_brevitas_cnv.py index 78ca361366902b37f826b575904126c783adbece..2592d381173ee2112565f17d6631dd98f05e221a 100644 --- a/tests/brevitas/test_brevitas_cnv.py +++ b/tests/brevitas/test_brevitas_cnv.py @@ -47,7 +47,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_brevitas_cnv.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("abits", [1, 2]) @pytest.mark.parametrize("wbits", [1, 2]) @pytest.mark.parametrize("QONNX_export", [False, True]) diff --git a/tests/brevitas/test_brevitas_debug.py b/tests/brevitas/test_brevitas_debug.py index e42b93babefd9ca6a7a86def18a5cbb21d795c4c..3db1a208456f7209623530681d96d6aa35928900 100644 --- a/tests/brevitas/test_brevitas_debug.py +++ b/tests/brevitas/test_brevitas_debug.py @@ -47,6 +47,7 @@ from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN from finn.util.test import get_test_model_trained +@pytest.mark.brevitas_export @pytest.mark.parametrize("QONNX_export", [False, True]) @pytest.mark.parametrize("QONNX_FINN_conversion", [False, True]) def test_brevitas_debug(QONNX_export, QONNX_FINN_conversion): diff --git a/tests/brevitas/test_brevitas_fc.py b/tests/brevitas/test_brevitas_fc.py index 8e1e3de8d06b24ce946fb0a6726d875d0e75736e..fc0f24b9172eb7882197026420ede8fe5d69bee5 100644 --- a/tests/brevitas/test_brevitas_fc.py +++ b/tests/brevitas/test_brevitas_fc.py @@ -49,6 +49,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = make_build_dir("test_brevitas_fc_") +@pytest.mark.brevitas_export # act bits @pytest.mark.parametrize("abits", [1, 2]) # weight bits diff --git a/tests/brevitas/test_brevitas_mobilenet.py b/tests/brevitas/test_brevitas_mobilenet.py index 108c97c2e83b7f3ca9dd6ead746b3ef8b4d10af5..189ca1da6c2862db6239186c7eb234a992a66472 100644 --- a/tests/brevitas/test_brevitas_mobilenet.py +++ b/tests/brevitas/test_brevitas_mobilenet.py @@ -52,7 +52,7 @@ from finn.util.basic import make_build_dir from finn.util.pytorch import NormalizePreProc from finn.util.test import crop_center, get_test_model_trained, resize_smaller_side - +@pytest.mark.brevitas_export @pytest.mark.xfail def test_brevitas_mobilenet(): # get single image as input and prepare image diff --git a/tests/brevitas/test_brevitas_non_scaled_quanthardtanh_export.py b/tests/brevitas/test_brevitas_non_scaled_quanthardtanh_export.py index b530b4bd84c548319549a8b16e0c3a79584e075d..4f9d2778028223d85882839ef7243e170ef90dd6 100644 --- a/tests/brevitas/test_brevitas_non_scaled_quanthardtanh_export.py +++ b/tests/brevitas/test_brevitas_non_scaled_quanthardtanh_export.py @@ -47,7 +47,7 @@ from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN export_onnx_path = "test_brevitas_non_scaled_QuantHardTanh_export.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("abits", [1, 2, 4, 8]) @pytest.mark.parametrize("narrow_range", [False, True]) @pytest.mark.parametrize("max_val", [1.0, 1 - 2 ** (-7)]) diff --git a/tests/brevitas/test_brevitas_qconv2d.py b/tests/brevitas/test_brevitas_qconv2d.py index beaea4e51ecdd4cff9f0d4d0c16735cdecad207c..4d9bd14ae3500fd8c0e78e6c4d377ce1f234d168 100644 --- a/tests/brevitas/test_brevitas_qconv2d.py +++ b/tests/brevitas/test_brevitas_qconv2d.py @@ -49,7 +49,7 @@ from finn.util.basic import gen_finn_dt_tensor export_onnx_path = "test_brevitas_conv.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("dw", [False, True]) @pytest.mark.parametrize("bias", [True, False]) @pytest.mark.parametrize("in_channels", [32]) diff --git a/tests/brevitas/test_brevitas_qlinear.py b/tests/brevitas/test_brevitas_qlinear.py index 1099d3ec83336e5cd07707b35baea112b7a2aee6..e78262fcb24a1fec1fa876a39c67bd3aa850299c 100644 --- a/tests/brevitas/test_brevitas_qlinear.py +++ b/tests/brevitas/test_brevitas_qlinear.py @@ -46,7 +46,7 @@ from finn.util.basic import gen_finn_dt_tensor export_onnx_path = "test_brevitas_qlinear.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("bias", [False, True]) @pytest.mark.parametrize("out_features", [4]) @pytest.mark.parametrize("in_features", [3]) diff --git a/tests/brevitas/test_brevitas_relu_act_export.py b/tests/brevitas/test_brevitas_relu_act_export.py index 57ead3b6c047220e90d4276620cc14b8f795fe08..01ba7f382535ea8a12a60f211b7718ca57164db4 100644 --- a/tests/brevitas/test_brevitas_relu_act_export.py +++ b/tests/brevitas/test_brevitas_relu_act_export.py @@ -47,7 +47,7 @@ from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN export_onnx_path = "test_brevitas_relu_act_export.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("abits", [2, 4, 8]) @pytest.mark.parametrize("max_val", [1.0, 1.5, 1 - 2 ** (-7)]) @pytest.mark.parametrize( @@ -110,7 +110,7 @@ scaling_impl.learned_value": torch.tensor( assert np.isclose(produced, expected, atol=1e-3).all() os.remove(export_onnx_path) - +@pytest.mark.brevitas_export @pytest.mark.parametrize("abits", [2, 4, 8]) @pytest.mark.parametrize("max_val", [1.0, 1.5, 1 - 2 ** (-7)]) @pytest.mark.parametrize("scaling_per_channel", [True, False]) diff --git a/tests/brevitas/test_brevitas_scaled_qhardtanh_export.py b/tests/brevitas/test_brevitas_scaled_qhardtanh_export.py index c6da2e2e971ee97cb73243284920cc87e8b4d7bb..9f17c0f26c42058f314a25c066c8ba37a06e0b65 100644 --- a/tests/brevitas/test_brevitas_scaled_qhardtanh_export.py +++ b/tests/brevitas/test_brevitas_scaled_qhardtanh_export.py @@ -47,7 +47,7 @@ from finn.transformation.qonnx.convert_qonnx_to_finn import ConvertQONNXtoFINN export_onnx_path = "test_brevitas_scaled_QHardTanh_export.onnx" - +@pytest.mark.brevitas_export @pytest.mark.parametrize("abits", [2, 4, 8]) @pytest.mark.parametrize("narrow_range", [False, True]) @pytest.mark.parametrize("min_val", [-1.0, -(1 - 2 ** (-7)), -2]) diff --git a/tests/brevitas/test_brevitas_validate_mobilenet.py b/tests/brevitas/test_brevitas_validate_mobilenet.py index 12e7e7aff2ec2ebae3e2ec7713a24046553dc5f2..67e6b785a70c81717adadd3d2695017e0382edda 100644 --- a/tests/brevitas/test_brevitas_validate_mobilenet.py +++ b/tests/brevitas/test_brevitas_validate_mobilenet.py @@ -61,7 +61,7 @@ mean = [0.485, 0.456, 0.406] std = 0.226 ch = 3 - +@pytest.mark.brevitas_export def test_brevitas_mobilenet_preproc(): if "IMAGENET_VAL_PATH" not in os.environ.keys(): pytest.skip("Can't do validation without IMAGENET_VAL_PATH") @@ -98,6 +98,7 @@ def test_brevitas_mobilenet_preproc(): assert (finn_img == pyt_img).all() +@pytest.mark.brevitas_export @pytest.mark.slow # marked as XFAIL until Brevitas export issues are resolved: # https://github.com/Xilinx/brevitas/issues/173 diff --git a/tests/end2end/test_end2end_bnn_pynq.py b/tests/end2end/test_end2end_bnn_pynq.py index 1d7d5e3e9a939b62a938c6f23e347e3d15a64663..b74875e10fc8c539b7a5f3eced5f1f11df3b5f94 100644 --- a/tests/end2end/test_end2end_bnn_pynq.py +++ b/tests/end2end/test_end2end_bnn_pynq.py @@ -206,8 +206,8 @@ def fold_cnv_small(model): fc_layers = model.get_nodes_by_op_type("StreamingFCLayer_Batch") # each tuple is (PE, SIMD) for a layer folding = [ - (8, 3, "auto"), - (16, 16, "auto"), + (8, 3, "distributed"), + (16, 16, "distributed"), (8, 16, "auto"), (8, 16, "block"), (4, 8, "auto"), diff --git a/tests/fpgadataflow/test_code_gen_trafo.py b/tests/fpgadataflow/test_code_gen_trafo.py index 5ddff3d36f03d17833e17bc98649a64dabf31577..837173b6772ce968c0c618b40e23f6c0f810015a 100644 --- a/tests/fpgadataflow/test_code_gen_trafo.py +++ b/tests/fpgadataflow/test_code_gen_trafo.py @@ -37,6 +37,7 @@ from finn.core.modelwrapper import ModelWrapper from finn.transformation.fpgadataflow.prepare_cppsim import PrepareCppSim +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_code_gen_trafo(): idt = wdt = odt = DataType["BIPOLAR"] diff --git a/tests/fpgadataflow/test_compilation_trafo.py b/tests/fpgadataflow/test_compilation_trafo.py index 81e2ff9a7c5829982cdb6121378e9e9e3af81632..e36bce7e9abc8c5d8f815e4559cbce52ca186934 100644 --- a/tests/fpgadataflow/test_compilation_trafo.py +++ b/tests/fpgadataflow/test_compilation_trafo.py @@ -38,6 +38,7 @@ from finn.transformation.fpgadataflow.compile_cppsim import CompileCppSim from finn.transformation.fpgadataflow.prepare_cppsim import PrepareCppSim +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_compilation_trafo(): idt = wdt = odt = DataType["BIPOLAR"] diff --git a/tests/fpgadataflow/test_convert_to_hls_1d_conv_layer.py b/tests/fpgadataflow/test_convert_to_hls_1d_conv_layer.py index 5cc5f8fa6c1ccd3e5a9e154b6fb2773caf4668a9..95c340694a71eb52c0f8dd4b00e06df244f4d651 100644 --- a/tests/fpgadataflow/test_convert_to_hls_1d_conv_layer.py +++ b/tests/fpgadataflow/test_convert_to_hls_1d_conv_layer.py @@ -67,6 +67,7 @@ from finn.util.basic import gen_finn_dt_tensor ) @pytest.mark.parametrize("depthwise", [False, True]) @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_convert_to_hls_1d_conv_layer(conv_config, depthwise, exec_mode): diff --git a/tests/fpgadataflow/test_convert_to_hls_channelwise_layer.py b/tests/fpgadataflow/test_convert_to_hls_channelwise_layer.py index bf690d1d68bc0f580663735c3596c1dfc0a651e8..946b748e583297c2e2fa52d73fed5f13fcba14ab 100644 --- a/tests/fpgadataflow/test_convert_to_hls_channelwise_layer.py +++ b/tests/fpgadataflow/test_convert_to_hls_channelwise_layer.py @@ -89,6 +89,7 @@ def make_single_maxpool_modelwrapper(onnx_op_name, ishape, idt, pdt, pshape): @pytest.mark.parametrize("scalar_param", [True, False]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_convert_to_hls_channelwise_layer( diff --git a/tests/fpgadataflow/test_convert_to_hls_conv_fc_transition.py b/tests/fpgadataflow/test_convert_to_hls_conv_fc_transition.py index 9b0f3d68aed655f0b36857d50a085093ea94aecb..005ec40288bed58d62993e99a84e3ca63cdfa679 100755 --- a/tests/fpgadataflow/test_convert_to_hls_conv_fc_transition.py +++ b/tests/fpgadataflow/test_convert_to_hls_conv_fc_transition.py @@ -75,6 +75,7 @@ def get_multithreshold_rand_params(channels, num_of_thres, seed=None): ) @pytest.mark.parametrize("depthwise", [False, True]) @pytest.mark.parametrize("use_reshape", [False, True]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_convert_to_hls_conv_fc_transition(conv_config, depthwise, use_reshape): diff --git a/tests/fpgadataflow/test_convert_to_hls_conv_layer.py b/tests/fpgadataflow/test_convert_to_hls_conv_layer.py index d96bc987567cdcfcd18a404986c954c7527c7354..cc1bac1ed199ba5b4eabcb4535f329772ea1ce35 100644 --- a/tests/fpgadataflow/test_convert_to_hls_conv_layer.py +++ b/tests/fpgadataflow/test_convert_to_hls_conv_layer.py @@ -58,6 +58,7 @@ from finn.util.basic import gen_finn_dt_tensor ) @pytest.mark.parametrize("depthwise", [False, True]) @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_convert_to_hls_conv_layer(conv_config, depthwise, exec_mode): diff --git a/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py b/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py index 3357ee6d6c1e540818549f2d0df8b8554690ca3c..292a2c8f7a7e0af757e7967e51204db81f79767c 100644 --- a/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py +++ b/tests/fpgadataflow/test_convert_to_hls_layers_cnv.py @@ -55,6 +55,7 @@ from finn.util.test import get_test_model_trained export_onnx_path_cnv = "test_convert_to_hls_layers_cnv.onnx" +@pytest.mark.fpgadataflow @pytest.mark.vivado # Standalone or fused thresholding-based activation @pytest.mark.parametrize("fused_activation", [True, False]) diff --git a/tests/fpgadataflow/test_convert_to_hls_layers_fc.py b/tests/fpgadataflow/test_convert_to_hls_layers_fc.py index a1dc11e0eee5aab462beb0ec34b8771ced20a379..f5e069a3e5486ee1771f6417a93dbafecaaa77d7 100644 --- a/tests/fpgadataflow/test_convert_to_hls_layers_fc.py +++ b/tests/fpgadataflow/test_convert_to_hls_layers_fc.py @@ -55,6 +55,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_convert_to_hls_layers_fc.onnx" +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_convert_to_hls_layers_tfc_w1a1(): tfc = get_test_model_trained("TFC", 1, 1) @@ -125,6 +126,7 @@ def test_convert_to_hls_layers_tfc_w1a1(): os.remove(export_onnx_path) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_convert_to_hls_layers_tfc_w1a2(): tfc = get_test_model_trained("TFC", 1, 2) diff --git a/tests/fpgadataflow/test_convert_to_hls_layers_synthetic.py b/tests/fpgadataflow/test_convert_to_hls_layers_synthetic.py index 6089901566cb412e63cd8acc7a8260081248ba52..06b0367507ea5c9df4c8280090900bc20548c541 100644 --- a/tests/fpgadataflow/test_convert_to_hls_layers_synthetic.py +++ b/tests/fpgadataflow/test_convert_to_hls_layers_synthetic.py @@ -143,6 +143,7 @@ def make_model(ch, ifmdim): @pytest.mark.parametrize("ch", [16]) # ifmdim @pytest.mark.parametrize("ifmdim", [5]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_convert_to_hls_layers_synthetic(ch, ifmdim, idt): diff --git a/tests/fpgadataflow/test_convert_to_hls_pool_batch.py b/tests/fpgadataflow/test_convert_to_hls_pool_batch.py index 0dd9991b2ff07a35c923afeda854352213f8ca09..7595275c3be34e947f40415d050c0f3e4a9a7a58 100644 --- a/tests/fpgadataflow/test_convert_to_hls_pool_batch.py +++ b/tests/fpgadataflow/test_convert_to_hls_pool_batch.py @@ -140,6 +140,7 @@ def prepare_inputs(input_tensor): @pytest.mark.parametrize("op_type", ["QuantAvgPool2d", "MaxPool", "MaxPool1D"]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_convert_to_hls_pool_batch( diff --git a/tests/fpgadataflow/test_depthwise_convolution.py b/tests/fpgadataflow/test_depthwise_convolution.py index 633db668d3bc5de815a313743c06cd74a7166c9c..11b358da3e20ecafa6b575a961bc24e496942ad4 100644 --- a/tests/fpgadataflow/test_depthwise_convolution.py +++ b/tests/fpgadataflow/test_depthwise_convolution.py @@ -168,6 +168,7 @@ def set_up_reference_model(act, idt, wdt, k, ifm_dim, ifm_ch, stride, padding): @pytest.mark.parametrize("stride", [1, 2]) # padding @pytest.mark.parametrize("padding", [0, 1]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_depthwise_conv_hls_cppsim(act, pe, k, stride, padding): @@ -210,6 +211,7 @@ def test_depthwise_conv_hls_cppsim(act, pe, k, stride, padding): @pytest.mark.parametrize("stride", [1, 2]) # padding @pytest.mark.parametrize("padding", [0, 1]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_depthwise_conv_hls_rtlsim(act, pe, k, stride, padding): diff --git a/tests/fpgadataflow/test_fpgadataflow_addstreams.py b/tests/fpgadataflow/test_fpgadataflow_addstreams.py index 8cbf54ec188b12c67e02a33e3540718e9b08f382..a3927cd2aa6a9e87c32068f986ab6030fbacc559 100644 --- a/tests/fpgadataflow/test_fpgadataflow_addstreams.py +++ b/tests/fpgadataflow/test_fpgadataflow_addstreams.py @@ -89,6 +89,7 @@ def prepare_inputs(input1, input2): @pytest.mark.parametrize("fold", [-1, 2, 1]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_addstreams(idt, ch, fold, exec_mode): if fold == -1: diff --git a/tests/fpgadataflow/test_fpgadataflow_channelwise_ops.py b/tests/fpgadataflow/test_fpgadataflow_channelwise_ops.py index 949046d4ae313b852471e7d8a93e44fea48f7b0f..f774a4ff53c636419d8eb7dcfba866fd601f0c98 100644 --- a/tests/fpgadataflow/test_fpgadataflow_channelwise_ops.py +++ b/tests/fpgadataflow/test_fpgadataflow_channelwise_ops.py @@ -100,6 +100,7 @@ def make_modelwrapper(C, pe, idt, odt, pdt, func, vecs): @pytest.mark.parametrize("func", ["add", "mul"]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_fpgadataflow_channelwise_ops(idt, act, pdt, nf, ich, func, vecs, exec_mode): diff --git a/tests/fpgadataflow/test_fpgadataflow_convinputgenerator.py b/tests/fpgadataflow/test_fpgadataflow_convinputgenerator.py index 47cd7e7ba1df76cc793cd0946581239a6883874e..afac8dc6f30982b63827dcd5a9ee4b70c92235ae 100644 --- a/tests/fpgadataflow/test_fpgadataflow_convinputgenerator.py +++ b/tests/fpgadataflow/test_fpgadataflow_convinputgenerator.py @@ -149,6 +149,7 @@ def prepare_inputs(input_tensor): @pytest.mark.parametrize("simd", [1, 2]) # depthwise @pytest.mark.parametrize("dw", [0, 1]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_slidingwindow( diff --git a/tests/fpgadataflow/test_fpgadataflow_convinputgenerator1d.py b/tests/fpgadataflow/test_fpgadataflow_convinputgenerator1d.py index 8440ac1fe46a0d1ea4db3d76489dfc4ce68ff642..0d8b26632307b2b514c2aacaa96b28989286cd0d 100644 --- a/tests/fpgadataflow/test_fpgadataflow_convinputgenerator1d.py +++ b/tests/fpgadataflow/test_fpgadataflow_convinputgenerator1d.py @@ -46,6 +46,8 @@ from finn.transformation.fpgadataflow.set_exec_mode import SetExecMode from finn.transformation.general import GiveUniqueNodeNames from finn.util.basic import gen_finn_dt_tensor +fpga_part = "xczu3eg-sbva484-1-e" + def make_single_im2col_modelwrapper( k, ifm_ch, ifm_dim, ofm_dim, simd, stride, dilation, idt @@ -90,7 +92,7 @@ def make_single_im2col_modelwrapper( def make_single_slidingwindow_modelwrapper( - k, ifm_ch, ifm_dim, ofm_dim, simd, stride, dilation, idt, dw=0 + k, ifm_ch, ifm_dim, ofm_dim, simd, stride, dilation, idt, parallel_window, dw=0 ): k_h, k_w = k ifm_dim_h, ifm_dim_w = ifm_dim @@ -122,6 +124,7 @@ def make_single_slidingwindow_modelwrapper( inputDataType=idt.name, outputDataType=odt.name, depthwise=dw, + parallel_window=parallel_window, ) graph = helper.make_graph( nodes=[SlidingWindow_node], @@ -155,8 +158,7 @@ def prepare_inputs(input_tensor): # Stride @pytest.mark.parametrize("stride", [[1, 1], [2, 1]]) # Dilation -# @pytest.mark.parametrize("dilation", [[1, 1], [2, 1]]) -@pytest.mark.parametrize("dilation", [[1, 1]]) +@pytest.mark.parametrize("dilation", [[1, 1], [2, 1]]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) # input channel parallelism ("SIMD") @@ -165,10 +167,23 @@ def prepare_inputs(input_tensor): @pytest.mark.parametrize("dw", [0, 1]) # Flip dimensions @pytest.mark.parametrize("flip", [False, True]) +# Use parallel window output variant +@pytest.mark.parametrize("parallel_window", [False, True]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_slidingwindow_1d( - idt, k, ifm_dim, ifm_ch, stride, dilation, exec_mode, simd, dw, flip + idt, + k, + ifm_dim, + ifm_ch, + stride, + dilation, + exec_mode, + simd, + dw, + flip, + parallel_window, ): if flip: k = k[::-1] @@ -186,6 +201,11 @@ def test_fpgadataflow_slidingwindow_1d( """Dilation value greater than 1 and stride greater than 1 currently not supported for 1D convolutions""" ) + if (dilation_h > 1 or dilation_w > 1) and dw == 0: + pytest.skip( + """Dilation value greater than 1 currently not supported + for non-dws 1D convolutions""" + ) if simd > ifm_ch: pytest.skip("SIMD cannot be larger than number of input channels") @@ -203,6 +223,7 @@ def test_fpgadataflow_slidingwindow_1d( stride=stride, dilation=dilation, idt=idt, + parallel_window=parallel_window, dw=dw, ) @@ -213,7 +234,7 @@ def test_fpgadataflow_slidingwindow_1d( elif exec_mode == "rtlsim": model = model.transform(SetExecMode("rtlsim")) model = model.transform(GiveUniqueNodeNames()) - model = model.transform(PrepareIP("xc7z020clg400-1", 5)) + model = model.transform(PrepareIP(fpga_part, 5)) model = model.transform(HLSSynthIP()) model = model.transform(PrepareRTLSim()) else: diff --git a/tests/fpgadataflow/test_fpgadataflow_duplicatestreams.py b/tests/fpgadataflow/test_fpgadataflow_duplicatestreams.py index 1faf647df225853cf026a49adbfc6bb9d8f1b670..838dec81d32799d5a2afa6cfda8db632b2ac3355 100644 --- a/tests/fpgadataflow/test_fpgadataflow_duplicatestreams.py +++ b/tests/fpgadataflow/test_fpgadataflow_duplicatestreams.py @@ -103,6 +103,7 @@ def prepare_inputs(input_tensor, idt): @pytest.mark.parametrize("n_dupl", [2, 3]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_duplicatestreams(idt, ch, fold, imdim, n_dupl, exec_mode): if fold == -1: diff --git a/tests/fpgadataflow/test_fpgadataflow_dwc.py b/tests/fpgadataflow/test_fpgadataflow_dwc.py index 248b591eb48d7cfd6f121738a9bca525c38a45f8..973bfcca2e9862769b2b973365682cbfbc4b4512 100644 --- a/tests/fpgadataflow/test_fpgadataflow_dwc.py +++ b/tests/fpgadataflow/test_fpgadataflow_dwc.py @@ -83,6 +83,7 @@ def prepare_inputs(input_tensor, dt): @pytest.mark.parametrize("OUTWidth", [2, 4]) # finn_dtype @pytest.mark.parametrize("finn_dtype", [DataType["BIPOLAR"], DataType["INT2"]]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_dwc_rtlsim(Shape, INWidth, OUTWidth, finn_dtype): diff --git a/tests/fpgadataflow/test_fpgadataflow_fclayer.py b/tests/fpgadataflow/test_fpgadataflow_fclayer.py index 02c3a3dc9506152fe999873df0612e76a5c9cefd..41bd5a6d0be4fdd82a40fbdcfc2a307f501b8c07 100644 --- a/tests/fpgadataflow/test_fpgadataflow_fclayer.py +++ b/tests/fpgadataflow/test_fpgadataflow_fclayer.py @@ -146,6 +146,7 @@ def prepare_inputs(input_tensor, idt, wdt): @pytest.mark.parametrize("mw", [16]) # HLS matrix height (output features) @pytest.mark.parametrize("mh", [16]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_fclayer_cppsim(mem_mode, idt, wdt, act, nf, sf, mw, mh): @@ -233,6 +234,7 @@ def test_fpgadataflow_fclayer_cppsim(mem_mode, idt, wdt, act, nf, sf, mw, mh): @pytest.mark.parametrize("mw", [16]) # HLS matrix height (output features) @pytest.mark.parametrize("mh", [16]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_fclayer_rtlsim(mem_mode, idt, wdt, act, nf, sf, mw, mh): @@ -332,6 +334,7 @@ def test_fpgadataflow_fclayer_rtlsim(mem_mode, idt, wdt, act, nf, sf, mw, mh): @pytest.mark.parametrize("mw", [128]) # HLS matrix height (output features) @pytest.mark.parametrize("mh", [128]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_fclayer_large_depth_decoupled_mode_rtlsim( mem_mode, idt, wdt, act, nf, sf, mw, mh diff --git a/tests/fpgadataflow/test_fpgadataflow_fifo.py b/tests/fpgadataflow/test_fpgadataflow_fifo.py index 4d3074fe14617df4386f060b6a476734931fb4ca..15e7f594ee4916559324f35d42b07de9acc5a2c6 100644 --- a/tests/fpgadataflow/test_fpgadataflow_fifo.py +++ b/tests/fpgadataflow/test_fpgadataflow_fifo.py @@ -87,6 +87,7 @@ def prepare_inputs(input_tensor, dt): @pytest.mark.parametrize("depth", [16]) # finn_dtype @pytest.mark.parametrize("finn_dtype", [DataType["BIPOLAR"]]) # , DataType["INT2"]]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_fifo_rtlsim(Shape, folded_shape, depth, finn_dtype): diff --git a/tests/fpgadataflow/test_fpgadataflow_fmpadding.py b/tests/fpgadataflow/test_fpgadataflow_fmpadding.py index b564273c0927938859dc438dce619e7067a7ad74..ce21ea0c321587b4d73b64dbd2729090f141cce8 100644 --- a/tests/fpgadataflow/test_fpgadataflow_fmpadding.py +++ b/tests/fpgadataflow/test_fpgadataflow_fmpadding.py @@ -111,6 +111,7 @@ def make_single_fmpadding_modelwrapper(idim, padding, num_ch, simd, idt, pad_sty @pytest.mark.parametrize("idt", [DataType["INT2"], DataType["INT4"]]) # execution mode @pytest.mark.parametrize("mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_fmpadding(idim, pad, num_ch, simd, pad_style, idt, mode): diff --git a/tests/fpgadataflow/test_fpgadataflow_globalaccpool.py b/tests/fpgadataflow/test_fpgadataflow_globalaccpool.py index 2299cc6e8f397df718d2fd65be8a562c2457e42d..fc622b10e9abcc3b050e30fc275ca927b89c7d9c 100644 --- a/tests/fpgadataflow/test_fpgadataflow_globalaccpool.py +++ b/tests/fpgadataflow/test_fpgadataflow_globalaccpool.py @@ -87,6 +87,7 @@ def prepare_inputs(input_tensor, idt): @pytest.mark.parametrize("imdim", [7]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_globalaccpool(idt, ch, fold, imdim, exec_mode): if fold == -1: diff --git a/tests/fpgadataflow/test_fpgadataflow_ipstitch.py b/tests/fpgadataflow/test_fpgadataflow_ipstitch.py index a4e75f5254b3bfd96871dbf32b8400edc2d55379..381ff3a09f667c326bfad43b8fc7ece538a1213e 100644 --- a/tests/fpgadataflow/test_fpgadataflow_ipstitch.py +++ b/tests/fpgadataflow/test_fpgadataflow_ipstitch.py @@ -201,6 +201,7 @@ def create_two_fc_model(mem_mode="decoupled"): @pytest.mark.parametrize("mem_mode", ["const", "decoupled"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_ipstitch_gen_model(mem_mode): model = create_one_fc_model(mem_mode) @@ -222,6 +223,7 @@ def test_fpgadataflow_ipstitch_gen_model(mem_mode): @pytest.mark.parametrize("mem_mode", ["const", "decoupled"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_ipstitch_do_stitch(mem_mode): model = load_test_checkpoint_or_skip( @@ -239,6 +241,7 @@ def test_fpgadataflow_ipstitch_do_stitch(mem_mode): @pytest.mark.parametrize("mem_mode", ["const", "decoupled"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_ipstitch_rtlsim(mem_mode): model = load_test_checkpoint_or_skip( @@ -287,6 +290,7 @@ def test_fpgadataflow_ipstitch_rtlsim(mem_mode): @pytest.mark.parametrize("mem_mode", ["const", "decoupled"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_fpgadataflow_ipstitch_synth_ooc(mem_mode): @@ -307,7 +311,7 @@ def test_fpgadataflow_ipstitch_synth_ooc(mem_mode): assert ret["BRAM"] == 0 assert ret["fmax_mhz"] > 100 - +@pytest.mark.fpgadataflow def test_fpgadataflow_ipstitch_iodma_floorplan(): model = create_one_fc_model() if model.graph.node[0].op_type == "StreamingDataflowPartition": @@ -330,10 +334,11 @@ def test_fpgadataflow_ipstitch_iodma_floorplan(): @pytest.mark.parametrize("period_ns", [5]) # override mem_mode to external @pytest.mark.parametrize("extw", [True, False]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado @pytest.mark.vitis -def test_fpgadataflow_ipstitch_vitis(board, period_ns, extw): +def test_fpgadataflow_ipstitch_vitis_end2end(board, period_ns, extw): if "VITIS_PATH" not in os.environ: pytest.skip("VITIS_PATH not set") platform = alveo_default_platform[board] @@ -353,9 +358,10 @@ def test_fpgadataflow_ipstitch_vitis(board, period_ns, extw): # board @pytest.mark.parametrize("board", ["Pynq-Z1"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado -def test_fpgadataflow_ipstitch_zynqbuild(board): +def test_fpgadataflow_ipstitch_zynqbuild_end2end(board): model = create_two_fc_model() if model.graph.node[0].op_type == "StreamingDataflowPartition": sdp_node = getCustomOp(model.graph.node[0]) diff --git a/tests/fpgadataflow/test_fpgadataflow_labelselect.py b/tests/fpgadataflow/test_fpgadataflow_labelselect.py index 8ed06c8bdf1c0dbfab2f8141bf724132f4a24705..2858426d1ee4b1f91f5de807ccded4ffe35a3a40 100644 --- a/tests/fpgadataflow/test_fpgadataflow_labelselect.py +++ b/tests/fpgadataflow/test_fpgadataflow_labelselect.py @@ -92,6 +92,7 @@ def prepare_inputs(input_tensor, idt): @pytest.mark.parametrize("k", [1, 5]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_fpgadataflow_labelselect(idt, labels, fold, k, exec_mode): np.random.seed(0) diff --git a/tests/fpgadataflow/test_fpgadataflow_lookup.py b/tests/fpgadataflow/test_fpgadataflow_lookup.py index 45678bbdf22c21d794777aba27d9070b42238267..0c284a530319290eb406c6b54a80e4f52d7ed1fa 100644 --- a/tests/fpgadataflow/test_fpgadataflow_lookup.py +++ b/tests/fpgadataflow/test_fpgadataflow_lookup.py @@ -36,8 +36,10 @@ from torch import nn from finn.core.datatype import DataType from finn.core.modelwrapper import ModelWrapper from finn.core.onnx_exec import execute_onnx +from finn.custom_op.registry import getCustomOp from finn.transformation.fpgadataflow.compile_cppsim import CompileCppSim from finn.transformation.fpgadataflow.convert_to_hls_layers import InferLookupLayer +from finn.transformation.fpgadataflow.create_stitched_ip import CreateStitchedIP from finn.transformation.fpgadataflow.hlssynth_ip import HLSSynthIP from finn.transformation.fpgadataflow.prepare_cppsim import PrepareCppSim from finn.transformation.fpgadataflow.prepare_ip import PrepareIP @@ -87,6 +89,7 @@ def make_lookup_model(embeddings, ishape, idt, edt): ) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_fpgadataflow_lookup(edt, embedding_cfg, exec_mode): @@ -124,9 +127,57 @@ def test_fpgadataflow_lookup(edt, embedding_cfg, exec_mode): model = model.transform(SetExecMode("cppsim")) elif exec_mode == "rtlsim": model = model.transform(GiveUniqueNodeNames()) - model = model.transform(PrepareIP("xc7z020clg400-1", 10)) + model = model.transform(PrepareIP("xczu3eg-sbva484-1-e", 10)) model = model.transform(HLSSynthIP()) model = model.transform(SetExecMode("rtlsim")) model = model.transform(PrepareRTLSim()) ret_sim = execute_onnx(model, {iname: itensor}) assert (exp_out == ret_sim[oname]).all() + + +@pytest.mark.fpgadataflow +@pytest.mark.vivado +@pytest.mark.slow +def test_fpgadataflow_lookup_external(): + fpga_part = "xczu3eg-sbva484-1-e" + edt = DataType["INT8"] + embedding_cfg = (200000, DataType["UINT32"], 300) + ishape = (1, 600) + num_embeddings, idt, embedding_dim = embedding_cfg + eshape = (num_embeddings, embedding_dim) + exp_oshape = tuple(list(ishape) + [embedding_dim]) + embeddings = gen_finn_dt_tensor(edt, eshape) + model = make_lookup_model(embeddings, ishape, idt, edt) + assert len(model.graph.node) == 1 + assert model.graph.node[0].op_type == "Gather" + iname = model.graph.input[0].name + ename = model.graph.node[0].input[0] + oname = model.graph.output[0].name + assert model.get_tensor_datatype(iname) == idt + assert model.get_tensor_datatype(ename) == edt + assert model.get_tensor_datatype(oname) == edt + assert tuple(model.get_tensor_shape(ename)) == eshape + assert tuple(model.get_tensor_shape(oname)) == exp_oshape + assert (model.get_initializer(ename) == embeddings).all() + # itensor = gen_finn_dt_tensor(idt, ishape).astype(np.int64) + # itensor = np.clip(itensor, 0, num_embeddings - 1) + # ret = execute_onnx(model, {iname: itensor}) + # exp_out = np.take(embeddings, itensor, axis=0) + # assert (exp_out == ret[oname]).all() + # call transformation to convert to HLS and verify conversion + model = model.transform(InferLookupLayer()) + assert model.graph.node[0].op_type == "Lookup" + assert model.graph.node[0].input[0] == iname + assert model.graph.node[0].input[1] == ename + assert model.graph.node[0].output[0] == oname + getCustomOp(model.graph.node[0]).set_nodeattr("mem_mode", "external") + model = model.transform(GiveUniqueNodeNames()) + model = model.transform(PrepareIP(fpga_part, 10)) + model = model.transform(HLSSynthIP()) + model = model.transform(CreateStitchedIP(fpga_part, 10.0)) + ifnames = eval(model.get_metadata_prop("vivado_stitch_ifnames")) + # check some generated files/interfaces for the generated stitched IP + assert ifnames["aximm"] == [["m_axi_gmem0", 32]] + assert ifnames["s_axis"] == [["s_axis_0", 32]] + assert ifnames["m_axis"] == [["m_axis_0", 32]] + assert ifnames["axilite"] == ["s_axi_control_0"] diff --git a/tests/fpgadataflow/test_fpgadataflow_res_estimate.py b/tests/fpgadataflow/test_fpgadataflow_res_estimate.py index fe52a73fc07df8551442e975c5eb378c132a56d7..951843a6585c842cf1d4ac93241b3e34554100a5 100644 --- a/tests/fpgadataflow/test_fpgadataflow_res_estimate.py +++ b/tests/fpgadataflow/test_fpgadataflow_res_estimate.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + from onnx import TensorProto, helper from finn.analysis.fpgadataflow.res_estimation import ( @@ -49,7 +51,7 @@ def check_two_dict_for_equality(dict1, dict2): return True - +@pytest.mark.fpgadataflow def test_res_estimate(): mw = mh = 4 simd = 1 diff --git a/tests/fpgadataflow/test_fpgadataflow_thresholding.py b/tests/fpgadataflow/test_fpgadataflow_thresholding.py index 341bd3f37041c9b5a1526e99b2c4bad4d3dd3029..4cfdbe82d5b9ea533519b97a85c3a09c3bffc97b 100644 --- a/tests/fpgadataflow/test_fpgadataflow_thresholding.py +++ b/tests/fpgadataflow/test_fpgadataflow_thresholding.py @@ -52,15 +52,21 @@ from finn.transformation.general import GiveUniqueNodeNames from finn.util.basic import gen_finn_dt_tensor from finn.util.pyverilator import axilite_read, axilite_write -test_fpga_part = "xc7z020clg400-1" +test_fpga_part = "xczu3eg-sbva484-1-e" target_clk_ns = 5 -def make_single_thresholding_modelwrapper(T, pe, idt, odt, actval, mem_mode): +def make_single_thresholding_modelwrapper( + T, pe, idt, odt, actval, mem_mode, n_inp_vecs +): NumChannels = T.shape[0] - inp = helper.make_tensor_value_info("inp", TensorProto.FLOAT, [1, NumChannels]) - outp = helper.make_tensor_value_info("outp", TensorProto.FLOAT, [1, NumChannels]) + inp = helper.make_tensor_value_info( + "inp", TensorProto.FLOAT, n_inp_vecs + [NumChannels] + ) + outp = helper.make_tensor_value_info( + "outp", TensorProto.FLOAT, n_inp_vecs + [NumChannels] + ) node_inp_list = ["inp", "thresh"] @@ -78,6 +84,7 @@ def make_single_thresholding_modelwrapper(T, pe, idt, odt, actval, mem_mode): outputDataType=odt.name, ActVal=actval, mem_mode=mem_mode, + numInputVectors=n_inp_vecs, ) graph = helper.make_graph( nodes=[Thresholding_node], @@ -109,16 +116,18 @@ def make_single_thresholding_modelwrapper(T, pe, idt, odt, actval, mem_mode): @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) # memory mode @pytest.mark.parametrize("mem_mode", ["const", "decoupled"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_fpgadataflow_thresholding(idt, act, nf, ich, exec_mode, mem_mode): if nf == -1: nf = ich pe = ich // nf + n_inp_vecs = [1, 2, 2] assert ich % pe == 0 # generate input data - x = gen_finn_dt_tensor(idt, (1, ich)) + x = gen_finn_dt_tensor(idt, tuple(n_inp_vecs + [ich])) odt = act n_steps = act.get_num_possible_values() - 1 @@ -135,7 +144,9 @@ def test_fpgadataflow_thresholding(idt, act, nf, ich, exec_mode, mem_mode): else: actval = odt.min() - model = make_single_thresholding_modelwrapper(T, pe, idt, odt, actval, mem_mode) + model = make_single_thresholding_modelwrapper( + T, pe, idt, odt, actval, mem_mode, n_inp_vecs + ) if exec_mode == "cppsim": model = model.transform(PrepareCppSim()) @@ -153,7 +164,10 @@ def test_fpgadataflow_thresholding(idt, act, nf, ich, exec_mode, mem_mode): # package input data as dictionary input_dict = {"inp": x} - y = multithreshold(x, T) + # multithreshold util fxn wants NCHW input, not NHWC + y = multithreshold(np.transpose(x, (0, 3, 1, 2)), T) + # convert back to NHWC for comparison to hw outputs + y = np.transpose(y, (0, 2, 3, 1)) if act == DataType["BIPOLAR"]: # binary to bipolar y = 2 * y - 1 @@ -183,8 +197,10 @@ def test_fpgadataflow_thresholding(idt, act, nf, ich, exec_mode, mem_mode): assert exp_cycles != 0 +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_runtime_thresholds_single_layer(): + n_inp_vecs = [1, 2, 2] mem_mode = "decoupled" act = DataType["INT4"] idt = DataType["INT16"] @@ -194,7 +210,7 @@ def test_runtime_thresholds_single_layer(): assert ich % pe == 0 # generate input data - in_tensor = gen_finn_dt_tensor(idt, (1, ich)) + in_tensor = gen_finn_dt_tensor(idt, tuple(n_inp_vecs + [ich])) odt = act n_steps = act.get_num_possible_values() - 1 @@ -207,7 +223,9 @@ def test_runtime_thresholds_single_layer(): else: actval = odt.min() - model = make_single_thresholding_modelwrapper(T, pe, idt, odt, actval, mem_mode) + model = make_single_thresholding_modelwrapper( + T, pe, idt, odt, actval, mem_mode, n_inp_vecs + ) op_inst = getCustomOp(model.graph.node[0]) op_inst.set_nodeattr("runtime_writeable_weights", 1) op_inst.make_weight_file(T, "decoupled_runtime", "old_weights.dat") @@ -244,7 +262,13 @@ def test_runtime_thresholds_single_layer(): # only use second batch element in output; first will be invalid due to # old weights (see above) y = exec_ctx["outp"][1] - expected = multithreshold(in_tensor, T)[1] + + # multithreshold util fxn wants NCHW input, not NHWC + expected = multithreshold(np.transpose(in_tensor, (0, 3, 1, 2)), T) + # convert back to NHWC for comparison to hw outputs + expected = np.transpose(expected, (0, 2, 3, 1))[1] + + # expected = multithreshold(in_tensor, T)[1] if act == DataType["BIPOLAR"]: # binary to bipolar expected = 2 * expected - 1 diff --git a/tests/fpgadataflow/test_fpgadataflow_upsampler.py b/tests/fpgadataflow/test_fpgadataflow_upsampler.py index 1709cfe32904a5ed369f8399150a8a1d05f4b781..362d9def1028c46a8ebf1d79649971156b1d57a3 100644 --- a/tests/fpgadataflow/test_fpgadataflow_upsampler.py +++ b/tests/fpgadataflow/test_fpgadataflow_upsampler.py @@ -125,6 +125,7 @@ class PyTorchTestModel(nn.Module): @pytest.mark.parametrize("NumChannels", [4]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.vivado @pytest.mark.slow def test_fpgadataflow_upsampler(dt, IFMDim, scale, NumChannels, exec_mode): diff --git a/tests/fpgadataflow/test_fpgadataflow_vvau.py b/tests/fpgadataflow/test_fpgadataflow_vvau.py index 9eb3a7f4514e610d79bb83cc62a7561a33ced543..46cb23a520b524ef4063916179bb33a6810ef7c8 100644 --- a/tests/fpgadataflow/test_fpgadataflow_vvau.py +++ b/tests/fpgadataflow/test_fpgadataflow_vvau.py @@ -158,6 +158,7 @@ def prepare_inputs(input_tensor): @pytest.mark.parametrize("channels", [3, 4]) # execution mode @pytest.mark.parametrize("exec_mode", ["cppsim", "rtlsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado def test_fpgadataflow_vvau( diff --git a/tests/fpgadataflow/test_layer_streaming_maxpool_batch.py b/tests/fpgadataflow/test_layer_streaming_maxpool_batch.py index 236eb2a0342a2782f106761f4cd356888a2f8630..494aea4dad000ff6d6bf61e9e38440b727d90dc7 100644 --- a/tests/fpgadataflow/test_layer_streaming_maxpool_batch.py +++ b/tests/fpgadataflow/test_layer_streaming_maxpool_batch.py @@ -28,25 +28,29 @@ import pytest -import numpy as np +# import numpy as np from onnx import TensorProto, helper import finn.core.onnx_exec as oxe from finn.analysis.fpgadataflow.exp_cycles_per_layer import exp_cycles_per_layer from finn.core.datatype import DataType from finn.core.modelwrapper import ModelWrapper -from finn.custom_op.registry import getCustomOp +from finn.custom_op.general.maxpoolnhwc import compute_pool_output_dim + +# from finn.custom_op.registry import getCustomOp from finn.transformation.fpgadataflow.compile_cppsim import CompileCppSim +from finn.transformation.fpgadataflow.convert_to_hls_layers import InferStreamingMaxPool from finn.transformation.fpgadataflow.hlssynth_ip import HLSSynthIP from finn.transformation.fpgadataflow.prepare_cppsim import PrepareCppSim from finn.transformation.fpgadataflow.prepare_ip import PrepareIP from finn.transformation.fpgadataflow.prepare_rtlsim import PrepareRTLSim from finn.transformation.fpgadataflow.set_exec_mode import SetExecMode from finn.transformation.general import GiveUniqueNodeNames +from finn.transformation.infer_shapes import InferShapes from finn.util.basic import gen_finn_dt_tensor -def make_single_maxpoolnhwc_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt): +def make_single_maxpoolnhwc_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt, ceil_mode): k_h, k_w = k ifm_dim_h, ifm_dim_w = ifm_dim ofm_dim_h, ofm_dim_w = ofm_dim @@ -65,6 +69,7 @@ def make_single_maxpoolnhwc_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt): domain="finn.custom_op.general", kernel_shape=[k_h, k_w], strides=[k_h, k_w], + ceil_mode=ceil_mode, pads=[0, 0, 0, 0], ) graph = helper.make_graph( @@ -80,7 +85,9 @@ def make_single_maxpoolnhwc_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt): return model -def make_single_streamingmaxpool_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt): +def make_single_streamingmaxpool_modelwrapper( + k, ifm_ch, pe, ifm_dim, ofm_dim, idt, ceil_mode +): k_h, k_w = k ifm_dim_h, ifm_dim_w = ifm_dim ofm_dim_h, ofm_dim_w = ofm_dim @@ -100,7 +107,9 @@ def make_single_streamingmaxpool_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt): backend="fpgadataflow", PoolDim=[k_h, k_w], NumChannels=ifm_ch, + PE=pe, ImgDim=[ifm_dim_h, ifm_dim_w], + CeilMode=ceil_mode, dataType=idt.name, ) graph = helper.make_graph( @@ -127,14 +136,21 @@ def prepare_inputs(input_tensor): # kernel size @pytest.mark.parametrize("k", [2, 4]) # input dimension -@pytest.mark.parametrize("ifm_dim", [4, 8]) +@pytest.mark.parametrize("ifm_dim", [4, 10]) # input channels @pytest.mark.parametrize("ifm_ch", [1, 3]) # 1,3 +# pe +@pytest.mark.parametrize("pe", [1, 3]) +# ceil mode +@pytest.mark.parametrize("ceil_mode", [1]) # execution mode @pytest.mark.parametrize("exec_mode", ["rtlsim", "cppsim"]) +@pytest.mark.fpgadataflow @pytest.mark.slow @pytest.mark.vivado -def test_fpgadataflow_streamingmaxpool(idt, dim_1d, k, ifm_dim, ifm_ch, exec_mode): +def test_fpgadataflow_streamingmaxpool( + idt, dim_1d, k, ifm_dim, ifm_ch, pe, ceil_mode, exec_mode +): ifm_dim_h = ifm_dim k_h = k if dim_1d: @@ -148,22 +164,31 @@ def test_fpgadataflow_streamingmaxpool(idt, dim_1d, k, ifm_dim, ifm_ch, exec_mod stride_h = k_h stride_w = k_w - ofm_dim_h = int(((ifm_dim_h - k_h) / stride_h) + 1) - ofm_dim_w = int(((ifm_dim_w - k_w) / stride_w) + 1) + ofm_dim_h = compute_pool_output_dim(ifm_dim_h, k_h, stride_h, 0, ceil_mode) + ofm_dim_w = compute_pool_output_dim(ifm_dim_w, k_w, stride_w, 0, ceil_mode) ofm_dim = (ofm_dim_h, ofm_dim_w) if idt == DataType["BIPOLAR"] and dim_1d: pytest.skip("Skipping binary StreamingMaxPool_1d (not implemented)") - if ifm_dim_h % k_h != 0 or ifm_dim_w % k_w != 0: - pytest.skip("Skipping StreamingMaxPool test w/ ImgDim % PoolDim != 0") + if (ifm_dim_h % k_h != 0 or ifm_dim_w % k_w != 0) and (not dim_1d): + pytest.skip("StreamingMaxPool_2d test w/ ImgDim % PoolDim != 0 not implemented") + if pe > ifm_ch: + pytest.skip("PE cannot be larger than number of input channels") + if pe > 1 and (not dim_1d): + pytest.skip("PE>1 only supported for StreamingMaxPool_1d") x = gen_finn_dt_tensor(idt, (1, ifm_dim_h, ifm_dim_w, ifm_ch)) # prepare input data input_dict = prepare_inputs(x) - golden = make_single_maxpoolnhwc_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt) + golden = make_single_maxpoolnhwc_modelwrapper( + k, ifm_ch, ifm_dim, ofm_dim, idt, ceil_mode + ) y_expected = oxe.execute_onnx(golden, input_dict)["outp"] - model = make_single_streamingmaxpool_modelwrapper(k, ifm_ch, ifm_dim, ofm_dim, idt) + model = golden.transform(InferStreamingMaxPool()) + model = model.transform(InferShapes()) + + assert model.graph.node[0].op_type == "StreamingMaxPool_Batch" if exec_mode == "cppsim": model = model.transform(SetExecMode("cppsim")) @@ -172,7 +197,7 @@ def test_fpgadataflow_streamingmaxpool(idt, dim_1d, k, ifm_dim, ifm_ch, exec_mod elif exec_mode == "rtlsim": model = model.transform(SetExecMode("rtlsim")) model = model.transform(GiveUniqueNodeNames()) - model = model.transform(PrepareIP("xc7z020clg400-1", 5)) + model = model.transform(PrepareIP("xczu3eg-sbva484-1-e", 5)) model = model.transform(HLSSynthIP()) model = model.transform(PrepareRTLSim()) else: @@ -184,9 +209,11 @@ def test_fpgadataflow_streamingmaxpool(idt, dim_1d, k, ifm_dim, ifm_ch, exec_mod if exec_mode == "rtlsim": node = model.get_nodes_by_op_type("StreamingMaxPool_Batch")[0] - inst = getCustomOp(node) - cycles_rtlsim = inst.get_nodeattr("cycles_rtlsim") + # inst = getCustomOp(node) + # cycles_rtlsim = inst.get_nodeattr("cycles_rtlsim") exp_cycles_dict = model.analysis(exp_cycles_per_layer) exp_cycles = exp_cycles_dict[node.name] - assert np.isclose(exp_cycles, cycles_rtlsim, atol=15) + # FIXME: maxpool cycles prediction needs a fix + # mostl likely due to some loops not flattening + # assert np.isclose(exp_cycles, cycles_rtlsim, atol=15) assert exp_cycles != 0 diff --git a/tests/fpgadataflow/test_runtime_weights.py b/tests/fpgadataflow/test_runtime_weights.py index 0196a78d5c4254d7cb116641f946bcccb9e1ebc9..8d0976cad2f3c1deb6947db7a06a669ed34dc5d7 100644 --- a/tests/fpgadataflow/test_runtime_weights.py +++ b/tests/fpgadataflow/test_runtime_weights.py @@ -43,10 +43,11 @@ from finn.util.basic import gen_finn_dt_tensor from finn.util.create import hls_random_mlp_maker from finn.util.pyverilator import axilite_read, axilite_write -test_fpga_part = "xc7z020clg400-1" +test_fpga_part = "xczu3eg-sbva484-1-e" target_clk_ns = 5 +@pytest.mark.fpgadataflow @pytest.mark.vivado def test_runtime_weights_single_layer(): idt = DataType["UINT32"] diff --git a/tests/fpgadataflow/test_set_folding.py b/tests/fpgadataflow/test_set_folding.py index 492f208671f4622d189c48ece874740a68b69072..b3d5458ff29edb18e01c33501db758587f865b35 100644 --- a/tests/fpgadataflow/test_set_folding.py +++ b/tests/fpgadataflow/test_set_folding.py @@ -112,6 +112,7 @@ def make_multi_fclayer_model(ch, wdt, adt, tdt, nnodes): @pytest.mark.parametrize("target_fps", [30, 10**5, 10**7]) # target chip or board @pytest.mark.parametrize("platform", ["Pynq-Z1", "Ultra96", "U200"]) +@pytest.mark.fpgadataflow def test_set_folding(target_fps, platform): model = make_multi_fclayer_model( diff --git a/tests/transformation/streamline/test_absorb_mul_into_topk.py b/tests/transformation/streamline/test_absorb_mul_into_topk.py index bc9a31d49c7edfc20ca3e932efd00df939f1135f..e75f2d21db5cb2fe1b2f93e43ee0e61c7a7681c9 100644 --- a/tests/transformation/streamline/test_absorb_mul_into_topk.py +++ b/tests/transformation/streamline/test_absorb_mul_into_topk.py @@ -39,6 +39,7 @@ from finn.transformation.insert_topk import InsertTopK from finn.transformation.streamline.absorb import AbsorbScalarMulAddIntoTopK +@pytest.mark.streamline # parameter to indicate if mul parameter is negative or positive @pytest.mark.parametrize("mul_positive", [True, False]) # parameter to indicate if mul parameter is scalar or not diff --git a/tests/transformation/streamline/test_absorb_opposite_transposes.py b/tests/transformation/streamline/test_absorb_opposite_transposes.py index 859e691277a261f01b559e2e166763e402c5d689..ca5ed6ba6a85935604750ab35df0ccf30e032c2c 100644 --- a/tests/transformation/streamline/test_absorb_opposite_transposes.py +++ b/tests/transformation/streamline/test_absorb_opposite_transposes.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import numpy as np import onnx.helper as oh from onnx import TensorProto @@ -36,6 +38,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline.absorb import AbsorbConsecutiveTransposes +@pytest.mark.streamline def test_absorb_opposite_transposes(): np.random.seed(0) input_shape = [1, 3, 4, 2] diff --git a/tests/transformation/streamline/test_absorb_transp_into_flatten.py b/tests/transformation/streamline/test_absorb_transp_into_flatten.py index 1e5d5fe5806d2e3f418438b260d2257f5ae31adf..533dc693da0774e89d2dbb44aac52a6bef038990 100644 --- a/tests/transformation/streamline/test_absorb_transp_into_flatten.py +++ b/tests/transformation/streamline/test_absorb_transp_into_flatten.py @@ -13,6 +13,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline.absorb import AbsorbTransposeIntoFlatten +@pytest.mark.streamline # permutation of transpose node @pytest.mark.parametrize("perm", [[0, 2, 3, 1], [0, 1, 3, 2], [3, 2, 0, 1]]) # reshape or flatten diff --git a/tests/transformation/streamline/test_collapse_repeated_op.py b/tests/transformation/streamline/test_collapse_repeated_op.py index 1741ab6b8f4fc1c3e806a8868f329cd7753eac4d..d48d4ad3c2a30e005c1ccc02eee4f7edcaa8a57b 100644 --- a/tests/transformation/streamline/test_collapse_repeated_op.py +++ b/tests/transformation/streamline/test_collapse_repeated_op.py @@ -38,6 +38,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline import CollapseRepeatedAdd, CollapseRepeatedMul +@pytest.mark.streamline def test_collapse_repeated_op(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [2]) add_param_0 = oh.make_tensor_value_info("add_param_0", TensorProto.FLOAT, [2]) @@ -74,6 +75,7 @@ def test_collapse_repeated_op(): assert new_model.graph.node[1].op_type == "Mul" +@pytest.mark.streamline @pytest.mark.parametrize( "test_args", [("Add", CollapseRepeatedAdd()), ("Mul", CollapseRepeatedMul())], diff --git a/tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py b/tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py index fca073f5a05e10bd721a18538dada05b4ad0d774..2e5ed2eebfcf7ac7c39ccd8c0f105dee8fb389a8 100644 --- a/tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py +++ b/tests/transformation/streamline/test_factor_out_mul_sign_magnitude.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import numpy as np import onnx.helper as oh from onnx import TensorProto @@ -36,6 +38,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline import FactorOutMulSignMagnitude +@pytest.mark.streamline def test_factor_out_mul_sign_magnitude(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [1, 2]) mul_param = oh.make_tensor_value_info("mul_param", TensorProto.FLOAT, [1, 2]) diff --git a/tests/transformation/streamline/test_linear_past_eltwise.py b/tests/transformation/streamline/test_linear_past_eltwise.py index 098b3f9d4f67a2cbc1a87fbb67a313d00e229777..0e4ad6237b3f293c2ee32dcb4963423f6e8d9f19 100644 --- a/tests/transformation/streamline/test_linear_past_eltwise.py +++ b/tests/transformation/streamline/test_linear_past_eltwise.py @@ -89,6 +89,7 @@ def make_model(shape): return model +@pytest.mark.streamline # channels @pytest.mark.parametrize("ch", [64]) # ifmdim @@ -133,6 +134,7 @@ def test_linear_past_eltwise_add(ch, ifmdim): os.remove(export_onnx_path) +@pytest.mark.streamline @pytest.mark.parametrize("ch", [64, 1]) # ifmdim @pytest.mark.parametrize("ifmdim", [-1, 7]) diff --git a/tests/transformation/streamline/test_maxpool_nhwc.py b/tests/transformation/streamline/test_maxpool_nhwc.py new file mode 100644 index 0000000000000000000000000000000000000000..446302be94d7c5e9c06da1c1fc926de7a3bff578 --- /dev/null +++ b/tests/transformation/streamline/test_maxpool_nhwc.py @@ -0,0 +1,109 @@ +import pytest + +import onnx +import onnx.helper as oh +from onnx import TensorProto + +import finn.core.onnx_exec as oxe +from finn.core.datatype import DataType +from finn.core.modelwrapper import ModelWrapper +from finn.custom_op.general.maxpoolnhwc import compute_pool_output_dim +from finn.transformation.infer_shapes import InferShapes +from finn.transformation.streamline.reorder import MakeMaxPoolNHWC +from finn.util.basic import gen_finn_dt_tensor + + +def create_maxpool(ifm_dim, ifm_ch, kernel_shape, pads, strides, ceil_mode, idt): + ofm_dim_h = compute_pool_output_dim( + ifm_dim[0], kernel_shape[0], strides[0], pads[0], ceil_mode + ) + ofm_dim_w = compute_pool_output_dim( + ifm_dim[1], kernel_shape[1], strides[1], pads[1], ceil_mode + ) + inp = oh.make_tensor_value_info( + "inp", TensorProto.FLOAT, [1, ifm_ch, ifm_dim[0], ifm_dim[1]] + ) + outp_mp = oh.make_tensor_value_info( + "outp_mp", TensorProto.FLOAT, [1, ifm_ch, ofm_dim_h, ofm_dim_w] + ) + outp = oh.make_tensor_value_info( + "outp", TensorProto.FLOAT, [1, ofm_dim_h, ofm_dim_w, ifm_ch] + ) + + maxpool_node = oh.make_node( + "MaxPool", + inputs=["inp"], + outputs=["out_mp"], + ceil_mode=ceil_mode, + kernel_shape=kernel_shape, + pads=pads, + strides=strides, + ) + + transpose_node = onnx.helper.make_node( + "Transpose", + inputs=["out_mp"], + outputs=["outp"], + name="Transpose1", + perm=[0, 2, 3, 1], + ) + + graph = oh.make_graph( + nodes=[maxpool_node, transpose_node], + name="maxpool_graph", + inputs=[inp], + outputs=[outp], + value_info=[outp_mp], + ) + + model = oh.make_model(graph, producer_name="maxpool_model") + model = ModelWrapper(model) + model.set_tensor_datatype("inp", idt) + model.set_tensor_datatype("outp", idt) + + model = model.transform(InferShapes()) + + return model + + +@pytest.mark.streamline +# input dimension +@pytest.mark.parametrize("ifm_dim", [[8, 8], [9, 9]]) +# input channels +@pytest.mark.parametrize("ifm_ch", [3]) +# kernel shape +@pytest.mark.parametrize("kernel_shape", [[2, 2]]) +# padding +@pytest.mark.parametrize("pads", [[0, 0, 0, 0], [1, 1, 1, 1]]) +# strides +@pytest.mark.parametrize("strides", [[2, 2]]) +# ceil_mode +@pytest.mark.parametrize("ceil_mode", [0, 1]) +# input datatype +@pytest.mark.parametrize("idt", [DataType["INT4"]]) +def test_maxpool_nhwc(ifm_dim, ifm_ch, kernel_shape, pads, strides, ceil_mode, idt): + # create MaxPool node + maxpool_model = create_maxpool( + ifm_dim, ifm_ch, kernel_shape, pads, strides, ceil_mode, idt + ) + + # generate input tensor for testing + input_tensor = gen_finn_dt_tensor(idt, [1, ifm_ch, ifm_dim[0], ifm_dim[1]]) + input_dict = {"inp": input_tensor} + + # execute first model + output_dict = oxe.execute_onnx(maxpool_model, input_dict) + expected = output_dict["outp"] + + # transform MaxPool into MaxPoolNHWC + maxpool_model = maxpool_model.transform(MakeMaxPoolNHWC()) + + # execute transformed model + output_node_name = maxpool_model.graph.output[0].name + output_dict = oxe.execute_onnx( + maxpool_model, input_dict, return_full_exec_context=False + ) + output = output_dict[output_node_name] + + # compare outputs + assert (expected == output).all() diff --git a/tests/transformation/streamline/test_move_add_past_mul.py b/tests/transformation/streamline/test_move_add_past_mul.py index 163b9d310a5f12bd0b854f9aa46f53a549bf109e..e0ee449734e523b1e1742c85dd6b9d1bbdd32537 100644 --- a/tests/transformation/streamline/test_move_add_past_mul.py +++ b/tests/transformation/streamline/test_move_add_past_mul.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import numpy as np import onnx.helper as oh from onnx import TensorProto @@ -36,6 +38,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline import MoveAddPastMul +@pytest.mark.streamline def test_move_add_past_mul_single(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [2]) add_param = oh.make_tensor_value_info("add_param", TensorProto.FLOAT, [2]) @@ -65,6 +68,7 @@ def test_move_add_past_mul_single(): assert new_model.graph.node[0].output[0] == new_model.graph.node[1].input[0] +@pytest.mark.streamline def test_move_add_past_mul_multi(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [2]) add_param_0 = oh.make_tensor_value_info("add_param_0", TensorProto.FLOAT, [2]) @@ -103,6 +107,7 @@ def test_move_add_past_mul_multi(): assert new_model.graph.node[i].output[0] == new_model.graph.node[i + 1].input[0] +@pytest.mark.streamline def test_move_add_past_mul_only_if_linear(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [2]) top_out = oh.make_tensor_value_info("top_out", TensorProto.FLOAT, [2]) diff --git a/tests/transformation/streamline/test_move_chw_add_past_conv.py b/tests/transformation/streamline/test_move_chw_add_past_conv.py index e4be8fc3836f18bf95eb193516937c2e9334e2ff..d43531fa7d48a67ed91d1e7843bbdfd726fcf14d 100644 --- a/tests/transformation/streamline/test_move_chw_add_past_conv.py +++ b/tests/transformation/streamline/test_move_chw_add_past_conv.py @@ -38,6 +38,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline.reorder import MoveAddPastConv +@pytest.mark.streamline # input dimension @pytest.mark.parametrize("idim", [4, 7]) # kernel size diff --git a/tests/transformation/streamline/test_move_flatten_past_affine.py b/tests/transformation/streamline/test_move_flatten_past_affine.py index ef01436dc9435676b562e2b635a8cf12e901046b..1a4cecf1c46fddcb4427975cbf7e31a25628bf9a 100644 --- a/tests/transformation/streamline/test_move_flatten_past_affine.py +++ b/tests/transformation/streamline/test_move_flatten_past_affine.py @@ -42,6 +42,7 @@ from finn.transformation.streamline.reorder import MoveFlattenPastAffine from finn.util.basic import gen_finn_dt_tensor +@pytest.mark.streamline # data layout @pytest.mark.parametrize("data_layout", [DataLayout.NHWC, DataLayout.NCHW]) # batch size diff --git a/tests/transformation/streamline/test_move_flatten_past_topk.py b/tests/transformation/streamline/test_move_flatten_past_topk.py index 6086f7804eda4447de8f5948f521f0b003f65020..e3d8c65434871ecfa87784e69c76d99330c3f554 100644 --- a/tests/transformation/streamline/test_move_flatten_past_topk.py +++ b/tests/transformation/streamline/test_move_flatten_past_topk.py @@ -42,6 +42,7 @@ from finn.transformation.streamline.reorder import MoveFlattenPastTopK from finn.util.basic import gen_finn_dt_tensor +@pytest.mark.streamline # data layout @pytest.mark.parametrize("data_layout", [DataLayout.NHWC, DataLayout.NCHW]) # batch size diff --git a/tests/transformation/streamline/test_move_identical_op_past_join_op.py b/tests/transformation/streamline/test_move_identical_op_past_join_op.py index 60e76b8b07e06048ecf1a15c72134fecf5c97346..1d840ec15403e7a70c8da67a6f57076d8521d587 100644 --- a/tests/transformation/streamline/test_move_identical_op_past_join_op.py +++ b/tests/transformation/streamline/test_move_identical_op_past_join_op.py @@ -60,6 +60,7 @@ def create_model(perm): return model +@pytest.mark.streamline # Permutation of transpose node @pytest.mark.parametrize("perm", [[0, 3, 1, 2], [0, 2, 3, 1]]) def test_move_identical_op_past_join_op(perm): diff --git a/tests/transformation/streamline/test_move_maxpool_past_multithreshold.py b/tests/transformation/streamline/test_move_maxpool_past_multithreshold.py index fca05afa5b155e6a293857c14c10c4a9b80eeaf4..127f0fde7bc8423d7135a94f0d6f2ff1317bff76 100644 --- a/tests/transformation/streamline/test_move_maxpool_past_multithreshold.py +++ b/tests/transformation/streamline/test_move_maxpool_past_multithreshold.py @@ -1,3 +1,5 @@ +import pytest + import numpy as np from onnx import TensorProto, helper @@ -17,7 +19,7 @@ def get_multithreshold_rand_params(channels, num_of_thres, seed=None): thres = ((thres - bias) * steps).astype(np.float32) return thres - +@pytest.mark.streamline def test_move_maxpool_past_multithreshold(): # generate test vectors of correct shape ch = 64 diff --git a/tests/transformation/streamline/test_move_mul_past_dw_conv.py b/tests/transformation/streamline/test_move_mul_past_dw_conv.py index e9e956d845ef8e56d2078bcd738ad3bb0ff72bfa..ee7f840bb4461b9b32f25048c0678da9a68526b5 100644 --- a/tests/transformation/streamline/test_move_mul_past_dw_conv.py +++ b/tests/transformation/streamline/test_move_mul_past_dw_conv.py @@ -12,6 +12,7 @@ from finn.transformation.streamline.reorder import MoveMulPastDWConv from finn.util.basic import gen_finn_dt_tensor +@pytest.mark.streamline # input dimension @pytest.mark.parametrize("ifm_dim", [4, 7]) # input channels diff --git a/tests/transformation/streamline/test_move_mul_past_maxpool.py b/tests/transformation/streamline/test_move_mul_past_maxpool.py index 2c51aaf36a79591fd0fd0cea368d5e23da0d07c3..5f92c514c05b8ea9d75e6c3813dfee998fd8b08b 100755 --- a/tests/transformation/streamline/test_move_mul_past_maxpool.py +++ b/tests/transformation/streamline/test_move_mul_past_maxpool.py @@ -13,6 +13,7 @@ from finn.transformation.streamline.reorder import MoveMulPastMaxPool from finn.util.basic import gen_finn_dt_tensor +@pytest.mark.streamline # input dimension @pytest.mark.parametrize("ifm_dim", [4, 7]) # input channels diff --git a/tests/transformation/streamline/test_move_past_fork.py b/tests/transformation/streamline/test_move_past_fork.py index 364590f933ac27539fd546d64e25325032c885c9..f578234d6200936502e2e00c841b49707a99656b 100644 --- a/tests/transformation/streamline/test_move_past_fork.py +++ b/tests/transformation/streamline/test_move_past_fork.py @@ -9,6 +9,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline.reorder import MoveLinearPastFork +@pytest.mark.streamline @pytest.mark.parametrize("ch", [64, 1]) # ifmdim @pytest.mark.parametrize("ifmdim", [-1, 7]) diff --git a/tests/transformation/streamline/test_move_scalar_past_conv.py b/tests/transformation/streamline/test_move_scalar_past_conv.py index 5e2ded0174e9aa7a02551ed6b658f97ff070a523..8f725db91a4dadc938fb9296606e7214f02dcb6e 100644 --- a/tests/transformation/streamline/test_move_scalar_past_conv.py +++ b/tests/transformation/streamline/test_move_scalar_past_conv.py @@ -10,6 +10,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline import MoveAddPastConv, MoveScalarMulPastConv +@pytest.mark.streamline @pytest.mark.parametrize("padding", [False, True]) @pytest.mark.parametrize( "test_args", @@ -90,6 +91,7 @@ def test_move_scalar_past_conv(test_args, padding): assert new_model.graph.node[2].op_type == scalar_op +@pytest.mark.streamline @pytest.mark.parametrize( "test_args", [("Add", MoveAddPastConv()), ("Mul", MoveScalarMulPastConv())], diff --git a/tests/transformation/streamline/test_move_scalar_past_matmul.py b/tests/transformation/streamline/test_move_scalar_past_matmul.py index b15f84303b0dc2e00bd51397543871cfeb99c1f9..4d6dd95173485c234fd6d231e524d30b50ab56de 100644 --- a/tests/transformation/streamline/test_move_scalar_past_matmul.py +++ b/tests/transformation/streamline/test_move_scalar_past_matmul.py @@ -41,6 +41,7 @@ from finn.transformation.streamline import ( ) +@pytest.mark.streamline def test_move_scalar_mul_past_matmul(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [1, 2]) mul_param = oh.make_tensor_value_info("mul_param", TensorProto.FLOAT, [1, 1]) @@ -72,6 +73,7 @@ def test_move_scalar_mul_past_matmul(): assert new_model.graph.node[0].output[0] == new_model.graph.node[1].input[0] +@pytest.mark.streamline def test_move_scalar_add_past_matmul(): top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, [1, 2]) add_param = oh.make_tensor_value_info("add_param", TensorProto.FLOAT, [1, 1]) @@ -103,6 +105,7 @@ def test_move_scalar_add_past_matmul(): assert new_model.graph.node[0].output[0] == new_model.graph.node[1].input[0] +@pytest.mark.streamline @pytest.mark.parametrize( "test_args", [("Add", MoveScalarAddPastMatMul()), ("Mul", MoveScalarMulPastMatMul())], diff --git a/tests/transformation/streamline/test_move_transpose_past_scalar_mul.py b/tests/transformation/streamline/test_move_transpose_past_scalar_mul.py index 9110ede98da81a627127767276db33362503ef84..ad174a4909202f2d62fa2a3c31a7da8ead900e0b 100644 --- a/tests/transformation/streamline/test_move_transpose_past_scalar_mul.py +++ b/tests/transformation/streamline/test_move_transpose_past_scalar_mul.py @@ -13,6 +13,7 @@ from finn.transformation.infer_shapes import InferShapes from finn.transformation.streamline.reorder import MoveTransposePastScalarMul +@pytest.mark.streamline # permutation of transpose node @pytest.mark.parametrize("perm", [[0, 2, 3, 1], [0, 1, 3, 2], [3, 2, 0, 1]]) # scalar mul diff --git a/tests/transformation/streamline/test_round_thresholds.py b/tests/transformation/streamline/test_round_thresholds.py index 2e57f1c85f6ac197ca7a4cf15e595c34cc0fb564..3a533b0694fa81bae846d2d2f6e8dbcb41a8ee6c 100644 --- a/tests/transformation/streamline/test_round_thresholds.py +++ b/tests/transformation/streamline/test_round_thresholds.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import numpy as np from onnx import TensorProto, helper @@ -35,6 +37,7 @@ from finn.core.modelwrapper import ModelWrapper from finn.transformation.streamline import RoundAndClipThresholds +@pytest.mark.streamline def test_round_thresholds(): v = helper.make_tensor_value_info("v", TensorProto.FLOAT, [1, 4]) thresholds = helper.make_tensor_value_info("thresholds", TensorProto.FLOAT, [4, 1]) diff --git a/tests/transformation/streamline/test_sign_to_thres.py b/tests/transformation/streamline/test_sign_to_thres.py index 2ffb5713c0363b115dee5c41484fb5826faf803a..aa9254e8d605bbcd1d8a61da4d79cc6d582a1764 100644 --- a/tests/transformation/streamline/test_sign_to_thres.py +++ b/tests/transformation/streamline/test_sign_to_thres.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import brevitas.onnx as bo import onnx import onnx.numpy_helper as nph @@ -42,6 +44,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_sign_to_thres.onnx" +@pytest.mark.streamline def test_sign_to_thres(): lfc = get_test_model_trained("LFC", 1, 1) bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path) diff --git a/tests/transformation/streamline/test_streamline_cnv.py b/tests/transformation/streamline/test_streamline_cnv.py index ed2595330323bfc8a576af36ae3fea27522ec66c..f2c4921c9ae55fa2206abbbb2661fe20e6068b93 100644 --- a/tests/transformation/streamline/test_streamline_cnv.py +++ b/tests/transformation/streamline/test_streamline_cnv.py @@ -50,6 +50,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = make_build_dir("test_streamline_cnv_") +@pytest.mark.streamline # act bits @pytest.mark.parametrize("abits", [1, 2]) # weight bits diff --git a/tests/transformation/streamline/test_streamline_fc.py b/tests/transformation/streamline/test_streamline_fc.py index 3563b87c45a7ffe99fe6e9bdfd9f54a39e89cb68..875a1c46029b83f59211556dc79c9bac26ff927f 100644 --- a/tests/transformation/streamline/test_streamline_fc.py +++ b/tests/transformation/streamline/test_streamline_fc.py @@ -51,6 +51,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = make_build_dir("test_streamline_fc_") +@pytest.mark.streamline # act bits @pytest.mark.parametrize("abits", [1, 2]) # weight bits diff --git a/tests/transformation/test_batchnorm_to_affine_bnn_pynq.py b/tests/transformation/test_batchnorm_to_affine_bnn_pynq.py index 300ef85faacf664b89c7b949ea2e462f110eef85..bdb988e2aa508ed7464aee33d30b671fa38ebacb 100644 --- a/tests/transformation/test_batchnorm_to_affine_bnn_pynq.py +++ b/tests/transformation/test_batchnorm_to_affine_bnn_pynq.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import pkg_resources as pk import brevitas.onnx as bo @@ -44,7 +46,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_output_bn2affine.onnx" - +@pytest.mark.transform def test_batchnorm_to_affine_cnv_w1a1(): lfc = get_test_model_trained("CNV", 1, 1) bo.export_finn_onnx(lfc, (1, 3, 32, 32), export_onnx_path) @@ -69,6 +71,7 @@ def test_batchnorm_to_affine_cnv_w1a1(): os.remove(export_onnx_path) +@pytest.mark.transform def test_batchnorm_to_affine_lfc_w1a1(): lfc = get_test_model_trained("LFC", 1, 1) bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path) diff --git a/tests/transformation/test_infer_data_layouts_cnv.py b/tests/transformation/test_infer_data_layouts_cnv.py index 10bc687d13d4a85ce64955cb38c1c0dfdc6d53da..fc26a7edce02198e2534dcb5bf56c500719ccec1 100644 --- a/tests/transformation/test_infer_data_layouts_cnv.py +++ b/tests/transformation/test_infer_data_layouts_cnv.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import brevitas.onnx as bo import os @@ -45,7 +47,7 @@ from finn.util.test import get_test_model_trained export_onnx_path_cnv = "test_infer_data_layouts.onnx" - +@pytest.mark.transform def test_infer_data_layouts_cnv(): cnv = get_test_model_trained("CNV", 1, 1) bo.export_finn_onnx(cnv, (1, 3, 32, 32), export_onnx_path_cnv) diff --git a/tests/transformation/test_infer_datatypes_lfc.py b/tests/transformation/test_infer_datatypes_lfc.py index 8883dac7a54eafaaa768c8ae991b2030e385b318..3758485860cf0176143fe6f55b71508327ffe762 100644 --- a/tests/transformation/test_infer_datatypes_lfc.py +++ b/tests/transformation/test_infer_datatypes_lfc.py @@ -26,6 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import pytest + import brevitas.onnx as bo import os @@ -40,6 +42,7 @@ from finn.util.test import get_test_model_trained export_onnx_path = "test_infer_datatypes.onnx" +@pytest.mark.transform def test_infer_datatypes_lfc(): lfc = get_test_model_trained("LFC", 1, 1) bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path) diff --git a/tests/transformation/test_qonnx_to_finn.py b/tests/transformation/test_qonnx_to_finn.py index df7d63e3d2e139077f0fa20b10714c0a43a24e47..d9443e381677273d15bcb06832b009990a6ad11a 100644 --- a/tests/transformation/test_qonnx_to_finn.py +++ b/tests/transformation/test_qonnx_to_finn.py @@ -88,6 +88,7 @@ def analysis_testing_for_no_quant_nodes(model): return dict() +@pytest.mark.transform # This test currently takes about 4 min and 20 seconds @pytest.mark.parametrize("abits", [1, 2]) @pytest.mark.parametrize("wbits", [1, 2]) diff --git a/tests/util/test_build_dataflow.py b/tests/util/test_build_dataflow.py index de1b3abcc314c0c1451bd86bab8a7b93600ca697..d33a4f2fd6c974b13ac315c7ef621eacb04002c4 100644 --- a/tests/util/test_build_dataflow.py +++ b/tests/util/test_build_dataflow.py @@ -39,7 +39,7 @@ from finn.util.basic import make_build_dir @pytest.mark.slow @pytest.mark.vivado -def test_build_dataflow_directory(): +def test_end2end_build_dataflow_directory(): test_dir = make_build_dir("test_build_dataflow_directory_") target_dir = test_dir + "/build_dataflow" example_data_dir = pk.resource_filename("finn.qnn-data", "build_dataflow/") diff --git a/tests/util/test_create.py b/tests/util/test_create.py index c11e60175ea3ac94b6686ec5f8401a7c134fe53e..655c01f06eecca84d414ce3b995cfe4d1ba58170 100644 --- a/tests/util/test_create.py +++ b/tests/util/test_create.py @@ -32,6 +32,7 @@ import finn.util.create as create from finn.core.datatype import DataType +@pytest.mark.util @pytest.mark.parametrize( "bitwidth", [DataType["BIPOLAR"], DataType["INT2"], DataType["INT4"]] ) diff --git a/tests/util/test_data_packing_hls.py b/tests/util/test_data_packing_hls.py index 7113a3051bffb568e36b01af59945f0956658f76..2b67ce26e10f0d672f49d10169adb0b3fa5427fd 100644 --- a/tests/util/test_data_packing_hls.py +++ b/tests/util/test_data_packing_hls.py @@ -38,6 +38,7 @@ from finn.core.datatype import DataType from finn.util.data_packing import numpy_to_hls_code +@pytest.mark.util @pytest.mark.parametrize( "dtype", [ @@ -99,7 +100,7 @@ def test_npy2apintstream(test_shape, dtype): g++ -o test_npy2apintstream test.cpp /workspace/cnpy/cnpy.cpp \ -I/workspace/cnpy/ -I{}/include -I/workspace/finn/src/finn/qnn-data/cpp \ --std=c++11 -lz""".format( - os.environ["VIVADO_PATH"] + os.environ["HLS_PATH"] ) with open(test_dir + "/compile.sh", "w") as f: f.write(cmd_compile) @@ -123,6 +124,7 @@ g++ -o test_npy2apintstream test.cpp /workspace/cnpy/cnpy.cpp \ assert success +@pytest.mark.util def test_numpy_to_hls_code(): def remove_all_whitespace(s): return "".join(s.split())