diff --git a/.gitignore b/.gitignore index 9281abdb7..5de7b8d20 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ transcript gmon.out .ipynb_checkpoints +site/ +work.lib++/ +target/sim/gemm_results.csv diff --git a/target/sim/Makefile b/target/sim/Makefile index 7bb198372..14ed6948e 100644 --- a/target/sim/Makefile +++ b/target/sim/Makefile @@ -240,7 +240,7 @@ test/testharness.sv: test/testharness.sv.tpl $(CFG) # @echo "[SOLDER] Device Tree" # @$(OCCAMYGEN) --cfg $(CFG) --outdir . --dts test/occamy.dts -$(TARGET_PLIC_DIR) $(TARGET_CLINT_DIR) $(TARGET_SOCCTRL_DIR) $(TARGET_HBMCTRL_DIR) $(TARGET_QUADCTRL_DIR): +$(TARGET_PLIC_DIR) $(TARGET_CLINT_DIR) $(TARGET_SOCCTRL_DIR) $(TARGET_HBMCTRL_DIR) $(TARGET_QUADCTRL_DIR) ${VSIM_BUILDDIR} ${LOGS_DIR}: @mkdir -p $@ CLINTROOT = $(shell $(BENDER) path clint) @@ -426,7 +426,7 @@ PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_hbm_xbar_peripheral.h .PHONY: sw clean-headers clean-sw -sw: $(PLATFORM_HEADERS) +sw: $(PLATFORM_HEADERS) | ${LOGS_DIR} $(MAKE) -C sw/ all clean-headers: @@ -529,7 +529,7 @@ ${VSIM_BUILDDIR}/compile.vsim.tcl: $(VSIM_SOURCES) ${TB_SRCS} ${TB_CC_SOURCES} t echo 'return 0' >> $@ # Build compilation script and compile all sources for Questasim simulation -$(BIN_DIR)/occamy_top.vsim: ${VSIM_BUILDDIR}/compile.vsim.tcl work/lib/libfesvr.a +$(BIN_DIR)/occamy_top.vsim: ${VSIM_BUILDDIR}/compile.vsim.tcl work/lib/libfesvr.a | ${VSIM_BUILDDIR} $(call QUESTASIM,tb_bin) @# Rename CVA6 trace to align with Snitch trace names @echo "mv ${CVA6_TRACE} $(LOGS_DIR)/trace_hart_00000.txt" >> $@ diff --git a/target/sim/bench.ipynb b/target/sim/bench.ipynb index cefbbb282..eb08171a4 100644 --- a/target/sim/bench.ipynb +++ b/target/sim/bench.ipynb @@ -329,6 +329,182 @@ "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "813a82e1-5683-497a-8603-545cb55baada", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46335c5f-0802-4541-8ebe-5f3ef2de12e4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "db94fbb4-44d3-4ca1-a271-5be3d0f904f5", + "metadata": {}, + "source": [ + "# Verify.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "002be39d-43cf-4b35-b939-b3e66f7965fb", + "metadata": {}, + "outputs": [], + "source": [ + "args = {}\n", + "args['sim_bin'] = \"bin/occamy_top.vsim\"\n", + "args['snitch_bin'] = \"sw/host/apps/offload/build/offload-gemm.elf\"\n", + "args['symbols_bin'] = \"sw/device/apps/blas/gemm/build/gemm.elf\"\n", + "args['log'] = None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62cd158c-0494-444b-bc86-de08c0681e6e", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "import os\n", + "from pathlib import Path\n", + "import numpy as np\n", + "\n", + "sys.path.append(os.path.join(os.path.abspath(''), \"../../working_dir/snitch_cluster/sw/blas/gemm\"))\n", + "from data.datagen import golden_model\n", + "\n", + "sys.path.append(os.path.join(os.path.abspath(''), \"../../working_dir/snitch_cluster/util/sim/\"))\n", + "import verification # noqa: E402\n", + "from elf import Elf # noqa: E402\n", + "from data_utils import bytes_to_doubles, bytes_to_uint32s # noqa: E402" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "347b27bc-bba7-41d5-9e8a-bb7e57a44d88", + "metadata": {}, + "outputs": [], + "source": [ + "!make CFG_OVERRIDE=cfg/1Q2C.hjson DEBUG=ON sw" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "91c3a858-8e0c-448b-8ecf-fa8583bb1317", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "%%time\n", + "# Run simulation and get outputs\n", + "raw_results = verification.simulate(sim_bin=args['sim_bin'],\n", + " snitch_bin=args['snitch_bin'],\n", + " symbols_bin=args['symbols_bin'],\n", + " log=args['log'],\n", + " output_uids=['c'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab065250-6faf-41ff-9a69-1633e6abc948", + "metadata": {}, + "outputs": [], + "source": [ + "# Extract input operands from ELF file\n", + "if args['symbols_bin']:\n", + " elf = Elf(args['symbols_bin'])\n", + "else:\n", + " elf = Elf(args['snitch_bin'])\n", + "alpha = bytes_to_uint32s(elf.get_symbol_contents('ALPHA'))[0]\n", + "m = bytes_to_uint32s(elf.get_symbol_contents('M'))[0]\n", + "n = bytes_to_uint32s(elf.get_symbol_contents('N'))[0]\n", + "k = bytes_to_uint32s(elf.get_symbol_contents('K'))[0]\n", + "ta = bytes_to_uint32s(elf.get_symbol_contents('TA'))[0]\n", + "tb = bytes_to_uint32s(elf.get_symbol_contents('TB'))[0]\n", + "a = np.array(bytes_to_doubles(elf.get_symbol_contents('a')))\n", + "b = np.array(bytes_to_doubles(elf.get_symbol_contents('b')))\n", + "c = np.array(bytes_to_doubles(elf.get_symbol_contents('c'))).reshape((m, n))\n", + "result = np.array(bytes_to_doubles(elf.get_symbol_contents('result'))).reshape((m,n))\n", + "\n", + "# Extract results in output_uids\n", + "c_actual = np.array(bytes_to_doubles(raw_results['c'])).reshape((m,n))\n", + "\n", + "if ta:\n", + " a = a.reshape((k, m))\n", + " a = a.transpose()\n", + "else:\n", + " a = a.reshape((m, k))\n", + " \n", + "if tb:\n", + " b = b.reshape((n, k))\n", + " b = b.transpose()\n", + "else:\n", + " b = b.reshape((k, n))\n", + "\n", + "# Verify results\n", + "c_golden = golden_model(a, b, alpha, c)\n", + "\n", + "ERR_THRESHOLD = 0.001\n", + "absolute_err = np.absolute(c_golden - c_actual)\n", + "errors = np.count_nonzero(absolute_err > ERR_THRESHOLD)\n", + "\n", + "if (errors):\n", + " print(f'Failed with {errors} errors.')\n", + " # verification.dump_results_to_csv([c_golden, c_actual, absolute_err],\n", + " # Path.cwd() / 'gemm_results.csv')\n", + "else:\n", + " print('SUCCESS. No actual C matches result.')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d181eb78-2dc7-4f0a-a0d4-6f67e70da99d", + "metadata": {}, + "outputs": [], + "source": [ + "((absolute_err > ERR_THRESHOLD)*1).reshape((m,n))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a5a2483-3454-4d5a-9a40-0a0e5984f932", + "metadata": {}, + "outputs": [], + "source": [ + "c_actual" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21675e83-8f1c-481d-91bb-98cd1a5957eb", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfb3f426-7610-4901-9af6-d0487d173046", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {