|
| 1 | +# Copyright (C) 2025, Advanced Micro Devices, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +# RUN: %python %s | FileCheck %s |
| 5 | + |
| 6 | +"""Regression tests for Buffer placement and resolution behaviour. |
| 7 | +
|
| 8 | +Covers the bug reported in https://github.com/Xilinx/mlir-aie/issues/3011: |
| 9 | + - A Buffer passed to a Worker is placed automatically by the placer and |
| 10 | + resolved before inline_ops callbacks fire, so indexing inside the callback |
| 11 | + works correctly. |
| 12 | + - A Buffer that is created but never given to any Worker has no tile and |
| 13 | + therefore cannot be resolved; InlineOpRuntimeTask must raise a clear |
| 14 | + ValueError rather than a confusing AttributeError from __setitem__. |
| 15 | + - Multiple RTP buffers (one per worker) in a list can all be written inside a |
| 16 | + single inline_ops callback, reflecting the common RTP-initialisation pattern |
| 17 | + seen in ML examples such as resnet layers_conv2_x. |
| 18 | +""" |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +from aie.iron import Buffer, ObjectFifo, Program, Runtime, Worker |
| 23 | +from aie.iron.placers import SequentialPlacer |
| 24 | +from aie.iron.device import NPU1Col1, NPU2 |
| 25 | + |
| 26 | +rtp_ty = np.ndarray[(16,), np.dtype[np.int32]] |
| 27 | +data_ty = np.ndarray[(64,), np.dtype[np.int32]] |
| 28 | + |
| 29 | + |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +# Test 1: Buffer given to a Worker is resolved before inline_ops fires, |
| 32 | +# so element writes inside the callback produce correct rtp_write ops. |
| 33 | +# CHECK-LABEL: TEST: rtp_buffer_written_in_inline_ops |
| 34 | +# CHECK: aiex.npu.rtp_write(@my_rtp, 0, 7) |
| 35 | +# CHECK: aiex.npu.rtp_write(@my_rtp, 1, 3) |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | +print("\nTEST: rtp_buffer_written_in_inline_ops") |
| 38 | + |
| 39 | +of_in = ObjectFifo(data_ty, name="in") |
| 40 | +of_out = ObjectFifo(data_ty, name="out") |
| 41 | +rtp_buf = Buffer(rtp_ty, name="my_rtp", use_write_rtp=True) |
| 42 | + |
| 43 | + |
| 44 | +def core_fn(of_in, of_out, rtp): |
| 45 | + scale = rtp[0] |
| 46 | + elem_in = of_in.acquire(1) |
| 47 | + elem_out = of_out.acquire(1) |
| 48 | + of_in.release(1) |
| 49 | + of_out.release(1) |
| 50 | + |
| 51 | + |
| 52 | +worker = Worker(core_fn, [of_in.cons(), of_out.prod(), rtp_buf]) |
| 53 | + |
| 54 | +rt = Runtime() |
| 55 | +with rt.sequence(data_ty, data_ty) as (inp, out): |
| 56 | + |
| 57 | + def set_rtp(buf): |
| 58 | + buf[0] = 7 |
| 59 | + buf[1] = 3 |
| 60 | + |
| 61 | + rt.inline_ops(set_rtp, [rtp_buf]) |
| 62 | + rt.start(worker) |
| 63 | + rt.fill(of_in.prod(), inp) |
| 64 | + rt.drain(of_out.cons(), out, wait=True) |
| 65 | + |
| 66 | +module = Program(NPU1Col1(), rt).resolve_program(SequentialPlacer()) |
| 67 | +print(module) |
| 68 | + |
| 69 | + |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | +# Test 2: Multiple RTP buffers (one per worker) in a list, all written in one |
| 72 | +# inline_ops callback — mirrors the resnet layers_conv2_x pattern. |
| 73 | +# CHECK-LABEL: TEST: multiple_rtp_buffers_in_inline_ops |
| 74 | +# CHECK: aiex.npu.rtp_write(@rtp_w0, 0, 1) |
| 75 | +# CHECK: aiex.npu.rtp_write(@rtp_w1, 0, 2) |
| 76 | +# CHECK: aiex.npu.rtp_write(@rtp_w2, 0, 3) |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | +print("\nTEST: multiple_rtp_buffers_in_inline_ops") |
| 79 | + |
| 80 | +n_workers = 3 |
| 81 | +of_ins = [ObjectFifo(data_ty, name=f"in{i}") for i in range(n_workers)] |
| 82 | +of_outs = [ObjectFifo(data_ty, name=f"out{i}") for i in range(n_workers)] |
| 83 | +rtps = [Buffer(rtp_ty, name=f"rtp_w{i}", use_write_rtp=True) for i in range(n_workers)] |
| 84 | + |
| 85 | + |
| 86 | +def core_fn_rtp(of_in, of_out, rtp): |
| 87 | + scale = rtp[0] |
| 88 | + elem_in = of_in.acquire(1) |
| 89 | + elem_out = of_out.acquire(1) |
| 90 | + of_in.release(1) |
| 91 | + of_out.release(1) |
| 92 | + |
| 93 | + |
| 94 | +workers = [ |
| 95 | + Worker(core_fn_rtp, [of_ins[i].cons(), of_outs[i].prod(), rtps[i]]) |
| 96 | + for i in range(n_workers) |
| 97 | +] |
| 98 | + |
| 99 | +rt2 = Runtime() |
| 100 | +with rt2.sequence(data_ty, data_ty, data_ty, data_ty, data_ty, data_ty) as ( |
| 101 | + i0, |
| 102 | + i1, |
| 103 | + i2, |
| 104 | + o0, |
| 105 | + o1, |
| 106 | + o2, |
| 107 | +): |
| 108 | + |
| 109 | + def set_rtps(rtps): |
| 110 | + rtps[0][0] = 1 |
| 111 | + rtps[1][0] = 2 |
| 112 | + rtps[2][0] = 3 |
| 113 | + |
| 114 | + rt2.inline_ops(set_rtps, [rtps]) |
| 115 | + rt2.start(*workers) |
| 116 | + rt2.fill(of_ins[0].prod(), i0) |
| 117 | + rt2.fill(of_ins[1].prod(), i1) |
| 118 | + rt2.fill(of_ins[2].prod(), i2) |
| 119 | + rt2.drain(of_outs[0].cons(), o0, wait=True) |
| 120 | + rt2.drain(of_outs[1].cons(), o1, wait=True) |
| 121 | + rt2.drain(of_outs[2].cons(), o2, wait=True) |
| 122 | + |
| 123 | +module2 = Program(NPU2(), rt2).resolve_program(SequentialPlacer()) |
| 124 | +print(module2) |
| 125 | + |
| 126 | + |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | +# Test 3: A Buffer never given to any Worker raises ValueError (not the |
| 129 | +# confusing AttributeError from __setitem__) when inline_ops fires. |
| 130 | +# This is the exact failure mode of GitHub issue #3011. |
| 131 | +# CHECK-LABEL: TEST: unplaced_buffer_in_inline_ops_raises |
| 132 | +# CHECK: PASSED |
| 133 | +# --------------------------------------------------------------------------- |
| 134 | +print("\nTEST: unplaced_buffer_in_inline_ops_raises") |
| 135 | + |
| 136 | +of_in3 = ObjectFifo(data_ty, name="in3") |
| 137 | +of_out3 = ObjectFifo(data_ty, name="out3") |
| 138 | +placed_rtp = Buffer(rtp_ty, name="placed_rtp", use_write_rtp=True) |
| 139 | +orphan_rtp = Buffer( |
| 140 | + rtp_ty, name="orphan_rtp", use_write_rtp=True |
| 141 | +) # never given to a Worker |
| 142 | + |
| 143 | + |
| 144 | +def core_fn3(of_in, of_out, rtp): |
| 145 | + scale = rtp[0] |
| 146 | + elem_in = of_in.acquire(1) |
| 147 | + elem_out = of_out.acquire(1) |
| 148 | + of_in.release(1) |
| 149 | + of_out.release(1) |
| 150 | + |
| 151 | + |
| 152 | +worker3 = Worker(core_fn3, [of_in3.cons(), of_out3.prod(), placed_rtp]) |
| 153 | + |
| 154 | +rt3 = Runtime() |
| 155 | +with rt3.sequence(data_ty, data_ty) as (inp3, out3): |
| 156 | + |
| 157 | + def write_both(placed, orphan): |
| 158 | + placed[0] = 1 |
| 159 | + orphan[0] = 1 # orphan has no tile → should raise ValueError |
| 160 | + |
| 161 | + rt3.inline_ops(write_both, [placed_rtp, orphan_rtp]) |
| 162 | + rt3.start(worker3) |
| 163 | + rt3.fill(of_in3.prod(), inp3) |
| 164 | + rt3.drain(of_out3.cons(), out3, wait=True) |
| 165 | + |
| 166 | +try: |
| 167 | + Program(NPU1Col1(), rt3).resolve_program(SequentialPlacer()) |
| 168 | + print("FAILED: expected ValueError but no exception was raised") |
| 169 | +except ValueError as e: |
| 170 | + assert "placed" in str(e).lower(), f"unexpected message: {e}" |
| 171 | + print("PASSED") |
| 172 | +except Exception as e: |
| 173 | + print(f"FAILED: expected ValueError, got {type(e).__name__}: {e}") |
0 commit comments