|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 | +"""Tests that importing the Qualcomm backend package has no side effects. |
| 8 | +
|
| 9 | +The package's __init__ used to do real work at import time. Merely importing it fetched the |
| 10 | +SDK and libc++ over the network, could re-execute the interpreter under a staged loader, |
| 11 | +rewrote QNN_SDK_ROOT and LD_LIBRARY_PATH, raised on a machine with no network, and disabled |
| 12 | +PyTorch's MKLDNN backend for the whole process on any AMD host. None of that is needed to |
| 13 | +load the package: the native adaptor resolves QNN symbols with dlopen when a model is |
| 14 | +compiled, so setup belongs on the paths that compile. |
| 15 | +
|
| 16 | +These are unit tests because the behaviour under test is what happens during import, which |
| 17 | +is observable without a Qualcomm SDK or a Qualcomm device. |
| 18 | +""" |
| 19 | + |
| 20 | +import ast |
| 21 | +import importlib |
| 22 | +import sys |
| 23 | +import types |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +import executorch.backends.qualcomm as qnn |
| 27 | +import pytest |
| 28 | +import torch |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def fake_cpuinfo(monkeypatch): |
| 33 | + """Replaces cpuinfo so a vendor can be chosen without an AMD host.""" |
| 34 | + |
| 35 | + def install(vendor): |
| 36 | + module = types.ModuleType("cpuinfo") |
| 37 | + module.get_cpu_info = lambda: {"vendor_id_raw": vendor} |
| 38 | + monkeypatch.setitem(sys.modules, "cpuinfo", module) |
| 39 | + |
| 40 | + return install |
| 41 | + |
| 42 | + |
| 43 | +def test_importing_the_package_leaves_mkldnn_alone(monkeypatch, fake_cpuinfo): |
| 44 | + """Import must not change a global PyTorch setting, even on an AMD host. |
| 45 | +
|
| 46 | + The setting affects every model in the interpreter, not just a Qualcomm one, so an import |
| 47 | + that flips it changes how unrelated code runs. |
| 48 | +
|
| 49 | + A reload is used deliberately: it re-executes every module level statement, which is exactly |
| 50 | + what an import does, and the stubbed cpuinfo is read inside those statements rather than |
| 51 | + bound at import, so unlike a stubbed installer it survives. |
| 52 | + """ |
| 53 | + fake_cpuinfo("AuthenticAMD") |
| 54 | + monkeypatch.setattr(torch.backends.mkldnn, "enabled", True) |
| 55 | + |
| 56 | + importlib.reload(qnn) |
| 57 | + |
| 58 | + assert torch.backends.mkldnn.enabled |
| 59 | + |
| 60 | + |
| 61 | +def test_importing_the_package_does_not_set_up_the_sdk(): |
| 62 | + """Import must not run the installer, which downloads and rewrites the environment. |
| 63 | +
|
| 64 | + Asserted against the module source rather than by importing with a stub in place. A reload |
| 65 | + re-executes the module, which rebinds the real installer and discards any stub, so a stubbed |
| 66 | + reload can only ever observe an empty call list and would pass even if the call came back. |
| 67 | + What actually matters is that no module level statement calls it. |
| 68 | + """ |
| 69 | + module = ast.parse(Path(qnn.__file__).read_text()) |
| 70 | + called_at_module_level = { |
| 71 | + node.func.id |
| 72 | + for statement in module.body |
| 73 | + if isinstance(statement, ast.Expr) |
| 74 | + for node in ast.walk(statement) |
| 75 | + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) |
| 76 | + } |
| 77 | + |
| 78 | + assert "install_qnn_sdk" not in called_at_module_level |
| 79 | + assert "setup_qnn_sdk" not in called_at_module_level |
| 80 | + # A guard on the two above, which would pass just as happily if the parse found nothing. |
| 81 | + assert any( |
| 82 | + isinstance(statement, ast.FunctionDef) and statement.name == "setup_qnn_sdk" |
| 83 | + for statement in module.body |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def test_setup_is_idempotent(monkeypatch): |
| 88 | + """The compile paths each call it, so only the first call may do the work.""" |
| 89 | + calls = [] |
| 90 | + monkeypatch.setattr(qnn, "install_qnn_sdk", lambda: calls.append(1) or True) |
| 91 | + monkeypatch.setattr(qnn, "is_linux_x86", lambda: True) |
| 92 | + monkeypatch.setattr(qnn, "_sdk_ready", False) |
| 93 | + monkeypatch.delenv("QNN_SDK_ROOT", raising=False) |
| 94 | + monkeypatch.delenv("EXECUTORCH_BUILDING_WHEEL", raising=False) |
| 95 | + |
| 96 | + qnn.setup_qnn_sdk() |
| 97 | + qnn.setup_qnn_sdk() |
| 98 | + |
| 99 | + assert len(calls) == 1 |
| 100 | + |
| 101 | + |
| 102 | +def test_setup_honours_a_preinstalled_sdk(monkeypatch): |
| 103 | + calls = [] |
| 104 | + monkeypatch.setattr(qnn, "install_qnn_sdk", lambda: calls.append(1) or True) |
| 105 | + monkeypatch.setattr(qnn, "_sdk_ready", False) |
| 106 | + monkeypatch.setenv("QNN_SDK_ROOT", "/opt/qcom/sdk") |
| 107 | + |
| 108 | + qnn.setup_qnn_sdk() |
| 109 | + |
| 110 | + assert not calls |
| 111 | + |
| 112 | + |
| 113 | +def test_setup_runs_once_under_concurrent_callers(monkeypatch): |
| 114 | + """Several modules call it, so two threads can reach it at the same time. |
| 115 | +
|
| 116 | + Without a lock they both see the flag unset and both run the installer, which downloads an |
| 117 | + SDK and rewrites the environment. The import lock does not cover this, because the calls |
| 118 | + come from different modules rather than from one package __init__. |
| 119 | + """ |
| 120 | + import threading |
| 121 | + import time |
| 122 | + |
| 123 | + calls = [] |
| 124 | + |
| 125 | + def slow_install(): |
| 126 | + # Widen the window between the check and the flag being set, so an unlocked version |
| 127 | + # fails reliably rather than occasionally. |
| 128 | + time.sleep(0.2) |
| 129 | + calls.append(1) |
| 130 | + return True |
| 131 | + |
| 132 | + monkeypatch.setattr(qnn, "install_qnn_sdk", slow_install) |
| 133 | + monkeypatch.setattr(qnn, "is_linux_x86", lambda: True) |
| 134 | + monkeypatch.setattr(qnn, "_sdk_ready", False) |
| 135 | + monkeypatch.delenv("QNN_SDK_ROOT", raising=False) |
| 136 | + monkeypatch.delenv("EXECUTORCH_BUILDING_WHEEL", raising=False) |
| 137 | + |
| 138 | + threads = [threading.Thread(target=qnn.setup_qnn_sdk) for _ in range(8)] |
| 139 | + for thread in threads: |
| 140 | + thread.start() |
| 141 | + for thread in threads: |
| 142 | + thread.join(timeout=30) |
| 143 | + |
| 144 | + # Reported before the count, so a hung thread says so rather than showing a confusing |
| 145 | + # number of installer calls. |
| 146 | + assert not any(thread.is_alive() for thread in threads) |
| 147 | + assert len(calls) == 1 |
| 148 | + |
| 149 | + |
| 150 | +def test_setup_reports_a_failed_install(monkeypatch): |
| 151 | + """A failure has to name the two ways out, since it cannot be resolved automatically.""" |
| 152 | + monkeypatch.setattr(qnn, "install_qnn_sdk", lambda: False) |
| 153 | + monkeypatch.setattr(qnn, "is_linux_x86", lambda: True) |
| 154 | + monkeypatch.setattr(qnn, "_sdk_ready", False) |
| 155 | + monkeypatch.delenv("QNN_SDK_ROOT", raising=False) |
| 156 | + monkeypatch.delenv("EXECUTORCH_BUILDING_WHEEL", raising=False) |
| 157 | + |
| 158 | + with pytest.raises(RuntimeError, match="QNN_SDK_ROOT"): |
| 159 | + qnn.setup_qnn_sdk() |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.parametrize( |
| 163 | + "vendor,expected", |
| 164 | + [ |
| 165 | + # The vendor string an AMD host reports. MKLDNN produces wrong results there. |
| 166 | + ("AuthenticAMD", False), |
| 167 | + ("GenuineIntel", True), |
| 168 | + # A host that reports nothing, such as Apple silicon. |
| 169 | + ("", True), |
| 170 | + ], |
| 171 | +) |
| 172 | +def test_mkldnn_is_disabled_only_on_amd(monkeypatch, fake_cpuinfo, vendor, expected): |
| 173 | + fake_cpuinfo(vendor) |
| 174 | + monkeypatch.setattr(torch.backends.mkldnn, "enabled", True) |
| 175 | + |
| 176 | + qnn.disable_mkldnn_on_amd() |
| 177 | + |
| 178 | + assert torch.backends.mkldnn.enabled is expected |
0 commit comments