|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import executorch.backends.arm.tosa.dialect # noqa: F401 |
| 7 | +import pytest |
| 8 | +import sympy # type: ignore[import-untyped] |
| 9 | +import torch |
| 10 | +from executorch.backends.arm.tosa.dialect.lib import TosaValueError |
| 11 | +from executorch.backends.arm.tosa.specification import ( |
| 12 | + TosaLoweringContext, |
| 13 | + TosaSpecification, |
| 14 | +) |
| 15 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 16 | +from torch._subclasses.fake_tensor import FakeTensorMode |
| 17 | +from torch.fx.experimental.symbolic_shapes import ShapeEnv |
| 18 | + |
| 19 | + |
| 20 | +def _make_symint( |
| 21 | + shape_env: ShapeEnv, symbol: str, hint: int, min: int = 1, max: int = 64 |
| 22 | +) -> torch.SymInt: |
| 23 | + symint = shape_env.create_symintnode(sympy.Symbol(symbol), hint=hint) |
| 24 | + assert isinstance(symint, torch.SymInt) |
| 25 | + shape_env.constrain_symbol_range( |
| 26 | + symint.node.expr, compiler_min=min, compiler_max=max |
| 27 | + ) |
| 28 | + return symint |
| 29 | + |
| 30 | + |
| 31 | +def _expr(sym: torch.SymInt) -> sympy.Expr: |
| 32 | + return sympy.sympify(str(sym.node._expr)) |
| 33 | + |
| 34 | + |
| 35 | +def test_fft2d_tosa_fp_fft() -> None: |
| 36 | + input_real = torch.randn((2, 8, 16), dtype=torch.float32) |
| 37 | + input_imag = torch.randn((2, 8, 16), dtype=torch.float32) |
| 38 | + |
| 39 | + with TosaLoweringContext( |
| 40 | + TosaSpecification.create_from_string("TOSA-1.1+FP+fft") |
| 41 | + ), FakeTensorMode() as mode: |
| 42 | + output_real, output_imag = exir_ops.backend.tosa.FFT2D.default( |
| 43 | + mode.from_tensor(input_real), |
| 44 | + mode.from_tensor(input_imag), |
| 45 | + ) |
| 46 | + |
| 47 | + assert output_real.dtype == torch.float32 |
| 48 | + assert output_imag.dtype == torch.float32 |
| 49 | + assert tuple(output_real.shape) == (2, 8, 16) |
| 50 | + assert tuple(output_imag.shape) == (2, 8, 16) |
| 51 | + |
| 52 | + |
| 53 | +def test_fft2d_accepts_matching_symbolic_shape() -> None: |
| 54 | + shape_env = ShapeEnv() |
| 55 | + width = _make_symint(shape_env, "w", hint=16) |
| 56 | + |
| 57 | + with TosaLoweringContext( |
| 58 | + TosaSpecification.create_from_string("TOSA-1.1+FP+fft"), |
| 59 | + shape_env, |
| 60 | + ), FakeTensorMode(shape_env=shape_env) as mode: |
| 61 | + input_real = torch.empty((2, 8, width), dtype=torch.float32) |
| 62 | + input_imag = torch.empty((2, 8, width), dtype=torch.float32) |
| 63 | + output_real, output_imag = exir_ops.backend.tosa.FFT2D.default( |
| 64 | + mode.from_tensor(input_real), |
| 65 | + mode.from_tensor(input_imag), |
| 66 | + ) |
| 67 | + |
| 68 | + assert isinstance(output_real.shape[2], torch.SymInt) |
| 69 | + assert isinstance(output_imag.shape[2], torch.SymInt) |
| 70 | + assert sympy.simplify(_expr(output_real.shape[2]) - sympy.Symbol("w")) == 0 |
| 71 | + assert sympy.simplify(_expr(output_imag.shape[2]) - sympy.Symbol("w")) == 0 |
| 72 | + |
| 73 | + |
| 74 | +def test_rfft2d_tosa_fp_fft() -> None: |
| 75 | + input_real = torch.randn((2, 8, 16), dtype=torch.float32) |
| 76 | + |
| 77 | + with TosaLoweringContext( |
| 78 | + TosaSpecification.create_from_string("TOSA-1.1+FP+fft") |
| 79 | + ), FakeTensorMode() as mode: |
| 80 | + output_real, output_imag = exir_ops.backend.tosa.RFFT2D.default( |
| 81 | + mode.from_tensor(input_real), |
| 82 | + ) |
| 83 | + |
| 84 | + assert output_real.dtype == torch.float32 |
| 85 | + assert output_imag.dtype == torch.float32 |
| 86 | + assert tuple(output_real.shape) == (2, 8, 9) |
| 87 | + assert tuple(output_imag.shape) == (2, 8, 9) |
| 88 | + |
| 89 | + |
| 90 | +def test_fft_requires_extension() -> None: |
| 91 | + input_real = torch.randn((2, 8, 16), dtype=torch.float32) |
| 92 | + input_imag = torch.randn((2, 8, 16), dtype=torch.float32) |
| 93 | + |
| 94 | + with TosaLoweringContext( |
| 95 | + TosaSpecification.create_from_string("TOSA-1.1+FP") |
| 96 | + ), FakeTensorMode() as mode: |
| 97 | + with pytest.raises(TosaValueError, match="doesn't support FFT2D"): |
| 98 | + exir_ops.backend.tosa.FFT2D.default( |
| 99 | + mode.from_tensor(input_real), |
| 100 | + mode.from_tensor(input_imag), |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def test_rfft2d_preserves_symbolic_width() -> None: |
| 105 | + shape_env = ShapeEnv() |
| 106 | + width = _make_symint(shape_env, "w", hint=16) |
| 107 | + |
| 108 | + with TosaLoweringContext( |
| 109 | + TosaSpecification.create_from_string("TOSA-1.1+FP+fft"), |
| 110 | + shape_env, |
| 111 | + ), FakeTensorMode(shape_env=shape_env) as mode: |
| 112 | + input_real = torch.empty((2, 8, width), dtype=torch.float32) |
| 113 | + output_real, output_imag = exir_ops.backend.tosa.RFFT2D.default( |
| 114 | + mode.from_tensor(input_real) |
| 115 | + ) |
| 116 | + |
| 117 | + expected = sympy.floor(sympy.Symbol("w") / 2) + sympy.Integer(1) |
| 118 | + assert isinstance(output_real.shape[2], torch.SymInt) |
| 119 | + assert isinstance(output_imag.shape[2], torch.SymInt) |
| 120 | + assert sympy.simplify(_expr(output_real.shape[2]) - expected) == 0 |
| 121 | + assert sympy.simplify(_expr(output_imag.shape[2]) - expected) == 0 |
0 commit comments