Skip to content

Commit

Permalink
Use Lit for ML example makefiles (#1408)
Browse files Browse the repository at this point in the history
Co-authored-by: Gagandeep Singh <gagan.posted@gmail.com>
Co-authored-by: singagan <53442471+singagan@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kristof Denolf <kristof.denolf@amd.com>
  • Loading branch information
5 people authored Apr 25, 2024
1 parent 6c7255f commit a74a480
Show file tree
Hide file tree
Showing 29 changed files with 195 additions and 278 deletions.
21 changes: 13 additions & 8 deletions programming_examples/ml/bottleneck/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,41 @@
#
# Copyright (C) 2024, Advanced Micro Devices, Inc.

include ../../makefile-common
srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

include ${srcdir}/../../makefile-common

mlirFileName = aie

VPATH := ${srcdir}/../../../aie_kernels/aie2

all: build/conv2dk1.o build/conv2dk3.o build/conv2dk1_skip.o build/final.xclbin

build/${mlirFileName}.mlir: aie2.py
build/${mlirFileName}.mlir: ${srcdir}/aie2.py
mkdir -p ${@D}
python3 $< > $@

insts.txt: build/${mlirFileName}.mlir
aiecc.py -v --aie-only-generate-npu --npu-insts-name=$@ $<

build/conv2dk1.o: ../../../aie_kernels/aie2/conv2dk1.cc
build/conv2dk1.o: conv2dk1.cc
xchesscc -d ${CHESSCC2_FLAGS} -DINT8_ACT -c $< -o $@

build/conv2dk3.o: ../../../aie_kernels/aie2/conv2dk3.cc
build/conv2dk3.o: conv2dk3.cc
xchesscc -d ${CHESSCC2_FLAGS} -DUINT8_ACT -c $< -o $@

build/conv2dk1_skip.o: ../../../aie_kernels/aie2/conv2dk1_skip.cc
build/conv2dk1_skip.o: conv2dk1_skip.cc
xchesscc -d ${CHESSCC2_FLAGS} -DINT8_ACT -c $< -o $@

build/final.xclbin: build/${mlirFileName}.mlir
build/final.xclbin: build/${mlirFileName}.mlir build/conv2dk1.o build/conv2dk3.o build/conv2dk1_skip.o
cd build && aiecc.py -v --aie-generate-cdo --aie-generate-npu --no-compile-host \
--xclbin-name=${@F} --npu-insts-name=insts.txt $(<:%=../%)
--basic-alloc-scheme \
--xclbin-name=${@F} --npu-insts-name=insts.txt ${<F}

clean:
rm -rf build *.elf* *.lst *.bif ${mlirFileName}.mlir.prj log .xclbin sim \
chess* *.o insts.txt \
*.log aie_partition.json *.bin BOOT.BIN _x test.exe

run_py:
${powershell} python3 test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE
${powershell} python3 ${srcdir}/test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE
12 changes: 0 additions & 12 deletions programming_examples/ml/bottleneck/run.lit

This file was deleted.

9 changes: 9 additions & 0 deletions programming_examples/ml/bottleneck/run_makefile.lit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// (c) Copyright 2024 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// REQUIRES: ryzen_ai, chess, torch
//
// RUN: make -f %S/Makefile clean
// RUN: make -f %S/Makefile
// RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s
// CHECK: PASS!
11 changes: 7 additions & 4 deletions programming_examples/ml/bottleneck/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,17 @@ def forward(self, x):
# ------------------------------------------------------
print("\nAvg NPU time: {}us.".format(int((npu_time_total / num_iter) / 1000)))

assert np.allclose(
if np.allclose(
ofm_mem_fmt_out.detach().numpy(),
golden_output.detach().numpy(),
rtol=0,
atol=inp_scale4,
)

print("\nPASS!\n")
):
print("\nPASS!\n")
exit(0)
else:
print("\nFailed.\n")
exit(-1)


if __name__ == "__main__":
Expand Down
32 changes: 16 additions & 16 deletions programming_examples/ml/conv2d/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
#
# Copyright (C) 2024, Advanced Micro Devices, Inc.

include ../../makefile-common
srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

mlirFileName = aieWithTrace_1core
include ${srcdir}/../../makefile-common

VPATH := ../../../aie_kernels/aie2
mlirFileName = aie

VPATH := ${srcdir}/../../../aie_kernels/aie2

all: build/conv2dk1_i8.o build/final.xclbin

build/conv2dk1_i8.o: conv2dk1_i8.cc
mkdir -p ${@D}
cd ${@D} && xchesscc_wrapper ${CHESSCCWRAP2_FLAGS} -DINT8_ACT -c $< -o ${@F}

build/${mlirFileName}.mlir: aie2.py
build/${mlirFileName}.mlir: ${srcdir}/aie2.py
mkdir -p ${@D}
python3 $< > $@


insts.txt: build/${mlirFileName}.mlir
aiecc.py -v --aie-only-generate-npu --npu-insts-name=$@ $<

build/conv2dk1_i8.o: conv2dk1_i8.cc
xchesscc -d ${CHESSCC2_FLAGS} -DINT8_ACT -c $< -o $@

build/final.xclbin: build/${mlirFileName}.mlir
cd build && aiecc.py -v --aie-generate-cdo --aie-generate-npu --no-compile-host \
build/final.xclbin: build/${mlirFileName}.mlir build/conv2dk1_i8.o
mkdir -p ${@D}
cd ${@D} && aiecc.py -v --aie-generate-cdo --aie-generate-npu --no-compile-host \
--xclbin-name=${@F} --npu-insts-name=insts.txt $(<:%=../%)

run_py: build/final.xclbin build/insts.txt
${powershell} python3 ${srcdir}/test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE

clean:
rm -rf build *.elf* *.lst *.bif ${mlirFileName}.mlir.prj log* *.xclbin sim \
chess* *.o insts.txt \
*.log aie_partition.json *.bin BOOT.BIN _x test.exe
run_py:
${powershell} python3 test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE
*.log aie_partition.json *.bin BOOT.BIN _x test.exe
86 changes: 0 additions & 86 deletions programming_examples/ml/conv2d/aie2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
bufOut = actOut * 2 # double buffer
actOutInt32s = actOut // 4

enableTrace = False
trace_size = 16384
traceSizeInInt32s = trace_size // 4


def conv2dk1():
with mlir_mod_ctx() as ctx:
Expand Down Expand Up @@ -83,9 +79,6 @@ def device_body():
ComputeTile2 = tile(0, 2)
compute_tile2_col, compute_tile2_row = 0, 2

if enableTrace:
flow(ComputeTile2, WireBundle.Trace, 0, ShimTile, WireBundle.DMA, 1)

# AIE-array data movement with object fifos
# Input
of_inOF_act_L3L2 = object_fifo(
Expand Down Expand Up @@ -155,85 +148,6 @@ def core_body():

@FuncOp.from_py_func(tensor_ty, memRef_wts_ty, tensor_ty)
def sequence(I, W, O):
if enableTrace:
# 0x340D0: Trace Control 0
# 0xAABB---C
# AA <- Event to stop trace capture
# BB <- Event to start trace capture
# C <- Trace mode, 00=event=time, 01=event-PC, 10=execution
# Configure so that "Event 1" (always true) causes tracing to start
npu_write32(
column=compute_tile2_col,
row=compute_tile2_row,
address=0x340D0,
value=0x00010000,
)
# 0x340D4: Trace Control 1
npu_write32(
column=compute_tile2_col,
row=compute_tile2_row,
address=0x340D4,
value=0x00000000,
)
# 0x340E0: Trace Event Group 1 (Which events to trace)
# 0xAABBCCDD AA, BB, CC, DD <- four event slots
npu_write32(
column=compute_tile2_col,
row=compute_tile2_row,
address=0x340E0,
value=0x4B222125,
)
# 0x340E4: Trace Event Group 2 (Which events to trace)
# 0xAABBCCDD AA, BB, CC, DD <- four event slots
npu_write32(
column=compute_tile2_col,
row=compute_tile2_row,
address=0x340E4,
value=0x2D2C1A4F,
)

npu_write32(
column=compute_tile2_col,
row=compute_tile2_row,
address=0x3FF00,
value=0x00000121,
)

# Configure a buffer descriptor to write tracing information that has been routed into this shim tile
# out to host DDR memory
trace_bd_id = 13 # use BD 13 for writing trace output from compute tile to DDR host memory
output_size = bufOut
npu_writebd_shimtile(
bd_id=trace_bd_id,
buffer_length=trace_size,
buffer_offset=output_size,
enable_packet=0,
out_of_order_id=0,
packet_id=0,
packet_type=0,
column=0,
column_num=1,
d0_size=0,
d0_stride=0,
d1_size=0,
d1_stride=0,
d2_stride=0,
ddr_id=2,
iteration_current=0,
iteration_size=0,
iteration_stride=0,
lock_acq_enable=0,
lock_acq_id=0,
lock_acq_val=0,
lock_rel_id=0,
lock_rel_val=0,
next_bd=0,
use_next_bd=0,
valid_bd=1,
)
# Set start BD to our shim bd_Id (3)
npu_write32(column=0, row=0, address=0x1D20C, value=trace_bd_id)

NpuWriteRTPOp("rtp2", col=0, row=2, index=0, value=10)

npu_dma_memcpy_nd(
Expand Down
10 changes: 0 additions & 10 deletions programming_examples/ml/conv2d/run.lit

This file was deleted.

9 changes: 9 additions & 0 deletions programming_examples/ml/conv2d/run_makefile.lit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// (c) Copyright 2024 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// REQUIRES: ryzen_ai, chess, torch
//
// RUN: make -f %S/Makefile clean
// RUN: make -f %S/Makefile
// RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s
// CHECK: PASS!
10 changes: 7 additions & 3 deletions programming_examples/ml/conv2d/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,17 @@ def forward(self, x):

print("\nAvg NPU time: {}us.".format(int((npu_time_total / num_iter) / 1000)))

assert np.allclose(
if np.allclose(
ofm_mem_fmt_out.detach().numpy(),
golden_output.detach().numpy(),
rtol=0,
atol=2 * int8_scale,
)
print("\nPASS!\n")
):
print("\nPASS!\n")
exit(0)
else:
print("\nFailed.\n")
exit(-1)


if __name__ == "__main__":
Expand Down
14 changes: 8 additions & 6 deletions programming_examples/ml/conv2d_fused_relu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
#
# Copyright (C) 2024, Advanced Micro Devices, Inc.

include ../../makefile-common
srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

include ${srcdir}/../../makefile-common

mlirFileName = aieWithTrace_1core

VPATH := ../../../aie_kernels/aie2
VPATH := ${srcdir}/../../../aie_kernels/aie2

all: build/conv2dk1.o build/final.xclbin

build/${mlirFileName}.mlir: aie2.py
build/${mlirFileName}.mlir: ${srcdir}/aie2.py
mkdir -p ${@D}
python3 $< > $@

Expand All @@ -24,14 +26,14 @@ insts.txt: build/${mlirFileName}.mlir
build/conv2dk1.o: conv2dk1.cc
xchesscc -d ${CHESSCC2_FLAGS} -DINT8_ACT -c $< -o $@

build/final.xclbin: build/${mlirFileName}.mlir
build/final.xclbin: build/${mlirFileName}.mlir build/conv2dk1.o
cd build && aiecc.py -v --aie-generate-cdo --aie-generate-npu --no-compile-host \
--xclbin-name=${@F} --npu-insts-name=insts.txt $(<:%=../%)
--xclbin-name=${@F} --npu-insts-name=insts.txt ${<F}

clean:
rm -rf build *.elf* *.lst *.bif ${mlirFileName}.mlir.prj log* *.xclbin sim \
chess* *.o insts.txt \
*.log aie_partition.json *.bin BOOT.BIN _x test.exe

run_py:
${powershell} python3 test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE
${powershell} python3 ${srcdir}/test.py -x build/final.xclbin -i build/insts.txt -k MLIR_AIE
10 changes: 0 additions & 10 deletions programming_examples/ml/conv2d_fused_relu/run.lit

This file was deleted.

9 changes: 9 additions & 0 deletions programming_examples/ml/conv2d_fused_relu/run_makefile.lit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// (c) Copyright 2024 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// REQUIRES: ryzen_ai, chess, torch
//
// RUN: make -f %S/Makefile clean
// RUN: make -f %S/Makefile
// RUN: %run_on_npu make -f %S/Makefile run_py | FileCheck %s
// CHECK: PASS!
11 changes: 7 additions & 4 deletions programming_examples/ml/conv2d_fused_relu/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,17 @@ def forward(self, x):
# ------------------------------------------------------
print("\nAvg NPU time: {}us.".format(int((npu_time_total / num_iter) / 1000)))

assert np.allclose(
if np.allclose(
ofm_mem_fmt_out.detach().numpy(),
golden_output.detach().numpy(),
rtol=0,
atol=2 * relu_scale,
)

print("\nPASS!\n")
):
print("\nPASS!\n")
exit(0)
else:
print("\nFailed.\n")
exit(-1)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit a74a480

Please sign in to comment.