-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathtransform_parallel.py
More file actions
72 lines (58 loc) · 1.87 KB
/
Copy pathtransform_parallel.py
File metadata and controls
72 lines (58 loc) · 1.87 KB
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
# transform_parallel.py -*- Python -*-
#
# Copyright (C) 2026 Advanced Micro Devices, Inc.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
"""Tutorial: parallel tile-by-tile elementwise transform on the NPU.
Same shape as ``transform.py`` but distributes the work across all
available NPU columns. The design body delegates to
:func:`aie.iron.algorithms.transform_parallel`.
"""
import argparse
import numpy as np
import aie.iron as iron
from aie.iron import CompileTime, In, Out
from aie.utils.verify import assert_pass
@iron.jit
def transform_parallel(
input: In,
output: Out,
*,
num_elements: CompileTime[int],
dtype: CompileTime[type],
tile_size: CompileTime[int] = 16,
):
tensor_ty = np.ndarray[(num_elements,), np.dtype[dtype]]
return iron.algorithms.transform_parallel(
lambda a: a + 1, tensor_ty, tile_size=tile_size
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose", action="store_true", help="print every output element"
)
parser.add_argument(
"-n",
"--num-elements",
type=int,
default=1024,
help="number of int32 elements per input tensor (default: %(default)s)",
)
args = parser.parse_args()
dtype = np.int32
input = iron.randint(0, 100, (args.num_elements,), dtype=dtype, device="npu")
output = iron.zeros_like(input)
transform_parallel(input, output, num_elements=int(input.shape[0]), dtype=dtype)
if args.verbose:
print(f"{'input':>6} + 1 = {'output':>6}")
print("-" * 24)
n = args.num_elements
for idx, (a, b) in enumerate(zip(input[:n], output[:n])):
print(f"{idx:2}: {a:6} + 1 = {b:6}")
assert_pass(
input.numpy() + 1,
output.numpy(),
fail_msg="transform_parallel output mismatch",
)
if __name__ == "__main__":
main()