forked from Xilinx/mlir-aie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaie2.py
178 lines (148 loc) · 6.05 KB
/
aie2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# (c) Copyright 2023 AMD Inc.
import sys
from aie.dialects.aie import *
from aie.dialects.aiex import *
from aie.dialects.scf import *
from aie.extras.context import mlir_mod_ctx
import aie.utils.trace as trace_utils
def my_eltwise_mul(trace_size):
word_size_in = 2
N = 65536
N_in_bytes = N * word_size_in
# Tile sizes
n = 1024
N_div_n = N // n
n_cores = 2
tiles = N_div_n // n_cores
buffer_depth = 2
@device(AIEDevice.npu1_1col)
def device_body():
memRef_ty = T.memref(n, T.bf16())
# Type used in the tile memory
memRef_A_ty = T.memref(n, T.bf16())
memRef_B_ty = T.memref(n, T.bf16())
memRef_C_ty = T.memref(n, T.bf16())
# Type used in the memory tile which aggregates across the 4 cores
memRef_A_MT_ty = T.memref(n * n_cores, T.bf16())
memRef_B_MT_ty = T.memref(n * n_cores, T.bf16())
memRef_C_MT_ty = T.memref(n * n_cores, T.bf16())
# AIE Core Function declarations
eltwise_mul_bf16_scalar = external_func(
"eltwise_mul_bf16_scalar", inputs=[memRef_ty, memRef_ty, memRef_ty]
)
eltwise_mul_bf16_vector = external_func(
"eltwise_mul_bf16_vector", inputs=[memRef_ty, memRef_ty, memRef_ty]
)
# elwise_int32 = external_func("scale_int32", inputs=[memRef_ty, memRef_ty])
# Tile declarations
ShimTile = tile(0, 0)
MemTile = tile(0, 1)
cores = [tile(0, 2 + i) for i in range(n_cores)]
# Set up a circuit-switched flow from core to shim for tracing information
if trace_size > 0:
flow(cores[0], WireBundle.Trace, 0, ShimTile, WireBundle.DMA, 1)
inA_fifo_names = [f"memA{i}" for i in range(n_cores)]
inB_fifo_names = [f"memB{i}" for i in range(n_cores)]
outC_fifo_names = [f"memC{i}" for i in range(n_cores)]
inA_fifos = {}
inB_fifos = {}
outC_fifos = {}
# AIE-array data movement with object fifos
# Input A
inA = object_fifo("inA", ShimTile, MemTile, buffer_depth, memRef_A_MT_ty)
for i in range(n_cores):
inA_fifos[inA_fifo_names[i]] = object_fifo(
inA_fifo_names[i], MemTile, cores[i], buffer_depth, memRef_A_ty
)
if n_cores > 1:
of_offsets = [
(np.prod(memRef_A_MT_ty.shape) // n_cores) * i for i in range(n_cores)
]
else:
of_offsets = []
object_fifo_link(inA, inA_fifo_names, [], of_offsets)
# Input B
inB = object_fifo("inB", ShimTile, MemTile, buffer_depth, memRef_B_MT_ty)
for i in range(n_cores):
inB_fifos[inB_fifo_names[i]] = object_fifo(
inB_fifo_names[i], MemTile, cores[i], buffer_depth, memRef_B_ty
)
if n_cores > 1:
of_offsets = [
(np.prod(memRef_B_MT_ty.shape) // n_cores) * i for i in range(n_cores)
]
else:
of_offsets = []
object_fifo_link(inB, inB_fifo_names[0:n_cores], [], of_offsets)
# Output C
for i in range(n_cores):
outC_fifos[outC_fifo_names[i]] = object_fifo(
outC_fifo_names[i], cores[i], MemTile, buffer_depth, memRef_C_ty
)
outC = object_fifo("outC", MemTile, ShimTile, buffer_depth, memRef_C_MT_ty)
if n_cores > 1:
of_offsets = [
(np.prod(memRef_C_MT_ty.shape) // n_cores) * i for i in range(n_cores)
]
else:
of_offsets = []
object_fifo_link(outC_fifo_names[0:n_cores], outC, of_offsets, [])
# Set up compute tiles
for i in range(n_cores):
# Compute tile i
@core(cores[i], "mul.o")
def core_body():
for _ in for_(0xFFFFFFFF):
for _ in for_(tiles):
elem_out = outC_fifos[outC_fifo_names[i]].acquire(
ObjectFifoPort.Produce, 1
)
elem_in_a = inA_fifos[inA_fifo_names[i]].acquire(
ObjectFifoPort.Consume, 1
)
elem_in_b = inB_fifos[inB_fifo_names[i]].acquire(
ObjectFifoPort.Consume, 1
)
call(
eltwise_mul_bf16_vector,
[elem_in_a, elem_in_b, elem_out],
)
inA_fifos[inA_fifo_names[i]].release(ObjectFifoPort.Consume, 1)
inB_fifos[inB_fifo_names[i]].release(ObjectFifoPort.Consume, 1)
outC_fifos[outC_fifo_names[i]].release(
ObjectFifoPort.Produce, 1
)
yield_([])
yield_([])
# To/from AIE-array data movement
tensor_ty = T.memref(N, T.bf16())
@runtime_sequence(tensor_ty, tensor_ty, tensor_ty)
def sequence(A, B, C):
if trace_size > 0:
trace_utils.configure_simple_tracing_aie2(
cores[0],
ShimTile,
ddr_id=2,
size=trace_size,
offset=N_in_bytes,
)
npu_dma_memcpy_nd(metadata="outC", bd_id=0, mem=C, sizes=[1, 1, 1, N])
npu_dma_memcpy_nd(metadata="inA", bd_id=1, mem=A, sizes=[1, 1, 1, N])
npu_dma_memcpy_nd(metadata="inB", bd_id=2, mem=B, sizes=[1, 1, 1, N])
npu_sync(column=0, row=0, direction=0, channel=0)
try:
trace_size = 0 if (len(sys.argv) < 2) else int(sys.argv[1])
except ValueError:
print("Argument is not an integer")
with mlir_mod_ctx() as ctx:
my_eltwise_mul(trace_size)
res = ctx.module.operation.verify()
if res == True:
print(ctx.module)
else:
print(res)