|
| 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 | +from __future__ import annotations |
| 8 | + |
| 9 | +import unittest |
| 10 | + |
| 11 | +from executorch.backends.qualcomm.serialization.qc_schema import ( |
| 12 | + QcomChipset, |
| 13 | + QnnExecuTorchBackendType, |
| 14 | +) |
| 15 | +from executorch.backends.qualcomm.serialization.qc_schema_serialize import ( |
| 16 | + flatbuffer_to_option, |
| 17 | +) |
| 18 | +from executorch.backends.qualcomm.utils.utils import ( |
| 19 | + generate_gpu_compiler_spec, |
| 20 | + generate_htp_compiler_spec, |
| 21 | + generate_qnn_executorch_compiler_spec, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class QnnCompilerSpecTest(unittest.TestCase): |
| 26 | + def test_fcb_pairs_soc_and_htp_options(self): |
| 27 | + first = generate_htp_compiler_spec(use_fp16=False) |
| 28 | + second = generate_htp_compiler_spec(use_fp16=True) |
| 29 | + option = flatbuffer_to_option( |
| 30 | + generate_qnn_executorch_compiler_spec( |
| 31 | + soc_model=[QcomChipset.SM8650, QcomChipset.SM8750], |
| 32 | + backend_options=[first, second], |
| 33 | + )[0].value |
| 34 | + ) |
| 35 | + |
| 36 | + self.assertTrue(option.target_options.fcb_reference_weight_sharing) |
| 37 | + self.assertEqual( |
| 38 | + [target.soc_info.soc_model for target in option.target_options.targets], |
| 39 | + [QcomChipset.SM8650, QcomChipset.SM8750], |
| 40 | + ) |
| 41 | + self.assertEqual( |
| 42 | + [ |
| 43 | + target.backend_options[0].htp_options.precision |
| 44 | + for target in option.target_options.targets |
| 45 | + ], |
| 46 | + [first.htp_options.precision, second.htp_options.precision], |
| 47 | + ) |
| 48 | + |
| 49 | + def test_fcb_rejects_invalid_target_configuration(self): |
| 50 | + htp = generate_htp_compiler_spec(use_fp16=False) |
| 51 | + cases = [ |
| 52 | + ( |
| 53 | + { |
| 54 | + "soc_model": [], |
| 55 | + "backend_options": [], |
| 56 | + }, |
| 57 | + "must not be empty", |
| 58 | + ), |
| 59 | + ( |
| 60 | + { |
| 61 | + "soc_model": [QcomChipset.SM8650, QcomChipset.SM8750], |
| 62 | + "backend_options": [htp], |
| 63 | + }, |
| 64 | + "equal-length", |
| 65 | + ), |
| 66 | + ( |
| 67 | + { |
| 68 | + "soc_model": [QcomChipset.SM8650, QcomChipset.SM8750], |
| 69 | + "backend_options": [htp, generate_gpu_compiler_spec()], |
| 70 | + }, |
| 71 | + "HTP", |
| 72 | + ), |
| 73 | + ( |
| 74 | + { |
| 75 | + "soc_model": [QcomChipset.SM8650, QcomChipset.SM8750], |
| 76 | + "backend_options": [htp, htp], |
| 77 | + "online_prepare": True, |
| 78 | + }, |
| 79 | + "offline_prepare", |
| 80 | + ), |
| 81 | + ( |
| 82 | + { |
| 83 | + "soc_model": QcomChipset.SM8650, |
| 84 | + "backend_options": [htp, htp], |
| 85 | + }, |
| 86 | + "equal-length", |
| 87 | + ), |
| 88 | + ( |
| 89 | + { |
| 90 | + "soc_model": [QcomChipset.SM8650, QcomChipset.SM8750], |
| 91 | + "backend_options": htp, |
| 92 | + }, |
| 93 | + "equal-length", |
| 94 | + ), |
| 95 | + ] |
| 96 | + for kwargs, message in cases: |
| 97 | + with self.subTest(kwargs=kwargs), self.assertRaisesRegex( |
| 98 | + ValueError, message |
| 99 | + ): |
| 100 | + generate_qnn_executorch_compiler_spec(**kwargs) |
| 101 | + |
| 102 | + def test_fcb_warns_for_dlbc_when_reference_sharing_enabled(self): |
| 103 | + htp = generate_htp_compiler_spec(use_fp16=False, use_dlbc=True) |
| 104 | + with self.assertWarnsRegex(UserWarning, "DLBC"): |
| 105 | + specs = generate_qnn_executorch_compiler_spec( |
| 106 | + soc_model=[QcomChipset.SM8650, QcomChipset.SM8750], |
| 107 | + backend_options=[htp, htp], |
| 108 | + ) |
| 109 | + self.assertGreater(len(specs), 0) |
| 110 | + |
| 111 | + def test_single_target_remains_non_fcb(self): |
| 112 | + htp = generate_htp_compiler_spec(use_fp16=False) |
| 113 | + for soc_model, backend_options in ( |
| 114 | + (QcomChipset.SM8650, htp), |
| 115 | + ([QcomChipset.SM8650], [htp]), |
| 116 | + (QcomChipset.SM8650, [htp]), |
| 117 | + ([QcomChipset.SM8650], htp), |
| 118 | + ): |
| 119 | + with self.subTest(soc_model=soc_model): |
| 120 | + option = flatbuffer_to_option( |
| 121 | + generate_qnn_executorch_compiler_spec( |
| 122 | + soc_model=soc_model, |
| 123 | + backend_options=backend_options, |
| 124 | + )[0].value |
| 125 | + ) |
| 126 | + self.assertIsNone(option.target_options) |
| 127 | + self.assertEqual( |
| 128 | + option.backend_options.backend_type, |
| 129 | + QnnExecuTorchBackendType.kHtpBackend, |
| 130 | + ) |
0 commit comments