|
6 | 6 | """Utilities for converting and verifying PyTorch models for export testing.""" |
7 | 7 |
|
8 | 8 | import asyncio |
| 9 | +import platform |
9 | 10 | import sys |
10 | 11 | import tempfile |
11 | 12 | from abc import ABC, abstractmethod |
|
25 | 26 | from coreai_opt import CoreMLExportError, ExportBackend |
26 | 27 | from tests.test_utils.general import verify_snr_psnr as _verify_snr_psnr |
27 | 28 |
|
| 29 | +if platform.system() == "Darwin": |
| 30 | + from coreai.runtime import ComputeUnitKind, SpecializationOptions |
| 31 | + |
28 | 32 | # Substring of the dtype guard message raised by the CoreML export validation. Shared so |
29 | 33 | # test files asserting the rejection don't drift from one another. |
30 | 34 | COREML_DTYPE_REJECTION_MATCH = "CoreML export does not support" |
31 | 35 |
|
| 36 | +# Compute unit selection driven by the --compute-unit-kind pytest option (see |
| 37 | +# tests/conftest.py). Default is "interpreter" so a plain `pytest` run uses the |
| 38 | +# bundled runtime. |
| 39 | +_COMPUTE_UNIT_KIND: str = "interpreter" |
| 40 | + |
| 41 | + |
| 42 | +def set_test_compute_unit_kind(name: str) -> None: |
| 43 | + """Set the compute unit used by ``MLIRConverter`` inference. |
| 44 | +
|
| 45 | + Called from tests/conftest.py::pytest_configure based on --compute-unit-kind. |
| 46 | +
|
| 47 | + Args: |
| 48 | + name (str): One of "interpreter", "cpu", "gpu", or "neural_engine". |
| 49 | + """ |
| 50 | + global _COMPUTE_UNIT_KIND |
| 51 | + _COMPUTE_UNIT_KIND = name |
| 52 | + |
| 53 | + |
| 54 | +def _get_test_specialization_options() -> "SpecializationOptions | None": |
| 55 | + """Translate the configured compute unit into ``SpecializationOptions`` (or None). |
| 56 | +
|
| 57 | + On non-macOS platforms only ``interpreter`` is supported — the runtime does |
| 58 | + not expose ``SpecializationOptions`` outside Darwin. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + SpecializationOptions | None: ``None`` for the interpreter (bundled |
| 62 | + runtime); otherwise the options selecting the requested delegate. |
| 63 | +
|
| 64 | + Raises: |
| 65 | + RuntimeError: If a real compute unit is requested off macOS. |
| 66 | + ValueError: If the configured compute unit kind is unknown. |
| 67 | + """ |
| 68 | + if _COMPUTE_UNIT_KIND == "interpreter": |
| 69 | + return None |
| 70 | + if platform.system() != "Darwin": |
| 71 | + msg = ( |
| 72 | + f"--compute-unit-kind={_COMPUTE_UNIT_KIND} is only supported on macOS; " |
| 73 | + "use --compute-unit-kind=interpreter on this platform." |
| 74 | + ) |
| 75 | + raise RuntimeError(msg) |
| 76 | + if _COMPUTE_UNIT_KIND == "cpu": |
| 77 | + return SpecializationOptions.cpu_only() |
| 78 | + if _COMPUTE_UNIT_KIND == "gpu": |
| 79 | + return SpecializationOptions.from_preferred_compute_unit_kind( |
| 80 | + compute_unit_kind=ComputeUnitKind.gpu(), |
| 81 | + ) |
| 82 | + if _COMPUTE_UNIT_KIND == "neural_engine": |
| 83 | + return SpecializationOptions.from_preferred_compute_unit_kind( |
| 84 | + compute_unit_kind=ComputeUnitKind.neural_engine(), |
| 85 | + ) |
| 86 | + msg = f"Unknown compute unit kind: {_COMPUTE_UNIT_KIND!r}" |
| 87 | + raise ValueError(msg) |
| 88 | + |
32 | 89 |
|
33 | 90 | def assert_coreml_finalize_rejects_unsupported_dtype(finalizer: Any) -> None: |
34 | 91 | """Assert ``finalizer.finalize(backend=CoreML)`` rejects an unsupported dtype. |
@@ -403,7 +460,9 @@ async def _run_inference_async( |
403 | 460 | suffix=".aimodel", |
404 | 461 | ) as tmpdir: |
405 | 462 | asset = converted_model.save_asset(Path(tmpdir)) |
406 | | - async with asset.executable() as ai_model: |
| 463 | + async with asset.executable( |
| 464 | + specialization_options=_get_test_specialization_options(), |
| 465 | + ) as ai_model: |
407 | 466 | rt_func = ai_model.load_function("main") |
408 | 467 |
|
409 | 468 | input_names = rt_func.desc.input_names |
|
0 commit comments