forked from Xilinx/mlir-aie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_parallel_binary.py
More file actions
69 lines (58 loc) · 2.04 KB
/
Copy pathtransform_parallel_binary.py
File metadata and controls
69 lines (58 loc) · 2.04 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
# transform_parallel_binary.py -*- Python -*-
#
# 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 2026 Advanced Micro Devices, Inc.
import argparse
import sys
import numpy as np
import aie.iron as iron
from aie.iron.algorithms import transform_parallel_binary
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose output"
)
parser.add_argument(
"-n",
"--num-elements",
type=int,
default=1024,
help="Number of elements (default: 1024)",
)
args = parser.parse_args()
dtype = np.int32
# Construct two input random tensors and an output zeroed tensor
# The three tensor are in memory accessible to the NPU
input0 = iron.randint(0, 100, (args.num_elements,), dtype=dtype, device="npu")
input1 = iron.randint(0, 100, (args.num_elements,), dtype=dtype, device="npu")
output = iron.zeros_like(input0)
# JIT compile the algorithm
iron.jit(transform_parallel_binary)(
lambda a, b: a + b, input0, input1, output, tile_size=16
)
# Check the correctness of the result
e = np.equal(input0.numpy() + input1.numpy(), output.numpy())
errors = np.size(e) - np.count_nonzero(e)
# Optionally, print the results
if args.verbose:
print(f"{'input0':>4} + {'input1':>4} = {'output':>4}")
print("-" * 34)
count = input0.numel()
for idx, (a, b, c) in enumerate(
zip(input0[:count], input1[:count], output[:count])
):
print(f"{idx:2}: {a:4} + {b:4} = {c:4}")
# If the result is correct, exit with a success code.
# Otherwise, exit with a failure code
if not errors:
print("\nPASS!\n")
sys.exit(0)
else:
print("\nError count: ", errors)
print("\nFailed.\n")
sys.exit(-1)
if __name__ == "__main__":
main()