|
| 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 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.xnnpack import get_xnnpack_recipe |
| 13 | +from executorch.exir.schema import DelegateCall, Program |
| 14 | +from executorch.export import export |
| 15 | +from torch import nn |
| 16 | +from torch.testing._internal.common_quantization import TestHelperModules |
| 17 | + |
| 18 | + |
| 19 | +class TestXnnpackRecipes(unittest.TestCase): |
| 20 | + def setUp(self) -> None: |
| 21 | + super().setUp() |
| 22 | + |
| 23 | + def tearDown(self) -> None: |
| 24 | + super().tearDown() |
| 25 | + |
| 26 | + def check_fully_delegated(self, program: Program) -> None: |
| 27 | + instructions = program.execution_plan[0].chains[0].instructions |
| 28 | + assert instructions is not None |
| 29 | + self.assertEqual(len(instructions), 1) |
| 30 | + self.assertIsInstance(instructions[0].instr_args, DelegateCall) |
| 31 | + |
| 32 | + def test_basic_recipe(self) -> None: |
| 33 | + m_eager = TestHelperModules.TwoLinearModule().eval() |
| 34 | + example_inputs = [(torch.randn(9, 8),)] |
| 35 | + session = export( |
| 36 | + model=m_eager, |
| 37 | + example_inputs=example_inputs, |
| 38 | + export_recipe=get_xnnpack_recipe("FP32_CPU_ACCELERATED_RECIPE"), |
| 39 | + ) |
| 40 | + self.assertTrue( |
| 41 | + torch.allclose( |
| 42 | + session.run_method("forward", example_inputs[0])[0], |
| 43 | + m_eager(*example_inputs[0]), |
| 44 | + ) |
| 45 | + ) |
| 46 | + self.check_fully_delegated(session.get_executorch_program()) |
| 47 | + |
| 48 | + def test_dynamic_quant_recipe(self) -> None: |
| 49 | + with torch.no_grad(): |
| 50 | + m_eager = TestHelperModules.TwoLinearModule().eval() |
| 51 | + example_inputs = [(torch.randn(9, 8),)] |
| 52 | + session = export( |
| 53 | + model=m_eager, |
| 54 | + example_inputs=example_inputs, |
| 55 | + export_recipe=get_xnnpack_recipe( |
| 56 | + "DYNAMIC_QUANT_CPU_ACCELERATED_RECIPE" |
| 57 | + ), |
| 58 | + ) |
| 59 | + self.assertTrue( |
| 60 | + torch.allclose( |
| 61 | + session.run_method("forward", example_inputs[0])[0], |
| 62 | + m_eager(*example_inputs[0]), |
| 63 | + atol=1e-1, |
| 64 | + ) |
| 65 | + ) |
| 66 | + self.check_fully_delegated(session.get_executorch_program()) |
| 67 | + |
| 68 | + def test_8a4w_recipe(self) -> None: |
| 69 | + class SimpleLinearModel(nn.Module): |
| 70 | + def __init__(self) -> None: |
| 71 | + super(SimpleLinearModel, self).__init__() |
| 72 | + self.layer1 = nn.Linear(32, 2) |
| 73 | + |
| 74 | + def forward(self, x) -> torch.Tensor: |
| 75 | + x = self.layer1(x) |
| 76 | + return x |
| 77 | + |
| 78 | + model = SimpleLinearModel() |
| 79 | + example_inputs = [(torch.randn(1, 32),)] |
| 80 | + session = export( |
| 81 | + model=model, |
| 82 | + example_inputs=example_inputs, |
| 83 | + export_recipe=get_xnnpack_recipe( |
| 84 | + "8A4W_CPU_ACCELERATED_RECIPE", group_size=32 |
| 85 | + ), |
| 86 | + ) |
| 87 | + self.assertTrue( |
| 88 | + torch.allclose( |
| 89 | + session.run_method("forward", example_inputs[0])[0], |
| 90 | + model(*example_inputs[0]), |
| 91 | + atol=1e-1, |
| 92 | + ) |
| 93 | + ) |
| 94 | + self.check_fully_delegated(session.get_executorch_program()) |
0 commit comments