Skip to content

Commit

Permalink
Amber docker updates (#962)
Browse files Browse the repository at this point in the history
* trying to clean up if-then-else jungle
* only whitespace diffs, see git diff -w
* new way to update docker for amber. is it better? i dunno
* oops no do not want to change how garnet is handled in gen_rtl
* CI test no longer overrides gen_rtl.sh docker choice
* Remove env-var docker-image-override mechanism
* Replace nonexistant archipelago commit
* kratos update requires pip install
* exit codes why not
* final cleanup
* cleaned up and updated gen_rtl.sh
* Prepare to adapt test_amber_rtl_build as a docker-based standalone check
* temporarily turn off docker pruning for fast test turnaround
* AN_CELL/AO_CELL hack for gemstone
* clean up some if statements
* MUCH simpler docker-amber-update mechanism
* separate amber-updates function in gen_rtl.sh
* Reference design(s) for RTL golden test(s), gzipped to save space
* prettified some comments
* little cleanup in comments and such
* final tweaks
  • Loading branch information
steveri committed May 21, 2023
1 parent bc713cc commit 2f9c9e2
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 59 deletions.
130 changes: 71 additions & 59 deletions mflowgen/common/rtl/gen_rtl.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#!/bin/bash
set -e; # DIE if any of the below commands exits with error status

# How does amber config differ from onyx default docker "garnet/latest"?
# (Optimally this would be the empty set)
# Generated by e.g. aha-fresh script
function get_amber_updates {
echo '
cd /aha/MetaMapper && git checkout e25b6f6;
cd /aha/archipelago && git checkout f35e6e5;
cd /aha/canal && git checkout 6fec524;
cd /aha/clockwork && git checkout efdd95e;
cd /aha/gemstone && git checkout 28b10a6;
cd /aha/kratos && git checkout 873b369;
cd /aha/lake && git checkout 837ffe1;
cd /aha/kratos && rm -rf build;
source /aha/bin/activate && cd /aha/kratos && pip install . --no-cache-dir --no-binary kratos;
'
}
# Option to simply print update commands and exit
if [ "$1" == "--get-amber-updates-only" ]; then
get_amber_updates
exit
fi

echo '--- gen_rtl BEGIN' `date +%H:%M`

# Hierarchical flows can accept RTL as an input from parent graph
Expand All @@ -17,7 +39,7 @@ if [ -f ../inputs/design.v ]; then
fi

# Did not find existing design.v. Generate a new one UNLESS soc_only is true
if [ $soc_only = True ]; then
if [ "$soc_only" == True ]; then
echo "soc_only set to true. Garnet not included"
touch outputs/design.v
echo '--- gen_rtl END' `date +%H:%M`
Expand All @@ -40,24 +62,30 @@ flags+=" -v --glb_tile_mem_size $glb_tile_mem_size"
[ "$WHICH_SOC" != "amber" ] && flags+=" --rv --sparse-cgra --sparse-cgra-combined"

# Default is power-aware, but can be turned off
[ $PWR_AWARE == False ] && flags+=" --no-pd"
[ "$PWR_AWARE" == False ] && flags+=" --no-pd"

# Where/when is this used?
[ $interconnect_only == True ] && flags+=" --interconnect-only"
[ "$interconnect_only" == True ] && flags+=" --interconnect-only"

# Use aha docker container for all dependencies
if [ $use_container == True ]; then
if [ "$use_container" == True ]; then
echo "Use aha docker container for all dependencies"

# Clone AHA repo
echo '--- gen_rtl aha clone BEGIN' `date +%H:%M`
git clone https://github.com/StanfordAHA/aha.git
cd aha
########################################################################
# What!!? No!!! Why??? Never used??
#
# # Clone AHA repo
# echo '--- gen_rtl aha clone BEGIN' `date +%H:%M`
# git clone https://github.com/StanfordAHA/aha.git
# cd aha
#
# Again: why?
mkdir -p aha; cd aha
########################################################################

# Prune docker images...
# ("yes" emits endless stream of y's)
echo '--- gen_rtl docker prune BEGIN' `date +%H:%M`
yes | docker image prune -a --filter "until=6h" --filter=label='description=garnet' || true
docker image prune -f -a --filter "until=6h" --filter=label='description=garnet' || true

echo ""; echo "After pruning:"; echo ""
docker images; echo ""
Expand Down Expand Up @@ -97,14 +125,14 @@ if [ $use_container == True ]; then
# Mount /cad and name it, and run container as a daemon in background
# Use container_name "gen_rtl_<proc_id>"

echo "Using docker container '$container_name'"
container_name=gen_rtl_$$
echo "Using docker container '$container_name'"
docker run -id --name ${container_name} --rm -v /cad:/cad ${rtl_docker_image} bash

# MAKE SURE the docker container gets killed when this script dies.
trap "docker kill $container_name" EXIT

if [ $use_local_garnet == True ]; then
if [ "$use_local_garnet" == True ]; then
docker exec $container_name /bin/bash -c "rm -rf /aha/garnet"
# Clone local garnet repo to prevent copying untracked files
git clone $GARNET_HOME ./garnet
Expand All @@ -113,68 +141,36 @@ if [ $use_container == True ]; then

# Update container for amber config if necessary
if [ "$WHICH_SOC" == "amber" ]; then
echo "+++ Updating container with amber diffs"

# N'EXISTE PAS
# archipelago ec505e6 80 (a1,b79) kuree/archipelago

# How does amber config differ from onyx default?
# Generated by e.g. aha-fresh script
# (Optimally this would be the empty set :( )
amber_diffs='
Halide-to-Hardware e8798fa 84 (a2,b82) stanfordaha/Halide-to-Hardware
MetaMapper e25b6f6 1 (a0,b1) rdaly525/MetaMapper
archipelago f35e6e5
canal 6fec524 19 (a19,b0) stanfordaha/canal
clockwork efdd95e 135 (a0,b135) dillonhuff/clockwork
gemstone 28b10a6 9 (a9,b0) stanfordaha/gemstone
kratos 873b369 91 (a91,b0) kuree/kratos
lake 837ffe1 2 (a2,b0) StanfordAHA/lake
lassen 70edcf3 2 (a2,b0) stanfordaha/lassen
mantle fb8532a 2 (a2,b0) phanrahan/mantle
peak 9a24cf7 10 (a10,b0) cdonovick/peak
pycoreir 80e072e 30 (a30,b0) leonardt/pycoreir
'
echo "$amber_diffs"
echo "------------------------------------------------------------------------"
echo "+++ Updating container with amber updates"

echo "BEFORE:"
docker exec $container_name /bin/bash -c "git submodule status"
echo "------------------------------------------------------------------------"
# E.g. updates="cd /aha/garnet && git checkout 9982932;
# cd /aha/gemstone && git checkout 28b10a6;
# cd /aha/kratos && git checkout 873b369;
# cd /aha/lake && git checkout 837ffe1;"

updates=`echo "$amber_diffs" | awk 'NF>=2{printf( "cd /aha/%-18s && git checkout %s\n", $1, $2 )}'`
amber_updates=`get_amber_updates`

# E.g. updates="cd /aha/garnet && git checkout 9982932
# cd /aha/gemstone && git checkout 28b10a6
# cd /aha/kratos && git checkout 873b369
# cd /aha/lake && git checkout 837ffe1"
echo "$amber_updates"
echo "------------------------------------------------------------------------"

echo "$updates"
echo "BEFORE:"
docker exec $container_name /bin/bash -c "git submodule status"
echo "------------------------------------------------------------------------"

echo "$updates" | while read u; do
echo "docker exec $container_name /bin/bash -c '$u'"
echo "$amber_updates" | while read u; do
[ "$u" == "" ] && continue # Skip blank lines
echo docker exec CONTAINER /bin/bash -c \"$u\"
docker exec $container_name /bin/bash -c "$u"
printf "\n"
# kratos update requires pip install because reasons
if expr "$u" : "cd /aha/kratos" > /dev/null; then
echo "docker exec $container_name /bin/bash -c 'cd /aha/kratos & pip install .)'"
docker exec $container_name /bin/bash -c \
"source /aha/bin/activate; cd /aha/kratos && pip install ." || echo okay
fi
printf "\n"
done

echo "------------------------------------------------------------------------"
echo "AFTER:"
docker exec $container_name /bin/bash -c "git submodule status"
echo "------------------------------------------------------------------------"
echo "--- continue..."
fi

#docker exec $container_name /bin/bash -c "cd /aha && git checkout master && git pull && git submodule update"
#docker exec $container_name /bin/bash -c "cd /aha/lake/ && git fetch origin && git checkout sparse_strawman && git pull"
#docker exec $container_name /bin/bash -c "cd /aha/kratos/ && git checkout master && git pull && DEBUG=1 pip install -e ."

# run garnet.py in container and concat all verilog outputs
echo "---docker exec $container_name"

Expand Down Expand Up @@ -209,6 +205,9 @@ if [ $use_container == True ]; then
source /aha/bin/activate; # Set up the build environment
# FIXME note that 'if' statements below will fail if e.g. 'interconnect_only' not set
# Easy fix is not obvious since we are already inside a double-quoted string :(
if [ $interconnect_only == True ]; then
echo --- INTERCONNECT_ONLY: aha garnet $flags
aha garnet $flags; # Here is where we build the verilog for the main chip
Expand Down Expand Up @@ -246,12 +245,25 @@ if [ $use_container == True ]; then

# Copy the concatenated design.v output out of the container
docker cp $container_name:/aha/garnet/design.v ../outputs/design.v

# FIXME there might be a more elegant solution for this, see e.g.
# /aha/gemstone/tests/common/rtl/{AN_CELL.sv,AO_CELL.sv}
if [ "$WHICH_SOC" == "amber" ]; then
echo '--- AN_CELL hack'
# docker cp $container_name:/aha/garnet/design.v ../outputs/tmp$$.v
cat ../outputs/design.v \
| sed 's/AN_CELL inst/ AN2D0BWP16P90 inst/' \
| sed 's/AO_CELL inst/ AO22D0BWP16P90 inst/' \
> ../outputs/tmp$$.v
mv ../outputs/tmp$$.v ../outputs/design.v
fi

if [ $glb_only == True ]; then
docker cp $container_name:/aha/garnet/global_buffer/header ../outputs/header
elif [ $interconnect_only == False ]; then
docker cp $container_name:/aha/garnet/global_buffer/header ../glb_header
docker cp $container_name:/aha/garnet/global_controller/header ../glc_header
mkdir ../outputs/header
mkdir -p ../outputs/header
cp -r ../glb_header/* ../outputs/header/
cp -r ../glc_header/* ../outputs/header/
fi
Expand Down
172 changes: 172 additions & 0 deletions tests/test_amber_rtl_build/amber-rtl-build-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#!/bin/bash

cmd=$0
cmd=amber-rtl-build-check.sh

function HELP { cat <<EOF
DESCRIPTION: Builds RTL for a 4x2 amber grid, compares to reference build.
USAGE (default is "--local"):
$cmd --docker # Use gen_rtl.sh to build design.v from inside a container
$cmd --local # Generate design.v locally i.e. maybe we're already in a container
EXAMPLE test from outside docker
cd /tmp/scratch # (optional)
$cmd --docker && echo PASS || echo FAIL
EXAMPLE test from inside docker
cd /tmp/scratch # (optional)
# 1. Setup / fire up a docker container
image=stanfordaha/garnet:latest
container=deleteme
docker pull \$image
docker run -id --name \$container --rm -v /cad:/cad \$image bash
function dexec { docker exec \$container /bin/bash -c "\$*"; }
# 2a. Run the test using garnet branch e.g. "amber-docker-updates"
dexec "cd /aha/garnet && git fetch origin && git checkout origin/amber-docker-updates"
dexec "/aha/garnet/mflowgen/common/rtl/gen_rtl.sh --get-amber-updates-only > tmp"
dexec "set -x; source tmp"
dexec "/aha/garnet/tests/test_amber_rtl_build/amber-rtl-build-check.sh --local"
# 2b. OR run the test using local copy of repo
# Setup
GARNET_HOME=/nobackup/steveri/github/garnet
< See item 1 above for rest of setup>
# Copy local repo to docker container
dexec "rm -rf /aha/garnet"
git clone \$GARNET_HOME /tmp/garnet
docker cp /tmp/garnet \$container:/aha/garnet
/bin/rm -rf /tmp/garnet
# Update the docker container for amber build
updates=\`dexec "/aha/garnet/mflowgen/common/rtl/gen_rtl.sh --get-amber-updates-only"\`
dexec "set -x; \$updates"
# Run the test
bc=/aha/garnet/tests/test_amber_rtl_build/amber-rtl-build-check.sh
dexec "\$bc --local"
EOF
}

[ "$1" == "--help" ] && HELP && exit

# Always debug (for now)
# Use "-v" as first arg if want extra debug info
# [ "$1" == "-v" ] && shift && set -x

# width=32 # slow 32x16
width=4 # quick 4x2
height=$((width/2))

# E.g. if script is "$GARNET_HOME/tests/test_amber_rtl_build/amber-rtl-build-check.sh"
# then scriptdir is "$GARNET_HOME/tests/test_amber_rtl_build"
scriptpath=$0
scriptpath=`readlink $scriptpath || echo $scriptpath` # Full path of script dir
scriptdir=${scriptpath%/*} # E.g. "build_tarfile.sh" or "foo/bar"

echo '--- RTL test BEGIN' `date`

if [ "$1" == "--docker" ]; then

# Use gen_rtl.sh to build a docker environment and use that to build the RTL
export array_width=$width
export array_height=$height
export glb_tile_mem_size=256
export num_glb_tiles=16
export pipeline_config_interval=8
export interconnect_only=False
export glb_only=False
export soc_only=False
export PWR_AWARE=True
export use_container=True

# export use_local_garnet=True
export use_local_garnet=False # for now

export save_verilog_to_tmpdir=False
export rtl_docker_image=default

export WHICH_SOC=amber

export GARNET_HOME=`cd $scriptdir/../..; pwd`
echo "--- Found GARNET_HOME=$GARNET_HOME"

# Build a docker environment and use that to build RTL "outputs/design.v"
# (gen_rtl.sh copies final design.v to "./outputs" subdirectory)
mkdir -p outputs
$GARNET_HOME/mflowgen/common/rtl/gen_rtl.sh
mv outputs/design.v .

else
# RTL-build flags
flags="--width $width --height $((width/2)) --pipeline_config_interval 8 -v --glb_tile_mem_size 256"
echo "FLAGS: $flags"

# Prep/clean
cd /aha
rm -rf garnet/genesis_verif
rm -f garnet/garnet.v

# Build new rtl
export WHICH_SOC='amber'
source /aha/bin/activate; # Set up the build environment
aha garnet $flags

# Assemble final design.v
cd /aha/garnet
cp garnet.v genesis_verif/garnet.v
cat genesis_verif/* > design.v
cat global_buffer/systemRDL/output/glb_pio.sv >> design.v
cat global_buffer/systemRDL/output/glb_jrdl_decode.sv >> design.v
cat global_buffer/systemRDL/output/glb_jrdl_logic.sv >> design.v
cat global_controller/systemRDL/output/*.sv >> design.v

# For better or worse: I put this in gen_rtl.sh
# Hack it up! FIXME should use same mechanism as onyx...define AO/AN_CELL
# Also see: garnet/mflowgen/common/rtl/gen_rtl.sh, gemstone/tests/common/rtl/{AN_CELL.sv,AO_CELL.sv}
cat design.v \
| sed 's/AN_CELL inst/AN2D0BWP16P90 inst/' \
| sed 's/AO_CELL inst/AO22D0BWP16P90 inst/' \
> /tmp/tmp.v
mv -f /tmp/tmp.v design.v
fi

printf "\n"
echo "+++ Compare result to reference build"

# Reference designs are gzipped to saze space
ref=garnet-4x2.v
cp $scriptdir/ref/$ref.gz .; gunzip $ref.gz
f1=design.v; f2=$ref

# Need 'sed s/unq...' to handle the case where both designs are
# exactly the same but different "unq" suffixes e.g.
# < Register_unq3 Register_inst0 (
# > Register_unq2 Register_inst0 (
function vcompare { sort $1 | sed 's/,$//' | sed 's/unq[0-9*]/unq/'; }

ndiffs=`diff -I Date <(vcompare $f1) <(vcompare $f2) | wc -l`

if [ "$ndiffs" != "0" ]; then

# ------------------------------------------------------------------------
# TEST FAILED

printf "Test FAILED with $ndiffs diffs\n\n"
printf "Top 40 diffs:"
diff -I Date <(vcompare $f1) <(vcompare $f2) | head -40
exit 13
fi

echo "Test PASSED"



Binary file added tests/test_amber_rtl_build/ref/garnet-4x2.v.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2f9c9e2

Please sign in to comment.