|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from iron.operators.repeat.op import Repeat |
| 8 | +from iron.operators.repeat.reference import generate_golden_reference |
| 9 | +from iron.common.test_utils import run_test |
| 10 | + |
| 11 | + |
| 12 | +def get_params(): |
| 13 | + # rows, cols, repeat, transfer_size. |
| 14 | + # |
| 15 | + # design.py splits cols into chunks <= 1023 by picking the smallest divisor that |
| 16 | + # gets under the hardware limit, so cols on either side of 1023 take different |
| 17 | + # paths and both need covering. The llama arm is the shape the only caller in the |
| 18 | + # tree actually dispatches: n_kv_groups=8 groups expanded to n_heads=32 over a |
| 19 | + # max_seq_len=2048 context of head_dim=64, i.e. repeat=4 with cols=2048*64. |
| 20 | + return [ |
| 21 | + pytest.param(8, 64, 4, None), |
| 22 | + pytest.param(8, 512, 4, 64), |
| 23 | + pytest.param(4, 1024, 2, None), |
| 24 | + pytest.param(4, 2048, 2, None, marks=[pytest.mark.extensive]), |
| 25 | + pytest.param(8, 2048 * 64, 4, 64, marks=[pytest.mark.extensive]), |
| 26 | + ] |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.metrics( |
| 30 | + Latency=r"Latency \(us\): (?P<value>[\d\.]+)", |
| 31 | + Bandwidth=r"Effective Bandwidth: (?P<value>[\d\.e\+-]+) GB/s", |
| 32 | +) |
| 33 | +@pytest.mark.parametrize("rows,cols,repeat,transfer_size", get_params()) |
| 34 | +def test_repeat(rows, cols, repeat, transfer_size, aie_context): |
| 35 | + """Repeat moves data and computes nothing, so the gate is exact equality. |
| 36 | +
|
| 37 | + A tolerance gate would accept a permutation that reads the wrong group -- which |
| 38 | + is the whole failure mode here, since the only caller uses this to expand KV |
| 39 | + groups to attention heads and a misrouted group is numerically plausible. |
| 40 | + """ |
| 41 | + golden_ref = generate_golden_reference(rows=rows, cols=cols, repeat=repeat) |
| 42 | + |
| 43 | + operator = Repeat( |
| 44 | + rows=rows, |
| 45 | + cols=cols, |
| 46 | + repeat=repeat, |
| 47 | + transfer_size=transfer_size, |
| 48 | + context=aie_context, |
| 49 | + ) |
| 50 | + |
| 51 | + errors, latency_us, bandwidth_gbps = run_test( |
| 52 | + operator, |
| 53 | + {"input": golden_ref["input"]}, |
| 54 | + {"output": golden_ref["output"]}, |
| 55 | + rel_tol=0.0, |
| 56 | + abs_tol=0.0, |
| 57 | + ) |
| 58 | + |
| 59 | + print(f"\nLatency (us): {latency_us:.1f}") |
| 60 | + print(f"Effective Bandwidth: {bandwidth_gbps:.6e} GB/s\n") |
| 61 | + |
| 62 | + assert not errors, f"Test failed with errors: {errors}" |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "cols,why", |
| 67 | + [ |
| 68 | + (513, "odd: every divisor is odd, so no chunk is a whole 32-bit word"), |
| 69 | + (1031, "prime > 1023: the only divisors are 1 and cols, neither legal"), |
| 70 | + (2062, "2 x 1031: the only word-aligned chunk leaves a 1031-wide chunk count"), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_cols_without_a_legal_split_is_rejected(cols, why, aie_context): |
| 74 | + """A split has to satisfy the innermost dim AND the dim holding the chunk count. |
| 75 | +
|
| 76 | + Both land on a 10-bit wrap field, and the innermost is denominated in 32-bit words, |
| 77 | + so bounding the chunk length alone lets through taps the BD verifier then rejects |
| 78 | + with a much less legible error. |
| 79 | + """ |
| 80 | + operator = Repeat(rows=8, cols=cols, repeat=4, context=aie_context) |
| 81 | + with pytest.raises(ValueError, match="Cannot split cols"): |
| 82 | + operator.compile() |
0 commit comments