|
| 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 | +"""Install a repaired Native wheel into a clean environment and test GGML.""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +import tempfile |
| 14 | +import venv |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +def main() -> None: |
| 19 | + parser = argparse.ArgumentParser(description=__doc__) |
| 20 | + parser.add_argument("--wheel", type=Path, required=True) |
| 21 | + parser.add_argument("--source", type=Path, required=True) |
| 22 | + parser.add_argument("--fixtures", type=Path, required=True) |
| 23 | + args = parser.parse_args() |
| 24 | + wheel = args.wheel.resolve() |
| 25 | + source = args.source.resolve() |
| 26 | + fixtures = args.fixtures.resolve() |
| 27 | + with tempfile.TemporaryDirectory(prefix="native-wheel-test-") as directory: |
| 28 | + work = Path(directory) |
| 29 | + venv.create(work / "venv", with_pip=True) |
| 30 | + python = work / "venv/bin/python" |
| 31 | + env = os.environ.copy() |
| 32 | + for name in ("PYTHONPATH", "PYTHONHOME", "LD_LIBRARY_PATH", "LD_PRELOAD"): |
| 33 | + env.pop(name, None) |
| 34 | + env["PYTHONNOUSERSITE"] = "1" |
| 35 | + subprocess.run( |
| 36 | + [str(python), "-m", "pip", "install", str(wheel)], |
| 37 | + cwd=work, |
| 38 | + env=env, |
| 39 | + check=True, |
| 40 | + timeout=600, |
| 41 | + ) |
| 42 | + subprocess.run( |
| 43 | + [str(python), "-m", "pip", "check"], |
| 44 | + cwd=work, |
| 45 | + env=env, |
| 46 | + check=True, |
| 47 | + timeout=60, |
| 48 | + ) |
| 49 | + shutil.copytree(fixtures, work / "fixtures") |
| 50 | + shutil.copyfile( |
| 51 | + source / "backends/native/pybindings/test/test_bindings.py", |
| 52 | + work / "test_bindings.py", |
| 53 | + ) |
| 54 | + subprocess.run( |
| 55 | + [ |
| 56 | + str(python), |
| 57 | + "-I", |
| 58 | + "-c", |
| 59 | + """ |
| 60 | +import importlib.metadata |
| 61 | +import importlib.util |
| 62 | +from pathlib import Path |
| 63 | +import numpy as np |
| 64 | +from executorch_native import Model |
| 65 | +
|
| 66 | +for name in ('torch', 'executorch'): |
| 67 | + assert importlib.util.find_spec(name) is None, name |
| 68 | +installed = {d.metadata['Name'].lower() for d in importlib.metadata.distributions()} |
| 69 | +assert installed <= {'pip', 'setuptools', 'executorch-native', 'numpy'}, installed |
| 70 | +for name in ('add', 'conv_bn'): |
| 71 | + package = Path('fixtures') / f'{name}.ptn' |
| 72 | + with np.load(package.with_suffix('.npz')) as data: |
| 73 | + session = Model.load(package).create_session('ggml') |
| 74 | + (actual,) = session.run([data['input']]) |
| 75 | + np.testing.assert_allclose(actual, data['expected'], rtol=1e-4, atol=1e-4) |
| 76 | + print(f'PASS: {package.name} with GGML') |
| 77 | +""", |
| 78 | + ], |
| 79 | + cwd=work, |
| 80 | + env=env, |
| 81 | + check=True, |
| 82 | + timeout=120, |
| 83 | + ) |
| 84 | + env["NATIVE_TEST_PACKAGE"] = str(work / "fixtures/add.ptn") |
| 85 | + env["NATIVE_TEST_ENGINE"] = "ggml" |
| 86 | + subprocess.run( |
| 87 | + [str(python), "-m", "unittest", "-v", "test_bindings"], |
| 88 | + cwd=work, |
| 89 | + env=env, |
| 90 | + check=True, |
| 91 | + timeout=300, |
| 92 | + ) |
| 93 | + print(f"PASS: clean install of {wheel.name}") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments