-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Access L2 Segment Allocation in Herd with Python Example (#668)
- Loading branch information
Showing
6 changed files
with
272 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# (c) Copyright 2024 Advanced Micro Devices, Inc. | ||
# SPDX-License-Identifier: MIT | ||
srcdir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) | ||
|
||
targetname := $(shell basename ${srcdir}) | ||
|
||
run: | ||
mkdir -p build | ||
cd build && ${powershell} python3 ${srcdir}/run.py | ||
|
||
clean: | ||
rm -rf build __pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# run.py -*- Python -*- | ||
# | ||
# Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved. | ||
# SPDX-License-Identifier: MIT | ||
# Copyright (C) 2024, Advanced Micro Devices, Inc. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import argparse | ||
import numpy as np | ||
import air.backend.xrt as xrt_backend | ||
import filelock | ||
|
||
from segment_alloc import * | ||
|
||
INOUT_DATATYPE = np.uint32 | ||
INOUT_ELEM_SIZE = np.dtype(INOUT_DATATYPE).itemsize | ||
INOUT_SIZE = IMAGE_SIZE[0] * IMAGE_SIZE[1] | ||
INOUT_SIZE_BYTES = INOUT_SIZE * INOUT_ELEM_SIZE | ||
|
||
|
||
def main(verbose=False, experimental_passes=False): | ||
mlir_module = build_module() | ||
|
||
input_a = np.arange(1, INOUT_SIZE + 1, dtype=INOUT_DATATYPE) | ||
output_b = np.arange(1, INOUT_SIZE + 1, dtype=INOUT_DATATYPE) | ||
for i in range(INOUT_SIZE): | ||
input_a[i] = i + 0x1000 | ||
output_b[i] = 0x00DEFACED | ||
|
||
backend = xrt_backend.XRTBackend( | ||
verbose=verbose, | ||
experimental_passes=experimental_passes, | ||
omit_while_true_loop=True, | ||
) | ||
|
||
# run the module | ||
with filelock.FileLock("/tmp/npu.lock"): | ||
mul = backend.compile_and_load(mlir_module) | ||
(_, output_b) = mul(input_a, output_b) | ||
|
||
backend.unload() | ||
|
||
# check output, should have the top left filled in | ||
errors = 0 | ||
for i in range(INOUT_SIZE): | ||
rb = output_b[i] | ||
|
||
row = i / IMAGE_WIDTH | ||
col = i % IMAGE_WIDTH | ||
|
||
if row < TILE_HEIGHT and col < TILE_WIDTH: | ||
# value should have been updated | ||
if not (rb == 0x1000 + i): | ||
print(f"IM {i} [{col}, {row}] should be 0x{i:x}, is 0x{rb:x}\n") | ||
errors += 1 | ||
else: | ||
# value should stay unchanged | ||
if rb != 0x00DEFACED: | ||
print( | ||
f"IM {i} [{col}, {row}] should be 0xdefaced, is 0x{rb:x}\n", | ||
i, | ||
col, | ||
row, | ||
rb, | ||
) | ||
errors += 1 | ||
|
||
if errors == 0: | ||
print("PASS!") | ||
exit(0) | ||
else: | ||
print("failed. errors=", errors) | ||
exit(-1) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
prog="run.py", | ||
description="Builds, runs, and tests the segment_alloc example", | ||
) | ||
|
||
parser.add_argument( | ||
"-v", | ||
"--verbose", | ||
action="store_true", | ||
) | ||
args = parser.parse_args() | ||
main(experimental_passes=True, verbose=args.verbose) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// (c) Copyright 2024 Advanced Micro Devices, Inc. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// REQUIRES: ryzen_ai | ||
// | ||
// RUN: make -f %S/Makefile clean | ||
// RUN: make -f %S/Makefile run | FileCheck %s | ||
// CHECK: PASS! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Copyright (C) 2024, Advanced Micro Devices, Inc. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from air.ir import * | ||
from air.dialects.air import * | ||
from air.dialects.memref import AllocOp, DeallocOp, load, store | ||
from air.dialects.func import FuncOp | ||
from air.dialects.scf import for_, yield_ | ||
|
||
range_ = for_ | ||
|
||
IMAGE_WIDTH = 32 | ||
IMAGE_HEIGHT = 16 | ||
IMAGE_SIZE = [IMAGE_WIDTH, IMAGE_HEIGHT] | ||
|
||
TILE_WIDTH = 16 | ||
TILE_HEIGHT = 8 | ||
TILE_SIZE = [TILE_WIDTH, TILE_HEIGHT] | ||
|
||
|
||
@module_builder | ||
def build_module(): | ||
memrefTyInOut = MemRefType.get(IMAGE_SIZE, T.i32()) | ||
|
||
# We will send an image worth of data in and out | ||
@FuncOp.from_py_func(memrefTyInOut, memrefTyInOut) | ||
def copy(arg0, arg1): | ||
|
||
# The arguments are the input and output | ||
@launch(operands=[arg0, arg1]) | ||
def launch_body(a, b): | ||
|
||
# The arguments are still the input and the output | ||
@segment(name="seg", operands=[a, b]) | ||
def segment_body(arg2, arg3): | ||
# We want to store our data in L1 memory | ||
mem_space_l2 = IntegerAttr.get(T.i32(), MemorySpace.L2) | ||
|
||
# This is the type definition of the tile | ||
tile_type_l2 = MemRefType.get( | ||
shape=TILE_SIZE, | ||
element_type=T.i32(), | ||
memory_space=mem_space_l2, | ||
) | ||
|
||
# We must allocate a buffer of tile size for the input/output | ||
tile_in_l2 = AllocOp(tile_type_l2, [], []) | ||
|
||
# The herd sizes correspond to the dimensions of the contiguous block of cores we are hoping to get. | ||
# We just need one compute core, so we ask for a 1x1 herd | ||
@herd(name="copyherd", sizes=[1, 1], operands=[arg2, arg3, tile_in_l2]) | ||
def herd_body(tx, ty, sx, sy, a, b, my_l2_tile): | ||
|
||
# We want to store our data in L1 memory | ||
mem_space_l1 = IntegerAttr.get(T.i32(), MemorySpace.L1) | ||
|
||
# This is the type definition of the tile | ||
tile_type_l1 = MemRefType.get( | ||
shape=TILE_SIZE, | ||
element_type=T.i32(), | ||
memory_space=mem_space_l1, | ||
) | ||
|
||
# We must allocate a buffer of tile size for the input/output | ||
tile_in_l1 = AllocOp(tile_type_l1, [], []) | ||
tile_out_l1 = AllocOp(tile_type_l1, [], []) | ||
|
||
dma_memcpy_nd( | ||
my_l2_tile, | ||
a, | ||
src_offsets=[0, 0], | ||
src_sizes=[TILE_HEIGHT, TILE_WIDTH], | ||
src_strides=[IMAGE_WIDTH, 1], | ||
) | ||
|
||
# Copy a tile from the input image (a) into the L1 memory region (tile_in) | ||
dma_memcpy_nd( | ||
tile_in_l1, | ||
my_l2_tile, | ||
) | ||
|
||
# Access every value in the tile | ||
for j in range_(TILE_HEIGHT): | ||
for i in range_(TILE_WIDTH): | ||
# Load the input value from tile_in | ||
val = load(tile_in_l1, [i, j]) | ||
|
||
# Store the output value in tile_out | ||
store(val, tile_out_l1, [i, j]) | ||
yield_([]) | ||
yield_([]) | ||
|
||
# Copy the output tile into the output | ||
dma_memcpy_nd( | ||
b, | ||
tile_out_l1, | ||
dst_offsets=[0, 0], | ||
dst_sizes=[TILE_HEIGHT, TILE_WIDTH], | ||
dst_strides=[IMAGE_WIDTH, 1], | ||
) | ||
|
||
# Deallocate our L1 buffers | ||
DeallocOp(tile_in_l1) | ||
DeallocOp(tile_out_l1) | ||
|
||
|
||
if __name__ == "__main__": | ||
module = build_module() | ||
print(module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters