From 1621b9b67c6b0cb85bd449309863f8583b1e9acb Mon Sep 17 00:00:00 2001 From: Erika Hunhoff Date: Fri, 19 Jul 2024 14:57:17 -0600 Subject: [PATCH] migrate passthrough_kernel to use xrtrunner --- .../passthrough/passthrough_kernel/Makefile | 9 ++- .../passthrough_kernel/passthrough_kernel.py | 71 +++++++++++++------ .../passthrough/passthrough_kernel/run.py | 43 ----------- 3 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 programming_examples/passthrough/passthrough_kernel/run.py diff --git a/programming_examples/passthrough/passthrough_kernel/Makefile b/programming_examples/passthrough/passthrough_kernel/Makefile index 2e0ca87ff..5408373e7 100644 --- a/programming_examples/passthrough/passthrough_kernel/Makefile +++ b/programming_examples/passthrough/passthrough_kernel/Makefile @@ -19,13 +19,16 @@ VPATH := ${MLIR_AIE_DIR}/aie_kernels/generic all: run +print: + ${powershell} python3 ${srcdir}/passthrough_kernel.py -p + build/passThrough.cc.o: ${VPATH}/passThrough.cc mkdir -p ${@D} cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -DBIT_WIDTH=8 -c $< -o ${@F} run: build/passThrough.cc.o - mkdir -p build - cd build && ${powershell} python3 ${srcdir}/run.py + mkdir -p ${srcdir}/build + cd ${srcdir}/build && ${powershell} python3 ${srcdir}/passthrough_kernel.py clean: - rm -rf ${srcdir}/build ${srcdir}/__pycache__ + rm -rf ${srcdir}/build ${srcdir}/__pycache__ \ No newline at end of file diff --git a/programming_examples/passthrough/passthrough_kernel/passthrough_kernel.py b/programming_examples/passthrough/passthrough_kernel/passthrough_kernel.py index 4e2f88457..5cf401b24 100644 --- a/programming_examples/passthrough/passthrough_kernel/passthrough_kernel.py +++ b/programming_examples/passthrough/passthrough_kernel/passthrough_kernel.py @@ -1,36 +1,27 @@ # Copyright (C) 2024, Advanced Micro Devices, Inc. # SPDX-License-Identifier: MIT -import sys -from pathlib import Path # if you haven't already done so - -# Python paths are a bit complex. Taking solution from : https://stackoverflow.com/questions/16981921/relative-imports-in-python-3 -file = Path(__file__).resolve() -parent, root = file.parent, file.parents[1] -sys.path.append(str(root)) - -# Additionally remove the current file's directory from sys.path -try: - sys.path.remove(str(parent)) -except ValueError: # Already removed - pass +import argparse +import numpy as np from air.ir import * from air.dialects.air import * from air.dialects.memref import AllocOp, DeallocOp from air.dialects.func import FuncOp from air.dialects.scf import for_, yield_ +from air.backend.xrt_runner import XRTRunner range_ = for_ -from common import * +INOUT_DATATYPE = np.uint8 +INOUT_ELEM_SIZE = np.dtype(INOUT_DATATYPE).itemsize @module_builder -def build_module(vector_size): - assert vector_size % NUM_VECTORS == 0 +def build_module(vector_size, num_subvectors): + assert vector_size % num_subvectors == 0 # chop input in 4 sub-tensors - lineWidthInBytes = vector_size // NUM_VECTORS + lineWidthInBytes = vector_size // num_subvectors # Type and method of input/output memrefTyInOut = T.memref(vector_size, T.ui8()) @@ -70,7 +61,7 @@ def segment_body(): @herd(name="copyherd", sizes=[1, 1], link_with="passThrough.cc.o") def herd_body(tx, ty, sx, sy): - for i in range_(NUM_VECTORS): + for i in range_(num_subvectors): # We must allocate a buffer of image size for the input/output tensor_in = AllocOp(tensor_type, [], []) tensor_out = AllocOp(tensor_type, [], []) @@ -92,5 +83,45 @@ def herd_body(tx, ty, sx, sy): if __name__ == "__main__": - module = build_module() - print(module) + parser = argparse.ArgumentParser( + prog="run.py", + description="Builds, runs, and tests the passthrough_dma example", + ) + parser.add_argument( + "-s", + "--vector_size", + type=int, + default=4096, + help="The size (in bytes) of the data vector to passthrough", + ) + parser.add_argument( + "--subvector_size", + type=int, + default=4, + help="The number of sub-vectors to break the vector into", + ) + parser.add_argument( + "-v", + "--verbose", + action="store_true", + ) + parser.add_argument( + "-p", + "--print-module-only", + action="store_true", + ) + args = parser.parse_args() + + mlir_module = build_module(args.vector_size, args.subvector_size) + if args.print_module_only: + print(mlir_module) + exit(0) + + input_a = np.arange(1, args.vector_size + 1, dtype=INOUT_DATATYPE) + output_b = np.arange(1, args.vector_size + 1, dtype=INOUT_DATATYPE) + for i in range(args.vector_size): + input_a[i] = i % 0xFF + output_b[i] = i % 0xFF + + runner = XRTRunner(verbose=args.verbose) + exit(runner.run_test(mlir_module, inputs=[input_a], expected_outputs=[output_b])) diff --git a/programming_examples/passthrough/passthrough_kernel/run.py b/programming_examples/passthrough/passthrough_kernel/run.py deleted file mode 100644 index 2b6008141..000000000 --- a/programming_examples/passthrough/passthrough_kernel/run.py +++ /dev/null @@ -1,43 +0,0 @@ -# run.py -*- Python -*- -# -# Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved. -# SPDX-License-Identifier: MIT -import argparse -import sys -from pathlib import Path # if you haven't already done so - -# Python paths are a bit complex. Taking solution from : https://stackoverflow.com/questions/16981921/relative-imports-in-python-3 -file = Path(__file__).resolve() -parent, root = file.parent, file.parents[1] -sys.path.append(str(root)) - -# Additionally remove the current file's directory from sys.path -try: - sys.path.remove(str(parent)) -except ValueError: # Already removed - pass - -from common import test_main -from passthrough_kernel.passthrough_kernel import build_module - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - prog="run.py", - description="Builds, runs, and tests the passthrough/passthrough_kernel example", - ) - parser.add_argument( - "-s", - "--vector_size", - type=int, - default=4096, - help="The size (in bytes) of the data vector to passthrough", - ) - parser.add_argument( - "-v", - "--verbose", - action="store_true", - ) - args = parser.parse_args() - test_main( - build_module, args.vector_size, experimental_passes=True, verbose=args.verbose - )