|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | +# pyre-strict |
| 7 | + |
| 8 | +import unittest |
| 9 | +from typing import cast |
| 10 | + |
| 11 | +import executorch.backends.cadence.aot.ops_registrations # noqa |
| 12 | +import torch |
| 13 | +from executorch.backends.cadence.aot.graph_builder import single_op_builder |
| 14 | +from executorch.backends.cadence.aot.pass_utils import count_node |
| 15 | +from executorch.backends.cadence.aot.type_dispatch import CompileTimeTypeDispatchPass |
| 16 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 17 | +from torch.fx.passes.infra.pass_base import PassResult |
| 18 | + |
| 19 | + |
| 20 | +class TestTypeDispatchPasses(unittest.TestCase): |
| 21 | + def test_int8_dispatch(self) -> None: |
| 22 | + """Test int8 x int8 inputs should dispatch to asym8sxasym8s_asym8s variant""" |
| 23 | + x = torch.randint(-128, 127, (2, 3), dtype=torch.int8) |
| 24 | + w = torch.randint(-128, 127, (4, 3), dtype=torch.int8) |
| 25 | + b = torch.randint(-2147483648, 2147483647, (4,), dtype=torch.int32) |
| 26 | + gm = single_op_builder( |
| 27 | + placeholders=(x, w, b), |
| 28 | + op=exir_ops.edge.cadence.quantized_fully_connected.per_tensor, |
| 29 | + args=(x, w, b, 0, 0, 1, 0, 0, None), |
| 30 | + ) |
| 31 | + p = CompileTimeTypeDispatchPass() |
| 32 | + gm = cast(PassResult, p(gm)).graph_module |
| 33 | + # Original op should be replaced |
| 34 | + self.assertEqual( |
| 35 | + count_node(gm, exir_ops.edge.cadence.quantized_fully_connected.per_tensor), |
| 36 | + 0, |
| 37 | + ) |
| 38 | + # Should be replaced with int8 specific variant |
| 39 | + self.assertEqual( |
| 40 | + count_node( |
| 41 | + gm, |
| 42 | + exir_ops.edge.cadence.quantized_fully_connected_asym8sxasym8s_asym8s.per_tensor, |
| 43 | + ), |
| 44 | + 1, |
| 45 | + ) |
| 46 | + |
| 47 | + def test_uint8_dispatch(self) -> None: |
| 48 | + """Test uint8 x uint8 inputs should dispatch to asym8uxasym8u_asym8u variant""" |
| 49 | + x = torch.randint(0, 255, (2, 3), dtype=torch.uint8) |
| 50 | + w = torch.randint(0, 255, (4, 3), dtype=torch.uint8) |
| 51 | + b = torch.randint(-2147483648, 2147483647, (4,), dtype=torch.int32) |
| 52 | + gm = single_op_builder( |
| 53 | + placeholders=(x, w, b), |
| 54 | + op=exir_ops.edge.cadence.quantized_fully_connected.per_tensor, |
| 55 | + args=(x, w, b, 0, 0, 1, 0, 0, None), |
| 56 | + ) |
| 57 | + p = CompileTimeTypeDispatchPass() |
| 58 | + gm = cast(PassResult, p(gm)).graph_module |
| 59 | + # Original op should be replaced |
| 60 | + self.assertEqual( |
| 61 | + count_node(gm, exir_ops.edge.cadence.quantized_fully_connected.per_tensor), |
| 62 | + 0, |
| 63 | + ) |
| 64 | + # Should be replaced with uint8 specific variant |
| 65 | + self.assertEqual( |
| 66 | + count_node( |
| 67 | + gm, |
| 68 | + exir_ops.edge.cadence.quantized_fully_connected_asym8uxasym8u_asym8u.per_tensor, |
| 69 | + ), |
| 70 | + 1, |
| 71 | + ) |
| 72 | + |
| 73 | + def test_mixed_types_error(self) -> None: |
| 74 | + """Test mixed int8/uint8 inputs should raise RuntimeError""" |
| 75 | + x = torch.randint(-128, 127, (2, 3), dtype=torch.int8) |
| 76 | + w = torch.randint(0, 255, (4, 3), dtype=torch.uint8) |
| 77 | + b = torch.randint(-2147483648, 2147483647, (4,), dtype=torch.int32) |
| 78 | + gm = single_op_builder( |
| 79 | + placeholders=(x, w, b), |
| 80 | + op=exir_ops.edge.cadence.quantized_fully_connected.per_tensor, |
| 81 | + args=(x, w, b, 0, 0, 1, 0, 0, None), |
| 82 | + ) |
| 83 | + p = CompileTimeTypeDispatchPass() |
| 84 | + # Mixed types should raise RuntimeError |
| 85 | + with self.assertRaises(RuntimeError) as context: |
| 86 | + cast(PassResult, p(gm)).graph_module |
| 87 | + self.assertIn("Unsupported input types", str(context.exception)) |
0 commit comments