-
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.
* Add channel size example * Update documentation * modify expected output for verification * Fix a few small issues * Make channel size standalone * matrix scalar add example is working * channel size example is working * Realized that there is not really common code in the channel examples so made herd_to_herd standalone too. Also updated docs * Clarify example documentation
- Loading branch information
Showing
7 changed files
with
250 additions
and
40 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
12 changes: 12 additions & 0 deletions
12
programming_examples/channel_examples/channel_size/Makefile
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 @@ | ||
# Copyright (C) 2022, 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__ |
124 changes: 124 additions & 0 deletions
124
programming_examples/channel_examples/channel_size/channel_size.py
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,124 @@ | ||
# 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] | ||
|
||
assert IMAGE_WIDTH % TILE_WIDTH == 0 | ||
assert IMAGE_HEIGHT % TILE_HEIGHT == 0 | ||
|
||
|
||
def format_name(prefix, index_0, index_1): | ||
return f"{prefix}{index_0:02}{index_1:02}" | ||
|
||
|
||
@module_builder | ||
def build_module(): | ||
memrefTyInOut = MemRefType.get(IMAGE_SIZE, T.i32()) | ||
|
||
# Create an input/output channel pair per worker | ||
ChannelOp("ChanIn", size=[IMAGE_WIDTH // TILE_WIDTH, IMAGE_HEIGHT // TILE_HEIGHT]) | ||
ChannelOp("ChanOut", size=[IMAGE_WIDTH // TILE_WIDTH, IMAGE_HEIGHT // TILE_HEIGHT]) | ||
|
||
# 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): | ||
|
||
# Transfer one tile of data per worker | ||
for h in range(IMAGE_HEIGHT // TILE_HEIGHT): | ||
for w in range(IMAGE_WIDTH // TILE_WIDTH): | ||
offset0 = IMAGE_HEIGHT * h | ||
offset1 = IMAGE_HEIGHT * w | ||
|
||
# Put data into the channel tile by tile | ||
ChannelPut( | ||
"ChanIn", | ||
a, | ||
indices=[w, h], | ||
offsets=[offset0, offset1], | ||
sizes=[TILE_HEIGHT, TILE_WIDTH], | ||
strides=[IMAGE_WIDTH, 1], | ||
) | ||
|
||
# Transfer one tile of data per worker | ||
for h in range(IMAGE_HEIGHT // TILE_HEIGHT): | ||
for w in range(IMAGE_WIDTH // TILE_WIDTH): | ||
offset0 = IMAGE_HEIGHT * h | ||
offset1 = IMAGE_HEIGHT * w | ||
|
||
# Write data back out to the channel tile by tile | ||
ChannelGet( | ||
"ChanOut", | ||
b, | ||
indices=[w, h], | ||
offsets=[offset0, offset1], | ||
sizes=[TILE_HEIGHT, TILE_WIDTH], | ||
strides=[IMAGE_WIDTH, 1], | ||
) | ||
|
||
# The arguments are still the input and the output | ||
@segment(name="seg") | ||
def segment_body(): | ||
|
||
@herd( | ||
name="xaddherd", | ||
sizes=[IMAGE_WIDTH // TILE_WIDTH, IMAGE_HEIGHT // TILE_HEIGHT], | ||
) | ||
def herd_body(th, tw, _sx, _sy): | ||
|
||
# We want to store our data in L1 memory | ||
mem_space = IntegerAttr.get(T.i32(), MemorySpace.L1) | ||
|
||
# This is the type definition of the tile | ||
tile_type = MemRefType.get( | ||
shape=TILE_SIZE, | ||
element_type=T.i32(), | ||
memory_space=mem_space, | ||
) | ||
|
||
# We must allocate a buffer of tile size for the input/output | ||
tile_in = AllocOp(tile_type, [], []) | ||
tile_out = AllocOp(tile_type, [], []) | ||
|
||
# Copy a tile from the input image (a) into the L1 memory region (tile_in) | ||
ChannelGet("ChanIn", tile_in, indices=[tw, th]) | ||
|
||
# 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, [i, j]) | ||
|
||
# Store the output value in tile_out | ||
store(val, tile_out, [i, j]) | ||
yield_([]) | ||
yield_([]) | ||
|
||
# Copy the output tile into the output | ||
ChannelPut("ChanOut", tile_out, indices=[tw, th]) | ||
|
||
# Deallocate our L1 buffers | ||
DeallocOp(tile_in) | ||
DeallocOp(tile_out) | ||
|
||
|
||
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
8 changes: 8 additions & 0 deletions
8
programming_examples/channel_examples/channel_size/run_makefile.lit
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
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