|
| 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 | +import unittest |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from executorch.devtools.backend_debug import get_delegation_info |
| 12 | +from executorch.examples.models.llama.export_llama_lib import _export_llama, build_args_parser |
| 13 | + |
| 14 | + |
| 15 | +# Ops expected to be found in the default exported llama_transformer. Obtained through |
| 16 | +# print_delegation_info from the backend_debug module, which is displayed with |
| 17 | +# export_llama under --verbose. |
| 18 | +BASE_EXPECTED_OPS = { |
| 19 | + "sym_size": 1, |
| 20 | + "alloc": 288, |
| 21 | + "aten_embedding_default": 1, |
| 22 | + "aten_select_copy_int": 12, |
| 23 | + "_local_scalar_dense": 11, |
| 24 | + "add": 1, |
| 25 | + "aten_slice_copy_tensor": 23, |
| 26 | + "aten_mul_tensor": 83, |
| 27 | + "aten_mean_dim": 11, |
| 28 | + "aten_add_tensor": 31, |
| 29 | + "aten_rsqrt_default": 11, |
| 30 | + "aten_linear_default": 36, |
| 31 | + "aten_view_copy_default": 40, |
| 32 | + "aten_squeeze_copy_dims": 20, |
| 33 | + "aten_sub_tensor": 10, |
| 34 | + "aten_unsqueeze_copy_default": 20, |
| 35 | + "aten_cat_default": 10, |
| 36 | + "update_cache": 10, |
| 37 | + "llama_custom_sdpa_default": 5, |
| 38 | + "aten_sigmoid_default": 5, |
| 39 | +} |
| 40 | + |
| 41 | +UNWANTED_OPS = [ |
| 42 | + "aten_permute_copy_default", |
| 43 | +] |
| 44 | + |
| 45 | +class ExportLlamaLibTest(unittest.TestCase): |
| 46 | + def test_has_expected_ops_and_op_counts(self): |
| 47 | + """ |
| 48 | + Tests that the presence of expected ops and counts for each op are |
| 49 | + do not change. |
| 50 | +
|
| 51 | + Serves as a proxy for a performance regression test, as performance |
| 52 | + is directly tied to which and how many of each ops are in the graph. |
| 53 | +
|
| 54 | + If this test breaks, please ensure that the difference in ops |
| 55 | + is intentional before updating the expected ops. |
| 56 | + """ |
| 57 | + # Since we aren't loading a checkpoint, it doesn't |
| 58 | + # matter what model we specify. Note that |
| 59 | + # we cannot test quantization args in this way |
| 60 | + # since quantization requires promoting meta tensors |
| 61 | + # to the cpu device, which requires real weights. |
| 62 | + export_args_str = """ |
| 63 | + --use_sdpa_with_kv_cache |
| 64 | + -kv |
| 65 | + --verbose |
| 66 | + """ |
| 67 | + args_list = export_args_str.strip().split() |
| 68 | + parser = build_args_parser() |
| 69 | + args = parser.parse_args(args_list) |
| 70 | + |
| 71 | + builder = _export_llama(args) |
| 72 | + graph_module = builder.edge_manager.exported_program().graph_module |
| 73 | + delegation_info = get_delegation_info(graph_module) |
| 74 | + |
| 75 | + for op, op_info in delegation_info.delegation_by_operator.items(): |
| 76 | + self.assertTrue(op in BASE_EXPECTED_OPS) |
| 77 | + self.assertTrue(op not in UNWANTED_OPS) |
| 78 | + self.assertEqual(op_info.non_delegated, BASE_EXPECTED_OPS[op]) |
| 79 | + |
| 80 | + self.assertEqual(len(delegation_info.delegation_by_operator), len(BASE_EXPECTED_OPS)) |
| 81 | + |
0 commit comments